Forward net_* rpc commands to the upstream (#377)
This commit is contained in:
parent
ede939dd9e
commit
e61c39b0b2
|
@ -5,7 +5,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/status-im/status-go/geth/node"
|
"github.com/status-im/status-go/geth/node"
|
||||||
|
@ -149,98 +151,93 @@ func (s *RPCTestSuite) TestCallRPC() {
|
||||||
require := s.Require()
|
require := s.Require()
|
||||||
require.NotNil(s.NodeManager)
|
require.NotNil(s.NodeManager)
|
||||||
|
|
||||||
nodeConfig, err := MakeTestNodeConfig(params.RinkebyNetworkID)
|
for _, upstreamEnabled := range []bool{false, true} {
|
||||||
require.NoError(err)
|
s.T().Logf("TestCallRPC with upstream: %t", upstreamEnabled)
|
||||||
|
|
||||||
nodeConfig.IPCEnabled = false
|
nodeConfig, err := MakeTestNodeConfig(params.RinkebyNetworkID)
|
||||||
nodeConfig.WSEnabled = false
|
require.NoError(err)
|
||||||
nodeConfig.HTTPHost = "" // to make sure that no HTTP interface is started
|
|
||||||
|
|
||||||
nodeStarted, err := s.NodeManager.StartNode(nodeConfig)
|
nodeConfig.IPCEnabled = false
|
||||||
require.NoError(err)
|
nodeConfig.WSEnabled = false
|
||||||
require.NotNil(nodeConfig)
|
nodeConfig.HTTPHost = "" // to make sure that no HTTP interface is started
|
||||||
|
|
||||||
defer s.NodeManager.StopNode()
|
if upstreamEnabled {
|
||||||
|
nodeConfig.UpstreamConfig.Enabled = true
|
||||||
|
nodeConfig.UpstreamConfig.URL = "https://rinkeby.infura.io/nKmXgiFgc2KqtoQ8BCGJ"
|
||||||
|
}
|
||||||
|
|
||||||
<-nodeStarted
|
nodeStarted, err := s.NodeManager.StartNode(nodeConfig)
|
||||||
|
require.NoError(err)
|
||||||
|
|
||||||
rpcClient := s.NodeManager.RPCClient()
|
<-nodeStarted
|
||||||
require.NotNil(rpcClient)
|
|
||||||
|
|
||||||
progress := make(chan struct{}, 25)
|
rpcClient := s.NodeManager.RPCClient()
|
||||||
type rpcCall struct {
|
require.NotNil(rpcClient)
|
||||||
inputJSON string
|
|
||||||
validator func(resultJSON string)
|
type rpcCall struct {
|
||||||
}
|
inputJSON string
|
||||||
var rpcCalls = []rpcCall{
|
validator func(resultJSON string)
|
||||||
{
|
}
|
||||||
`{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
|
var rpcCalls = []rpcCall{
|
||||||
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
|
{
|
||||||
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
|
`{"jsonrpc":"2.0","method":"shh_version","params":[],"id":67}`,
|
||||||
"gas": "0x76c0",
|
func(resultJSON string) {
|
||||||
"gasPrice": "0x9184e72a000",
|
expected := `{"jsonrpc":"2.0","id":67,"result":"5.0"}`
|
||||||
"value": "0x9184e72a",
|
s.Equal(expected, resultJSON)
|
||||||
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],
|
},
|
||||||
"id": 1
|
|
||||||
}`,
|
|
||||||
func(resultJSON string) {
|
|
||||||
progress <- struct{}{}
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
{
|
`{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`,
|
||||||
`{"jsonrpc":"2.0","method":"shh_version","params":[],"id":67}`,
|
func(resultJSON string) {
|
||||||
func(resultJSON string) {
|
expected := `{"jsonrpc":"2.0","id":64,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}`
|
||||||
expected := `{"jsonrpc":"2.0","id":67,"result":"5.0"}`
|
s.Equal(expected, resultJSON)
|
||||||
s.Equal(expected, resultJSON)
|
},
|
||||||
progress <- struct{}{}
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
{
|
`{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}`,
|
||||||
`{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`,
|
func(resultJSON string) {
|
||||||
func(resultJSON string) {
|
expected := `{"jsonrpc":"2.0","id":67,"result":"4"}`
|
||||||
expected := `{"jsonrpc":"2.0","id":64,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}`
|
s.Equal(expected, resultJSON)
|
||||||
s.Equal(expected, resultJSON)
|
},
|
||||||
progress <- struct{}{}
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
{
|
`[{"jsonrpc":"2.0","method":"net_listening","params":[],"id":67}]`,
|
||||||
`{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}`,
|
func(resultJSON string) {
|
||||||
func(resultJSON string) {
|
expected := `[{"jsonrpc":"2.0","id":67,"result":true}]`
|
||||||
expected := `{"jsonrpc":"2.0","id":67,"result":"4"}`
|
s.Equal(expected, resultJSON)
|
||||||
s.Equal(expected, resultJSON)
|
},
|
||||||
progress <- struct{}{}
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
{
|
`[{"jsonrpc":"2.0","method":"net_version","params":[],"id":67},{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":68}]`,
|
||||||
`[{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}]`,
|
func(resultJSON string) {
|
||||||
func(resultJSON string) {
|
expected := `[{"jsonrpc":"2.0","id":67,"result":"4"},{"jsonrpc":"2.0","id":68,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}]`
|
||||||
expected := `[{"jsonrpc":"2.0","id":67,"result":"4"}]`
|
s.Equal(expected, resultJSON)
|
||||||
s.Equal(expected, resultJSON)
|
},
|
||||||
progress <- struct{}{}
|
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
{
|
|
||||||
`[{"jsonrpc":"2.0","method":"net_version","params":[],"id":67},{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":68}]`,
|
|
||||||
func(resultJSON string) {
|
|
||||||
expected := `[{"jsonrpc":"2.0","id":67,"result":"4"},{"jsonrpc":"2.0","id":68,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}]`
|
|
||||||
s.Equal(expected, resultJSON)
|
|
||||||
progress <- struct{}{}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
cnt := len(rpcCalls) - 1 // send transaction blocks up until complete/discarded/times out
|
var wg sync.WaitGroup
|
||||||
for _, r := range rpcCalls {
|
for _, r := range rpcCalls {
|
||||||
go func(r rpcCall) {
|
wg.Add(1)
|
||||||
resultJSON := rpcClient.CallRaw(r.inputJSON)
|
go func(r rpcCall) {
|
||||||
r.validator(resultJSON)
|
defer wg.Done()
|
||||||
}(r)
|
resultJSON := rpcClient.CallRaw(r.inputJSON)
|
||||||
}
|
r.validator(resultJSON)
|
||||||
|
}(r)
|
||||||
|
}
|
||||||
|
|
||||||
for range progress {
|
done := make(chan struct{})
|
||||||
cnt -= 1
|
go func() {
|
||||||
if cnt <= 0 {
|
wg.Wait()
|
||||||
break
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-time.After(time.Second * 30):
|
||||||
|
s.NodeManager.StopNode()
|
||||||
|
s.FailNow("test timed out")
|
||||||
|
case <-done:
|
||||||
|
s.NodeManager.StopNode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ func (r *router) routeRemote(method string) bool {
|
||||||
// remoteMethods contains methods that should be routed to
|
// remoteMethods contains methods that should be routed to
|
||||||
// the upstream node; the rest is considered to be routed to
|
// the upstream node; the rest is considered to be routed to
|
||||||
// the local node.
|
// the local node.
|
||||||
|
// A list of supported methods: https://infura.io/docs/#supported-json-rpc-methods
|
||||||
// TODO(tiabc): Write a test on each of these methods to ensure they're all routed to the proper node and ensure they really work
|
// TODO(tiabc): Write a test on each of these methods to ensure they're all routed to the proper node and ensure they really work
|
||||||
// TODO(tiabc: as we already caught https://github.com/status-im/status-go/issues/350 as the result of missing such test.
|
// TODO(tiabc: as we already caught https://github.com/status-im/status-go/issues/350 as the result of missing such test.
|
||||||
// Although it's tempting to only list methods coming to the local node as there're fewer of them
|
// Although it's tempting to only list methods coming to the local node as there're fewer of them
|
||||||
|
@ -85,4 +86,7 @@ var remoteMethods = [...]string{
|
||||||
"eth_getWork",
|
"eth_getWork",
|
||||||
"eth_submitWork",
|
"eth_submitWork",
|
||||||
"eth_submitHashrate",
|
"eth_submitHashrate",
|
||||||
|
"net_version",
|
||||||
|
"net_peerCount",
|
||||||
|
"net_listening",
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// some of the upstream examples
|
// localMethods are methods that should be executed locally.
|
||||||
var localMethods = []string{"some_weirdo_method", "shh_newMessageFilter", "net_version"}
|
var localTestMethods = []string{"some_weirdo_method", "shh_newMessageFilter", "eth_accounts"}
|
||||||
|
|
||||||
func TestRouteWithUpstream(t *testing.T) {
|
func TestRouteWithUpstream(t *testing.T) {
|
||||||
router := newRouter(true)
|
router := newRouter(true)
|
||||||
|
@ -15,7 +16,7 @@ func TestRouteWithUpstream(t *testing.T) {
|
||||||
require.True(t, router.routeRemote(method), "method "+method+" should routed to remote")
|
require.True(t, router.routeRemote(method), "method "+method+" should routed to remote")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, method := range localMethods {
|
for _, method := range localTestMethods {
|
||||||
t.Run(method, func(t *testing.T) {
|
t.Run(method, func(t *testing.T) {
|
||||||
require.False(t, router.routeRemote(method), "method "+method+" should routed to local")
|
require.False(t, router.routeRemote(method), "method "+method+" should routed to local")
|
||||||
})
|
})
|
||||||
|
@ -29,7 +30,7 @@ func TestRouteWithoutUpstream(t *testing.T) {
|
||||||
require.False(t, router.routeRemote(method), "method "+method+" should routed to locally without UpstreamEnabled")
|
require.False(t, router.routeRemote(method), "method "+method+" should routed to locally without UpstreamEnabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, method := range localMethods {
|
for _, method := range localTestMethods {
|
||||||
require.False(t, router.routeRemote(method), "method "+method+" should routed to local")
|
require.False(t, router.routeRemote(method), "method "+method+" should routed to local")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue