Skip to content

Commit 412e979

Browse files
authored
fix: exclude empty datum hash from Alonzo TX output CBOR (#1061)
This also renames the TX output datum hash struct field for consistency Fixes #1059 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 326e3d8 commit 412e979

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

ledger/alonzo/alonzo.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,10 @@ func (b *AlonzoTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
267267
type AlonzoTransactionOutput struct {
268268
cbor.StructAsArray
269269
cbor.DecodeStoreCbor
270-
OutputAddress common.Address
271-
OutputAmount mary.MaryTransactionOutputValue
272-
TxOutputDatumHash *common.Blake2b256
273-
legacyOutput bool
270+
OutputAddress common.Address
271+
OutputAmount mary.MaryTransactionOutputValue
272+
OutputDatumHash *common.Blake2b256
273+
legacyOutput bool
274274
}
275275

276276
func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
@@ -302,7 +302,14 @@ func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) {
302302
}
303303
return cbor.Encode(&tmpOutput)
304304
}
305-
return cbor.EncodeGeneric(o)
305+
tmpOutput := []any{
306+
o.OutputAddress,
307+
o.OutputAmount,
308+
}
309+
if o.OutputDatumHash != nil {
310+
tmpOutput = append(tmpOutput, o.OutputDatumHash)
311+
}
312+
return cbor.Encode(tmpOutput)
306313
}
307314

308315
func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
@@ -316,8 +323,8 @@ func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
316323
Amount: o.OutputAmount.Amount,
317324
Assets: o.OutputAmount.Assets,
318325
}
319-
if o.TxOutputDatumHash != nil {
320-
tmpObj.DatumHash = o.TxOutputDatumHash.String()
326+
if o.OutputDatumHash != nil {
327+
tmpObj.DatumHash = o.OutputDatumHash.String()
321328
}
322329
return json.Marshal(&tmpObj)
323330
}
@@ -339,7 +346,7 @@ func (o AlonzoTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTy
339346
}
340347

341348
func (o AlonzoTransactionOutput) DatumHash() *common.Blake2b256 {
342-
return o.TxOutputDatumHash
349+
return o.OutputDatumHash
343350
}
344351

345352
func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
@@ -376,7 +383,7 @@ func (o AlonzoTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
376383
Coin: o.Amount(),
377384
Assets: assets,
378385
Datum: &utxorpc.Datum{
379-
Hash: o.TxOutputDatumHash.Bytes(),
386+
Hash: o.OutputDatumHash.Bytes(),
380387
},
381388
},
382389
nil

ledger/alonzo/pparams_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ func TestAlonzoTransactionOutput_Utxorpc(t *testing.T) {
461461

462462
// Mock output
463463
output := alonzo.AlonzoTransactionOutput{
464-
OutputAddress: address,
465-
OutputAmount: mary.MaryTransactionOutputValue{Amount: amount},
466-
TxOutputDatumHash: &datumHash,
464+
OutputAddress: address,
465+
OutputAmount: mary.MaryTransactionOutputValue{Amount: amount},
466+
OutputDatumHash: &datumHash,
467467
}
468468

469469
got, err := output.Utxorpc()
@@ -503,9 +503,9 @@ func TestAlonzoTransactionBody_Utxorpc(t *testing.T) {
503503
address := common.Address{}
504504
datumHash := common.Blake2b256{1, 2, 3, 4}
505505
output := alonzo.AlonzoTransactionOutput{
506-
OutputAddress: address,
507-
OutputAmount: mary.MaryTransactionOutputValue{Amount: 1000},
508-
TxOutputDatumHash: &datumHash,
506+
OutputAddress: address,
507+
OutputAmount: mary.MaryTransactionOutputValue{Amount: 1000},
508+
OutputDatumHash: &datumHash,
509509
}
510510

511511
body := alonzo.AlonzoTransactionBody{
@@ -560,9 +560,9 @@ func TestAlonzoTransaction_Utxorpc(t *testing.T) {
560560
address := common.Address{}
561561
datumHash := &common.Blake2b256{0x11, 0x22, 0x33}
562562
output := alonzo.AlonzoTransactionOutput{
563-
OutputAddress: address,
564-
OutputAmount: mary.MaryTransactionOutputValue{Amount: 2000},
565-
TxOutputDatumHash: datumHash,
563+
OutputAddress: address,
564+
OutputAmount: mary.MaryTransactionOutputValue{Amount: 2000},
565+
OutputDatumHash: datumHash,
566566
}
567567

568568
body := alonzo.AlonzoTransactionBody{

0 commit comments

Comments
 (0)