Skip to content

Commit

Permalink
PR1: Slightly optimizing and fixing initial distrubutions handling (#456
Browse files Browse the repository at this point in the history
)

* slightly optimizing and fixing initial distrubutions handling

* updating readme

* openssl in flake
  • Loading branch information
mmontin authored Feb 26, 2025
1 parent e06e833 commit 81b4de2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

- `positivePart` and `negativePart` in `ValueUtils.hs`. Replaced by `Api.split`.
- Redundant logging of errors in mockchain runs.
- Useless minting of non-ADA value in the dummy initial transaction.

### Changed

Expand Down Expand Up @@ -66,6 +67,8 @@
in the excess.
- Transactions that do not involve script are now properly generated without any
- All kinds of scripts can now be used as reference scripts.
- A bug where scripts being paid to in the initial distribution would not be
stored in the MockChain.

## [[4.0.0]](https://github.com/tweag/cooked-validators/releases/tag/v4.0.0) - 2024-06-28

Expand Down
37 changes: 10 additions & 27 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
zlib
xz
glibcLocales
openssl_3_3
postgresql # For pg_config
## We change the way 'blst' is built so that it takes into
## account the current architecture of the processor. This
Expand All @@ -65,6 +66,7 @@
libsodium
zlib
xz
openssl_3_3
postgresql # For cardano-node-emulator
openldap # For freer-extras‽
];
Expand Down
31 changes: 21 additions & 10 deletions src/Cooked/MockChain/MockChainSt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Cooked.Output
import Cooked.Skeleton
import Data.Bifunctor (bimap)
import Data.Default
import Data.Either.Combinators (mapLeft)
import Data.List (foldl')
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
Expand Down Expand Up @@ -163,24 +162,38 @@ mockChainSt0 = MockChainSt def utxoIndex0 Map.empty Map.empty 0
-- * Initial `MockChainSt` from an initial distribution

mockChainSt0From :: InitialDistribution -> MockChainSt
mockChainSt0From i0 = MockChainSt def (utxoIndex0From i0) (datumMap0From i0) (referenceScriptMap0From i0) 0
mockChainSt0From i0 = MockChainSt def (utxoIndex0From i0) (datumMap0From i0) (referenceScriptMap0From i0 <> scriptMap0From i0) 0

-- | Reference scripts from initial distributions should be accounted for in the
-- `MockChainSt` which is done using this function.
referenceScriptMap0From :: InitialDistribution -> Map Script.ValidatorHash (Script.Versioned Script.Validator)
referenceScriptMap0From (InitialDistribution initDist) =
referenceScriptMap0From =
-- This builds a map of entries from the reference scripts contained in the
-- initial distribution
Map.fromList $ mapMaybe unitMaybeFrom initDist
Map.fromList . mapMaybe unitMaybeFrom . unInitialDistribution
where
-- This takes a single output and returns a possible map entry when it
-- contains a reference script
unitMaybeFrom :: TxSkelOut -> Maybe (Script.ValidatorHash, Script.Versioned Script.Validator)
unitMaybeFrom (Pays output) = do
refScript <- view outputReferenceScriptL output
let vScript@(Script.Versioned script version) = toVersionedScript refScript
Api.ScriptHash scriptHash = toScriptHash vScript
return (Script.ValidatorHash scriptHash, Script.Versioned (Script.Validator script) version)
return (Script.ValidatorHash $ Api.getScriptHash $ toScriptHash vScript, Script.Versioned (Script.Validator script) version)

-- | Scripts from initial distributions should be accounted for in the
-- `MockChainSt` which is done using this function.
scriptMap0From :: InitialDistribution -> Map Script.ValidatorHash (Script.Versioned Script.Validator)
scriptMap0From =
-- This builds a map of entries from the scripts contained in the initial
-- distribution
Map.fromList . mapMaybe unitMaybeFrom . unInitialDistribution
where
-- This takes a single output and returns a possible map entry when it
-- contains a script
unitMaybeFrom :: TxSkelOut -> Maybe (Script.ValidatorHash, Script.Versioned Script.Validator)
unitMaybeFrom txSkelOut = do
val <- txSkelOutValidator txSkelOut
return (Script.ValidatorHash $ Api.getScriptHash $ toScriptHash val, val)

-- | Datums from initial distributions should be accounted for in the
-- `MockChainSt` which is done using this function.
Expand Down Expand Up @@ -222,15 +235,13 @@ utxoIndex0From (InitialDistribution initDist) = case mkBody of
where
mkBody :: Either GenerateTxError (Cardano.TxBody Cardano.ConwayEra)
mkBody = do
value <- mapLeft (ToCardanoError "Value error") $ Ledger.toCardanoValue (foldl' (\v -> (v <>) . view txSkelOutValueL) mempty initDist)
let mintValue = flip (Cardano.TxMintValue Cardano.MaryEraOnwardsConway) (Cardano.BuildTxWith mempty) . Cardano.filterValue (/= Cardano.AdaAssetId) $ value
theNetworkId = Cardano.Testnet $ Cardano.NetworkMagic 42
let theNetworkId = Cardano.Testnet $ Cardano.NetworkMagic 42
genesisKeyHash = Cardano.GenesisUTxOKeyHash $ Shelley.KeyHash "23d51e91ae5adc7ae801e9de4cd54175fb7464ec2680b25686bbb194"
inputs = [(Cardano.genesisUTxOPseudoTxIn theNetworkId genesisKeyHash, Cardano.BuildTxWith $ Cardano.KeyWitness Cardano.KeyWitnessForSpending)]
outputs <- mapM (generateTxOut theNetworkId) initDist
left (TxBodyError "Body error") $
Cardano.createAndValidateTransactionBody Cardano.ShelleyBasedEraConway $
Ledger.emptyTxBodyContent {Cardano.txMintValue = mintValue, Cardano.txOuts = outputs, Cardano.txIns = inputs}
Ledger.emptyTxBodyContent {Cardano.txOuts = outputs, Cardano.txIns = inputs}

utxoIndex0 :: Ledger.UtxoIndex
utxoIndex0 = utxoIndex0From def

0 comments on commit 81b4de2

Please sign in to comment.