Skip to content

Commit

Permalink
Merge pull request #658 from input-output-hk/ensemble/st-in-output-no…
Browse files Browse the repository at this point in the history
…t-checked

ST in output not checked
  • Loading branch information
v0d1ch authored Jan 11, 2023
2 parents 5ed00df + e780c01 commit 21e8855
Show file tree
Hide file tree
Showing 26 changed files with 536 additions and 516 deletions.
10 changes: 9 additions & 1 deletion hydra-cardano-api/src/Hydra/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Hydra.Cardano.Api.CtxUTxO (ToUTxOContext (..))
import Hydra.Cardano.Api.Hash (unsafeScriptHashFromBytes)
import Hydra.Cardano.Api.MultiAssetSupportedInEra (multiAssetSupportedInEra)
import Plutus.V1.Ledger.Value (flattenValue)
import Plutus.V2.Ledger.Api (adaSymbol, adaToken, fromBuiltin, unCurrencySymbol, unTokenName)
import Plutus.V2.Ledger.Api (CurrencySymbol, adaSymbol, adaToken, fromBuiltin, unCurrencySymbol, unTokenName)
import qualified Plutus.V2.Ledger.Api as Plutus

-- * Extras
Expand Down Expand Up @@ -96,3 +96,11 @@ fromPlutusValue plutusValue =
toPlutusValue :: Value -> Plutus.Value
toPlutusValue =
Ledger.transValue . toLedgerValue

-- | Convert Cardano api 'PolicyId' to Plutus `CurrencySymbol`
toPlutusCurrencySymbol :: PolicyId -> CurrencySymbol
toPlutusCurrencySymbol = Ledger.transPolicyID . toLedgerPolicyID

-- | Convert Cardano api 'PolicyId' to Cardano ledger `PolicyID`
toLedgerPolicyID :: PolicyId -> Ledger.PolicyID StandardCrypto
toLedgerPolicyID (PolicyId sh) = Ledger.PolicyID (toShelleyScriptHash sh)
3 changes: 2 additions & 1 deletion hydra-node/exe/tx-cost/TxCost.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import Hydra.Ledger.Cardano.Evaluate (
maxTxSize,
slotNoFromUTCTime,
)
import Hydra.Options (maximumNumberOfParties)
import Hydra.Snapshot (genConfirmedSnapshot)
import Plutus.Orphans ()
import Test.QuickCheck (generate, sublistOf)
Expand Down Expand Up @@ -194,7 +195,7 @@ computeFanOutCost = do
pure $ interesting <> limit
where
compute numElems = do
(utxo, tx) <- generate $ genFanoutTx 3 numElems
(utxo, tx) <- generate $ genFanoutTx maximumNumberOfParties numElems
case checkSizeAndEvaluate tx utxo of
Just (txSize, memUnit, cpuUnit, minFee) ->
pure $ Just (NumUTxO numElems, txSize, memUnit, cpuUnit, minFee)
Expand Down
84 changes: 42 additions & 42 deletions hydra-node/golden/ChainState.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21991,7 +21991,7 @@
"chainState": {
"chainState": {
"contents": {
"openHeadId": "ca007409d49dd87ff1821b56468c83faa7507134117045306aac4551",
"headId": "ca007409d49dd87ff1821b56468c83faa7507134117045306aac4551",
"openHeadTokenScript": {
"cborHex": "5910f15910ee0100003333232323233223232323232323232323232332232323322323232323232323232323232323232323232323232323232323232323232323232323232222232232325335323232323232323253350081330134910b6275726e742077726f6e6700335501b30134912a696e636f6e73697374656e74207175616e74697479206f66206865616420746f6b656e73206275726e740033320015019335501b33355501d302612001223303100133225335001213003001148000cd540ad5409d40bccd54074cd5406140714010c8004cd540748d4004888800cd40088800520003012500133533355016501a5002335501b2001301150012335501c3223335002220013502e35002220020013212330010022233702004002a05c666aa054aa04ca05c0029000099aa80d9809a490c6d696e7465642077726f6e670033018323232325335330180140031335501f301749011e6e6f20696e697469616c206f75747075747320666f722070617274696573003332001501d00148000884cc078ccc8005407c00ccd54074c0b0480054010cc8cd54004c8cd40d088ccd400c88008008004d40048800448cc00408013c8d4004894cd4ccd540814090028004854cd40044c141262213500222533500313332001502800148008884c159261350524901116e6f20505420646973747269627574656400302c12001500450061330160120015006500633018355004220023301833320015019355004220013302f500348008ccd54c09848004d40a8cd40b8894cd40084124400411894cd4ccd5cd19b8f3500f2200235001220020480471333573466e1cd403c88004d40048800412011c411cc8cd54c080480048d400488008004c04940144d4018880084c06801454cd4cc04403140084c105262213500222533500315333500213504949010d6d697373696e6720646174756d00213504a49117756e657870656374656420696e6c696e6520646174756d00232153353235001222222222222300e0025008215335335501f23232323232323232323333333574801446666ae68cdc3a8012400c46666aae7d40288d412c4888800c941281688cccd5cd19b875003480108cccd55cfa8059282591999aab9f50082504c233335573ea0104a09a46666aae7d4020941388cccd55cf9aba250092533533503603735742a02042a66a60766ae854034854cd4c0e8d5d0a80690a99a981e9aba1500d21350541222233330040080070060051505215051150501504f2504f05f05e05d05c05b23333573466e1d40112002233335573ea0184a09846666aae7d402c941348cccd55cfa8059282711999aab9f35744a0184a66a60726ae854040854cd4cd40dc0e0d5d0a80790a99a981d1aba1500f21350531222233300100700600515051150501504f2504f05f05e05d05c23333573466e1d40152000233335573ea01a4a09a46666aae7d4034941388cccd55cf9aba2500e25335303935742a02042a66a66a06e0706ae85404084d414848888cc008018014541405413c9413c17c1781749413016c168164160941209412094120941201604d55cea80409aba25001135744a00226ae8940104d5d1280089aba25002135744a00226aae7940044dd500080090a9999a800911109a828a4919756e657870656374656420537461746520696e20646174756d0013504d490119756e657870656374656420537461746520696e20646174756d00221335501e302d1200100122213505049119756e657870656374656420537461746520696e20646174756d0013504c49012e657870656374656420636f6d6d697420646174756d20747970652c20676f7420736f6d657468696e6720656c73650013504b490114636f756c64206e6f742066696e6420646174756d003200135504d2253350011503d22135002225335333573466e3c00801c13c1384d41080044c01800c884c11d2615335333550125016500233550172001300d500121333355501a550222235001223355038330190023332001501a00448008cc0c000520023355034044480000044cd540cc109200013500222002130160013333573466e1cd55cea801a4000466442466002006004646464646464646464646464646666ae68cdc39aab9d500c480008cccccccccccc88888888888848cccccccccccc00403403002c02802402001c01801401000c008cd40e00e4d5d0a80619a81c01c9aba1500b33503803a35742a014666aa078eb940ecd5d0a804999aa81e3ae503b35742a01066a07008c6ae85401cccd540f011dd69aba150063232323333573466e1cd55cea801240004664424660020060046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd4145d69aba150023052357426ae8940088c98c8168cd5ce02982d02c09aab9e5001137540026ae854008c8c8c8cccd5cd19b8735573aa004900011991091980080180119a828bad35742a00460a46ae84d5d1280111931902d19ab9c05305a058135573ca00226ea8004d5d09aba2500223263205633573809e0ac0a826aae7940044dd50009aba1500533503875c6ae854010ccd540f010c8004d5d0a801999aa81e3ae200135742a004608a6ae84d5d1280111931902919ab9c04b052050135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d55cf280089baa00135742a006606a6ae84d5d1280191931902219ab9c03d0440423333573466e1d40112002212200223333573466e1d40152000212200123263204433573807a088084082208426a0809210350543500135573ca00226ea80044d55ce9baa00123500122222222222200823500122222222222200c2253350011036133573800406a446a002444444444444666aa601e24002446a00444446a0084466a00440104a66a666ae68cdc780080b825024899a81c19aa81d00280300408042818005190009aa81b1108911299a800880111099802801199aa98038900080280200088911911801000990009aa81b91299a8008a8139109a80111299a9980400100389a8160008980300188911999aa8011919a80c11199a80b0018008011a809800a8049119b8000148008005200022533500210011030122333573466e1c0080040c00bc488ccd5cd19b8f00200102f02e1122300200123500122333350012326320323357389201024c680003220012326320323357389201024c68000322326320323357389201024c680003211122223333550045005003001002133500e2223003300200120013200135502c22112253350011501b22133501c3004002335530061200100400112322333333357480024a03a4a03a460066eb000894074940740b4c8004d540b088cccd55cf80091a80f280d1299a98021aba1002215335300435744006426a04066a0360040022a03c2a03a05a4a66a6004002426a0360022a03246666666ae90004940649406494064940648d4068dd7001014919191999999aba400323333573466e1cd55cea801a400046666aae7d400c940708cccd55cf9aba2500425335300835742a00a426a03e0022a03a4a03a05a0584a0360544a0344a0344a0344a03405426aae7940044dd500091999999aba4001250172501725017235018375a0044a02e04e2222444666aa60082400200646a00244600a002002640026aa04a4422444a66a00226a00c006442666a01200a6008004666aa600e2400200a008002246a00244002246a00244004224466aa0046a00600240022466a00644666a006440040040026a004002246a002440022442466002006004266a002004900009109198008018011119b800020011232230023758002640026aa038446666aae7c004940288cd4024c010d5d080118019aba200201d232323333573466e1cd55cea80124000466442466002006004601e6ae854008c014d5d09aba2500223263201d33573802c03a03626aae7940044dd50009191919191999ab9a3370e6aae75401120002333322221233330010050040030023232323333573466e1cd55cea8012400046644246600200600460306ae854008cd404005cd5d09aba2500223263202233573803604404026aae7940044dd50009aba150043335500875ca00e6ae85400cc8c8c8cccd5cd19b875001480108c84888c008010d5d09aab9e500323333573466e1d4009200223212223001004375c6ae84d55cf280211999ab9a3370ea00690001091100191931901219ab9c01d024022021020135573aa00226ea8004d5d0a80119a8063ae357426ae8940088c98c8078cd5ce00b80f00e09aba25001135744a00226aae7940044dd5000899aa800bae75a224464460046eac004c8004d5406488c8cccd55cf80112804119a80399aa80498031aab9d5002300535573ca00460086ae8800c06c4d5d08008891001091091198008020018891091980080180109119191999ab9a3370ea002900011a80398029aba135573ca00646666ae68cdc3a801240044a00e464c6403066ae700440600580544d55cea80089baa0011212230020031122001232323333573466e1d400520062321222230040053007357426aae79400c8cccd5cd19b875002480108c848888c008014c024d5d09aab9e500423333573466e1d400d20022321222230010053007357426aae7940148cccd5cd19b875004480008c848888c00c014dd71aba135573ca00c464c6402c66ae7003c05805004c0480444d55cea80089baa001232323333573466e1cd55cea80124000466442466002006004600a6ae854008dd69aba135744a004464c6402466ae7002c0480404d55cf280089baa0012323333573466e1cd55cea800a400046eb8d5d09aab9e500223263201033573801202001c26ea80048c8c8c8c8c8cccd5cd19b8750014803084888888800c8cccd5cd19b875002480288488888880108cccd5cd19b875003480208cc8848888888cc004024020dd71aba15005375a6ae84d5d1280291999ab9a3370ea00890031199109111111198010048041bae35742a00e6eb8d5d09aba2500723333573466e1d40152004233221222222233006009008300c35742a0126eb8d5d09aba2500923333573466e1d40192002232122222223007008300d357426aae79402c8cccd5cd19b875007480008c848888888c014020c038d5d09aab9e500c23263201933573802403202e02c02a02802602402226aae7540104d55cf280189aab9e5002135573ca00226ea80048c8c8c8c8cccd5cd19b875001480088ccc888488ccc00401401000cdd69aba15004375a6ae85400cdd69aba135744a00646666ae68cdc3a80124000464244600400660106ae84d55cf280311931900919ab9c00b01201000f135573aa00626ae8940044d55cf280089baa001232323333573466e1d400520022321223001003375c6ae84d55cf280191999ab9a3370ea004900011909118010019bae357426aae7940108c98c803ccd5ce00400780680609aab9d50011375400224464646666ae68cdc3a800a40084244400246666ae68cdc3a8012400446424446006008600c6ae84d55cf280211999ab9a3370ea00690001091100111931900819ab9c00901000e00d00c135573aa00226ea80048c8cccd5cd19b8750014800880208cccd5cd19b8750024800080208c98c8030cd5ce00280600500489aab9d3754002921035054310023500549011b65787065637465642073696e676c652068656164206f75747075740023500449012077726f6e67207175616e74697479206f66205054206469737472696275746564001220021220011232632003335738002006930900088919180080091198019801001000a4411c8d73f125395466f1d68570447e4f4b87cd633c6728f3802b2dcfca200048811c58d3332af22d527dc44f97a34e38f41c3489c22f18fed9d30e176c54003351223300248920030303060601060200010304050100050808060207080108020704020103020300480c08848cc00400c0088005",
"description": "",
Expand Down
49 changes: 27 additions & 22 deletions hydra-node/src/Hydra/Chain/Direct/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import Hydra.Ledger (IsTx (hashUTxO))
import Hydra.Ledger.Cardano (genOneUTxOFor, genTxIn, genUTxOAdaOnlyOfSize, genVerificationKey)
import Hydra.Ledger.Cardano.Evaluate (genPointInTimeBefore, genValidityBoundsFromContestationPeriod, slotNoFromUTCTime)
import Hydra.Ledger.Cardano.Json ()
import Hydra.Options (maximumNumberOfParties)
import Hydra.Party (Party, deriveParty)
import Hydra.Snapshot (
ConfirmedSnapshot (..),
Expand Down Expand Up @@ -214,7 +215,7 @@ data InitialState = InitialState
{ initialThreadOutput :: InitialThreadOutput
, initialInitials :: [UTxOWithScript]
, initialCommits :: [UTxOWithScript]
, initialHeadId :: HeadId
, headId :: HeadId
, initialHeadTokenScript :: PlutusScript
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)
Expand All @@ -235,7 +236,7 @@ instance HasKnownUTxO InitialState where

data OpenState = OpenState
{ openThreadOutput :: OpenThreadOutput
, openHeadId :: HeadId
, headId :: HeadId
, openHeadTokenScript :: PlutusScript
, openUtxoHash :: UTxOHash
}
Expand All @@ -251,7 +252,7 @@ instance HasKnownUTxO OpenState where

data ClosedState = ClosedState
{ closedThreadOutput :: ClosedThreadOutput
, closedHeadId :: HeadId
, headId :: HeadId
, closedHeadTokenScript :: PlutusScript
}
deriving (Eq, Show, Generic, ToJSON, FromJSON)
Expand Down Expand Up @@ -295,9 +296,9 @@ commit ctx st utxo = do
case UTxO.pairs utxo of
[aUTxO] -> do
rejectByronAddress aUTxO
Right $ commitTx scriptRegistry networkId ownParty (Just aUTxO) initial
Right $ commitTx scriptRegistry networkId headId ownParty (Just aUTxO) initial
[] -> do
Right $ commitTx scriptRegistry networkId ownParty Nothing initial
Right $ commitTx scriptRegistry networkId headId ownParty Nothing initial
_ ->
Left (MoreThanOneUTxOCommitted @Tx)
where
Expand All @@ -306,6 +307,7 @@ commit ctx st utxo = do
InitialState
{ initialInitials
, initialHeadTokenScript
, headId
} = st

ownInitial :: Maybe (TxIn, TxOut CtxUTxO, Hash PaymentKey)
Expand Down Expand Up @@ -370,13 +372,14 @@ collect ::
Tx
collect ctx st = do
let commits = Map.fromList $ fmap tripleToPair initialCommits
in collectComTx networkId ownVerificationKey initialThreadOutput commits
in collectComTx networkId ownVerificationKey initialThreadOutput commits headId
where
ChainContext{networkId, ownVerificationKey} = ctx

InitialState
{ initialThreadOutput
, initialCommits
, headId
} = st

tripleToPair (a, b, c) = (a, (b, c))
Expand All @@ -397,7 +400,7 @@ close ::
PointInTime ->
Tx
close ctx st confirmedSnapshot startSlotNo pointInTime =
closeTx ownVerificationKey closingSnapshot startSlotNo pointInTime openThreadOutput
closeTx ownVerificationKey closingSnapshot startSlotNo pointInTime openThreadOutput headId
where
closingSnapshot = case confirmedSnapshot of
-- XXX: Not needing anything of the 'InitialSnapshot' is another hint that
Expand All @@ -415,6 +418,7 @@ close ctx st confirmedSnapshot startSlotNo pointInTime =
OpenState
{ openThreadOutput
, openUtxoHash
, headId
} = st

-- | Construct a contest transaction based on the 'ClosedState' and a confirmed
Expand All @@ -427,7 +431,7 @@ contest ::
PointInTime ->
Tx
contest ctx st confirmedSnapshot pointInTime = do
contestTx ownVerificationKey sn sigs pointInTime closedThreadOutput
contestTx ownVerificationKey sn sigs pointInTime closedThreadOutput headId
where
(sn, sigs) =
case confirmedSnapshot of
Expand All @@ -438,6 +442,7 @@ contest ctx st confirmedSnapshot pointInTime = do

ClosedState
{ closedThreadOutput
, headId
} = st

-- | Construct a fanout transaction based on the 'ClosedState' and off-chain
Expand Down Expand Up @@ -498,7 +503,7 @@ observeInit ctx tx = do
{ initialThreadOutput = threadOutput
, initialInitials = initials
, initialCommits = commits
, initialHeadId = headId
, headId = headId
, initialHeadTokenScript = headTokenScript
}

Expand Down Expand Up @@ -546,20 +551,20 @@ observeCollect ::
observeCollect st tx = do
let utxo = getKnownUTxO st
observation <- observeCollectComTx utxo tx
let CollectComObservation{threadOutput, headId, utxoHash} = observation
guard (headId == initialHeadId)
let CollectComObservation{threadOutput, headId = collectComHeadId, utxoHash} = observation
guard (headId == collectComHeadId)
let event = OnCollectComTx
let st' =
OpenState
{ openThreadOutput = threadOutput
, openHeadId = initialHeadId
, headId = headId
, openHeadTokenScript = initialHeadTokenScript
, openUtxoHash = utxoHash
}
pure (event, st')
where
InitialState
{ initialHeadId
{ headId = headId
, initialHeadTokenScript
} = st

Expand All @@ -584,8 +589,8 @@ observeClose ::
observeClose st tx = do
let utxo = getKnownUTxO st
observation <- observeCloseTx utxo tx
let CloseObservation{threadOutput, headId, snapshotNumber} = observation
guard (headId == openHeadId)
let CloseObservation{threadOutput, headId = closeObservationHeadId, snapshotNumber} = observation
guard (openHeadId == closeObservationHeadId)
let ClosedThreadOutput{closedContestationDeadline} = threadOutput
let event =
OnCloseTx
Expand All @@ -595,13 +600,13 @@ observeClose st tx = do
let st' =
ClosedState
{ closedThreadOutput = threadOutput
, closedHeadId = headId
, headId = openHeadId
, closedHeadTokenScript = openHeadTokenScript
}
pure (event, st')
where
OpenState
{ openHeadId
{ headId = openHeadId
, openHeadTokenScript
} = st

Expand All @@ -616,14 +621,14 @@ observeContest ::
observeContest st tx = do
let utxo = getKnownUTxO st
observation <- observeContestTx utxo tx
let ContestObservation{contestedThreadOutput, headId, snapshotNumber} = observation
guard (headId == closedHeadId)
let ContestObservation{contestedThreadOutput, headId = contestObservationHeadId, snapshotNumber} = observation
guard (closedStateHeadId == contestObservationHeadId)
let event = OnContestTx{snapshotNumber}
let st' = st{closedThreadOutput = closedThreadOutput{closedThreadUTxO = contestedThreadOutput}}
pure (event, st')
where
ClosedState
{ closedHeadId
{ headId = closedStateHeadId
, closedThreadOutput
} = st

Expand Down Expand Up @@ -838,7 +843,7 @@ genCommit =

genCollectComTx :: Gen (ChainContext, [UTxO], InitialState, Tx)
genCollectComTx = do
ctx <- genHydraContextFor 3
ctx <- genHydraContextFor maximumNumberOfParties
txInit <- genInitTx ctx
commits <- genCommits ctx txInit
cctx <- pickChainContext ctx
Expand All @@ -857,7 +862,7 @@ genCloseTx numParties = do

genContestTx :: Gen (HydraContext, PointInTime, ClosedState, Tx)
genContestTx = do
ctx <- genHydraContextFor 3
ctx <- genHydraContextFor maximumNumberOfParties
(u0, stOpen) <- genStOpen ctx
confirmed <- genConfirmedSnapshot 0 u0 []
cctx <- pickChainContext ctx
Expand Down
Loading

0 comments on commit 21e8855

Please sign in to comment.