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

Refactor in preparation to reuse balanceTransaction within quitStakePool. #3555

Merged
merged 30 commits into from
Nov 14, 2022

Conversation

Unisay
Copy link
Contributor

@Unisay Unisay commented Oct 27, 2022

  • I did rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools because Cardano.Wallet.Shelly namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
  • split one mkRewardAccountBuilder function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
  • removed queryRewardBalance function as it adds no value on top of the getCachedRewardAccountBalance.

(The PR is optimized for reviewing commit-by-commit)

Issue Number

In preparation to ADP-2267

@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch 2 times, most recently from 33eecf8 to c5c3b38 Compare October 31, 2022 16:35
Copy link
Member

@jonathanknowles jonathanknowles left a comment

Choose a reason for hiding this comment

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

Hi @Unisay

Thanks for making this PR!

From looking through, it seems that this PR makes a few distinct changes:

  1. Introduces various readability improvements. (Thanks for this! 💯 )
  2. Introduces the WalletException sum type, which groups together all the individual error types and replaces the use of "errors" with exceptions.
  3. Restructures mkRewardAccountBuilder.
  4. Reworks quitStakePool

Can I trouble you with a very small request: would it be possible to separate this out into separate, smaller PRs? 🙏🏻

  1. Just the readability improvements on their own. (We could approve this very quickly.)
  2. Just the WalletException refactoring on its own. (This deserves more attention and discussion, I feel.)
  3. Just the restructuring of mkRewardAccountBuilder.
  4. The rework of quitStakePool.

In the current PR, the above changes are sometimes mixed together a bit. In particular, the WalletException refactoring is bundled up together with a rework of quitStakePool (in the final commit). But the WalletException work is interesting in its own right (and deserving of its own review, IMO).

Many thanks! Let me know if I can help in any way.

lib/wallet/src/Cardano/Wallet.hs Outdated Show resolved Hide resolved
@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch from c5c3b38 to 4bfe080 Compare November 1, 2022 11:51
@Unisay
Copy link
Contributor Author

Unisay commented Nov 1, 2022

Can I trouble you with a very small request: would it be possible to separate this out into separate, smaller PRs? 🙏🏻
1. Just the readability improvements on their own. (We could approve this very quickly.)
2. Just the WalletException refactoring on its own. (This deserves more attention and discussion, I feel.)
3. Just the restructuring of mkRewardAccountBuilder.
4. The rework of quitStakePool.

I am afraid that I see the overhead and effort associated with splitting this into individual PRs more vividly than benefits of doing it:
splitting would allow to review/approve/merge only that part independently of other parts; The price for it is: having to spend time and rewrite git history isolating changes; Some of the PRs depend on each other and need to be merged in sequence, etc.

Side note: in practice, such opportunistic-refactoring style modifications happen naturally and aren't well isolated, for example: I started reading code of the quitStakePool, as I read it I also improve it: give better names to expressions (like withdrawal instead of wdrl or walletId instead of wid), I reformat code to improve code style and expression locality. Half way through the process I realize I can simplify function type by getting rid of the ExceptT in favor of exceptions in IO. This triggers a larger change in the way exceptions are handled and defined; Once this is done I am back to modifying quitStakePool.

Anyway, I did my best grouping changes into individual commits (retrospectively), this should facilitate code review.

@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch from d3af78c to a51009d Compare November 2, 2022 15:45
@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch from 19f689c to 03bfe2c Compare November 7, 2022 12:01
@Unisay Unisay requested a review from Anviking November 7, 2022 13:27
lib/wallet/api/http/Cardano/Wallet/Shelley.hs Outdated Show resolved Hide resolved
lib/wallet/src/Cardano/Wallet.hs Show resolved Hide resolved
@Anviking
Copy link
Member

Anviking commented Nov 7, 2022

bors try

iohk-bors bot added a commit that referenced this pull request Nov 7, 2022
Copy link
Member

@Anviking Anviking left a comment

Choose a reason for hiding this comment

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

Nice! Lgtm.

But it looks like bors try is about to fail

      Migration from Random wallet to target address count: 1. [✘] (1358ms)
      Migration from Random wallet to target address count: 3. [✘] (3828ms)
      Migration from Random wallet to target address count: 10. [✘] (4016ms)
      Migration from Icarus wallet to target address count: 1. [✘] (4177ms)
      Migration from Icarus wallet to target address count: 3. [✘] (4237ms)
      Migration from Icarus wallet to target address count: 10. [✘] (1748ms)

lib/wallet/api/http/Cardano/Wallet/Shelley.hs Outdated Show resolved Hide resolved
lib/wallet/api/http/Cardano/Wallet/Shelley.hs Outdated Show resolved Hide resolved
lib/wallet/src/Cardano/Wallet.hs Outdated Show resolved Hide resolved
lib/wallet/src/Cardano/Wallet.hs Outdated Show resolved Hide resolved
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 7, 2022

try

Build failed:

@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch 2 times, most recently from d677889 to e552b8c Compare November 8, 2022 13:27
@Unisay Unisay requested a review from Anviking November 8, 2022 14:20
Comment on lines 3948 to 3982
shelleyOnlyRewardAccountBuilder
:: forall s k (n :: NetworkDiscriminant)
. ( HardDerivation k
, Bounded (Index (AddressIndexDerivationType k) (AddressCredential k))
, WalletKey k
, Typeable s
, Typeable n
)
=> Maybe ApiWithdrawalPostData
-> Handler (RewardAccountBuilder k)
mkRewardAccountBuilder withdrawal = do
let selfRewardCredentials (rootK, pwdP) =
(getRawKey (deriveRewardAccount @k pwdP rootK), pwdP)
=> ApiWithdrawalPostData
-> Either ErrReadRewardAccount (RewardAccountBuilder k)
shelleyOnlyRewardAccountBuilder w =
case testEquality (typeRep @s) (typeRep @(SeqState n ShelleyKey)) of
Nothing -> liftHandler $ throwE ErrReadRewardAccountNotAShelleyWallet
Just Refl -> case withdrawal of
Nothing -> pure selfRewardCredentials
Just w -> case w of
SelfWithdrawal -> pure selfRewardCredentials
ExternalWithdrawal (ApiMnemonicT m) -> do
let (xprv, _acct, _path) = W.someRewardAccount @ShelleyKey m
pure (const (xprv, mempty))
Nothing -> throwError ErrReadRewardAccountNotAShelleyWallet
Just Refl -> case w of
SelfWithdrawal -> pure selfRewardAccountBuilder
ExternalWithdrawal (ApiMnemonicT m) -> do
let (xprv, _acct, _path) = W.someRewardAccount @ShelleyKey m
pure (const (xprv, mempty))

selfRewardAccountBuilder
:: forall k
. ( HardDerivation k
, Bounded (Index (AddressIndexDerivationType k) (AddressCredential k))
, WalletKey k
)
=> RewardAccountBuilder k
selfRewardAccountBuilder (rootK, pwdP) =
(getRawKey (deriveRewardAccount @k pwdP rootK), pwdP)
Copy link
Member

Choose a reason for hiding this comment

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

Thinking loudly: these are only used from the "old" tx endpoints for the sake of W.buildAndSignTransaction, so we should eventually be able to remove/re-work into some clearer form. I think external withdrawals is what constructTx wouldn't handle automatically, and if it wants to continue being unaware of them, perhaps something like this could work:

postTransactionOld = do
    tx <- signTx wallet
        $ addVkWitness externalWithdrawalKey -- after balancing
        $ balanceTx  wallet
        $ addWithdrawal externalWithdrawal -- before balancing
        $ constructUnbalancedTx payment
    ...

(conceptually)

@Unisay
Copy link
Contributor Author

Unisay commented Nov 8, 2022

bors r+

iohk-bors bot added a commit that referenced this pull request Nov 8, 2022
3555: Refactor in preparation to reuse `balanceTransaction` within `quitStakePool`. r=Unisay a=Unisay

- [x] I did [rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools](3af3732) because `Cardano.Wallet.Shelly` namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
- [x] split one `mkRewardAccountBuilder` function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
- [x] removed `queryRewardBalance` function as it adds no value on top of the `getCachedRewardAccountBalance`.

(The PR is optimized for reviewing commit-by-commit)

### Issue Number

In preparation to ADP-2267


Co-authored-by: Yura Lazarev <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 8, 2022

Build failed:

@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch from e552b8c to b08c8be Compare November 9, 2022 11:53
@Unisay
Copy link
Contributor Author

Unisay commented Nov 9, 2022

bors r+

iohk-bors bot added a commit that referenced this pull request Nov 9, 2022
3555: Refactor in preparation to reuse `balanceTransaction` within `quitStakePool`. r=Unisay a=Unisay

- [x] I did [rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools](3af3732) because `Cardano.Wallet.Shelly` namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
- [x] split one `mkRewardAccountBuilder` function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
- [x] removed `queryRewardBalance` function as it adds no value on top of the `getCachedRewardAccountBalance`.

(The PR is optimized for reviewing commit-by-commit)

### Issue Number

In preparation to ADP-2267


Co-authored-by: Yura Lazarev <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 9, 2022

Build failed:

@Unisay
Copy link
Contributor Author

Unisay commented Nov 9, 2022

bors retry

@Unisay Unisay force-pushed the yura/ADP-2267/quit-sp-balance-tx branch from a8a2027 to eee2269 Compare November 11, 2022 17:09
@Unisay
Copy link
Contributor Author

Unisay commented Nov 11, 2022

bors r+

iohk-bors bot added a commit that referenced this pull request Nov 11, 2022
3555: Refactor in preparation to reuse `balanceTransaction` within `quitStakePool`. r=Unisay a=Unisay

- [x] I did [rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools](3af3732) because `Cardano.Wallet.Shelly` namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
- [x] split one `mkRewardAccountBuilder` function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
- [x] removed `queryRewardBalance` function as it adds no value on top of the `getCachedRewardAccountBalance`.

(The PR is optimized for reviewing commit-by-commit)

### Issue Number

In preparation to ADP-2267


Co-authored-by: Yura Lazarev <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 11, 2022

Build failed:

@Unisay
Copy link
Contributor Author

Unisay commented Nov 12, 2022

bors merge

iohk-bors bot added a commit that referenced this pull request Nov 12, 2022
3555: Refactor in preparation to reuse `balanceTransaction` within `quitStakePool`. r=Unisay a=Unisay

- [x] I did [rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools](3af3732) because `Cardano.Wallet.Shelly` namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
- [x] split one `mkRewardAccountBuilder` function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
- [x] removed `queryRewardBalance` function as it adds no value on top of the `getCachedRewardAccountBalance`.

(The PR is optimized for reviewing commit-by-commit)

### Issue Number

In preparation to ADP-2267


Co-authored-by: Yura Lazarev <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 12, 2022

Build failed:

@Unisay
Copy link
Contributor Author

Unisay commented Nov 14, 2022

bors merge

iohk-bors bot added a commit that referenced this pull request Nov 14, 2022
3555: Refactor in preparation to reuse `balanceTransaction` within `quitStakePool`. r=Unisay a=Unisay

- [x] I did [rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools](3af3732) because `Cardano.Wallet.Shelly` namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
- [x] split one `mkRewardAccountBuilder` function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
- [x] removed `queryRewardBalance` function as it adds no value on top of the `getCachedRewardAccountBalance`.

(The PR is optimized for reviewing commit-by-commit)

### Issue Number

In preparation to ADP-2267


Co-authored-by: Yura Lazarev <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 14, 2022

Build failed:

@Anviking
Copy link
Member

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 14, 2022

Build succeeded:

@iohk-bors iohk-bors bot merged commit a3e6478 into master Nov 14, 2022
@iohk-bors iohk-bors bot deleted the yura/ADP-2267/quit-sp-balance-tx branch November 14, 2022 13:59
WilliamKingNoel-Bot pushed a commit that referenced this pull request Nov 14, 2022
…saction` within `quitStakePool`. r=Anviking a=Unisay - [x] I did [rename Cardano.Wallet.Shelley.Pools to Cardano.Wallet.Pools](3af3732) because `Cardano.Wallet.Shelly` namespace is a leftover from the times we had 2 separate libraries (core/shelley). It makes sense to me to gradually remove this separation.
 - [x] split one `mkRewardAccountBuilder` function into smaller ones (corresponding to individual concers) as it had to many responsibilities at once, was hard to reason about.
 - [x] removed `queryRewardBalance` function as it adds no value on top of the `getCachedRewardAccountBalance`.
 
 (The PR is optimized for reviewing commit-by-commit)
 
 ### Issue Number
 
 In preparation to ADP-2267
 Co-authored-by: Yura Lazarev <[email protected]> Source commit: a3e6478
iohk-bors bot added a commit that referenced this pull request Nov 15, 2022
3583: Revise error name to `WithdrawalNotBeneficial`. r=jonathanknowles a=jonathanknowles

## Issue Number

None. Noticed while reviewing #3555.

## Description

This PR renames `ErrWithdrawalNotWorth` to `ErrWithdrawalNotBeneficial`.

The wording of the former error name is broken. We _can_ say "a withdrawal is not worth **it**", but saying something is "not worth" (without the "it") just doesn't make sense in English.

## Compatibility

Usages of the JSON error code `withdrawal_not_worth` are very rare: [Usages of `withdrawal_not_worth`](https://github.com/search?q=org%3Ainput-output-hk+%22withdrawal_not_worth%22&type=code).

These rare cases can easily be updated with a tiny PR or two.

Co-authored-by: Jonathan Knowles <[email protected]>
WilliamKingNoel-Bot pushed a commit that referenced this pull request Nov 15, 2022
…`. r=jonathanknowles a=jonathanknowles ## Issue Number None. Noticed while reviewing #3555.
 
 ## Description
 
 This PR renames `ErrWithdrawalNotWorth` to `ErrWithdrawalNotBeneficial`.
 
 The wording of the former error name is broken. We _can_ say "a withdrawal is not worth **it**", but saying something is "not worth" (without the "it") just doesn't make sense in English.
 
 ## Compatibility
 
 Usages of the JSON error code `withdrawal_not_worth` are very rare: [Usages of `withdrawal_not_worth`](https://github.com/search?q=org%3Ainput-output-hk+%22withdrawal_not_worth%22&type=code).
 
 These rare cases can easily be updated with a tiny PR or two. Co-authored-by: Jonathan Knowles <[email protected]> Source commit: d59b05e
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.

4 participants