|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE NumericUnderscores #-} |
| 4 | +{-# LANGUAGE OverloadedLists #-} |
| 5 | +{-# LANGUAGE OverloadedStrings #-} |
| 6 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 7 | + |
| 8 | +module Cardano.Testnet.Test.Api.TxSupplementalDatum |
| 9 | + ( hprop_tx_supp_datum |
| 10 | + ) |
| 11 | +where |
| 12 | + |
| 13 | +import Cardano.Api |
| 14 | +import qualified Cardano.Api.Ledger as L |
| 15 | +import qualified Cardano.Api.Network as Net |
| 16 | +import qualified Cardano.Api.Network as Net.Tx |
| 17 | +import Cardano.Api.Shelley |
| 18 | + |
| 19 | +import Cardano.Testnet |
| 20 | + |
| 21 | +import Prelude |
| 22 | + |
| 23 | +import Control.Monad |
| 24 | +import Data.Default.Class |
| 25 | +import qualified Data.Map.Strict as M |
| 26 | +import Data.Proxy |
| 27 | +import Data.Set (Set) |
| 28 | +import GHC.Exts (IsList (..)) |
| 29 | +import Lens.Micro |
| 30 | + |
| 31 | +import Testnet.Components.Query |
| 32 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 33 | +import Testnet.Types |
| 34 | + |
| 35 | +import Hedgehog (Property, (===)) |
| 36 | +import qualified Hedgehog as H |
| 37 | +import qualified Hedgehog.Extras.Test.Base as H |
| 38 | +import qualified Hedgehog.Extras.Test.TestWatchdog as H |
| 39 | + |
| 40 | +hprop_tx_supp_datum :: Property |
| 41 | +hprop_tx_supp_datum = integrationRetryWorkspace 2 "api-tx-supp-dat" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do |
| 42 | + conf@Conf{tempAbsPath} <- mkConf tempAbsBasePath' |
| 43 | + let tempAbsPath' = unTmpAbsPath tempAbsPath |
| 44 | + |
| 45 | + let ceo = ConwayEraOnwardsConway |
| 46 | + beo = convert ceo |
| 47 | + sbe = convert ceo |
| 48 | + eraProxy = proxyToAsType Proxy |
| 49 | + options = def{cardanoNodeEra = AnyShelleyBasedEra sbe} |
| 50 | + |
| 51 | + tr@TestnetRuntime |
| 52 | + { configurationFile |
| 53 | + , testnetNodes = node0 : _ |
| 54 | + , wallets = wallet0@(PaymentKeyInfo _ addrTxt0) : wallet1 : _ |
| 55 | + } <- |
| 56 | + cardanoTestnetDefault options def conf |
| 57 | + |
| 58 | + systemStart <- H.noteShowM $ getStartTime tempAbsPath' tr |
| 59 | + epochStateView <- getEpochStateView configurationFile (nodeSocketPath node0) |
| 60 | + connectionInfo <- node0ConnectionInfo tr |
| 61 | + pparams <- getProtocolParams epochStateView ceo |
| 62 | + |
| 63 | + -- prepare tx inputs and output address |
| 64 | + H.noteShow_ addrTxt0 |
| 65 | + addr0 <- H.nothingFail $ deserialiseAddress (AsAddressInEra eraProxy) addrTxt0 |
| 66 | + |
| 67 | + let (PaymentKeyInfo _ addrTxt1) = wallet1 |
| 68 | + H.noteShow_ addrTxt1 |
| 69 | + addr1 <- H.nothingFail $ deserialiseAddress (AsAddressInEra eraProxy) addrTxt1 |
| 70 | + |
| 71 | + -- read key witnesses |
| 72 | + [wit0, wit1] :: [ShelleyWitnessSigningKey] <- |
| 73 | + forM [wallet0, wallet1] $ \wallet -> |
| 74 | + H.leftFailM . H.evalIO $ |
| 75 | + readFileTextEnvelopeAnyOf |
| 76 | + [FromSomeType (proxyToAsType Proxy) WitnessGenesisUTxOKey] |
| 77 | + (signingKey $ paymentKeyInfoPair wallet) |
| 78 | + |
| 79 | + -- query node for era history |
| 80 | + epochInfo <- |
| 81 | + (H.leftFail <=< H.leftFailM) . H.evalIO $ |
| 82 | + executeLocalStateQueryExpr connectionInfo Net.VolatileTip $ |
| 83 | + fmap toLedgerEpochInfo <$> queryEraHistory |
| 84 | + |
| 85 | + let scriptData1 = unsafeHashableScriptData $ ScriptDataBytes "CAFEBABE" |
| 86 | + scriptData2 = unsafeHashableScriptData $ ScriptDataBytes "DEADBEEF" |
| 87 | + txDatum1 = |
| 88 | + TxOutDatumHash |
| 89 | + (convert beo) |
| 90 | + (hashScriptDataBytes scriptData1) |
| 91 | + txDatum2 = TxOutDatumInline (convert ceo) scriptData2 |
| 92 | + |
| 93 | + -- Build a first transaction with txout supplemental data |
| 94 | + tx1Utxo <- do |
| 95 | + txIn <- findLargestUtxoForPaymentKey epochStateView sbe wallet0 |
| 96 | + |
| 97 | + -- prepare txout |
| 98 | + let txOutValue = lovelaceToTxOutValue sbe 100_000_000 |
| 99 | + txOuts = |
| 100 | + [ TxOut addr1 txOutValue txDatum1 ReferenceScriptNone |
| 101 | + , TxOut addr1 txOutValue txDatum2 ReferenceScriptNone |
| 102 | + ] |
| 103 | + |
| 104 | + -- build a transaction |
| 105 | + content = |
| 106 | + defaultTxBodyContent sbe |
| 107 | + & setTxIns [(txIn, pure $ KeyWitness KeyWitnessForSpending)] |
| 108 | + & setTxOuts txOuts |
| 109 | + & setTxProtocolParams (pure $ pure pparams) |
| 110 | + |
| 111 | + utxo <- UTxO <$> findAllUtxos epochStateView sbe |
| 112 | + |
| 113 | + BalancedTxBody _ txBody _ fee <- |
| 114 | + H.leftFail $ |
| 115 | + makeTransactionBodyAutoBalance |
| 116 | + sbe |
| 117 | + systemStart |
| 118 | + epochInfo |
| 119 | + pparams |
| 120 | + mempty |
| 121 | + mempty |
| 122 | + mempty |
| 123 | + utxo |
| 124 | + content |
| 125 | + addr0 |
| 126 | + Nothing -- keys override |
| 127 | + H.noteShow_ fee |
| 128 | + |
| 129 | + let tx = signShelleyTransaction sbe txBody [wit0] |
| 130 | + txId <- H.noteShow . getTxId $ getTxBody tx |
| 131 | + |
| 132 | + H.evalIO (submitTxToNodeLocal connectionInfo (TxInMode sbe tx)) >>= \case |
| 133 | + Net.Tx.SubmitFail reason -> H.noteShow_ reason >> H.failure |
| 134 | + Net.Tx.SubmitSuccess -> H.success |
| 135 | + |
| 136 | + -- wait till transaction gets included in the block |
| 137 | + _ <- waitForBlocks epochStateView 1 |
| 138 | + |
| 139 | + -- test if it's in UTxO set |
| 140 | + utxo1 <- findAllUtxos epochStateView sbe |
| 141 | + let txUtxo = M.filterWithKey (\(TxIn txId' _) _ -> txId == txId') utxo1 |
| 142 | + 3 === length txUtxo |
| 143 | + |
| 144 | + let chainTxOuts = |
| 145 | + reverse |
| 146 | + . drop 1 |
| 147 | + . reverse |
| 148 | + . map (fromCtxUTxOTxOut . snd) |
| 149 | + . toList |
| 150 | + $ M.filterWithKey (\(TxIn txId' _) _ -> txId == txId') utxo1 |
| 151 | + |
| 152 | + txOuts === chainTxOuts |
| 153 | + |
| 154 | + pure txUtxo |
| 155 | + |
| 156 | + do |
| 157 | + [(txIn1, _)] <- pure $ filter (\(_, TxOut _ _ datum _) -> datum == txDatum1) $ toList tx1Utxo |
| 158 | + [(txIn2, _)] <- pure $ filter (\(_, TxOut _ _ datum _) -> datum == txDatum2) $ toList tx1Utxo |
| 159 | + |
| 160 | + let scriptData3 = unsafeHashableScriptData $ ScriptDataBytes "C0FFEE" |
| 161 | + txDatum = TxOutDatumInline (convert ceo) scriptData3 |
| 162 | + txOutValue = lovelaceToTxOutValue sbe 99_999_500 |
| 163 | + txOut = TxOut addr0 txOutValue txDatum ReferenceScriptNone |
| 164 | + |
| 165 | + let content = |
| 166 | + defaultTxBodyContent sbe |
| 167 | + & setTxIns [(txIn1, pure $ KeyWitness KeyWitnessForSpending)] |
| 168 | + & setTxInsReference (TxInsReference beo [txIn2]) |
| 169 | + & setTxFee (TxFeeExplicit sbe 500) |
| 170 | + & setTxOuts [txOut] |
| 171 | + |
| 172 | + txBody@(ShelleyTxBody _ _ _ (TxBodyScriptData _ (L.TxDats' datums) _) _ _) <- |
| 173 | + H.leftFail $ createTransactionBody sbe content |
| 174 | + let bodyScriptData = fromList . map fromAlonzoData $ M.elems datums :: Set HashableScriptData |
| 175 | + -- TODO why bodyScriptData is empty here? |
| 176 | + [scriptData1, scriptData2, scriptData3] === bodyScriptData |
| 177 | + |
| 178 | + let tx = signShelleyTransaction sbe txBody [wit1] |
| 179 | + -- H.noteShowPretty_ tx |
| 180 | + txId <- H.noteShow . getTxId $ getTxBody tx |
| 181 | + |
| 182 | + H.evalIO (submitTxToNodeLocal connectionInfo (TxInMode sbe tx)) >>= \case |
| 183 | + Net.Tx.SubmitFail reason -> H.noteShow_ reason >> H.failure |
| 184 | + Net.Tx.SubmitSuccess -> H.success |
| 185 | + |
| 186 | + -- wait till transaction gets included in the block |
| 187 | + _ <- waitForBlocks epochStateView 1 |
| 188 | + |
| 189 | + -- test if it's in UTxO set |
| 190 | + utxo1 <- findAllUtxos epochStateView sbe |
| 191 | + let txUtxo = M.filterWithKey (\(TxIn txId' _) _ -> txId == txId') utxo1 |
| 192 | + [txOut] === M.elems txUtxo |
| 193 | + |
| 194 | + H.failure |
0 commit comments