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-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"
|
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"
|
|
|
|
"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-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-07-25 14:03:35 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig("/tmp", "", params.FleetBeta, 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{
|
|
|
|
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-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{
|
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-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{
|
|
|
|
From: account.FromAddress(TestConfig.Account1.Address),
|
|
|
|
To: account.ToAddress(TestConfig.Account2.Address),
|
|
|
|
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{
|
2018-08-16 11:37:53 +00:00
|
|
|
Address: account.FromAddress(TestConfig.Account1.Address),
|
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{
|
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-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{
|
|
|
|
Address: account.FromAddress(TestConfig.Account2.Address),
|
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{
|
|
|
|
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
|
|
|
|
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-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{
|
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-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{
|
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-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)
|
|
|
|
}
|