2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
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"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
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"
|
2017-10-11 14:20:51 +00:00
|
|
|
. "github.com/status-im/status-go/testing"
|
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
|
|
|
|
txServiceMock *fake.MockFakePublicTransactionPoolAPI
|
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)
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TearDownTest() {
|
|
|
|
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-01-18 16:55:17 +00:00
|
|
|
func (s *TxQueueTestSuite) setupTransactionPoolAPI(account *common.SelectedExtKey, nonce hexutil.Uint64, gas hexutil.Big, txErr error) {
|
|
|
|
s.txServiceMock.EXPECT().GetTransactionCount(gomock.Any(), account.Address, gethrpc.PendingBlockNumber).Return(&nonce, nil)
|
|
|
|
s.txServiceMock.EXPECT().GasPrice(gomock.Any()).Return(big.NewInt(10), nil)
|
|
|
|
s.txServiceMock.EXPECT().EstimateGas(gomock.Any(), gomock.Any()).Return(&gas, nil)
|
|
|
|
s.txServiceMock.EXPECT().SendRawTransaction(gomock.Any(), gomock.Any()).Return(gethcommon.Hash{}, txErr)
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
func (s *TxQueueTestSuite) setupStatusBackend(account *common.SelectedExtKey, password string, passwordErr error) {
|
|
|
|
nodeConfig, nodeErr := params.NewNodeConfig("/tmp", params.RopstenNetworkID, true)
|
|
|
|
s.nodeManagerMock.EXPECT().NodeConfig().Return(nodeConfig, nodeErr)
|
|
|
|
s.accountManagerMock.EXPECT().SelectedAccount().Return(account, nil)
|
|
|
|
s.accountManagerMock.EXPECT().VerifyAccountPassword(nodeConfig.KeyStoreDir, account.Address.String(), password).Return(
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
s.setupStatusBackend(account, password, nil)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
nonce := hexutil.Uint64(10)
|
|
|
|
gas := hexutil.Big(*big.NewInt(defaultGas + 1))
|
|
|
|
s.setupTransactionPoolAPI(account, nonce, gas, nil)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2017-09-27 00:50:41 +00:00
|
|
|
txQueueManager := NewManager(s.nodeManagerMock, s.accountManagerMock)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
txQueueManager.Start()
|
|
|
|
defer txQueueManager.Stop()
|
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
err := txQueueManager.QueueTransaction(tx)
|
|
|
|
s.NoError(err)
|
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
w := make(chan struct{})
|
2017-09-04 12:56:58 +00:00
|
|
|
go func() {
|
2018-01-18 16:55:17 +00:00
|
|
|
hash, err := txQueueManager.CompleteTransaction(tx.ID, password)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(tx.Hash, hash)
|
|
|
|
close(w)
|
2017-09-04 12:56:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
err = txQueueManager.WaitForTransaction(tx)
|
2018-01-18 16:55:17 +00:00
|
|
|
s.NoError(err)
|
2017-09-04 12:56:58 +00:00
|
|
|
// Check that error is assigned to the transaction.
|
2018-01-18 16:55:17 +00:00
|
|
|
s.NoError(tx.Err)
|
2017-09-04 12:56:58 +00:00
|
|
|
// Transaction should be already removed from the queue.
|
|
|
|
s.False(txQueueManager.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
|
|
|
}
|
|
|
|
|
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},
|
|
|
|
}
|
|
|
|
s.setupStatusBackend(account, password, nil)
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
nonce := hexutil.Uint64(10)
|
|
|
|
gas := hexutil.Big(*big.NewInt(defaultGas + 1))
|
|
|
|
s.setupTransactionPoolAPI(account, nonce, gas, nil)
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2017-09-27 00:50:41 +00:00
|
|
|
txQueueManager := NewManager(s.nodeManagerMock, s.accountManagerMock)
|
2018-01-26 05:59:21 +00:00
|
|
|
txQueueManager.DisableNotificactions()
|
2017-09-18 08:53:08 +00:00
|
|
|
txQueueManager.Start()
|
|
|
|
defer txQueueManager.Stop()
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{
|
2017-09-18 08:53:08 +00:00
|
|
|
From: common.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: common.ToAddress(TestConfig.Account2.Address),
|
|
|
|
})
|
|
|
|
|
|
|
|
err := txQueueManager.QueueTransaction(tx)
|
|
|
|
s.NoError(err)
|
|
|
|
|
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-01-18 16:55:17 +00:00
|
|
|
_, err := txQueueManager.CompleteTransaction(tx.ID, password)
|
2017-09-18 08:53:08 +00:00
|
|
|
mu.Lock()
|
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
|
|
|
mu.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = txQueueManager.WaitForTransaction(tx)
|
2018-01-18 16:55:17 +00:00
|
|
|
s.NoError(err)
|
2017-09-18 08:53:08 +00:00
|
|
|
// Check that error is assigned to the transaction.
|
2018-01-18 16:55:17 +00:00
|
|
|
s.NoError(tx.Err)
|
2017-09-18 08:53:08 +00:00
|
|
|
// Transaction should be already removed from the queue.
|
|
|
|
s.False(txQueueManager.TransactionQueue().Has(tx.ID))
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
s.accountManagerMock.EXPECT().SelectedAccount().Return(&common.SelectedExtKey{
|
|
|
|
Address: common.FromAddress(TestConfig.Account2.Address),
|
|
|
|
}, nil)
|
|
|
|
|
2017-09-27 00:50:41 +00:00
|
|
|
txQueueManager := NewManager(s.nodeManagerMock, s.accountManagerMock)
|
2018-01-26 05:59:21 +00:00
|
|
|
txQueueManager.DisableNotificactions()
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
txQueueManager.Start()
|
|
|
|
defer txQueueManager.Stop()
|
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
|
|
|
|
err := txQueueManager.QueueTransaction(tx)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
_, err = txQueueManager.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.
|
|
|
|
s.True(txQueueManager.TransactionQueue().Has(tx.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2017-09-27 00:50:41 +00:00
|
|
|
txQueueManager := NewManager(s.nodeManagerMock, s.accountManagerMock)
|
2018-01-26 05:59:21 +00:00
|
|
|
txQueueManager.DisableNotificactions()
|
2017-09-04 12:56:58 +00:00
|
|
|
txQueueManager.Start()
|
|
|
|
defer txQueueManager.Stop()
|
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
|
|
|
|
err := txQueueManager.QueueTransaction(tx)
|
|
|
|
s.NoError(err)
|
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
_, err = txQueueManager.CompleteTransaction(tx.ID, password)
|
|
|
|
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.
|
|
|
|
s.True(txQueueManager.TransactionQueue().Has(tx.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TestDiscardTransaction() {
|
2017-09-27 00:50:41 +00:00
|
|
|
txQueueManager := NewManager(s.nodeManagerMock, s.accountManagerMock)
|
2018-01-26 05:59:21 +00:00
|
|
|
txQueueManager.DisableNotificactions()
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
txQueueManager.Start()
|
|
|
|
defer txQueueManager.Stop()
|
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
|
|
|
|
err := txQueueManager.QueueTransaction(tx)
|
|
|
|
s.NoError(err)
|
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
w := make(chan struct{})
|
2017-09-04 12:56:58 +00:00
|
|
|
go func() {
|
2018-01-26 05:59:21 +00:00
|
|
|
s.NoError(txQueueManager.DiscardTransaction(tx.ID))
|
2018-01-18 16:55:17 +00:00
|
|
|
close(w)
|
2017-09-04 12:56:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
err = txQueueManager.WaitForTransaction(tx)
|
2018-01-26 05:59:21 +00:00
|
|
|
s.Equal(queue.ErrQueuedTxDiscarded, err)
|
2017-09-04 12:56:58 +00:00
|
|
|
// Check that error is assigned to the transaction.
|
2018-01-26 05:59:21 +00:00
|
|
|
s.Equal(queue.ErrQueuedTxDiscarded, tx.Err)
|
2017-09-04 12:56:58 +00:00
|
|
|
// Transaction should be already removed from the queue.
|
|
|
|
s.False(txQueueManager.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
|
|
|
}
|