proxyd: Pass through finalized and safe tags

This commit is contained in:
Matthew Slipper 2023-05-25 13:01:34 -06:00
parent 56938b6b01
commit 61db300042
2 changed files with 40 additions and 0 deletions

@ -159,6 +159,13 @@ func rewriteTagMap(rctx RewriteContext, m map[string]interface{}, key string) (b
} }
func rewriteTag(rctx RewriteContext, current string) (string, bool, error) { func rewriteTag(rctx RewriteContext, current string) (string, bool, error) {
// If a tag is the safe or finalized block number, don't rewrite it.
// We have a custom check here because the rpc.BlockNumberOrHash type
// doesn't support these custom tags yet.
if current == "safe" || current == "finalized" {
return current, false, nil
}
jv, err := json.Marshal(current) jv, err := json.Marshal(current)
if err != nil { if err != nil {
return "", false, err return "", false, err
@ -177,5 +184,6 @@ func rewriteTag(rctx RewriteContext, current string) (string, bool, error) {
return "", false, ErrRewriteBlockOutOfRange return "", false, ErrRewriteBlockOutOfRange
} }
} }
return current, false, nil return current, false, nil
} }

@ -308,6 +308,38 @@ func TestRewriteRequest(t *testing.T) {
require.Equal(t, hexutil.Uint64(100).String(), p[0]) require.Equal(t, hexutil.Uint64(100).String(), p[0])
}, },
}, },
{
name: "eth_getBlockByNumber finalized",
args: args{
rctx: RewriteContext{latest: hexutil.Uint64(100)},
req: &RPCReq{Method: "eth_getBlockByNumber", Params: mustMarshalJSON([]string{"finalized"})},
res: nil,
},
expected: RewriteNone,
check: func(t *testing.T, args args) {
var p []string
err := json.Unmarshal(args.req.Params, &p)
require.Nil(t, err)
require.Equal(t, 1, len(p))
require.Equal(t, "finalized", p[0])
},
},
{
name: "eth_getBlockByNumber safe",
args: args{
rctx: RewriteContext{latest: hexutil.Uint64(100)},
req: &RPCReq{Method: "eth_getBlockByNumber", Params: mustMarshalJSON([]string{"safe"})},
res: nil,
},
expected: RewriteNone,
check: func(t *testing.T, args args) {
var p []string
err := json.Unmarshal(args.req.Params, &p)
require.Nil(t, err)
require.Equal(t, 1, len(p))
require.Equal(t, "safe", p[0])
},
},
{ {
name: "eth_getBlockByNumber within range", name: "eth_getBlockByNumber within range",
args: args{ args: args{