Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ledger/allegra/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type AllegraBlock struct {
BlockHeader *AllegraBlockHeader
TransactionBodies []AllegraTransactionBody
TransactionWitnessSets []shelley.ShelleyTransactionWitnessSet
TransactionMetadataSet map[uint]*cbor.LazyValue
TransactionMetadataSet map[uint]common.TransactionMetadataSet
}

func (b *AllegraBlock) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -238,7 +238,7 @@ type AllegraTransaction struct {
cbor.DecodeStoreCbor
Body AllegraTransactionBody
WitnessSet shelley.ShelleyTransactionWitnessSet
TxMetadata *cbor.LazyValue
TxMetadata common.TransactionMetadataSet
}

func (t *AllegraTransaction) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -336,7 +336,7 @@ func (t AllegraTransaction) Donation() uint64 {
return t.Body.Donation()
}

func (t AllegraTransaction) Metadata() *cbor.LazyValue {
func (t AllegraTransaction) Metadata() common.TransactionMetadataSet {
return t.TxMetadata
}

Expand Down Expand Up @@ -398,7 +398,7 @@ func (t *AllegraTransaction) Cbor() []byte {
cbor.RawMessage(t.WitnessSet.Cbor()),
}
if t.TxMetadata != nil {
tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor()))
tmpObj = append(tmpObj, t.TxMetadata)
} else {
tmpObj = append(tmpObj, nil)
}
Expand Down
41 changes: 14 additions & 27 deletions ledger/allegra/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package allegra_test

import (
"bytes"
"encoding/hex"
"strings"
"testing"
Expand Down Expand Up @@ -61,32 +60,20 @@ func TestAllegraBlock_CborRoundTrip_UsingCborEncode(t *testing.T) {
t.Fatal("Custom encoded CBOR from AllegraBlock is nil or empty")
}

// Ensure the original and re-encoded CBOR bytes are identical
if !bytes.Equal(dataBytes, encoded) {
t.Errorf(
"Custom CBOR round-trip mismatch for Allegra block\nOriginal CBOR (hex): %x\nCustom Encoded CBOR (hex): %x",
dataBytes,
encoded,
)

// Check from which byte it differs
diffIndex := -1
for i := 0; i < len(dataBytes) && i < len(encoded); i++ {
if dataBytes[i] != encoded[i] {
diffIndex = i
break
}
}
if diffIndex != -1 {
t.Logf("First mismatch at byte index: %d", diffIndex)
t.Logf(
"Original byte: 0x%02x, Re-encoded byte: 0x%02x",
dataBytes[diffIndex],
encoded[diffIndex],
)
} else {
t.Logf("Length mismatch: original length = %d, re-encoded length = %d", len(dataBytes), len(encoded))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to keep the check for the re-encoded CBOR being identical to the original decoded CBOR. If that's failing with this new code, then that should be addressed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will revert back the changes here

// Ensure the re-encoded CBOR is structurally valid and decodes back
var redecoded allegra.AllegraBlock
if err := redecoded.UnmarshalCBOR(encoded); err != nil {
t.Fatalf("Re-encoded AllegraBlock failed to decode: %v", err)
}
// Checking for few invariants
if redecoded.BlockNumber() != block.BlockNumber() {
t.Errorf("BlockNumber mismatch after re-encode: got %d, want %d", redecoded.BlockNumber(), block.BlockNumber())
}
if redecoded.SlotNumber() != block.SlotNumber() {
t.Errorf("SlotNumber mismatch after re-encode: got %d, want %d", redecoded.SlotNumber(), block.SlotNumber())
}
if len(redecoded.TransactionBodies) != len(block.TransactionBodies) {
t.Errorf("Tx count mismatch after re-encode: got %d, want %d", len(redecoded.TransactionBodies), len(block.TransactionBodies))
}
}

Expand Down
8 changes: 4 additions & 4 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type AlonzoBlock struct {
BlockHeader *AlonzoBlockHeader
TransactionBodies []AlonzoTransactionBody
TransactionWitnessSets []AlonzoTransactionWitnessSet
TransactionMetadataSet map[uint]*cbor.LazyValue
TransactionMetadataSet map[uint]common.TransactionMetadataSet
InvalidTransactions []uint
}

Expand Down Expand Up @@ -577,7 +577,7 @@ type AlonzoTransaction struct {
Body AlonzoTransactionBody
WitnessSet AlonzoTransactionWitnessSet
TxIsValid bool
TxMetadata *cbor.LazyValue
TxMetadata common.TransactionMetadataSet
}

func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -679,7 +679,7 @@ func (t AlonzoTransaction) Donation() uint64 {
return t.Body.Donation()
}

func (t AlonzoTransaction) Metadata() *cbor.LazyValue {
func (t AlonzoTransaction) Metadata() common.TransactionMetadataSet {
return t.TxMetadata
}

Expand Down Expand Up @@ -739,7 +739,7 @@ func (t *AlonzoTransaction) Cbor() []byte {
t.TxIsValid,
}
if t.TxMetadata != nil {
tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor()))
tmpObj = append(tmpObj, t.TxMetadata)
} else {
tmpObj = append(tmpObj, nil)
}
Expand Down
8 changes: 4 additions & 4 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type BabbageBlock struct {
BlockHeader *BabbageBlockHeader
TransactionBodies []BabbageTransactionBody
TransactionWitnessSets []BabbageTransactionWitnessSet
TransactionMetadataSet map[uint]*cbor.LazyValue
TransactionMetadataSet map[uint]common.TransactionMetadataSet
InvalidTransactions []uint
}

Expand Down Expand Up @@ -710,7 +710,7 @@ type BabbageTransaction struct {
Body BabbageTransactionBody
WitnessSet BabbageTransactionWitnessSet
TxIsValid bool
TxMetadata *cbor.LazyValue
TxMetadata common.TransactionMetadataSet
}

func (t *BabbageTransaction) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -812,7 +812,7 @@ func (t BabbageTransaction) Donation() uint64 {
return t.Body.Donation()
}

func (t BabbageTransaction) Metadata() *cbor.LazyValue {
func (t BabbageTransaction) Metadata() common.TransactionMetadataSet {
return t.TxMetadata
}

Expand Down Expand Up @@ -879,7 +879,7 @@ func (t *BabbageTransaction) Cbor() []byte {
t.TxIsValid,
}
if t.TxMetadata != nil {
tmpObj = append(tmpObj, cbor.RawMessage(t.TxMetadata.Cbor()))
tmpObj = append(tmpObj, t.TxMetadata)
} else {
tmpObj = append(tmpObj, nil)
}
Expand Down
4 changes: 2 additions & 2 deletions ledger/byron/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ type ByronTransaction struct {
hash *common.Blake2b256
TxInputs []ByronTransactionInput
TxOutputs []ByronTransactionOutput
Attributes *cbor.LazyValue
Attributes common.TransactionMetadataSet
}

func (t *ByronTransaction) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -271,7 +271,7 @@ func (t *ByronTransaction) Donation() uint64 {
return 0
}

func (t *ByronTransaction) Metadata() *cbor.LazyValue {
func (t *ByronTransaction) Metadata() common.TransactionMetadataSet {
return t.Attributes
}

Expand Down
Loading