@@ -103,6 +103,7 @@ import Hydra.Cluster.Mithril (MithrilLog)
103
103
import Hydra.Cluster.Options (Options )
104
104
import Hydra.Cluster.Util (chainConfigFor , keysFor , modifyConfig , setNetworkId )
105
105
import Hydra.Contract.Dummy (dummyRewardingScript )
106
+ import Hydra.Contract.Sha512Example qualified as Sha512
106
107
import Hydra.Ledger.Cardano (mkSimpleTx , mkTransferTx , unsafeBuildTransaction )
107
108
import Hydra.Ledger.Cardano.Evaluate (maxTxExecutionUnits )
108
109
import Hydra.Logging (Tracer , traceWith )
@@ -759,6 +760,127 @@ singlePartyUsesWithdrawZeroTrick tracer workDir node hydraScriptsTxId =
759
760
where
760
761
RunningNode {networkId, nodeSocket, blockTime} = node
761
762
763
+ singlePartyUsesSha512ScriptOnL2 ::
764
+ Tracer IO EndToEndLog ->
765
+ FilePath ->
766
+ RunningNode ->
767
+ [TxId ] ->
768
+ IO ()
769
+ singlePartyUsesSha512ScriptOnL2 tracer workDir node hydraScriptsTxId =
770
+ ( `finally`
771
+ do
772
+ returnFundsToFaucet tracer node Alice
773
+ returnFundsToFaucet tracer node AliceFunds
774
+ )
775
+ $ do
776
+ refuelIfNeeded tracer node Alice 250_000_000
777
+ let contestationPeriod = UnsafeContestationPeriod 1
778
+ let depositDeadline = UnsafeDepositDeadline 1
779
+ aliceChainConfig <- chainConfigFor Alice workDir nodeSocket hydraScriptsTxId [] contestationPeriod depositDeadline
780
+ let hydraNodeId = 1
781
+ let hydraTracer = contramap FromHydraNode tracer
782
+ withHydraNode hydraTracer aliceChainConfig workDir hydraNodeId aliceSk [] [1 ] $ \ n1 -> do
783
+ send n1 $ input " Init" []
784
+ headId <- waitMatch (10 * blockTime) n1 $ headIsInitializingWith (Set. fromList [alice])
785
+
786
+ (walletVk, walletSk) <- keysFor AliceFunds
787
+
788
+ -- Create money on L1
789
+ let commitAmount = 100_000_000
790
+ utxoToCommit <- seedFromFaucet node walletVk commitAmount (contramap FromFaucet tracer)
791
+
792
+ -- Push it into L2
793
+ requestCommitTx n1 utxoToCommit
794
+ <&> signTx walletSk >>= \ tx -> do
795
+ submitTx node tx
796
+
797
+ -- Check UTxO is present in L2
798
+ waitFor hydraTracer (10 * blockTime) [n1] $
799
+ output " HeadIsOpen" [" utxo" .= toJSON utxoToCommit, " headId" .= headId]
800
+
801
+ pparams <- getProtocolParameters n1
802
+
803
+ -- Send the UTxO to a script; in preparation for running the script
804
+ let serializedScript = Sha512. dummyValidatorScript
805
+ let scriptAddress = mkScriptAddress networkId serializedScript
806
+ let scriptOutput =
807
+ mkTxOutAutoBalance
808
+ pparams
809
+ scriptAddress
810
+ (lovelaceToValue 0 )
811
+ (mkTxOutDatumHash () )
812
+ ReferenceScriptNone
813
+
814
+ Right tx <- buildTransactionWithPParams pparams networkId nodeSocket (mkVkAddress networkId walletVk) utxoToCommit [] [scriptOutput]
815
+
816
+ let signedL2tx = signTx walletSk tx
817
+ send n1 $ input " NewTx" [" transaction" .= signedL2tx]
818
+
819
+ waitMatch 10 n1 $ \ v -> do
820
+ guard $ v ^? key " tag" == Just " SnapshotConfirmed"
821
+ guard $
822
+ toJSON signedL2tx
823
+ `elem` (v ^.. key " snapshot" . key " confirmed" . values)
824
+
825
+ -- Now, spend the money from the script
826
+ let scriptWitness =
827
+ BuildTxWith $
828
+ ScriptWitness scriptWitnessInCtx $
829
+ PlutusScriptWitness
830
+ serializedScript
831
+ (mkScriptDatum () )
832
+ (toScriptData () )
833
+ maxTxExecutionUnits
834
+
835
+ let txIn = mkTxIn signedL2tx 0
836
+ let remainder = mkTxIn signedL2tx 1
837
+
838
+ let outAmt = foldMap txOutValue (txOuts' tx)
839
+ let body =
840
+ defaultTxBodyContent
841
+ & addTxIns [(txIn, scriptWitness), (remainder, BuildTxWith $ KeyWitness KeyWitnessForSpending )]
842
+ & addTxInsCollateral [remainder]
843
+ & addTxOuts [TxOut (mkVkAddress networkId walletVk) outAmt TxOutDatumNone ReferenceScriptNone ]
844
+ & setTxProtocolParams (BuildTxWith $ Just $ LedgerProtocolParameters pparams)
845
+
846
+ -- TODO: Instead of using `createAndValidateTransactionBody`, we
847
+ -- should be able to just construct the Tx with autobalancing via
848
+ -- `buildTransactionWithBody`. Unfortunately this is broken in the
849
+ -- version of cardano-api that we presently use; in a future upgrade
850
+ -- of that library we can try again.
851
+ -- tx' <- either (failure . show) pure =<< buildTransactionWithBody networkId nodeSocket (mkVkAddress networkId walletVk) body utxoToCommit
852
+ txBody <- either (failure . show ) pure (createAndValidateTransactionBody body)
853
+
854
+ let spendTx' = makeSignedTransaction [] txBody
855
+ spendTx = fromLedgerTx $ recomputeIntegrityHash pparams [PlutusV3 ] (toLedgerTx spendTx')
856
+ let signedTx = signTx walletSk spendTx
857
+
858
+ send n1 $ input " NewTx" [" transaction" .= signedTx]
859
+
860
+ waitMatch 10 n1 $ \ v -> do
861
+ guard $ v ^? key " tag" == Just " SnapshotConfirmed"
862
+ guard $
863
+ toJSON signedTx
864
+ `elem` (v ^.. key " snapshot" . key " confirmed" . values)
865
+
866
+ -- And check that we can close and fanout the head successfully
867
+ send n1 $ input " Close" []
868
+ deadline <- waitMatch (10 * blockTime) n1 $ \ v -> do
869
+ guard $ v ^? key " tag" == Just " HeadIsClosed"
870
+ v ^? key " contestationDeadline" . _JSON
871
+ remainingTime <- diffUTCTime deadline <$> getCurrentTime
872
+ waitFor hydraTracer (remainingTime + 3 * blockTime) [n1] $
873
+ output " ReadyToFanout" [" headId" .= headId]
874
+ send n1 $ input " Fanout" []
875
+ waitMatch (10 * blockTime) n1 $ \ v ->
876
+ guard $ v ^? key " tag" == Just " HeadIsFinalized"
877
+
878
+ -- Assert final wallet balance
879
+ (balance <$> queryUTxOFor networkId nodeSocket QueryTip walletVk)
880
+ `shouldReturn` lovelaceToValue commitAmount
881
+ where
882
+ RunningNode {networkId, nodeSocket, blockTime} = node
883
+
762
884
-- | Compute the integrity hash of a transaction using a list of plutus languages.
763
885
recomputeIntegrityHash ::
764
886
(AlonzoEraPParams ppera , AlonzoEraTxWits txera , AlonzoEraTxBody txera , EraTx txera ) =>
@@ -838,15 +960,15 @@ singlePartyCommitsScriptBlueprint tracer workDir node hydraScriptsTxId =
838
960
getSnapshotUTxO n1 `shouldReturn` scriptUTxO <> scriptUTxO'
839
961
where
840
962
prepareScriptPayload lovelaceAmt = do
841
- let scriptAddress = mkScriptAddress networkId dummyValidatorScript
963
+ let scriptAddress = mkScriptAddress networkId Sha512. dummyValidatorScript
842
964
let datumHash = mkTxOutDatumHash ()
843
965
(scriptIn, scriptOut) <- createOutputAtAddress node scriptAddress datumHash (lovelaceToValue lovelaceAmt)
844
966
let scriptUTxO = UTxO. singleton (scriptIn, scriptOut)
845
967
846
968
let scriptWitness =
847
969
BuildTxWith $
848
970
ScriptWitness scriptWitnessInCtx $
849
- mkScriptWitness dummyValidatorScript (mkScriptDatum () ) (toScriptData () )
971
+ mkScriptWitness Sha512. dummyValidatorScript (mkScriptDatum () ) (toScriptData () )
850
972
let spendingTx =
851
973
unsafeBuildTransaction $
852
974
defaultTxBodyContent
0 commit comments