|
4 | 4 | package message |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "bytes" |
8 | | - "context" |
9 | 7 | "encoding/base64" |
10 | 8 | "math/rand" |
11 | 9 | "testing" |
12 | 10 |
|
13 | | - "github.com/ava-labs/avalanchego/ids" |
14 | 11 | "github.com/ava-labs/libevm/common" |
15 | | - "github.com/stretchr/testify/assert" |
16 | 12 | "github.com/stretchr/testify/require" |
17 | 13 | ) |
18 | 14 |
|
@@ -52,6 +48,7 @@ func TestMarshalLeafsRequest(t *testing.T) { |
52 | 48 | require.Equal(t, leafsRequest.Start, l.Start) |
53 | 49 | require.Equal(t, leafsRequest.End, l.End) |
54 | 50 | require.Equal(t, leafsRequest.Limit, l.Limit) |
| 51 | + require.Equal(t, NodeType(0), l.NodeType) // make sure it is not serialized |
55 | 52 | } |
56 | 53 |
|
57 | 54 | // TestMarshalLeafsResponse requires that the structure or serialization logic hasn't changed, primarily to |
@@ -107,61 +104,53 @@ func TestMarshalLeafsResponse(t *testing.T) { |
107 | 104 | require.Equal(t, leafsResponse.ProofVals, l.ProofVals) |
108 | 105 | } |
109 | 106 |
|
110 | | -func TestLeafsRequestValidation(t *testing.T) { |
111 | | - mockRequestHandler := &mockHandler{} |
112 | | - |
113 | | - tests := map[string]struct { |
114 | | - request LeafsRequest |
115 | | - assertResponse func(t *testing.T) |
116 | | - }{ |
117 | | - "node type StateTrieNode": { |
118 | | - request: LeafsRequest{ |
119 | | - Root: common.BytesToHash([]byte("some hash goes here")), |
120 | | - Start: bytes.Repeat([]byte{0x00}, common.HashLength), |
121 | | - End: bytes.Repeat([]byte{0xff}, common.HashLength), |
122 | | - Limit: 10, |
123 | | - }, |
124 | | - assertResponse: func(t *testing.T) { |
125 | | - assert.True(t, mockRequestHandler.handleStateTrieCalled) |
126 | | - assert.False(t, mockRequestHandler.handleBlockRequestCalled) |
127 | | - assert.False(t, mockRequestHandler.handleCodeRequestCalled) |
128 | | - }, |
129 | | - }, |
130 | | - } |
131 | | - for name, test := range tests { |
132 | | - t.Run(name, func(t *testing.T) { |
133 | | - _, _ = test.request.Handle(context.Background(), ids.GenerateTestNodeID(), 1, mockRequestHandler) |
134 | | - test.assertResponse(t) |
135 | | - mockRequestHandler.reset() |
136 | | - }) |
| 107 | +// TestLeafsRequestNodeTypeNotSerialized verifies that NodeType is not serialized |
| 108 | +// and does not affect the encoded output. This ensures backward compatibility. |
| 109 | +func TestLeafsRequestNodeTypeNotSerialized(t *testing.T) { |
| 110 | + // set random seed for deterministic random |
| 111 | + rand := rand.New(rand.NewSource(1)) |
| 112 | + |
| 113 | + startBytes := make([]byte, common.HashLength) |
| 114 | + endBytes := make([]byte, common.HashLength) |
| 115 | + |
| 116 | + _, err := rand.Read(startBytes) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + _, err = rand.Read(endBytes) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + // Create request without explicit NodeType (defaults to 0) |
| 123 | + leafsRequestDefault := LeafsRequest{ |
| 124 | + Root: common.BytesToHash([]byte("test root")), |
| 125 | + Start: startBytes, |
| 126 | + End: endBytes, |
| 127 | + Limit: 512, |
137 | 128 | } |
138 | | -} |
139 | 129 |
|
140 | | -var _ RequestHandler = (*mockHandler)(nil) |
| 130 | + // Create request with explicit NodeType |
| 131 | + leafsRequestWithNodeType := LeafsRequest{ |
| 132 | + Root: common.BytesToHash([]byte("test root")), |
| 133 | + Start: startBytes, |
| 134 | + End: endBytes, |
| 135 | + Limit: 512, |
| 136 | + NodeType: StateTrieNode, |
| 137 | + } |
141 | 138 |
|
142 | | -type mockHandler struct { |
143 | | - handleStateTrieCalled, |
144 | | - handleBlockRequestCalled, |
145 | | - handleCodeRequestCalled bool |
146 | | -} |
| 139 | + bytesDefault, err := Codec.Marshal(Version, leafsRequestDefault) |
| 140 | + require.NoError(t, err) |
147 | 141 |
|
148 | | -func (m *mockHandler) HandleStateTrieLeafsRequest(context.Context, ids.NodeID, uint32, LeafsRequest) ([]byte, error) { |
149 | | - m.handleStateTrieCalled = true |
150 | | - return nil, nil |
151 | | -} |
| 142 | + bytesWithNodeType, err := Codec.Marshal(Version, leafsRequestWithNodeType) |
| 143 | + require.NoError(t, err) |
152 | 144 |
|
153 | | -func (m *mockHandler) HandleBlockRequest(context.Context, ids.NodeID, uint32, BlockRequest) ([]byte, error) { |
154 | | - m.handleBlockRequestCalled = true |
155 | | - return nil, nil |
156 | | -} |
| 145 | + require.Equal(t, bytesDefault, bytesWithNodeType, "NodeType should not affect serialization") |
157 | 146 |
|
158 | | -func (m *mockHandler) HandleCodeRequest(context.Context, ids.NodeID, uint32, CodeRequest) ([]byte, error) { |
159 | | - m.handleCodeRequestCalled = true |
160 | | - return nil, nil |
161 | | -} |
| 147 | + var unmarshaled LeafsRequest |
| 148 | + _, err = Codec.Unmarshal(bytesWithNodeType, &unmarshaled) |
| 149 | + require.NoError(t, err) |
162 | 150 |
|
163 | | -func (m *mockHandler) reset() { |
164 | | - m.handleStateTrieCalled = false |
165 | | - m.handleBlockRequestCalled = false |
166 | | - m.handleCodeRequestCalled = false |
| 151 | + require.Equal(t, NodeType(0), unmarshaled.NodeType, "NodeType should not be serialized") |
| 152 | + require.Equal(t, leafsRequestDefault.Root, unmarshaled.Root) |
| 153 | + require.Equal(t, leafsRequestDefault.Start, unmarshaled.Start) |
| 154 | + require.Equal(t, leafsRequestDefault.End, unmarshaled.End) |
| 155 | + require.Equal(t, leafsRequestDefault.Limit, unmarshaled.Limit) |
167 | 156 | } |
0 commit comments