Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxyd/fix: rewrite should support BlockNumberOrHash #5618

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions proxyd/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package proxyd
import (
"encoding/json"
"errors"
"strings"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
)

type RewriteContext struct {
Expand Down Expand Up @@ -159,15 +159,21 @@ func rewriteTagMap(rctx RewriteContext, m map[string]interface{}, key string) (b
}

func rewriteTag(rctx RewriteContext, current string) (string, bool, error) {
if current == "latest" {
jv, err := json.Marshal(current)
if err != nil {
return "", false, err
}

var bnh rpc.BlockNumberOrHash
err = bnh.UnmarshalJSON(jv)
if err != nil {
return "", false, err
}

if bnh.BlockNumber != nil && *bnh.BlockNumber == rpc.LatestBlockNumber {
return rctx.latest.String(), true, nil
} else if strings.HasPrefix(current, "0x") {
decode, err := hexutil.DecodeUint64(current)
if err != nil {
return current, false, err
}
b := hexutil.Uint64(decode)
if b > rctx.latest {
} else if bnh.BlockNumber != nil {
if hexutil.Uint64(bnh.BlockNumber.Int64()) > rctx.latest {
return "", false, ErrRewriteBlockOutOfRange
}
}
Expand Down
12 changes: 12 additions & 0 deletions proxyd/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ func TestRewriteRequest(t *testing.T) {
expected: RewriteOverrideError,
expectedErr: ErrRewriteBlockOutOfRange,
},
{
name: "eth_getStorageAt using rpc.BlockNumberOrHash",
args: args{
rctx: RewriteContext{latest: hexutil.Uint64(100)},
req: &RPCReq{Method: "eth_getStorageAt", Params: mustMarshalJSON([]string{
"0xae851f927ee40de99aabb7461c00f9622ab91d60",
"0x65a7ed542fb37fe237fdfbdd70b31598523fe5b32879e307bae27a0bd9581c08",
"0x1c4840bcb3de3ac403c0075b46c2c47d4396c5b624b6e1b2874ec04e8879b483"})},
res: nil,
},
expected: RewriteNone,
},
}

// generalize tests for other methods with same interface and behavior
Expand Down