2017-10-11 14:20:51 +00:00
|
|
|
package transactions
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-21 17:04:07 +00:00
|
|
|
"context"
|
2017-05-16 12:09:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
2017-10-11 14:20:51 +00:00
|
|
|
"testing"
|
2017-05-16 12:09:52 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-09-26 13:44:26 +00:00
|
|
|
"github.com/status-im/status-go/geth/account"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-09-25 18:22:57 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/status-im/status-go/geth/transactions"
|
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"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-04-04 17:39:38 +00:00
|
|
|
type initFunc func([]byte, *transactions.SendTxArgs)
|
2018-03-23 11:54:09 +00:00
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
func txURLString(result sign.Result) string {
|
|
|
|
return fmt.Sprintf("https://ropsten.etherscan.io/tx/%s", result.Response.Hash().Hex())
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func TestTransactionsTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TransactionsTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionsTestSuite struct {
|
|
|
|
e2e.BackendTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) TestCallRPCSendTransaction() {
|
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
|
|
|
|
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)
|
|
|
|
|
|
|
|
transactionCompleted := make(chan struct{})
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
var signResult sign.Result
|
2017-10-11 14:20:51 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
|
2017-10-20 09:06:22 +00:00
|
|
|
var sg signal.Envelope
|
|
|
|
err := json.Unmarshal([]byte(rawSignal), &sg)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if sg.Type == sign.EventSignRequestAdded {
|
2017-10-20 09:06:22 +00:00
|
|
|
event := sg.Event.(map[string]interface{})
|
2018-04-10 10:02:54 +00:00
|
|
|
//check for the correct method name
|
|
|
|
method := event["method"].(string)
|
|
|
|
s.Equal(params.SendTransactionMethodName, method)
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
txID := event["id"].(string)
|
2018-04-10 10:02:54 +00:00
|
|
|
signResult = s.Backend.ApproveSignRequest(string(txID), TestConfig.Account1.Password)
|
|
|
|
s.NoError(signResult.Error, "cannot complete queued transaction %s", txID)
|
2017-10-11 14:20:51 +00:00
|
|
|
close(transactionCompleted)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
result := s.Backend.CallRPC(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "eth_sendTransaction",
|
|
|
|
"params": [{
|
|
|
|
"from": "` + TestConfig.Account1.Address + `",
|
|
|
|
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
|
|
|
|
"value": "0x9184e72a"
|
|
|
|
}]
|
|
|
|
}`)
|
|
|
|
s.NotContains(result, "error")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-transactionCompleted:
|
|
|
|
case <-time.After(time.Minute):
|
|
|
|
s.FailNow("sending transaction timed out")
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(`{"jsonrpc":"2.0","id":1,"result":"`+signResult.Response.Hash().Hex()+`"}`, result)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) TestCallRPCSendTransactionUpstream() {
|
2017-10-26 12:33:42 +00:00
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
s.T().Skip()
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
addr, err := GetRemoteURL()
|
2017-10-25 12:37:42 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.StartTestBackend(e2e.WithUpstream(addr))
|
2017-10-11 14:20:51 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(TestConfig.Account2.Address, TestConfig.Account2.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
transactionCompleted := make(chan struct{})
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
var signResult sign.Result
|
2017-10-11 14:20:51 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
|
2017-10-24 16:48:31 +00:00
|
|
|
var signalEnvelope signal.Envelope
|
|
|
|
err := json.Unmarshal([]byte(rawSignal), &signalEnvelope)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if signalEnvelope.Type == sign.EventSignRequestAdded {
|
2017-10-24 16:48:31 +00:00
|
|
|
event := signalEnvelope.Event.(map[string]interface{})
|
2017-10-11 14:20:51 +00:00
|
|
|
txID := event["id"].(string)
|
|
|
|
|
|
|
|
// Complete with a wrong passphrase.
|
2018-04-10 10:02:54 +00:00
|
|
|
signResult = s.Backend.ApproveSignRequest(string(txID), "some-invalid-passphrase")
|
|
|
|
s.EqualError(signResult.Error, keystore.ErrDecrypt.Error(), "should return an error as the passphrase was invalid")
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// Complete with a correct passphrase.
|
2018-04-10 10:02:54 +00:00
|
|
|
signResult = s.Backend.ApproveSignRequest(string(txID), TestConfig.Account2.Password)
|
|
|
|
s.NoError(signResult.Error, "cannot complete queued transaction %s", txID)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
close(transactionCompleted)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
result := s.Backend.CallRPC(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "eth_sendTransaction",
|
|
|
|
"params": [{
|
|
|
|
"from": "` + TestConfig.Account2.Address + `",
|
|
|
|
"to": "` + TestConfig.Account1.Address + `",
|
|
|
|
"value": "0x9184e72a"
|
|
|
|
}]
|
|
|
|
}`)
|
|
|
|
s.NotContains(result, "error")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-transactionCompleted:
|
|
|
|
case <-time.After(time.Minute):
|
|
|
|
s.FailNow("sending transaction timed out")
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(`{"jsonrpc":"2.0","id":1,"result":"`+signResult.Response.Hash().Hex()+`"}`, result)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 19:16:32 +00:00
|
|
|
func (s *TransactionsTestSuite) TestEmptyToFieldPreserved() {
|
|
|
|
s.StartTestBackend()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 12:14:02 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2018-04-07 19:16:32 +00:00
|
|
|
err := s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
transactionCompleted := make(chan struct{})
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
|
|
|
|
var sg struct {
|
|
|
|
Type string
|
|
|
|
Event json.RawMessage
|
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(rawSignal), &sg)
|
|
|
|
s.NoError(err)
|
2018-04-10 10:02:54 +00:00
|
|
|
if sg.Type == sign.EventSignRequestAdded {
|
|
|
|
var event sign.PendingRequestEvent
|
2018-04-07 19:16:32 +00:00
|
|
|
s.NoError(json.Unmarshal(sg.Event, &event))
|
2018-04-09 08:18:22 +00:00
|
|
|
args := event.Args.(map[string]interface{})
|
|
|
|
s.NotNil(args["from"])
|
|
|
|
s.Nil(args["to"])
|
2018-04-10 10:02:54 +00:00
|
|
|
signResult := s.Backend.ApproveSignRequest(event.ID, TestConfig.Account1.Password)
|
|
|
|
s.NoError(signResult.Error)
|
2018-04-07 19:16:32 +00:00
|
|
|
close(transactionCompleted)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
result := s.Backend.CallRPC(`{
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"id": 1,
|
|
|
|
"method": "eth_sendTransaction",
|
|
|
|
"params": [{
|
|
|
|
"from": "` + TestConfig.Account1.Address + `"
|
|
|
|
}]
|
|
|
|
}`)
|
|
|
|
s.NotContains(result, "error")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-transactionCompleted:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
s.FailNow("sending transaction timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 11:54:09 +00:00
|
|
|
// TestSendContractCompat tries to send transaction using the legacy "Data"
|
|
|
|
// field, which is supported for backward compatibility reasons.
|
|
|
|
func (s *TransactionsTestSuite) TestSendContractTxCompat() {
|
2018-04-04 17:39:38 +00:00
|
|
|
initFunc := func(byteCode []byte, args *transactions.SendTxArgs) {
|
2018-03-23 11:54:09 +00:00
|
|
|
args.Data = (hexutil.Bytes)(byteCode)
|
|
|
|
}
|
|
|
|
s.testSendContractTx(initFunc, nil, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSendContractCompat tries to send transaction using both the legacy
|
|
|
|
// "Data" and "Input" fields. Also makes sure that the error is returned if
|
|
|
|
// they have different values.
|
|
|
|
func (s *TransactionsTestSuite) TestSendContractTxCollision() {
|
|
|
|
// Scenario 1: Both fields are filled and have the same value, expect success
|
2018-04-04 17:39:38 +00:00
|
|
|
initFunc := func(byteCode []byte, args *transactions.SendTxArgs) {
|
2018-03-23 11:54:09 +00:00
|
|
|
args.Input = (hexutil.Bytes)(byteCode)
|
|
|
|
args.Data = (hexutil.Bytes)(byteCode)
|
|
|
|
}
|
|
|
|
s.testSendContractTx(initFunc, nil, "")
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.NoError(s.Backend.AccountManager().Logout())
|
|
|
|
|
2018-03-23 11:54:09 +00:00
|
|
|
// Scenario 2: Both fields are filled with different values, expect an error
|
|
|
|
inverted := func(source []byte) []byte {
|
|
|
|
inverse := make([]byte, len(source))
|
|
|
|
copy(inverse, source)
|
|
|
|
for i, b := range inverse {
|
|
|
|
inverse[i] = b ^ 0xFF
|
|
|
|
}
|
|
|
|
return inverse
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:39:38 +00:00
|
|
|
initFunc2 := func(byteCode []byte, args *transactions.SendTxArgs) {
|
2018-03-23 11:54:09 +00:00
|
|
|
args.Input = (hexutil.Bytes)(byteCode)
|
|
|
|
args.Data = (hexutil.Bytes)(inverted(byteCode))
|
|
|
|
}
|
2018-04-04 17:39:38 +00:00
|
|
|
s.testSendContractTx(initFunc2, transactions.ErrInvalidSendTxArgs, "expected error when invalid tx args are sent")
|
2018-03-23 11:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestSendContractTx() {
|
2018-04-04 17:39:38 +00:00
|
|
|
initFunc := func(byteCode []byte, args *transactions.SendTxArgs) {
|
2018-03-23 11:54:09 +00:00
|
|
|
args.Input = (hexutil.Bytes)(byteCode)
|
|
|
|
}
|
|
|
|
s.testSendContractTx(initFunc, nil, "")
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
func (s *TransactionsTestSuite) setDefaultNodeNotificationHandler(signRequestResult *[]byte, sampleAddress string, done chan struct{}, expectedError error) {
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint :dupl
|
|
|
|
var envelope signal.Envelope
|
2018-04-10 10:02:54 +00:00
|
|
|
err := json.Unmarshal([]byte(jsonEvent), &envelope)
|
2017-05-16 12:09:52 +00:00
|
|
|
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
log.Info("transaction queued (will be completed shortly)", "id", event["id"].(string))
|
|
|
|
|
|
|
|
// the first call will fail (we are not logged in, but trying to complete tx)
|
|
|
|
log.Info("trying to complete with no user logged in")
|
2018-04-10 10:02:54 +00:00
|
|
|
err = s.Backend.ApproveSignRequest(
|
2018-04-04 17:39:38 +00:00
|
|
|
string(event["id"].(string)),
|
2017-09-04 12:56:58 +00:00
|
|
|
TestConfig.Account1.Password,
|
2018-04-10 10:02:54 +00:00
|
|
|
).Error
|
2017-09-04 12:56:58 +00:00
|
|
|
s.EqualError(
|
|
|
|
err,
|
2017-09-26 13:44:26 +00:00
|
|
|
account.ErrNoAccountSelected.Error(),
|
2017-09-04 12:56:58 +00:00
|
|
|
fmt.Sprintf("expected error on queued transaction[%v] not thrown", event["id"]),
|
|
|
|
)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// the second call will also fail (we are logged in as different user)
|
|
|
|
log.Info("trying to complete with invalid user")
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(sampleAddress, TestConfig.Account1.Password)
|
2017-05-16 12:09:52 +00:00
|
|
|
s.NoError(err)
|
2018-04-10 10:02:54 +00:00
|
|
|
err = s.Backend.ApproveSignRequest(
|
2018-04-04 17:39:38 +00:00
|
|
|
string(event["id"].(string)),
|
2017-09-04 12:56:58 +00:00
|
|
|
TestConfig.Account1.Password,
|
2018-04-10 10:02:54 +00:00
|
|
|
).Error
|
2017-09-04 12:56:58 +00:00
|
|
|
s.EqualError(
|
|
|
|
err,
|
2018-04-10 10:02:54 +00:00
|
|
|
transactions.ErrInvalidCompleteTxSender.Error(),
|
2017-09-04 12:56:58 +00:00
|
|
|
fmt.Sprintf("expected error on queued transaction[%v] not thrown", event["id"]),
|
|
|
|
)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// the third call will work as expected (as we are logged in with correct credentials)
|
2017-09-04 12:56:58 +00:00
|
|
|
log.Info("trying to complete with correct user, this should succeed")
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
|
2018-04-10 10:02:54 +00:00
|
|
|
result := s.Backend.ApproveSignRequest(
|
2018-04-04 17:39:38 +00:00
|
|
|
string(event["id"].(string)),
|
2017-09-04 12:56:58 +00:00
|
|
|
TestConfig.Account1.Password,
|
|
|
|
)
|
2018-04-09 08:18:22 +00:00
|
|
|
if expectedError != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(expectedError, result.Error)
|
2018-04-09 08:18:22 +00:00
|
|
|
} else {
|
2018-04-10 10:02:54 +00:00
|
|
|
s.NoError(result.Error, fmt.Sprintf("cannot complete queued transaction[%v]", event["id"]))
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
*signRequestResult = result.Response.Bytes()[:]
|
|
|
|
|
|
|
|
log.Info("contract transaction complete", "URL", txURLString(result))
|
|
|
|
close(done)
|
2017-05-16 12:09:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) testSendContractTx(setInputAndDataValue initFunc, expectedError error, expectedErrorDescription string) {
|
|
|
|
s.StartTestBackend()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
|
|
|
|
|
|
|
sampleAddress, _, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
completeQueuedTransaction := make(chan struct{})
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
var signRequestResult []byte
|
|
|
|
s.setDefaultNodeNotificationHandler(&signRequestResult, sampleAddress, completeQueuedTransaction, expectedError)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// this call blocks, up until Complete Transaction is called
|
|
|
|
byteCode, err := hexutil.Decode(`0x6060604052341561000c57fe5b5b60a58061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636ffa1caa14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b60008160020290505b9190505600a165627a7a72305820ccdadd737e4ac7039963b54cee5e5afb25fa859a275252bdcf06f653155228210029`)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-02-27 10:39:30 +00:00
|
|
|
gas := uint64(params.DefaultGas)
|
2018-04-04 17:39:38 +00:00
|
|
|
args := transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
2017-05-16 12:09:52 +00:00
|
|
|
To: nil, // marker, contract creation is expected
|
|
|
|
//Value: (*hexutil.Big)(new(big.Int).Mul(big.NewInt(1), gethcommon.Ether)),
|
2018-03-23 11:54:09 +00:00
|
|
|
Gas: (*hexutil.Uint64)(&gas),
|
|
|
|
}
|
|
|
|
|
|
|
|
setInputAndDataValue(byteCode, &args)
|
|
|
|
txHashCheck, err := s.Backend.SendTransaction(context.TODO(), args)
|
|
|
|
|
|
|
|
if expectedError != nil {
|
|
|
|
s.Equal(expectedError, err, expectedErrorDescription)
|
|
|
|
return
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
s.NoError(err, "cannot send transaction")
|
|
|
|
|
2017-09-15 10:35:31 +00:00
|
|
|
select {
|
|
|
|
case <-completeQueuedTransaction:
|
|
|
|
case <-time.After(2 * time.Minute):
|
|
|
|
s.FailNow("completing transaction timed out")
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(txHashCheck.Bytes(), signRequestResult, "transaction hash returned from SendTransaction is invalid")
|
2017-05-16 12:09:52 +00:00
|
|
|
s.False(reflect.DeepEqual(txHashCheck, gethcommon.Hash{}), "transaction was never queued or completed")
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.PendingSignRequests().Count(), "tx queue must be empty at this point")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 11:05:52 +00:00
|
|
|
func (s *TransactionsTestSuite) TestSendEther() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-10-16 21:54:56 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-10-23 11:05:52 +00:00
|
|
|
|
2017-10-16 21:54:56 +00:00
|
|
|
// create an account
|
|
|
|
sampleAddress, _, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
completeQueuedTransaction := make(chan struct{})
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
2018-04-10 10:02:54 +00:00
|
|
|
var signRequestResult []byte
|
|
|
|
s.setDefaultNodeNotificationHandler(&signRequestResult, sampleAddress, completeQueuedTransaction, nil)
|
2017-10-16 21:54:56 +00:00
|
|
|
|
|
|
|
// this call blocks, up until Complete Transaction is called
|
2018-04-04 17:39:38 +00:00
|
|
|
txHashCheck, err := s.Backend.SendTransaction(context.TODO(), transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2017-10-16 21:54:56 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
|
|
|
s.NoError(err, "cannot send transaction")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-completeQueuedTransaction:
|
|
|
|
case <-time.After(2 * time.Minute):
|
|
|
|
s.FailNow("completing transaction timed out")
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(txHashCheck.Bytes(), signRequestResult, "transaction hash returned from SendTransaction is invalid")
|
2017-05-16 12:09:52 +00:00
|
|
|
s.False(reflect.DeepEqual(txHashCheck, gethcommon.Hash{}), "transaction was never queued or completed")
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestSendEtherTxUpstream() {
|
2017-10-26 12:33:42 +00:00
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
s.T().Skip()
|
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
addr, err := GetRemoteURL()
|
2017-10-25 12:37:42 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.StartTestBackend(e2e.WithUpstream(addr))
|
2017-09-19 11:19:18 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
err = s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
2017-09-19 11:19:18 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
completeQueuedTransaction := make(chan struct{})
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
var txHash = gethcommon.Hash{}
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) { // nolint: dupl
|
|
|
|
var envelope signal.Envelope
|
2017-10-26 10:15:40 +00:00
|
|
|
err = json.Unmarshal([]byte(jsonEvent), &envelope)
|
2017-09-19 11:19:18 +00:00
|
|
|
s.NoError(err, "cannot unmarshal JSON: %s", jsonEvent)
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-09-19 11:19:18 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
log.Info("transaction queued (will be completed shortly)", "id", event["id"].(string))
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
signResult := s.Backend.ApproveSignRequest(
|
2018-04-04 17:39:38 +00:00
|
|
|
string(event["id"].(string)),
|
2017-09-19 11:19:18 +00:00
|
|
|
TestConfig.Account1.Password,
|
|
|
|
)
|
2018-04-10 10:02:54 +00:00
|
|
|
s.NoError(signResult.Error, "cannot complete queued transaction[%v]", event["id"])
|
2017-09-19 11:19:18 +00:00
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
txHash = signResult.Response.Hash()
|
|
|
|
log.Info("contract transaction complete", "URL", txURLString(signResult))
|
2017-09-19 11:19:18 +00:00
|
|
|
close(completeQueuedTransaction)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// This call blocks, up until Complete Transaction is called.
|
|
|
|
// Explicitly not setting Gas to get it estimated.
|
2018-04-04 17:39:38 +00:00
|
|
|
txHashCheck, err := s.Backend.SendTransaction(context.TODO(), transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2017-09-19 11:19:18 +00:00
|
|
|
GasPrice: (*hexutil.Big)(big.NewInt(28000000000)),
|
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
|
|
|
s.NoError(err, "cannot send transaction")
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-completeQueuedTransaction:
|
|
|
|
case <-time.After(1 * time.Minute):
|
|
|
|
s.FailNow("completing transaction timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Equal(txHash.Hex(), txHashCheck.Hex(), "transaction hash returned from SendTransaction is invalid")
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
|
2017-09-19 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestDoubleCompleteQueuedTransactions() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-05-16 12:09:52 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// log into account from which transactions will be sent
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-15 10:35:31 +00:00
|
|
|
completeQueuedTransaction := make(chan struct{})
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
txFailedEventCalled := false
|
2018-04-10 10:02:54 +00:00
|
|
|
signHash := gethcommon.Hash{}
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-05-16 12:09:52 +00:00
|
|
|
err := json.Unmarshal([]byte(jsonEvent), &envelope)
|
|
|
|
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2018-04-04 17:39:38 +00:00
|
|
|
txID := string(event["id"].(string))
|
2017-05-16 12:09:52 +00:00
|
|
|
log.Info("transaction queued (will be failed and completed on the second call)", "id", txID)
|
|
|
|
|
|
|
|
// try with wrong password
|
|
|
|
// make sure that tx is NOT removed from the queue (by re-trying with the correct password)
|
2018-04-10 10:02:54 +00:00
|
|
|
err = s.Backend.ApproveSignRequest(txID, TestConfig.Account1.Password+"wrong").Error
|
2017-05-16 12:09:52 +00:00
|
|
|
s.EqualError(err, keystore.ErrDecrypt.Error())
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Equal(1, s.PendingSignRequests().Count(), "txqueue cannot be empty, as tx has failed")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// now try to complete transaction, but with the correct password
|
2018-04-10 10:02:54 +00:00
|
|
|
signResult := s.Backend.ApproveSignRequest(txID, TestConfig.Account1.Password)
|
|
|
|
s.NoError(signResult.Error)
|
|
|
|
|
|
|
|
log.Info("transaction complete", "URL", txURLString(signResult))
|
|
|
|
|
|
|
|
signHash = signResult.Response.Hash()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
close(completeQueuedTransaction)
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestFailed {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
log.Info("transaction return event received", "id", event["id"].(string))
|
|
|
|
|
|
|
|
receivedErrMessage := event["error_message"].(string)
|
|
|
|
expectedErrMessage := "could not decrypt key with given passphrase"
|
|
|
|
s.Equal(receivedErrMessage, expectedErrMessage)
|
|
|
|
|
|
|
|
receivedErrCode := event["error_code"].(string)
|
|
|
|
s.Equal("2", receivedErrCode)
|
|
|
|
|
|
|
|
txFailedEventCalled = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// this call blocks, and should return on *second* attempt to ApproveSignRequest (w/ the correct password)
|
|
|
|
sendTxHash, err := s.Backend.SendTransaction(context.TODO(), transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2017-05-16 12:09:52 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
|
|
|
s.NoError(err, "cannot send transaction")
|
|
|
|
|
2017-09-15 10:35:31 +00:00
|
|
|
select {
|
|
|
|
case <-completeQueuedTransaction:
|
|
|
|
case <-time.After(time.Minute):
|
|
|
|
s.FailNow("test timed out")
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(sendTxHash, signHash, "transaction hash returned from SendTransaction is invalid")
|
|
|
|
s.False(reflect.DeepEqual(sendTxHash, gethcommon.Hash{}), "transaction was never queued or completed")
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
|
2017-05-16 12:09:52 +00:00
|
|
|
s.True(txFailedEventCalled, "expected tx failure signal is not received")
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestDiscardQueuedTransaction() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-05-16 12:09:52 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// log into account from which transactions will be sent
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-15 10:35:31 +00:00
|
|
|
completeQueuedTransaction := make(chan struct{})
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
txFailedEventCalled := false
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-05-16 12:09:52 +00:00
|
|
|
err := json.Unmarshal([]byte(jsonEvent), &envelope)
|
|
|
|
s.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2018-04-04 17:39:38 +00:00
|
|
|
txID := string(event["id"].(string))
|
2017-05-16 12:09:52 +00:00
|
|
|
log.Info("transaction queued (will be discarded soon)", "id", txID)
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.True(s.Backend.PendingSignRequests().Has(txID), "txqueue should still have test tx")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// discard
|
2018-04-10 10:02:54 +00:00
|
|
|
err := s.Backend.DiscardSignRequest(txID)
|
2017-05-16 12:09:52 +00:00
|
|
|
s.NoError(err, "cannot discard tx")
|
|
|
|
|
|
|
|
// try completing discarded transaction
|
2018-04-10 10:02:54 +00:00
|
|
|
err = s.Backend.ApproveSignRequest(txID, TestConfig.Account1.Password).Error
|
|
|
|
s.EqualError(err, sign.ErrSignReqNotFound.Error(), "expects tx not found, but call to ApproveSignRequest succeeded")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure that tx complete signal propagates
|
2018-04-09 08:18:22 +00:00
|
|
|
s.False(s.Backend.PendingSignRequests().Has(txID),
|
2017-05-16 12:09:52 +00:00
|
|
|
fmt.Sprintf("txqueue should not have test tx at this point (it should be discarded): %s", txID))
|
|
|
|
|
|
|
|
close(completeQueuedTransaction)
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestFailed {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
log.Info("transaction return event received", "id", event["id"].(string))
|
|
|
|
|
|
|
|
receivedErrMessage := event["error_message"].(string)
|
2018-04-09 08:18:22 +00:00
|
|
|
expectedErrMessage := sign.ErrSignReqDiscarded.Error()
|
2017-05-16 12:09:52 +00:00
|
|
|
s.Equal(receivedErrMessage, expectedErrMessage)
|
|
|
|
|
|
|
|
receivedErrCode := event["error_code"].(string)
|
|
|
|
s.Equal("4", receivedErrCode)
|
|
|
|
|
|
|
|
txFailedEventCalled = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// this call blocks, and should return when DiscardQueuedTransaction() is called
|
2018-04-04 17:39:38 +00:00
|
|
|
txHashCheck, err := s.Backend.SendTransaction(context.TODO(), transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2017-05-16 12:09:52 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
2018-04-09 08:18:22 +00:00
|
|
|
s.EqualError(err, sign.ErrSignReqDiscarded.Error(), "transaction is expected to be discarded")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-15 10:35:31 +00:00
|
|
|
select {
|
|
|
|
case <-completeQueuedTransaction:
|
2018-04-09 08:18:22 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
2017-09-15 10:35:31 +00:00
|
|
|
s.FailNow("test timed out")
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
s.True(reflect.DeepEqual(txHashCheck, gethcommon.Hash{}), "transaction returned hash, while it shouldn't")
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
|
2017-05-16 12:09:52 +00:00
|
|
|
s.True(txFailedEventCalled, "expected tx failure signal is not received")
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestCompleteMultipleQueuedTransactions() {
|
2018-02-05 15:48:54 +00:00
|
|
|
s.setupLocalNode()
|
2017-05-16 12:09:52 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
// 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)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-02-05 15:48:54 +00:00
|
|
|
s.sendConcurrentTransactions(3)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestDiscardMultipleQueuedTransactions() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-05-16 12:09:52 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// log into account from which transactions will be sent
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
testTxCount := 3
|
2018-04-04 17:39:38 +00:00
|
|
|
txIDs := make(chan string, testTxCount)
|
2017-09-15 10:35:31 +00:00
|
|
|
allTestTxDiscarded := make(chan struct{})
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
txFailedEventCallCount := 0
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-05-16 12:09:52 +00:00
|
|
|
err := json.Unmarshal([]byte(jsonEvent), &envelope)
|
|
|
|
s.NoError(err)
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2018-04-04 17:39:38 +00:00
|
|
|
txID := string(event["id"].(string))
|
2017-05-16 12:09:52 +00:00
|
|
|
log.Info("transaction queued (will be discarded soon)", "id", txID)
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.True(s.Backend.PendingSignRequests().Has(txID),
|
2017-09-04 12:56:58 +00:00
|
|
|
"txqueue should still have test tx")
|
2017-05-16 12:09:52 +00:00
|
|
|
txIDs <- txID
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestFailed {
|
2017-05-16 12:09:52 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
log.Info("transaction return event received", "id", event["id"].(string))
|
|
|
|
|
|
|
|
receivedErrMessage := event["error_message"].(string)
|
2018-04-09 08:18:22 +00:00
|
|
|
expectedErrMessage := sign.ErrSignReqDiscarded.Error()
|
2017-05-16 12:09:52 +00:00
|
|
|
s.Equal(receivedErrMessage, expectedErrMessage)
|
|
|
|
|
|
|
|
receivedErrCode := event["error_code"].(string)
|
|
|
|
s.Equal("4", receivedErrCode)
|
|
|
|
|
|
|
|
txFailedEventCallCount++
|
|
|
|
if txFailedEventCallCount == testTxCount {
|
2017-09-15 10:35:31 +00:00
|
|
|
close(allTestTxDiscarded)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-31 07:34:31 +00:00
|
|
|
require := s.Require()
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// this call blocks, and should return when DiscardQueuedTransaction() for a given tx id is called
|
|
|
|
sendTx := func() {
|
2018-04-04 17:39:38 +00:00
|
|
|
txHashCheck, err := s.Backend.SendTransaction(context.TODO(), transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2017-05-16 12:09:52 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
2018-04-09 08:18:22 +00:00
|
|
|
require.EqualError(err, sign.ErrSignReqDiscarded.Error())
|
2018-01-31 07:34:31 +00:00
|
|
|
require.Equal(gethcommon.Hash{}, txHashCheck, "transaction returned hash, while it shouldn't")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
signRequests := s.Backend.PendingSignRequests()
|
2018-01-31 07:34:31 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// wait for transactions, and discard immediately
|
2018-04-04 17:39:38 +00:00
|
|
|
discardTxs := func(txIDs []string) {
|
2017-09-04 12:56:58 +00:00
|
|
|
txIDs = append(txIDs, "invalid-tx-id")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// discard
|
2018-04-10 10:02:54 +00:00
|
|
|
discardResults := s.Backend.DiscardSignRequests(txIDs)
|
2018-01-31 07:34:31 +00:00
|
|
|
require.Len(discardResults, 1, "cannot discard txs: %v", discardResults)
|
2018-04-10 10:02:54 +00:00
|
|
|
require.Error(discardResults["invalid-tx-id"], sign.ErrSignReqNotFound, "cannot discard txs: %v", discardResults)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// try completing discarded transaction
|
2018-04-10 10:02:54 +00:00
|
|
|
completeResults := s.Backend.ApproveSignRequests(txIDs, TestConfig.Account1.Password)
|
|
|
|
require.Len(completeResults, testTxCount+1, "unexpected number of errors (call to ApproveSignRequest should not succeed)")
|
2017-07-13 06:54:10 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
for _, txResult := range completeResults {
|
2018-04-10 10:02:54 +00:00
|
|
|
require.Error(txResult.Error, sign.ErrSignReqNotFound, "invalid error for %s", txResult.Response.Hex())
|
|
|
|
require.Equal(sign.EmptyResponse, txResult.Response, "invalid hash (expected zero): %s", txResult.Response.Hex())
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure that tx complete signal propagates
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
for _, txID := range txIDs {
|
2018-01-31 07:34:31 +00:00
|
|
|
require.False(
|
2018-04-09 08:18:22 +00:00
|
|
|
signRequests.Has(txID),
|
2017-09-04 12:56:58 +00:00
|
|
|
"txqueue should not have test tx at this point (it should be discarded): %s",
|
|
|
|
txID,
|
|
|
|
)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
2018-04-04 17:39:38 +00:00
|
|
|
ids := make([]string, testTxCount)
|
2017-05-16 12:09:52 +00:00
|
|
|
for i := 0; i < testTxCount; i++ {
|
2017-09-04 12:56:58 +00:00
|
|
|
ids[i] = <-txIDs
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
discardTxs(ids)
|
2017-05-16 12:09:52 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// send multiple transactions
|
|
|
|
for i := 0; i < testTxCount; i++ {
|
|
|
|
go sendTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-allTestTxDiscarded:
|
2017-08-04 16:14:17 +00:00
|
|
|
case <-time.After(1 * time.Minute):
|
2017-10-11 14:20:51 +00:00
|
|
|
s.FailNow("test timed out")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
time.Sleep(5 * time.Second)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.Backend.PendingSignRequests().Count(), "tx queue must be empty at this point")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *TransactionsTestSuite) TestNonExistentQueuedTransactions() {
|
2017-10-23 16:03:07 +00:00
|
|
|
s.StartTestBackend()
|
2017-05-16 12:09:52 +00:00
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
// log into account from which transactions will be sent
|
2018-03-22 12:31:12 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password))
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// replace transaction notification handler
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(string) {})
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// try completing non-existing transaction
|
2018-04-10 10:02:54 +00:00
|
|
|
err := s.Backend.ApproveSignRequest("some-bad-transaction-id", TestConfig.Account1.Password).Error
|
2017-05-16 12:09:52 +00:00
|
|
|
s.Error(err, "error expected and not received")
|
2018-04-09 08:18:22 +00:00
|
|
|
s.EqualError(err, sign.ErrSignReqNotFound.Error())
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-02-05 15:48:54 +00:00
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) TestCompleteMultipleQueuedTransactionsUpstream() {
|
|
|
|
s.setupUpstreamNode()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
// 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)
|
2018-02-05 15:48:54 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
s.sendConcurrentTransactions(30)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) setupLocalNode() {
|
|
|
|
s.StartTestBackend()
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
2018-02-05 15:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) setupUpstreamNode() {
|
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
s.T().Skip()
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := GetRemoteURL()
|
|
|
|
s.NoError(err)
|
|
|
|
s.StartTestBackend(e2e.WithUpstream(addr))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactionsTestSuite) sendConcurrentTransactions(testTxCount int) {
|
2018-04-04 17:39:38 +00:00
|
|
|
txIDs := make(chan string, testTxCount)
|
2018-02-05 15:48:54 +00:00
|
|
|
allTestTxCompleted := make(chan struct{})
|
|
|
|
|
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
|
|
|
err := json.Unmarshal([]byte(jsonEvent), &envelope)
|
|
|
|
require.NoError(err, fmt.Sprintf("cannot unmarshal JSON: %s", jsonEvent))
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
if envelope.Type == sign.EventSignRequestAdded {
|
2018-02-05 15:48:54 +00:00
|
|
|
event := envelope.Event.(map[string]interface{})
|
2018-04-04 17:39:38 +00:00
|
|
|
txID := string(event["id"].(string))
|
2018-02-05 15:48:54 +00:00
|
|
|
log.Info("transaction queued (will be completed in a single call, once aggregated)", "id", txID)
|
|
|
|
|
|
|
|
txIDs <- txID
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// this call blocks, and should return when DiscardQueuedTransaction() for a given tx id is called
|
|
|
|
sendTx := func() {
|
2018-04-04 17:39:38 +00:00
|
|
|
txHashCheck, err := s.Backend.SendTransaction(context.TODO(), transactions.SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2018-02-05 15:48:54 +00:00
|
|
|
Value: (*hexutil.Big)(big.NewInt(1000000000000)),
|
|
|
|
})
|
|
|
|
require.NoError(err, "cannot send transaction")
|
|
|
|
require.NotEqual(gethcommon.Hash{}, txHashCheck, "transaction returned empty hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for transactions, and complete them in a single call
|
2018-04-04 17:39:38 +00:00
|
|
|
completeTxs := func(txIDs []string) {
|
2018-02-05 15:48:54 +00:00
|
|
|
txIDs = append(txIDs, "invalid-tx-id")
|
2018-04-10 10:02:54 +00:00
|
|
|
results := s.Backend.ApproveSignRequests(txIDs, TestConfig.Account1.Password)
|
2018-02-05 15:48:54 +00:00
|
|
|
s.Len(results, testTxCount+1)
|
2018-04-10 10:02:54 +00:00
|
|
|
s.EqualError(results["invalid-tx-id"].Error, sign.ErrSignReqNotFound.Error())
|
2018-02-05 15:48:54 +00:00
|
|
|
|
|
|
|
for txID, txResult := range results {
|
|
|
|
s.False(
|
|
|
|
txResult.Error != nil && txID != "invalid-tx-id",
|
|
|
|
"invalid error for %s", txID,
|
|
|
|
)
|
|
|
|
s.False(
|
2018-04-10 10:02:54 +00:00
|
|
|
len(txResult.Response.Bytes()) < 1 && txID != "invalid-tx-id",
|
2018-02-05 15:48:54 +00:00
|
|
|
"invalid hash (expected non empty hash): %s", txID,
|
|
|
|
)
|
2018-04-10 10:02:54 +00:00
|
|
|
log.Info("transaction complete", "URL", txURLString(txResult))
|
2018-02-05 15:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) // make sure that tx complete signal propagates
|
|
|
|
|
|
|
|
for _, txID := range txIDs {
|
|
|
|
s.False(
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Backend.PendingSignRequests().Has(txID),
|
2018-02-05 15:48:54 +00:00
|
|
|
"txqueue should not have test tx at this point (it should be completed)",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
2018-04-04 17:39:38 +00:00
|
|
|
ids := make([]string, testTxCount)
|
2018-02-05 15:48:54 +00:00
|
|
|
for i := 0; i < testTxCount; i++ {
|
|
|
|
ids[i] = <-txIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
completeTxs(ids)
|
|
|
|
close(allTestTxCompleted)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// send multiple transactions
|
|
|
|
for i := 0; i < testTxCount; i++ {
|
|
|
|
go sendTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-allTestTxCompleted:
|
2018-02-27 10:39:30 +00:00
|
|
|
case <-time.After(60 * time.Second):
|
2018-02-05 15:48:54 +00:00
|
|
|
s.FailNow("test timed out")
|
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.Zero(s.PendingSignRequests().Count(), "queue should be empty")
|
2018-02-05 15:48:54 +00:00
|
|
|
}
|