fix input

This commit is contained in:
Felipe Andrade 2023-06-02 13:45:42 -07:00
parent a3cf38f8b2
commit 804a57566f
3 changed files with 29 additions and 63 deletions

@ -257,11 +257,6 @@ const ReceiptsTargetAlchemyGetTransactionReceipts = "alchemy_getTransactionRecei
const ReceiptsTargetParityGetTransactionReceipts = "parity_getBlockReceipts" const ReceiptsTargetParityGetTransactionReceipts = "parity_getBlockReceipts"
const ReceiptsTargetEthGetTransactionReceipts = "eth_getBlockReceipts" const ReceiptsTargetEthGetTransactionReceipts = "eth_getBlockReceipts"
type ConsensusGetReceiptsRequest struct {
BlockOrHash *rpc.BlockNumberOrHash `json:"blockOrHash"`
Transactions []common.Hash `json:"transactions"`
}
type ConsensusGetReceiptsResult struct { type ConsensusGetReceiptsResult struct {
Method string `json:"method"` Method string `json:"method"`
Result interface{} `json:"result"` Result interface{} `json:"result"`
@ -442,12 +437,11 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool
if rpcReq.Method == ConsensusGetReceiptsMethod { if rpcReq.Method == ConsensusGetReceiptsMethod {
translatedReqs[string(rpcReq.ID)] = rpcReq translatedReqs[string(rpcReq.ID)] = rpcReq
rpcReq.Method = b.receiptsTarget rpcReq.Method = b.receiptsTarget
var reqParams []ConsensusGetReceiptsRequest var reqParams []rpc.BlockNumberOrHash
err := json.Unmarshal(rpcReq.Params, &reqParams) err := json.Unmarshal(rpcReq.Params, &reqParams)
if err != nil { if err != nil {
return nil, ErrInvalidRequest("invalid request") return nil, ErrInvalidRequest("invalid request")
} }
bnh := reqParams[0].BlockOrHash
var translatedParams []byte var translatedParams []byte
switch rpcReq.Method { switch rpcReq.Method {
@ -457,20 +451,20 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool
// conventional methods use an array of strings having either block number or block hash // conventional methods use an array of strings having either block number or block hash
// i.e. ["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"] // i.e. ["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"]
params := make([]string, 1) params := make([]string, 1)
if bnh.BlockNumber != nil { if reqParams[0].BlockNumber != nil {
params[0] = bnh.BlockNumber.String() params[0] = reqParams[0].BlockNumber.String()
} else { } else {
params[0] = bnh.BlockHash.Hex() params[0] = reqParams[0].BlockHash.Hex()
} }
translatedParams = mustMarshalJSON(params) translatedParams = mustMarshalJSON(params)
case ReceiptsTargetAlchemyGetTransactionReceipts: case ReceiptsTargetAlchemyGetTransactionReceipts:
// alchemy uses an array of object with either block number or block hash // alchemy uses an array of object with either block number or block hash
// i.e. [{ blockHash: "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b" }] // i.e. [{ blockHash: "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b" }]
params := make([]BlockHashOrNumberParameter, 1) params := make([]BlockHashOrNumberParameter, 1)
if bnh.BlockNumber != nil { if reqParams[0].BlockNumber != nil {
params[0].BlockNumber = bnh.BlockNumber params[0].BlockNumber = reqParams[0].BlockNumber
} else { } else {
params[0].BlockHash = bnh.BlockHash params[0].BlockHash = reqParams[0].BlockHash
} }
translatedParams = mustMarshalJSON(params) translatedParams = mustMarshalJSON(params)
default: default:

@ -793,8 +793,8 @@ func TestConsensus(t *testing.T) {
// reset request counts // reset request counts
nodes["node1"].mockBackend.Reset() nodes["node1"].mockBackend.Reset()
resRaw, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ resRaw, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"}}) []interface{}{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 200, statusCode) require.Equal(t, 200, statusCode)
@ -820,8 +820,9 @@ func TestConsensus(t *testing.T) {
// reset request counts // reset request counts
nodes["node1"].mockBackend.Reset() nodes["node1"].mockBackend.Reset()
resRaw, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ resRaw, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "latest"}}) []interface{}{"latest"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 200, statusCode) require.Equal(t, 200, statusCode)
@ -847,8 +848,9 @@ func TestConsensus(t *testing.T) {
// reset request counts // reset request counts
nodes["node1"].mockBackend.Reset() nodes["node1"].mockBackend.Reset()
resRaw, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ resRaw, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "0x55"}}) []interface{}{"0x55"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 200, statusCode) require.Equal(t, 200, statusCode)
@ -877,8 +879,9 @@ func TestConsensus(t *testing.T) {
nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("alchemy_getTransactionReceipts")) nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("alchemy_getTransactionReceipts"))
defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts")) defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts"))
resRaw, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ resRaw, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"}}) []interface{}{"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 200, statusCode) require.Equal(t, 200, statusCode)
@ -908,8 +911,9 @@ func TestConsensus(t *testing.T) {
nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("alchemy_getTransactionReceipts")) nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("alchemy_getTransactionReceipts"))
defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts")) defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts"))
resRaw, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ resRaw, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "0x55"}}) []interface{}{"0x55"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 200, statusCode) require.Equal(t, 200, statusCode)
@ -939,8 +943,9 @@ func TestConsensus(t *testing.T) {
nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("alchemy_getTransactionReceipts")) nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("alchemy_getTransactionReceipts"))
defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts")) defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts"))
resRaw, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ resRaw, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "latest"}}) []interface{}{"latest"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 200, statusCode) require.Equal(t, 200, statusCode)
@ -966,8 +971,9 @@ func TestConsensus(t *testing.T) {
nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("unsupported_consensus_receipts_target")) nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("unsupported_consensus_receipts_target"))
defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts")) defer nodes["node1"].backend.Override(proxyd.WithConsensusReceiptTarget("debug_getRawReceipts"))
_, statusCode, err := client.SendRPC("consensus_getReceipts", []interface{}{map[string]interface{}{ _, statusCode, err := client.SendRPC("consensus_getReceipts",
"blockOrHash": "latest"}}) []interface{}{"latest"})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 400, statusCode) require.Equal(t, 400, statusCode)
}) })
@ -978,7 +984,7 @@ func TestConsensus(t *testing.T) {
_, statusCode, err := client.SendBatchRPC( _, statusCode, err := client.SendBatchRPC(
NewRPCReq("1", "eth_getBlockByNumber", []interface{}{"latest"}), NewRPCReq("1", "eth_getBlockByNumber", []interface{}{"latest"}),
NewRPCReq("2", "consensus_getReceipts", []interface{}{"0x102"}), NewRPCReq("2", "consensus_getReceipts", []interface{}{"0x55"}),
NewRPCReq("3", "eth_getBlockByNumber", []interface{}{"0xe1"})) NewRPCReq("3", "eth_getBlockByNumber", []interface{}{"0xe1"}))
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 400, statusCode) require.Equal(t, 400, statusCode)

@ -63,9 +63,7 @@ func RewriteRequest(rctx RewriteContext, req *RPCReq, res *RPCRes) (RewriteResul
case "eth_getLogs", case "eth_getLogs",
"eth_newFilter": "eth_newFilter":
return rewriteRange(rctx, req, res, 0) return rewriteRange(rctx, req, res, 0)
case "consensus_getReceipts": case "debug_getRawReceipts", "consensus_getReceipts":
return rewriteGetReceiptsParams(rctx, req, res)
case "debug_getRawReceipts":
return rewriteParam(rctx, req, res, 0, true) return rewriteParam(rctx, req, res, 0, true)
case "eth_getBalance", case "eth_getBalance",
"eth_getCode", "eth_getCode",
@ -84,38 +82,6 @@ func RewriteRequest(rctx RewriteContext, req *RPCReq, res *RPCRes) (RewriteResul
return RewriteNone, nil return RewriteNone, nil
} }
func rewriteGetReceiptsParams(rctx RewriteContext, req *RPCReq, res *RPCRes) (RewriteResult, error) {
var p []interface{}
err := json.Unmarshal(req.Params, &p)
if err != nil {
return RewriteOverrideError, err
}
if len(p) != 1 {
return RewriteNone, nil
}
if m, ok := p[0].(map[string]interface{}); !ok || m["blockOrHash"] == nil {
return RewriteNone, nil
}
val, rw, err := rewriteTag(rctx, p[0].(map[string]interface{})["blockOrHash"].(string))
if err != nil {
return RewriteOverrideError, err
}
if rw {
p[0].(map[string]interface{})["blockOrHash"] = val
paramRaw, err := json.Marshal(p)
if err != nil {
return RewriteOverrideError, err
}
req.Params = paramRaw
return RewriteOverrideRequest, nil
}
return RewriteNone, nil
}
func rewriteParam(rctx RewriteContext, req *RPCReq, res *RPCRes, pos int, required bool) (RewriteResult, error) { func rewriteParam(rctx RewriteContext, req *RPCReq, res *RPCRes, pos int, required bool) (RewriteResult, error) {
var p []interface{} var p []interface{}
err := json.Unmarshal(req.Params, &p) err := json.Unmarshal(req.Params, &p)