Skip to content

Commit 1cf067b

Browse files
committed
feat(ledger): validate metadata during rule checks
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 7524697 commit 1cf067b

File tree

21 files changed

+242
-12
lines changed

21 files changed

+242
-12
lines changed

ledger/allegra/allegra.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ type AllegraTransaction struct {
259259
Body AllegraTransactionBody
260260
WitnessSet shelley.ShelleyTransactionWitnessSet
261261
TxMetadata common.TransactionMetadatum
262+
RawAuxData []byte // Raw auxiliary data bytes (includes scripts)
262263
}
263264

264265
func (t *AllegraTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -282,8 +283,10 @@ func (t *AllegraTransaction) UnmarshalCBOR(cborData []byte) error {
282283
return fmt.Errorf("failed to decode transaction witness set: %w", err)
283284
}
284285
// Handle metadata (component 3, index 2) - always present, but may be CBOR nil
285-
// DecodeAuxiliaryDataToMetadata already preserves raw bytes via DecodeMetadatumRaw
286-
if len(txArray) > 2 && len(txArray[2]) > 0 {
286+
// Store raw auxiliary data bytes (including any scripts)
287+
if len(txArray) > 2 && len(txArray[2]) > 0 && txArray[2][0] != 0xF6 { // 0xF6 is CBOR null
288+
t.RawAuxData = []byte(txArray[2])
289+
// Also extract metadata
287290
metadata, err := common.DecodeAuxiliaryDataToMetadata(txArray[2])
288291
if err == nil && metadata != nil {
289292
t.TxMetadata = metadata
@@ -393,6 +396,10 @@ func (t *AllegraTransaction) Metadata() common.TransactionMetadatum {
393396
return t.TxMetadata
394397
}
395398

399+
func (t *AllegraTransaction) RawAuxiliaryData() []byte {
400+
return t.RawAuxData
401+
}
402+
396403
func (t AllegraTransaction) IsValid() bool {
397404
return true
398405
}

ledger/allegra/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package allegra
1616

1717
import (
1818
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/ledger/common"
1921
)
2022

2123
type OutsideValidityIntervalUtxoError struct {
@@ -30,3 +32,11 @@ func (e OutsideValidityIntervalUtxoError) Error() string {
3032
e.Slot,
3133
)
3234
}
35+
36+
// Metadata validation errors are now in common package
37+
// Type aliases for backward compatibility
38+
type (
39+
MissingTransactionMetadataError = common.MissingTransactionMetadataError
40+
MissingTransactionAuxiliaryDataHashError = common.MissingTransactionAuxiliaryDataHashError
41+
ConflictingMetadataHashError = common.ConflictingMetadataHashError
42+
)

ledger/allegra/rules.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
)
2323

2424
var UtxoValidationRules = []common.UtxoValidationRuleFunc{
25+
UtxoValidateMetadata,
2526
UtxoValidateOutsideValidityIntervalUtxo,
2627
UtxoValidateInputSetEmptyUtxo,
2728
UtxoValidateFeeTooSmallUtxo,
@@ -167,3 +168,12 @@ func UtxoValidateMaxTxSizeUtxo(
167168
tmpPparams,
168169
)
169170
}
171+
172+
func UtxoValidateMetadata(
173+
tx common.Transaction,
174+
slot uint64,
175+
ls common.LedgerState,
176+
pp common.ProtocolParameters,
177+
) error {
178+
return shelley.UtxoValidateMetadata(tx, slot, ls, pp)
179+
}

ledger/alonzo/alonzo.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ type AlonzoTransaction struct {
655655
WitnessSet AlonzoTransactionWitnessSet
656656
TxIsValid bool
657657
TxMetadata common.TransactionMetadatum
658+
RawAuxData []byte // Raw auxiliary data bytes (includes scripts)
658659
}
659660

660661
func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -688,8 +689,8 @@ func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
688689
}
689690

690691
// Handle metadata (component 4, always present - either data or CBOR nil)
691-
// DecodeAuxiliaryDataToMetadata already preserves raw bytes via DecodeMetadatumRaw
692-
if len(txArray) > 3 && len(txArray[3]) > 0 {
692+
if len(txArray[3]) > 0 && txArray[3][0] != 0xF6 { // 0xF6 is CBOR null
693+
t.RawAuxData = []byte(txArray[3])
693694
metadata, err := common.DecodeAuxiliaryDataToMetadata(txArray[3])
694695
if err == nil && metadata != nil {
695696
t.TxMetadata = metadata
@@ -704,6 +705,10 @@ func (t *AlonzoTransaction) Metadata() common.TransactionMetadatum {
704705
return t.TxMetadata
705706
}
706707

708+
func (t *AlonzoTransaction) RawAuxiliaryData() []byte {
709+
return t.RawAuxData
710+
}
711+
707712
func (AlonzoTransaction) Type() int {
708713
return TxTypeAlonzo
709714
}

ledger/alonzo/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ type NoCollateralInputsError struct{}
6464
func (NoCollateralInputsError) Error() string {
6565
return "no collateral inputs"
6666
}
67+
68+
// Metadata validation errors are now in common package
69+
// Type aliases for backward compatibility
70+
type (
71+
MissingTransactionMetadataError = common.MissingTransactionMetadataError
72+
MissingTransactionAuxiliaryDataHashError = common.MissingTransactionAuxiliaryDataHashError
73+
ConflictingMetadataHashError = common.ConflictingMetadataHashError
74+
)

ledger/alonzo/rules.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
)
2626

2727
var UtxoValidationRules = []common.UtxoValidationRuleFunc{
28+
UtxoValidateMetadata,
2829
UtxoValidateOutsideValidityIntervalUtxo,
2930
UtxoValidateInputSetEmptyUtxo,
3031
UtxoValidateFeeTooSmallUtxo,
@@ -428,3 +429,12 @@ func MinCoinTxOut(
428429
minCoinTxOut := uint64(tmpPparams.MinUtxoValue)
429430
return minCoinTxOut, nil
430431
}
432+
433+
func UtxoValidateMetadata(
434+
tx common.Transaction,
435+
slot uint64,
436+
ls common.LedgerState,
437+
pp common.ProtocolParameters,
438+
) error {
439+
return shelley.UtxoValidateMetadata(tx, slot, ls, pp)
440+
}

ledger/babbage/babbage.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ type BabbageTransaction struct {
886886
WitnessSet BabbageTransactionWitnessSet
887887
TxIsValid bool
888888
TxMetadata common.TransactionMetadatum
889+
RawAuxData []byte // Raw auxiliary data bytes (includes scripts)
889890
}
890891

891892
func (t *BabbageTransaction) UnmarshalCBOR(cborData []byte) error {
@@ -919,8 +920,8 @@ func (t *BabbageTransaction) UnmarshalCBOR(cborData []byte) error {
919920
}
920921

921922
// Handle metadata (component 4, always present - either data or CBOR nil)
922-
// DecodeAuxiliaryDataToMetadata already preserves raw bytes via DecodeMetadatumRaw
923-
if len(txArray[3]) > 0 {
923+
if len(txArray[3]) > 0 && txArray[3][0] != 0xF6 { // 0xF6 is CBOR null
924+
t.RawAuxData = []byte(txArray[3])
924925
metadata, err := common.DecodeAuxiliaryDataToMetadata(txArray[3])
925926
if err == nil && metadata != nil {
926927
t.TxMetadata = metadata
@@ -935,6 +936,10 @@ func (t *BabbageTransaction) Metadata() common.TransactionMetadatum {
935936
return t.TxMetadata
936937
}
937938

939+
func (t *BabbageTransaction) RawAuxiliaryData() []byte {
940+
return t.RawAuxData
941+
}
942+
938943
func (BabbageTransaction) Type() int {
939944
return TxTypeBabbage
940945
}

ledger/babbage/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package babbage
1616

1717
import (
1818
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/ledger/common"
1921
)
2022

2123
type TooManyCollateralInputsError struct {
@@ -43,3 +45,11 @@ func (e IncorrectTotalCollateralFieldError) Error() string {
4345
e.TotalCollateral,
4446
)
4547
}
48+
49+
// Metadata validation errors are now in common package
50+
// Type aliases for backward compatibility
51+
type (
52+
MissingTransactionMetadataError = common.MissingTransactionMetadataError
53+
MissingTransactionAuxiliaryDataHashError = common.MissingTransactionAuxiliaryDataHashError
54+
ConflictingMetadataHashError = common.ConflictingMetadataHashError
55+
)

ledger/babbage/rules.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
)
2727

2828
var UtxoValidationRules = []common.UtxoValidationRuleFunc{
29+
UtxoValidateMetadata,
2930
UtxoValidateOutsideValidityIntervalUtxo,
3031
UtxoValidateInputSetEmptyUtxo,
3132
UtxoValidateFeeTooSmallUtxo,
@@ -499,3 +500,12 @@ func MinCoinTxOut(
499500
minCoinTxOut := tmpPparams.AdaPerUtxoByte * uint64(len(txOutBytes))
500501
return minCoinTxOut, nil
501502
}
503+
504+
func UtxoValidateMetadata(
505+
tx common.Transaction,
506+
slot uint64,
507+
ls common.LedgerState,
508+
pp common.ProtocolParameters,
509+
) error {
510+
return shelley.UtxoValidateMetadata(tx, slot, ls, pp)
511+
}

ledger/byron/byron.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ func (t *ByronTransaction) Metadata() common.TransactionMetadatum {
292292
return nil
293293
}
294294

295+
func (t *ByronTransaction) RawAuxiliaryData() []byte {
296+
return nil
297+
}
298+
295299
func (t *ByronTransaction) LeiosHash() common.Blake2b256 {
296300
if t.hash == nil {
297301
tmpHash := common.Blake2b256Hash(t.Cbor())

0 commit comments

Comments
 (0)