Skip to content

Commit d8df5dd

Browse files
s1naqianhh
authored andcommitted
eth/catalyst: fix validation of type 0 request (#31103)
I caught this error on Hive. It was introduced by ethereum/go-ethereum#31071 because after adding the equality check the request type 0 will be rejected.
1 parent 898a3c2 commit d8df5dd

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

eth/catalyst/api.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,18 +1296,16 @@ func convertRequests(hex []hexutil.Bytes) [][]byte {
12961296

12971297
// validateRequests checks that requests are ordered by their type and are not empty.
12981298
func validateRequests(requests [][]byte) error {
1299-
var last byte
1300-
for _, req := range requests {
1299+
for i, req := range requests {
13011300
// No empty requests.
13021301
if len(req) < 2 {
13031302
return fmt.Errorf("empty request: %v", req)
13041303
}
13051304
// Check that requests are ordered by their type.
13061305
// Each type must appear only once.
1307-
if req[0] <= last {
1306+
if i > 0 && req[0] <= requests[i-1][0] {
13081307
return fmt.Errorf("invalid request order: %v", req)
13091308
}
1310-
last = req[0]
13111309
}
13121310
return nil
13131311
}

eth/catalyst/api_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,3 +1823,61 @@ func TestGetClientVersion(t *testing.T) {
18231823
t.Fatalf("client info does match expected, got %s", info.String())
18241824
}
18251825
}
1826+
1827+
func TestValidateRequests(t *testing.T) {
1828+
tests := []struct {
1829+
name string
1830+
requests [][]byte
1831+
wantErr bool
1832+
}{
1833+
{
1834+
name: "valid ascending",
1835+
requests: [][]byte{
1836+
{0x00, 0xAA, 0xBB}, // type 0x00
1837+
{0x01, 0xCC}, // type 0x01
1838+
{0x02, 0xDD}, // type 0x02
1839+
},
1840+
wantErr: false,
1841+
},
1842+
{
1843+
name: "empty request (too short)",
1844+
requests: [][]byte{
1845+
{0x00}, // only 1 byte: type with no data
1846+
},
1847+
wantErr: true,
1848+
},
1849+
{
1850+
name: "duplicate type",
1851+
requests: [][]byte{
1852+
{0x00, 0x11},
1853+
{0x01, 0x22},
1854+
{0x01, 0x33}, // duplicate type 0x01
1855+
},
1856+
wantErr: true,
1857+
},
1858+
{
1859+
name: "out of order",
1860+
requests: [][]byte{
1861+
{0x01, 0xAA}, // type 0x01
1862+
{0x00, 0xBB}, // type 0x00 out of order (should be ascending)
1863+
},
1864+
wantErr: true,
1865+
},
1866+
{
1867+
name: "single request valid",
1868+
requests: [][]byte{
1869+
{0x01, 0xAB},
1870+
},
1871+
wantErr: false,
1872+
},
1873+
}
1874+
for _, tt := range tests {
1875+
t.Run(tt.name, func(t *testing.T) {
1876+
err := validateRequests(tt.requests)
1877+
if (err != nil) != tt.wantErr {
1878+
t.Errorf("validateRequests(%v) error = %v, wantErr = %v",
1879+
tt.requests, err, tt.wantErr)
1880+
}
1881+
})
1882+
}
1883+
}

0 commit comments

Comments
 (0)