Skip to content

Commit

Permalink
route "earliest" to first shard
Browse files Browse the repository at this point in the history
also, prevent uint64 conversion underflows by routing any other block tag
that is encoded to a negative number to the default proxy
  • Loading branch information
pirtleshell committed Mar 4, 2024
1 parent 1afa8c8 commit bbca4de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,10 @@ func TestE2ETest_HeightBasedRouting(t *testing.T) {
expectRoute: service.ResponseBackendShard,
},
{
name: "request for earliest height -> default",
name: "request for earliest height -> 1st shard",
method: "eth_getBlockByNumber",
params: []interface{}{"earliest", false},
expectRoute: service.ResponseBackendDefault,
expectRoute: service.ResponseBackendShard,
},
{
name: "request for latest height -> pruning",
Expand Down
14 changes: 13 additions & 1 deletion service/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,24 @@ func (sp ShardProxies) ProxyForRequest(r *http.Request) (*httputil.ReverseProxy,
}

// parse height from the request
height, err := decode.ParseBlockNumberFromParams(decodedReq.Method, decodedReq.Params)
parsedHeight, err := decode.ParseBlockNumberFromParams(decodedReq.Method, decodedReq.Params)
if err != nil {
sp.Error().Msg(fmt.Sprintf("expected but failed to parse block number for %+v: %s", decodedReq, err))
return sp.defaultProxies.ProxyForRequest(r)
}

// handle encoded block numbers
height := parsedHeight
if height == decode.BlockTagToNumberCodec[decode.BlockTagEarliest] {
// convert "earliest" to "1" so it routes to first shard
height = 1
} else if parsedHeight < 1 {
// route all other encoded tags to default proxy.
// in practice, this is unreachable because they will be handled by the pruning Proxies
// if shard routing is enabled without PruningOrDefaultProxies, this handles all special block tags
return sp.defaultProxies.ProxyForRequest(r)
}

// look for shard including height
url, shardHeight, found := shardsForHost.Lookup(uint64(height))
if !found {
Expand Down
23 changes: 11 additions & 12 deletions service/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,6 @@ func TestUnitTest_ShardProxies(t *testing.T) {
expectBackend: service.ResponseBackendDefault,
expectRoute: archiveBackend,
},
{
// TODO: should it do this? if shards exist, route to first shard?
name: "routes to default for 'earliest' block",
url: "//archive.kava.io",
req: &decode.EVMRPCRequestEnvelope{
Method: "eth_getBlockByNumber",
Params: []interface{}{"earliest", false},
},
expectFound: true,
expectBackend: service.ResponseBackendDefault,
expectRoute: archiveBackend,
},

// PRUNING ROUTE CASES
{
Expand Down Expand Up @@ -305,6 +293,17 @@ func TestUnitTest_ShardProxies(t *testing.T) {
},

// SHARD ROUTE CASES
{
name: "routes to 1st shard for 'earliest' block",
url: "//archive.kava.io",
req: &decode.EVMRPCRequestEnvelope{
Method: "eth_getBlockByNumber",
Params: []interface{}{"earliest", false},
},
expectFound: true,
expectBackend: service.ResponseBackendShard,
expectRoute: shard1Backend,
},
{
name: "routes to shard 1 for specific height in shard 1",
url: "//archive.kava.io",
Expand Down

0 comments on commit bbca4de

Please sign in to comment.