Skip to content

Commit

Permalink
Reduce script size
Browse files Browse the repository at this point in the history
Inline mustNotChangeParameters and add two bang patterns to reduce
script size enough to fit in the 16384 bytes limit.

Re-generate plutus golden files.

Format the code.
  • Loading branch information
v0d1ch committed Mar 19, 2024
1 parent 3e5c58f commit 603d1c7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion hydra-cluster/config/devnet/genesis-shelley.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"keyDeposit": 0,
"maxBlockBodySize": 65536,
"maxBlockHeaderSize": 1100,
"maxTxSize": 16405,
"maxTxSize": 16384,
"minFeeA": 44,
"minFeeB": 155381,
"minPoolCost": 0,
Expand Down
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/Chain/Direct/StateSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ import PlutusLedgerApi.V2 qualified as Plutus
import Test.Aeson.GenericSpecs (roundtripAndGoldenSpecs)
import Test.QuickCheck (
Property,
forAllShrink,
Testable (property),
checkCoverage,
classify,
Expand All @@ -87,6 +86,7 @@ import Test.QuickCheck (
forAll,
forAllBlind,
forAllShow,
forAllShrink,
getPositive,
label,
sized,
Expand Down
4 changes: 2 additions & 2 deletions hydra-plutus/scripts/mHead.plutus

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hydra-plutus/scripts/vCommit.plutus
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "PlutusScriptV2",
"description": "hydra-vCommit-0.14.0-878-g46ff6715e",
"description": "hydra-vCommit-0.15.0-463-g09800bd268",
"cborHex": "5907b45907b10100003232323322323233223232323232222533533223530060052225323233500713300249010343303100323301922533500110172213500222325335333573400203a038260280042600e00866e3c008028004d4c0040188888888888880204cc0092401034330320033018225335001101622135002223253353335734002038036260280042600e00866e3c008024d54cd4d4c0040188888888888880284c0592401035054380022100222220032350012200222533500110151335738004028646464a66a64666ae68004048044cdc3a400000422440022a66a64666ae68004048044cdc3a400400422440042602401e6aae78008d55ce8009baa0023232323253353335734002024022264646644246600200600464646464a66a666ae6800406005c4c8c8c8c8c8c8c8c8c8c8cc8ccccccc8ccc8ccc88888888888848cccccccccccc00403403002c02802402001c01801401000c008cc07c8c8c8c8c94cd4ccd5cd00081501489991091980080180118139aba10023006357426ae880084c0ac0a0cdc3a40000046aae78008d55ce8009baa0013574201e6ae84034cc07c004d5d0806119191919299a999ab9a001029028132323233332222123333001005004003002323232325335333573400206005e266442466002006004603c6ae84008cc014074d5d09aba20021303102e3370e90000011aab9e00235573a0026ea8d5d080298049aba1003323232325335333573400206005e22444006264a66a666ae680040c40c04c84888c004010dd71aba100313253353335734002064062264244460040086ae840104c0cc0c0cdc3a400800866e1d20020033370e90000011aab9e00235573a0026ea8d5d080119800bae357426ae8800888c8c8c8c94cd4ccd5cd0008188180891000899299a999ab9a00103203113212230020033007357420062606606066e1d20000033370e90010011aab9e00235573a0026ea8004d5d10009aba20021302a0273370e90000011aab9e00235573a0026ea8004c004d5d080518009aba10093001300175a6ae8401cd5d08031aba10053301d75c6ae84010d5d08019980090009aba1002232230023756002604c446666aae7c00480888c8cd4088cc8848cc00400c008dd71aab9d001300535573c00260086ae8800cd5d08010139aba1357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10010980c80b19b8748000008d55cf0011aab9d00137546ae84010c8c8c8c94cd4ccd5cd00080c00b8990911118018029bae35742004264a66a666ae680040640604c848888c004014c058d5d0801899299a999ab9a00101a019132122223002005300735742008264a66a666ae6800406c0684c848888c010014c8c8c8c94cd4ccd5cd00080f80f09909111111180280418061aba10021325335333573400204003e26424444444600e010601a6ae8400c4c94cd4ccd5cd00081081009991091111111980300480418071aba1004375c6ae84d5d1002099299a999ab9a001022021133221222222233002009008375c6ae84014dd71aba13574400a264a66a666ae6800408c0884cc8848888888cc004024020dd71aba1006375a6ae84d5d1003099299a999ab9a0010240231122222220041325335333573400204a0482244444440062604c04666e1d200c0083370e900500399b8748020018cdc3a400c00a66e1d20040043370e900100199b8748000008d55cf0011aab9d00137546ae840144c070064cdc3a400c00a66e1d20040043370e900100199b8748000008d55cf0011aab9d00137546ae84d5d1002119191919299a999ab9a0010180171321223002003300635742004264a66a666ae680040640604c8ccc888488ccc00401401000cdd69aba1004375a6ae84004dd69aba1357440026ae8800c4c06805ccdc3a400400666e1d200000235573c0046aae74004dd5000919191919299a999ab9a0010170161321223002003375c6ae840084c94cd4ccd5cd00080c00b89909118008019bae357420062603202c66e1d20020033370e90000011aab9e00235573a0026ea80044c04c040cdc3a40000046aae78008d55ce8009baa00110101300f49010350543500322323232325335333573400202001e264666444246660020080060046eb8d5d080198039aba1001375c6ae84d5d10009aba20021301100e3370e90000011aab9e00235573a0026ea8004c0108c8c8c94cd4c8ccd5cd00080780719b87480000084cc8848cc00400c008c02cd5d08009bae357426ae880044c03c030d55cf0011aab9d0013754002601844a66a00220144426a0044464a66a666ae6800404003c4c8c8ccd5cd00080900899b87003001337029000240042600e00866e3c008020c02c894cd40044024884d400888c94cd4ccd5cd000807807099199ab9a00101000f3370e00490010980380219b8f0020074890b4879647261486561645631002322300237580026014446666aae7c00480188cd4014c010d5d080118019aba200200b122002122122330010040032323232325335333573400201000e26644246600200600464646464a66a666ae6800403002c4dd71aba10021300d00a3370e90000011aab9e00235573a0026ea8d5d08011bad357426ae880084c024018cdc3a40000046aae78008d55ce8009baa00149010350543100122002122001232633573800200424002464600200244660066004004003"
}
4 changes: 2 additions & 2 deletions hydra-plutus/scripts/vHead.plutus

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hydra-plutus/scripts/vInitial.plutus
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "PlutusScriptV2",
"description": "hydra-vInitial-0.14.0-878-g46ff6715e",
"description": "hydra-vInitial-0.15.0-463-g09800bd268",
"cborHex": "59100b5910080100003323322323232323322323232323322323232323232323232323232323232323232323232323300b22225335333004375c006646464a66a64666ae68004098094cdc3a400000422440042a66a64666ae68004098094cdc3a40040042642446002006660420446ae840044c09808cd55cf0011aab9d001375400464646464a66a666ae680040980944c8c8cc8848cc00400c008c8c8c8c94cd4ccd5cd000816015899191919191919191919199199999991999199911111111111091999999999998008068060058050048040038030028020018011981a919191919299a999ab9a00103e03d133221233001003002303b35742004600c6ae84d5d10010981f81e19b8748000008d55cf0011aab9d00137540026ae8403cd5d08069981a8009aba100c2323232325335333573400207a07826464646666444424666600200a00800600464646464a66a666ae6800411010c4cc8848cc00400c008c078d5d08011980280e9aba1357440042608a08466e1d200000235573c0046aae74004dd51aba100530093574200664646464a66a666ae6800411010c4488800c4c94cd4ccd5cd000822822099091118008021bae35742006264a66a666ae680041181144c84888c008010d5d08020982382219b8748010010cdc3a400400666e1d200000235573c0046aae74004dd51aba10023300175c6ae84d5d10011119191919299a999ab9a00104504410331325335333573400208c08a26080600e6ae8400c4c11c110cdc3a400000666e1d200200235573c0046aae74004dd50009aba2001357440042607c07666e1d200000235573c0046aae74004dd500098009aba100a30013574201260026002eb4d5d08039aba10063574200a66066eb8d5d08021aba100333001200135742004464460046eac004c0f888cccd55cf800902011919a8201981b1bae35573a002600a6aae78004c010d5d10019aba100203b357426ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba20021302d02a3370e90000011aab9e00235573a0026ea8d5d080219191919299a999ab9a00102c02b132122223003005375c6ae840084c94cd4ccd5cd00081681609909111180080298151aba10031325335333573400205c05a26424444600400a600e6ae840104c94cd4ccd5cd00081781709909111180200299191919299a999ab9a001033032132122222223005008300c35742004264a66a666ae680040d00cc4c848888888c01c020c034d5d0801899299a999ab9a001035034133221222222233006009008300e357420086eb8d5d09aba20041325335333573400206c06a2664424444444660040120106eb8d5d08029bae357426ae880144c94cd4ccd5cd00081b81b0999109111111198008048041bae3574200c6eb4d5d09aba20061325335333573400207006e224444444008264a66a666ae680040e40e044888888800c4c0e80dccdc3a401801066e1d200a0073370e900400319b8748018014cdc3a400800866e1d20020033370e90000011aab9e00235573a0026ea8d5d08028981801699b8748018014cdc3a400800866e1d20020033370e90000011aab9e00235573a0026ea8d5d09aba2004232323232533533357340020580562642446004006600c6ae840084c94cd4ccd5cd0008168160991999110911998008028020019bad357420086eb4d5d08009bad357426ae88004d5d10018981701599b874800800ccdc3a40000046aae78008d55ce8009baa0012323232325335333573400205605426424460040066eb8d5d0801099299a999ab9a00102c02b1321223001003375c6ae8400c4c0b40a8cdc3a400400666e1d200000235573c0046aae74004dd50008981381219b8748000008d55cf0011aab9d0013754002204826046920103505435002222533500213300a4910349303100323302a2253350011024221350022232533533357340020540522601a0042600e00866e3c008028004c02cd40048800884c8c8c8c8c8c94cd4cc045241034930330033232230020013031225335001102c22135002232230020013036225335001130070042213500222325335333573466e200040080d40d84c01c01040d4c8cc0f4894cd4004520002213500222325335333573400207a078260100042600e00866e3c008038034c0f0894cd4004520002213500222325335333573400207807620042600e00866e3c008020c8c038c03ccc0100300054018cc0054010cc8c8c8c88c008004c0cc894cd4004400c884ccc01888008c01c008c010004c004880048cc00400c0c4cc05804003d4014400840a0c04c050c8c8c94cd4cc0492401034930340030203302550063550032200210011029325335330124910349313300330223550032200100b100110293253353301249010349303200323233033225335001102d221533530050021030130040010023023533533032225335001101b22135002225335330290020121302b0011300600350062153350011302d49010349303600221350022253350031533532333573400206206066e1c00520021002130314910349303600221303349103493036001302c491034930350033031225335001103222133503300230040013500222222222222200410011029330114901034931340030133012001500515335330010095004130284910349313200221350022253350031533350021302c49103493039002153353233333302200222325335333573400206005e2a66a6666aae7c008807c8c8c8c0b4cc0b8008004d5d10021aba100303221350012253355335302700221302e001102121533533335573e0044046464646062660640040026ae88010d5d080181b109a80091299a980580110a99a9999aab9f0022027232323035002357440086ae8400c0e8854cd4c0b400484c0d048ccc004020010008409c409840944088408440784078cdc3a40000044038403840384038646446666660480024403e403c004403c403c6068446666aae7c004801094cd4cccccc094d5d08011119299a999ab9a0010330321533533335573e0044044464646060660620040026ae88010d5d080181a909a80091299a9999998158011119299a999ab9a0010390381533533335573e004405046464606c6606e0040026ae88010d5d080181d909a80091299a9999998188011119299a999ab9a00103f03e1533533335573e004405c4646460780046ae88010d5d080182090a99a981a00090981d80088170816881699b874800000880ac80ac80ac80ac854cd4cccd55cf801101611919181d0011aba20043574200607e42a66a6666660660024405c405a405a46072002405a42607266086006002205820562054204e204e66e1d2000002202520252025202521533533335573e004404c4646460680046ae88010d5d080181c90a99a981600090981989198008020010813081288120810881099b8748000008807c807c807c807c854cd4c010d5d100190981619a81b801000880f89900f98019aba2002031302603221350012223302b0020011302d491034931310021302d49010349313000221302e4910349313200223302f2253350011030221325335350032222350042233500220222325335333573400206a0682605e6606000c00e204666e3c00403884cd40cc0040084004c010004d40048888888888880284c94cd4d401488d4008888888888888cccd4034808c808c808c8cc0f0894cd40044094884d4008894cd4cc0c800801c4c0d40104c01800c03484c00800440b0cc04803002c4cc0ac894cd400440b0884cd40b54cd4c8d54018888888888888c034030c0bc894cd40044060884d4008894cd4cc0940080204c0a00104c01800c840044c0a524103493135003004001002135002220023024225335001101e2213500222325335333573400204804626464666ae68004098094cdc380180099b81480012002130070043371e0049110b4879647261486561645631003023225335001102422133300b00c00230040013022225335001102322133502435002220013004001235001220012350012222003225335001101a133573800403246a002444444444444010603a44a66a0022030446426a006446006002604244a66a0022600c0064426a0044464a66a666ae680040840804c01c0104080cdc3a40000024644460066010666a012022004002603a44a66a002203c44264660026a00644660300046604844a66a002204a4426600e6a004446603c004666a00246601e90000009119808001000919807800a40006008002002600a004a03e4466e00008004888c00cc008005402cc060894cd40044064884cd4068d400888cc048008ccd40048c02400488ccd40240440080048c028004c0100044c8888c8c8cc074894cd4004400c884cd407c008c010004c008010c014cc070894cd40044074884c94cd4d400c8c88c00802cc088894cd40044074884d4008894cd4cc04000801c40804c01800c4cd407c00c0044004c010004008c06c894cd40044070884cd4074d400888c8c8cc05c010c004024c088894cd4004400c884d4008894cd4cc04000802444888cc00802c0104c01800cc034004c010004c05c894cd40044060884cd4064d400888cc0440084888c00c010c010004c058894cd4004405c884cd4060d400888cc0400084888c00c010c010004c054894cd40044058884cd405cd400888cc03c008c01c004c01000484888c0040108cccccc0080048940209401c9401c9401c8c020004888888ccccccd5d2003119198039aab9d00135573c0026ea801c8c014dd5803918021bac00723003375a00e460046eb801c04cc04488d4004894cd400854cd40044038884c045240103493037002215335003130114910349303800221350042235004223253353300f37666a008444466e95200032323357406a00c4466e95200032335740600200666ae80cc0148cd40048cdd2a400066ae80c00c004018888cdd2a400466ae80dd400199aba0375000466ae80dd4000804001002119a800919ba548000cd5d01ba900100523374a900119aba0375200200a66ae80cc8c004c0048dd400091911ba630020013025225335001137640464426a0044466ae80cdd81ba900230070013006003005335740a666a008266e952000001213374a900219aba0001002213374a900119aba0375200200466ae80cc009d48018009bb101f225335001213374a900019aba030030013762042266e95200237620400042002202c64a66a6601c00a0062002202c60186602200e00a446a004446a0064464a66a666ae6800404003c4c8ccd5cd00080880819b87004002100f3371e0080044464666ae6800402c028cdc7801000909118010018891000910919800801801119118011bac001300c2233335573e002401c466a01a60086ae84008c00cd5d1001004919191919299a999ab9a00100800713300c323232325335333573400201801626eb8d5d08010980680519b8748000008d55cf0011aab9d00137546ae84008dd69aba1357440042601200c66e1d200000235573c0046aae74004dd5000a4903505431001220021220012326335738002004240029311091980080180109100109109119800802001919180080091198019801001000a4411c56b0f0b597150e619c76bed60683f3b1e42d7bc0685ed951b882bfc50001"
}
48 changes: 18 additions & 30 deletions hydra-plutus/src/Hydra/Contract/Head.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ checkCollectCom ::
Bool
checkCollectCom ctx@ScriptContext{scriptContextTxInfo = txInfo} (contestationPeriod, parties, headId) =
mustCollectUtxoHash
&& mustNotChangeParameters
&& mustNotChangeParameters (parties', parties) (contestationPeriod', contestationPeriod) (headId', headId)
&& mustCollectAllValue
-- XXX: Is this really needed? If yes, why not check on the output?
&& traceIfFalse $(errorCode STNotSpent) (hasST headId val)
Expand All @@ -157,12 +157,6 @@ checkCollectCom ctx@ScriptContext{scriptContextTxInfo = txInfo} (contestationPer
traceIfFalse $(errorCode IncorrectUtxoHash) $
utxoHash == hashPreSerializedCommits collectedCommits

mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
parties' == parties
&& contestationPeriod' == contestationPeriod
&& headId' == headId

mustCollectAllValue =
traceIfFalse $(errorCode NotAllValueCollected) $
-- NOTE: Instead of checking the head output val' against all collected
Expand Down Expand Up @@ -221,7 +215,7 @@ checkCollectCom ctx@ScriptContext{scriptContextTxInfo = txInfo} (contestationPer
-- if it is there return the committed utxo
commitDatum :: TxOut -> [Commit]
commitDatum input = do
let datum = getTxOutDatum input
let !datum = getTxOutDatum input
case fromBuiltinData @Commit.DatumType $ getDatum datum of
Just (_party, commits, _headId) ->
commits
Expand All @@ -238,18 +232,12 @@ checkDecrement ::
Integer ->
Bool
checkDecrement ctx@ScriptContext{scriptContextTxInfo = txInfo} prevParties prevSnapshotNumber prevCperiod prevHeadId signature numberOfDecommitOutputs =
mustNotChangeParameters
mustNotChangeParameters (prevParties, nextParties) (prevCperiod, nextCperiod) (prevHeadId, nextHeadId)
&& checkSnapshot
&& checkSnapshotSignature
&& mustBeSignedByParticipant ctx prevHeadId
&& mustDecreaseValue
where
mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
prevHeadId == nextHeadId
&& prevParties == nextParties
&& prevCperiod == nextCperiod

checkSnapshot =
traceIfFalse $(errorCode SnapshotNumberMismatch) $
nextSnapshotNumber > prevSnapshotNumber
Expand Down Expand Up @@ -319,7 +307,7 @@ checkClose ctx parties initialUtxoHash sig cperiod headPolicyId =
&& mustBeSignedByParticipant ctx headPolicyId
&& mustInitializeContesters
&& mustPreserveValue
&& mustNotChangeParameters
&& mustNotChangeParameters (parties', parties) (cperiod', cperiod) (headId', headPolicyId)
where
mustPreserveValue =
traceIfFalse $(errorCode HeadValueIsNotPreserved) $
Expand Down Expand Up @@ -370,12 +358,6 @@ checkClose ctx parties initialUtxoHash sig cperiod headPolicyId =
LowerBound (Finite t) _ -> t
_InfiniteBound -> traceError $(errorCode InfiniteLowerBound)

mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
headId' == headPolicyId
&& parties' == parties
&& cperiod' == cperiod

mustInitializeContesters =
traceIfFalse $(errorCode ContestersNonEmpty) $
null contesters'
Expand Down Expand Up @@ -428,7 +410,7 @@ checkContest ctx contestationDeadline contestationPeriod parties closedSnapshotN
&& mustBeWithinContestationPeriod
&& mustUpdateContesters
&& mustPushDeadline
&& mustNotChangeParameters
&& mustNotChangeParameters (parties', parties) (contestationPeriod', contestationPeriod) (headId', headId)
&& mustPreserveValue
where
mustPreserveValue =
Expand All @@ -453,12 +435,6 @@ checkContest ctx contestationDeadline contestationPeriod parties closedSnapshotN
time <= contestationDeadline
_ -> traceError $(errorCode ContestNoUpperBoundDefined)

mustNotChangeParameters =
traceIfFalse $(errorCode ChangedParameters) $
parties' == parties
&& headId' == headId
&& contestationPeriod' == contestationPeriod

mustPushDeadline =
if length contesters' == length parties'
then
Expand Down Expand Up @@ -564,6 +540,18 @@ getHeadAddress :: ScriptContext -> Address
getHeadAddress = txOutAddress . txInInfoResolved . getHeadInput
{-# INLINEABLE getHeadAddress #-}

mustNotChangeParameters ::
([Party], [Party]) ->
(ContestationPeriod, ContestationPeriod) ->
(CurrencySymbol, CurrencySymbol) ->
Bool
mustNotChangeParameters (parties', parties) (contestationPeriod', contestationPeriod) (headId', headId) =
traceIfFalse $(errorCode ChangedParameters) $
parties' == parties
&& contestationPeriod' == contestationPeriod
&& headId' == headId
{-# INLINEABLE mustNotChangeParameters #-}

-- XXX: We might not need to distinguish between the three cases here.
mustBeSignedByParticipant ::
ScriptContext ->
Expand Down Expand Up @@ -643,7 +631,7 @@ hashTxOuts =
-- | Check if 'TxOut' contains the PT token.
hasPT :: CurrencySymbol -> TxOut -> Bool
hasPT headCurrencySymbol txOut =
let pts = findParticipationTokens headCurrencySymbol (txOutValue txOut)
let !pts = findParticipationTokens headCurrencySymbol (txOutValue txOut)
in length pts == 1
{-# INLINEABLE hasPT #-}

Expand Down

0 comments on commit 603d1c7

Please sign in to comment.