2017-10-11 14:20:51 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2018-04-23 13:34:49 +00:00
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2018-04-09 08:18:22 +00:00
|
|
|
"github.com/status-im/status-go/sign"
|
2018-02-08 12:52:47 +00:00
|
|
|
e2e "github.com/status-im/status-go/t/e2e"
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJailRPCTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(JailRPCTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type JailRPCTestSuite struct {
|
|
|
|
e2e.BackendTestSuite
|
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
jail jail.Manager
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailRPCTestSuite) SetupTest() {
|
|
|
|
s.BackendTestSuite.SetupTest()
|
|
|
|
s.jail = s.Backend.JailManager()
|
|
|
|
s.NotNil(s.jail)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailRPCTestSuite) TestJailRPCSend() {
|
2018-04-18 15:13:27 +00:00
|
|
|
CheckTestSkipForNetworks(s.T(), params.MainNetworkID)
|
|
|
|
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-10-21 17:04:07 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// load Status JS and add test command to it
|
2017-11-07 17:36:42 +00:00
|
|
|
s.jail.SetBaseJS(baseStatusJSCode)
|
|
|
|
s.jail.CreateAndInitCell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
|
|
|
cell, err := s.jail.Cell(testChatID)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(cell)
|
|
|
|
|
|
|
|
// internally (since we replaced `web3.send` with `jail.Send`)
|
|
|
|
// all requests to web3 are forwarded to `jail.Send`
|
|
|
|
_, err = cell.Run(`
|
|
|
|
var balance = web3.eth.getBalance("` + TestConfig.Account1.Address + `");
|
|
|
|
var sendResult = web3.fromWei(balance, "ether")
|
|
|
|
`)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
value, err := cell.Get("sendResult")
|
|
|
|
s.NoError(err, "cannot obtain result of balance check operation")
|
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
balance, err := value.Value().ToFloat()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.T().Logf("Balance of %.2f ETH found on '%s' account", balance, TestConfig.Account1.Address)
|
2017-11-07 17:46:11 +00:00
|
|
|
s.False(balance < 1, "wrong balance (there should be lots of test Ether on that account)")
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailRPCTestSuite) TestIsConnected() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
s.jail.CreateAndInitCell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
|
|
|
cell, err := s.jail.Cell(testChatID)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
_, err = cell.Run(`
|
|
|
|
var responseValue = web3.isConnected();
|
|
|
|
responseValue = JSON.stringify(responseValue);
|
|
|
|
`)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
responseValue, err := cell.Get("responseValue")
|
|
|
|
s.NoError(err, "cannot obtain result of isConnected()")
|
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
response, err := responseValue.Value().ToBoolean()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err, "cannot parse result")
|
2017-11-07 17:36:42 +00:00
|
|
|
s.True(response)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 15:50:26 +00:00
|
|
|
// regression test: eth_getTransactionReceipt with invalid transaction hash should return "result":null.
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *JailRPCTestSuite) TestRegressionGetTransactionReceipt() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
rpcClient := s.Backend.StatusNode().RPCClient()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(rpcClient)
|
|
|
|
|
|
|
|
// note: transaction hash is assumed to be invalid
|
|
|
|
got := rpcClient.CallRaw(`{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xbbebf28d0a3a3cbb38e6053a5b21f08f82c62b0c145a17b1c4313cac3f68ae7c"],"id":7}`)
|
2018-04-24 15:50:26 +00:00
|
|
|
expected := `{"jsonrpc":"2.0","id":7,"result":null}`
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Equal(expected, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailRPCTestSuite) TestContractDeployment() {
|
2018-04-18 15:13:27 +00:00
|
|
|
CheckTestSkipForNetworks(s.T(), params.MainNetworkID)
|
|
|
|
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-23 13:34:49 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-11-07 17:36:42 +00:00
|
|
|
s.jail.CreateAndInitCell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
cell, err := s.jail.Cell(testChatID)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
completeQueuedTransaction := make(chan struct{})
|
|
|
|
|
|
|
|
var txHash gethcommon.Hash
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
2017-10-26 10:15:40 +00:00
|
|
|
var envelope signal.Envelope
|
2017-11-27 21:56:52 +00:00
|
|
|
unmarshalErr := json.Unmarshal([]byte(jsonEvent), &envelope)
|
|
|
|
s.NoError(unmarshalErr, "cannot unmarshal JSON: %s", jsonEvent)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-10-11 14:20:51 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
s.T().Logf("transaction queued and will be completed shortly, id: %v", event["id"])
|
|
|
|
|
|
|
|
txID := event["id"].(string)
|
2018-04-10 10:02:54 +00:00
|
|
|
result := s.Backend.ApproveSignRequest(txID, TestConfig.Account1.Password)
|
|
|
|
txHash.SetBytes(result.Response.Bytes())
|
|
|
|
if s.NoError(result.Error, event["id"]) {
|
2017-10-11 14:20:51 +00:00
|
|
|
s.T().Logf("contract transaction complete, URL: %s", "https://ropsten.etherscan.io/tx/"+txHash.Hex())
|
|
|
|
}
|
|
|
|
|
|
|
|
close(completeQueuedTransaction)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = cell.Run(`
|
|
|
|
var responseValue = null;
|
|
|
|
var errorValue = null;
|
|
|
|
var testContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"int256"}],"name":"double","outputs":[{"name":"","type":"int256"}],"payable":false,"type":"function"}]);
|
|
|
|
var test = testContract.new(
|
|
|
|
{
|
|
|
|
from: '` + TestConfig.Account1.Address + `',
|
|
|
|
data: '0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029',
|
|
|
|
gas: '` + strconv.Itoa(params.DefaultGas) + `'
|
|
|
|
}, function (e, contract) {
|
|
|
|
// NOTE: The callback will fire twice!
|
|
|
|
if (e) {
|
|
|
|
errorValue = e;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Once the contract has the transactionHash property set and once its deployed on an address.
|
|
|
|
if (!contract.address) {
|
|
|
|
responseValue = contract.transactionHash;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-completeQueuedTransaction:
|
|
|
|
case <-time.After(time.Minute):
|
|
|
|
s.FailNow("test timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until callback is fired and `responseValue` is set. Hacky but simple.
|
2017-11-07 17:46:11 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
errorValue, err := cell.Get("errorValue")
|
|
|
|
s.NoError(err)
|
2018-03-01 16:48:30 +00:00
|
|
|
s.Equal("null", errorValue.Value().String())
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
responseValue, err := cell.Get("responseValue")
|
|
|
|
s.NoError(err)
|
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
response, err := responseValue.Value().ToString()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
expectedResponse := txHash.Hex()
|
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailRPCTestSuite) TestJailVMPersistence() {
|
2018-04-18 15:13:27 +00:00
|
|
|
CheckTestSkipForNetworks(s.T(), params.MainNetworkID)
|
|
|
|
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// log into account from which transactions will be sent
|
2018-03-22 12:31:12 +00:00
|
|
|
err := s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err, "cannot select account: %v", TestConfig.Account1.Address)
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
command string
|
|
|
|
params string
|
|
|
|
validator func(response string) error
|
|
|
|
}
|
|
|
|
var testCases = []testCase{
|
|
|
|
{
|
|
|
|
`["sendTestTx"]`,
|
|
|
|
`{"amount": "0.000001", "from": "` + TestConfig.Account1.Address + `"}`,
|
|
|
|
func(response string) error {
|
|
|
|
if strings.Contains(response, "error") {
|
|
|
|
return fmt.Errorf("unexpected response: %v", response)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["sendTestTx"]`,
|
|
|
|
`{"amount": "0.000002", "from": "` + TestConfig.Account1.Address + `"}`,
|
|
|
|
func(response string) error {
|
|
|
|
if strings.Contains(response, "error") {
|
|
|
|
return fmt.Errorf("unexpected response: %v", response)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["ping"]`,
|
|
|
|
`{"pong": "Ping1", "amount": 0.42}`,
|
|
|
|
func(response string) error {
|
2018-02-08 11:25:01 +00:00
|
|
|
expectedResponse := `{"result":"Ping1"}`
|
2017-10-11 14:20:51 +00:00
|
|
|
if response != expectedResponse {
|
|
|
|
return fmt.Errorf("unexpected response, expected: %v, got: %v", expectedResponse, response)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`["ping"]`,
|
|
|
|
`{"pong": "Ping2", "amount": 0.42}`,
|
|
|
|
func(response string) error {
|
2018-02-08 11:25:01 +00:00
|
|
|
expectedResponse := `{"result":"Ping2"}`
|
2017-10-11 14:20:51 +00:00
|
|
|
if response != expectedResponse {
|
|
|
|
return fmt.Errorf("unexpected response, expected: %v, got: %v", expectedResponse, response)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
jail := s.Backend.JailManager()
|
2017-11-07 17:36:42 +00:00
|
|
|
jail.SetBaseJS(baseStatusJSCode)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
parseResult := jail.CreateAndInitCell(testChatID, `
|
2017-10-11 14:20:51 +00:00
|
|
|
var total = 0;
|
|
|
|
_status_catalog['ping'] = function(params) {
|
|
|
|
total += Number(params.amount);
|
|
|
|
return params.pong;
|
|
|
|
}
|
|
|
|
|
|
|
|
_status_catalog['sendTestTx'] = function(params) {
|
|
|
|
var amount = params.amount;
|
|
|
|
var transaction = {
|
|
|
|
"from": params.from,
|
|
|
|
"to": "`+TestConfig.Account2.Address+`",
|
|
|
|
"value": web3.toWei(amount, "ether")
|
|
|
|
};
|
|
|
|
web3.eth.sendTransaction(transaction, function (error, result) {
|
|
|
|
if(!error) {
|
|
|
|
total += Number(amount);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-01-09 16:53:33 +00:00
|
|
|
|
|
|
|
_status_catalog;
|
2017-10-11 14:20:51 +00:00
|
|
|
`)
|
|
|
|
s.NotContains(parseResult, "error", "further will fail if initial parsing failed")
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-10-20 09:06:22 +00:00
|
|
|
if e := json.Unmarshal([]byte(jsonEvent), &envelope); e != nil {
|
2017-10-11 14:20:51 +00:00
|
|
|
s.T().Errorf("cannot unmarshal event's JSON: %s", jsonEvent)
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-10-11 14:20:51 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
s.T().Logf("Transaction queued (will be completed shortly): {id: %s}\n", event["id"].(string))
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
var txHash gethcommon.Hash
|
2017-10-11 14:20:51 +00:00
|
|
|
txID := event["id"].(string)
|
2018-04-10 10:02:54 +00:00
|
|
|
result := s.Backend.ApproveSignRequest(txID, TestConfig.Account1.Password)
|
|
|
|
s.NoError(result.Error, "cannot complete queued transaction[%v]: %v", event["id"], result.Error)
|
|
|
|
|
|
|
|
txHash.SetBytes(result.Response.Bytes())
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
s.T().Logf("Transaction complete: https://ropsten.etherscan.io/tx/%s", txHash.Hex())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// run commands concurrently
|
|
|
|
for _, tc := range testCases {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(tc testCase) {
|
|
|
|
defer wg.Done() // ensure we don't forget it
|
|
|
|
|
|
|
|
s.T().Logf("CALL START: %v %v", tc.command, tc.params)
|
|
|
|
response := jail.Call(testChatID, tc.command, tc.params)
|
2017-10-20 09:06:22 +00:00
|
|
|
if e := tc.validator(response); e != nil {
|
|
|
|
s.T().Errorf("failed test validation: %v, err: %v", tc.command, e)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
s.T().Logf("CALL END: %v %v", tc.command, tc.params)
|
|
|
|
}(tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
finishTestCases := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(finishTestCases)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-finishTestCases:
|
|
|
|
case <-time.After(time.Minute):
|
|
|
|
s.FailNow("some tests failed to finish in time")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait till eth_sendTransaction callbacks have been executed.
|
|
|
|
// FIXME(tiabc): more reliable means of testing that.
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
// Validate total.
|
|
|
|
cell, err := jail.Cell(testChatID)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
totalOtto, err := cell.Get("total")
|
|
|
|
s.NoError(err)
|
|
|
|
|
2018-03-01 16:48:30 +00:00
|
|
|
total, err := totalOtto.Value().ToFloat()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.T().Log(total)
|
|
|
|
s.InDelta(0.840003, total, 0.0000001)
|
|
|
|
}
|