Skip to content

Commit dc70718

Browse files
committed
feat: Added support for proper encoding/decoding of TX metdata
Signed-off-by: Akhil Repala <[email protected]>
1 parent b75fedb commit dc70718

File tree

9 files changed

+271
-54
lines changed

9 files changed

+271
-54
lines changed

ledger/allegra/allegra.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type AllegraBlock struct {
4949
BlockHeader *AllegraBlockHeader
5050
TransactionBodies []AllegraTransactionBody
5151
TransactionWitnessSets []shelley.ShelleyTransactionWitnessSet
52-
TransactionMetadataSet map[uint]*cbor.LazyValue
52+
TransactionMetadataSet map[uint]common.TransactionMetadataSet
5353
}
5454

5555
func (b *AllegraBlock) UnmarshalCBOR(cborData []byte) error {
@@ -238,7 +238,7 @@ type AllegraTransaction struct {
238238
cbor.DecodeStoreCbor
239239
Body AllegraTransactionBody
240240
WitnessSet shelley.ShelleyTransactionWitnessSet
241-
TxMetadata *cbor.LazyValue
241+
TxMetadata common.TransactionMetadataSet
242242
}
243243

244244
func (t *AllegraTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -336,7 +336,7 @@ func (t AllegraTransaction) Donation() uint64 {
336336
return t.Body.Donation()
337337
}
338338

339-
func (t AllegraTransaction) Metadata() *cbor.LazyValue {
339+
func (t AllegraTransaction) Metadata() common.TransactionMetadataSet {
340340
return t.TxMetadata
341341
}
342342

@@ -398,7 +398,7 @@ func (t *AllegraTransaction) Cbor() []byte {
398398
cbor.RawMessage(t.WitnessSet.Cbor()),
399399
}
400400
if t.TxMetadata != nil {
401-
tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor()))
401+
tmpObj = append(tmpObj, t.TxMetadata)
402402
} else {
403403
tmpObj = append(tmpObj, nil)
404404
}

ledger/allegra/block_test.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package allegra_test
1616

1717
import (
18-
"bytes"
1918
"encoding/hex"
2019
"strings"
2120
"testing"
@@ -61,32 +60,20 @@ func TestAllegraBlock_CborRoundTrip_UsingCborEncode(t *testing.T) {
6160
t.Fatal("Custom encoded CBOR from AllegraBlock is nil or empty")
6261
}
6362

64-
// Ensure the original and re-encoded CBOR bytes are identical
65-
if !bytes.Equal(dataBytes, encoded) {
66-
t.Errorf(
67-
"Custom CBOR round-trip mismatch for Allegra block\nOriginal CBOR (hex): %x\nCustom Encoded CBOR (hex): %x",
68-
dataBytes,
69-
encoded,
70-
)
71-
72-
// Check from which byte it differs
73-
diffIndex := -1
74-
for i := 0; i < len(dataBytes) && i < len(encoded); i++ {
75-
if dataBytes[i] != encoded[i] {
76-
diffIndex = i
77-
break
78-
}
79-
}
80-
if diffIndex != -1 {
81-
t.Logf("First mismatch at byte index: %d", diffIndex)
82-
t.Logf(
83-
"Original byte: 0x%02x, Re-encoded byte: 0x%02x",
84-
dataBytes[diffIndex],
85-
encoded[diffIndex],
86-
)
87-
} else {
88-
t.Logf("Length mismatch: original length = %d, re-encoded length = %d", len(dataBytes), len(encoded))
89-
}
63+
// Ensure the re-encoded CBOR is structurally valid and decodes back
64+
var redecoded allegra.AllegraBlock
65+
if err := redecoded.UnmarshalCBOR(encoded); err != nil {
66+
t.Fatalf("Re-encoded AllegraBlock failed to decode: %v", err)
67+
}
68+
// Checking for few invariants
69+
if redecoded.BlockNumber() != block.BlockNumber() {
70+
t.Errorf("BlockNumber mismatch after re-encode: got %d, want %d", redecoded.BlockNumber(), block.BlockNumber())
71+
}
72+
if redecoded.SlotNumber() != block.SlotNumber() {
73+
t.Errorf("SlotNumber mismatch after re-encode: got %d, want %d", redecoded.SlotNumber(), block.SlotNumber())
74+
}
75+
if len(redecoded.TransactionBodies) != len(block.TransactionBodies) {
76+
t.Errorf("Tx count mismatch after re-encode: got %d, want %d", len(redecoded.TransactionBodies), len(block.TransactionBodies))
9077
}
9178
}
9279

ledger/alonzo/alonzo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type AlonzoBlock struct {
5555
BlockHeader *AlonzoBlockHeader
5656
TransactionBodies []AlonzoTransactionBody
5757
TransactionWitnessSets []AlonzoTransactionWitnessSet
58-
TransactionMetadataSet map[uint]*cbor.LazyValue
58+
TransactionMetadataSet map[uint]common.TransactionMetadataSet
5959
InvalidTransactions []uint
6060
}
6161

@@ -577,7 +577,7 @@ type AlonzoTransaction struct {
577577
Body AlonzoTransactionBody
578578
WitnessSet AlonzoTransactionWitnessSet
579579
TxIsValid bool
580-
TxMetadata *cbor.LazyValue
580+
TxMetadata common.TransactionMetadataSet
581581
}
582582

583583
func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -679,7 +679,7 @@ func (t AlonzoTransaction) Donation() uint64 {
679679
return t.Body.Donation()
680680
}
681681

682-
func (t AlonzoTransaction) Metadata() *cbor.LazyValue {
682+
func (t AlonzoTransaction) Metadata() common.TransactionMetadataSet {
683683
return t.TxMetadata
684684
}
685685

@@ -739,7 +739,7 @@ func (t *AlonzoTransaction) Cbor() []byte {
739739
t.TxIsValid,
740740
}
741741
if t.TxMetadata != nil {
742-
tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor()))
742+
tmpObj = append(tmpObj, t.TxMetadata)
743743
} else {
744744
tmpObj = append(tmpObj, nil)
745745
}

ledger/babbage/babbage.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type BabbageBlock struct {
5555
BlockHeader *BabbageBlockHeader
5656
TransactionBodies []BabbageTransactionBody
5757
TransactionWitnessSets []BabbageTransactionWitnessSet
58-
TransactionMetadataSet map[uint]*cbor.LazyValue
58+
TransactionMetadataSet map[uint]common.TransactionMetadataSet
5959
InvalidTransactions []uint
6060
}
6161

@@ -710,7 +710,7 @@ type BabbageTransaction struct {
710710
Body BabbageTransactionBody
711711
WitnessSet BabbageTransactionWitnessSet
712712
TxIsValid bool
713-
TxMetadata *cbor.LazyValue
713+
TxMetadata common.TransactionMetadataSet
714714
}
715715

716716
func (t *BabbageTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -812,7 +812,7 @@ func (t BabbageTransaction) Donation() uint64 {
812812
return t.Body.Donation()
813813
}
814814

815-
func (t BabbageTransaction) Metadata() *cbor.LazyValue {
815+
func (t BabbageTransaction) Metadata() common.TransactionMetadataSet {
816816
return t.TxMetadata
817817
}
818818

@@ -879,7 +879,7 @@ func (t *BabbageTransaction) Cbor() []byte {
879879
t.TxIsValid,
880880
}
881881
if t.TxMetadata != nil {
882-
tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor()))
882+
tmpObj = append(tmpObj, t.TxMetadata)
883883
} else {
884884
tmpObj = append(tmpObj, nil)
885885
}

ledger/byron/byron.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ type ByronTransaction struct {
142142
hash *common.Blake2b256
143143
TxInputs []ByronTransactionInput
144144
TxOutputs []ByronTransactionOutput
145-
Attributes *cbor.LazyValue
145+
Attributes common.TransactionMetadataSet
146146
}
147147

148148
func (t *ByronTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -271,7 +271,7 @@ func (t *ByronTransaction) Donation() uint64 {
271271
return 0
272272
}
273273

274-
func (t *ByronTransaction) Metadata() *cbor.LazyValue {
274+
func (t *ByronTransaction) Metadata() common.TransactionMetadataSet {
275275
return t.Attributes
276276
}
277277

0 commit comments

Comments
 (0)