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-04 12:56:58 +00:00
|
|
|
"testing"
|
2018-01-18 16:55:17 +00:00
|
|
|
"time"
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-04-07 19:16:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
|
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-04-07 19:16:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/contracts/ens/contract"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
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-04-07 19:16:32 +00:00
|
|
|
gethparams "github.com/ethereum/go-ethereum/params"
|
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
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-04-09 08:18:22 +00:00
|
|
|
"github.com/status-im/status-go/sign"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/transactions/fake"
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-02-08 12:52:47 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-09-04 12:56:58 +00:00
|
|
|
)
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
func simpleVerifyFunc(acc *account.SelectedExtKey) func(string) (*account.SelectedExtKey, error) {
|
|
|
|
return func(string) (*account.SelectedExtKey, error) {
|
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 19:16:32 +00:00
|
|
|
func TestTxQueueTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TxQueueTestSuite))
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
type TxQueueTestSuite struct {
|
|
|
|
suite.Suite
|
2018-03-27 16:30:37 +00:00
|
|
|
server *gethrpc.Server
|
|
|
|
client *gethrpc.Client
|
|
|
|
txServiceMockCtrl *gomock.Controller
|
|
|
|
txServiceMock *fake.MockPublicTransactionPoolAPI
|
|
|
|
nodeConfig *params.NodeConfig
|
2018-02-07 22:23:57 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
manager *Transactor
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) SetupTest() {
|
2018-01-18 16:55:17 +00:00
|
|
|
s.txServiceMockCtrl = gomock.NewController(s.T())
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
s.server, s.txServiceMock = fake.NewTestServer(s.txServiceMockCtrl)
|
|
|
|
s.client = gethrpc.DialInProc(s.server)
|
2018-04-09 08:18:22 +00:00
|
|
|
rpcClient, _ := rpc.NewClient(s.client, params.UpstreamRPCConfig{})
|
2018-04-07 19:16:32 +00:00
|
|
|
// expected by simulated backend
|
|
|
|
chainID := gethparams.AllEthashProtocolChanges.ChainId.Uint64()
|
2018-04-26 17:59:57 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig("/tmp", "", chainID)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.nodeConfig = nodeConfig
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
s.manager = NewTransactor(sign.NewPendingRequests())
|
|
|
|
s.manager.sendTxTimeout = time.Second
|
|
|
|
s.manager.SetNetworkID(chainID)
|
2018-04-10 10:02:54 +00:00
|
|
|
s.manager.SetRPC(rpcClient, time.Second)
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TearDownTest() {
|
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-06-06 07:35:32 +00:00
|
|
|
testGas = hexutil.Uint64(defaultGas + 1)
|
|
|
|
testGasPrice = (*hexutil.Big)(big.NewInt(10))
|
|
|
|
testOverridenGas = hexutil.Uint64(defaultGas + 2)
|
|
|
|
testOverridenGasPrice = (*hexutil.Big)(big.NewInt(20))
|
|
|
|
testNonce = hexutil.Uint64(10)
|
2018-02-05 16:28:21 +00:00
|
|
|
)
|
|
|
|
|
2018-06-06 07:35:32 +00:00
|
|
|
func (s *TxQueueTestSuite) setupTransactionPoolAPI(args SendTxArgs, returnNonce, resultNonce hexutil.Uint64, account *account.SelectedExtKey, txErr error, signArgs *sign.TxArgs) {
|
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-06-06 07:35:32 +00:00
|
|
|
if signArgs != nil && signArgs.GasPrice != nil {
|
|
|
|
usedGasPrice = (*big.Int)(signArgs.GasPrice)
|
|
|
|
} else if args.GasPrice == nil {
|
2018-02-05 16:28:21 +00:00
|
|
|
usedGasPrice = (*big.Int)(testGasPrice)
|
|
|
|
s.txServiceMock.EXPECT().GasPrice(gomock.Any()).Return(usedGasPrice, nil)
|
|
|
|
} else {
|
2018-04-09 08:18:22 +00:00
|
|
|
usedGasPrice = (*big.Int)(args.GasPrice)
|
2018-02-05 16:28:21 +00:00
|
|
|
}
|
2018-06-06 07:35:32 +00:00
|
|
|
if signArgs != nil && signArgs.Gas != nil {
|
|
|
|
usedGas = *signArgs.Gas
|
|
|
|
} else if args.Gas == nil {
|
2018-02-05 16:28:21 +00:00
|
|
|
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-04-09 08:18:22 +00:00
|
|
|
usedGas = *args.Gas
|
2018-02-05 16:28:21 +00:00
|
|
|
}
|
|
|
|
// Prepare the transaction anD RLP encode it.
|
2018-04-09 08:18:22 +00:00
|
|
|
data := s.rlpEncodeTx(args, 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-04-09 08:18:22 +00:00
|
|
|
func (s *TxQueueTestSuite) rlpEncodeTx(args SendTxArgs, config *params.NodeConfig, account *account.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),
|
2018-04-09 08:18:22 +00:00
|
|
|
gethcommon.Address(*args.To),
|
|
|
|
args.Value.ToInt(),
|
2018-02-27 10:39:30 +00:00
|
|
|
uint64(gas),
|
2018-02-05 16:28:21 +00:00
|
|
|
gasPrice,
|
2018-04-09 08:18:22 +00:00
|
|
|
[]byte(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-01-18 16:55:17 +00:00
|
|
|
func (s *TxQueueTestSuite) TestCompleteTransaction() {
|
|
|
|
key, _ := crypto.GenerateKey()
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
|
|
|
Address: account.FromAddress(TestConfig.Account1.Address),
|
2018-01-18 16:55:17 +00:00
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
testCases := []struct {
|
2018-06-06 07:35:32 +00:00
|
|
|
name string
|
|
|
|
gas *hexutil.Uint64
|
|
|
|
gasPrice *hexutil.Big
|
|
|
|
signTxArgs *sign.TxArgs
|
2018-02-05 16:28:21 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"noGasDef",
|
|
|
|
nil,
|
|
|
|
nil,
|
2018-06-06 07:35:32 +00:00
|
|
|
s.defaultSignTxArgs(),
|
2018-02-05 16:28:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"gasDefined",
|
2018-02-27 10:39:30 +00:00
|
|
|
&testGas,
|
2018-02-05 16:28:21 +00:00
|
|
|
nil,
|
2018-06-06 07:35:32 +00:00
|
|
|
s.defaultSignTxArgs(),
|
2018-02-05 16:28:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"gasPriceDefined",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
2018-06-06 07:35:32 +00:00
|
|
|
s.defaultSignTxArgs(),
|
2018-02-05 16:28:21 +00:00
|
|
|
},
|
2018-02-27 10:39:30 +00:00
|
|
|
{
|
|
|
|
"inputPassedInLegacyDataField",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
2018-06-06 07:35:32 +00:00
|
|
|
s.defaultSignTxArgs(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"overrideGas",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
&sign.TxArgs{
|
|
|
|
Gas: &testGas,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"overridePreExistingGas",
|
|
|
|
&testGas,
|
|
|
|
nil,
|
|
|
|
&sign.TxArgs{
|
|
|
|
Gas: &testOverridenGas,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"overridePreExistingGasPrice",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
|
|
|
&sign.TxArgs{
|
|
|
|
GasPrice: testOverridenGasPrice,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nilSignTransactionSpecificArgs",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"overridePreExistingGasWithNil",
|
|
|
|
&testGas,
|
|
|
|
nil,
|
|
|
|
&sign.TxArgs{
|
|
|
|
Gas: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"overridePreExistingGasPriceWithNil",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
|
|
|
&sign.TxArgs{
|
|
|
|
GasPrice: nil,
|
|
|
|
},
|
2018-02-27 10:39:30 +00:00
|
|
|
},
|
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-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2018-02-05 16:28:21 +00:00
|
|
|
Gas: testCase.gas,
|
|
|
|
GasPrice: testCase.gasPrice,
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2018-06-06 07:35:32 +00:00
|
|
|
s.setupTransactionPoolAPI(args, testNonce, testNonce, selectedAccount, nil, testCase.signTxArgs)
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-02-05 16:28:21 +00:00
|
|
|
w := make(chan struct{})
|
2018-04-10 10:02:54 +00:00
|
|
|
var sendHash gethcommon.Hash
|
2018-02-05 16:28:21 +00:00
|
|
|
go func() {
|
2018-04-09 08:18:22 +00:00
|
|
|
var sendErr error
|
|
|
|
sendHash, sendErr = s.manager.SendTransaction(context.Background(), args)
|
|
|
|
s.NoError(sendErr)
|
2018-02-05 16:28:21 +00:00
|
|
|
close(w)
|
|
|
|
}()
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
for i := 10; i > 0; i-- {
|
|
|
|
if s.manager.pendingSignRequests.Count() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := s.manager.pendingSignRequests.First()
|
|
|
|
s.NotNil(req)
|
2018-06-06 07:35:32 +00:00
|
|
|
approveResult := s.manager.pendingSignRequests.Approve(req.ID, "", testCase.signTxArgs, simpleVerifyFunc(selectedAccount))
|
2018-04-10 10:02:54 +00:00
|
|
|
s.NoError(approveResult.Error)
|
2018-02-05 16:28:21 +00:00
|
|
|
s.NoError(WaitClosed(w, time.Second))
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
// Transaction should be already removed from the queue.
|
|
|
|
s.False(s.manager.pendingSignRequests.Has(req.ID))
|
2018-04-10 10:02:54 +00:00
|
|
|
s.Equal(sendHash.Bytes(), approveResult.Response.Bytes())
|
2018-02-05 16:28:21 +00:00
|
|
|
})
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 07:35:32 +00:00
|
|
|
func (s *TxQueueTestSuite) defaultSignTxArgs() *sign.TxArgs {
|
|
|
|
return &sign.TxArgs{}
|
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
func (s *TxQueueTestSuite) TestAccountMismatch() {
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2018-04-09 08:18:22 +00:00
|
|
|
Address: account.FromAddress(TestConfig.Account2.Address),
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2017-09-18 08:53:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
go func() {
|
|
|
|
s.manager.SendTransaction(context.Background(), args) // nolint: errcheck
|
|
|
|
}()
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
for i := 10; i > 0; i-- {
|
|
|
|
if s.manager.pendingSignRequests.Count() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
2018-03-26 15:53:10 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
req := s.manager.pendingSignRequests.First()
|
|
|
|
s.NotNil(req)
|
2018-06-06 07:35:32 +00:00
|
|
|
result := s.manager.pendingSignRequests.Approve(req.ID, "", s.defaultSignTxArgs(), simpleVerifyFunc(selectedAccount))
|
2018-04-10 10:02:54 +00:00
|
|
|
s.EqualError(result.Error, ErrInvalidCompleteTxSender.Error())
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
// Transaction should stay in the queue as mismatched accounts
|
|
|
|
// is a recoverable error.
|
2018-04-09 08:18:22 +00:00
|
|
|
s.True(s.manager.pendingSignRequests.Has(req.ID))
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 15:53:10 +00:00
|
|
|
func (s *TxQueueTestSuite) TestDiscardTransaction() {
|
2018-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2018-03-26 15:53:10 +00:00
|
|
|
w := make(chan struct{})
|
|
|
|
go func() {
|
2018-04-09 08:18:22 +00:00
|
|
|
_, err := s.manager.SendTransaction(context.Background(), args)
|
|
|
|
s.Equal(sign.ErrSignReqDiscarded, err)
|
2018-03-26 15:53:10 +00:00
|
|
|
close(w)
|
|
|
|
}()
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
for i := 10; i > 0; i-- {
|
|
|
|
if s.manager.pendingSignRequests.Count() > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
req := s.manager.pendingSignRequests.First()
|
|
|
|
s.NotNil(req)
|
|
|
|
err := s.manager.pendingSignRequests.Discard(req.ID)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NoError(WaitClosed(w, time.Second))
|
2018-02-07 22:23:57 +00:00
|
|
|
}
|
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
|
|
|
|
key, _ := crypto.GenerateKey()
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
|
|
|
Address: account.FromAddress(TestConfig.Account1.Address),
|
2018-02-07 22:23:57 +00:00
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
|
|
|
nonce := hexutil.Uint64(0)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
approved := 0
|
|
|
|
for {
|
|
|
|
// 3 in a cycle, then 2
|
|
|
|
if approved >= txCount+2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req := s.manager.pendingSignRequests.First()
|
|
|
|
if req == nil {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
} else {
|
2018-06-06 07:35:32 +00:00
|
|
|
s.manager.pendingSignRequests.Approve(req.ID, "", s.defaultSignTxArgs(), simpleVerifyFunc(selectedAccount)) // nolint: errcheck
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
for i := 0; i < txCount; i++ {
|
2018-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2018-06-06 07:35:32 +00:00
|
|
|
s.setupTransactionPoolAPI(args, nonce, hexutil.Uint64(i), selectedAccount, nil, nil)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
_, err := s.manager.SendTransaction(context.Background(), args)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(err)
|
2018-04-09 08:18:22 +00:00
|
|
|
resultNonce, _ := s.manager.localNonce.Load(args.From)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.Equal(uint64(i)+1, resultNonce.(uint64))
|
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
nonce = hexutil.Uint64(5)
|
2018-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 07:35:32 +00:00
|
|
|
s.setupTransactionPoolAPI(args, nonce, nonce, selectedAccount, nil, nil)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
_, err := s.manager.SendTransaction(context.Background(), args)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.NoError(err)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
resultNonce, _ := s.manager.localNonce.Load(args.From)
|
2018-02-07 22:23:57 +00:00
|
|
|
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")
|
2018-03-22 12:31:12 +00:00
|
|
|
s.txServiceMock.EXPECT().GetTransactionCount(gomock.Any(), selectedAccount.Address, gethrpc.PendingBlockNumber).Return(nil, testErr)
|
2018-04-09 08:18:22 +00:00
|
|
|
args = SendTxArgs{
|
2018-03-22 12:31:12 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.manager.SendTransaction(context.Background(), args)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.EqualError(testErr, err.Error())
|
2018-04-09 08:18:22 +00:00
|
|
|
resultNonce, _ = s.manager.localNonce.Load(args.From)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.Equal(uint64(nonce)+1, resultNonce.(uint64))
|
2018-01-05 20:58:17 +00:00
|
|
|
}
|
2018-04-07 19:16:32 +00:00
|
|
|
|
|
|
|
func (s *TxQueueTestSuite) TestContractCreation() {
|
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
testaddr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
genesis := core.GenesisAlloc{
|
|
|
|
testaddr: {Balance: big.NewInt(100000000000)},
|
|
|
|
}
|
|
|
|
backend := backends.NewSimulatedBackend(genesis)
|
|
|
|
selectedAccount := &account.SelectedExtKey{
|
|
|
|
Address: testaddr,
|
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
s.manager.sender = backend
|
|
|
|
s.manager.gasCalculator = backend
|
|
|
|
s.manager.pendingNonceProvider = backend
|
|
|
|
tx := SendTxArgs{
|
2018-04-07 19:16:32 +00:00
|
|
|
From: testaddr,
|
|
|
|
Input: hexutil.Bytes(gethcommon.FromHex(contract.ENSBin)),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for i := 1000; i > 0; i-- {
|
|
|
|
req := s.manager.pendingSignRequests.First()
|
|
|
|
if req == nil {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
} else {
|
2018-06-06 07:35:32 +00:00
|
|
|
s.manager.pendingSignRequests.Approve(req.ID, "", s.defaultSignTxArgs(), simpleVerifyFunc(selectedAccount)) // nolint: errcheck
|
2018-04-09 08:18:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
hash, err := s.manager.SendTransaction(context.Background(), tx)
|
2018-04-07 19:16:32 +00:00
|
|
|
s.NoError(err)
|
|
|
|
backend.Commit()
|
|
|
|
receipt, err := backend.TransactionReceipt(context.TODO(), hash)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(crypto.CreateAddress(testaddr, 0), receipt.ContractAddress)
|
|
|
|
}
|