Skip to content

Commit fa3d95a

Browse files
committed
cardano-testnet | Add test for providing reference input datums
1 parent c75741e commit fa3d95a

File tree

6 files changed

+363
-15
lines changed

6 files changed

+363
-15
lines changed

cardano-testnet/cardano-testnet.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ test-suite cardano-testnet-test
185185

186186
main-is: cardano-testnet-test.hs
187187

188-
other-modules: Cardano.Testnet.Test.Cli.Conway.StakeSnapshot
188+
other-modules: Cardano.Testnet.Test.Api.TxReferenceInputDatum
189+
Cardano.Testnet.Test.Cli.Conway.StakeSnapshot
189190
Cardano.Testnet.Test.Cli.KesPeriodInfo
190191
Cardano.Testnet.Test.Cli.LeadershipSchedule
191192
Cardano.Testnet.Test.Cli.Query

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Testnet.Components.Query
3636
, checkDRepsNumber
3737
, checkDRepState
3838
, assertNewEpochState
39+
, getProtocolParams
3940
, getGovActionLifetime
4041
, getKeyDeposit
4142
, getDelegationState
@@ -44,8 +45,9 @@ module Testnet.Components.Query
4445

4546
import Cardano.Api as Api
4647
import Cardano.Api.Ledger (Credential, DRepState, EpochInterval (..), KeyRole (DRepRole))
47-
import Cardano.Api.Shelley (ShelleyLedgerEra)
4848
import qualified Cardano.Api.Ledger as L
49+
import Cardano.Api.Shelley (LedgerProtocolParameters (..), ShelleyLedgerEra)
50+
import qualified Cardano.Api.Tx.UTxO as Utxo
4951

5052
import Cardano.Crypto.Hash (hashToStringAsHex)
5153
import Cardano.Ledger.Api (ConwayGovState)
@@ -65,7 +67,6 @@ import Data.IORef
6567
import Data.List (sortOn)
6668
import qualified Data.Map as Map
6769
import Data.Map.Strict (Map)
68-
import qualified Data.Map.Strict as M
6970
import Data.Maybe
7071
import Data.Ord (Down (..))
7172
import Data.Text (Text)
@@ -301,11 +302,11 @@ findAllUtxos
301302
=> MonadTest m
302303
=> EpochStateView
303304
-> ShelleyBasedEra era
304-
-> m (Map TxIn (TxOut CtxUTxO era))
305+
-> m (UTxO era)
305306
findAllUtxos epochStateView sbe = withFrozenCallStack $ do
306307
AnyNewEpochState sbe' _ tbs <- getEpochState epochStateView
307308
Refl <- H.leftFail $ assertErasEqual sbe sbe'
308-
pure $ getLedgerTablesUTxOValues sbe' tbs
309+
pure . UTxO $ getLedgerTablesUTxOValues sbe' tbs
309310

310311
-- | Retrieve utxos from the epoch state view for an address.
311312
findUtxosWithAddress
@@ -316,7 +317,7 @@ findUtxosWithAddress
316317
=> EpochStateView
317318
-> ShelleyBasedEra era
318319
-> Text -- ^ Address
319-
-> m (Map TxIn (TxOut CtxUTxO era))
320+
-> m (UTxO era)
320321
findUtxosWithAddress epochStateView sbe address = withFrozenCallStack $ do
321322
utxos <- findAllUtxos epochStateView sbe
322323
H.note_ $ "Finding UTxOs for " <> T.unpack address
@@ -327,7 +328,7 @@ findUtxosWithAddress epochStateView sbe address = withFrozenCallStack $ do
327328
maybeToEither ("Could not deserialize address: " <> T.unpack address)
328329
(deserialiseAddress AsAddressAny address)
329330

330-
let utxos' = M.filter (\(TxOut txAddr _ _ _) -> txAddr == address') utxos
331+
let utxos' = Utxo.filter (\(TxOut txAddr _ _ _) -> txAddr == address') utxos
331332
H.note_ $ unlines (map show $ toList utxos')
332333
pure utxos'
333334
where
@@ -344,7 +345,7 @@ findLargestUtxoWithAddress
344345
-> Text -- ^ Address
345346
-> m (Maybe (TxIn, TxOut CtxUTxO era))
346347
findLargestUtxoWithAddress epochStateView sbe address = withFrozenCallStack $ do
347-
utxos <- M.assocs <$> findUtxosWithAddress epochStateView sbe address
348+
utxos <- toList <$> findUtxosWithAddress epochStateView sbe address
348349
pure
349350
. listToMaybe
350351
$ sortOn (\(_, TxOut _ txOutValue _ _) -> Down $ txOutValueToLovelace txOutValue) utxos
@@ -558,6 +559,15 @@ assertNewEpochState epochStateView sbe maxWait lens expected = withFrozenCallSta
558559
Refl <- H.leftFail $ assertErasEqual sbe actualEra
559560
pure $ newEpochState ^. lens
560561

562+
-- | Return current protocol parameters from the governance state
563+
getProtocolParams :: (H.MonadAssertion m, MonadTest m, MonadIO m)
564+
=> EpochStateView
565+
-> ConwayEraOnwards era
566+
-> m (LedgerProtocolParameters era)
567+
getProtocolParams epochStateView ceo = conwayEraOnwardsConstraints ceo $ do
568+
govState :: ConwayGovState era <- getGovState epochStateView ceo
569+
pure . LedgerProtocolParameters $ govState ^. L.cgsCurPParamsL
570+
561571

562572
-- | Obtains the @govActionLifetime@ from the protocol parameters.
563573
-- The @govActionLifetime@ or governance action maximum lifetime in epochs is

cardano-testnet/src/Testnet/Types.hs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Testnet.Types
2121
, testnetSprockets
2222
, TestnetNode(..)
2323
, nodeSocketPath
24+
, nodeConnectionInfo
2425
, isTestnetNodeSpo
2526
, SpoNodeKeys(..)
2627
, Delegator(..)
@@ -62,18 +63,20 @@ import Data.List (intercalate)
6263
import Data.Maybe
6364
import Data.MonoTraversable (Element, MonoFunctor (..))
6465
import Data.Text (Text)
65-
import Data.Time.Clock (UTCTime)
6666
import GHC.Exts (IsString (..))
6767
import GHC.Generics (Generic)
6868
import qualified GHC.IO.Handle as IO
6969
import GHC.Stack
70+
import Lens.Micro (ix, (^?))
7071
import Network.Socket (HostAddress, PortNumber, hostAddressToTuple, tupleToHostAddress)
7172
import System.FilePath
7273
import qualified System.Process as IO
7374

7475
import Testnet.Start.Types
7576

77+
import Hedgehog (MonadTest)
7678
import qualified Hedgehog as H
79+
import qualified Hedgehog.Extras as H
7780
import qualified Hedgehog.Extras.Stock as H
7881
import Hedgehog.Extras.Stock.IO.Network.Sprocket (Sprocket (..))
7982

@@ -115,7 +118,7 @@ data SKey k
115118
data TestnetRuntime = TestnetRuntime
116119
{ configurationFile :: !(NodeConfigFile In)
117120
, shelleyGenesisFile :: !FilePath
118-
, testnetMagic :: !Int
121+
, testnetMagic :: !Int -- TODO change to Word32
119122
, testnetNodes :: ![TestnetNode]
120123
, wallets :: ![PaymentKeyInfo]
121124
, delegators :: ![Delegator]
@@ -148,6 +151,23 @@ isTestnetNodeSpo = isJust . poolKeys
148151
nodeSocketPath :: TestnetNode -> SocketPath
149152
nodeSocketPath = File . H.sprocketSystemName . nodeSprocket
150153

154+
-- | Connection data for a node in the testnet
155+
nodeConnectionInfo :: MonadTest m
156+
=> TestnetRuntime
157+
-> Int -- ^ node index, starting from 0
158+
-> m LocalNodeConnectInfo -- ^ fails when there's no node with requested index
159+
nodeConnectionInfo TestnetRuntime{testnetMagic, testnetNodes} index = do
160+
case testnetNodes ^? ix index of
161+
Nothing -> do
162+
H.note_ $ "There is no node in the testnet with index: " <> show index <> ". Number of nodes: " <> show (length testnetNodes)
163+
H.failure
164+
Just node ->
165+
pure LocalNodeConnectInfo
166+
{ localNodeSocketPath= nodeSocketPath node
167+
, localNodeNetworkId=Testnet (NetworkMagic $ fromIntegral testnetMagic)
168+
, localConsensusModeParams=CardanoModeParams $ EpochSlots 21600}
169+
170+
151171
data SpoNodeKeys = SpoNodeKeys
152172
{ poolNodeKeysCold :: KeyPair StakePoolKey
153173
, poolNodeKeysVrf :: KeyPair VrfKey
@@ -187,14 +207,14 @@ getStartTime
187207
=> HasCallStack
188208
=> FilePath
189209
-> TestnetRuntime
190-
-> m UTCTime
210+
-> m SystemStart
191211
getStartTime tempRootPath TestnetRuntime{configurationFile} = withFrozenCallStack $ H.evalEither <=< H.evalIO . runExceptT $ do
192212
byronGenesisFile <-
193213
decodeNodeConfiguration configurationFile >>= \case
194214
NodeProtocolConfigurationCardano NodeByronProtocolConfiguration{npcByronGenesisFile} _ _ _ _ _ ->
195215
pure $ unGenesisFile npcByronGenesisFile
196216
let byronGenesisFilePath = tempRootPath </> byronGenesisFile
197-
G.gdStartTime . G.configGenesisData <$> decodeGenesisFile byronGenesisFilePath
217+
SystemStart . G.gdStartTime . G.configGenesisData <$> decodeGenesisFile byronGenesisFilePath
198218
where
199219
decodeNodeConfiguration :: File NodeConfig In -> ExceptT String IO NodeProtocolConfiguration
200220
decodeNodeConfiguration (File file) = do

0 commit comments

Comments
 (0)