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

Logic changes to join/quitStakePools #4468

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/integration/framework/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ module Test.Integration.Framework.DSL
, notDelegating
, delegating
, onlyVoting
, votingAndDelegating
, getSlotParams
, arbitraryStake

Expand Down Expand Up @@ -235,6 +236,7 @@ module Test.Integration.Framework.DSL
, listLimitedTransactions
, noConway
, noBabbage
, babbageOrConway
) where

import Prelude
Expand Down Expand Up @@ -3522,17 +3524,28 @@ delegating pidActive nexts = (notDelegating nexts)
{ active = ApiWalletDelegationNext Delegating (Just pidActive) Nothing Nothing
}

-- this one will be revisited when I add voting and delegation together. Now only voting
onlyVoting
:: ApiT DRep
-- ^ Pool joined
-- ^ voting
-> [(Maybe (ApiT PoolId), EpochInfo)]
-- ^ Pools to be joined & epoch at which the new delegation will become active
-> ApiWalletDelegation
onlyVoting drep nexts = (notDelegating nexts)
{ active = ApiWalletDelegationNext Voting Nothing (Just drep) Nothing
}

votingAndDelegating
:: ApiT PoolId
-- ^ Pool joined
-> ApiT DRep
-- ^ voting
-> [(Maybe (ApiT PoolId), EpochInfo)]
-- ^ Pools to be joined & epoch at which the new delegation will become active
-> ApiWalletDelegation
votingAndDelegating pidActive drep nexts = (notDelegating nexts)
{ active = ApiWalletDelegationNext VotingAndDelegating (Just pidActive) (Just drep) Nothing
}

getRetirementEpoch :: StakePool -> Maybe EpochNo
getRetirementEpoch = fmap (view #epochNumber) . view #retirement

Expand Down Expand Up @@ -3602,3 +3615,9 @@ noBabbage :: MonadIO m => Context -> String -> m ()
noBabbage ctx reason = liftIO $ do
when (_mainEra ctx == ApiBabbage) $
pendingWith $ "BABBAGE is not supported: " <> reason

babbageOrConway :: Context -> a -> a -> a
babbageOrConway ctx valBabbage valConway =
case _mainEra ctx of
ApiBabbage -> valBabbage
_ -> valConway
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..)
)
import Cardano.Wallet.Primitive.Types.DRep
( DRep (..)
)
import Cardano.Wallet.Primitive.Types.Tx.Constraints
( TxSize (..)
)
Expand Down Expand Up @@ -152,6 +155,7 @@ import Test.Integration.Framework.DSL
( Headers (..)
, Payload (..)
, arbitraryStake
, babbageOrConway
, bracketSettings
, decodeErrorInfo
, delegating
Expand Down Expand Up @@ -193,6 +197,7 @@ import Test.Integration.Framework.DSL
, verify
, verifyMaintenanceAction
, verifyMetadataSource
, votingAndDelegating
, waitForEpoch
, waitForNextEpoch
, waitForTxImmutability
Expand Down Expand Up @@ -399,6 +404,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
]

-- Listing stake keys shows
let delegStatus = babbageOrConway ctx Delegating VotingAndDelegating
request @(ApiStakeKeys n) ctx (Link.listStakeKeys src) Default Empty
>>= flip
verify
Expand All @@ -410,7 +416,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
(acc ^. #_stake) .> ApiAmount 0
acc
^. (#_delegation . #active . #status)
`shouldBe` Delegating
`shouldBe` delegStatus
acc
^. (#_delegation . #active . #target)
`shouldBe` (Just (ApiT pool))
Expand Down Expand Up @@ -709,12 +715,13 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
waitForTxStatus ctx w InLedger . getResponse
=<< joinStakePool @n ctx (SpecificPool pool1) (w, fixturePassphrase)

let delegStatus = babbageOrConway ctx Delegating VotingAndDelegating
request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty
>>= flip
verify
[ expectField (#delegation . #next) $ \case
[dlg] -> do
(dlg ^. #status) `shouldBe` Delegating
(dlg ^. #status) `shouldBe` delegStatus
(dlg ^. #target) `shouldBe` Just (ApiT pool1)
(view #epochNumber <$> dlg ^. #changesAt)
`shouldSatisfy` ( \x ->
Expand All @@ -736,10 +743,14 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
-- Epoch A+2: stake is active, rewards start accumulating.
waitNumberOfEpochBoundaries 2 ctx

let deleg pool = babbageOrConway ctx
(delegating (ApiT pool) [])
(votingAndDelegating (ApiT pool) (ApiT Abstain) [])

request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty
>>= flip
verify
[expectField #delegation (`shouldBe` delegating (ApiT pool1) [])]
[expectField #delegation (`shouldBe` deleg pool1)]

-- join another stake pool
waitForTxStatus ctx w InLedger . getResponse
Expand All @@ -751,7 +762,7 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty
>>= flip
verify
[expectField #delegation (`shouldBe` delegating (ApiT pool2) [])]
[expectField #delegation (`shouldBe` deleg pool2)]

it "STAKE_POOLS_JOIN_04 - Rewards accumulate" $ \ctx -> runResourceT $ do
w <- fixtureWallet ctx
Expand Down Expand Up @@ -986,10 +997,14 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do

waitNumberOfEpochBoundaries 2 ctx

let deleg = babbageOrConway ctx
(delegating (ApiT pool1) [])
(votingAndDelegating (ApiT pool1) (ApiT Abstain) [])

request @ApiWallet ctx (Link.getWallet @'Shelley w) Default Empty
>>= flip
verify
[expectField #delegation (`shouldBe` delegating (ApiT pool1) [])]
[expectField #delegation (`shouldBe` deleg)]

-- Cannot join the same pool
liftIO
Expand Down Expand Up @@ -1138,14 +1153,18 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
-- Epoch A+2: stake is active, rewards start accumulating.
waitNumberOfEpochBoundaries 2 ctx

let deleg = babbageOrConway ctx
(delegating (ApiT pool) [])
(votingAndDelegating (ApiT pool) (ApiT Abstain) [])

request @ApiWallet
ctx
(Link.getWallet @'Shelley w)
Default
Empty
>>= flip
verify
[ expectField #delegation (`shouldBe` delegating (ApiT pool) [])
[ expectField #delegation (`shouldBe` deleg)
]

rQuit <- quitStakePool @n ctx (w, fixturePassphrase)
Expand Down Expand Up @@ -1202,7 +1221,8 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
"STAKE_POOLS_QUIT_01x - \
\I cannot quit if I have not enough to cover fees"
$ \ctx -> runResourceT $ do
let initBalance = [costOfJoining ctx + depositAmt ctx]
let fuzz = costOf 50 ctx
let initBalance = [costOfJoining ctx + depositAmt ctx + fuzz]
w <- fixtureWalletWith @n ctx initBalance

pool : _ <-
Expand All @@ -1225,14 +1245,18 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do
-- Epoch A+2: stake is active, rewards start accumulating.
waitNumberOfEpochBoundaries 2 ctx

let deleg = babbageOrConway ctx
(delegating (ApiT pool) [])
(votingAndDelegating (ApiT pool) (ApiT Abstain) [])

request @ApiWallet
ctx
(Link.getWallet @'Shelley w)
Default
Empty
>>= flip
verify
[ expectField #delegation (`shouldBe` delegating (ApiT pool) [])
[ expectField #delegation (`shouldBe` deleg)
]

response <- quitStakePool @n ctx (w, fixturePassphrase)
Expand Down
12 changes: 6 additions & 6 deletions lib/unit/test/unit/Cardano/Wallet/DelegationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,26 @@ spec = describe "Cardano.Wallet.DelegationSpec" $ do
`shouldBe` Left (W.ErrNoSuchPool pidUnknown)
it "Cannot quit when active: not_delegating, next = []" $ do
let dlg = WalletDelegation {active = NotDelegating, next = []}
WD.guardQuit dlg NoWithdrawal (Coin 0)
WD.guardQuit dlg NoWithdrawal (Coin 0) False
`shouldBe` Left (W.ErrNotDelegatingOrAboutTo)
it "Cannot quit when active: A, next = [not_delegating]" $ do
let next1 = WalletDelegationNext (EpochNo 1) NotDelegating
let dlg = WalletDelegation
{active = Delegating pidA, next = [next1]}
WD.guardQuit dlg NoWithdrawal (Coin 0)
WD.guardQuit dlg NoWithdrawal (Coin 0) False
`shouldBe` Left (W.ErrNotDelegatingOrAboutTo)
it "Cannot quit when active: A, next = [B, not_delegating]" $ do
let next1 = WalletDelegationNext (EpochNo 1) (Delegating pidB)
let next2 = WalletDelegationNext (EpochNo 2) NotDelegating
let dlg = WalletDelegation
{active = Delegating pidA, next = [next1, next2]}
WD.guardQuit dlg NoWithdrawal (Coin 0)
WD.guardQuit dlg NoWithdrawal (Coin 0) False
`shouldBe` Left (W.ErrNotDelegatingOrAboutTo)
it "Can quit when active: not_delegating, next = [A]" $ do
let next1 = WalletDelegationNext (EpochNo 1) (Delegating pidA)
let dlg = WalletDelegation
{active = NotDelegating, next = [next1]}
WD.guardQuit dlg NoWithdrawal (Coin 0) `shouldBe` Right ()
WD.guardQuit dlg NoWithdrawal (Coin 0) False `shouldBe` Right ()
where
pidA = PoolId "A"
pidB = PoolId "B"
Expand Down Expand Up @@ -192,7 +192,7 @@ prop_guardJoinQuit knownPoolsList dlg pid wdrl mRetirementInfo = checkCoverage
label "ErrNoSuchPool" $ property True
Left W.ErrAlreadyDelegating{} ->
label "ErrAlreadyDelegating"
(WD.guardQuit dlg wdrl (Coin 0) === Right ())
(WD.guardQuit dlg wdrl (Coin 0) False === Right ())
where
knownPools = Set.fromList knownPoolsList
retirementNotPlanned =
Expand All @@ -215,7 +215,7 @@ prop_guardQuitJoin
prop_guardQuitJoin (NonEmpty knownPoolsList) dlg rewards wdrl =
let knownPools = Set.fromList knownPoolsList in
let noRetirementPlanned = Nothing in
case WD.guardQuit dlg wdrl (Coin.fromWord64 rewards) of
case WD.guardQuit dlg wdrl (Coin.fromWord64 rewards) False of
Right () ->
label "I can quit" $ property True
Left W.ErrNotDelegatingOrAboutTo ->
Expand Down
18 changes: 9 additions & 9 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ import qualified Cardano.Wallet.Api.Types.Amount as ApiAmount
import qualified Cardano.Wallet.Api.Types.WalletAssets as ApiWalletAssets
import qualified Cardano.Wallet.DB as W
import qualified Cardano.Wallet.Delegation as WD
import qualified Cardano.Wallet.IO.Delegation
import qualified Cardano.Wallet.IO.Delegation as IODeleg
import qualified Cardano.Wallet.Network as NW
import qualified Cardano.Wallet.Primitive.Ledger.Convert as Convert
import qualified Cardano.Wallet.Primitive.Types as W
Expand Down Expand Up @@ -2144,7 +2144,7 @@ selectCoinsForJoin ctx knownPools getPoolStatus poolId walletId = do
poolStatus <- liftIO $ getPoolStatus poolId
pools <- liftIO knownPools
withWorkerCtx ctx walletId liftE liftE $ \workerCtx -> liftIO $ do
W.CoinSelection{..} <- Cardano.Wallet.IO.Delegation.selectCoinsForJoin
W.CoinSelection{..} <- IODeleg.selectCoinsForJoin
workerCtx
pools
poolId
Expand Down Expand Up @@ -2177,7 +2177,7 @@ selectCoinsForQuit
selectCoinsForQuit ctx (ApiT walletId) = do
withWorkerCtx ctx walletId liftE liftE $ \workerCtx -> liftIO $ do
W.CoinSelection{..} <-
Cardano.Wallet.IO.Delegation.selectCoinsForQuit workerCtx
IODeleg.selectCoinsForQuit workerCtx
pure ApiCoinSelection
{ inputs = mkApiCoinSelectionInput <$> inputs
, outputs = mkApiCoinSelectionOutput <$> outputs
Expand Down Expand Up @@ -2736,15 +2736,15 @@ constructTransaction api argGenChange knownPools poolStatus apiWalletId body = d
currentEpochSlotting <- liftIO $ getCurrentEpochSlotting netLayer
optionalDelegationAction <- liftIO $
forM delegationRequest $
Cardano.Wallet.IO.Delegation.handleDelegationRequest
IODeleg.handleDelegationRequest
wrk
currentEpochSlotting knownPools
poolStatus withdrawal

optionalVoteAction <- case (body ^. #vote) of
Just (ApiT action) ->
liftIO $ Just <$>
Cardano.Wallet.IO.Delegation.voteAction wrk action
IODeleg.voteAction wrk action
Nothing ->
pure Nothing

Expand Down Expand Up @@ -3286,13 +3286,13 @@ constructSharedTransaction

optionalDelegationAction <- liftIO $
forM delegationRequest $
Cardano.Wallet.IO.Delegation.handleDelegationRequest
IODeleg.handleDelegationRequest
wrk currentEpochSlotting knownPools
getPoolStatus NoWithdrawal

optionalVoteAction <- case (body ^. #vote) of
Just (ApiT action) -> liftIO $ Just <$>
Cardano.Wallet.IO.Delegation.voteAction wrk action
IODeleg.voteAction wrk action
Nothing -> pure Nothing

let txCtx = defaultTransactionCtx
Expand Down Expand Up @@ -3961,7 +3961,7 @@ joinStakePool

withWorkerCtx ctx walletId liftE liftE $ \wrk -> do
(BuiltTx{..}, txTime) <- liftIO
$ Cardano.Wallet.IO.Delegation.joinStakePool
$ IODeleg.joinStakePool
wrk
walletId
pools
Expand Down Expand Up @@ -4025,7 +4025,7 @@ quitStakePool ctx@ApiLayer{..} (ApiT walletId) body = do

withWorkerCtx ctx walletId liftE liftE $ \wrk -> do
(BuiltTx{..}, txTime) <- liftIO
$ Cardano.Wallet.IO.Delegation.quitStakePool
$ IODeleg.quitStakePool
wrk
walletId
(coerce $ getApiT $ body ^. #passphrase)
Expand Down
Loading
Loading