|
| 1 | +// Copyright 2025 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package leios |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 21 | + "github.com/blinklabs-io/gouroboros/ledger/babbage" |
| 22 | + "github.com/blinklabs-io/gouroboros/ledger/common" |
| 23 | +) |
| 24 | + |
| 25 | +func TestLeiosEra(t *testing.T) { |
| 26 | + if EraLeios.Id != EraIdLeios { |
| 27 | + t.Errorf( |
| 28 | + "Era ID mismatch: expected %d, got %d", |
| 29 | + EraIdLeios, |
| 30 | + EraLeios.Id, |
| 31 | + ) |
| 32 | + } |
| 33 | + if EraLeios.Name != EraNameLeios { |
| 34 | + t.Errorf( |
| 35 | + "Era name mismatch: expected %s, got %s", |
| 36 | + EraNameLeios, |
| 37 | + EraLeios.Name, |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestLeiosBlockTypes(t *testing.T) { |
| 43 | + if BlockTypeLeiosRanking != 8 { |
| 44 | + t.Errorf( |
| 45 | + "BlockTypeLeiosRanking mismatch: expected 8, got %d", |
| 46 | + BlockTypeLeiosRanking, |
| 47 | + ) |
| 48 | + } |
| 49 | + if BlockTypeLeiosEndorser != 9 { |
| 50 | + t.Errorf( |
| 51 | + "BlockTypeLeiosEndorser mismatch: expected 9, got %d", |
| 52 | + BlockTypeLeiosEndorser, |
| 53 | + ) |
| 54 | + } |
| 55 | + if BlockHeaderTypeLeios != 7 { |
| 56 | + t.Errorf( |
| 57 | + "BlockHeaderTypeLeios mismatch: expected 7, got %d", |
| 58 | + BlockHeaderTypeLeios, |
| 59 | + ) |
| 60 | + } |
| 61 | + if TxTypeLeios != 7 { |
| 62 | + t.Errorf("TxTypeLeios mismatch: expected 7, got %d", TxTypeLeios) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestLeiosEndorserBlock_Type(t *testing.T) { |
| 67 | + block := &LeiosEndorserBlock{} |
| 68 | + if block.Type() != BlockTypeLeiosEndorser { |
| 69 | + t.Errorf( |
| 70 | + "LeiosEndorserBlock.Type() mismatch: expected %d, got %d", |
| 71 | + BlockTypeLeiosEndorser, |
| 72 | + block.Type(), |
| 73 | + ) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestLeiosRankingBlock_Type(t *testing.T) { |
| 78 | + block := &LeiosRankingBlock{} |
| 79 | + if block.Type() != BlockTypeLeiosRanking { |
| 80 | + t.Errorf( |
| 81 | + "LeiosRankingBlock.Type() mismatch: expected %d, got %d", |
| 82 | + BlockTypeLeiosRanking, |
| 83 | + block.Type(), |
| 84 | + ) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestLeiosBlockHeader_Era(t *testing.T) { |
| 89 | + header := &LeiosBlockHeader{} |
| 90 | + if header.Era() != EraLeios { |
| 91 | + t.Errorf( |
| 92 | + "LeiosBlockHeader.Era() mismatch: expected %v, got %v", |
| 93 | + EraLeios, |
| 94 | + header.Era(), |
| 95 | + ) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestLeiosEndorserBlock_Era(t *testing.T) { |
| 100 | + block := &LeiosEndorserBlock{} |
| 101 | + if block.Era() != EraLeios { |
| 102 | + t.Errorf( |
| 103 | + "LeiosEndorserBlock.Era() mismatch: expected %v, got %v", |
| 104 | + EraLeios, |
| 105 | + block.Era(), |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestLeiosRankingBlock_Era(t *testing.T) { |
| 111 | + block := &LeiosRankingBlock{} |
| 112 | + if block.Era() != EraLeios { |
| 113 | + t.Errorf( |
| 114 | + "LeiosRankingBlock.Era() mismatch: expected %v, got %v", |
| 115 | + EraLeios, |
| 116 | + block.Era(), |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestLeiosEndorserBlock_Hash(t *testing.T) { |
| 122 | + block := &LeiosEndorserBlock{ |
| 123 | + TxReferences: map[common.Blake2b256]uint16{ |
| 124 | + {0x01, 0x02}: 100, |
| 125 | + {0x03, 0x04}: 200, |
| 126 | + }, |
| 127 | + } |
| 128 | + hash := block.Hash() |
| 129 | + if hash == (common.Blake2b256{}) { |
| 130 | + t.Error("LeiosEndorserBlock.Hash() returned zero hash") |
| 131 | + } |
| 132 | + // Hash should be consistent |
| 133 | + hash2 := block.Hash() |
| 134 | + if hash != hash2 { |
| 135 | + t.Error("LeiosEndorserBlock.Hash() not consistent") |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestLeiosBlockHeader_Hash(t *testing.T) { |
| 140 | + header := &LeiosBlockHeader{ |
| 141 | + Body: LeiosBlockHeaderBody{ |
| 142 | + BabbageBlockHeaderBody: babbage.BabbageBlockHeaderBody{ |
| 143 | + BlockNumber: 1, |
| 144 | + Slot: 100, |
| 145 | + PrevHash: common.Blake2b256{0x01}, |
| 146 | + BlockBodySize: 1000, |
| 147 | + BlockBodyHash: common.Blake2b256{0x02}, |
| 148 | + }, |
| 149 | + }, |
| 150 | + } |
| 151 | + hash := header.Hash() |
| 152 | + if hash == (common.Blake2b256{}) { |
| 153 | + t.Error("LeiosBlockHeader.Hash() returned zero hash") |
| 154 | + } |
| 155 | + // Hash should be consistent |
| 156 | + hash2 := header.Hash() |
| 157 | + if hash != hash2 { |
| 158 | + t.Error("LeiosBlockHeader.Hash() not consistent") |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestLeiosEndorserBlock_CborRoundTrip(t *testing.T) { |
| 163 | + original := &LeiosEndorserBlock{ |
| 164 | + TxReferences: map[common.Blake2b256]uint16{ |
| 165 | + {0x01, 0x02}: 100, |
| 166 | + {0x03, 0x04}: 200, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + // Encode to CBOR |
| 171 | + cborData, err := cbor.Encode(original) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("Failed to encode LeiosEndorserBlock: %v", err) |
| 174 | + } |
| 175 | + |
| 176 | + // Decode back |
| 177 | + var decoded LeiosEndorserBlock |
| 178 | + if _, err := cbor.Decode(cborData, &decoded); err != nil { |
| 179 | + t.Fatalf("Failed to decode LeiosEndorserBlock: %v", err) |
| 180 | + } |
| 181 | + |
| 182 | + // Check TxReferences |
| 183 | + if len(decoded.TxReferences) != len(original.TxReferences) { |
| 184 | + t.Errorf( |
| 185 | + "TxReferences length mismatch: expected %d, got %d", |
| 186 | + len(original.TxReferences), |
| 187 | + len(decoded.TxReferences), |
| 188 | + ) |
| 189 | + } |
| 190 | + for hash, size := range original.TxReferences { |
| 191 | + if decoded.TxReferences[hash] != size { |
| 192 | + t.Errorf( |
| 193 | + "TxReference mismatch for hash %x: expected %d, got %d", |
| 194 | + hash, |
| 195 | + size, |
| 196 | + decoded.TxReferences[hash], |
| 197 | + ) |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments