Skip to content

Commit 29017f8

Browse files
committed
fix: exclude empty datum hash from Alonzo TX output CBOR
This also renames the TX output datum hash struct field for consistency Fixes #1059 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 3bbd004 commit 29017f8

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
@@ -265,10 +265,10 @@ func (b *AlonzoTransactionBody) Utxorpc() (*utxorpc.Tx, error) {
265265
type AlonzoTransactionOutput struct {
266266
cbor.StructAsArray
267267
cbor.DecodeStoreCbor
268-
OutputAddress common.Address
269-
OutputAmount mary.MaryTransactionOutputValue
270-
TxOutputDatumHash *common.Blake2b256
271-
legacyOutput bool
268+
OutputAddress common.Address
269+
OutputAmount mary.MaryTransactionOutputValue
270+
OutputDatumHash *common.Blake2b256
271+
legacyOutput bool
272272
}
273273

274274
func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
@@ -300,7 +300,14 @@ func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) {
300300
}
301301
return cbor.Encode(&tmpOutput)
302302
}
303-
return cbor.EncodeGeneric(o)
303+
tmpOutput := []any{
304+
o.OutputAddress,
305+
o.OutputAmount,
306+
}
307+
if o.OutputDatumHash != nil {
308+
tmpOutput = append(tmpOutput, o.OutputDatumHash)
309+
}
310+
return cbor.Encode(tmpOutput)
304311
}
305312

306313
func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
@@ -314,8 +321,8 @@ func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
314321
Amount: o.OutputAmount.Amount,
315322
Assets: o.OutputAmount.Assets,
316323
}
317-
if o.TxOutputDatumHash != nil {
318-
tmpObj.DatumHash = o.TxOutputDatumHash.String()
324+
if o.OutputDatumHash != nil {
325+
tmpObj.DatumHash = o.OutputDatumHash.String()
319326
}
320327
return json.Marshal(&tmpObj)
321328
}
@@ -337,7 +344,7 @@ func (o AlonzoTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTy
337344
}
338345

339346
func (o AlonzoTransactionOutput) DatumHash() *common.Blake2b256 {
340-
return o.TxOutputDatumHash
347+
return o.OutputDatumHash
341348
}
342349

343350
func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
@@ -374,7 +381,7 @@ func (o AlonzoTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
374381
Coin: o.Amount(),
375382
Assets: assets,
376383
Datum: &utxorpc.Datum{
377-
Hash: o.TxOutputDatumHash.Bytes(),
384+
Hash: o.OutputDatumHash.Bytes(),
378385
},
379386
},
380387
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)