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"
|
2019-01-18 09:01:14 +00:00
|
|
|
"math/big"
|
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-09-25 16:04:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-18 15:13:27 +00:00
|
|
|
"github.com/status-im/status-go/t/e2e"
|
2018-02-08 12:52:47 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils"
|
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 {
|
2018-04-05 09:45:26 +00:00
|
|
|
e2e.StatusNodeTestSuite
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPCTestSuite) SetupTest() {
|
2018-04-05 09:45:26 +00:00
|
|
|
s.StatusNode = node.New()
|
|
|
|
s.NotNil(s.StatusNode)
|
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} {
|
2018-03-02 09:25:30 +00:00
|
|
|
nodeConfig, err := 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
|
2018-06-06 13:16:17 +00:00
|
|
|
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
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.StatusNode.Start(nodeConfig))
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
rpcClient := s.StatusNode.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) {
|
2018-03-02 09:25:30 +00:00
|
|
|
expected := `{"jsonrpc":"2.0","id":67,"result":"6.0"}`
|
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":"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
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.StatusNode.Stop())
|
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() {
|
2018-03-02 09:25:30 +00:00
|
|
|
nodeConfig, err := MakeTestNodeConfig(GetNetworkID())
|
2017-09-25 16:04:40 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.StatusNode.Start(nodeConfig))
|
2017-09-25 16:04:40 +00:00
|
|
|
|
2018-08-02 06:48:44 +00:00
|
|
|
client := s.StatusNode.RPCPrivateClient()
|
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}`)
|
2018-03-02 09:25:30 +00:00
|
|
|
s.Equal(`{"jsonrpc":"2.0","id":67,"result":"6.0"}`, jsonResult)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.StatusNode.Stop())
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 17:17:41 +00:00
|
|
|
// TestCallRawResultGetTransactionReceipt checks if returned response
|
2018-04-24 15:50:26 +00:00
|
|
|
// for a not yet mined transaction is "result": null.
|
2018-01-11 17:17:41 +00:00
|
|
|
// Issue: https://github.com/status-im/status-go/issues/547
|
|
|
|
func (s *RPCTestSuite) TestCallRawResultGetTransactionReceipt() {
|
2018-03-02 09:25:30 +00:00
|
|
|
nodeConfig, err := MakeTestNodeConfig(GetNetworkID())
|
2018-01-11 17:17:41 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.StatusNode.Start(nodeConfig))
|
2018-01-11 17:17:41 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
client := s.StatusNode.RPCClient()
|
2018-01-11 17:17:41 +00:00
|
|
|
s.NotNil(client)
|
|
|
|
|
|
|
|
jsonResult := client.CallRaw(`{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x0ca0d8f2422f62bea77e24ed17db5711a77fa72064cccbb8e53c53b699cd3b34"],"id":5}`)
|
2018-04-24 15:50:26 +00:00
|
|
|
s.Equal(`{"jsonrpc":"2.0","id":5,"result":null}`, jsonResult)
|
2018-01-11 17:17:41 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
s.NoError(s.StatusNode.Stop())
|
2018-01-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2018-04-18 15:13:27 +00:00
|
|
|
CheckTestSkipForNetworks(s.T(), params.MainNetworkID)
|
|
|
|
|
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
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.StatusNode.EnsureSync)
|
2017-10-23 11:05:52 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
client := s.StatusNode.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
|
2019-01-18 09:01:14 +00:00
|
|
|
err := client.CallContext(ctx, &balance, "eth_getBalance", TestConfig.Account1.WalletAddress, "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
|
|
|
}
|