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"
|
2019-02-15 11:31:20 +00:00
|
|
|
"fmt"
|
2018-08-27 08:22:21 +00:00
|
|
|
"math"
|
2018-01-18 16:55:17 +00:00
|
|
|
"math/big"
|
2018-08-16 11:37:53 +00:00
|
|
|
"reflect"
|
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"
|
2019-02-21 09:53:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
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-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-02-08 12:52:47 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2019-01-18 09:01:14 +00:00
|
|
|
"github.com/status-im/status-go/transactions/fake"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2017-09-04 12:56:58 +00:00
|
|
|
)
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func TestTransactorSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(TransactorSuite))
|
2018-04-07 19:16:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
type TransactorSuite struct {
|
2017-09-04 12:56:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) 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
|
2018-06-20 14:51:55 +00:00
|
|
|
chainID := gethparams.AllEthashProtocolChanges.ChainID.Uint64()
|
2018-09-21 14:09:31 +00:00
|
|
|
nodeConfig, err := MakeTestNodeConfigWithDataDir("", "/tmp", chainID)
|
2018-02-07 22:23:57 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.nodeConfig = nodeConfig
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
s.manager = NewTransactor()
|
2018-04-09 08:18:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) 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-08-16 11:37:53 +00:00
|
|
|
testGas = hexutil.Uint64(defaultGas + 1)
|
|
|
|
testGasPrice = (*hexutil.Big)(big.NewInt(10))
|
|
|
|
testNonce = hexutil.Uint64(10)
|
2018-02-05 16:28:21 +00:00
|
|
|
)
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) setupTransactionPoolAPI(args SendTxArgs, returnNonce, resultNonce hexutil.Uint64, account *account.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-08-16 11:37:53 +00:00
|
|
|
if args.GasPrice == nil {
|
2018-02-05 16:28:21 +00:00
|
|
|
usedGasPrice = (*big.Int)(testGasPrice)
|
2018-06-20 14:51:55 +00:00
|
|
|
s.txServiceMock.EXPECT().GasPrice(gomock.Any()).Return(testGasPrice, nil)
|
2018-02-05 16:28:21 +00:00
|
|
|
} else {
|
2018-04-09 08:18:22 +00:00
|
|
|
usedGasPrice = (*big.Int)(args.GasPrice)
|
2018-02-05 16:28:21 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
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
|
|
|
}
|
2018-08-16 11:37:53 +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-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) 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-06-12 16:50:25 +00:00
|
|
|
*args.To,
|
2018-04-09 08:18:22 +00:00
|
|
|
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-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestGasValues() {
|
2018-01-18 16:55:17 +00:00
|
|
|
key, _ := crypto.GenerateKey()
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: account.FromAddress(TestConfig.Account1.WalletAddress),
|
2018-01-18 16:55:17 +00:00
|
|
|
AccountKey: &keystore.Key{PrivateKey: key},
|
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
testCases := []struct {
|
2018-08-16 11:37:53 +00:00
|
|
|
name string
|
|
|
|
gas *hexutil.Uint64
|
|
|
|
gasPrice *hexutil.Big
|
2018-02-05 16:28:21 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"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-06-06 07:35:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nilSignTransactionSpecificArgs",
|
|
|
|
nil,
|
|
|
|
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{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-02-05 16:28:21 +00:00
|
|
|
Gas: testCase.gas,
|
|
|
|
GasPrice: testCase.gasPrice,
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
s.setupTransactionPoolAPI(args, testNonce, testNonce, selectedAccount, nil)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
hash, err := s.manager.SendTransaction(args, selectedAccount)
|
|
|
|
s.NoError(err)
|
|
|
|
s.False(reflect.DeepEqual(hash, gethcommon.Hash{}))
|
2018-02-05 16:28:21 +00:00
|
|
|
})
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestArgsValidation() {
|
|
|
|
args := SendTxArgs{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-08-16 11:37:53 +00:00
|
|
|
Data: hexutil.Bytes([]byte{0x01, 0x02}),
|
|
|
|
Input: hexutil.Bytes([]byte{0x02, 0x01}),
|
|
|
|
}
|
|
|
|
s.False(args.Valid())
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: account.FromAddress(TestConfig.Account1.WalletAddress),
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
_, err := s.manager.SendTransaction(args, selectedAccount)
|
|
|
|
s.EqualError(err, ErrInvalidSendTxArgs.Error())
|
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestAccountMismatch() {
|
2018-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2017-09-18 08:53:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
var err error
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// missing account
|
|
|
|
_, err = s.manager.SendTransaction(args, nil)
|
|
|
|
s.EqualError(err, account.ErrNoAccountSelected.Error())
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// mismatched accounts
|
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: account.FromAddress(TestConfig.Account2.WalletAddress),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
_, err = s.manager.SendTransaction(args, selectedAccount)
|
|
|
|
s.EqualError(err, ErrInvalidTxSender.Error())
|
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
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestLocalNonce() {
|
2018-02-07 22:23:57 +00:00
|
|
|
txCount := 3
|
|
|
|
key, _ := crypto.GenerateKey()
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: account.FromAddress(TestConfig.Account1.WalletAddress),
|
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
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
for i := 0; i < txCount; i++ {
|
2018-04-09 08:18:22 +00:00
|
|
|
args := SendTxArgs{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
s.setupTransactionPoolAPI(args, nonce, hexutil.Uint64(i), selectedAccount, nil)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
_, err := s.manager.SendTransaction(args, selectedAccount)
|
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{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
s.setupTransactionPoolAPI(args, nonce, nonce, selectedAccount, nil)
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
_, err := s.manager.SendTransaction(args, selectedAccount)
|
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{
|
2019-01-18 09:01:14 +00:00
|
|
|
From: account.FromAddress(TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.WalletAddress),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
_, err = s.manager.SendTransaction(args, selectedAccount)
|
|
|
|
s.EqualError(err, testErr.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
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestContractCreation() {
|
2018-04-07 19:16:32 +00:00
|
|
|
key, _ := crypto.GenerateKey()
|
|
|
|
testaddr := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
genesis := core.GenesisAlloc{
|
|
|
|
testaddr: {Balance: big.NewInt(100000000000)},
|
|
|
|
}
|
2018-08-27 08:22:21 +00:00
|
|
|
backend := backends.NewSimulatedBackend(genesis, math.MaxInt64)
|
2018-04-07 19:16:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
hash, err := s.manager.SendTransaction(tx, selectedAccount)
|
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)
|
|
|
|
}
|
2019-02-15 11:31:20 +00:00
|
|
|
|
|
|
|
func (s *TransactorSuite) TestSendTransactionWithSignature() {
|
|
|
|
privKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
address := crypto.PubkeyToAddress(privKey.PublicKey)
|
|
|
|
|
|
|
|
scenarios := []struct {
|
|
|
|
localNonce hexutil.Uint64
|
|
|
|
txNonce hexutil.Uint64
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
localNonce: hexutil.Uint64(0),
|
|
|
|
txNonce: hexutil.Uint64(0),
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
localNonce: hexutil.Uint64(1),
|
|
|
|
txNonce: hexutil.Uint64(0),
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
localNonce: hexutil.Uint64(0),
|
|
|
|
txNonce: hexutil.Uint64(1),
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, scenario := range scenarios {
|
|
|
|
desc := fmt.Sprintf("local nonce: %d, tx nonce: %d, expect error: %v", scenario.localNonce, scenario.txNonce, scenario.expectError)
|
|
|
|
s.T().Run(desc, func(t *testing.T) {
|
|
|
|
s.manager.localNonce.Store(address, uint64(scenario.localNonce))
|
|
|
|
|
|
|
|
nonce := scenario.txNonce
|
|
|
|
from := address
|
|
|
|
to := address
|
|
|
|
value := (*hexutil.Big)(big.NewInt(10))
|
|
|
|
gas := hexutil.Uint64(21000)
|
|
|
|
gasPrice := (*hexutil.Big)(big.NewInt(2000000000))
|
|
|
|
data := []byte{}
|
|
|
|
chainID := big.NewInt(int64(s.nodeConfig.NetworkID))
|
|
|
|
|
|
|
|
args := SendTxArgs{
|
|
|
|
From: from,
|
|
|
|
To: &to,
|
|
|
|
Gas: &gas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Nonce: &nonce,
|
|
|
|
Data: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// simulate transaction signed externally
|
|
|
|
signer := types.NewEIP155Signer(chainID)
|
|
|
|
tx := types.NewTransaction(uint64(nonce), to, (*big.Int)(value), uint64(gas), (*big.Int)(gasPrice), data)
|
|
|
|
hash := signer.Hash(tx)
|
|
|
|
sig, err := crypto.Sign(hash[:], privKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
txWithSig, err := tx.WithSignature(signer, sig)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
expectedEncodedTx, err := rlp.EncodeToBytes(txWithSig)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.txServiceMock.EXPECT().
|
|
|
|
GetTransactionCount(gomock.Any(), address, gethrpc.PendingBlockNumber).
|
|
|
|
Return(&scenario.localNonce, nil)
|
|
|
|
|
|
|
|
if !scenario.expectError {
|
|
|
|
s.txServiceMock.EXPECT().
|
|
|
|
SendRawTransaction(gomock.Any(), hexutil.Bytes(expectedEncodedTx)).
|
|
|
|
Return(gethcommon.Hash{}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.manager.SendTransactionWithSignature(args, sig)
|
|
|
|
if scenario.expectError {
|
|
|
|
s.Error(err)
|
|
|
|
// local nonce should not be incremented
|
|
|
|
resultNonce, _ := s.manager.localNonce.Load(args.From)
|
|
|
|
s.Equal(uint64(scenario.localNonce), resultNonce.(uint64))
|
|
|
|
} else {
|
|
|
|
s.NoError(err)
|
|
|
|
// local nonce should be incremented
|
|
|
|
resultNonce, _ := s.manager.localNonce.Load(args.From)
|
|
|
|
s.Equal(uint64(nonce)+1, resultNonce.(uint64))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
|
|
|
|
func (s *TransactorSuite) TestHashTransaction() {
|
|
|
|
privKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
address := crypto.PubkeyToAddress(privKey.PublicKey)
|
|
|
|
|
|
|
|
remoteNonce := hexutil.Uint64(1)
|
|
|
|
txNonce := hexutil.Uint64(0)
|
|
|
|
from := address
|
|
|
|
to := address
|
|
|
|
value := (*hexutil.Big)(big.NewInt(10))
|
|
|
|
gas := hexutil.Uint64(21000)
|
|
|
|
gasPrice := (*hexutil.Big)(big.NewInt(2000000000))
|
|
|
|
|
|
|
|
args := SendTxArgs{
|
|
|
|
From: from,
|
|
|
|
To: &to,
|
|
|
|
Gas: &gas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Nonce: &txNonce,
|
|
|
|
Data: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.txServiceMock.EXPECT().
|
|
|
|
GetTransactionCount(gomock.Any(), address, gethrpc.PendingBlockNumber).
|
|
|
|
Return(&remoteNonce, nil)
|
|
|
|
|
|
|
|
newArgs, hash, err := s.manager.HashTransaction(args)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
// args should be updated with the right nonce
|
|
|
|
s.NotEqual(*args.Nonce, *newArgs.Nonce)
|
|
|
|
s.Equal(remoteNonce, *newArgs.Nonce)
|
|
|
|
|
|
|
|
s.NotEqual(common.Hash{}, hash)
|
|
|
|
}
|