2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-15 11:31:20 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
2024-10-11 17:01:14 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
|
|
|
"github.com/status-im/status-go/rpc/chain/ethclient"
|
|
|
|
"github.com/status-im/status-go/rpc/chain/rpclimiter"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2024-09-20 09:08:11 +00:00
|
|
|
"go.uber.org/mock/gomock"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-10-15 15:59:17 +00:00
|
|
|
statusRpc "github.com/status-im/status-go/rpc"
|
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-01-18 16:55:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-12-11 13:59:37 +00:00
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
gethcrypto "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"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-12-19 18:27:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2024-07-08 09:02:06 +00:00
|
|
|
wallet_common "github.com/status-im/status-go/services/wallet/common"
|
2024-11-05 21:28:24 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/wallettypes"
|
2024-05-13 13:02:22 +00:00
|
|
|
"github.com/status-im/status-go/sqlite"
|
2019-10-04 15:21:24 +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"
|
2024-10-03 19:59:44 +00:00
|
|
|
mock_fake "github.com/status-im/status-go/transactions/fake"
|
2017-09-04 12:56:58 +00:00
|
|
|
)
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func TestTransactorSuite(t *testing.T) {
|
2019-10-04 15:21:24 +00:00
|
|
|
utils.Init()
|
2018-08-16 11:37:53 +00:00
|
|
|
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
|
2024-10-03 19:59:44 +00:00
|
|
|
txServiceMock *mock_fake.MockPublicTransactionPoolAPI
|
2018-03-27 16:30:37 +00:00
|
|
|
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)
|
2022-12-19 12:37:37 +00:00
|
|
|
|
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()
|
2024-05-13 13:02:22 +00:00
|
|
|
db, err := sqlite.OpenUnecryptedDB(sqlite.InMemoryPath) // dummy to make rpc.Client happy
|
|
|
|
s.Require().NoError(err)
|
2024-10-15 15:59:17 +00:00
|
|
|
|
|
|
|
config := statusRpc.ClientConfig{
|
|
|
|
Client: s.client,
|
|
|
|
UpstreamChainID: chainID,
|
|
|
|
Networks: nil,
|
|
|
|
DB: db,
|
|
|
|
WalletFeed: nil,
|
|
|
|
ProviderConfigs: nil,
|
|
|
|
}
|
|
|
|
rpcClient, _ := statusRpc.NewClient(config)
|
|
|
|
|
2022-12-19 12:37:37 +00:00
|
|
|
rpcClient.UpstreamChainID = chainID
|
2024-10-11 17:01:14 +00:00
|
|
|
|
|
|
|
ethClients := []ethclient.RPSLimitedEthClientInterface{
|
|
|
|
ethclient.NewRPSLimitedEthClient(s.client, rpclimiter.NewRPCRpsLimiter(), "local-1-chain-id-1"),
|
|
|
|
}
|
2024-10-15 15:59:17 +00:00
|
|
|
localClient := chain.NewClient(ethClients, chainID, nil)
|
2024-10-11 17:01:14 +00:00
|
|
|
rpcClient.SetClient(chainID, localClient)
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
nodeConfig, err := utils.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
|
|
|
)
|
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
func (s *TransactorSuite) setupTransactionPoolAPI(args wallettypes.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
|
2019-12-11 13:59:37 +00:00
|
|
|
s.txServiceMock.EXPECT().GetTransactionCount(gomock.Any(), gomock.Eq(common.Address(account.Address)), gethrpc.PendingBlockNumber).Return(&returnNonce, nil)
|
2021-07-14 07:48:01 +00:00
|
|
|
if !args.IsDynamicFeeTx() {
|
|
|
|
if args.GasPrice == nil {
|
|
|
|
usedGasPrice = (*big.Int)(testGasPrice)
|
|
|
|
s.txServiceMock.EXPECT().GasPrice(gomock.Any()).Return(testGasPrice, nil)
|
|
|
|
} else {
|
|
|
|
usedGasPrice = (*big.Int)(args.GasPrice)
|
|
|
|
}
|
2024-07-17 08:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if args.Gas == nil {
|
|
|
|
s.txServiceMock.EXPECT().EstimateGas(gomock.Any(), gomock.Any()).Return(testGas, nil)
|
|
|
|
usedGas = testGas
|
|
|
|
} else {
|
|
|
|
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.
|
2019-12-11 13:59:37 +00:00
|
|
|
s.txServiceMock.EXPECT().SendRawTransaction(gomock.Any(), data).Return(common.Hash{}, txErr)
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
func (s *TransactorSuite) rlpEncodeTx(args wallettypes.SendTxArgs, config *params.NodeConfig, account *account.SelectedExtKey, nonce *hexutil.Uint64, gas hexutil.Uint64, gasPrice *big.Int) hexutil.Bytes {
|
2021-07-14 07:48:01 +00:00
|
|
|
var txData gethtypes.TxData
|
|
|
|
to := common.Address(*args.To)
|
|
|
|
if args.IsDynamicFeeTx() {
|
|
|
|
gasTipCap := (*big.Int)(args.MaxPriorityFeePerGas)
|
|
|
|
gasFeeCap := (*big.Int)(args.MaxFeePerGas)
|
|
|
|
|
|
|
|
txData = &gethtypes.DynamicFeeTx{
|
|
|
|
Nonce: uint64(*nonce),
|
|
|
|
Gas: uint64(gas),
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
To: &to,
|
|
|
|
Value: args.Value.ToInt(),
|
|
|
|
Data: args.GetInput(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
txData = &gethtypes.LegacyTx{
|
|
|
|
Nonce: uint64(*nonce),
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Gas: uint64(gas),
|
|
|
|
To: &to,
|
|
|
|
Value: args.Value.ToInt(),
|
|
|
|
Data: args.GetInput(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newTx := gethtypes.NewTx(txData)
|
2022-12-19 12:37:37 +00:00
|
|
|
chainID := big.NewInt(int64(s.nodeConfig.NetworkID))
|
2022-06-09 13:09:56 +00:00
|
|
|
|
2021-07-14 07:48:01 +00:00
|
|
|
signedTx, err := gethtypes.SignTx(newTx, gethtypes.NewLondonSigner(chainID), account.AccountKey.PrivateKey)
|
2018-02-05 16:28:21 +00:00
|
|
|
s.NoError(err)
|
2021-07-14 11:05:12 +00:00
|
|
|
data, err := signedTx.MarshalBinary()
|
2018-02-05 16:28:21 +00:00
|
|
|
s.NoError(err)
|
|
|
|
return hexutil.Bytes(data)
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestGasValues() {
|
2019-12-11 13:59:37 +00:00
|
|
|
key, _ := gethcrypto.GenerateKey()
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2019-10-04 15:21:24 +00:00
|
|
|
Address: account.FromAddress(utils.TestConfig.Account1.WalletAddress),
|
2019-12-19 18:27:27 +00:00
|
|
|
AccountKey: &types.Key{PrivateKey: key},
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
testCases := []struct {
|
2021-07-14 07:48:01 +00:00
|
|
|
name string
|
|
|
|
gas *hexutil.Uint64
|
|
|
|
gasPrice *hexutil.Big
|
|
|
|
maxFeePerGas *hexutil.Big
|
|
|
|
maxPriorityFeePerGas *hexutil.Big
|
2018-02-05 16:28:21 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"noGasDef",
|
|
|
|
nil,
|
|
|
|
nil,
|
2021-07-14 07:48:01 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
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,
|
2021-07-14 07:48:01 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2018-02-05 16:28:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"gasPriceDefined",
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
2021-07-14 07:48:01 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2018-06-06 07:35:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"nilSignTransactionSpecificArgs",
|
|
|
|
nil,
|
|
|
|
nil,
|
2021-07-14 07:48:01 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"maxFeeAndPriorityset",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
testGasPrice,
|
|
|
|
testGasPrice,
|
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()
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2021-07-14 07:48:01 +00:00
|
|
|
From: account.FromAddress(utils.TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(utils.TestConfig.Account2.WalletAddress),
|
|
|
|
Gas: testCase.gas,
|
|
|
|
GasPrice: testCase.gasPrice,
|
|
|
|
MaxFeePerGas: testCase.maxFeePerGas,
|
|
|
|
MaxPriorityFeePerGas: testCase.maxPriorityFeePerGas,
|
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
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
hash, _, err := s.manager.SendTransaction(args, selectedAccount, -1)
|
2018-08-16 11:37:53 +00:00
|
|
|
s.NoError(err)
|
2019-12-11 13:59:37 +00:00
|
|
|
s.False(reflect.DeepEqual(hash, common.Hash{}))
|
2018-02-05 16:28:21 +00:00
|
|
|
})
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
func (s *TransactorSuite) setupBuildTransactionMocks(args wallettypes.SendTxArgs, account *account.SelectedExtKey) {
|
2024-07-17 08:11:34 +00:00
|
|
|
s.txServiceMock.EXPECT().GetTransactionCount(gomock.Any(), gomock.Eq(common.Address(account.Address)), gethrpc.PendingBlockNumber).Return(&testNonce, nil)
|
|
|
|
|
|
|
|
if !args.IsDynamicFeeTx() && args.GasPrice == nil {
|
|
|
|
s.txServiceMock.EXPECT().GasPrice(gomock.Any()).Return(testGasPrice, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.Gas == nil {
|
|
|
|
s.txServiceMock.EXPECT().EstimateGas(gomock.Any(), gomock.Any()).Return(testGas, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TransactorSuite) TestBuildAndValidateTransaction() {
|
|
|
|
key, _ := gethcrypto.GenerateKey()
|
|
|
|
selectedAccount := &account.SelectedExtKey{
|
|
|
|
Address: account.FromAddress(utils.TestConfig.Account1.WalletAddress),
|
|
|
|
AccountKey: &types.Key{PrivateKey: key},
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := s.nodeConfig.NetworkID
|
|
|
|
fromAddress := account.FromAddress(utils.TestConfig.Account1.WalletAddress)
|
|
|
|
toAddress := account.ToAddress(utils.TestConfig.Account2.WalletAddress)
|
|
|
|
value := (*hexutil.Big)(big.NewInt(10))
|
|
|
|
|
|
|
|
expectedGasPrice := (*big.Int)(testGasPrice)
|
|
|
|
expectedGas := uint64(testGas)
|
|
|
|
expectedNonce := uint64(testNonce)
|
|
|
|
|
|
|
|
s.T().Run("DynamicFeeTransaction", func(t *testing.T) {
|
|
|
|
s.SetupTest()
|
|
|
|
|
|
|
|
gas := hexutil.Uint64(21000)
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2024-07-17 08:11:34 +00:00
|
|
|
From: fromAddress,
|
|
|
|
To: toAddress,
|
|
|
|
Gas: &gas,
|
|
|
|
Value: value,
|
|
|
|
MaxFeePerGas: testGasPrice,
|
|
|
|
MaxPriorityFeePerGas: testGasPrice,
|
|
|
|
}
|
|
|
|
s.setupBuildTransactionMocks(args, selectedAccount)
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
tx, _, err := s.manager.ValidateAndBuildTransaction(chainID, args, -1)
|
2024-07-17 08:11:34 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(tx.Gas(), uint64(gas), "The gas shouldn't be estimated, but should use the gas from the Tx")
|
|
|
|
s.Equal(tx.GasFeeCap(), expectedGasPrice, "The maxFeePerGas should be the same as in the original Tx")
|
|
|
|
s.Equal(tx.GasTipCap(), expectedGasPrice, "The maxPriorityFeePerGas should be the same as in the original Tx")
|
|
|
|
s.Equal(tx.Type(), uint8(gethtypes.DynamicFeeTxType), "The transaction type should be DynamicFeeTxType")
|
|
|
|
})
|
|
|
|
|
|
|
|
s.T().Run("DynamicFeeTransaction with gas estimation", func(t *testing.T) {
|
|
|
|
s.SetupTest()
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2024-07-17 08:11:34 +00:00
|
|
|
From: fromAddress,
|
|
|
|
To: toAddress,
|
|
|
|
Value: value,
|
|
|
|
MaxFeePerGas: testGasPrice,
|
|
|
|
MaxPriorityFeePerGas: testGasPrice,
|
|
|
|
}
|
|
|
|
s.setupBuildTransactionMocks(args, selectedAccount)
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
tx, _, err := s.manager.ValidateAndBuildTransaction(chainID, args, -1)
|
2024-07-17 08:11:34 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(tx.Gas(), expectedGas, "The gas should be estimated if not present in the original Tx")
|
|
|
|
s.Equal(tx.Nonce(), expectedNonce, "The nonce should be added if not present in the original Tx")
|
|
|
|
s.Equal(tx.GasFeeCap(), expectedGasPrice, "The maxFeePerGas should be the same as in the original Tx")
|
|
|
|
s.Equal(tx.GasTipCap(), expectedGasPrice, "The maxPriorityFeePerGas should be the same as in the original Tx")
|
|
|
|
s.Equal(tx.Type(), uint8(gethtypes.DynamicFeeTxType), "The transaction type should be DynamicFeeTxType")
|
|
|
|
})
|
|
|
|
|
|
|
|
s.T().Run("LegacyTransaction", func(t *testing.T) {
|
|
|
|
s.SetupTest()
|
|
|
|
|
|
|
|
gas := hexutil.Uint64(21000)
|
|
|
|
gasPrice := (*hexutil.Big)(big.NewInt(10))
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2024-07-17 08:11:34 +00:00
|
|
|
From: fromAddress,
|
|
|
|
To: toAddress,
|
|
|
|
Value: value,
|
|
|
|
Gas: &gas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
}
|
|
|
|
s.setupBuildTransactionMocks(args, selectedAccount)
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
tx, _, err := s.manager.ValidateAndBuildTransaction(chainID, args, -1)
|
2024-07-17 08:11:34 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(tx.Gas(), uint64(gas), "The gas shouldn't be estimated, but should use the gas from the Tx")
|
|
|
|
s.Equal(tx.GasPrice(), expectedGasPrice, "The gasPrice should be the same as in the original Tx")
|
|
|
|
s.Equal(tx.Type(), uint8(gethtypes.LegacyTxType), "The transaction type should be LegacyTxType")
|
|
|
|
})
|
|
|
|
s.T().Run("LegacyTransaction without gas estimation", func(t *testing.T) {
|
|
|
|
s.SetupTest()
|
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2024-07-17 08:11:34 +00:00
|
|
|
From: fromAddress,
|
|
|
|
To: toAddress,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
s.setupBuildTransactionMocks(args, selectedAccount)
|
|
|
|
|
2024-08-12 12:07:32 +00:00
|
|
|
tx, _, err := s.manager.ValidateAndBuildTransaction(chainID, args, -1)
|
2024-07-17 08:11:34 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(tx.Gas(), expectedGas, "The gas should be estimated if not present in the original Tx")
|
|
|
|
s.Equal(tx.GasPrice(), expectedGasPrice, "The gasPrice should be estimated if not present in the original Tx")
|
|
|
|
s.Equal(tx.Type(), uint8(gethtypes.LegacyTxType), "The transaction type should be LegacyTxType")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestArgsValidation() {
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2019-12-19 18:27:27 +00:00
|
|
|
From: account.FromAddress(utils.TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(utils.TestConfig.Account2.WalletAddress),
|
|
|
|
Data: types.HexBytes([]byte{0x01, 0x02}),
|
|
|
|
Input: types.HexBytes([]byte{0x02, 0x01}),
|
2018-08-16 11:37:53 +00:00
|
|
|
}
|
|
|
|
s.False(args.Valid())
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount := &account.SelectedExtKey{
|
2019-10-04 15:21:24 +00:00
|
|
|
Address: account.FromAddress(utils.TestConfig.Account1.WalletAddress),
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2024-08-12 12:07:32 +00:00
|
|
|
_, _, err := s.manager.SendTransaction(args, selectedAccount, -1)
|
2024-11-05 21:28:24 +00:00
|
|
|
s.EqualError(err, wallettypes.ErrInvalidSendTxArgs.Error())
|
2018-08-16 11:37:53 +00:00
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func (s *TransactorSuite) TestAccountMismatch() {
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2019-12-19 18:27:27 +00:00
|
|
|
From: account.FromAddress(utils.TestConfig.Account1.WalletAddress),
|
|
|
|
To: account.ToAddress(utils.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
|
2024-08-12 12:07:32 +00:00
|
|
|
_, _, err = s.manager.SendTransaction(args, nil, -1)
|
2018-08-16 11:37:53 +00:00
|
|
|
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-10-04 15:21:24 +00:00
|
|
|
Address: account.FromAddress(utils.TestConfig.Account2.WalletAddress),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
2024-08-12 12:07:32 +00:00
|
|
|
_, _, err = s.manager.SendTransaction(args, selectedAccount, -1)
|
2024-11-05 21:28:24 +00:00
|
|
|
s.EqualError(err, wallettypes.ErrInvalidTxSender.Error())
|
2018-02-07 22:23:57 +00:00
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
|
2019-02-15 11:31:20 +00:00
|
|
|
func (s *TransactorSuite) TestSendTransactionWithSignature() {
|
2019-12-19 18:27:27 +00:00
|
|
|
privKey, err := crypto.GenerateKey()
|
2019-02-15 11:31:20 +00:00
|
|
|
s.Require().NoError(err)
|
2019-12-19 18:27:27 +00:00
|
|
|
address := crypto.PubkeyToAddress(privKey.PublicKey)
|
2019-02-15 11:31:20 +00:00
|
|
|
|
|
|
|
scenarios := []struct {
|
2023-12-19 13:38:01 +00:00
|
|
|
nonceFromNetwork hexutil.Uint64
|
|
|
|
txNonce hexutil.Uint64
|
|
|
|
expectError bool
|
2019-02-15 11:31:20 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-12-19 13:38:01 +00:00
|
|
|
nonceFromNetwork: hexutil.Uint64(0),
|
|
|
|
txNonce: hexutil.Uint64(0),
|
|
|
|
expectError: false,
|
2019-02-15 11:31:20 +00:00
|
|
|
},
|
|
|
|
{
|
2023-12-19 13:38:01 +00:00
|
|
|
nonceFromNetwork: hexutil.Uint64(0),
|
|
|
|
txNonce: hexutil.Uint64(1),
|
|
|
|
expectError: true,
|
2019-02-15 11:31:20 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-18 16:48:02 +00:00
|
|
|
for _, localScenario := range scenarios {
|
|
|
|
// to satisfy gosec: C601 checks
|
|
|
|
scenario := localScenario
|
2023-12-19 13:38:01 +00:00
|
|
|
desc := fmt.Sprintf("nonceFromNetwork: %d, tx nonce: %d, expect error: %v", scenario.nonceFromNetwork, scenario.txNonce, scenario.expectError)
|
2019-02-15 11:31:20 +00:00
|
|
|
s.T().Run(desc, func(t *testing.T) {
|
|
|
|
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))
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2019-02-15 11:31:20 +00:00
|
|
|
From: from,
|
|
|
|
To: &to,
|
|
|
|
Gas: &gas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Nonce: &nonce,
|
|
|
|
Data: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
// simulate transaction signed externally
|
2021-07-14 07:48:01 +00:00
|
|
|
signer := gethtypes.NewLondonSigner(chainID)
|
2019-12-19 18:27:27 +00:00
|
|
|
tx := gethtypes.NewTransaction(uint64(nonce), common.Address(to), (*big.Int)(value), uint64(gas), (*big.Int)(gasPrice), data)
|
2019-02-15 11:31:20 +00:00
|
|
|
hash := signer.Hash(tx)
|
2019-12-11 13:59:37 +00:00
|
|
|
sig, err := gethcrypto.Sign(hash[:], privKey)
|
2019-02-15 11:31:20 +00:00
|
|
|
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().
|
2019-12-19 18:27:27 +00:00
|
|
|
GetTransactionCount(gomock.Any(), common.Address(address), gethrpc.PendingBlockNumber).
|
2023-12-19 13:38:01 +00:00
|
|
|
Return(&scenario.nonceFromNetwork, nil)
|
2019-02-15 11:31:20 +00:00
|
|
|
|
|
|
|
if !scenario.expectError {
|
|
|
|
s.txServiceMock.EXPECT().
|
|
|
|
SendRawTransaction(gomock.Any(), hexutil.Bytes(expectedEncodedTx)).
|
2019-12-11 13:59:37 +00:00
|
|
|
Return(common.Hash{}, nil)
|
2019-02-15 11:31:20 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 08:31:13 +00:00
|
|
|
tx, err = s.manager.BuildTransactionWithSignature(s.nodeConfig.NetworkID, args, sig)
|
2019-02-15 11:31:20 +00:00
|
|
|
if scenario.expectError {
|
|
|
|
s.Error(err)
|
|
|
|
} else {
|
|
|
|
s.NoError(err)
|
2024-05-26 08:31:13 +00:00
|
|
|
|
|
|
|
_, err = s.manager.SendTransactionWithSignature(common.Address(args.From), args.Symbol, args.MultiTransactionID, tx)
|
|
|
|
if scenario.expectError {
|
|
|
|
s.Error(err)
|
|
|
|
} else {
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
2019-02-15 11:31:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 09:53:39 +00:00
|
|
|
|
2019-03-01 13:49:30 +00:00
|
|
|
func (s *TransactorSuite) TestSendTransactionWithSignature_InvalidSignature() {
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{}
|
2024-05-26 08:31:13 +00:00
|
|
|
_, err := s.manager.BuildTransactionWithSignature(1, args, []byte{})
|
2019-03-01 13:49:30 +00:00
|
|
|
s.Equal(ErrInvalidSignatureSize, err)
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
func (s *TransactorSuite) TestHashTransaction() {
|
2019-12-19 18:27:27 +00:00
|
|
|
privKey, err := crypto.GenerateKey()
|
2019-02-21 09:53:39 +00:00
|
|
|
s.Require().NoError(err)
|
2019-12-19 18:27:27 +00:00
|
|
|
address := crypto.PubkeyToAddress(privKey.PublicKey)
|
2019-02-21 09:53:39 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
args := wallettypes.SendTxArgs{
|
2019-02-21 09:53:39 +00:00
|
|
|
From: from,
|
|
|
|
To: &to,
|
|
|
|
Gas: &gas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Nonce: &txNonce,
|
|
|
|
Data: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.txServiceMock.EXPECT().
|
2019-12-19 18:27:27 +00:00
|
|
|
GetTransactionCount(gomock.Any(), common.Address(address), gethrpc.PendingBlockNumber).
|
2019-02-21 09:53:39 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-07-08 09:02:06 +00:00
|
|
|
|
|
|
|
func (s *TransactorSuite) TestStoreAndTrackPendingTx() {
|
|
|
|
s.Nil(s.manager.pendingTracker)
|
|
|
|
|
|
|
|
// Empty tracker doesn't produce error
|
|
|
|
err := s.manager.StoreAndTrackPendingTx(common.Address{}, "", 0, wallet_common.MultiTransactionIDType(0), nil)
|
|
|
|
s.NoError(err)
|
|
|
|
}
|