Skip to content

Commit

Permalink
Query tip slot number in Haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Oct 25, 2021
1 parent 37b4d36 commit b3f4e50
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
15 changes: 14 additions & 1 deletion local-cluster/src/CardanoClient.hs
Expand Up @@ -12,7 +12,7 @@ import Cardano.Api

import Cardano.Api.Shelley (ProtocolParameters (protocolParamTxFeeFixed, protocolParamTxFeePerByte), VerificationKey (PaymentVerificationKey))
import Cardano.CLI.Shelley.Run.Address (buildShelleyAddress)
import Cardano.CLI.Shelley.Run.Query (executeQuery)
import Cardano.CLI.Shelley.Run.Query (executeQuery, queryQueryTip)
import qualified Cardano.Ledger.Keys as Keys
import qualified Data.Set as Set
import qualified Hydra.Chain.Direct.Wallet as Hydra
Expand Down Expand Up @@ -74,6 +74,19 @@ runQuery networkId socket query =
cardanoModeParams = CardanoModeParams $ EpochSlots defaultByronEpochSlots
localNodeConnectInfo = LocalNodeConnectInfo cardanoModeParams networkId socket

queryTipSlotNo :: NetworkId -> FilePath -> IO SlotNo
queryTipSlotNo networkId socket = do
tip <- fst <$> queryQueryTip localNodeConnectInfo Nothing
pure $ case tip of
ChainTipAtGenesis -> 0
ChainTip slotNo _ _ -> slotNo
where
-- NOTE(AB): extracted from Parsers in cardano-cli, this is needed to run in 'cardanoMode' which
-- is the default for cardano-cli
defaultByronEpochSlots = 21600 :: Word64
cardanoModeParams = CardanoModeParams $ EpochSlots defaultByronEpochSlots
localNodeConnectInfo = LocalNodeConnectInfo cardanoModeParams networkId socket

-- | Build a "raw" transaction from a bunch of inputs, outputs and fees.
buildRaw :: [TxIn] -> [TxOut AlonzoEra] -> SlotNo -> Lovelace -> IO (TxBody AlonzoEra)
buildRaw txIns txOuts invalidAfter fee = do
Expand Down
12 changes: 7 additions & 5 deletions local-cluster/test/Test/LocalClusterSpec.hs
Expand Up @@ -8,6 +8,7 @@ import Cardano.Api (
Lovelace,
MultiAssetSupportedInEra (MultiAssetInAlonzoEra),
ShelleyAddr,
SlotNo (SlotNo),
TxIn (TxIn),
TxIx (TxIx),
TxOut (TxOut),
Expand All @@ -21,7 +22,7 @@ import Cardano.Api (
shelleyAddressInEra,
)
import Cardano.Api.Shelley (Lovelace (Lovelace))
import CardanoClient (Sizes (..), buildAddress, buildRaw, calculateMinFee, defaultSizes, queryProtocolParameters, queryUtxo)
import CardanoClient (Sizes (..), buildAddress, buildRaw, calculateMinFee, defaultSizes, queryProtocolParameters, queryTipSlotNo, queryUtxo)
import CardanoCluster (ClusterConfig (..), ClusterLog (..), RunningCluster (..), keysFor, testClusterConfig, withCluster)
import CardanoNode (ChainTip (..), RunningNode (..), cliQueryTip)
import qualified Data.Map as Map
Expand Down Expand Up @@ -75,13 +76,13 @@ assertCanSpendInitialFunds = \case
rawTx <- buildRaw [txIn] [TxOut (shelleyAddressInEra addr) (TxOutValue MultiAssetInAlonzoEra (lovelaceToValue 100_000_000)) TxOutDatumHashNone] 0 0
pparams <- queryProtocolParameters networkId socket
let fee = calculateMinFee networkId rawTx defaultSizes{inputs = 1, outputs = 2, witnesses = 1} pparams

runTestScript nodeDirectory addr txIn amount fee socket
slotNo <- queryTipSlotNo networkId socket
runTestScript nodeDirectory addr txIn amount slotNo fee socket
_ ->
error "empty cluster?"

runTestScript :: FilePath -> Address ShelleyAddr -> TxIn -> Lovelace -> Lovelace -> FilePath -> IO ()
runTestScript nodeDirectory addr (TxIn txId (TxIx txIx)) (Lovelace amount) (Lovelace fee) socket = do
runTestScript :: FilePath -> Address ShelleyAddr -> TxIn -> Lovelace -> SlotNo -> Lovelace -> FilePath -> IO ()
runTestScript nodeDirectory addr (TxIn txId (TxIx txIx)) (Lovelace amount) (SlotNo slot) (Lovelace fee) socket = do
inputScript <- Pkg.getDataFileName "test_submit.sh"
currentEnv <- getEnvironment
let scriptOutput = nodeDirectory </> "test_submit.out"
Expand All @@ -101,6 +102,7 @@ runTestScript nodeDirectory addr (TxIn txId (TxIx txIx)) (Lovelace amount) (Love
unpack $ serialiseToRawBytesHexText txId <> "#" <> show txIx
, show amount
, show fee
, show slot
]
)
{ env = Just (socketEnv : baseEnv)
Expand Down
9 changes: 4 additions & 5 deletions local-cluster/test_submit.sh
Expand Up @@ -9,14 +9,13 @@ utxo_addr=$1
utxo=$2
amount=$3
fees=$4
slot=$5

transfer_amount=100000000

slot=$(cardano-cli query tip --testnet-magic 42 | jq .slot)

cardano-cli transaction build-raw --tx-in $alice_txin \
--tx-out $alice_addr+$transfer_amount \
--tx-out $alice_addr+$(($alice_fortune - $transfer_amount - $fees)) \
cardano-cli transaction build-raw --tx-in $utxo \
--tx-out $utxo_addr+$transfer_amount \
--tx-out $utxo_addr+$(($amount - $transfer_amount - $fees)) \
--invalid-hereafter $((slot + 100)) --fee $fees --out-file tx.draft

cardano-cli transaction sign --tx-body-file tx.draft --signing-key-file ../alice.sk --testnet-magic 42 --out-file tx.signed
Expand Down

0 comments on commit b3f4e50

Please sign in to comment.