Skip to content

Commit 237ce11

Browse files
committed
Fix testnet queries
1 parent 186ab52 commit 237ce11

File tree

2 files changed

+66
-35
lines changed

2 files changed

+66
-35
lines changed

cardano-testnet/src/Testnet/Components/Query.hs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Testnet.Components.Query
3939
, getProtocolParams
4040
, getGovActionLifetime
4141
, getKeyDeposit
42-
, getDelegationState
42+
, getAccountsStates
4343
, getTxIx
4444
) where
4545

@@ -50,12 +50,11 @@ import qualified Cardano.Api.UTxO as Utxo
5050

5151
import Cardano.Ledger.Api (ConwayGovState)
5252
import qualified Cardano.Ledger.Api as L
53+
import qualified Cardano.Ledger.Api.State.Query as SQ
5354
import qualified Cardano.Ledger.Conway.Governance as L
5455
import qualified Cardano.Ledger.Conway.PParams as L
5556
import qualified Cardano.Ledger.Shelley.LedgerState as L
56-
import qualified Cardano.Ledger.UMap as L
57-
import qualified Cardano.Ledger.Api.State.Query as SQ
58-
import qualified Data.Set as Set
57+
import qualified Cardano.Ledger.State as L
5958

6059
import Prelude
6160

@@ -69,6 +68,7 @@ import qualified Data.Map as Map
6968
import Data.Map.Strict (Map)
7069
import Data.Maybe
7170
import Data.Ord (Down (..))
71+
import qualified Data.Set as Set
7272
import Data.Text (Text)
7373
import qualified Data.Text as T
7474
import Data.Type.Equality
@@ -412,7 +412,7 @@ checkDRepState epochStateView@EpochStateView{nodeConfigPath, socketPath} sbe f =
412412
$ \(AnyNewEpochState actualEra newEpochState _) _slotNumber _blockNumber -> do
413413
Refl <- either error pure $ assertErasEqual sbe actualEra
414414
let dreps =
415-
shelleyBasedEraConstraints sbe
415+
shelleyBasedEraConstraints sbe
416416
$ SQ.queryDRepState newEpochState Set.empty
417417
case f dreps of
418418
Nothing -> pure ConditionNotMet
@@ -467,7 +467,7 @@ getTreasuryValue
467467
-> m L.Coin -- ^ The current value of the treasury
468468
getTreasuryValue epochStateView = withFrozenCallStack $ do
469469
AnyNewEpochState _ newEpochState _ <- getEpochState epochStateView
470-
pure $ newEpochState ^. L.nesEpochStateL . L.epochStateTreasuryL
470+
pure $ newEpochState ^. L.nesEpochStateL . L.treasuryL
471471

472472
-- | Obtain minimum deposit amount for governance action from node
473473
getMinGovActionDeposit
@@ -590,19 +590,21 @@ getKeyDeposit epochStateView ceo = conwayEraOnwardsConstraints ceo $ do
590590
. L.ppKeyDepositL
591591

592592

593-
-- | Returns delegation state from the epoch state.
594-
getDelegationState :: (H.MonadAssertion m, MonadTest m, MonadIO m)
593+
-- | Returns staking accounts state
594+
getAccountsStates :: (H.MonadAssertion m, MonadTest m, MonadIO m)
595595
=> EpochStateView
596-
-> m L.StakeCredentials
597-
getDelegationState epochStateView = do
596+
-> ShelleyBasedEra era
597+
-> m (Map (L.Credential L.Staking) (L.AccountState (ShelleyLedgerEra era)))
598+
getAccountsStates epochStateView sbe' = shelleyBasedEraConstraints sbe' $ do
598599
AnyNewEpochState sbe newEpochState _ <- getEpochState epochStateView
599-
let pools = shelleyBasedEraConstraints sbe $ newEpochState
600-
^. L.nesEsL
601-
. L.esLStateL
602-
. L.lsCertStateL
603-
. L.certDStateL
604-
605-
pure $ L.toStakeCredentials pools
600+
Refl <- H.nothingFail $ testEquality sbe sbe'
601+
pure $ newEpochState
602+
^. L.nesEsL
603+
. L.esLStateL
604+
. L.lsCertStateL
605+
. L.certDStateL
606+
. L.accountsL
607+
. L.accountsMapL
606608

607609
-- | Returns the transaction index of a transaction with a given amount and ID.
608610
getTxIx :: forall m era. HasCallStack

cardano-testnet/src/Testnet/Process/Cli/SPO.hs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module Testnet.Process.Cli.SPO
1818
import Cardano.Api hiding (cardanoEra)
1919
import qualified Cardano.Api.Ledger as L
2020

21-
import qualified Cardano.Ledger.Api.State.Query as L
2221
import qualified Cardano.Ledger.Shelley.LedgerState as L
22+
import qualified Cardano.Ledger.State as L
2323
import qualified Cardano.Ledger.UMap as L
2424

2525
import Control.Monad
@@ -130,30 +130,59 @@ checkStakeKeyRegistered tempAbsP nodeConfigFile sPath terminationEpoch execConfi
130130
]
131131
where
132132
handler :: StakeAddress -> AnyNewEpochState -> SlotNo -> BlockNo -> StateT DelegationsAndRewards IO ConditionResult
133-
handler (StakeAddress network sCred) (AnyNewEpochState sbe newEpochState _) _ _ =
134-
let umap = shelleyBasedEraConstraints sbe $ newEpochState ^. L.nesEsL . L.epochStateUMapL
135-
dag = L.filterStakePoolDelegsAndRewards umap $ Set.singleton sCred
136-
allStakeCredentials = umap ^. L.umElemsL -- This does not include pointer addresses
137-
delegsAndRewards = shelleyBasedEraConstraints sbe $ toDelegationsAndRewards network sbe dag
138-
in case Map.lookup sCred allStakeCredentials of
139-
Nothing -> return ConditionNotMet
140-
Just _ -> StateT.put delegsAndRewards >> return ConditionMet
133+
handler (StakeAddress network sCred) (AnyNewEpochState sbe newEpochState _) _ _ = shelleyBasedEraConstraints sbe $ do
134+
let accountsMap = newEpochState
135+
^. L.nesEsL
136+
. L.esLStateL
137+
. L.lsCertStateL
138+
. L.certDStateL
139+
. L.accountsL
140+
. L.accountsMapL
141+
142+
143+
-- let umap = shelleyBasedEraConstraints sbe $ newEpochState ^. L.nesEsL . L.epochStateUMapL
144+
-- dag = L.filterStakePoolDelegsAndRewards umap $ Set.singleton sCred
145+
-- allStakeCredentials = umap ^. L.umElemsL -- This does not include pointer addresses
146+
-- delegsAndRewards = shelleyBasedEraConstraints sbe $ toDelegationsAndRewards network sbe dag
147+
148+
case Map.lookup sCred accountsMap of
149+
Nothing -> pure ConditionNotMet
150+
Just _ -> do
151+
StateT.put $ toDelegationsAndRewards sbe network accountsMap
152+
pure ConditionMet
141153

142154
toDelegationsAndRewards
143-
:: L.Network
144-
-> ShelleyBasedEra era
145-
-> (Map (L.Credential L.Staking) (L.KeyHash L.StakePool), Map (L.Credential 'L.Staking) L.Coin)
155+
:: ShelleyBasedEra era
156+
-> L.Network
157+
-> Map (L.Credential L.Staking) (L.AccountState (ShelleyLedgerEra era))
146158
-> DelegationsAndRewards
147-
toDelegationsAndRewards n _ (delegationMap, rewardsMap) =
148-
let apiDelegationMap = Map.map toApiPoolId $ Map.mapKeys (toApiStakeAddress n) delegationMap
149-
apiRewardsMap = Map.mapKeys (toApiStakeAddress n) rewardsMap
150-
in DelegationsAndRewards (apiRewardsMap, apiDelegationMap)
159+
toDelegationsAndRewards sbe n accountsMap = do
160+
let accountsMap' = Map.mapKeys (toApiStakeAddress n) accountsMap
161+
let apiDelegationMap = Map.mapMaybe (toApiPoolId sbe) accountsMap'
162+
apiRewardsMap = Map.map (toBalance sbe) accountsMap'
163+
DelegationsAndRewards (apiRewardsMap, apiDelegationMap)
164+
165+
-- toApiPoolId :: L.KeyHash L.StakePool -> PoolId
166+
toApiPoolId :: ShelleyBasedEra era
167+
-> L.AccountState (ShelleyLedgerEra era)
168+
-> Maybe PoolId
169+
toApiPoolId sbe accountState =
170+
fmap StakePoolKeyHash $
171+
shelleyBasedEraConstraints sbe $
172+
accountState ^. L.stakePoolDelegationAccountStateL
173+
174+
175+
toBalance :: ShelleyBasedEra era
176+
-> L.AccountState (ShelleyLedgerEra era)
177+
-> L.Coin
178+
toBalance sbe accountState =
179+
shelleyBasedEraConstraints sbe $
180+
accountState ^. L.balanceAccountStateL . to L.fromCompact
181+
151182

152183
toApiStakeAddress :: L.Network -> L.Credential 'L.Staking -> StakeAddress
153184
toApiStakeAddress = StakeAddress
154185

155-
toApiPoolId :: L.KeyHash L.StakePool -> PoolId
156-
toApiPoolId = StakePoolKeyHash
157186

158187
createStakeDelegationCertificate
159188
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack)

0 commit comments

Comments
 (0)