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

Translate subset of Wallet API to Servant #76

Merged
merged 52 commits into from
Mar 21, 2019

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented Mar 18, 2019

Translate a subset of the Wallet API to Servant. (See Issue #53)

Included in this PR

This PR:

  • translates a small number of endpoints within the wallets namespace.
  • translates all types associated with the above endpoints.
  • provides JSON encodings that (should) match the Swagger type definitions.
  • provides round-trip validation for all types.
  • provides round-trip tests for JSON serialization and deserialization.

Not included in this PR

This PR:

  • doesn't attempt to encode the entire API. Instead, a small subset is encoded.
  • doesn't attempt to automatically verify that the Servant definition matches the Swagger definition.

Design choices

This PR makes some design choices, which should be reviewed.

1. Place each type in a separate module

Reasons:

  • Better encapulation and type safety.
    Example: WalletName cannot be constructed without a valid name.
  • GHC can parallelize builds of types within separate modules.

2. Favour simple generic JSON encoding

Manually-written JSON code can be tricky to debug.

This PR makes the choice of using generically-derived ToJSON and FromJSON instances wherever possible.

Whenever there's a non-trivial mapping between a Haskell type and a desired encoding, we encode the desired structure in a new Haskell type that's closer to the desired encoding, and write a pair of conversion functions that can easily be checked by the compiler.

For example: WalletState provides an idiomatic Haskell encoding of a wallet's restoration state, but we also provide UnvalidatedWalletState as a type that is more trivial to encode as JSON. We provide validate and unvalidate to convert between these two types.

@jonathanknowles jonathanknowles changed the title Jonathanknowles/wallet api servant Translate subset of Wallet API to Servant Mar 18, 2019
@jonathanknowles jonathanknowles self-assigned this Mar 18, 2019
@rvl rvl added this to the Wallet Layer Integration milestone Mar 18, 2019
KtorZ
KtorZ previously requested changes Mar 18, 2019
src/Cardano/Wallet/Api/V2/Types/CurrencyUnit.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/V2/Lenses.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/V2/Types/WalletAddressPoolGap.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/V2/Types/WalletId.hs Outdated Show resolved Hide resolved
cardano-wallet.cabal Outdated Show resolved Hide resolved
cardano-wallet.cabal Outdated Show resolved Hide resolved
test/unit/Cardano/Wallet/Api/V2/Arbitraries.hs Outdated Show resolved Hide resolved
test/unit/Cardano/Wallet/Api/V2/Arbitraries.hs Outdated Show resolved Hide resolved
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/wallet-api-servant branch 2 times, most recently from d6d22da to 8557eaf Compare March 19, 2019 04:20
Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

Looks pretty good. I have a few questions and one comment.

src/Cardano/Wallet/Api.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/Percentage.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletDelegation.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletState.hs Outdated Show resolved Hide resolved
test/unit/Cardano/Wallet/Api/JsonSpec.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/Amount.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletAddressPoolGap.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletAddressPoolGap.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletAddressPoolGap.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletBalance.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/JSON.hs Outdated Show resolved Hide resolved
src/Cardano/Wallet/Api/Types/WalletName.hs Outdated Show resolved Hide resolved
test/unit/Cardano/Wallet/Api/JsonSpec.hs Outdated Show resolved Hide resolved
@KtorZ
Copy link
Member

KtorZ commented Mar 20, 2019

@jonathanknowles I've continued the work on this PR with some of the points we raised during reviews (cf the commit history). I haven't got time to finalize aligning the primitives with the API and moving types around but it's almost there 👌

Copy link
Contributor

@rvl rvl left a comment

Choose a reason for hiding this comment

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

Looks pretty good.
Maybe later - some tests for parsing JSON with invalid amounts/percentages/wallet names.

src/Cardano/Api/Types.hs Outdated Show resolved Hide resolved
@rvl
Copy link
Contributor

rvl commented Mar 21, 2019

I also feel (and this may be nitpicking) that Cardano.Wallet.Api is a better choice than Cardano.Api. After all, we're providing an API for the wallet, and not Cardano in general.

@rvl rvl requested a review from KtorZ March 21, 2019 05:58
jonathanknowles and others added 18 commits March 21, 2019 06:28
Didn't do in the previous commit not to mess with git and makes
it easy to understand what happened to that file (shows a rename
in the git history).
* One space before opening brackets, everywhere.
* No multiple consecutive blank lines.
@jonathanknowles
Copy link
Member Author

I also feel (and this may be nitpicking) that Cardano.Wallet.Api is a better choice than Cardano.Api. After all, we're providing an API for the wallet, and not Cardano in general.

I tend to agree with this. Having Cardano.Api seems to imply that we're providing an API for Cardano in general. I'd be happy to keep things in Cardano.Wallet.Api for the moment.

The API we provide is an API for the wallet, rather than for Cardano as a whole.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/wallet-api-servant branch from e9f0e03 to 85e386d Compare March 21, 2019 06:37
@rvl rvl dismissed KtorZ’s stale review March 21, 2019 07:41

Comments are addressed

@rvl rvl merged commit 9ab3c90 into master Mar 21, 2019
@rvl rvl deleted the jonathanknowles/wallet-api-servant branch March 21, 2019 07:42
@KtorZ
Copy link
Member

KtorZ commented Mar 21, 2019

@rvl: Comments are addressed

Well... not really :/ ---> #76 (comment)

There were still a few types to be moved from the Api.Types to the wallet primitive as mentioned. We have quite a few duplicated logic between both layers and the Api should just be about representing those internal types in Servant / JSON (like I started for WalletDelegation).

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.

None yet

3 participants