Skip to content

Commit

Permalink
Correctly deserialize initial redeemer when observing commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ authored and ch1bo committed Jan 26, 2022
1 parent 0f337bf commit b700856
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions hydra-node/src/Hydra/Chain/Direct/Tx.hs
Expand Up @@ -70,7 +70,6 @@ import Hydra.Party (MultiSigned, Party (Party), toPlutusSignatures, vkey)
import Hydra.Snapshot (Snapshot (..))
import Ledger.Typed.Scripts (DatumType)
import Ledger.Value (AssetClass (..), currencyMPSHash)
import Ouroboros.Consensus.Util (eitherToMaybe)
import Plutus.V1.Ledger.Api (MintingPolicyHash, PubKeyHash (..), fromBuiltin, fromData)
import qualified Plutus.V1.Ledger.Api as Plutus
import Plutus.V1.Ledger.Value (assetClass, currencySymbol, tokenName)
Expand Down Expand Up @@ -476,7 +475,7 @@ observeCommitTx ::
observeCommitTx networkId initials (Api.getTxBody . fromLedgerTx -> txBody) = do
initialTxIn <- findInitialTxIn
initialRedeemer <- findRedeemerSpending txBody initialTxIn
let mCommittedTxIn = Api.fromPlutusTxOutRef <$> initialRedeemer
mCommittedTxIn <- matchCommit initialRedeemer

(commitIn, commitOut) <- Api.findTxOutByAddress commitAddress txBody
dat <- getDatum commitOut
Expand All @@ -487,7 +486,8 @@ observeCommitTx networkId initials (Api.getTxBody . fromLedgerTx -> txBody) = do
case (mCommittedTxIn, mCommittedTxOut) of
(Nothing, Nothing) -> Just mempty
(Just i, Just o) -> Just $ Api.singletonUtxo (i, o)
_ -> Nothing
(Nothing, Just{}) -> error "found commit with no redeemer out ref but with serialized output."
(Just{}, Nothing) -> error "found commit with redeemer out ref but with no serialized output."

let onChainTx = OnCommitTx (convertParty party) comittedUtxo
pure
Expand All @@ -503,17 +503,25 @@ observeCommitTx networkId initials (Api.getTxBody . fromLedgerTx -> txBody) = do
let ins = filterTxIn (`elem` (Api.fromLedgerTxIn <$> initials)) txBody
in case ins of
[input] -> Just input
_ ->
-- XXX(SN): this is indicating a problem and we at least wan't to know about it
Nothing
[] -> Nothing
_ -> error "transaction consuming more than one initial at once."

matchCommit :: Initial.InitialRedeemer -> Maybe (Maybe Api.TxIn)
matchCommit = \case
Initial.Abort ->
Nothing
Initial.Commit{committedRef} ->
Just (Api.fromPlutusTxOutRef <$> committedRef)

convertTxOut :: Maybe Commit.SerializedTxOut -> Maybe (Api.TxOut Api.CtxUTxO Api.Era)
convertTxOut = \case
Nothing -> Nothing
Just (Commit.SerializedTxOut outBytes) ->
-- XXX(SN): these errors might be more severe and we could throw an
-- exception here?
eitherToMaybe $ Api.fromLedgerTxOut <$> decodeFull' (fromBuiltin outBytes)
case Api.fromLedgerTxOut <$> decodeFull' (fromBuiltin outBytes) of
Right result -> Just result
Left{} -> error "couldn't deserialize serialized output in commit's datum."

commitAddress = mkScriptAddress @Api.PlutusScriptV1 networkId commitScript

Expand Down

0 comments on commit b700856

Please sign in to comment.