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 in shared wallets #3842

Closed

Conversation

paweljakubas
Copy link
Contributor

@paweljakubas paweljakubas commented Apr 7, 2023

  • Extending shared state
  • add readRewardAccount
  • add manageSharedBalance
  • use manageSharedBalance
  • impl IsOurs for RewardAccount
  • extending integration testing (joining/rejoining)

Comments

PR on top of #3830

Issue Number

adp-2603

@paweljakubas paweljakubas self-assigned this Apr 7, 2023
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 @paweljakubas

Just some very early initial comments and suggestions!

Comment on lines 3012 to 3018
let rewardAcctM = case scriptM of
Just script ->
let scriptHash =
CA.unScriptHash $
CA.toScriptHash script
in Just $ RewardAccount scriptHash
Nothing -> Nothing
Copy link
Member

Choose a reason for hiding this comment

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

I think we can rewrite this as:

Suggested change
let rewardAcctM = case scriptM of
Just script ->
let scriptHash =
CA.unScriptHash $
CA.toScriptHash script
in Just $ RewardAccount scriptHash
Nothing -> Nothing
let rewardAcctM =
RewardAccount . CA.unScriptHash . CA.toScriptHash <$> scriptM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

of course, done

Comment on lines 3472 to 3477
if numberStakingNativeScripts == 0 then
0
else if numberStakingNativeScripts == 1 then
Shared.estimateMinWitnessRequiredPerInput scriptD
else
error "wallet supports transactions with 0 or 1 staking script"
Copy link
Member

Choose a reason for hiding this comment

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

Can this be rewritten with pattern guards or MultiwayIf?

Something like:

Suggested change
if numberStakingNativeScripts == 0 then
0
else if numberStakingNativeScripts == 1 then
Shared.estimateMinWitnessRequiredPerInput scriptD
else
error "wallet supports transactions with 0 or 1 staking script"
| numberStakingNativeScripts == 0 =
0
| numberStakingNativeScripts == 1 =
Shared.estimateMinWitnessRequiredPerInput scriptD
| otherwise =
error "wallet supports transactions with 0 or 1 staking script"

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 1336 to 1339
walletState <- getState <$>
withExceptT ErrReadRewardAccountNoSuchWallet
(readWalletCheckpoint db wid)
pure $ Shared.rewardAccountKey walletState
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
walletState <- getState <$>
withExceptT ErrReadRewardAccountNoSuchWallet
(readWalletCheckpoint db wid)
pure $ Shared.rewardAccountKey walletState
Shared.rewardAccountKey . getState <$>
withExceptT ErrReadRewardAccountNoSuchWallet
(readWalletCheckpoint db wid)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it i not in this form anymore. path was added

Comment on lines 1341 to 1342
readWalletCheckpoint ::
DBLayer IO s k -> WalletId -> ExceptT ErrNoSuchWallet IO (Wallet s)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
readWalletCheckpoint ::
DBLayer IO s k -> WalletId -> ExceptT ErrNoSuchWallet IO (Wallet s)
readWalletCheckpoint
:: DBLayer IO s k -> WalletId -> ExceptT ErrNoSuchWallet IO (Wallet s)

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 2025 to 2031
stakingKeyM = case xprvM of
Just xprv -> Just
( hashVerificationKey @k CA.Delegation $ liftRawKey $ toXPub xprv
, xprv
, rootPwd
)
Nothing -> Nothing
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
stakingKeyM = case xprvM of
Just xprv -> Just
( hashVerificationKey @k CA.Delegation $ liftRawKey $ toXPub xprv
, xprv
, rootPwd
)
Nothing -> Nothing
stakingKeyM = xprvM <&> \xprv ->
( hashVerificationKey @k CA.Delegation $ liftRawKey $ toXPub xprv
, xprv
, rootPwd
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

neat form! done

Left rewardAcct ->
_getCachedRewardAccountBalance rewardsObserverFromKeyHash rewardAcct
Right rewardAcct ->
_getCachedRewardAccountBalance rewardsObserverFromScriptHash rewardAcct
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_getCachedRewardAccountBalance rewardsObserverFromScriptHash rewardAcct
_getCachedRewardAccountBalance
rewardsObserverFromScriptHash rewardAcct

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

@@ -453,6 +453,11 @@ instance IsServerError ErrConstructTx where
, "'PATCH /shared-wallets/{walletId}/delegation-script-template'"
, "to make it suitable for constructing transactions."
]
ErrConstructTxStakingInvalid ->
apiError err403 StakingInvalid $ T.unwords
[ "I cannot construct a delegating transaction for a shared wallet "
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[ "I cannot construct a delegating transaction for a shared wallet "
[ "I cannot construct a delegating transaction for a shared 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.

done

Comment on lines +722 to +725
apiError err404 MissingRewardAccount $ mconcat
[ "I couldn't read a reward account which is required for reward "
, "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.

Hi @paweljakubas

Could you explain a little more about what this means:
"managing rewards was used for shared wallets missing delegation template."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shared wallets can be with and without delegation template. When the delegation template is missing we are having wallet that cannot stake, is generating enterprise addresses (only with payment credential). In that case we should not spin up "managing awards". It is the error that make sure "managing awards service" is used for multisig wallets with delegation templates present

, depositsReturned = []
, certificates = certs
, depositsTaken =
(Quantity . fromIntegral . unCoin . W.stakeKeyDeposit $ pp)
Copy link
Member

@jonathanknowles jonathanknowles Apr 11, 2023

Choose a reason for hiding this comment

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

Suggestion: use Coin.toQuantity (this is a statically-checked total function, whereas fromIntegral is statically-unchecked and partial).

Suggested change
(Quantity . fromIntegral . unCoin . W.stakeKeyDeposit $ pp)
(Coin.toQuantity . W.stakeKeyDeposit $ pp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good suggestion. done

(Quantity . fromIntegral . unCoin . W.stakeKeyDeposit $ pp)
<$ filter ourRewardAccountRegistration certs
, depositsReturned =
(Quantity . fromIntegral . unCoin . W.stakeKeyDeposit $ pp)
Copy link
Member

@jonathanknowles jonathanknowles Apr 11, 2023

Choose a reason for hiding this comment

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

Suggestion: use Coin.toQuantity (this is a statically-checked total function, whereas fromIntegral is statically-unchecked and partial).

Suggested change
(Quantity . fromIntegral . unCoin . W.stakeKeyDeposit $ pp)
(Coin.toQuantity . W.stakeKeyDeposit $ pp)

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

@paweljakubas paweljakubas changed the base branch from master to paweljakubas/adp-2602/shared-wallets-delegation April 11, 2023 16:45
@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2602/shared-wallets-delegation branch 2 times, most recently from b3c44c7 to 6cde360 Compare April 12, 2023 11:14
@@ -160,7 +160,8 @@ data NetworkLayer m block = NetworkLayer
-> m StakePoolsSummary

, getCachedRewardAccountBalance
:: RewardAccount
:: Either RewardAccount RewardAccount
Copy link
Member

Choose a reason for hiding this comment

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

Is the distinction between RewardAccount and RewardAccount really needed, or could we not get away with just RewardAccount?

Copy link
Contributor Author

@paweljakubas paweljakubas Apr 12, 2023

Choose a reason for hiding this comment

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

yes, one is key hashes based and another is script hashes based. And this information is need when transforming from node reward account. See how this point is used in Cardano.Wallet.Shelley.Network.Node module (shelleyQry)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, so I take it really is a binary tag for script vs keyHash object we need for the query, and is not already in the RewardAccount bytes...

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2602/shared-wallets-delegation branch 4 times, most recently from 990aaff to 2490c5e Compare April 17, 2023 06:46
Comment on lines +364 to +367
rewardsObserverFromKeyHash <- newRewardBalanceFetcher
tr readNodeTip queryRewardQ RewardAccountFromKeyHash
rewardsObserverFromScriptHash <- newRewardBalanceFetcher
tr readNodeTip queryRewardQ RewardAccountFromScriptHash
Copy link
Member

Choose a reason for hiding this comment

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

Creating two different fetchers with two different RewardAccountSource is unnecessary. Batching all script and key credentials into the same local state query should be more efficient. The reason you can't currently do this is the RewardAccount type containing too little information.

What about:

  1. Either importing or re-exporting Cardano.StakeCredential / Ledger.StakeCredential crypto or defining something similar to it. (Could also be data StakeCredential = ...FromKeyHash RewardAccount | ...FromScriptHash RewardAccount)
  2. Changing getCachedRewardAccountBalance to take StakeCredential instead of Either RewardAccount RewardAccount
  3. Removing RewardAccountSource and the duplicate call to newRewardBalanceFetcher

?

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.

@paweljakubas paweljakubas force-pushed the paweljakubas/adp-2602/shared-wallets-delegation branch from 2490c5e to 76cc989 Compare April 17, 2023 12:44
Base automatically changed from paweljakubas/adp-2602/shared-wallets-delegation to master April 17, 2023 13:53
@paweljakubas
Copy link
Contributor Author

closing in favor of #3872

@paweljakubas paweljakubas deleted the paweljakubas/rewards-withdrawals-shared-wallets branch April 20, 2023 10:08
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