Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement deposit validators and off-chain code #1580

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions hydra-node/bench/tx-cost/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ scriptSizes =
, "| " <> "νCommit" <> " | " <> serialiseToRawBytesHexText commitScriptHash <> " | " <> show commitScriptSize <> " | "
, "| " <> "νHead" <> " | " <> serialiseToRawBytesHexText headScriptHash <> " | " <> show headScriptSize <> " | "
, "| " <> "μHead" <> " | " <> serialiseToRawBytesHexText mintingScriptHash <> "* | " <> show mintingScriptSize <> " | "
, "| " <> "νDeposit" <> " | " <> serialiseToRawBytesHexText depositScriptHash <> " | " <> show depositScriptSize <> " | "
, ""
, "* The minting policy hash is only usable for comparison. As the script is parameterized, the actual script is unique per head."
]
Expand All @@ -148,6 +149,8 @@ scriptSizes =
, commitScriptSize
, headScriptHash
, headScriptSize
, depositScriptHash
, depositScriptSize
} = scriptInfo

costOfInit :: IO Text
Expand Down
4 changes: 3 additions & 1 deletion hydra-plutus/hydra-plutus.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: hydra-plutus
version: 0.18.1
version: 0.19
synopsis: Hydra Plutus Contracts
author: IOG
copyright: 2022 IOG
Expand Down Expand Up @@ -44,6 +44,8 @@ library
Hydra.Contract
Hydra.Contract.Commit
Hydra.Contract.CommitError
Hydra.Contract.Deposit
Hydra.Contract.DepositError
Hydra.Contract.Error
Hydra.Contract.Hash
Hydra.Contract.Head
Expand Down
5 changes: 0 additions & 5 deletions hydra-plutus/scripts/mDeposit.plutus

This file was deleted.

4 changes: 2 additions & 2 deletions hydra-plutus/scripts/mHead.plutus

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions hydra-plutus/scripts/vDeposit.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV2",
"description": "hydra-vDeposit-0.18.1-27-g5d6d4b00e",
"cborHex": "590ae4590ae10100003233322232332232323232323232323232323232323322323232323232323232323232323232323232222533533322253353232325333573466e1d2000002112200215333573466e1d20020021321223001003375a6ae840044c05c060d55cf0011aab9d001375400a20326442a6a6a6aa00444444444444400a440044466aa666a00442660109210344303400333573466e24004d401c88800807807c4c061241034430330013018491034430330013300749010344303500333573466e3cdc9180f99a8058019aa8021111111111110051b9230273024302b3500622200101e01d101d1350012200222533500110191335738004030646464a666ae68cdc3a4000004264646464a666ae68cdc3a4000004264666444246660020080060046eb8d5d08011bad35742002600a6ae84d5d10009aba20011301801935573c0046aae74004dd51aba1002300b23232325333573466e1d20000021332212330010030023013357420026eb8d5d09aba20011301801935573c0046aae74004dd50008980a00a9aab9e00235573a0026ea800cc8c8c94ccd5cd19b87480000084cc8848cc00400c008c8c8c94ccd5cd19b87480000084c8c8c8c8c8c8c8c8c8c8cccccccccccc88888888888848cccccccccccc00403403002c02802402001c01801401000c008cc06006cd5d08059980c00d9aba100a3301801e35742012666048eb9408cd5d08041998123ae50233574200e660300346ae84018ccc09007dd69aba10053232325333573466e1d20000021332212330010030023232325333573466e1d20000021332212330010030023301d75a6ae84004c070d5d09aba20011302702835573c0046aae74004dd51aba10013232325333573466e1d20000021332212330010030023301d75a6ae84004c070d5d09aba20011302702835573c0046aae74004dd51aba1357440022604804a6aae78008d55ce8009baa3574200866030eb8d5d080199981200c80a9aba100233302475c02a6ae84004c074d5d09aba2001357440026ae88004d5d10009aba2001357440026ae88004d5d10009aba2001357440026ae880044c05c060d55cf0011aab9d00137546ae84004c030d5d09aba20011301401535573c0046aae74004dd5000880e09808a481035054350033028221225333573466e240092000102715335001102722133502800233530061200133702008900100089000900091919192999ab9a3370e9000001080a0a999ab9a3370e9001001080a898078081aab9e00235573a0026ea800488c8c8c94ccd5cd19b87480000084488800854ccd5cd19b87480080084c84888c00c010c014d5d08008a999ab9a3370e900200108911000898078081aab9e00235573a0026ea80048c88c008dd60009812911999aab9f0012023233502230043574200460066ae880080408c8c8c94ccd5cd19b87480000084c848888c00c014dd71aba100115333573466e1d20020021321222230010053007357420022a666ae68cdc3a400800426424444600400a60146ae8400454ccd5cd19b87480180084c848888c010014c014d5d0800898060069aab9e00235573a0026ea80048c8c8c94ccd5cd19b87480000084c848888888c014020c024d5d08008a999ab9a3370e900100109909111111180380418049aba100115333573466e1d20040021332212222222330060090083009357420026eb8d5d09aba200115333573466e1d2006002133221222222233002009008375c6ae84004dd71aba1357440022a666ae68cdc3a40100042664424444444660020120106eb8d5d08009bad357426ae8800454ccd5cd19b874802800844888888801054ccd5cd19b874803000844888888800c4c02c030d55cf0011aab9d00137540024646464a666ae68cdc3a4000004266442466002006004600a6ae84004c01cd5d09aba20011300a00b35573c0046aae74004dd500091919192999ab9a3370e900000109991091980080180118029aba1001375a6ae84d5d1000898048051aab9e00235573a0026ea80048c8c8c94ccd5cd19b87480000084dd71aba10011300800935573c0046aae74004dd50009911919192999ab9a3370e900000109919199991110919998008028020018011919192999ab9a3370e900000109991091980080180118061aba10013300a00b357426ae880044c034038d55cf0011aab9d00137546ae8400cccc035d728061aba10023232325333573466e1d20000021122200315333573466e1d200200213212223001004375c6ae8400454ccd5cd19b87480100084c84888c008010d5d0800898068071aab9e00235573a0026ea8d5d080099803bae357426ae88004d5d10009aba20011300800935573c0046aae74004dd5000911919192999ab9a3370e900100108910008a999ab9a3370e9000001099091180100198029aba10011300800935573c0046aae74004dd500091919192999ab9a3370e9000001099091180100198029aba100115333573466e1d2002002132333222122333001005004003375a6ae84008dd69aba1001375a6ae84d5d10009aba20011300600735573c0046aae74004dd500091919192999ab9a3370e900000109909118010019bae357420022a666ae68cdc3a400400426424460020066eb8d5d0800898028031aab9e00235573a0026ea80048c98cd5ce000802a481035054310013300175ceb488c88c008dd5800980d111999aab9f001201823233501833221233001003002300635573a002600a6aae78004c010d5d10019aba1002005120011220021220013014225335001100b221337146eccd40088888cdd2a400066ae80d401088cdd2a400066ae80c02c008cd5d019806119a800919ba548000cd5d018068009bb10102223374a900119aba0375000666ae80dd400119aba037500026ec4048004dd880719aba033300b75266016ea48dd400080199aba05333500213374a90001bb100c213374a900219aba0001376201a4266e9520023357406ea4004dd880699aba03300a7520026ec4030c0100048cd40048cdd2a400066ae80dd48009bb100523374a900119aba037520026ec4014894cd400484cdd2a400066ae80c00c004dd8802899ba548008dd8802111911ba6300200130132253350011376400a4426a0044466ae80cdd8180400118038009803001a4c601e44a66a002260046006018442a66a0022004442600c600e00a601c44a66a0022016442a66a002200644266a01c6600e008004600c002601a444a66a0042002442a66a006200864442a666a6602200a0042a006266a01e0046601000e0022a006266a0180046600a002006601844a66a00220064426a0044466e28004c01800d22010035003222001350022220023500122200333230010012212323330022300222230030042300222230020042300222230010042353300400400335003001122223330042533500113350060070072215335001133500800300922153233353300d00500315001133300800333500b00500c002150011333006002233500b00500100122253350011335008335008003002300600932221533353300d00600215003133300800233500b0060050011500313350083350080030023006001222532335002130014988854c8ccd4cc03801c00c540044c011261500113330070022300633500c0070010012335009300333500900400a30070021220021221223300100400332223500222350022235005223500222533353300b00600215333573466e1c014004403854ccd5cd19b89005001100c100d13300b00600213300b006002225333573466e3c008004401454ccd5cd19b91002001100310041222003122200212220012323001001230022330020020011"
}
4 changes: 2 additions & 2 deletions hydra-plutus/scripts/vHead.plutus

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions hydra-plutus/src/Hydra/Contract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Hydra.Cardano.Api (
pattern PlutusScript,
)
import Hydra.Contract.Commit qualified as Commit
import Hydra.Contract.Deposit qualified as Deposit
import Hydra.Contract.Head qualified as Head
import Hydra.Contract.HeadTokens qualified as HeadTokens
import Hydra.Contract.Initial qualified as Initial
Expand All @@ -30,6 +31,8 @@ data ScriptInfo = ScriptInfo
, commitScriptSize :: Int64
, headScriptHash :: ScriptHash
, headScriptSize :: Int64
, depositScriptHash :: ScriptHash
, depositScriptSize :: Int64
}
deriving stock (Eq, Show, Generic)
deriving anyclass (ToJSON)
Expand All @@ -47,6 +50,8 @@ scriptInfo =
, commitScriptSize = scriptSize Commit.validatorScript
, headScriptHash = plutusScriptHash Head.validatorScript
, headScriptSize = scriptSize Head.validatorScript
, depositScriptHash = plutusScriptHash Deposit.validatorScript
, depositScriptSize = scriptSize Deposit.validatorScript
}
where
plutusScriptHash =
Expand Down
106 changes: 106 additions & 0 deletions hydra-plutus/src/Hydra/Contract/Deposit.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-specialize #-}
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:conservative-optimisation #-}
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:defer-errors #-}
-- Plutus core version to compile to. In babbage era, that is Cardano protocol
-- version 7 and 8, only plutus-core version 1.0.0 is available.
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:target-version=1.0.0 #-}

-- | The validator used to deposit and recover locked funds
module Hydra.Contract.Deposit where

import PlutusTx.Prelude

import Hydra.Cardano.Api (PlutusScriptVersion (PlutusScriptV2))
import Hydra.Contract.Commit (Commit)
import Hydra.Contract.DepositError (
DepositError (
DepositDeadlineNotReached,
DepositNoLowerBoundDefined,
IncorrectDepositHash
),
)
import Hydra.Contract.Error (errorCode)
import Hydra.Contract.Head (hashPreSerializedCommits, hashTxOuts)
import Hydra.Plutus.Extras (ValidatorType, scriptValidatorHash, wrapValidator)
import PlutusLedgerApi.V2 (
CurrencySymbol,
Datum (Datum),
Extended (Finite),
Interval (ivFrom),
LowerBound (LowerBound),
POSIXTime,
Redeemer (Redeemer),
ScriptContext (..),
ScriptHash,
SerialisedScript,
serialiseCompiledCode,
txInfoOutputs,
txInfoValidRange,
)
import PlutusTx (CompiledCode, toBuiltinData)
import PlutusTx qualified

data DepositRedeemer
= -- | Claims already deposited funds.
Claim
| -- | Recovers m number of deposited outputs.
Recover Integer

PlutusTx.unstableMakeIsData ''DepositRedeemer

-- | Deposit datum containing HeadId, deadline and a list of deposits.
newtype DepositDatum
= DepositDatum (CurrencySymbol, POSIXTime, [Commit])

PlutusTx.unstableMakeIsData ''DepositDatum

-- | v_deposit validator checks
--
-- * Claim redeemer -> more checks will be added
--
-- * Recover redeemer
-- * The deadline has been reached.
-- * The hash of recovered outputs are matching the deposited outputs.
validator :: DepositDatum -> DepositRedeemer -> ScriptContext -> Bool
validator depositDatum r ctx =
case r of
Claim -> False
Recover m ->
afterDeadline
&& recoverOutputs m
where
DepositDatum (_headId, dl, deposits) = depositDatum
recoverOutputs m =
traceIfFalse $(errorCode IncorrectDepositHash) $
hashOfOutputs m == hashPreSerializedCommits deposits

hashOfOutputs m =
hashTxOuts $ take m (txInfoOutputs txInfo)

afterDeadline =
case ivFrom (txInfoValidRange txInfo) of
LowerBound (Finite t) _ ->
traceIfFalse $(errorCode DepositDeadlineNotReached) $
t > dl
_ -> traceError $(errorCode DepositNoLowerBoundDefined)

ScriptContext{scriptContextTxInfo = txInfo} = ctx

compiledValidator :: CompiledCode ValidatorType
compiledValidator =
$$(PlutusTx.compile [||wrap validator||])
where
wrap = wrapValidator @DepositDatum @DepositRedeemer

validatorScript :: SerialisedScript
validatorScript = serialiseCompiledCode compiledValidator

validatorHash :: ScriptHash
validatorHash = scriptValidatorHash PlutusScriptV2 validatorScript

datum :: DepositDatum -> Datum
datum a = Datum (toBuiltinData a)

redeemer :: DepositRedeemer -> Redeemer
redeemer a = Redeemer (toBuiltinData a)
25 changes: 25 additions & 0 deletions hydra-plutus/src/Hydra/Contract/DepositError.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Hydra.Contract.DepositError (
errorCode,
module Hydra.Contract.DepositError,
) where

import Hydra.Contract.Error (ToErrorCode (..), errorCode)
import Text.Show (Show)

data DepositError
= DepositDeadlineSurpassed
| DepositNoUpperBoundDefined
| DepositNoLowerBoundDefined
| DepositDeadlineNotReached
| IncorrectDepositHash
| DTNotMinted
deriving stock (Show)

instance ToErrorCode DepositError where
toErrorCode = \case
DepositDeadlineSurpassed -> "D01"
DepositNoUpperBoundDefined -> "D02"
DepositNoLowerBoundDefined -> "D03"
DepositDeadlineNotReached -> "D04"
IncorrectDepositHash -> "D05"
DTNotMinted -> "D06"
Loading
Loading