|
| 1 | +package stateless |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 8 | + "github.com/ethereum/go-ethereum/core/types" |
| 9 | + "github.com/ethereum/go-ethereum/rlp" |
| 10 | +) |
| 11 | + |
| 12 | +// TestDecodeRLP_BorWitnessFormat verifies that a witness RLP-encoded in the |
| 13 | +// canonical 3-field BorWitness format is correctly decoded. |
| 14 | +func TestDecodeRLP_BorWitnessFormat(t *testing.T) { |
| 15 | + contextHeader := &types.Header{Number: big.NewInt(100)} |
| 16 | + parentHeader := &types.Header{Number: big.NewInt(99)} |
| 17 | + node1 := []byte("statenode1") |
| 18 | + node2 := []byte("statenode2") |
| 19 | + |
| 20 | + bw := &BorWitness{ |
| 21 | + Context: contextHeader, |
| 22 | + Headers: []*types.Header{parentHeader}, |
| 23 | + State: [][]byte{node1, node2}, |
| 24 | + } |
| 25 | + |
| 26 | + encoded, err := rlp.EncodeToBytes(bw) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("failed to encode BorWitness: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + var w Witness |
| 32 | + if err := rlp.DecodeBytes(encoded, &w); err != nil { |
| 33 | + t.Fatalf("DecodeRLP failed for BorWitness input: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + if w.context.Number.Cmp(contextHeader.Number) != 0 { |
| 37 | + t.Errorf("context number: got %v, want %v", w.context.Number, contextHeader.Number) |
| 38 | + } |
| 39 | + if len(w.Headers) != 1 || w.Headers[0].Number.Cmp(parentHeader.Number) != 0 { |
| 40 | + t.Errorf("headers mismatch after decode") |
| 41 | + } |
| 42 | + |
| 43 | + // All State items from BorWitness should land in w.State. |
| 44 | + if len(w.State) != 2 { |
| 45 | + t.Errorf("state len: got %d, want 2", len(w.State)) |
| 46 | + } |
| 47 | + for _, node := range [][]byte{node1, node2} { |
| 48 | + if _, ok := w.State[string(node)]; !ok { |
| 49 | + t.Errorf("state node %q missing after decode", node) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Codes are not part of the BorWitness wire format. |
| 54 | + if len(w.Codes) != 0 { |
| 55 | + t.Errorf("Codes should be empty after BorWitness decode, got %d entries", len(w.Codes)) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// TestDecodeRLP_ExtWitnessFormat verifies backward compatibility: a witness |
| 60 | +// encoded in the legacy 5-field ExtWitness format is correctly decoded via the |
| 61 | +// fallback path, with Codes and State placed in their respective internal maps. |
| 62 | +func TestDecodeRLP_ExtWitnessFormat(t *testing.T) { |
| 63 | + contextHeader := &types.Header{Number: big.NewInt(100)} |
| 64 | + parentHeader := &types.Header{Number: big.NewInt(99)} |
| 65 | + code1 := hexutil.Bytes("contractbytecode") |
| 66 | + node1 := hexutil.Bytes("statetrienode") |
| 67 | + |
| 68 | + ext := &ExtWitness{ |
| 69 | + Context: contextHeader, |
| 70 | + Headers: []*types.Header{parentHeader}, |
| 71 | + Codes: []hexutil.Bytes{code1}, |
| 72 | + State: []hexutil.Bytes{node1}, |
| 73 | + Keys: nil, |
| 74 | + } |
| 75 | + |
| 76 | + encoded, err := rlp.EncodeToBytes(ext) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("failed to encode ExtWitness: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + var w Witness |
| 82 | + if err := rlp.DecodeBytes(encoded, &w); err != nil { |
| 83 | + t.Fatalf("DecodeRLP failed for ExtWitness input: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + if w.context.Number.Cmp(contextHeader.Number) != 0 { |
| 87 | + t.Errorf("context number: got %v, want %v", w.context.Number, contextHeader.Number) |
| 88 | + } |
| 89 | + if len(w.Headers) != 1 || w.Headers[0].Number.Cmp(parentHeader.Number) != 0 { |
| 90 | + t.Errorf("headers mismatch after decode") |
| 91 | + } |
| 92 | + |
| 93 | + if len(w.Codes) != 1 { |
| 94 | + t.Errorf("Codes len: got %d, want 1", len(w.Codes)) |
| 95 | + } |
| 96 | + if _, ok := w.Codes[string(code1)]; !ok { |
| 97 | + t.Errorf("code %q missing from Codes after ExtWitness decode", code1) |
| 98 | + } |
| 99 | + |
| 100 | + if len(w.State) != 1 { |
| 101 | + t.Errorf("State len: got %d, want 1", len(w.State)) |
| 102 | + } |
| 103 | + if _, ok := w.State[string(node1)]; !ok { |
| 104 | + t.Errorf("node %q missing from State after ExtWitness decode", node1) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// TestEncodeRLP_UsesBorWitnessFormat verifies that EncodeRLP produces the |
| 109 | +// canonical 3-field BorWitness format and that codes are excluded from it. |
| 110 | +func TestEncodeRLP_UsesBorWitnessFormat(t *testing.T) { |
| 111 | + w := &Witness{ |
| 112 | + context: &types.Header{Number: big.NewInt(100)}, |
| 113 | + Headers: []*types.Header{{Number: big.NewInt(99)}}, |
| 114 | + Codes: map[string]struct{}{"contractcode": {}}, |
| 115 | + State: map[string]struct{}{"statenode": {}}, |
| 116 | + } |
| 117 | + |
| 118 | + encoded, err := rlp.EncodeToBytes(w) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("EncodeRLP failed: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // The output must be decodable as BorWitness (3 fields). |
| 124 | + var bw BorWitness |
| 125 | + if err := rlp.DecodeBytes(encoded, &bw); err != nil { |
| 126 | + t.Fatalf("encoded output is not a valid BorWitness: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Only State nodes should be present — codes are not encoded. |
| 130 | + if len(bw.State) != 1 || string(bw.State[0]) != "statenode" { |
| 131 | + t.Errorf("BorWitness.State = %v, want [statenode]", bw.State) |
| 132 | + } |
| 133 | + |
| 134 | + // The output must NOT be decodable as ExtWitness (5 fields). |
| 135 | + var ext ExtWitness |
| 136 | + if err := rlp.DecodeBytes(encoded, &ext); err == nil { |
| 137 | + t.Error("expected ExtWitness decode to fail for BorWitness output, but it succeeded") |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// TestRoundtrip_BorWitnessFormat verifies the full encode→decode cycle using |
| 142 | +// EncodeRLP and DecodeRLP (BorWitness path). |
| 143 | +func TestRoundtrip_BorWitnessFormat(t *testing.T) { |
| 144 | + original := &Witness{ |
| 145 | + context: &types.Header{Number: big.NewInt(200)}, |
| 146 | + Headers: []*types.Header{{Number: big.NewInt(199)}, {Number: big.NewInt(198)}}, |
| 147 | + Codes: map[string]struct{}{"code": {}}, |
| 148 | + State: map[string]struct{}{"node1": {}, "node2": {}}, |
| 149 | + } |
| 150 | + |
| 151 | + encoded, err := rlp.EncodeToBytes(original) |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("EncodeRLP failed: %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + var decoded Witness |
| 157 | + if err := rlp.DecodeBytes(encoded, &decoded); err != nil { |
| 158 | + t.Fatalf("DecodeRLP failed: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + if decoded.context.Number.Cmp(original.context.Number) != 0 { |
| 162 | + t.Errorf("context number: got %v, want %v", decoded.context.Number, original.context.Number) |
| 163 | + } |
| 164 | + if len(decoded.Headers) != len(original.Headers) { |
| 165 | + t.Errorf("headers count: got %d, want %d", len(decoded.Headers), len(original.Headers)) |
| 166 | + } |
| 167 | + |
| 168 | + // State nodes are preserved across the roundtrip. |
| 169 | + for node := range original.State { |
| 170 | + if _, ok := decoded.State[node]; !ok { |
| 171 | + t.Errorf("state node %q missing after roundtrip", node) |
| 172 | + } |
| 173 | + } |
| 174 | + if len(decoded.State) != len(original.State) { |
| 175 | + t.Errorf("state len: got %d, want %d", len(decoded.State), len(original.State)) |
| 176 | + } |
| 177 | + |
| 178 | + // Codes are not in the BorWitness wire format and will be empty after decode. |
| 179 | + if len(decoded.Codes) != 0 { |
| 180 | + t.Errorf("Codes should be empty after BorWitness roundtrip, got %d", len(decoded.Codes)) |
| 181 | + } |
| 182 | +} |
0 commit comments