Skip to content

Commit 154602a

Browse files
committed
fix(ledger): use raw bytes for metadata
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent ac6eddd commit 154602a

File tree

9 files changed

+500
-102
lines changed

9 files changed

+500
-102
lines changed

ledger/allegra/allegra.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func (b *AllegraBlock) UnmarshalCBOR(cborData []byte) error {
6161
}
6262
*b = AllegraBlock(tmp)
6363
b.SetCbor(cborData)
64+
65+
// Extract and store CBOR for each component
66+
if err := common.ExtractTransactionCbor(cborData, b.TransactionBodies, b.TransactionWitnessSets); err != nil {
67+
return err
68+
}
6469
return nil
6570
}
6671

@@ -104,13 +109,14 @@ func (b *AllegraBlock) Transactions() []common.Transaction {
104109
ret := make([]common.Transaction, len(b.TransactionBodies))
105110
// #nosec G115
106111
for idx := range b.TransactionBodies {
107-
// Note: Ignoring the presence flag; if distinguishing "missing" vs "present but empty/failed decode" is needed, plumb the second return value through
108-
txMetadata, _ := b.TransactionMetadataSet.GetMetadata(uint(idx))
109-
ret[idx] = &AllegraTransaction{
112+
tx := &AllegraTransaction{
110113
Body: b.TransactionBodies[idx],
111114
WitnessSet: b.TransactionWitnessSets[idx],
112-
TxMetadata: txMetadata,
113115
}
116+
if metadata, ok := b.TransactionMetadataSet.GetMetadata(uint(idx)); ok {
117+
tx.TxMetadata = metadata
118+
}
119+
ret[idx] = tx
114120
}
115121
return ret
116122
}
@@ -250,12 +256,25 @@ type AllegraTransaction struct {
250256
}
251257

252258
func (t *AllegraTransaction) UnmarshalCBOR(cborData []byte) error {
253-
type tAllegraTransaction AllegraTransaction
254-
var tmp tAllegraTransaction
255-
if _, err := cbor.Decode(cborData, &tmp); err != nil {
259+
// Decode as raw array to capture metadata bytes
260+
var txArray []cbor.RawMessage
261+
if _, err := cbor.Decode(cborData, &txArray); err != nil {
256262
return err
257263
}
258-
*t = AllegraTransaction(tmp)
264+
if len(txArray) < 2 {
265+
return fmt.Errorf(
266+
"invalid transaction: expected at least 2 components, got %d",
267+
len(txArray),
268+
)
269+
}
270+
// Decode body and witness set
271+
if _, err := cbor.Decode(txArray[0], &t.Body); err != nil {
272+
return fmt.Errorf("failed to decode transaction body: %w", err)
273+
}
274+
if _, err := cbor.Decode(txArray[1], &t.WitnessSet); err != nil {
275+
return fmt.Errorf("failed to decode transaction witness set: %w", err)
276+
}
277+
// Metadata will be decoded lazily in Metadata() method
259278
t.SetCbor(cborData)
260279
return nil
261280
}
@@ -356,8 +375,25 @@ func (t AllegraTransaction) Donation() uint64 {
356375
return t.Body.Donation()
357376
}
358377

359-
func (t AllegraTransaction) Metadata() common.TransactionMetadatum {
360-
return t.TxMetadata
378+
func (t *AllegraTransaction) Metadata() common.TransactionMetadatum {
379+
if t.TxMetadata != nil {
380+
return t.TxMetadata
381+
}
382+
// Decode from stored CBOR
383+
cborData := t.Cbor()
384+
var txArray []cbor.RawMessage
385+
if _, err := cbor.Decode(cborData, &txArray); err != nil {
386+
return nil
387+
}
388+
if len(txArray) < 3 {
389+
return nil
390+
}
391+
metadata, err := common.DecodeAuxiliaryDataToMetadata(txArray[2])
392+
if err != nil {
393+
return nil
394+
}
395+
t.TxMetadata = metadata
396+
return metadata
361397
}
362398

363399
func (t AllegraTransaction) IsValid() bool {
@@ -417,8 +453,12 @@ func (t *AllegraTransaction) Cbor() []byte {
417453
cbor.RawMessage(t.Body.Cbor()),
418454
cbor.RawMessage(t.WitnessSet.Cbor()),
419455
}
456+
var metadataRaw []byte
420457
if t.TxMetadata != nil {
421-
tmpObj = append(tmpObj, t.TxMetadata)
458+
metadataRaw = t.TxMetadata.Cbor()
459+
}
460+
if len(metadataRaw) > 0 {
461+
tmpObj = append(tmpObj, cbor.RawMessage(metadataRaw))
422462
} else {
423463
tmpObj = append(tmpObj, nil)
424464
}

ledger/alonzo/alonzo.go

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func (b *AlonzoBlock) UnmarshalCBOR(cborData []byte) error {
6868
}
6969
*b = AlonzoBlock(tmp)
7070
b.SetCbor(cborData)
71+
72+
// Extract and store CBOR for each component
73+
if err := common.ExtractTransactionCbor(cborData, b.TransactionBodies, b.TransactionWitnessSets); err != nil {
74+
return err
75+
}
7176
return nil
7277
}
7378

@@ -156,14 +161,15 @@ func (b *AlonzoBlock) Transactions() []common.Transaction {
156161
ret := make([]common.Transaction, len(b.TransactionBodies))
157162
// #nosec G115
158163
for idx := range b.TransactionBodies {
159-
// Note: Ignoring the presence flag; if distinguishing "missing" vs "present but empty/failed decode" is needed, plumb the second return value through
160-
txMetadata, _ := b.TransactionMetadataSet.GetMetadata(uint(idx))
161-
ret[idx] = &AlonzoTransaction{
164+
tx := &AlonzoTransaction{
162165
Body: b.TransactionBodies[idx],
163166
WitnessSet: b.TransactionWitnessSets[idx],
164-
TxMetadata: txMetadata,
165167
TxIsValid: !invalidTxMap[uint(idx)],
166168
}
169+
if metadata, ok := b.TransactionMetadataSet.GetMetadata(uint(idx)); ok {
170+
tx.TxMetadata = metadata
171+
}
172+
ret[idx] = tx
167173
}
168174
return ret
169175
}
@@ -646,16 +652,61 @@ type AlonzoTransaction struct {
646652
}
647653

648654
func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
649-
type tAlonzoTransaction AlonzoTransaction
650-
var tmp tAlonzoTransaction
651-
if _, err := cbor.Decode(cborData, &tmp); err != nil {
655+
// Decode as raw array to preserve metadata bytes
656+
var txArray []cbor.RawMessage
657+
if _, err := cbor.Decode(cborData, &txArray); err != nil {
652658
return err
653659
}
654-
*t = AlonzoTransaction(tmp)
660+
661+
// Ensure we have at least 3 components
662+
if len(txArray) < 3 {
663+
return fmt.Errorf(
664+
"invalid transaction: expected at least 3 components, got %d",
665+
len(txArray),
666+
)
667+
}
668+
669+
// Decode body
670+
if _, err := cbor.Decode([]byte(txArray[0]), &t.Body); err != nil {
671+
return fmt.Errorf("failed to decode transaction body: %w", err)
672+
}
673+
674+
// Decode witness set
675+
if _, err := cbor.Decode([]byte(txArray[1]), &t.WitnessSet); err != nil {
676+
return fmt.Errorf("failed to decode transaction witness set: %w", err)
677+
}
678+
679+
// Decode TxIsValid flag
680+
if _, err := cbor.Decode([]byte(txArray[2]), &t.TxIsValid); err != nil {
681+
return fmt.Errorf("failed to decode TxIsValid: %w", err)
682+
}
683+
655684
t.SetCbor(cborData)
656685
return nil
657686
}
658687

688+
func (t *AlonzoTransaction) Metadata() common.TransactionMetadatum {
689+
if t.TxMetadata != nil {
690+
return t.TxMetadata
691+
}
692+
// Decode from stored CBOR
693+
cborData := t.Cbor()
694+
var txArray []cbor.RawMessage
695+
if _, err := cbor.Decode(cborData, &txArray); err != nil {
696+
return nil
697+
}
698+
// auxiliary_data? is the 4th component: [body, witness_set, is_valid, auxiliary_data?]
699+
if len(txArray) < 4 {
700+
return nil
701+
}
702+
metadata, err := common.DecodeAuxiliaryDataToMetadata(txArray[3])
703+
if err != nil {
704+
return nil
705+
}
706+
t.TxMetadata = metadata
707+
return metadata
708+
}
709+
659710
func (AlonzoTransaction) Type() int {
660711
return TxTypeAlonzo
661712
}
@@ -756,10 +807,6 @@ func (t AlonzoTransaction) Donation() uint64 {
756807
return t.Body.Donation()
757808
}
758809

759-
func (t AlonzoTransaction) Metadata() common.TransactionMetadatum {
760-
return t.TxMetadata
761-
}
762-
763810
func (t AlonzoTransaction) IsValid() bool {
764811
return t.TxIsValid
765812
}
@@ -815,8 +862,12 @@ func (t *AlonzoTransaction) Cbor() []byte {
815862
cbor.RawMessage(t.WitnessSet.Cbor()),
816863
t.TxIsValid,
817864
}
865+
var metadataRaw []byte
818866
if t.TxMetadata != nil {
819-
tmpObj = append(tmpObj, t.TxMetadata)
867+
metadataRaw = t.TxMetadata.Cbor()
868+
}
869+
if len(metadataRaw) > 0 {
870+
tmpObj = append(tmpObj, cbor.RawMessage(metadataRaw))
820871
} else {
821872
tmpObj = append(tmpObj, nil)
822873
}

ledger/babbage/babbage.go

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ func (b *BabbageBlock) UnmarshalCBOR(cborData []byte) error {
118118
b.TransactionMetadataSet = tmp.TransactionMetadataSet
119119

120120
b.SetCbor(cborData)
121+
122+
// Extract and store CBOR for each component
123+
if err := common.ExtractTransactionCbor(cborData, b.TransactionBodies, b.TransactionWitnessSets); err != nil {
124+
return err
125+
}
121126
return nil
122127
}
123128

@@ -187,14 +192,15 @@ func (b *BabbageBlock) Transactions() []common.Transaction {
187192
ret := make([]common.Transaction, len(b.TransactionBodies))
188193
// #nosec G115
189194
for idx := range b.TransactionBodies {
190-
// Note: Ignoring the presence flag; if distinguishing "missing" vs "present but empty/failed decode" is needed, plumb the second return value through
191-
txMetadata, _ := b.TransactionMetadataSet.GetMetadata(uint(idx))
192-
ret[idx] = &BabbageTransaction{
195+
tx := &BabbageTransaction{
193196
Body: b.TransactionBodies[idx],
194197
WitnessSet: b.TransactionWitnessSets[idx],
195-
TxMetadata: txMetadata,
196198
TxIsValid: !invalidTxMap[uint(idx)],
197199
}
200+
if metadata, ok := b.TransactionMetadataSet.GetMetadata(uint(idx)); ok {
201+
tx.TxMetadata = metadata
202+
}
203+
ret[idx] = tx
198204
}
199205
return ret
200206
}
@@ -877,16 +883,63 @@ type BabbageTransaction struct {
877883
}
878884

879885
func (t *BabbageTransaction) UnmarshalCBOR(cborData []byte) error {
880-
type tBabbageTransaction BabbageTransaction
881-
var tmp tBabbageTransaction
882-
if _, err := cbor.Decode(cborData, &tmp); err != nil {
886+
// Decode as raw array to preserve metadata bytes
887+
var txArray []cbor.RawMessage
888+
if _, err := cbor.Decode(cborData, &txArray); err != nil {
883889
return err
884890
}
885-
*t = BabbageTransaction(tmp)
891+
892+
// Ensure we have at least 3 components
893+
if len(txArray) < 3 {
894+
return fmt.Errorf(
895+
"invalid transaction: expected at least 3 components, got %d",
896+
len(txArray),
897+
)
898+
}
899+
900+
// Decode body
901+
if _, err := cbor.Decode([]byte(txArray[0]), &t.Body); err != nil {
902+
return fmt.Errorf("failed to decode transaction body: %w", err)
903+
}
904+
905+
// Decode witness set
906+
if _, err := cbor.Decode([]byte(txArray[1]), &t.WitnessSet); err != nil {
907+
return fmt.Errorf("failed to decode transaction witness set: %w", err)
908+
}
909+
910+
// Decode TxIsValid flag
911+
if _, err := cbor.Decode([]byte(txArray[2]), &t.TxIsValid); err != nil {
912+
return fmt.Errorf("failed to decode TxIsValid: %w", err)
913+
}
914+
915+
// Handle auxiliary data (component 3) - will be decoded lazily
916+
886917
t.SetCbor(cborData)
887918
return nil
888919
}
889920

921+
func (t *BabbageTransaction) Metadata() common.TransactionMetadatum {
922+
if t.TxMetadata != nil {
923+
return t.TxMetadata
924+
}
925+
// Decode from stored CBOR
926+
cborData := t.Cbor()
927+
var txArray []cbor.RawMessage
928+
if _, err := cbor.Decode(cborData, &txArray); err != nil {
929+
return nil
930+
}
931+
// auxiliary_data? is the 4th component: [body, witness_set, is_valid, auxiliary_data?]
932+
if len(txArray) < 4 {
933+
return nil
934+
}
935+
metadata, err := common.DecodeAuxiliaryDataToMetadata(txArray[3])
936+
if err != nil {
937+
return nil
938+
}
939+
t.TxMetadata = metadata
940+
return metadata
941+
}
942+
890943
func (BabbageTransaction) Type() int {
891944
return TxTypeBabbage
892945
}
@@ -987,10 +1040,6 @@ func (t BabbageTransaction) Donation() uint64 {
9871040
return t.Body.Donation()
9881041
}
9891042

990-
func (t BabbageTransaction) Metadata() common.TransactionMetadatum {
991-
return t.TxMetadata
992-
}
993-
9941043
func (t BabbageTransaction) IsValid() bool {
9951044
return t.TxIsValid
9961045
}
@@ -1053,8 +1102,12 @@ func (t *BabbageTransaction) Cbor() []byte {
10531102
cbor.RawMessage(t.WitnessSet.Cbor()),
10541103
t.TxIsValid,
10551104
}
1105+
var metadataRaw []byte
10561106
if t.TxMetadata != nil {
1057-
tmpObj = append(tmpObj, t.TxMetadata)
1107+
metadataRaw = t.TxMetadata.Cbor()
1108+
}
1109+
if len(metadataRaw) > 0 {
1110+
tmpObj = append(tmpObj, cbor.RawMessage(metadataRaw))
10581111
} else {
10591112
tmpObj = append(tmpObj, nil)
10601113
}

0 commit comments

Comments
 (0)