eth/catalyst: fix validation of type 0 request

This commit is contained in:
Sina Mahmoodi 2025-01-31 11:59:14 +01:00
parent 8daefeb890
commit 0596e9a77e
2 changed files with 61 additions and 2 deletions

View file

@ -1272,7 +1272,8 @@ func convertRequests(hex []hexutil.Bytes) [][]byte {
// validateRequests checks that requests are ordered by their type and are not empty. // validateRequests checks that requests are ordered by their type and are not empty.
func validateRequests(requests [][]byte) error { func validateRequests(requests [][]byte) error {
var last byte // Magic value to ensure the first request is always valid.
last := byte(0xff)
for _, req := range requests { for _, req := range requests {
// No empty requests. // No empty requests.
if len(req) < 2 { if len(req) < 2 {
@ -1280,7 +1281,7 @@ func validateRequests(requests [][]byte) error {
} }
// Check that requests are ordered by their type. // Check that requests are ordered by their type.
// Each type must appear only once. // Each type must appear only once.
if req[0] <= last { if last != 0xff && req[0] <= last {
return fmt.Errorf("invalid request order: %v", req) return fmt.Errorf("invalid request order: %v", req)
} }
last = req[0] last = req[0]

View file

@ -1737,3 +1737,61 @@ func TestGetClientVersion(t *testing.T) {
t.Fatalf("client info does match expected, got %s", info.String()) t.Fatalf("client info does match expected, got %s", info.String())
} }
} }
func TestValidateRequests(t *testing.T) {
tests := []struct {
name string
requests [][]byte
wantErr bool
}{
{
name: "valid ascending",
requests: [][]byte{
{0x00, 0xAA, 0xBB}, // type 0x00
{0x01, 0xCC}, // type 0x01
{0x02, 0xDD}, // type 0x02
},
wantErr: false,
},
{
name: "empty request (too short)",
requests: [][]byte{
{0x00}, // only 1 byte: type with no data
},
wantErr: true,
},
{
name: "duplicate type",
requests: [][]byte{
{0x00, 0x11},
{0x01, 0x22},
{0x01, 0x33}, // duplicate type 0x01
},
wantErr: true,
},
{
name: "out of order",
requests: [][]byte{
{0x01, 0xAA}, // type 0x01
{0x00, 0xBB}, // type 0x00 out of order (should be ascending)
},
wantErr: true,
},
{
name: "single request valid",
requests: [][]byte{
{0x01, 0xAB},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateRequests(tt.requests)
if (err != nil) != tt.wantErr {
t.Errorf("validateRequests(%v) error = %v, wantErr = %v",
tt.requests, err, tt.wantErr)
}
})
}
}