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 1 commit
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
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 ran
mmontin marked this conversation as resolved.
Show resolved Hide resolved
and assets will be transferred. However, these withdrawals are not properly
constrainted yet.
mmontin marked this conversation as resolved.
Show resolved Hide resolved

### Removed

Expand Down
2 changes: 2 additions & 0 deletions cooked-validators.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ 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
Expand Down Expand Up @@ -172,6 +173,7 @@ test-suite spec
Cooked.Tweak.TamperDatumSpec
Cooked.Tweak.ValidityRangeSpec
Cooked.TweakSpec
Cooked.WithdrawalsSpec
Paths_cooked_validators
autogen-modules:
Paths_cooked_validators
Expand Down
5 changes: 3 additions & 2 deletions src/Cooked/MockChain/Balancing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,18 @@ estimateTxSkelFee skel fee collateralIns returnCollateralWallet = 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
-- 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)
-- 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)
Expand Down
9 changes: 8 additions & 1 deletion src/Cooked/MockChain/GenerateTx/Body.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Cooked.MockChain.GenerateTx.Input qualified as Input
import Cooked.MockChain.GenerateTx.Mint qualified as Mint
import Cooked.MockChain.GenerateTx.Output qualified as Output
import Cooked.MockChain.GenerateTx.Proposal qualified as Proposal
import Cooked.MockChain.GenerateTx.Withdrawals qualified as Withdrawals
import Cooked.Skeleton
import Cooked.Wallet
import Data.Either.Combinators
Expand Down Expand Up @@ -59,6 +60,11 @@ instance Transform TxContext Input.InputContext where
instance Transform TxContext Collateral.CollateralContext where
transform TxContext {..} = Collateral.CollateralContext {..}

instance Transform TxContext Withdrawals.WithdrawalsContext where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit skeptical about this Transform typeclass. This is a whole topic on its own, not related to this here specifically, so I won't comment on this here anymore. We may discuss it between maintainers or open the discussion in an issue.

transform TxContext {..} =
let networkId = Emulator.pNetworkId params
in Withdrawals.WithdrawalsContext {..}

-- | Generates a body content from a skeleton
txSkelToBodyContent :: TxSkel -> BodyGen (Cardano.TxBodyContent Cardano.BuildTx Cardano.ConwayEra)
txSkelToBodyContent skel@TxSkel {..} | txSkelReferenceInputs <- txSkelReferenceTxOutRefs skel = do
Expand Down Expand Up @@ -91,9 +97,9 @@ txSkelToBodyContent skel@TxSkel {..} | txSkelReferenceInputs <- txSkelReferenceT
txProposalProcedures <-
Just . Cardano.Featured Cardano.ConwayEraOnwardsConway
<$> liftTxGen (Proposal.toProposalProcedures txSkelProposals (txOptAnchorResolution txSkelOpts))
txWithdrawals <- liftTxGen (Withdrawals.toWithdrawals txSkelWithdrawals)
let txMetadata = Cardano.TxMetadataNone -- That's what plutus-apps does as well
txAuxScripts = Cardano.TxAuxScriptsNone -- That's what plutus-apps does as well
txWithdrawals = Cardano.TxWithdrawalsNone -- That's what plutus-apps does as well
txUpdateProposal = Cardano.TxUpdateProposalNone -- That's what plutus-apps does as well
txCertificates = Cardano.TxCertificatesNone -- That's what plutus-apps does as well
mmontin marked this conversation as resolved.
Show resolved Hide resolved
txScriptValidity = Cardano.TxScriptValidityNone -- That's what plutus-apps does as well
Expand All @@ -104,6 +110,7 @@ txSkelToBodyContent skel@TxSkel {..} | txSkelReferenceInputs <- txSkelReferenceT
-- sign it with the required signers.
txSkelToCardanoTx :: TxSkel -> BodyGen (Cardano.Tx Cardano.ConwayEra)
txSkelToCardanoTx txSkel = do
-- We begin by creating the body content of the transaction
txBodyContent <- txSkelToBodyContent txSkel

-- We create the associated Shelley TxBody
Expand Down
58 changes: 58 additions & 0 deletions src/Cooked/MockChain/GenerateTx/Withdrawals.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Cooked.MockChain.GenerateTx.Withdrawals
( WithdrawalsContext (..),
toWithdrawals,
)
where

import Cardano.Api qualified as Cardano
import Cardano.Api.Ledger qualified as Cardano
import Cardano.Api.Shelley qualified as Cardano
import Control.Monad
import Control.Monad.Reader
import Cooked.Conversion
import Cooked.MockChain.GenerateTx.Common
import Cooked.MockChain.GenerateTx.Witness
import Cooked.Skeleton
import Data.Map (Map)
import Data.Map qualified as Map
import Ledger.Tx.CardanoAPI qualified as Ledger
import Plutus.Script.Utils.Ada qualified as Script
import PlutusLedgerApi.V3 qualified as Api

data WithdrawalsContext where
WithdrawalsContext ::
{ managedTxOuts :: Map Api.TxOutRef Api.TxOut,
networkId :: Cardano.NetworkId
} ->
WithdrawalsContext

instance Transform WithdrawalsContext (Map Api.TxOutRef Api.TxOut) where
transform = managedTxOuts

type WithdrawalsGen a = TxGen WithdrawalsContext a

toWithdrawals :: TxSkelWithdrawals -> WithdrawalsGen (Cardano.TxWithdrawals Cardano.BuildTx Cardano.ConwayEra)
toWithdrawals (Map.toList -> []) = return Cardano.TxWithdrawalsNone
toWithdrawals (Map.toList -> withdrawals) =
fmap
(Cardano.TxWithdrawals Cardano.ShelleyBasedEraConway)
$ forM withdrawals
$ \(staker, Script.Lovelace n) ->
do
(witness, sCred) <-
case staker of
Right pkh -> do
sCred <-
throwOnToCardanoError "toWithdrawals: unable to translate pkh stake credential" $
Cardano.StakeCredentialByKey <$> Ledger.toCardanoStakeKeyHash pkh
return (Cardano.KeyWitness Cardano.KeyWitnessForStakeAddr, sCred)
Left (script, red) -> do
witness <-
Cardano.ScriptWitness Cardano.ScriptWitnessForStakeAddr
<$> liftTxGen (toScriptWitness script red Cardano.NoScriptDatumForStake)
sCred <-
throwOnToCardanoError "toWithdrawals: unable to translate script stake credential" $
Cardano.StakeCredentialByScript <$> Ledger.toCardanoScriptHash (toScriptHash script)
return (witness, sCred)
networkId <- asks networkId
return (Cardano.makeStakeAddress networkId sCred, Cardano.Coin n, Cardano.BuildTxWith witness)
mmontin marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 16 additions & 2 deletions src/Cooked/Pretty/Cooked.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ instance PrettyCooked MockChainLog where
go acc [] = reverse acc

prettyTxSkel :: PrettyCookedOpts -> SkelContext -> TxSkel -> DocCooked
prettyTxSkel opts skelContext (TxSkel lbl txopts mints signers validityRange ins insReference outs proposals) =
prettyTxSkel opts skelContext (TxSkel lbl txopts mints signers validityRange ins insReference outs proposals withdrawals) =
prettyItemize
"transaction skeleton:"
"-"
Expand All @@ -183,10 +183,24 @@ prettyTxSkel opts skelContext (TxSkel lbl txopts mints signers validityRange ins
prettyItemizeNonEmpty "Inputs:" "-" (prettyTxSkelIn opts skelContext <$> Map.toList ins),
prettyItemizeNonEmpty "Reference inputs:" "-" (mapMaybe (prettyTxSkelInReference opts skelContext) $ Set.toList insReference),
prettyItemizeNonEmpty "Outputs:" "-" (prettyTxSkelOut opts <$> outs),
prettyItemizeNonEmpty "Proposals:" "-" (prettyTxSkelProposal opts <$> proposals)
prettyItemizeNonEmpty "Proposals:" "-" (prettyTxSkelProposal opts <$> proposals),
prettyWithdrawals opts withdrawals
]
)

prettyWithdrawals :: PrettyCookedOpts -> TxSkelWithdrawals -> Maybe DocCooked
prettyWithdrawals pcOpts withdrawals =
prettyItemizeNonEmpty "Withdrawals:" "-" $ prettyWithdrawal <$> Map.toList withdrawals
where
prettyWithdrawal :: (Either (Script.Versioned Script.Script, TxSkelRedeemer) Api.PubKeyHash, Script.Ada) -> DocCooked
prettyWithdrawal (cred, ada) =
prettyItemizeNoTitle "-" $
( case cred of
Left (script, red) -> prettyCookedOpt pcOpts script : prettyTxSkelRedeemer pcOpts red
Right pkh -> [prettyCookedOpt pcOpts pkh]
)
++ [prettyCookedOpt pcOpts (toValue ada)]

prettyTxParameterChange :: PrettyCookedOpts -> TxParameterChange -> DocCooked
prettyTxParameterChange opts (FeePerByte n) = "Fee per byte:" <+> prettyCookedOpt opts n
prettyTxParameterChange opts (FeeFixed n) = "Fee fixed:" <+> prettyCookedOpt opts n
Expand Down
2 changes: 1 addition & 1 deletion src/Cooked/ShowBS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ instance ShowBS Api.TxInfo where
<> showBS txInfoMint
<> "certificates:"
<> showBS txInfoTxCerts
<> "wdrl:" -- TODO: what is wdrl? Explain better here
<> "wdrl:"
<> showBS txInfoWdrl
<> "valid range:"
<> showBS txInfoValidRange
Expand Down
27 changes: 23 additions & 4 deletions src/Cooked/Skeleton.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ module Cooked.Skeleton
txSkelProposalActionL,
txSkelProposalWitnessL,
txSkelProposalAnchorL,
TxSkelWithdrawals,
txSkelWithdrawnValue,
TxSkel (..),
txSkelLabelL,
txSkelOptsL,
Expand All @@ -76,6 +78,7 @@ module Cooked.Skeleton
txSkelInsL,
txSkelInsReferenceL,
txSkelOutsL,
txSkelWithdrawalsL,
txSkelTemplate,
txSkelDataInOutputs,
txSkelValidatorsInOutputs,
Expand Down Expand Up @@ -119,6 +122,7 @@ import Data.Set qualified as Set
import Ledger.Slot qualified as Ledger
import Optics.Core
import Optics.TH
import Plutus.Script.Utils.Ada qualified as Script
import Plutus.Script.Utils.Scripts qualified as Script
import Plutus.Script.Utils.Typed qualified as Script hiding (validatorHash)
import Plutus.Script.Utils.Value qualified as Script hiding (adaSymbol, adaToken)
Expand Down Expand Up @@ -582,6 +586,16 @@ withWitness prop (s, red) = prop {txSkelProposalWitness = Just (toScript s, red)
withAnchor :: TxSkelProposal -> String -> TxSkelProposal
withAnchor prop url = prop {txSkelProposalAnchor = Just url}

-- * Description of the Withdrawals

type TxSkelWithdrawals =
Map
(Either (Script.Versioned Script.Script, TxSkelRedeemer) Api.PubKeyHash)
Script.Ada

txSkelWithdrawnValue :: TxSkel -> Api.Value
txSkelWithdrawnValue = mconcat . (toValue . snd <$>) . Map.toList . txSkelWithdrawals

-- * Description of the Minting

-- | A description of what a transaction mints. For every policy, there can only
Expand Down Expand Up @@ -1014,8 +1028,11 @@ data TxSkel where
-- | The outputs of the transaction. These will occur in exactly this
-- order on the transaction.
txSkelOuts :: [TxSkelOut],
-- | Possible proposals issued in this transaction to be voted on and possible enacted later on.
txSkelProposals :: [TxSkelProposal]
-- | Possible proposals issued in this transaction to be voted on and
-- possible enacted later on.
txSkelProposals :: [TxSkelProposal],
-- | Withdrawals performed by the transaction
txSkelWithdrawals :: TxSkelWithdrawals
} ->
TxSkel
deriving (Show, Eq)
Expand All @@ -1029,7 +1046,8 @@ makeLensesFor
("txSkelIns", "txSkelInsL"),
("txSkelInsReference", "txSkelInsReferenceL"),
("txSkelOuts", "txSkelOutsL"),
("txSkelProposals", "txSkelProposalsL")
("txSkelProposals", "txSkelProposalsL"),
("txSkelWithdrawals", "txSkelWithdrawalsL")
]
''TxSkel

Expand All @@ -1045,7 +1063,8 @@ txSkelTemplate =
txSkelIns = Map.empty,
txSkelInsReference = Set.empty,
txSkelOuts = [],
txSkelProposals = []
txSkelProposals = [],
txSkelWithdrawals = Map.empty
}

-- | The missing information on a 'TxSkel' that can only be resolved by querying
Expand Down
59 changes: 59 additions & 0 deletions tests/Cooked/WithdrawalsSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module Cooked.WithdrawalsSpec where

import Control.Monad
import Cooked
import Data.Default
import Data.Map qualified as Map
import Plutus.Script.Utils.Ada qualified as Script
import Plutus.Script.Utils.Scripts qualified as Script
import PlutusLedgerApi.V3 qualified as Api
import PlutusTx qualified
import PlutusTx.AssocMap qualified as PMap
import PlutusTx.Prelude qualified as PlutusTx
import Test.Tasty
import Test.Tasty.HUnit

checkWithdrawalScript :: PlutusTx.BuiltinData -> PlutusTx.BuiltinData -> ()
checkWithdrawalScript red ctx =
let scriptContext = PlutusTx.unsafeFromBuiltinData @Api.ScriptContext ctx
withdrawals = Api.txInfoWdrl PlutusTx.$ Api.scriptContextTxInfo scriptContext
quantity = PlutusTx.unsafeFromBuiltinData @Integer red
purpose = Api.scriptContextPurpose scriptContext
in case purpose of
Api.Rewarding cred -> case PMap.toList withdrawals of
[(cred', Api.Lovelace n)] ->
if cred PlutusTx.== cred'
then
if n PlutusTx.== quantity
then ()
else PlutusTx.traceError "Wrong quantity."
else PlutusTx.traceError "Wrong credential."
_ -> PlutusTx.traceError "Wrong withdrawal."
_ -> PlutusTx.traceError "Wrong script purpose."

checkWithdrawalVersionedScript :: Script.Versioned Script.Script
checkWithdrawalVersionedScript = mkScript $$(PlutusTx.compile [||checkWithdrawalScript||])

testWithdrawingScript :: (MonadBlockChain m) => Integer -> Integer -> m ()
testWithdrawingScript n1 n2 =
void $
validateTxSkel $
txSkelTemplate
{ txSkelSigners = [wallet 1],
txSkelWithdrawals =
Map.singleton
(Left (checkWithdrawalVersionedScript, txSkelSomeRedeemer (n1 * 1_000 :: Integer)))
(Script.Lovelace $ n2 * 1_000)
}

tests :: TestTree
tests =
testGroup
"Withdrawing scripts"
[ testCase "We can use a withdrawing script" $
testSucceeds def $
testWithdrawingScript 2 2,
testCase "But the script might fail" $
testFailsFrom def (isCekEvaluationFailure def) def $
testWithdrawingScript 2 1
]
4 changes: 3 additions & 1 deletion tests/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Cooked.ReferenceInputsSpec qualified as ReferenceInputsSpec
import Cooked.ReferenceScriptsSpec qualified as ReferenceScriptsSpec
import Cooked.ShowBSSpec qualified as ShowBSSpec
import Cooked.TweakSpec qualified as TweakSpec
import Cooked.WithdrawalsSpec qualified as WithdrawalsSpec
import Test.Tasty

main :: IO ()
Expand All @@ -30,5 +31,6 @@ main =
MockChainSpec.tests,
ShowBSSpec.tests,
InitDistribSpec.tests,
ProposingSpec.tests
ProposingSpec.tests,
WithdrawalsSpec.tests
]