Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit changes #445

Merged
merged 62 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
579ecbe
tests pass with new mockchain return type
Jul 4, 2024
fc36439
more log levels
Jul 4, 2024
38c0718
logging removal of unusable balancing utxos
Jul 4, 2024
b525aeb
improving logging in balancing
mmontin Jul 8, 2024
98b5e81
new log version with dedicated constructors
mmontin Jul 10, 2024
47f54a5
changing item
mmontin Jul 10, 2024
2ff0e46
integrating comments, adding comments and more readable bullets
mmontin Jul 10, 2024
73d8ab2
fixing the bug where collateral inputs were not resolved
mmontin Jul 10, 2024
a94db0a
CHANGELOG.md
mmontin Jul 10, 2024
580b35c
merging main in this
Jul 28, 2024
f2cfce6
integrating review comments
Jul 28, 2024
8764a1a
typo
Jul 28, 2024
798c8c3
removing useless instances
Jul 28, 2024
492d057
wip
mmontin Jul 7, 2024
817c777
reverting balancingspec
mmontin Jul 10, 2024
c59f14c
starting to consume scripts in balancing spec, to be continued
mmontin Jul 10, 2024
96b3250
reworking empty collaterals
mmontin Jul 11, 2024
34aa2af
2 first test groups passé
mmontin Jul 11, 2024
291df89
all tests fixed
mmontin Jul 11, 2024
5f696fd
doc
mmontin Jul 11, 2024
c8cf373
updating doc
mmontin Jul 11, 2024
73c209f
logging of unused collateral option
mmontin Jul 11, 2024
2275916
post-rebase small fixes
Jul 28, 2024
f44b98e
bye bye Ledger.TxOut
Jul 28, 2024
6ffc16e
Merge branch 'main' into mm/logger
Jul 29, 2024
089191f
Merge branch 'mm/logger' into mm/collateral-when-no-script
Jul 29, 2024
47e99e6
Merge branch 'mm/collateral-when-no-script' into mm/txout
Jul 29, 2024
14da58f
update capi
mmontin Jul 31, 2024
1ccb4b1
Proper script hash computation for all plutus versions
mmontin Jul 31, 2024
5114f60
helpers and qol changes
mmontin Jul 31, 2024
5c6adbe
MockChainSt has its own module now
mmontin Jul 31, 2024
db84394
Support for hashed datums in reference inputs
mmontin Jul 31, 2024
72e2820
withdrawal support
mmontin Jul 31, 2024
cc595b1
fixing balancing bug
mmontin Jul 31, 2024
63bb587
small post-rebase changes
mmontin Jul 31, 2024
97d1460
showbsspec finally gone
mmontin Jul 31, 2024
bdf3676
Recreating an index to pass to the new fee estimate function
mmontin Jul 31, 2024
2e6897d
merging logging into this
mmontin Aug 1, 2024
57fec8c
merging collaterals into this
mmontin Aug 1, 2024
cd8e5de
merging txout into this
mmontin Aug 1, 2024
5e465f4
merging bump cardano api into this
mmontin Aug 6, 2024
13271b9
post merge fix
mmontin Aug 6, 2024
395cc53
no tests built for dependencies, relying on cne directly
mmontin Aug 8, 2024
9d4be2d
CHANGELOG.md
mmontin Aug 8, 2024
1555195
merging update to new version of capi
mmontin Aug 8, 2024
4854587
relying on the fork for translation functions
mmontin Aug 8, 2024
f33029e
Merge branch 'mm/bump-capi' into mm/djed-audit
mmontin Aug 8, 2024
55c00e2
credential and staking credential of a wallet
mmontin Aug 29, 2024
785be72
moving time from either the lower or upper bound of current slot
mmontin Aug 29, 2024
cdc788b
depending on cne
mmontin Aug 29, 2024
c1d6f45
merging main into this
mmontin Aug 29, 2024
82f2b7f
merging main into this
mmontin Aug 29, 2024
ee333f6
post merge mini fix
mmontin Aug 29, 2024
70a0bee
merging collaterals into this
mmontin Aug 29, 2024
1554e51
merging txout into this
mmontin Aug 29, 2024
1654dbb
Merge branch 'main' into mm/collateral-when-no-script
mmontin Aug 30, 2024
bb45ed1
Merge branch 'mm/collateral-when-no-script' into mm/txout
mmontin Aug 30, 2024
3e1b3a1
Merge branch 'mm/txout' into mm/djed-audit
mmontin Aug 30, 2024
f08e504
reworking withdrawals for proper maps
mmontin Aug 30, 2024
b15fc69
hpack
mmontin Aug 30, 2024
bbe4122
merging
mmontin Sep 5, 2024
943d937
post review changes
mmontin Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@
- `toInitDistWithMinAda` and `unsafeToInitDistWithMinAda` to ensure the initial
distribution only provides outputs with the required minimal ada based on
default parameters.
- All kinds of scripts can now be used as reference scripts.
- `validateTxSkel_` which validates a skeleton and ignores the output.
- `txSkelMintsFromList'` which only allows one redeemer per minting policy.
- `validatorToTypedValidatorV2`
- `walletPKHashToWallet` that retrives a wallet from a pkh. It used to be
present but somehow disapeared.
- It is now possible to reference an output which has a hashed datum.
- `txSkelHashedData` the gives all the datum hashes in inputs and reference inputs.
- Partial support for withdrawals in txSkels. The rewarding scripts will be run
and assets will be transferred. However, these withdrawals are not properly
constrained yet.
- PrettyCooked option `pcOptPrintLog`, which is a boolean, to turn on or off the log
display in the pretty printer. The default value is `True`.

### Removed

- `positivePart` and `negativePart` in `ValueUtils.hs`. Replaced by `Api.split`.
Expand All @@ -26,6 +37,12 @@
constructors: `txSkelSomeRedeemer`, `txSkelEmptyRedeemer`,
`txSkelSomeRedeemerAndReferenceScript`,
`txSkelEmptyRedeemerAndReferenceScript`.
- `mkProposingScript` changed to `mkScript`
- `withDatumHashed` changed to `withUnresolvedDatumHash`
- `paysScriptDatumHashed` changed to `paysScriptUnresolvedDatumHash`
- `txSkelInputData` changed to `txSkelInputDataAsHashes`
- Pretty printing of hashed datum now includes the hash (and not only the
resolved datum).
- Dependency to cardano-api bumped to 8.46.
- Logging has been reworked:
* it is no longer limited to `StagedMockChain` runs
Expand All @@ -34,12 +51,16 @@
* it now displays the discarding of utxos during balancing.
* it now displays when the user specifies useless collateral utxos.
* it is not visible from outside of `cooked-validators`
- Dependency to cardano-api bumped to 8.46.

### Fixed

- A bug where the script hashes would not be computed properly for early plutus
version (V1 and V2).
- A bug where balancing would fail with excessive inputs and not enough min ada
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.
- Transactions that do not involve scripts are now properly generated without any
collateral.

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

Expand Down
5 changes: 5 additions & 0 deletions cooked-validators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ library
Cooked.MockChain.GenerateTx.Mint
Cooked.MockChain.GenerateTx.Output
Cooked.MockChain.GenerateTx.Proposal
Cooked.MockChain.GenerateTx.Withdrawals
Cooked.MockChain.GenerateTx.Witness
Cooked.MockChain.MinAda
Cooked.MockChain.MockChainSt
Cooked.MockChain.Staged
Cooked.MockChain.Testing
Cooked.MockChain.UtxoSearch
Expand Down Expand Up @@ -109,6 +111,7 @@ library
, cardano-api
, cardano-crypto
, cardano-data
, cardano-ledger-alonzo
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-shelley
Expand Down Expand Up @@ -169,6 +172,7 @@ test-suite spec
Cooked.Tweak.TamperDatumSpec
Cooked.Tweak.ValidityRangeSpec
Cooked.TweakSpec
Cooked.WithdrawalsSpec
Paths_cooked_validators
autogen-modules:
Paths_cooked_validators
Expand Down Expand Up @@ -201,6 +205,7 @@ test-suite spec
, cardano-api
, cardano-crypto
, cardano-data
, cardano-ledger-alonzo
, cardano-ledger-conway
, cardano-ledger-core
, cardano-ledger-shelley
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
- cardano-api
- cardano-crypto
- cardano-data
- cardano-ledger-alonzo
- cardano-ledger-core
- cardano-ledger-shelley
- cardano-ledger-conway
Expand Down
16 changes: 6 additions & 10 deletions src/Cooked/Conversion/ToScriptHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
module Cooked.Conversion.ToScriptHash where

import Cooked.Conversion.ToScript
import Plutus.Script.Utils.Scripts qualified as Script hiding (scriptHash)
import Plutus.Script.Utils.Scripts qualified as Script
import Plutus.Script.Utils.Typed qualified as Script
import Plutus.Script.Utils.V3.Scripts qualified as Script (scriptHash)
import PlutusLedgerApi.V3 qualified as Api

class ToScriptHash a where
Expand All @@ -13,17 +12,14 @@ class ToScriptHash a where
instance ToScriptHash Api.ScriptHash where
toScriptHash = id

instance ToScriptHash Script.Script where
toScriptHash = Script.scriptHash

instance ToScriptHash Api.SerialisedScript where
toScriptHash = toScriptHash . Script.Script
instance ToScriptHash Script.MintingPolicyHash where
toScriptHash (Script.MintingPolicyHash hash) = Script.ScriptHash hash

instance ToScriptHash Script.ValidatorHash where
toScriptHash (Script.ValidatorHash h) = Script.ScriptHash h
toScriptHash (Script.ValidatorHash hash) = Script.ScriptHash hash

instance ToScriptHash (Script.Versioned Script.Script) where
toScriptHash (Script.Versioned s _) = toScriptHash s
toScriptHash = Script.scriptHash

instance ToScriptHash (Script.Versioned Script.Validator) where
toScriptHash = toScriptHash . toScript
Expand All @@ -32,4 +28,4 @@ instance ToScriptHash (Script.TypedValidator a) where
toScriptHash = toScriptHash . toScript

instance ToScriptHash (Script.Versioned Script.MintingPolicy) where
toScriptHash = toScriptHash . toScript
toScriptHash = toScriptHash . Script.mintingPolicyHash
1 change: 1 addition & 0 deletions src/Cooked/MockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Cooked.MockChain.Balancing as X
import Cooked.MockChain.BlockChain as X hiding (MockChainLogEntry, logEvent)
import Cooked.MockChain.Direct as X hiding (MockChainReturn)
import Cooked.MockChain.MinAda as X
import Cooked.MockChain.MockChainSt as X (MockChainSt (..), mockChainSt0From)
import Cooked.MockChain.Staged as X hiding (StagedMockChain)
import Cooked.MockChain.Testing as X
import Cooked.MockChain.UtxoSearch as X
Expand Down
34 changes: 27 additions & 7 deletions src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ balanceTxSkel skelUnbal@TxSkel {..} = do
-- We retrieve the various kinds of scripts
spendingScripts <- txSkelInputValidators skelUnbal
-- The transaction will only require collaterals when involving scripts
let noScriptInvolved = Map.null txSkelMints && null (mapMaybe txSkelProposalWitness txSkelProposals) && Map.null spendingScripts
let noScriptInvolved =
Map.null txSkelMints
&& null (mapMaybe txSkelProposalWitness txSkelProposals)
&& Map.null spendingScripts
&& null (txSkelWithdrawalsScripts skelUnbal)
case (noScriptInvolved, txOptCollateralUtxos txSkelOpts) of
(True, CollateralUtxosFromSet utxos _) -> logEvent (MCLogUnusedCollaterals $ Right utxos) >> return Nothing
(True, CollateralUtxosFromWallet cWallet) -> logEvent (MCLogUnusedCollaterals $ Left cWallet) >> return Nothing
Expand Down Expand Up @@ -298,7 +302,7 @@ estimateTxSkelFee :: (MonadBlockChainBalancing m) => TxSkel -> Fee -> Maybe (Col
estimateTxSkelFee skel fee mCollaterals = do
-- We retrieve the necessary data to generate the transaction body
params <- getParams
managedData <- txSkelInputData skel
managedData <- txSkelHashedData skel
mmontin marked this conversation as resolved.
Show resolved Hide resolved
let collateralIns = case mCollaterals of
Nothing -> []
Just (s, _) -> Set.toList s
Expand All @@ -310,7 +314,7 @@ estimateTxSkelFee skel fee mCollaterals = do
Right txBodyContent -> return txBodyContent
-- We create the actual body and send if for validation
txBody <- case Cardano.createAndValidateTransactionBody Cardano.ShelleyBasedEraConway txBodyContent of
Left err -> throwError $ MCEGenerationError (TxBodyError "Error creating body when estimating fees" err)
Left err -> throwError $ MCEGenerationError $ TxBodyError "Error creating body when estimating fees" err
Right txBody -> return txBody
-- We retrieve the estimate number of required witness in the transaction
let nkeys = Cardano.estimateTransactionKeyWitnessCount txBodyContent
Expand All @@ -331,24 +335,40 @@ estimateTxSkelFee skel fee mCollaterals = do

-- | This creates a balanced skeleton from a given skeleton and fee. In other
-- words, this ensures that the following equation holds: input value + minted
-- value = output value + burned value + fee + deposits
-- value + withdrawn value = output value + burned value + fee + deposits
computeBalancedTxSkel :: (MonadBlockChainBalancing m) => Wallet -> BalancingOutputs -> TxSkel -> Fee -> m TxSkel
computeBalancedTxSkel balancingWallet balancingUtxos txSkel@TxSkel {..} (Script.lovelace -> feeValue) = do
params <- getParams
-- We compute the necessary values from the skeleton that are part of the
-- equation, except for the `feeValue` which we already have.
let (burnedValue, mintedValue) = Api.split $ txSkelMintsValue txSkelMints
outValue = txSkelValueInOutputs txSkel
withdrawnValue = txSkelWithdrawnValue txSkel
inValue <- txSkelInputValue txSkel
depositedValue <- toValue <$> txSkelProposalsDeposit txSkel
-- We compute the values missing in the left and right side of the equation
let (missingRight, missingLeft) = Api.split $ outValue <> burnedValue <> feeValue <> depositedValue <> PlutusTx.negate (inValue <> mintedValue)
let (missingRight, missingLeft) = Api.split $ outValue <> burnedValue <> feeValue <> depositedValue <> PlutusTx.negate (inValue <> mintedValue <> withdrawnValue)
-- We compute the minimal ada requirement of the missing payment
rightMinAda <- case getTxSkelOutMinAda params $ paysPK balancingWallet missingRight of
Left err -> throwError $ MCEGenerationError err
Right a -> return a
-- We compute the current ada of the missing payment. If the missing payment
-- is not empty and the minimal ada is not present, some value is missing.
let Script.Lovelace rightAda = missingRight ^. Script.adaL
missingAda = rightMinAda - rightAda
missingAdaValue = if missingRight /= mempty && missingAda > 0 then Script.lovelace missingAda else mempty
-- The actual missing value on the left might needs to account for any missing
-- min ada on the missing payment of the transaction skeleton. This also has
-- to be repercuted on the missing value on the right.
let missingLeft' = missingLeft <> missingAdaValue
missingRight' = missingRight <> missingAdaValue
-- This gives us what we need to run our `reachValue` algorithm and append to
-- the resulting values whatever payment was missing in the initial skeleton
let candidatesRaw = second (<> missingRight) <$> reachValue balancingUtxos missingLeft (toInteger $ length balancingUtxos)
let candidatesRaw = second (<> missingRight') <$> reachValue balancingUtxos missingLeft' (toInteger $ length balancingUtxos)
-- We prepare a possible balancing error with the difference between the
-- requested amount and the maximum amount provided by the balancing wallet
let totalValue = mconcat $ Api.txOutValue . snd <$> balancingUtxos
difference = snd $ Api.split $ missingLeft <> PlutusTx.negate totalValue
difference = snd $ Api.split $ missingLeft' <> PlutusTx.negate totalValue
balancingError = MCEUnbalanceable balancingWallet difference txSkel
-- Which one of our candidates should be picked depends on three factors
-- - Whether there exists a perfect candidate set with empty surplus value
Expand Down
Loading