From cc1fee97d097800991cd60aae6f9fbf44ff5f5fc Mon Sep 17 00:00:00 2001 From: Sebastian Nagel Date: Thu, 13 Apr 2023 18:23:36 +0200 Subject: [PATCH] Move genTxIn and fix imports in Hydra.Ledger.Cardano --- hydra-node/src/Hydra/Chain/Direct/State.hs | 6 ++-- hydra-node/src/Hydra/Ledger/Cardano.hs | 36 ++++++++-------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/hydra-node/src/Hydra/Chain/Direct/State.hs b/hydra-node/src/Hydra/Chain/Direct/State.hs index a62ca248e4f..3e4e19bf9ed 100644 --- a/hydra-node/src/Hydra/Chain/Direct/State.hs +++ b/hydra-node/src/Hydra/Chain/Direct/State.hs @@ -36,6 +36,7 @@ import Hydra.Cardano.Api ( UTxO' (UTxO), Value, chainPointToSlotNo, + genTxIn, modifyTxOutValue, selectLovelace, txIns', @@ -101,7 +102,7 @@ import Hydra.ContestationPeriod (ContestationPeriod) import Hydra.Crypto (HydraKey) import Hydra.Data.ContestationPeriod (posixToUTCTime) import Hydra.Ledger (IsTx (hashUTxO)) -import Hydra.Ledger.Cardano (genOneUTxOFor, genTxIn, genUTxOAdaOnlyOfSize, genVerificationKey) +import Hydra.Ledger.Cardano (genOneUTxOFor, genUTxOAdaOnlyOfSize, genVerificationKey) import Hydra.Ledger.Cardano.Evaluate (genPointInTimeBefore, genValidityBoundsFromContestationPeriod, slotNoFromUTCTime) import Hydra.Ledger.Cardano.Json () import Hydra.Options (maximumNumberOfParties) @@ -360,7 +361,8 @@ commit ctx st utxo = do rejectMoreThanMainnetLimit :: NetworkId -> TxOut CtxUTxO -> Either (PostTxError Tx) () rejectMoreThanMainnetLimit network output = when (network == Mainnet && lovelaceAmt > maxMainnetLovelace) $ - Left $ CommittedTooMuchADAForMainnet lovelaceAmt maxMainnetLovelace + Left $ + CommittedTooMuchADAForMainnet lovelaceAmt maxMainnetLovelace where lovelaceAmt = selectLovelace (txOutValue output) diff --git a/hydra-node/src/Hydra/Ledger/Cardano.hs b/hydra-node/src/Hydra/Ledger/Cardano.hs index 301fde8d8ca..2bfc202253e 100644 --- a/hydra-node/src/Hydra/Ledger/Cardano.hs +++ b/hydra-node/src/Hydra/Ledger/Cardano.hs @@ -14,9 +14,10 @@ import Hydra.Cardano.Api hiding (initialLedgerState) import Hydra.Ledger.Cardano.Builder import qualified Cardano.Api.UTxO as UTxO -import Cardano.Binary (decodeAnnotator, serialize', unsafeDeserialize') +import Cardano.Binary (decodeAnnotator, serialize') import qualified Cardano.Crypto.DSIGN as CC import qualified Cardano.Ledger.Babbage.Tx as Ledger +import Cardano.Ledger.BaseTypes (StrictMaybe (..)) import qualified Cardano.Ledger.BaseTypes as Ledger import qualified Cardano.Ledger.Credential as Ledger import qualified Cardano.Ledger.Shelley.API.Mempool as Ledger @@ -24,7 +25,6 @@ import qualified Cardano.Ledger.Shelley.Genesis as Ledger import qualified Cardano.Ledger.Shelley.LedgerState as Ledger import qualified Cardano.Ledger.Shelley.Rules.Ledger as Ledger import qualified Cardano.Ledger.Shelley.UTxO as Ledger -import qualified Cardano.Ledger.TxIn as Ledger import qualified Codec.CBOR.Decoding as CBOR import qualified Codec.CBOR.Encoding as CBOR import Control.Arrow (left) @@ -33,13 +33,12 @@ import qualified Data.ByteString as BS import Data.Default (def) import qualified Data.Map.Strict as Map import Data.Maybe (fromJust) -import Data.Maybe.Strict (StrictMaybe (..)) import Data.Text.Lazy.Builder (toLazyText) import Formatting.Buildable (build) import qualified Hydra.Contract.Head as Head import Hydra.Ledger (IsTx (..), Ledger (..), ValidationError (..)) import Hydra.Ledger.Cardano.Json () -import Plutus.V2.Ledger.Api (fromBuiltin) +import PlutusLedgerApi.V2 (fromBuiltin) import Test.Cardano.Ledger.Babbage.Serialisation.Generators () import Test.QuickCheck ( choose, @@ -127,7 +126,7 @@ instance Arbitrary Tx where -- TODO: shrinker! arbitrary = fromLedgerTx . withoutProtocolUpdates <$> arbitrary where - withoutProtocolUpdates tx@(Ledger.ValidatedTx body _ _ _) = + withoutProtocolUpdates tx@(Ledger.AlonzoTx body _ _ _) = let body' = body{Ledger.txUpdates = SNothing} in tx{Ledger.body = body'} @@ -152,14 +151,14 @@ mkSimpleTx (txin, TxOut owner valueIn datum refScript) (recipient, valueOut) sk } outs = - TxOut @CtxTx recipient valueOut TxOutDatumNone ReferenceScriptNone : - [ TxOut @CtxTx - owner - (valueIn <> negateValue valueOut) - (toTxContext datum) - refScript - | valueOut /= valueIn - ] + TxOut @CtxTx recipient valueOut TxOutDatumNone ReferenceScriptNone + : [ TxOut @CtxTx + owner + (valueIn <> negateValue valueOut) + (toTxContext datum) + refScript + | valueOut /= valueIn + ] fee = Lovelace 0 @@ -218,14 +217,6 @@ generateOneTransfer networkId (utxo, (_, sender), txs) _ = do _ -> error "Couldn't generate transaction sequence: need exactly one UTXO." --- | A more random generator than the 'Arbitrary TxIn' from cardano-ledger. -genTxIn :: Gen TxIn -genTxIn = - fmap fromLedgerTxIn . Ledger.TxIn - -- NOTE: [88, 32] is a CBOR prefix for a bytestring of 32 bytes. - <$> fmap (unsafeDeserialize' . BS.pack . ([88, 32] <>)) (vectorOf 32 arbitrary) - <*> fmap Ledger.TxIx (choose (0, 99)) - -- TODO: Enable arbitrary datum in generators -- TODO: This should better be called 'genOutputFor' genOutput :: @@ -387,9 +378,6 @@ shrinkValue = instance Arbitrary AssetName where arbitrary = AssetName . BS.take 32 <$> arbitrary -instance Arbitrary TxIn where - arbitrary = genTxIn - instance Arbitrary TxId where arbitrary = onlyTxId <$> arbitrary where