Skip to content

Commit 3bb00ba

Browse files
johnalotoskiclaude
andcommitted
update: add sub-transaction encoding for Dijkstra TopTx/SubTx
- Extract shared TxBody fields into encodeSharedTxBody - Add encodeSubTx and encodeSubTxBody for Tx SubTx DijkstraEra - Encode requiredTopLevelGuards in sub-transaction bodies - Pass MetadataFormat through encodeTxBody for sub-tx metadata Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 34d49f1 commit 3bb00ba

1 file changed

Lines changed: 73 additions & 11 deletions

File tree

server/src/Ogmios/Data/Json/Dijkstra.hs

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ encodeTx (fmt, opts) x =
328328
<>
329329
"spends" .= Alonzo.encodeIsValid (x ^. Ledger.isValidTxL)
330330
<>
331-
encodeTxBody opts (x ^. Ledger.bodyTxL) (strictMaybe mempty (Map.keys . snd) auxiliary)
331+
encodeTxBody (fmt, opts) (x ^. Ledger.bodyTxL) (strictMaybe mempty (Map.keys . snd) auxiliary)
332332
<>
333333
"metadata" .=? OmitWhenNothing fst auxiliary
334334
<>
@@ -349,23 +349,87 @@ encodeTx (fmt, opts) x =
349349
)
350350

351351
encodeTxBody
352-
:: IncludeCbor
352+
:: (MetadataFormat, IncludeCbor)
353353
-> Ledger.TxBody Ledger.TopTx DijkstraEra
354354
-> [Ledger.ScriptHash]
355355
-> Series
356-
encodeTxBody opts x scripts =
357-
"inputs" .=
358-
encodeFoldable (encodeObject . Shelley.encodeTxIn) (x ^. Ledger.inputsTxBodyL) <>
359-
"references" .=? OmitWhen null
360-
(encodeFoldable (encodeObject . Shelley.encodeTxIn)) (x ^. Ledger.referenceInputsTxBodyL) <>
361-
"outputs" .=
362-
encodeFoldable (encodeObject . Babbage.encodeTxOut (encodeScript opts)) (x ^. Ledger.outputsTxBodyL) <>
356+
encodeTxBody (fmt, opts) x scripts =
357+
encodeSharedTxBody opts x scripts <>
363358
"collaterals" .=? OmitWhen null
364359
(encodeFoldable (encodeObject . Shelley.encodeTxIn)) (x ^. Ledger.collateralInputsTxBodyL) <>
365360
"collateralReturn" .=? OmitWhenNothing
366361
(encodeObject . Babbage.encodeTxOut (encodeScript opts)) (x ^. Ledger.collateralReturnTxBodyL) <>
367362
"totalCollateral" .=? OmitWhenNothing
368363
encodeCoin (x ^. Ledger.totalCollateralTxBodyL) <>
364+
"fee" .=
365+
encodeCoin (x ^. Ledger.feeTxBodyL) <>
366+
"subTransactions" .=? OmitWhen null
367+
(encodeFoldable (encodeSubTx (fmt, opts)))
368+
(x ^. Di.subTransactionsTxBodyL)
369+
370+
-- | Encode a sub-transaction. Unlike top-level transactions, sub-transactions
371+
-- have no 'isValid'/'spends' field, no fee, and no collateral.
372+
encodeSubTx
373+
:: (MetadataFormat, IncludeCbor)
374+
-> Ledger.Tx Ledger.SubTx DijkstraEra
375+
-> Json
376+
encodeSubTx (fmt, opts) x =
377+
encodeObject
378+
( Shelley.encodeTxId (Ledger.txIdTxBody @DijkstraEra (x ^. Ledger.bodyTxL))
379+
<>
380+
encodeSubTxBody opts (x ^. Ledger.bodyTxL) (strictMaybe mempty (Map.keys . snd) auxiliary)
381+
<>
382+
"metadata" .=? OmitWhenNothing fst auxiliary
383+
<>
384+
Alonzo.encodeWitnessSet (snd <$> auxiliary) encodeScriptPurposeIndex (encodeScript opts) (x ^. Ledger.witsTxL)
385+
<>
386+
-- NOTE: Using @DijkstraEra (not @ConwayEra as in encodeTx) because
387+
-- sub-transactions are a Dijkstra-only feature.
388+
if includeTransactionCbor opts then
389+
"cbor" .= encodeByteStringBase16 (encodeCbor @DijkstraEra x)
390+
else
391+
mempty
392+
)
393+
where
394+
auxiliary = do
395+
hash <- Shelley.encodeAuxiliaryDataHash . Ledger.hashTxAuxData <$> (x ^. Ledger.auxDataTxL)
396+
(labels, scripts) <- Alonzo.encodeAuxiliaryData (fmt, opts) <$> x ^. Ledger.auxDataTxL
397+
pure
398+
( encodeObject ("hash" .= hash <> "labels" .= labels)
399+
, scripts
400+
)
401+
402+
encodeSubTxBody
403+
:: IncludeCbor
404+
-> Ledger.TxBody Ledger.SubTx DijkstraEra
405+
-> [Ledger.ScriptHash]
406+
-> Series
407+
encodeSubTxBody opts x scripts =
408+
encodeSharedTxBody opts x scripts <>
409+
"requiredTopLevelGuards" .=? OmitWhen null
410+
(encodeMapAsList encodeGuardEntry)
411+
(x ^. Di.requiredTopLevelGuardsL)
412+
where
413+
encodeGuardEntry credential mData =
414+
encodeObject
415+
( "credential" `Shelley.encodeCredential` credential
416+
<> "datum" .=? OmitWhenNothing
417+
(Alonzo.encodeData @DijkstraEra) mData
418+
)
419+
420+
-- | Encode TxBody fields shared between TopTx and SubTx.
421+
encodeSharedTxBody
422+
:: IncludeCbor
423+
-> Ledger.TxBody l DijkstraEra
424+
-> [Ledger.ScriptHash]
425+
-> Series
426+
encodeSharedTxBody opts x scripts =
427+
"inputs" .=
428+
encodeFoldable (encodeObject . Shelley.encodeTxIn) (x ^. Ledger.inputsTxBodyL) <>
429+
"references" .=? OmitWhen null
430+
(encodeFoldable (encodeObject . Shelley.encodeTxIn)) (x ^. Ledger.referenceInputsTxBodyL) <>
431+
"outputs" .=
432+
encodeFoldable (encodeObject . Babbage.encodeTxOut (encodeScript opts)) (x ^. Ledger.outputsTxBodyL) <>
369433
"certificates" .=? OmitWhen null
370434
(encodeConcatNonEmptyFoldable (fmap encodeObject . encodeTxCert)) (x ^. Ledger.certsTxBodyL) <>
371435
"withdrawals" .=? OmitWhen (null . Ledger.unWithdrawals)
@@ -380,8 +444,6 @@ encodeTxBody opts x scripts =
380444
Shelley.encodeNetwork (x ^. Ledger.networkIdTxBodyL) <>
381445
"scriptIntegrityHash" .=? OmitWhenNothing
382446
Alonzo.encodeScriptIntegrityHash (x ^. Ledger.scriptIntegrityHashTxBodyL) <>
383-
"fee" .=
384-
encodeCoin (x ^. Ledger.feeTxBodyL) <>
385447
"validityInterval" .=
386448
Allegra.encodeValidityInterval (x ^. Ledger.vldtTxBodyL) <>
387449
"proposals" .=? OmitWhen null

0 commit comments

Comments
 (0)