serializedUtxo = case utxo of
-
( Commit.SerializedTxOutRef (toBuiltin $ serialize' $ Api.toLedgerTxIn i)
-
, Commit.SerializedTxOut (toBuiltin $ serialize' $ Api.toLedgerTxOut o)
+
Just $ Commit.SerializedTxOut (toBuiltin $ serialize' $ Api.toLedgerTxOut o)
-- | Create a transaction collecting all "committed" utxo and opening a Head,
-- i.e. driving the Head script state.
convertParty = Party . partyToVerKey
-- | Identify a commit tx by looking for an output which pays to v_commit.
+
-- To reconstruct a commit from a commit transaction we need to:
+
-- - Find out which 'initials' is being committed
+
-- - Find the redeemer corresponding to that 'initial' which contain the
+
-- output-reference of the commit
+
-- - Find the outputs which pays to the commit validator
+
-- - Extract the datum of that output, which contains the output of the commit
+
-- - Reconstruct the committed UTXO from both values (output ref and output).
+
-- TODO: This type may be too large for this function, we only probably need
Maybe (OnChainTx CardanoTx, (TxIn, TxOut CtxUTxO Era, ScriptData))
-
observeCommitTx networkId (getTxBody -> txBody) = do
+
observeCommitTx networkId initials (getTxBody -> txBody) = do
+
let ins = filterTxIn (`elem` initials) txBody
(commitIn, commitOut) <- findTxOutByAddress commitAddress txBody
dat <- getDatum commitOut
-
(party, _, committedUtxo) <- fromData @(DatumType Commit.Commit) $ toPlutusData dat
-
convertedUtxo <- convertUtxo committedUtxo
+
(party, _, serializedTxOut) <- fromData @(DatumType Commit.Commit) $ toPlutusData dat
+
convertedTxOut <- convertTxOut serializedTxOut
let onChainTx = OnCommitTx (convertParty party) convertedUtxo
pure (onChainTx, (commitIn, toCtxUTxOTxOut commitOut, dat))
-
convertUtxo :: Maybe (Commit.SerializedTxOutRef, Commit.SerializedTxOut) -> Maybe Utxo
+
convertTxOut :: Maybe Commit.SerializedTxOut -> Maybe (TxOut CtxTx Era)
-
Just (Commit.SerializedTxOutRef inBytes, Commit.SerializedTxOut outBytes) ->
+
Just (Commit.SerializedTxOut outBytes) ->
-- XXX(SN): these errors might be more severe and we could throw an
-
txIn <- fromLedgerTxIn <$> decodeFull' (fromBuiltin inBytes)
-
txOut <- fromLedgerTxOut <$> decodeFull' (fromBuiltin outBytes)
-
pure $ singletonUtxo (txIn, txOut)
+
eitherToMaybe $ decodeFull' (fromBuiltin outBytes)
commitAddress = mkScriptAddress @PlutusScriptV1 networkId commitScript
Maybe (OnChainTx CardanoTx, OnChainHeadState)
observeCommit networkId tx = \case
Initial{threadOutput, initials, commits} -> do
-
(onChainTx, commitTriple) <- observeCommitTx networkId tx
+
(onChainTx, commitTriple) <- observeCommitTx networkId initials tx
-- NOTE(SN): A commit tx has been observed and thus we can remove all it's
-- inputs from our tracked initials
let commitIns = inputs tx