Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADP-3372] Deposit Wallet - json roundtrips for types #4710

Merged
merged 7 commits into from
Aug 2, 2024

Conversation

paweljakubas
Copy link
Contributor

  • Adding roundtrip property
  • Customer type relocation
  • Roundtrip for ApiT Address
  • Roundtrip for ApiT Customer
  • Roundtrip for ApiT CustomerList

Comments

Issue Number

adp-3372

@paweljakubas paweljakubas self-assigned this Jul 31, 2024
Copy link
Contributor

@HeinrichApfelmus HeinrichApfelmus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, thank you! 😊 However, I have a request — please do not move the Customer type, it's not part of the Read hierarchy and also not an abstract data type. See comments for more info.

Comment on lines 17 to 19
, Customer
, fromRawCustomer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, Customer
, fromRawCustomer

Wait — the concern of the Read modules is to read data as specified in the ledger specification, and as it appears on the blockchain, but Customer is not part of that. 😅 (The name Deposit is accidental, I want to merge this module with Cardano.Wallet.Read but had to choose a different name here in order to avoid a name clash.)

Could you leave the Customer type where it is, and instead solve the Word32 → Customer conversion directly in the Cardano.Wallet.Deposit.HTTP.JSON.JSONSpec module? 🤓

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it - reverted the commit


prop_jsonRoundtrip :: (Eq a, Show a, FromJSON a, ToJSON a) => a -> Property
prop_jsonRoundtrip val =
decode (encode val) === Just val
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 😊


instance Arbitrary (ApiT Customer) where
arbitrary =
ApiT . fromRawCustomer <$> arbitrary
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ApiT . fromRawCustomer <$> arbitrary
ApiT <$> chooseBoundedIntegral

🤓 On the matter of the Word32 → Customer conversion: Somewhat unusually, I have chose to make Customer a concrete type rather than an abstract data type — specifically, I have defined Customer = Word31. In other words, the deposit wallet will only support customer IDs in the range 0 … 2^31-1!

This is intentional, and due to the BIP-32 address derivation, where the index for one derivation step is exactly a Word31. I could have used longer indices, but I decided to keep this limit for compatibility with hardware wallets; I don't want to explain derivations with arbitrary length. Hence, Customer = Word31 is baked into the Deposit Wallet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introduced fromRawCustomer that creates Customer from Word31 and added Arbitrary Word31.

Comment on lines +73 to +80
listLen <- chooseInt (0, 100)
let genPair = (,) <$> (unApiT <$> arbitrary) <*> (unApiT <$> arbitrary)
ApiT <$> vectorOf listLen genPair
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
listLen <- chooseInt (0, 100)
let genPair = (,) <$> (unApiT <$> arbitrary) <*> (unApiT <$> arbitrary)
ApiT <$> vectorOf listLen genPair
let genPair = (,) <$> (unApiT <$> arbitrary) <*> (unApiT <$> arbitrary)
ApiT <$> listOf genPair

Perhaps? listOf depends on the QuickCheck size parameter, which usually composes better. But since we don't care about composability here, vectorOf is also fine. I leave this up to you.

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-3372/json-roundtrips branch from bffed90 to d72ceda Compare August 2, 2024 12:57
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-3372/json-roundtrips branch from d72ceda to 959531b Compare August 2, 2024 13:10
Copy link
Contributor

@HeinrichApfelmus HeinrichApfelmus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thank you! 😊

Minor request on the definition of fromRawCustomer remaining.

(Also, could you squash the commits a little bit? I.e. work post revert changes into the earlier commits.)

@@ -135,6 +136,9 @@ isCustomerAddress :: Address -> WalletState -> Bool
isCustomerAddress address =
flip Address.isCustomerAddress (Read.toRawAddress address) . addresses

fromRawCustomer :: Word31 -> Customer
fromRawCustomer = fromIntegral
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fromRawCustomer = fromIntegral
fromRawCustomer = id

As much as I love abstract data types — this one is positively a type synonym. 🤓

https://github.com/cardano-foundation/cardano-wallet-agda/blob/5a24dca9b1d884688483ad78a00b8f7485d522a4/lib/customer-deposit-wallet-pure/agda/Cardano/Wallet/Deposit/Pure/Address.agda#L82

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed! done

@paweljakubas paweljakubas enabled auto-merge August 2, 2024 14:01
@paweljakubas paweljakubas added this pull request to the merge queue Aug 2, 2024
Merged via the queue into master with commit bffb2d7 Aug 2, 2024
22 checks passed
@paweljakubas paweljakubas deleted the paweljakubas/adp-3372/json-roundtrips branch August 2, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants