Skip to content

Commit

Permalink
Lookup actual comittedValue in Initial validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Jan 26, 2022
1 parent b2d34a5 commit 1798d7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion hydra-node/test/Hydra/Chain/Direct/Contract/Commit.hs
Expand Up @@ -65,7 +65,7 @@ healthyCommitTx =
committedUtxo :: (TxIn, TxOut CtxUTxO Era)
committedUtxo = flip generateWith 42 $ do
txIn <- arbitrary
txOut <- modifyTxOutValue (const $ lovelaceToValue 8_000_000) <$> (genOutput =<< arbitrary)
txOut <- modifyTxOutValue (const $ lovelaceToValue 7_000_000) <$> (genOutput =<< arbitrary)
pure (txIn, txOut)

commitVerificationKey :: VerificationKey PaymentKey
Expand Down
31 changes: 20 additions & 11 deletions hydra-plutus/src/Hydra/Contract/Initial.hs
Expand Up @@ -12,7 +12,7 @@ import PlutusTx.Prelude
import qualified Hydra.Contract.Commit as Commit
import Ledger.Typed.Scripts (TypedValidator, ValidatorTypes (..))
import qualified Ledger.Typed.Scripts as Scripts
import Plutus.V1.Ledger.Ada (fromValue, getLovelace, lovelaceValueOf)
import Plutus.V1.Ledger.Ada (fromValue, getLovelace)
import qualified PlutusTx
import PlutusTx.IsData.Class (ToData (..))

Expand All @@ -39,22 +39,31 @@ validator ::
InitialRedeemer ->
ScriptContext ->
Bool
validator commitValidator _datum red context@ScriptContext{scriptContextTxInfo = txInfo} =
validator commitValidator _datum red context =
case red of
Abort -> True
Commit{} -> checkOutputValue
where
checkOutputValue =
traceIfFalse "commitLockedValue does not match" $
traceIfFalse ("commitLockedValue: " `appendString` debugValue commitLockedValue) $
traceIfFalse ("initialValue: " `appendString` debugValue initialValue) $
traceIfFalse ("comittedValue: " `appendString` debugValue committedValue) $
commitLockedValue == initialValue + committedValue
Commit{committedRef} -> checkCommit commitValidator committedRef context

checkCommit ::
-- | Commit validator
ValidatorHash ->
Maybe TxOutRef ->
ScriptContext ->
Bool
checkCommit commitValidator comittedRef context@ScriptContext{scriptContextTxInfo = txInfo} =
traceIfFalse "commitLockedValue does not match" $
traceIfFalse ("commitLockedValue: " `appendString` debugValue commitLockedValue) $
traceIfFalse ("initialValue: " `appendString` debugValue initialValue) $
traceIfFalse ("comittedValue: " `appendString` debugValue committedValue) $
commitLockedValue == initialValue + committedValue
where
initialValue =
maybe mempty (txOutValue . txInInfoResolved) $ findOwnInput context

committedValue = lovelaceValueOf 8_000_000
committedValue =
maybe mempty (txOutValue . txInInfoResolved) $ do
ref <- comittedRef
findTxInByTxOutRef ref txInfo

commitLockedValue = valueLockedBy txInfo commitValidator

Expand Down

0 comments on commit 1798d7a

Please sign in to comment.