|
15 | 15 | package allegra_test |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "bytes" |
19 | 18 | "encoding/hex" |
20 | 19 | "strings" |
21 | 20 | "testing" |
@@ -61,32 +60,20 @@ func TestAllegraBlock_CborRoundTrip_UsingCborEncode(t *testing.T) { |
61 | 60 | t.Fatal("Custom encoded CBOR from AllegraBlock is nil or empty") |
62 | 61 | } |
63 | 62 |
|
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)) |
90 | 77 | } |
91 | 78 | } |
92 | 79 |
|
|
0 commit comments