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

Rewards detection in multisig #3872

Merged
merged 20 commits into from
Apr 28, 2023
Merged

Conversation

paweljakubas
Copy link
Contributor

@paweljakubas paweljakubas commented Apr 20, 2023

  • Extending shared state
  • add readRewardAccount
  • add manageSharedBalance
  • use manageSharedBalance
  • impl IsOurs for RewardAccount
  • extending integration testing (joining/rejoining)
  • redefine RewardAccount and deal with code changes throughout all codebase

Comments

Issue Number

adp-2603

@paweljakubas paweljakubas self-assigned this Apr 20, 2023
@paweljakubas paweljakubas mentioned this pull request Apr 20, 2023
6 tasks
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2603/rewards-multisig branch 3 times, most recently from cd9682c to 8385923 Compare April 24, 2023 07:45
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.

LGTM, but just realised concern with roundtrip of RewardAccount, and there are some fromJust which shouldn't be needed

Comment on lines 329 to 333
data RewardAccountSource
= RewardAccountFromKeyHash
| RewardAccountFromScriptHash
deriving (Eq, Show)

Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't be needed any longer

Suggested change
data RewardAccountSource
= RewardAccountFromKeyHash
| RewardAccountFromScriptHash
deriving (Eq, Show)

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

Comment on lines +382 to +383
show (CmdAdversarialReg (FromKeyHash a)) = "CmdAdversarialReg " <> B8.unpack a
show (CmdAdversarialReg (FromScriptHash a)) = "CmdAdversarialReg " <> B8.unpack a
Copy link
Member

Choose a reason for hiding this comment

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

These changes look inconvenient, but still preferable to having two reward observers I think.

Comment on lines +724 to +715
, "detection. Either there is db malfunction or managing rewards "
, "was used for shared wallets missing delegation template."
Copy link
Member

Choose a reason for hiding this comment

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

I'm guessing this error should never hit users of the API (if manageSharedRewardBalance is only called for wallets with delegation templates). Should it be err500 instead then?

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, err500 is better here

verify r
[ expectField
(#balance . #reward)
(.> (Quantity 0))
Copy link
Member

Choose a reason for hiding this comment

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

👌 nice!

Comment on lines 1429 to 1439
manageSharedRewardBalance
:: forall n block
. Tracer IO WalletWorkerLog
-> NetworkLayer IO block
-> DBLayer IO (SharedState n SharedKey) SharedKey
-> WalletId
-> IO ()
manageSharedRewardBalance tr' netLayer db@DBLayer{..} wid = do
Copy link
Member

Choose a reason for hiding this comment

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

Could parts common to both manageSharedRewardBalance and manageRewardBalance be factored out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 1442 to 1447
when (isNothing acctM) $
throwE ErrFetchRewardsMissingRewardAccount
liftIO $ getCachedRewardAccountBalance netLayer (fst $ fromJust acctM)
Copy link
Member

Choose a reason for hiding this comment

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

No need for fromJust:

Suggested change
when (isNothing acctM) $
throwE ErrFetchRewardsMissingRewardAccount
liftIO $ getCachedRewardAccountBalance netLayer (fst $ fromJust acctM)
case acctM of
Nothing -> throwE ErrFetchRewardsMissingRewardAccount
Just acct -> liftIO $ getCachedRewardAccountBalance netLayer (fst acct)

Comment on lines 2939 to 2940
liftHandler $ throwE ErrConstructTxDelegationInvalid
let path = snd $ fromJust res
Copy link
Member

Choose a reason for hiding this comment

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

Same as other comment — fromJust shouldn't be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

delCerts <- case optionalDelegationAction of
Nothing -> pure Nothing
Just action -> do
res <- liftHandler $ W.readSharedRewardAccount @n db wid
Copy link
Member

@Anviking Anviking Apr 24, 2023

Choose a reason for hiding this comment

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

At this point in the flow, we've already managed to create a balanced transaction, so I believe the ErrConstructTxDelegationInvalid would never been thrown from here. (not sure if there's an easy fix though)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right. ErrConstructTxDelegationInvalid will never be thrown here. If ever, it would be caught earlier. removed that.

properties:
message:
type: string
description: May occur when there is a missing reward account but wallet participates in staking.
Copy link
Member

Choose a reason for hiding this comment

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

Same as in the other comment, users would run into ErrDelegationInvalid but never this, modulo DB malfunctions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, MissingRewardAccount is now err500 and can occur when there is no reward account in db (for example db malfunction), and DelegationInvalid is user error (err403) occurring when user instantiate shared wallet with only spending script and creates tx with delegation action.


instance FromText RewardAccount where
fromText = fmap (RewardAccount . getHash @"RewardAccount") . fromText
fromText = fmap (FromKeyHash . getHash @"RewardAccount") . fromText
Copy link
Member

Choose a reason for hiding this comment

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

🤔 This wouldn't roundtrip... Could this be problematic for the DB?

If this is indeed a problem, maybe there's a middle ground between having RewardAccount = FromKeyHash ByteString | FromScriptHash ByteString everywhere and having two newRewardBalanceFetcher — e.g. having the same RewardAccount as before but letting newRewardBalanceFetcher operate on Either RewardAccount RewardAccount (or something isomorphic) as opposed to having two of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after some thought and having in mind db migrations I come up with the following solution:

instance ToText RewardAccount where
    toText (FromKeyHash bs) = T.cons 'k' . toText . Hash @"RewardAccount" $ bs
    toText (FromScriptHash bs) = T.cons 's' . toText . Hash @"RewardAccount" $ bs

instance FromText RewardAccount where
    fromText txt = case T.splitAt 1 txt of
        ("s", txt') ->
            fmap (FromScriptHash . getHash @"RewardAccount") . fromText $ txt'
        ("k", txt') ->
            fmap (FromKeyHash . getHash @"RewardAccount") . fromText $ txt'
        _ -> -- for backward compatibility when there is already db
            fmap (FromKeyHash . getHash @"RewardAccount") . fromText $ txt

hash is represented as hex so 0-9af alphabet. for key we prepend k for script s so outside the alphabet. Also to be backward compatible we fromText to key version (as this is one we should care atm). core unit tests passes. Old data type with all consequences stays. I think we should be good.

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2603/rewards-multisig branch from c2cc8f6 to 59e1d32 Compare April 26, 2023 13:23
Comment on lines 65 to 64
_ -> -- for backward compatibility when there is already db
fmap (FromKeyHash . getHash @"RewardAccount") . fromText $ txt
Copy link
Member

Choose a reason for hiding this comment

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

Ah 👍

Copy link
Member

@Anviking Anviking Apr 27, 2023

Choose a reason for hiding this comment

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

Btw: is there a roundtrip test for this instance? One that would have caught the previous failure to roundtrip?

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2603/rewards-multisig branch from 59e1d32 to 294e8c8 Compare April 26, 2023 14:54
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.

lgtm

Comment on lines 65 to 64
_ -> -- for backward compatibility when there is already db
fmap (FromKeyHash . getHash @"RewardAccount") . fromText $ txt
Copy link
Member

@Anviking Anviking Apr 27, 2023

Choose a reason for hiding this comment

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

Btw: is there a roundtrip test for this instance? One that would have caught the previous failure to roundtrip?

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2603/rewards-multisig branch from 294e8c8 to 10a5d8b Compare April 27, 2023 10:37
@paweljakubas
Copy link
Contributor Author

yes, there is roundtrip for "ApiRewardAccount" which is newtype wrapper for "RewardAccount". Still, I extended Arbitrary RewardAccount which now uses both data constructors.

@paweljakubas
Copy link
Contributor Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 27, 2023
3872: Rewards detection in multisig r=paweljakubas a=paweljakubas

<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
   ✓ Self-review your changes to make sure nothing unexpected slipped through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->


- [x] Extending shared state 
- [x] add readRewardAccount
- [x] add manageSharedBalance 
- [x] use manageSharedBalance    
- [x] impl IsOurs for RewardAccount
- [x] extending integration testing (joining/rejoining) 
- [x] redefine RewardAccount and deal with code changes throughout all codebase 

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number
adp-2603
<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Pawel Jakubas <pawel.jakubas@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 27, 2023

Build failed:

@piotr-iohk
Copy link
Contributor

@paweljakubas I see some integration tests fail with error 500 from the API:

That's embarrassing. Your wallet looks good, but I couldn't open a new database to store its data. This is unexpected and likely not your fault. Perhaps, check your filesystem's permissions or available space?

I observe the same when trying to create "incomplete" shared wallet on preprod (fails only when delegation template is incomplete, still seems to work fine for incomplete payment template):

curl -X POST http://localhost:8090/v2/shared-wallets \
-d '{
   "mnemonic_sentence":[
      "camera",
      "they",
       ....
      "kit"
   ],
   "passphrase":"Secure Passphrase",
   "name":"My Test Shared Wallet",
   "account_index":"0H",
   "payment_script_template":{
      "cosigners":{
         "cosigner#0":"self"
      },
      "template":{
         "all":[
            "cosigner#0"
         ]
      }
   },
   "delegation_script_template":{
      "cosigners":{
         "cosigner#0":"self"
      },
      "template":{
         "all":[
            "cosigner#0",
            "cosigner#1" <----------- delegation template "incomplete" as it has one extra cosigner that can be patched later on
         ]
      }
   }
}' \
-H "Content-Type: application/json"

👇

{
  "code": "unexpected_error",
  "message": "That's embarrassing. Your wallet looks good, but I couldn't open a new database to store its data. 
            This is unexpected and likely not your fault. Perhaps, check your filesystem's permissions or available space?"
}

In the log there is:

CallStack (from HasCallStack):
  error, called at src/Cardano/Wallet/Address/Derivation/SharedKey.hs:144:24 in cardano-wallet-2023.4.14-2N7szGvXGvP3BR5Wn8oQmO:Cardano.Wallet.Address.Derivation.SharedKey
  toKeyHash, called at src/Cardano/Wallet/Address/Derivation/SharedKey.hs:130:54 in cardano-wallet-2023.4.14-2N7szGvXGvP3BR5Wn8oQmO:Cardano.Wallet.Address.Derivation.SharedKey
[cardano-wallet.wallet-engine:Error:160] [2023-04-27 12:58:03.24 UTC] Worker has exited: Unhandled exception: Impossible: cosigner without accXPpub.
CallStack (from HasCallStack):
  error, called at src/Cardano/Wallet/Address/Derivation/SharedKey.hs:144:24 in cardano-wallet-2023.4.14-2N7szGvXGvP3BR5Wn8oQmO:Cardano.Wallet.Address.Derivation.SharedKey
  toKeyHash, called at src/Cardano/Wallet/Address/Derivation/SharedKey.hs:130:54 in cardano-wallet-2023.4.14-2N7szGvXGvP3BR5Wn8oQmO:Cardano.Wallet.Address.Derivation.SharedKey
[cardano-wallet.api-server:Error:158] [2023-04-27 12:58:03.24 UTC] [RequestId 19] POST /v2/shared-wallets 500 Internal Server Error in 0.19656104s

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2603/rewards-multisig branch from 10a5d8b to 10fb99a Compare April 28, 2023 12:39
@paweljakubas
Copy link
Contributor Author

bors r+

iohk-bors bot added a commit that referenced this pull request Apr 28, 2023
3872: Rewards detection in multisig r=paweljakubas a=paweljakubas

<!--
Detail in a few bullet points the work accomplished in this PR.

Before you submit, don't forget to:

* Make sure the GitHub PR fields are correct:
   ✓ Set a good Title for your PR.
   ✓ Assign yourself to the PR.
   ✓ Assign one or more reviewer(s).
   ✓ Link to a Jira issue, and/or other GitHub issues or PRs.
   ✓ In the PR description delete any empty sections
     and all text commented in <!--, so that this text does not appear
     in merge commit messages.

* Don't waste reviewers' time:
   ✓ If it's a draft, select the Create Draft PR option.
   ✓ Self-review your changes to make sure nothing unexpected slipped through.

* Try to make your intent clear:
   ✓ Write a good Description that explains what this PR is meant to do.
   ✓ Jira will detect and link to this PR once created, but you can also
     link this PR in the description of the corresponding Jira ticket.
   ✓ Highlight what Testing you have done.
   ✓ Acknowledge any changes required to the Documentation.
-->


- [x] Extending shared state 
- [x] add readRewardAccount
- [x] add manageSharedBalance 
- [x] use manageSharedBalance    
- [x] impl IsOurs for RewardAccount
- [x] extending integration testing (joining/rejoining) 
- [x] redefine RewardAccount and deal with code changes throughout all codebase 

### Comments

<!-- Additional comments, links, or screenshots to attach, if any. -->

### Issue Number
adp-2603
<!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
  Note: Jira issues of the form ADP- will be auto-linked. -->


Co-authored-by: Pawel Jakubas <pawel.jakubas@iohk.io>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 28, 2023

Build failed:

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2603/rewards-multisig branch from 10fb99a to 352dda7 Compare April 28, 2023 12:54
@paweljakubas
Copy link
Contributor Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Apr 28, 2023

Build succeeded:

@iohk-bors iohk-bors bot merged commit 56b346a into master Apr 28, 2023
@iohk-bors iohk-bors bot deleted the paweljakubas/adp-2603/rewards-multisig branch April 28, 2023 13:29
WilliamKingNoel-Bot pushed a commit that referenced this pull request Apr 28, 2023
…a=paweljakubas <!-- Detail in a few bullet points the work accomplished in this PR. Before you submit, don't forget to: CODE-OF-CONDUCT.md LICENSE README.md bors.toml cabal.project default.nix docker-compose.yml docs flake.lock flake.nix floskell.json hie-direnv.yaml lib nix prototypes reports scripts shell.nix specifications test touch.me.CI weeder.dhall Make sure the GitHub PR fields are correct: ✓ Set a good Title for your PR. ✓ Assign yourself to the PR. ✓ Assign one or more reviewer(s). ✓ Link to a Jira issue, and/or other GitHub issues or PRs. ✓ In the PR description delete any empty sections and all text commented in <!--, so that this text does not appear in merge commit messages. CODE-OF-CONDUCT.md LICENSE README.md bors.toml cabal.project default.nix docker-compose.yml docs flake.lock flake.nix floskell.json hie-direnv.yaml lib nix prototypes reports scripts shell.nix specifications test touch.me.CI weeder.dhall Don't waste reviewers' time: ✓ If it's a draft, select the Create Draft PR option. ✓ Self-review your changes to make sure nothing unexpected slipped through. CODE-OF-CONDUCT.md LICENSE README.md bors.toml cabal.project default.nix docker-compose.yml docs flake.lock flake.nix floskell.json hie-direnv.yaml lib nix prototypes reports scripts shell.nix specifications test touch.me.CI weeder.dhall Try to make your intent clear: ✓ Write a good Description that explains what this PR is meant to do. ✓ Jira will detect and link to this PR once created, but you can also link this PR in the description of the corresponding Jira ticket. ✓ Highlight what Testing you have done. ✓ Acknowledge any changes required to the Documentation. --> - [x] Extending shared state - [x] add readRewardAccount - [x] add manageSharedBalance - [x] use manageSharedBalance - [x] impl IsOurs for RewardAccount - [x] extending integration testing (joining/rejoining) - [x] redefine RewardAccount and deal with code changes throughout all codebase ### Comments <!-- Additional comments, links, or screenshots to attach, if any. --> ### Issue Number adp-2603
 <!-- Reference the Jira/GitHub issue that this PR relates to, and which requirements it tackles.
 Note: Jira issues of the form ADP- will be auto-linked. -->
 Co-authored-by: Pawel Jakubas <pawel.jakubas@iohk.io> Source commit: 56b346a
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.

3 participants