Skip to content

Commit

Permalink
Finalize schnorkel test case
Browse files Browse the repository at this point in the history
Still getting PPViewHashesDontMatch but this is the final error to solve
  • Loading branch information
v0d1ch committed Dec 24, 2024
1 parent d6ff585 commit 7f70541
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 32 deletions.
4 changes: 0 additions & 4 deletions hydra-cluster/hydra-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ library
, base >=4.7 && <5
, bytestring
, cardano-slotting
, cardano-api
, containers
, contra-tracer
, data-default
Expand All @@ -99,7 +98,6 @@ library
, hydra-cardano-api
, hydra-node
, hydra-prelude
, hydra-plutus
, hydra-test-utils
, hydra-tx
, hydra-tx:testlib
Expand All @@ -108,8 +106,6 @@ library
, lens
, lens-aeson
, optparse-applicative
, plutus-ledger-api
, plutus-tx
, process
, QuickCheck
, req
Expand Down
106 changes: 84 additions & 22 deletions hydra-cluster/src/Hydra/Cluster/Scenarios.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ module Hydra.Cluster.Scenarios where
import Hydra.Prelude
import Test.Hydra.Prelude

import qualified Cardano.Api.Shelley as C
import qualified PlutusTx.Prelude as PlutusTx
import qualified PlutusLedgerApi.V1 as PV1
import Cardano.Api.UTxO qualified as UTxO
import CardanoClient (
QueryPoint (QueryTip),
RunningNode (..),
buildTransaction,
queryProtocolParameters,
queryTip,
queryUTxOFor,
submitTx,
waitForUTxO,
)
import Hydra.Contract.Dummy (dummyValidatorHash)
import CardanoNode (NodeLog)
import Control.Concurrent.Async (mapConcurrently_)
import Control.Lens ((^..), (^?))
Expand All @@ -41,12 +38,9 @@ import Hydra.Cardano.Api (
Coin (..),
File (File),
Key (SigningKey),
KeyWitnessInCtx (KeyWitnessForSpending),
PaymentKey,
StakeAddressReference(..),
PaymentCredential(..),
Tx,
shelleyBasedEra,
makeShelleyAddressInEra,
TxId,
UTxO,
getTxBody,
Expand All @@ -57,19 +51,23 @@ import Hydra.Cardano.Api (
mkScriptAddress,
mkScriptDatum,
mkScriptWitness,
mkTxOutAutoBalance,
mkTxOutDatumHash,
mkVkAddress,
scriptWitnessInCtx,
selectLovelace,
setTxFee,
signTx,
toScriptData,
txOutValue,
utxoFromTx,
writeFileTextEnvelope,
pattern BuildTxWith,
pattern KeyWitness,
pattern PlutusScriptSerialised,
pattern ReferenceScriptNone,
pattern ScriptWitness,
pattern TxFeeExplicit,
pattern TxOut,
pattern TxOutDatumNone,
)
Expand All @@ -79,7 +77,7 @@ import Hydra.Cluster.Fixture (Actor (..), actorName, alice, aliceSk, aliceVk, bo
import Hydra.Cluster.Mithril (MithrilLog)
import Hydra.Cluster.Options (Options)
import Hydra.Cluster.Util (chainConfigFor, keysFor, modifyConfig, setNetworkId)
import Hydra.Ledger.Cardano (addInputs, emptyTxBody, mkSimpleTx, mkTransferTx, unsafeBuildTransaction)
import Hydra.Ledger.Cardano (addCollateralInput, addInputs, addOutputs, changePParams, emptyTxBody, mkSimpleTx, mkTransferTx, unsafeBuildTransaction)
import Hydra.Logging (Tracer, traceWith)
import Hydra.Options (DirectChainConfig (..), networkId, startChainFrom)
import Hydra.Tx (HeadId, IsTx (balance), Party, txId)
Expand Down Expand Up @@ -405,29 +403,93 @@ singlePartyUsesSchnorrkelScriptOnL2 tracer workDir node hydraScriptsTxId =
withHydraNode hydraTracer aliceChainConfig workDir hydraNodeId aliceSk [] [1] $ \n1 -> do
send n1 $ input "Init" []
headId <- waitMatch (10 * blockTime) n1 $ headIsInitializingWith (Set.fromList [alice])

requestCommitTx n1 utxoToCommit <&> signTx walletSk >>= submitTx node
waitFor hydraTracer (10 * blockTime) [n1] $
output "HeadIsOpen" ["utxo" .= toJSON utxoToCommit, "headId" .= headId]
scriptHash <- unTransScriptHash dummyValidatorHash
let scriptAddress = makeShelleyAddressInEra shelleyBasedEra networkId (PaymentCredentialByScript scriptHash) NoStakeAddress
let i = undefined
let o = undefined
let tx = mkSimpleTx (i, o) (scriptAddress, txOutValue o) walletSk
send n1 $ input "NewTx" ["transaction" .= tx]
(clientPayload, scriptUTxO) <- prepareScriptPayload
res <-
runReq defaultHttpConfig $
req
POST
(http "127.0.0.1" /: "commit")
(ReqBodyJson clientPayload)
(Proxy :: Proxy (JsonResponse Tx))
(port $ 4000 + hydraNodeId)

let commitTx = responseBody res
submitTx node commitTx

depositTxId <- waitMatch (10 * blockTime) n1 $ \v -> do
guard $ v ^? key "headId" == Just (toJSON headId)
guard $ v ^? key "tag" == Just "CommitFinalized"
pure $ v ^? key "theDeposit"
depositTxId `shouldBe` Just (toJSON $ getTxId $ getTxBody commitTx)
let (collateralInput, _) = List.head $ UTxO.pairs utxoToCommit

let (scriptInput, _) = List.head $ UTxO.pairs scriptUTxO
let (normalInput', _) = List.head $ UTxO.pairs utxoToCommit
pparams <- queryProtocolParameters networkId nodeSocket QueryTip

let serializedScript = PlutusScriptSerialised dummyValidatorScript
let scriptAddress = mkScriptAddress networkId serializedScript
let scriptOutput =
mkTxOutAutoBalance
pparams
scriptAddress
(lovelaceToValue 0)
(mkTxOutDatumHash ())
ReferenceScriptNone
let returnOutput =
TxOut (mkVkAddress networkId walletVk) (lovelaceToValue 4_826_535) TxOutDatumNone ReferenceScriptNone
let normalInput = (,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> [normalInput']
let scriptWitness =
BuildTxWith $
ScriptWitness scriptWitnessInCtx $
mkScriptWitness serializedScript (mkScriptDatum ()) (toScriptData ())
let tx =
unsafeBuildTransaction $
emptyTxBody
& changePParams pparams
& addInputs ([(scriptInput, scriptWitness)] <> normalInput)
& addCollateralInput collateralInput
& addOutputs [scriptOutput, returnOutput]
& setTxFee (TxFeeExplicit $ Coin 173_465)
let signedL2tx = signTx walletSk tx
send n1 $ input "NewTx" ["transaction" .= signedL2tx]

waitMatch 10 n1 $ \v -> do
guard $ v ^? key "tag" == Just "SnapshotConfirmed"
guard $
toJSON tx
`elem` (v ^.. key "snapshot" . key "confirmed" . values)
v ^? key "snapshot" . key "utxo" >>= parseMaybe parseJSON
where
RunningNode{networkId, nodeSocket, blockTime} = node
unTransScriptHash :: PV1.ScriptHash -> IO C.ScriptHash
unTransScriptHash (PV1.ScriptHash vh) =
case C.deserialiseFromRawBytes C.AsScriptHash $ PlutusTx.fromBuiltin vh of
Left e -> fail $ show e
Right x -> pure x
where
RunningNode{networkId, nodeSocket, blockTime} = node
-- TODO: extract this to standalone function
prepareScriptPayload = do
let script = dummyValidatorScript
let serializedScript = PlutusScriptSerialised script
let scriptAddress = mkScriptAddress networkId serializedScript
let datumHash = mkTxOutDatumHash ()
(scriptIn, scriptOut) <- createOutputAtAddress node scriptAddress datumHash (lovelaceToValue 0)
let scriptUTxO = UTxO.singleton (scriptIn, scriptOut)

let scriptWitness =
BuildTxWith $
ScriptWitness scriptWitnessInCtx $
mkScriptWitness serializedScript (mkScriptDatum ()) (toScriptData ())
let spendingTx =
unsafeBuildTransaction $
emptyTxBody
& addInputs [(scriptIn, scriptWitness)]
pure
( Aeson.object
[ "blueprintTx" .= spendingTx
, "utxo" .= scriptUTxO
]
, scriptUTxO
)

singlePartyCommitsScriptBlueprint ::
Tracer IO EndToEndLog ->
Expand Down
7 changes: 1 addition & 6 deletions hydra-node/src/Hydra/Chain/Direct/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import Cardano.Ledger.Shelley.API qualified as Ledger
import Cardano.Ledger.Val (invert)
import Cardano.Slotting.EpochInfo (EpochInfo)
import Cardano.Slotting.Time (SystemStart (..))
import Control.Concurrent.Class.MonadSTM (check, newTVarIO, readTVarIO, writeTVar)
import Control.Concurrent.Class.MonadSTM (newTVarIO, readTVarIO, writeTVar)
import Control.Lens (view, (%~), (.~), (^.))
import Data.List qualified as List
import Data.Map.Strict ((!))
Expand Down Expand Up @@ -144,11 +144,6 @@ data WalletInfoOnChain = WalletInfoOnChain

type ChainQuery m = QueryPoint -> Api.Address ShelleyAddr -> m WalletInfoOnChain

watchUTxOUntil :: (Map TxIn TxOut -> Bool) -> TinyWallet IO -> IO (Map TxIn TxOut)
watchUTxOUntil predicate TinyWallet{getUTxO} = atomically $ do
u <- getUTxO
u <$ check (predicate u)

-- | Create a new tiny wallet handle.
newTinyWallet ::
-- | A tracer for logging
Expand Down
9 changes: 9 additions & 0 deletions hydra-tx/src/Hydra/Ledger/Cardano/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ addInputs :: TxIns BuildTx -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addInputs ins tx =
tx{txIns = txIns tx <> ins}

-- | Add new inputs to an ongoing builder.
addCollateralInput :: TxIn -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addCollateralInput txin tx =
tx{txInsCollateral = TxInsCollateral [txin]}

changePParams :: PParams (ShelleyLedgerEra Era) -> TxBodyContent BuildTx -> TxBodyContent BuildTx
changePParams pparams tx =
tx{txProtocolParams = BuildTxWith $ Just $ LedgerProtocolParameters pparams}

addReferenceInputs :: [TxIn] -> TxBodyContent BuildTx -> TxBodyContent BuildTx
addReferenceInputs refs' tx =
tx
Expand Down

0 comments on commit 7f70541

Please sign in to comment.