Skip to content

Commit

Permalink
Move genTxIn and fix imports in Hydra.Ledger.Cardano
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Apr 20, 2023
1 parent bbf3691 commit cc1fee9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
6 changes: 4 additions & 2 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Hydra.Cardano.Api (
UTxO' (UTxO),
Value,
chainPointToSlotNo,
genTxIn,
modifyTxOutValue,
selectLovelace,
txIns',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
36 changes: 12 additions & 24 deletions hydra-node/src/Hydra/Ledger/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ 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
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)
Expand All @@ -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,
Expand Down Expand Up @@ -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'}

Expand All @@ -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

Expand Down Expand Up @@ -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 ::
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cc1fee9

Please sign in to comment.