Skip to content

Commit

Permalink
Check committed TxOut is preserved in commit datum
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Jan 26, 2022
1 parent d62ec12 commit 6bf9506
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions hydra-plutus/src/Hydra/Contract/Commit.hs
Expand Up @@ -25,6 +25,9 @@ PlutusTx.unstableMakeIsData ''SerializedTxOutRef
newtype SerializedTxOut = SerializedTxOut BuiltinByteString
PlutusTx.unstableMakeIsData ''SerializedTxOut

instance Eq SerializedTxOut where
SerializedTxOut bs == SerializedTxOut bs' = bs == bs'

-- TODO: Having the 'TxOutRef' on-chain is not necessary but it is convenient
-- for the off-chain code to reconstrut the commit UTXO.
--
Expand Down
18 changes: 14 additions & 4 deletions hydra-plutus/src/Hydra/Contract/Initial.hs
Expand Up @@ -9,11 +9,13 @@ module Hydra.Contract.Initial where
import Ledger hiding (validatorHash)
import PlutusTx.Prelude

import Hydra.Contract.Commit (SerializedTxOut, SerializedTxOutRef)
import Hydra.Contract.Commit (SerializedTxOut (SerializedTxOut), SerializedTxOutRef)
import qualified Hydra.Contract.Commit as Commit
import Hydra.Contract.Encoding (encodeTxOut)
import Hydra.Data.Party (Party)
import Ledger.Typed.Scripts (TypedValidator, ValidatorTypes (..))
import qualified Ledger.Typed.Scripts as Scripts
import Plutus.Codec.CBOR.Encoding (encodingToBuiltinByteString)
import Plutus.V1.Ledger.Ada (fromValue, getLovelace)
import qualified PlutusTx
import PlutusTx.IsData.Class (ToData (..), fromBuiltinData)
Expand Down Expand Up @@ -71,16 +73,24 @@ checkCommit commitValidator committedRef context@ScriptContext{scriptContextTxIn
case fromBuiltinData @(Party, ValidatorHash, Maybe (SerializedTxOutRef, SerializedTxOut)) da of
Just (_party, _headScriptHash, Nothing) ->
traceIfFalse "committed UTXO is not in output datum" $ isNothing committedRef
Just (_party, _headScriptHash, Just (_serialisedTxOutRef, serialisedTxOut)) ->
case txInInfoResolved <$> committedTxOut of
Nothing -> traceError "unexpected UTXO in output datum"
Just txOut ->
traceIfFalse "mismatch committed TxOut in datum" $
SerializedTxOut (encodingToBuiltinByteString (encodeTxOut txOut)) == serialisedTxOut
_ -> traceError "TODO"
_ -> traceError "expected single commit output"

initialValue =
maybe mempty (txOutValue . txInInfoResolved) $ findOwnInput context

committedValue =
maybe mempty (txOutValue . txInInfoResolved) $ do
ref <- committedRef
findTxInByTxOutRef ref txInfo
maybe mempty (txOutValue . txInInfoResolved) $ committedTxOut

committedTxOut = do
ref <- committedRef
findTxInByTxOutRef ref txInfo

commitLockedValue = valueLockedBy txInfo commitValidator

Expand Down

0 comments on commit 6bf9506

Please sign in to comment.