Forward net_* rpc commands to the upstream (#377)

This commit is contained in:
Adam Babik 2017-09-29 17:09:41 +02:00 committed by Ivan Daniluk
parent ede939dd9e
commit e61c39b0b2
3 changed files with 86 additions and 84 deletions

View File

@ -5,7 +5,9 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/geth/node"
@ -149,6 +151,9 @@ func (s *RPCTestSuite) TestCallRPC() {
require := s.Require()
require.NotNil(s.NodeManager)
for _, upstreamEnabled := range []bool{false, true} {
s.T().Logf("TestCallRPC with upstream: %t", upstreamEnabled)
nodeConfig, err := MakeTestNodeConfig(params.RinkebyNetworkID)
require.NoError(err)
@ -156,43 +161,29 @@ func (s *RPCTestSuite) TestCallRPC() {
nodeConfig.WSEnabled = false
nodeConfig.HTTPHost = "" // to make sure that no HTTP interface is started
if upstreamEnabled {
nodeConfig.UpstreamConfig.Enabled = true
nodeConfig.UpstreamConfig.URL = "https://rinkeby.infura.io/nKmXgiFgc2KqtoQ8BCGJ"
}
nodeStarted, err := s.NodeManager.StartNode(nodeConfig)
require.NoError(err)
require.NotNil(nodeConfig)
defer s.NodeManager.StopNode()
<-nodeStarted
rpcClient := s.NodeManager.RPCClient()
require.NotNil(rpcClient)
progress := make(chan struct{}, 25)
type rpcCall struct {
inputJSON string
validator func(resultJSON string)
}
var rpcCalls = []rpcCall{
{
`{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a",
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],
"id": 1
}`,
func(resultJSON string) {
progress <- struct{}{}
},
},
{
`{"jsonrpc":"2.0","method":"shh_version","params":[],"id":67}`,
func(resultJSON string) {
expected := `{"jsonrpc":"2.0","id":67,"result":"5.0"}`
s.Equal(expected, resultJSON)
progress <- struct{}{}
},
},
{
@ -200,7 +191,6 @@ func (s *RPCTestSuite) TestCallRPC() {
func(resultJSON string) {
expected := `{"jsonrpc":"2.0","id":64,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}`
s.Equal(expected, resultJSON)
progress <- struct{}{}
},
},
{
@ -208,15 +198,13 @@ func (s *RPCTestSuite) TestCallRPC() {
func(resultJSON string) {
expected := `{"jsonrpc":"2.0","id":67,"result":"4"}`
s.Equal(expected, resultJSON)
progress <- struct{}{}
},
},
{
`[{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}]`,
`[{"jsonrpc":"2.0","method":"net_listening","params":[],"id":67}]`,
func(resultJSON string) {
expected := `[{"jsonrpc":"2.0","id":67,"result":"4"}]`
expected := `[{"jsonrpc":"2.0","id":67,"result":true}]`
s.Equal(expected, resultJSON)
progress <- struct{}{}
},
},
{
@ -224,23 +212,32 @@ func (s *RPCTestSuite) TestCallRPC() {
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 {
wg.Add(1)
go func(r rpcCall) {
defer wg.Done()
resultJSON := rpcClient.CallRaw(r.inputJSON)
r.validator(resultJSON)
}(r)
}
for range progress {
cnt -= 1
if cnt <= 0 {
break
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-time.After(time.Second * 30):
s.NodeManager.StopNode()
s.FailNow("test timed out")
case <-done:
s.NodeManager.StopNode()
}
}
}

View File

@ -35,6 +35,7 @@ func (r *router) routeRemote(method string) bool {
// remoteMethods contains methods that should be routed to
// the upstream node; the rest is considered to be routed to
// 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: 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
@ -85,4 +86,7 @@ var remoteMethods = [...]string{
"eth_getWork",
"eth_submitWork",
"eth_submitHashrate",
"net_version",
"net_peerCount",
"net_listening",
}

View File

@ -1,12 +1,13 @@
package rpc
import (
"github.com/stretchr/testify/require"
"testing"
"github.com/stretchr/testify/require"
)
// some of the upstream examples
var localMethods = []string{"some_weirdo_method", "shh_newMessageFilter", "net_version"}
// localMethods are methods that should be executed locally.
var localTestMethods = []string{"some_weirdo_method", "shh_newMessageFilter", "eth_accounts"}
func TestRouteWithUpstream(t *testing.T) {
router := newRouter(true)
@ -15,7 +16,7 @@ func TestRouteWithUpstream(t *testing.T) {
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) {
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")
}
for _, method := range localMethods {
for _, method := range localTestMethods {
require.False(t, router.routeRemote(method), "method "+method+" should routed to local")
}
}