2017-10-11 14:20:51 +00:00
|
|
|
package rpc
|
2017-05-28 13:57:30 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-25 16:04:40 +00:00
|
|
|
"context"
|
2017-10-25 22:36:47 +00:00
|
|
|
"fmt"
|
2017-09-29 15:09:41 +00:00
|
|
|
"sync"
|
2017-05-28 13:57:30 +00:00
|
|
|
"testing"
|
2017-09-29 15:09:41 +00:00
|
|
|
"time"
|
2017-05-28 13:57:30 +00:00
|
|
|
|
2017-10-23 17:18:09 +00:00
|
|
|
"math/big"
|
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/e2e"
|
2017-05-28 13:57:30 +00:00
|
|
|
"github.com/status-im/status-go/geth/node"
|
2017-10-26 13:14:11 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-10-23 11:05:52 +00:00
|
|
|
. "github.com/status-im/status-go/testing"
|
2017-05-28 13:57:30 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRPCTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(RPCTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type RPCTestSuite struct {
|
2017-10-11 14:20:51 +00:00
|
|
|
e2e.NodeManagerTestSuite
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCTestSuite) SetupTest() {
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NodeManager = node.NewNodeManager()
|
|
|
|
s.NotNil(s.NodeManager)
|
2017-09-07 07:49:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 13:57:30 +00:00
|
|
|
func (s *RPCTestSuite) TestCallRPC() {
|
2017-10-26 12:33:42 +00:00
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
s.T().Skip()
|
|
|
|
}
|
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
for _, upstreamEnabled := range []bool{false, true} {
|
2017-10-25 22:07:50 +00:00
|
|
|
nodeConfig, err := e2e.MakeTestNodeConfig(GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
nodeConfig.IPCEnabled = false
|
|
|
|
nodeConfig.WSEnabled = false
|
|
|
|
nodeConfig.HTTPHost = "" // to make sure that no HTTP interface is started
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
if upstreamEnabled {
|
2017-10-26 13:11:24 +00:00
|
|
|
networkURL, err := GetRemoteURL()
|
2017-10-25 22:07:50 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
nodeConfig.UpstreamConfig.Enabled = true
|
2017-10-25 22:07:50 +00:00
|
|
|
nodeConfig.UpstreamConfig.URL = networkURL
|
2017-09-29 15:09:41 +00:00
|
|
|
}
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
nodeStarted, err := s.NodeManager.StartNode(nodeConfig)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-09-29 15:09:41 +00:00
|
|
|
<-nodeStarted
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
rpcClient := s.NodeManager.RPCClient()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(rpcClient)
|
2017-09-29 15:09:41 +00:00
|
|
|
|
|
|
|
type rpcCall struct {
|
|
|
|
inputJSON string
|
|
|
|
validator func(resultJSON string)
|
|
|
|
}
|
|
|
|
var rpcCalls = []rpcCall{
|
|
|
|
{
|
|
|
|
`{"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)
|
|
|
|
},
|
2017-05-28 13:57:30 +00:00
|
|
|
},
|
2017-09-29 15:09:41 +00:00
|
|
|
{
|
|
|
|
`{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`,
|
|
|
|
func(resultJSON string) {
|
|
|
|
expected := `{"jsonrpc":"2.0","id":64,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}`
|
|
|
|
s.Equal(expected, resultJSON)
|
|
|
|
},
|
2017-05-28 13:57:30 +00:00
|
|
|
},
|
2017-09-29 15:09:41 +00:00
|
|
|
{
|
|
|
|
`{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}`,
|
|
|
|
func(resultJSON string) {
|
2017-10-25 22:36:47 +00:00
|
|
|
expected := `{"jsonrpc":"2.0","id":67,"result":"` + fmt.Sprintf("%d", GetNetworkID()) + `"}`
|
2017-09-29 15:09:41 +00:00
|
|
|
s.Equal(expected, resultJSON)
|
|
|
|
},
|
2017-05-28 13:57:30 +00:00
|
|
|
},
|
2017-09-29 15:09:41 +00:00
|
|
|
{
|
|
|
|
`[{"jsonrpc":"2.0","method":"net_listening","params":[],"id":67}]`,
|
|
|
|
func(resultJSON string) {
|
|
|
|
expected := `[{"jsonrpc":"2.0","id":67,"result":true}]`
|
|
|
|
s.Equal(expected, resultJSON)
|
|
|
|
},
|
2017-09-18 14:24:35 +00:00
|
|
|
},
|
2017-09-29 15:09:41 +00:00
|
|
|
{
|
|
|
|
`[{"jsonrpc":"2.0","method":"net_version","params":[],"id":67},{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":68}]`,
|
|
|
|
func(resultJSON string) {
|
2017-10-25 22:36:47 +00:00
|
|
|
expected := `[{"jsonrpc":"2.0","id":67,"result":"` + fmt.Sprintf("%d", GetNetworkID()) + `"},{"jsonrpc":"2.0","id":68,"result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}]`
|
2017-09-29 15:09:41 +00:00
|
|
|
s.Equal(expected, resultJSON)
|
|
|
|
},
|
2017-09-18 14:24:35 +00:00
|
|
|
},
|
2017-09-29 15:09:41 +00:00
|
|
|
}
|
2017-05-28 13:57:30 +00:00
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-05-28 13:57:30 +00:00
|
|
|
|
2017-09-29 15:09:41 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * 30):
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Fail("test timed out")
|
2017-09-29 15:09:41 +00:00
|
|
|
case <-done:
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
stoppedNode, err := s.NodeManager.StopNode()
|
|
|
|
s.NoError(err)
|
|
|
|
<-stoppedNode
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-25 16:04:40 +00:00
|
|
|
|
|
|
|
// TestCallRawResult checks if returned response is a valid JSON-RPC response.
|
|
|
|
func (s *RPCTestSuite) TestCallRawResult() {
|
2017-10-24 10:23:53 +00:00
|
|
|
nodeConfig, err := e2e.MakeTestNodeConfig(GetNetworkID())
|
2017-09-25 16:04:40 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
nodeStarted, err := s.NodeManager.StartNode(nodeConfig)
|
|
|
|
s.NoError(err)
|
|
|
|
<-nodeStarted
|
|
|
|
|
|
|
|
client := s.NodeManager.RPCClient()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(client)
|
2017-09-25 16:04:40 +00:00
|
|
|
|
|
|
|
jsonResult := client.CallRaw(`{"jsonrpc":"2.0","method":"shh_version","params":[],"id":67}`)
|
|
|
|
s.Equal(`{"jsonrpc":"2.0","id":67,"result":"5.0"}`, jsonResult)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
s.NodeManager.StopNode() //nolint: errcheck
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCallContextResult checks if result passed to CallContext
|
|
|
|
// is set accordingly to its underlying memory layout.
|
|
|
|
func (s *RPCTestSuite) TestCallContextResult() {
|
2017-10-23 17:18:09 +00:00
|
|
|
s.StartTestNode()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestNode()
|
2017-09-25 16:04:40 +00:00
|
|
|
|
2017-10-23 21:39:13 +00:00
|
|
|
EnsureNodeSync(s.NodeManager)
|
2017-10-23 11:05:52 +00:00
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
client := s.NodeManager.RPCClient()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(client)
|
|
|
|
|
2017-10-23 11:05:52 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
2017-10-11 14:20:51 +00:00
|
|
|
defer cancel()
|
2017-09-25 16:04:40 +00:00
|
|
|
|
2017-10-23 11:05:52 +00:00
|
|
|
var balance hexutil.Big
|
2017-11-20 18:21:30 +00:00
|
|
|
err := client.CallContext(ctx, &balance, "eth_getBalance", "0xbF164ca341326a03b547c05B343b2E21eFAe24b9", "latest")
|
2017-09-25 16:04:40 +00:00
|
|
|
s.NoError(err)
|
2017-10-23 11:05:52 +00:00
|
|
|
s.True(balance.ToInt().Cmp(big.NewInt(0)) > 0, "balance should be higher than 0")
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|