Skip to content

Commit

Permalink
Decoded the cborHex using deserialiseFromBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
noonio authored and ffakenz committed Dec 18, 2024
1 parent 4603054 commit 60cf58f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 46 deletions.
24 changes: 5 additions & 19 deletions hydra-chain-observer/test/Hydra/ChainObserverSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ module Hydra.ChainObserverSpec where
import Hydra.Prelude
import Test.Hydra.Prelude

import Hydra.Cardano.Api (
fromPlutusScript,
utxoFromTx,
pattern PlutusScript,
)
import Hydra.Cardano.Api (utxoFromTx)
import Hydra.Chain.Direct.State (HasKnownUTxO (getKnownUTxO), genChainStateWithTx)
import Hydra.Chain.Direct.State qualified as Transition
import Hydra.Chain.Direct.Tx (HeadObservation (..))
import Hydra.ChainObserver.NodeClient (observeAll, observeTx)
import Hydra.Ledger.Cardano (genSequenceOfSimplePaymentTransactions)
import Hydra.Tx.ScriptRegistry (SerialisedScriptRegistry (..), mkValidatorScript, serialisedScriptRegistry)
import Hydra.Tx.ScriptRegistry (serialisedScriptRegistry)
import Test.Hydra.Tx.Fixture (testNetworkId)
import Test.QuickCheck (counterexample, forAll, forAllBlind, property, (=/=), (===))
import Test.QuickCheck.Property (checkCoverage)
Expand All @@ -27,7 +23,7 @@ spec =
genericCoverTable [transition] $
counterexample (show transition) $
let utxo = getKnownUTxO st <> utxoFromTx tx <> additionalUTxO
in case snd $ observeTx testNetworkId mockSerialisedScriptRegistry utxo tx of
in case snd $ observeTx testNetworkId serialisedScriptRegistry utxo tx of
Just (Init{}) -> transition === Transition.Init
Just (Commit{}) -> transition === Transition.Commit
Just (CollectCom{}) -> transition === Transition.Collect
Expand All @@ -37,23 +33,13 @@ spec =
Just (Close{}) -> transition === Transition.Close
Just (Contest{}) -> transition === Transition.Contest
Just (Fanout{}) -> transition === Transition.Fanout
-- TODO! add Recover
_ -> property False

prop "Updates UTxO state given transaction part of Head lifecycle" $
forAllBlind genChainStateWithTx $ \(_ctx, st, additionalUTxO, tx, _transition) ->
let utxo = getKnownUTxO st <> additionalUTxO
in fst (observeTx testNetworkId mockSerialisedScriptRegistry utxo tx) =/= utxo
in fst (observeTx testNetworkId serialisedScriptRegistry utxo tx) =/= utxo

prop "Does not updates UTxO state given transactions outside of Head lifecycle" $
forAll genSequenceOfSimplePaymentTransactions $ \(utxo, txs) ->
fst (observeAll testNetworkId mockSerialisedScriptRegistry utxo txs) === utxo

mockSerialisedScriptRegistry :: SerialisedScriptRegistry
mockSerialisedScriptRegistry =
SerialisedScriptRegistry
{ initialScriptValidator = mkValidatorScript "590a6501010033232323232323232323223225333005323232323253323300b3001300d37540042646464a66666602c00c26464646464a66602660080022a66602e602c6ea802c5400804854ccc04cc0240044c94ccc06000404c4c94cccccc0740040500504c94ccc068c07400c4cc020004894ccc0700085401c4c94cccccc0840044ccc0240044c008c08000c060060060060060c078008054dd600080a00a180d000980b1baa00b012301437540142a666022600460266ea80104c8c8c8c8c8c8c94ccc060c024c068dd5008099802248103493031003370e6660026eacc018c06cdd5009003a4410b487964726148656164563100480044cc8c8c8c8c8c8c8c8c8c8c8c8c8c8c88c8c8c8c8c94cccccc0d40040080084c8cc080004894ccc0d00084c8c94ccc0c8cc07924103493133003371e0426eb8c058c0d4dd50040a999819191919299981a981300089811249034930350015333035302b001132533303a0011302349103493036001533303a303d001133022491034930320032323300100100622533303d00114a0264a66607466e3cdd718200010020a511330030030013040001375c60780022604692010349303600323300100100322533303b00114bd700991919299981d18181bad303d00313303f37520026600a00a00426600a00a0046eb8c0ec008c0fc008c0f40044c08924010349303600301a001330183301d0020224bd6f7b6301bac303830393039303930393039303930393039303537540582a6660646603c92103493134003375e6040606a6ea80b1300101a000153330323301e49103493033003371266e00c048004c048ccc050cc04c00894ccc0ccc090c0d4dd500089bab3010303637546020606c6ea8c0e4c0d8dd50008a5eb7bdb1812f5bded8c002a60246660286602601446eacc040c0d8dd5000a5eb7bdb1800544cc0300080145280a5014a02940cc058c94ccc0c8c0a0c0d0dd50008a60103d87a80001301d33037300f30353754601e606a6ea8c0e0c0d4dd5000a5eb80cc034dd6180c981a1baa02b0254bd6f7b6301980780491929998191811981a1baa0011301d3303730383035375400297ae013010490103493135003300d3758603260686ea80ac0044c94cccccc0e400454ccc0c4c088c0ccdd5000899299981b000803899299999981d800899299981c000804899299999981e800805005005005099299981d181e801899981300209803981e8040058059bae001303a001303a00200800800800830380013034375400200c00c00c00c00c606c0046eb0004008008c024c0bcdd50010980c2491f4661696c656420746f206465636f6465206c6f636b65645f636f6d6d69747300533302e0011300849103493132001533302e303100113232533302c301d0011300a49103493039001533302c30220011300a490103493130001323253333330360021533302e301f30303754004264a66606600200426464a66606a00200826464a66606e00200c264a66666607800200e00e00e00e264a666072607800620120106eb8004c0e4004c0e4008c0dc004c0dc008c0d4004c0c4dd500100080080080080089805a4903493131003032302f3754004605a6ea8004c0c00044c02124010349313200330080012300e302d375400264660020026eb0c034c0b0dd50119129998170008a5eb804c8c94ccc0b0c94ccc0b4c08cc0bcdd5000899b8f02a375c606660606ea8004528180a18179baa3014302f37540042660620046600800800226600800800260640046060002600200244464646464a6660600042a666060006294400400454ccc0bc0044c06124103493037001533302f0021301849010349303800132323232533302f30203031375400826466038921034930340053330303371e6eccc034c0ccdd50009bae300d303337540062a66606066ebcc060c0ccdd5000980c18199baa003133300b00b00400214a02940c0d4c0c8dd50020a503035005303300430330023031001375860600046eb0c0bcc0c0004cc0b4dd3801198169ba70014bd701119198008008019129998160008a6103d87a800013232533302a3375e6024605a6ea80080144c054cc0bc0092f5c02660080080026060004605c0024605460560024a660480022c44646600200200644a666052002297ae013302a3003302b00133002002302c001233300a00148810048810022323300100100322533302700114bd700998141ba63003302900133002002302a00122232333001001004003222533302800210011333003003302b00233004001375660540044464666002002006004444a66604c004200226466600800860540066644646600200200a44a66605600226605866ec0dd48021ba60034bd6f7b630099191919299981599b90008002133030337606ea4020dd30038028a99981599b8f008002132533302c301d302e375400226606266ec0dd4804981918179baa001004100432533302c533303000114a22940530103d87a80001301733031374c00297ae03233300100100800222253330320021001132333004004303600333223233001001005225333037001133038337606ea4010dd4001a5eb7bdb1804c8c8c8c94ccc0dccdc800400109981e19bb037520106ea001c01454ccc0dccdc7804001099299981c1814981d1baa00113303d337606ea4024c0f8c0ecdd5000802080219299981c18148008a60103d87a8000130233303d375000297ae03370000e00226607866ec0dd48011ba800133006006003375a60720066eb8c0dc008c0ec008c0e4004dd718188009bad30320013034002133030337606ea4008dd3000998030030019bab302d003375c6056004605e004605a0026eb8c094004dd598130009814001118119812181200091299980e9809980f9baa002100113756604660406ea8008c004004894ccc07c0045200013370090011980100118110009180f8009bac301e301b375402044464a6660366022603a6ea8004520001375a6042603c6ea8004c94ccc06cc044c074dd50008a6103d87a80001323300100137566044603e6ea8008894ccc084004530103d87a80001323232325333021337220100042a66604266e3c0200084c030cc098dd4000a5eb80530103d87a8000133006006003375a60460066eb8c084008c094008c08c004cc01000c00888c8cc00400400c894ccc078004530103d87a8000132323232533301e3372200e0042a66603c66e3c01c0084c024cc08cdd3000a5eb80530103d87a8000133006006003375660400066eb8c078008c088008c080004dd2a400044a66602c00229444c00c00894cc054004588c064c068c068c068c068004dd7180b980a1baa0041533012491054c36323b3500162225333013300430153754006264a666030002004264a66666603a00200600600600626464a66603600200a264a66666604000200c00c00c264a66603a60400062a01000e6eb4004018c074004c07400cdd7000980d000980b1baa003001370e9000111919800800801911980180098010010068068068069809980a001180900098071baa002370e90010b1807980800118070009807001180600098041baa00114984d958dd70008a998012481054c35383b3500165734ae7155ceaab9e5573eae815d0aba257489811e581c3e5a776bcee213e3dfd15806952a10ac5590e3e97d09d62eb99266b20001"
, commitScriptValidator = mkValidatorScript "5902ac010100323232323232323232322533300332323232325332330093001300b37540042646464a66666602800c2646464a66601e60060022a66602660246ea80245400803854ccc03cc01c00454ccc04cc048dd50048a80100700718081baa0081533300d3001300f37540042646464646464a6660266016602a6ea80344cc00cc01130103d87980003370e6660026eacc064c068c068c068c068c058dd50079bae30053016375400c91010b487964726148656164563100480044c94ccc050c020c058dd50008998021802a6103d87a8000300c333002375660346036602e6ea8c068c05cdd50009bae30063017375400e9110b4879647261486561645631001533015491054c35373b39001632533301800114c103d87a80001300333019301a0014bd701bac30053016375401e44464a66602c601c60306ea8004520001375a603860326ea8004c94ccc058c038c060dd50008a6103d87a8000132330010013756603a60346ea8008894ccc070004530103d87a8000132323232533301c337220100042a66603866e3c0200084c02ccc084dd4000a5eb80530103d87a8000133006006003375a603c0066eb8c070008c080008c078004c8cc004004010894ccc06c0045300103d87a8000132323232533301b337220100042a66603666e3c0200084c028cc080dd3000a5eb80530103d87a80001330060060033756603a0066eb8c06c008c07c008c074004dd2a400044a666024002294454cc04c0085894ccc040c010c048dd50008a4903433031001491034330320023015301630160013013301037540042a6601c9201054c34373b350016370e900000580580580598089809001180800098061baa002370e90010b1806980700118060009806001180500098031baa00114984d95854cc0092401054c34333b3500165734ae7155ceaab9e5573eae815d0aba257481"
, headScriptValidator = headScriptValidator serialisedScriptRegistry
, depositScriptValidator = depositScriptValidator serialisedScriptRegistry
}
fst (observeAll testNetworkId serialisedScriptRegistry utxo txs) === utxo
12 changes: 7 additions & 5 deletions hydra-tx/hydra-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ source-repository head
common project-config
default-language: GHC2021
default-extensions:
NoImplicitPrelude
DataKinds
DefaultSignatures
DeriveAnyClass
Expand All @@ -27,6 +26,7 @@ common project-config
GADTs
LambdaCase
MultiWayIf
NoImplicitPrelude
OverloadedStrings
PartialTypeSignatures
PatternSynonyms
Expand Down Expand Up @@ -95,7 +95,6 @@ library
, hydra-plutus-extras
, hydra-prelude
, lens
, lens-aeson
, ouroboros-consensus
, ouroboros-consensus-cardano
, plutus-core
Expand Down Expand Up @@ -125,7 +124,8 @@ library testlib
, cardano-crypto-class
, cardano-ledger-alonzo
, cardano-ledger-api
, cardano-ledger-conway:{cardano-ledger-conway, testlib}
, cardano-ledger-conway
, cardano-ledger-conway:testlib
, cardano-ledger-core
, cardano-ledger-mary
, cardano-ledger-shelley
Expand Down Expand Up @@ -190,9 +190,11 @@ test-suite tests
, hydra-plutus-extras
, hydra-prelude
, hydra-test-utils
, hydra-tx:{hydra-tx, testlib}
, hydra-tx
, hydra-tx:testlib
, lens
, plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib}
, plutus-ledger-api
, plutus-ledger-api:plutus-ledger-api-testlib
, plutus-tx
, QuickCheck
, quickcheck-instances
Expand Down
32 changes: 10 additions & 22 deletions hydra-tx/src/Hydra/Tx/ScriptRegistry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,18 @@ module Hydra.Tx.ScriptRegistry where

import Hydra.Prelude

import Cardano.Api.UTxO (UTxO)
import Cardano.Api.UTxO qualified as UTxO
import Codec.CBOR.Read qualified as CBOR
import Control.Lens ((^.))
import Data.Aeson qualified as Aeson
import Data.Aeson.Lens (key, nth, _String)
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Lazy qualified as LBS
import Data.ByteString.Short qualified as SBS
import Data.Map qualified as Map
import Hydra.Cardano.Api
import Hydra.Cardano.Api (
CtxUTxO,
ScriptHash,
TxIn (..),
TxOut,
hashScriptInAnyLang,
txOutReferenceScript,
pattern ReferenceScript,
pattern ReferenceScriptNone,
)
import Hydra.Contract (ScriptInfo (..), scriptInfo)
import Hydra.Contract.Deposit qualified as Deposit
import Hydra.Contract.Head qualified as Head
import Hydra.Plutus (commitValidatorScript, initialValidatorScript)
import PlutusLedgerApi.Common (MajorProtocolVersion (..), ScriptDecodeError, SerialisedScript)
import PlutusLedgerApi.V3 qualified as PlutusV3
import PlutusLedgerApi.Common (SerialisedScript)

-- | Hydra scripts published as reference scripts at these UTxO.
data ScriptRegistry = ScriptRegistry
Expand Down Expand Up @@ -101,19 +87,21 @@ serialisedScriptRegistry =
, depositScriptValidator = Deposit.validatorScript
}

mkValidatorScript :: Text -> SerialisedScript
mkValidatorScript base16Text =
-- XXX: used to parse Aiken `compiledCode`.
serialisedScriptFromText :: Text -> SerialisedScript
serialisedScriptFromText base16Text =
case Base16.decode base16Bytes of
Left e -> error $ "Failed to decode initial validator: " <> show e
Right bytes -> toShort bytes
where
base16Bytes = encodeUtf8 base16Text

mkValidatorScriptV2 :: ByteString -> Either String SerialisedScript
mkValidatorScriptV2 cborHex = do
-- XXX: used to parse Plutus `cborHex`.
cborHexToSerialisedScript :: ByteString -> SerialisedScript
cborHexToSerialisedScript cborHex = either (error . show) SBS.toShort $ do
bytes <- Base16.decode cborHex
r <- CBOR.deserialiseFromBytes @ByteString bytes
pure $ SBS.toShort r
(_, a) <- first show $ CBOR.deserialiseFromBytes @ByteString fromCBOR (LBS.fromStrict bytes)
pure a

-- | Get the UTxO that corresponds to a script registry.
--
Expand Down

0 comments on commit 60cf58f

Please sign in to comment.