2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-02-07 22:23:57 +00:00
|
|
|
"errors"
|
2018-01-18 16:55:17 +00:00
|
|
|
"math/big"
|
2017-09-18 08:53:08 +00:00
|
|
|
"sync"
|
2017-09-04 12:56:58 +00:00
|
|
|
"testing"
|
2018-01-18 16:55:17 +00:00
|
|
|
"time"
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2018-01-18 16:55:17 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-02-05 16:28:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-01-18 16:55:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-02-05 16:28:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2018-01-18 16:55:17 +00:00
|
|
|
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
2017-09-04 12:56:58 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2018-01-18 16:55:17 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-01-18 16:55:17 +00:00
|
|
|
"github.com/status-im/status-go/geth/rpc"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/status-im/status-go/geth/transactions/fake"
|
|
|
|
"github.com/status-im/status-go/geth/transactions/queue"
|
2018-02-08 12:52:47 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-09-04 12:56:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTxQueueTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TxQueueTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type TxQueueTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
nodeManagerMockCtrl *gomock.Controller
|
|
|
|
nodeManagerMock *common.MockNodeManager
|
|
|
|
accountManagerMockCtrl *gomock.Controller
|
|
|
|
accountManagerMock *common.MockAccountManager
|
2018-01-18 16:55:17 +00:00
|
|
|
server *gethrpc.Server
|
|
|
|
client *gethrpc.Client
|
|
|
|
txServiceMockCtrl *gomock.Controller
|
2018-01-05 20:58:17 +00:00
|
|
|
txServiceMock *fake.MockPublicTransactionPoolAPI
|
2018-02-07 22:23:57 +00:00
|
|
|
nodeConfig *params.NodeConfig
|
|
|
|
|
|
|
|
manager *Manager
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) SetupTest() {
|
|
|
|
s.nodeManagerMockCtrl = gomock.NewController(s.T())
|
|
|
|
s.accountManagerMockCtrl = gomock.NewController(s.T())
|
2018-01-18 16:55:17 +00:00
|
|
|
s.txServiceMockCtrl = gomock.NewController(s.T())
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
s.nodeManagerMock = common.NewMockNodeManager(s.nodeManagerMockCtrl)
|
|
|
|
s.accountManagerMock = common.NewMockAccountManager(s.accountManagerMockCtrl)
|
2018-01-18 16:55:17 +00:00
|
|
|
|
|
|
|
s.server, s.txServiceMock = fake.NewTestServer(s.txServiceMockCtrl)
|
|
|
|
s.client = gethrpc.DialInProc(s.server)
|
|
|
|
rpclient, _ := rpc.NewClient(s.client, params.UpstreamRPCConfig{})
|
|
|
|
s.nodeManagerMock.EXPECT().RPCClient().Return(rpclient)
|
2018-03-19 16:22:09 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig("/tmp", "", params.RopstenNetworkID, true)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.nodeConfig = nodeConfig
|
|
|
|
|
|
|
|
s.manager = NewManager(s.nodeManagerMock, s.accountManagerMock)
|
|
|
|
s.manager.DisableNotificactions()
|
|
|
|
s.manager.completionTimeout = time.Second
|
|
|
|
s.manager.rpcCallTimeout = time.Second
|
|
|
|
s.manager.Start()
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TearDownTest() {
|
2018-02-07 22:23:57 +00:00
|
|
|
s.manager.Stop()
|
2017-09-04 12:56:58 +00:00
|
|
|
s.nodeManagerMockCtrl.Finish()
|
|
|
|
s.accountManagerMockCtrl.Finish()
|
2018-01-18 16:55:17 +00:00
|
|
|
s.txServiceMockCtrl.Finish()
|
|
|
|
s.server.Stop()
|
|
|
|
s.client.Close()
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 16:28:21 +00:00
|
|
|
var (
|
2018-02-27 10:39:30 +00:00
|
|
|
testGas = hexutil.Uint64(defaultGas + 1)
|
2018-02-05 16:28:21 +00:00
|
|
|
testGasPrice = (*hexutil.Big)(big.NewInt(10))
|
|
|
|
testNonce = hexutil.Uint64(10)
|
|
|
|
)
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
func (s *TxQueueTestSuite) setupTransactionPoolAPI(tx *common.QueuedTx, returnNonce, resultNonce hexutil.Uint64, account *common.SelectedExtKey, txErr error) {
|
2018-02-05 16:28:21 +00:00
|
|
|
// Expect calls to gas functions only if there are no user defined values.
|
|
|
|
// And also set the expected gas and gas price for RLP encoding the expected tx.
|
2018-02-27 10:39:30 +00:00
|
|
|
var usedGas hexutil.Uint64
|
|
|
|
var usedGasPrice *big.Int
|
2018-02-07 22:23:57 +00:00
|
|
|
s.txServiceMock.EXPECT().GetTransactionCount(gomock.Any(), account.Address, gethrpc.PendingBlockNumber).Return(&returnNonce, nil)
|
2018-02-05 16:28:21 +00:00
|
|
|
if tx.Args.GasPrice == nil {
|
|
|
|
usedGasPrice = (*big.Int)(testGasPrice)
|
|
|
|
s.txServiceMock.EXPECT().GasPrice(gomock.Any()).Return(usedGasPrice, nil)
|
|
|
|
} else {
|
|
|
|
usedGasPrice = (*big.Int)(tx.Args.GasPrice)
|
|
|
|
}
|
|
|
|
if tx.Args.Gas == nil {
|
|
|
|
s.txServiceMock.EXPECT().EstimateGas(gomock.Any(), gomock.Any()).Return(testGas, nil)
|
2018-02-27 10:39:30 +00:00
|
|
|
usedGas = testGas
|
2018-02-05 16:28:21 +00:00
|
|
|
} else {
|
2018-02-27 10:39:30 +00:00
|
|
|
usedGas = *tx.Args.Gas
|
2018-02-05 16:28:21 +00:00
|
|
|
}
|
|
|
|
// Prepare the transaction anD RLP encode it.
|
2018-02-07 22:23:57 +00:00
|
|
|
data := s.rlpEncodeTx(tx, s.nodeConfig, account, &resultNonce, usedGas, usedGasPrice)
|
2018-02-05 16:28:21 +00:00
|
|
|
// Expect the RLP encoded transaction.
|
|
|
|
s.txServiceMock.EXPECT().SendRawTransaction(gomock.Any(), data).Return(gethcommon.Hash{}, txErr)
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-02-27 10:39:30 +00:00
|
|
|
func (s *TxQueueTestSuite) rlpEncodeTx(tx *common.QueuedTx, config *params.NodeConfig, account *common.SelectedExtKey, nonce *hexutil.Uint64, gas hexutil.Uint64, gasPrice *big.Int) hexutil.Bytes {
|
2018-02-05 16:28:21 +00:00
|
|
|
newTx := types.NewTransaction(
|
|
|
|
uint64(*nonce),
|
|
|
|
gethcommon.Address(*tx.Args.To),
|
|
|
|
tx.Args.Value.ToInt(),
|
2018-02-27 10:39:30 +00:00
|
|
|
uint64(gas),
|
2018-02-05 16:28:21 +00:00
|
|
|
gasPrice,
|
2018-02-27 10:39:30 +00:00
|
|
|
[]byte(tx.Args.Input),
|
2018-02-05 16:28:21 +00:00
|
|
|
)
|
|
|
|
chainID := big.NewInt(int64(config.NetworkID))
|
|
|
|
signedTx, err := types.SignTx(newTx, types.NewEIP155Signer(chainID), account.AccountKey.PrivateKey)
|
|
|
|
s.NoError(err)
|
|
|
|
data, err := rlp.EncodeToBytes(signedTx)
|
|
|
|
s.NoError(err)
|
|
|
|
return hexutil.Bytes(data)
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
func (s *TxQueueTestSuite) setupStatusBackend(account *common.SelectedExtKey, password string, passwordErr error) {
|
|
|
|
s.nodeManagerMock.EXPECT().NodeConfig().Return(s.nodeConfig, nil)
|
2018-01-18 16:55:17 +00:00
|
|
|
s.accountManagerMock.EXPECT().SelectedAccount().Return(account, nil)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.accountManagerMock.EXPECT().VerifyAccountPassword(s.nodeConfig.KeyStoreDir, account.Address.String(), password).Return(
|
2018-01-18 16:55:17 +00:00
|
|
|
nil, passwordErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TestCompleteTransaction() {
|
|
|
|
password := TestConfig.Account1.Password
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
account := &common.SelectedExtKey{
|
|
|
|
Address: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2018-02-27 10:39:30 +00:00
|
|
|
gas *hexutil.Uint64
|
2018-02-05 16:28:21 +00:00
|
|
|
gasPrice *hexutil.Big
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"noGasDef",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gasDefined",
|
2018-02-27 10:39:30 +00:00
|
|
|
&testGas,
|
2018-02-05 16:28:21 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"gasPriceDefined",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
|
|
|
},
|
2018-02-27 10:39:30 +00:00
|
|
|
{
|
|
|
|
"inputPassedInLegacyDataField",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
|
|
|
},
|
2018-02-05 16:28:21 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-02-05 16:28:21 +00:00
|
|
|
for _, testCase := range testCases {
|
|
|
|
s.T().Run(testCase.name, func(t *testing.T) {
|
|
|
|
s.SetupTest()
|
2018-02-07 22:23:57 +00:00
|
|
|
s.setupStatusBackend(account, password, nil)
|
2018-02-05 16:28:21 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
Gas: testCase.gas,
|
|
|
|
GasPrice: testCase.gasPrice,
|
|
|
|
})
|
2018-02-07 22:23:57 +00:00
|
|
|
s.setupTransactionPoolAPI(tx, testNonce, testNonce, account, nil)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
2018-02-05 16:28:21 +00:00
|
|
|
w := make(chan struct{})
|
|
|
|
var (
|
|
|
|
hash gethcommon.Hash
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
go func() {
|
2018-02-07 22:23:57 +00:00
|
|
|
hash, err = s.manager.CompleteTransaction(tx.ID, password)
|
2018-02-05 16:28:21 +00:00
|
|
|
s.NoError(err)
|
|
|
|
close(w)
|
|
|
|
}()
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
rst := s.manager.WaitForTransaction(tx)
|
2018-02-05 16:28:21 +00:00
|
|
|
// Check that error is assigned to the transaction.
|
|
|
|
s.NoError(rst.Error)
|
|
|
|
// Transaction should be already removed from the queue.
|
2018-02-07 22:23:57 +00:00
|
|
|
s.False(s.manager.TransactionQueue().Has(tx.ID))
|
2018-02-05 16:28:21 +00:00
|
|
|
s.NoError(WaitClosed(w, time.Second))
|
|
|
|
s.Equal(hash, rst.Hash)
|
|
|
|
})
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 08:53:08 +00:00
|
|
|
func (s *TxQueueTestSuite) TestCompleteTransactionMultipleTimes() {
|
2018-01-18 16:55:17 +00:00
|
|
|
password := TestConfig.Account1.Password
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
account := &common.SelectedExtKey{
|
|
|
|
Address: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
2018-02-07 22:23:57 +00:00
|
|
|
s.setupStatusBackend(account, password, nil)
|
2018-02-05 16:28:21 +00:00
|
|
|
|
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
s.setupTransactionPoolAPI(tx, testNonce, testNonce, account, nil)
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
err := s.manager.QueueTransaction(tx)
|
2018-02-05 16:28:21 +00:00
|
|
|
s.NoError(err)
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
mu sync.Mutex
|
|
|
|
completedTx int
|
|
|
|
inprogressTx int
|
|
|
|
txCount = 3
|
|
|
|
)
|
|
|
|
for i := 0; i < txCount; i++ {
|
2017-09-18 08:53:08 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-02-07 22:23:57 +00:00
|
|
|
_, err := s.manager.CompleteTransaction(tx.ID, password)
|
2017-09-18 08:53:08 +00:00
|
|
|
mu.Lock()
|
2018-02-05 16:28:21 +00:00
|
|
|
defer mu.Unlock()
|
2018-01-18 16:55:17 +00:00
|
|
|
if err == nil {
|
|
|
|
completedTx++
|
2018-01-26 05:59:21 +00:00
|
|
|
} else if err == queue.ErrQueuedTxInProgress {
|
2018-01-18 16:55:17 +00:00
|
|
|
inprogressTx++
|
|
|
|
} else {
|
|
|
|
s.Fail("tx failed with unexpected error: ", err.Error())
|
|
|
|
}
|
2017-09-18 08:53:08 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
rst := s.manager.WaitForTransaction(tx)
|
2017-09-18 08:53:08 +00:00
|
|
|
// Check that error is assigned to the transaction.
|
2018-01-05 20:58:17 +00:00
|
|
|
s.NoError(rst.Error)
|
2017-09-18 08:53:08 +00:00
|
|
|
// Transaction should be already removed from the queue.
|
2018-02-07 22:23:57 +00:00
|
|
|
s.False(s.manager.TransactionQueue().Has(tx.ID))
|
2017-09-18 08:53:08 +00:00
|
|
|
|
|
|
|
// Wait for all CompleteTransaction calls.
|
|
|
|
wg.Wait()
|
2018-01-18 16:55:17 +00:00
|
|
|
s.Equal(1, completedTx, "only 1 tx expected to be completed")
|
|
|
|
s.Equal(txCount-1, inprogressTx, "txs expected to be reported as inprogress")
|
2017-09-18 08:53:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
func (s *TxQueueTestSuite) TestAccountMismatch() {
|
2018-02-07 22:23:57 +00:00
|
|
|
s.nodeManagerMock.EXPECT().NodeConfig().Return(s.nodeConfig, nil)
|
2017-09-04 12:56:58 +00:00
|
|
|
s.accountManagerMock.EXPECT().SelectedAccount().Return(&common.SelectedExtKey{
|
|
|
|
Address: common.FromAddress(TestConfig.Account2.Address),
|
|
|
|
}, nil)
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
2017-09-04 12:56:58 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
_, err := s.manager.CompleteTransaction(tx.ID, TestConfig.Account1.Password)
|
2018-01-26 05:59:21 +00:00
|
|
|
s.Equal(err, queue.ErrInvalidCompleteTxSender)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
// Transaction should stay in the queue as mismatched accounts
|
|
|
|
// is a recoverable error.
|
2018-02-07 22:23:57 +00:00
|
|
|
s.True(s.manager.TransactionQueue().Has(tx.ID))
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TestInvalidPassword() {
|
2018-01-18 16:55:17 +00:00
|
|
|
password := "invalid-password"
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
account := &common.SelectedExtKey{
|
|
|
|
Address: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
|
|
|
s.setupStatusBackend(account, password, keystore.ErrDecrypt)
|
2018-01-26 05:59:21 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
2017-09-04 12:56:58 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
|
|
|
_, err := s.manager.CompleteTransaction(tx.ID, password)
|
2018-01-18 16:55:17 +00:00
|
|
|
s.Equal(err.Error(), keystore.ErrDecrypt.Error())
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
// Transaction should stay in the queue as mismatched accounts
|
|
|
|
// is a recoverable error.
|
2018-02-07 22:23:57 +00:00
|
|
|
s.True(s.manager.TransactionQueue().Has(tx.ID))
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TestDiscardTransaction() {
|
2018-01-26 05:59:21 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
2017-09-04 12:56:58 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
2018-01-18 16:55:17 +00:00
|
|
|
w := make(chan struct{})
|
2017-09-04 12:56:58 +00:00
|
|
|
go func() {
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(s.manager.DiscardTransaction(tx.ID))
|
2018-01-18 16:55:17 +00:00
|
|
|
close(w)
|
2017-09-04 12:56:58 +00:00
|
|
|
}()
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
rst := s.manager.WaitForTransaction(tx)
|
2018-01-05 20:58:17 +00:00
|
|
|
s.Equal(ErrQueuedTxDiscarded, rst.Error)
|
2017-09-04 12:56:58 +00:00
|
|
|
// Transaction should be already removed from the queue.
|
2018-02-07 22:23:57 +00:00
|
|
|
s.False(s.manager.TransactionQueue().Has(tx.ID))
|
2018-01-18 16:55:17 +00:00
|
|
|
s.NoError(WaitClosed(w, time.Second))
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TestCompletionTimedOut() {
|
2018-02-07 22:23:57 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
2018-01-05 20:58:17 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
|
|
|
rst := s.manager.WaitForTransaction(tx)
|
|
|
|
s.Equal(ErrQueuedTxTimedOut, rst.Error)
|
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
// TestLocalNonce verifies that local nonce will be used unless
|
|
|
|
// upstream nonce is updated and higher than a local
|
|
|
|
// in test we will run 3 transaction with nonce zero returned by upstream
|
|
|
|
// node, after each call local nonce will be incremented
|
|
|
|
// then, we return higher nonce, as if another node was used to send 2 transactions
|
|
|
|
// upstream nonce will be equal to 5, we update our local counter to 5+1
|
|
|
|
// as the last step, we verify that if tx failed nonce is not updated
|
|
|
|
func (s *TxQueueTestSuite) TestLocalNonce() {
|
|
|
|
txCount := 3
|
|
|
|
password := TestConfig.Account1.Password
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
account := &common.SelectedExtKey{
|
|
|
|
Address: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
|
|
|
// setup call expectations for 5 transactions in total
|
|
|
|
for i := 0; i < txCount+2; i++ {
|
|
|
|
s.setupStatusBackend(account, password, nil)
|
|
|
|
}
|
|
|
|
nonce := hexutil.Uint64(0)
|
|
|
|
for i := 0; i < txCount; i++ {
|
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
|
|
|
s.setupTransactionPoolAPI(tx, nonce, hexutil.Uint64(i), account, nil)
|
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
|
|
|
hash, err := s.manager.CompleteTransaction(tx.ID, password)
|
|
|
|
rst := s.manager.WaitForTransaction(tx)
|
|
|
|
// simple sanity checks
|
|
|
|
s.NoError(err)
|
|
|
|
s.NoError(rst.Error)
|
|
|
|
s.Equal(rst.Hash, hash)
|
|
|
|
resultNonce, _ := s.manager.localNonce.Load(tx.Args.From)
|
|
|
|
s.Equal(uint64(i)+1, resultNonce.(uint64))
|
|
|
|
}
|
|
|
|
nonce = hexutil.Uint64(5)
|
2018-01-05 20:58:17 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
2018-02-07 22:23:57 +00:00
|
|
|
s.setupTransactionPoolAPI(tx, nonce, nonce, account, nil)
|
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
|
|
|
hash, err := s.manager.CompleteTransaction(tx.ID, password)
|
|
|
|
rst := s.manager.WaitForTransaction(tx)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NoError(rst.Error)
|
|
|
|
s.Equal(rst.Hash, hash)
|
|
|
|
resultNonce, _ := s.manager.localNonce.Load(tx.Args.From)
|
|
|
|
s.Equal(uint64(nonce)+1, resultNonce.(uint64))
|
2018-01-05 20:58:17 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
testErr := errors.New("test")
|
|
|
|
s.txServiceMock.EXPECT().GetTransactionCount(gomock.Any(), account.Address, gethrpc.PendingBlockNumber).Return(nil, testErr)
|
|
|
|
tx = common.CreateTransaction(context.Background(), common.SendTxArgs{
|
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
|
|
|
s.NoError(s.manager.QueueTransaction(tx))
|
|
|
|
_, err = s.manager.CompleteTransaction(tx.ID, password)
|
|
|
|
rst = s.manager.WaitForTransaction(tx)
|
|
|
|
s.EqualError(testErr, err.Error())
|
|
|
|
s.EqualError(testErr, rst.Error.Error())
|
|
|
|
resultNonce, _ = s.manager.localNonce.Load(tx.Args.From)
|
|
|
|
s.Equal(uint64(nonce)+1, resultNonce.(uint64))
|
2018-01-05 20:58:17 +00:00
|
|
|
}
|