Skip to content

Commit 28e24cd

Browse files
committed
Almost there; insufficient collateral now
1 parent 5dae6c3 commit 28e24cd

File tree

1 file changed

+82
-70
lines changed

1 file changed

+82
-70
lines changed

hydra-cluster/src/Hydra/Cluster/Scenarios.hs

Lines changed: 82 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -397,86 +397,98 @@ singlePartyUsesSchnorrkelScriptOnL2 ::
397397
[TxId] ->
398398
IO ()
399399
singlePartyUsesSchnorrkelScriptOnL2 tracer workDir node hydraScriptsTxId =
400-
(`finally` returnFundsToFaucet tracer node Alice) $ do
401-
refuelIfNeeded tracer node Alice 20_000_000
402-
aliceChainConfig <- chainConfigFor Alice workDir nodeSocket hydraScriptsTxId [] $ UnsafeContestationPeriod 100
403-
let hydraNodeId = 1
404-
let hydraTracer = contramap FromHydraNode tracer
405-
(walletVk, walletSk) <- keysFor AliceFunds
406-
utxoToCommit <- seedFromFaucet node walletVk 5_000_000 (contramap FromFaucet tracer)
407-
withHydraNode hydraTracer aliceChainConfig workDir hydraNodeId aliceSk [] [1] $ \n1 -> do
408-
send n1 $ input "Init" []
409-
headId <- waitMatch (10 * blockTime) n1 $ headIsInitializingWith (Set.fromList [alice])
400+
( `finally`
401+
do
402+
returnFundsToFaucet tracer node Alice
403+
returnFundsToFaucet tracer node AliceFunds
404+
)
405+
$ do
406+
refuelIfNeeded tracer node Alice 25_000_000
407+
aliceChainConfig <- chainConfigFor Alice workDir nodeSocket hydraScriptsTxId [] $ UnsafeContestationPeriod 100
408+
let hydraNodeId = 1
409+
let hydraTracer = contramap FromHydraNode tracer
410+
withHydraNode hydraTracer aliceChainConfig workDir hydraNodeId aliceSk [] [1] $ \n1 -> do
411+
send n1 $ input "Init" []
412+
headId <- waitMatch (10 * blockTime) n1 $ headIsInitializingWith (Set.fromList [alice])
410413

411-
requestCommitTx n1 utxoToCommit <&> signTx walletSk >>= submitTx node
412-
waitFor hydraTracer (10 * blockTime) [n1] $
413-
output "HeadIsOpen" ["utxo" .= toJSON utxoToCommit, "headId" .= headId]
414-
(clientPayload, scriptUTxO) <- prepareScriptPayload
415-
res <-
416-
runReq defaultHttpConfig $
417-
req
418-
POST
419-
(http "127.0.0.1" /: "commit")
420-
(ReqBodyJson clientPayload)
421-
(Proxy :: Proxy (JsonResponse Tx))
422-
(port $ 4000 + hydraNodeId)
414+
-- Commit nothing just to open
415+
requestCommitTx n1 mempty >>= submitTx node
416+
waitFor hydraTracer (10 * blockTime) [n1] $
417+
output "HeadIsOpen" ["utxo" .= object mempty, "headId" .= headId]
423418

424-
let commitTx = responseBody res
425-
submitTx node commitTx
419+
-- Then commit a Tx with the schnorrkel validator
420+
(walletVk, walletSk) <- keysFor AliceFunds
426421

427-
depositTxId <- waitMatch (10 * blockTime) n1 $ \v -> do
428-
guard $ v ^? key "headId" == Just (toJSON headId)
429-
guard $ v ^? key "tag" == Just "CommitFinalized"
430-
pure $ v ^? key "theDeposit"
431-
depositTxId `shouldBe` Just (toJSON $ getTxId $ getTxBody commitTx)
432-
let (collateralInput, _) = List.head $ UTxO.pairs utxoToCommit
433-
434-
let (scriptInput, _) = List.head $ UTxO.pairs scriptUTxO
435-
let (normalInput', _) = List.head $ UTxO.pairs utxoToCommit
436-
pparams <- queryProtocolParameters networkId nodeSocket QueryTip
437-
438-
let serializedScript = PlutusScriptSerialised dummyValidatorScript
439-
let scriptAddress = mkScriptAddress networkId serializedScript
440-
let scriptOutput =
441-
mkTxOutAutoBalance
442-
pparams
443-
scriptAddress
444-
(lovelaceToValue 0)
445-
(mkTxOutDatumHash ())
446-
ReferenceScriptNone
447-
let returnOutput =
448-
TxOut (mkVkAddress networkId walletVk) (lovelaceToValue 4_826_535) TxOutDatumNone ReferenceScriptNone
449-
let normalInput = (,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> [normalInput']
450-
let scriptWitness =
451-
BuildTxWith $
452-
ScriptWitness scriptWitnessInCtx $
453-
mkScriptWitness serializedScript (mkScriptDatum ()) (toScriptData ())
454-
let tx =
455-
unsafeBuildTransaction $
456-
defaultTxBodyContent
457-
& changePParams pparams
458-
& addTxIns ([(scriptInput, scriptWitness)] <> normalInput)
459-
& addTxInsCollateral [collateralInput]
460-
& addTxOuts [scriptOutput, returnOutput]
461-
& setTxFee (TxFeeExplicit $ Coin 173_465)
462-
let signedL2tx = signTx walletSk tx
463-
send n1 $ input "NewTx" ["transaction" .= signedL2tx]
464-
465-
waitMatch 10 n1 $ \v -> do
466-
guard $ v ^? key "tag" == Just "SnapshotConfirmed"
467-
guard $
468-
toJSON tx
469-
`elem` (v ^.. key "snapshot" . key "confirmed" . values)
470-
v ^? key "snapshot" . key "utxo" >>= parseMaybe parseJSON
422+
let amt = 5_000_000
423+
424+
utxoToCommit <- seedFromFaucet node walletVk amt (contramap FromFaucet tracer)
425+
426+
(clientPayload, scriptUTxO) <- prepareScriptPayload amt
427+
res <-
428+
runReq defaultHttpConfig $
429+
req
430+
POST
431+
(http "127.0.0.1" /: "commit")
432+
(ReqBodyJson clientPayload)
433+
(Proxy :: Proxy (JsonResponse Tx))
434+
(port $ 4000 + hydraNodeId)
435+
436+
let commitTx = responseBody res
437+
submitTx node commitTx
438+
439+
depositTxId <- waitMatch (10 * blockTime) n1 $ \v -> do
440+
guard $ v ^? key "headId" == Just (toJSON headId)
441+
guard $ v ^? key "tag" == Just "CommitFinalized"
442+
pure $ v ^? key "theDeposit"
443+
depositTxId `shouldBe` Just (toJSON $ getTxId $ getTxBody commitTx)
444+
let (collateralInput, _) = List.head $ UTxO.pairs utxoToCommit
445+
446+
let (scriptInput, _) = List.head $ UTxO.pairs scriptUTxO
447+
let (normalInput', _) = List.head $ UTxO.pairs utxoToCommit
448+
pparams <- queryProtocolParameters networkId nodeSocket QueryTip
449+
450+
let serializedScript = PlutusScriptSerialised dummyValidatorScript
451+
let scriptAddress = mkScriptAddress networkId serializedScript
452+
let scriptOutput =
453+
mkTxOutAutoBalance
454+
pparams
455+
scriptAddress
456+
(lovelaceToValue 0)
457+
(mkTxOutDatumHash ())
458+
ReferenceScriptNone
459+
let returnOutput =
460+
TxOut (mkVkAddress networkId walletVk) (lovelaceToValue 4_826_535) TxOutDatumNone ReferenceScriptNone
461+
let normalInput = (,BuildTxWith $ KeyWitness KeyWitnessForSpending) <$> [normalInput']
462+
let scriptWitness =
463+
BuildTxWith $
464+
ScriptWitness scriptWitnessInCtx $
465+
mkScriptWitness serializedScript (mkScriptDatum ()) (toScriptData ())
466+
let tx =
467+
unsafeBuildTransaction $
468+
defaultTxBodyContent
469+
& changePParams pparams
470+
& addTxIns ([(scriptInput, scriptWitness)] <> normalInput)
471+
& addTxInsCollateral [collateralInput]
472+
& addTxOuts [scriptOutput, returnOutput]
473+
& setTxFee (TxFeeExplicit $ Coin 173_465)
474+
let signedL2tx = signTx walletSk tx
475+
send n1 $ input "NewTx" ["transaction" .= signedL2tx]
476+
477+
waitMatch 10 n1 $ \v -> do
478+
guard $ v ^? key "tag" == Just "SnapshotConfirmed"
479+
guard $
480+
toJSON tx
481+
`elem` (v ^.. key "snapshot" . key "confirmed" . values)
482+
v ^? key "snapshot" . key "utxo" >>= parseMaybe parseJSON
471483
where
472484
RunningNode{networkId, nodeSocket, blockTime} = node
473485
-- TODO: extract this to standalone function
474-
prepareScriptPayload = do
486+
prepareScriptPayload lovelaceAmt = do
475487
let script = dummyValidatorScript
476488
let serializedScript = PlutusScriptSerialised script
477489
let scriptAddress = mkScriptAddress networkId serializedScript
478490
let datumHash = mkTxOutDatumHash ()
479-
(scriptIn, scriptOut) <- createOutputAtAddress node scriptAddress datumHash (lovelaceToValue 1_000_000)
491+
(scriptIn, scriptOut) <- createOutputAtAddress node scriptAddress datumHash (lovelaceToValue lovelaceAmt)
480492
let scriptUTxO = UTxO.singleton (scriptIn, scriptOut)
481493

482494
let scriptWitness =

0 commit comments

Comments
 (0)