Skip to content

Commit

Permalink
Fix for the golden plutus tests with blueprint apply
Browse files Browse the repository at this point in the history
  • Loading branch information
v0d1ch committed Nov 6, 2024
1 parent 4ca8892 commit 5ebb06a
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 56 deletions.
1 change: 1 addition & 0 deletions hydra-plutus/hydra-plutus.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ test-suite tests
, plutus-ledger-api
, process
, QuickCheck
, text

build-tool-depends: hspec-discover:hspec-discover

Expand Down
16 changes: 8 additions & 8 deletions hydra-plutus/plutus.json

Large diffs are not rendered by default.

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/vDeposit.plutus
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "PlutusScriptV3",
"description": "hydra-vDeposit-0.19.0-289-gad254dc10",
"description": "hydra-vDeposit-0.19.0-284-g2f3d76240",
"cborHex": "5912fe5912fb0100003233223332223233223232323232323232323232323232323232323232323232323232323232323223235300100222233333350012230154910350543500230144910350543500223015490103505435002301449103505435002253350012153353322323353232325335333573466e1d20000020350341321223002003375c6ae8400454cd4ccd5cd19b87480080080d40d04c8488c00400cdd69aba10011301c01835573c0046aae74004dd50041299a9a9a9808a80111000912999a8011099809a490344303100333573466e24004d401c8880080d80d44c07124103443032001301c4910344303200133010490103443036003301c00135004222003103225335353530115002220022253335002213301349010344303400333573466e24004d401c8880080d40d84c07124103443033001301c4910344303300133010490103443035003301c3724603a66a0320026aa004444444444444444401c6e48c098c08cc0a8d401088800440c84d400488800cc8c8c94cd4ccd5cd19b87480000080c80c44c8c8c8c94cd4ccd5cd19b87480000080d80d44c8ccc88848ccc00401000c008dd71aba1002375a6ae84004c014d5d09aba2001357440022603a0326aae78008d55ce8009baa35742004602c4646464a66a666ae68cdc3a400000406c06a26644246600200600460206ae84004dd71aba1357440022603a0326aae78008d55ce8009baa0011301901535573c0046aae74004dd50009803804080e0980b249035054350013015491035054350023014491035054350032323232323232323223232325335333573466e1d2000002035034132333222123330010040030023232325335333573466e1d2000002039038132323232323232323232323232323333333333333333222222222222222212333333333333333300101101000f00e00d00c00b00a0090080070060050040030023302b01a3574201e660560346ae84038cc0ac06cd5d08069bad3574201866603eeb94078d5d08059981580c1aba100a33301f01c75a6ae84024c8c8c94cd4ccd5cd19b87480000081281244cc8848cc00400c008c8c8c94cd4ccd5cd19b87480000081341304cc8848cc00400c008cc0b9d69aba1001302d357426ae880044c0d00c0d55cf0011aab9d00137546ae84004c8c8c94cd4ccd5cd19b87480000081341304cc8848cc00400c008cc0b9d69aba1001302d357426ae880044c0d00c0d55cf0011aab9d00137546ae84d5d1000898188169aab9e00235573a0026ea8d5d080419815bae3574200e66603e4646464a66a666ae68cdc3a40000040960942642444444600a00e6eb8d5d08008a99a999ab9a3370e90010010258250990911111180100398129aba100115335333573466e1d200400204b04a132122222230030073020357420022a66a666ae68cdc3a400c0040960942664424444446600c01000e6eb4d5d0800980e1aba1357440022a66a666ae68cdc3a40100040960942642444444600200e603a6ae8400454cd4ccd5cd19b874802800812c1284cc884888888cc01002001cdd69aba1001301b357426ae880044c0c80b8d55cf0011aab9d001375400204c6ae84018ccc07dd70131aba1005375c6ae84010ccc07c064cc07c0a48c8c8c94cd4ccd5cd19b874800000812c1284488800854cd4ccd5cd19b874800800812c1284488800454cd4ccd5cd19b874801000812c1284488800c4c0c80b8d55cf0011aab9d00137540026ae8400ccc0ac05cd5d08011980ebad357420026603aeb4d5d09aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae880044c080070d55cf0011aab9d00137546ae84008d5d0800991919299a999ab9a3370e900000101c81c099091111118028039bae357420022a66a666ae68cdc3a40040040720702664424444446600401000e60266ae84004cc03c060d5d09aba200115335333573466e1d200400203903813212222223003007300e357420022a66a666ae68cdc3a400c0040720702664424444446600c01000e6eb4d5d080098051aba1357440022a66a666ae68cdc3a40100040720702642444444600200e60166ae8400454cd4ccd5cd19b87480280080e40e04cc884888888cc01002001cdd69aba10013009357426ae880044c080070d55cf0011aab9d00137546ae84d5d10009aba20011301c01835573c0046aae74004dd50009191919299a999ab9a3370e900000101a019899199911091998008020018011bad3574200460146ae84004c8c8c94cd4ccd5cd19b87480000080e00dc4c8ccc88848888888ccc00c028024020cc03c06cd5d08011aba10013300f75c6ae84d5d10009aba200115335333573466e1d20020020380371332212222222330070090083300e01a35742002646464a66a666ae68cdc3a40000040760742664424660020060046eb4d5d08009bad357426ae880044c088078d55cf0011aab9d00137546ae84d5d10008a99a999ab9a3370e900200101c01b899910911111119801004804199808006bad357420026601ceb8d5d09aba200115335333573466e1d20060020380371321222222230040083300e01a357420022a66a666ae68cdc3a401000407006e2646466664444244444446666002016014012010660200386ae8400ccc07803cd5d0801199809007bad357420026a646464a66a666ae68cdc3a400000407a0782660586eb4d5d08009bad357426ae880044c090080d55cf0011aab9d00137546ae84d5d1000911980b8010009aba2001357440022a66a666ae68cdc3a401400407006e26644244444446600a0120106601c0346ae84004c8c8c94cd4ccd5cd19b87480000080ec0e84cc045d71aba10011302201e35573c0046aae74004dd51aba1357440022a66a666ae68cdc3a401800407006e22444444400c2603e0366aae78008d55ce8009baa357426ae88004d5d10008980d80b9aab9e00235573a0026ea8004c88c8c8c94cd4ccd5cd19b87480000080d00cc4cc88488888888888cc014034030c024d5d0800998053ad357426ae8800454cd4ccd5cd19b87480080080d00cc4cc88488888888888cc008034030c024d5d0800998053ad357426ae8800454cd4ccd5cd19b87480100080d00cc4cc88488888888888cc028034030c024d5d080098029aba1357440022a66a666ae68cdc3a400c00406806626466644424444444444466600c01c01a01860146ae84008c018d5d08009bad357426ae88004d5d10008a99a999ab9a3370e900400101a019899910911111111111980380680618049aba1001375a6ae84d5d10008a99a999ab9a3370e900500101a019899091111111111180080618049aba100115335333573466e1d200c002034033133221222222222223300300d00c3009357420026eb4d5d09aba200115335333573466e1d200e002034033133221222222222223300900d00c375c6ae84004dd71aba1357440022a66a666ae68cdc3a4020004068066266442444444444446601001a0186eb8d5d08009bad357426ae8800454cd4ccd5cd19b87480480080d00cc4cc88488888888888cc02c034030c024d5d080098049aba1357440022a66a666ae68cdc3a4028004068066264244444444444600801860126ae840044c06c05cd55cf0011aab9d0013754002644646464a66a666ae68cdc3a4000004068066264244460060086eb8d5d08008a99a999ab9a3370e900100101a0198990911180080218029aba100115335333573466e1d200400203403313322122233002005004375c6ae84004c014d5d09aba20011301b01735573c0046aae74004dd50009191919299a999ab9a3370e90000010198190990911180180218041aba100115335333573466e1d20020020330321122200215335333573466e1d2004002033032112220011301a01635573c0046aae74004dd50009191919299a999ab9a3370e90000010190188990911180180218039aba100115335333573466e1d2002002032031132122230020043007357420022a66a666ae68cdc3a4008004064062264244460020086eb8d5d08008980c80a9aab9e00235573a0026ea80048c8c8c94cd4ccd5cd19b87480000080c40c04cc8848cc00400c008c02cd5d080098029aba135744002260300286aae78008d55ce8009baa00123232325335333573466e1d200000203002f13232333322221233330010050040030023232325335333573466e1d2000002035034133221233001003002300a35742002660164646464a66a666ae68cdc3a40000040720702642446004006601c6ae8400454cd4ccd5cd19b87480080080e40e04c8ccc888488ccc00401401000cdd69aba1002375a6ae84004dd69aba1357440026ae880044c080070d55cf0011aab9d00137540026ae84d5d10008980e00c1aab9e00235573a0026ea8d5d08019998053ae500935742004646464a66a666ae68cdc3a400000406a068224440062a66a666ae68cdc3a400400406a068264244460020086eb8d5d08008a99a999ab9a3370e900200101a81a099091118010021aba10011301c01835573c0046aae74004dd51aba10013300875c6ae84d5d10009aba2001357440022602e0266aae78008d55ce8009baa00123232325335333573466e1d200000202f02e1321223002003375c6ae8400454cd4ccd5cd19b87480080080bc0b84c8488c00400cdd71aba10011301601235573c0046aae74004dd500091191919299a999ab9a3370e900100101781708910008a99a999ab9a3370e9000001017817099091180100198029aba10011301601235573c0046aae74004dd5000899800bae75a4464460046eac004c0ac88cccd55cf800901391919a8139980e98031aab9d001300535573c00260086ae8800cd5d080100a1191919299a999ab9a3370e90000010150148999109198008018011bae357420026eb4d5d09aba20011301100d35573c0046aae74004dd50009813111299a999ab9a3370e00201a05004e2601e9201035054330015335333573466e200040340a009c4cc00ccdc080680119b8100d001132332212330010030023370800600266e10008004cc010008004c0948894cd4ccd5cd19b8700100c02702610021330030013370c00400244a66a002204a266ae700080908d4004888888888888888802480048c8c8c94cd4ccd5cd19b874800000809008c408c54cd4ccd5cd19b874800800809008c40904c02c01cd55cf0011aab9d001375400244646464a66a666ae68cdc3a4000004048046224440042a66a666ae68cdc3a400400404804626424446006008600a6ae8400454cd4ccd5cd19b874801000809008c448880044c02c01cd55cf0011aab9d00137540024646464a66a666ae68cdc3a40000040440422664424660020060046eb8d5d08009bad357426ae880044c024014d55cf0011aab9d00137540029210350543100232230023758002603c446666aae7c00480688cd4064c010d5d080118019aba200200748000cc06c884894cd4ccd5cd19b8900248000078074406054cd40044060884cd4064008cd4c01848004cdc08022400400224002464c66ae700040084800488ccd5cd19b8f0020010190183017225335001100c221337146eccd40088888cdd2a400066ae80d401088cdd2a400066ae80c02c008cd5d019806119a800919ba548000cd5d018068009bb100f2223374a900119aba0375000666ae80dd400119aba037500026ec4044004dd880699aba033300c75266018ea48dd400080199aba05333500213374a90001bb100b213374a900219aba000137620184266e9520023357406ea4004dd880619aba03300a7520026ec402cc0100048cd40048cdd2a400066ae80dd48009bb100423374a900119aba037520026ec4010894cd400484cdd2a400066ae80c00c004dd8802099ba548008dd8801a4c446446e98c008004c05488cd40052f5bded8c0446a0044466ae80cdd8180400118038009803001910919800801801180891299a800898011801806110a99a800880111098031803802980811299a8008805910a99a800880191099a8071980380200118030009807911299a8010800910a99a801880219110a999a998088028010a801899a807801198040038008a801899a80600119802800801980711299a80088019109a8011119b8a00130060034890035003222001350022220023500122200333230010012212323330022300222230030042300222230020042300222230010042353300400400335003001122223330042533500113350060070072215335001133500800300922153233353300d00500315001133300800333500b00500c002150011333006002233500b00500100122253350011335008335008003002300600932221533353300d00600215003133300800233500b0060050011500313350083350080030023006001222532335002130014988854c8ccd4cc03801c00c540044c011261500113330070022300633500c0070010012335009300333500900400a30070021220021221223300100400332223500222350022235005223500222533353300b00600215335333573466e1c01400404003c403854cd4ccd5cd19b8900500101000f100c100d13300b00600213300b006002225335333573466e3c00800401c018401454cd4ccd5cd19b91002001007006100310041222003122200212220011220021220012323001001230022330020020011"
}
2 changes: 1 addition & 1 deletion hydra-plutus/scripts/vHead.plutus

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions hydra-plutus/scripts/vInitial.plutus

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion hydra-plutus/test/Hydra/Plutus/GoldenSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ module Hydra.Plutus.GoldenSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import Control.Lens ((^?!))
import Data.Aeson.Lens (key, nth, _String)
import Data.Text qualified as T
import Hydra.Cardano.Api (
AsType (AsPlutusScriptV3, AsScript),
File (..),
Expand All @@ -35,10 +38,13 @@ import Test.Hspec.Golden (Golden (..))

spec :: Spec
spec = do
it "Commit validator script" $ do
it "Commit and initial validator script" $ do
original <- readFileBS "plutus.json"
-- This re-generate plutus.json
let commitHash = T.unpack $ original ^?! key "validators" . nth 0 . key "compiledCode" . _String
void $ readProcess "aiken" ["build", "-t", "compact"] ""
void $ readProcess "aiken" ["blueprint", "apply", "-v", "initial.initial.spend", "-o", "plutus.json", commitHash] ""
void $ readProcess "aiken" ["blueprint", "apply", "-v", "initial.initial.else", "-o", "plutus.json", commitHash] ""
regenerated <- readFileBS "plutus.json"
regenerated `shouldBe` original
it "Initial validator script" $
Expand Down
18 changes: 13 additions & 5 deletions hydra-plutus/validators/commit.ak
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use aiken/collection/list
use cardano/assets.{PolicyId, Value}
use cardano/transaction.{OutputReference, Transaction}
use util.{trace_if_false, must_burn_st, hydra_head_v1}
use util.{hydra_head_v1, must_burn_st, trace_if_false}

// TODO: Party is not used on-chain but is needed off-chain while it's still
// based on mock crypto. When we move to real crypto we could simply use
// the PT's token name to identify the committing party
pub type Datum {
party: Data,
commits: Data,
head_id: PolicyId
head_id: PolicyId,
}

pub type Redeemer {
Expand Down Expand Up @@ -38,15 +38,20 @@ fn toErrorCode(err: CommitError) -> String {
//
// * ST is present in the output if the redeemer is 'ViaCollectCom'
validator commit {
spend(datum: Option<Datum>, redeemer: Redeemer, _utxo: OutputReference, self: Transaction) -> Bool {
spend(
datum: Option<Datum>,
redeemer: Redeemer,
_utxo: OutputReference,
self: Transaction,
) {
expect Some(datum) = datum
when redeemer is {
// NOTE: The reimbursement of the committed output 'commit' is
// delegated to the 'head' script who has more information to do it.
ViaAbort ->
trace_if_false(
toErrorCode(STNotBurnedError),
must_burn_st(datum.head_id, self.mint)?
must_burn_st(datum.head_id, self.mint)?,
)
ViaCollectCom -> {
expect Some(head_output) = self.outputs |> list.head
Expand All @@ -57,9 +62,12 @@ validator commit {
}
}
}

else(_) {
fail
}
}

fn has_st(head_id: PolicyId, value: Value) -> Bool {
assets.quantity_of(value, head_id, hydra_head_v1) == 1
}

58 changes: 23 additions & 35 deletions hydra-plutus/validators/initial.ak
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use aiken/crypto.{ScriptHash, VerificationKeyHash}
use aiken/option
use cardano/assets.{PolicyId, Value}
use cardano/transaction.{
DatumHash, InlineDatum, Input, Output, NoDatum, OutputReference, Transaction,
DatumHash, InlineDatum, Input, NoDatum, Output, OutputReference, Transaction,
}
use commit
use util.{trace_if_false, trace_fail, must_burn_st}
use util.{must_burn_st, trace_fail, trace_if_false}

pub type Datum =
PolicyId
Expand Down Expand Up @@ -41,7 +41,6 @@ pub type Redeemer {
// MintingOrBurningIsForbidden -> @"I14"
// OutRefNotFound -> @"I15"


// | The v_initial validator verifies that:
//
// * spent in a transaction also consuming a v_head output
Expand All @@ -62,11 +61,7 @@ validator initial(commit_validator: ScriptHash) {
) {
expect Some(head_id) = datum
when redeemer is {
ViaAbort ->
trace_if_false(
@"I01" ,
must_burn_st(head_id, self.mint)?
)
ViaAbort -> trace_if_false(@"I01", must_burn_st(head_id, self.mint)?)
ViaCommit(committed_refs) ->
check_commit(head_id, commit_validator, committed_refs, utxo, self)
}
Expand All @@ -91,8 +86,7 @@ fn check_commit(
[dat] ->
when dat is {
NoDatum -> trace_fail(@"I09")
DatumHash(_dataHash) ->
trace_fail(@"I10")
DatumHash(_dataHash) -> trace_fail(@"I10")
InlineDatum(data) ->
if data is commit.Datum {
data
Expand All @@ -103,7 +97,6 @@ fn check_commit(
_otherwise -> trace_fail(@"I12")
}
if commit_datum.commits is locked_commits: List<CommitDatum> {

let committed_utxo =
committed_refs
|> list.map(
Expand All @@ -122,19 +115,23 @@ fn check_commit(
|> transaction.find_input(utxo)
|> option.map(fn(i) { i.output.value })
|> option.or_else(assets.zero)
check_head_id(head_id, commit_datum.head_id)
&& must_be_signed_by_participant( head_id, initial_value, self.extra_signatories)
&& must_not_mint_or_burn(self.mint)
&& check_committed_value(committed_utxo, initial_value, tx_outputs)
&& check_locked_commit(committed_utxo, locked_commits)
check_head_id(head_id, commit_datum.head_id) && must_be_signed_by_participant(
head_id,
initial_value,
self.extra_signatories,
) && must_not_mint_or_burn(self.mint) && check_committed_value(
committed_utxo,
initial_value,
tx_outputs,
) && check_locked_commit(committed_utxo, locked_commits)
} else {
trace_fail(@"Failed to decode locked_commits")
}
}

fn get_script_outputs (commit_validator: ScriptHash, self : Transaction){
self.outputs
|> transaction.find_script_outputs(commit_validator)
fn get_script_outputs(commit_validator: ScriptHash, self: Transaction) {
self.outputs
|> transaction.find_script_outputs(commit_validator)
}

fn check_committed_value(
Expand All @@ -145,7 +142,8 @@ fn check_committed_value(
let initial_lovelace =
initial_value
|> assets.lovelace_of
let locked_value = commit_outputs
let locked_value =
commit_outputs
|> list.map(fn(output) { output.value })
|> list.reduce(assets.zero, assets.merge)
|> assets.lovelace_of
Expand Down Expand Up @@ -193,10 +191,7 @@ fn check_locked_commit(
}

fn check_head_id(self_head_id: PolicyId, committed_head_id: PolicyId) -> Bool {
trace_if_false(
@"I13",
self_head_id == committed_head_id,
)
trace_if_false(@"I13", self_head_id == committed_head_id)
}

fn must_be_signed_by_participant(
Expand All @@ -208,8 +203,7 @@ fn must_be_signed_by_participant(
initial_value
|> assets.tokens(head_id)
when dict.size(token_map) is {
0 ->
trace_fail(@"I05")
0 -> trace_fail(@"I05")
1 -> {
let participation_tokens =
token_map
Expand All @@ -222,19 +216,13 @@ fn must_be_signed_by_participant(
signatories
|> list.has(our_participation_token_name),
)
_more_than_one_token ->
trace_fail(@"I06")
_more_than_one_token -> trace_fail(@"I06")
}
}
_more_than_one_token ->
trace_fail(@"I06")
_more_than_one_token -> trace_fail(@"I06")
}
}

fn must_not_mint_or_burn(mint: Value) -> Bool {
trace_if_false(
@"I14" ,
assets.is_zero(mint),
)
trace_if_false(@"I14", assets.is_zero(mint))
}

1 change: 0 additions & 1 deletion hydra-plutus/validators/util.ak
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use cardano/assets.{PolicyId, Value}

pub const hydra_head_v1: ByteArray = "HydraHeadV1"
Expand Down

0 comments on commit 5ebb06a

Please sign in to comment.