2018-04-09 10:18:22 +02:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
2018-08-16 13:37:53 +02:00
|
|
|
"bytes"
|
2018-04-09 10:18:22 +02:00
|
|
|
"context"
|
2019-03-01 14:49:30 +01:00
|
|
|
"errors"
|
2019-02-15 12:31:20 +01:00
|
|
|
"fmt"
|
2018-04-09 10:18:22 +02:00
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2019-12-19 19:27:27 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-21 10:53:39 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-11-23 18:57:05 +01:00
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
2018-04-09 10:18:22 +02:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
|
2018-06-08 13:29:50 +02:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-12-19 19:27:27 +01:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2019-11-23 18:57:05 +01:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-06-08 13:29:50 +02:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-04-09 10:18:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sendTxTimeout defines how many seconds to wait before returning result in sentTransaction().
|
2018-04-10 12:02:54 +02:00
|
|
|
sendTxTimeout = 300 * time.Second
|
2018-04-09 10:18:22 +02:00
|
|
|
|
|
|
|
defaultGas = 90000
|
2019-03-01 14:49:30 +01:00
|
|
|
|
|
|
|
validSignatureSize = 65
|
2018-04-09 10:18:22 +02:00
|
|
|
)
|
|
|
|
|
2019-03-01 14:49:30 +01:00
|
|
|
// ErrInvalidSignatureSize is returned if a signature is not 65 bytes to avoid panic from go-ethereum
|
|
|
|
var ErrInvalidSignatureSize = errors.New("signature size must be 65")
|
|
|
|
|
2019-02-15 12:31:20 +01:00
|
|
|
type ErrBadNonce struct {
|
2019-02-21 10:53:39 +01:00
|
|
|
nonce uint64
|
|
|
|
expectedNonce uint64
|
2019-02-15 12:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrBadNonce) Error() string {
|
2019-02-21 10:53:39 +01:00
|
|
|
return fmt.Sprintf("bad nonce. expected %d, got %d", e.expectedNonce, e.nonce)
|
2019-02-15 12:31:20 +01:00
|
|
|
}
|
|
|
|
|
2018-04-09 10:18:22 +02:00
|
|
|
// Transactor validates, signs transactions.
|
|
|
|
// It uses upstream to propagate transactions to the Ethereum network.
|
|
|
|
type Transactor struct {
|
|
|
|
sender ethereum.TransactionSender
|
|
|
|
pendingNonceProvider PendingNonceProvider
|
|
|
|
gasCalculator GasCalculator
|
|
|
|
sendTxTimeout time.Duration
|
|
|
|
rpcCallTimeout time.Duration
|
|
|
|
networkID uint64
|
|
|
|
|
|
|
|
addrLock *AddrLocker
|
|
|
|
localNonce sync.Map
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransactor returns a new Manager.
|
2018-08-16 13:37:53 +02:00
|
|
|
func NewTransactor() *Transactor {
|
2018-04-09 10:18:22 +02:00
|
|
|
return &Transactor{
|
2018-08-16 13:37:53 +02:00
|
|
|
addrLock: &AddrLocker{},
|
|
|
|
sendTxTimeout: sendTxTimeout,
|
|
|
|
localNonce: sync.Map{},
|
|
|
|
log: log.New("package", "status-go/transactions.Manager"),
|
2018-04-09 10:18:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNetworkID selects a correct network.
|
|
|
|
func (t *Transactor) SetNetworkID(networkID uint64) {
|
|
|
|
t.networkID = networkID
|
|
|
|
}
|
|
|
|
|
2018-04-10 12:02:54 +02:00
|
|
|
// SetRPC sets RPC params, a client and a timeout
|
|
|
|
func (t *Transactor) SetRPC(rpcClient *rpc.Client, timeout time.Duration) {
|
2018-04-09 10:18:22 +02:00
|
|
|
rpcWrapper := newRPCWrapper(rpcClient)
|
|
|
|
t.sender = rpcWrapper
|
|
|
|
t.pendingNonceProvider = rpcWrapper
|
|
|
|
t.gasCalculator = rpcWrapper
|
2018-04-10 12:02:54 +02:00
|
|
|
t.rpcCallTimeout = timeout
|
2018-04-09 10:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction is an implementation of eth_sendTransaction. It queues the tx to the sign queue.
|
2019-11-23 18:57:05 +01:00
|
|
|
func (t *Transactor) SendTransaction(sendArgs SendTxArgs, verifiedAccount *account.SelectedExtKey) (hash types.Hash, err error) {
|
2018-08-16 13:37:53 +02:00
|
|
|
hash, err = t.validateAndPropagate(verifiedAccount, sendArgs)
|
|
|
|
return
|
2018-04-09 10:18:22 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 12:31:20 +01:00
|
|
|
// SendTransactionWithSignature receive a transaction and a signature, serialize them together and propage it to the network.
|
|
|
|
// It's different from eth_sendRawTransaction because it receives a signature and not a serialized transaction with signature.
|
|
|
|
// Since the transactions is already signed, we assume it was validated and used the right nonce.
|
2019-11-23 18:57:05 +01:00
|
|
|
func (t *Transactor) SendTransactionWithSignature(args SendTxArgs, sig []byte) (hash types.Hash, err error) {
|
2019-02-15 12:31:20 +01:00
|
|
|
if !args.Valid() {
|
|
|
|
return hash, ErrInvalidSendTxArgs
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:49:30 +01:00
|
|
|
if len(sig) != validSignatureSize {
|
|
|
|
return hash, ErrInvalidSignatureSize
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:31:20 +01:00
|
|
|
chainID := big.NewInt(int64(t.networkID))
|
2019-11-23 18:57:05 +01:00
|
|
|
signer := gethtypes.NewEIP155Signer(chainID)
|
2019-02-15 12:31:20 +01:00
|
|
|
|
2019-02-21 10:53:39 +01:00
|
|
|
tx := t.buildTransaction(args)
|
2019-02-15 12:31:20 +01:00
|
|
|
t.addrLock.LockAddr(args.From)
|
|
|
|
defer func() {
|
|
|
|
// nonce should be incremented only if tx completed without error
|
|
|
|
// and if no other transactions have been sent while signing the current one.
|
|
|
|
if err == nil {
|
2019-02-21 10:53:39 +01:00
|
|
|
t.localNonce.Store(args.From, uint64(*args.Nonce)+1)
|
2019-02-15 12:31:20 +01:00
|
|
|
}
|
|
|
|
t.addrLock.UnlockAddr(args.From)
|
|
|
|
}()
|
|
|
|
|
2019-02-21 10:53:39 +01:00
|
|
|
expectedNonce, err := t.getTransactionNonce(args)
|
2019-02-15 12:31:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:53:39 +01:00
|
|
|
if tx.Nonce() != expectedNonce {
|
|
|
|
return hash, &ErrBadNonce{tx.Nonce(), expectedNonce}
|
2019-02-15 12:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
signedTx, err := tx.WithSignature(signer, sig)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 10:53:39 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
2019-02-15 12:31:20 +01:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := t.sender.SendTransaction(ctx, signedTx); err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:57:05 +01:00
|
|
|
return types.Hash(signedTx.Hash()), nil
|
2019-02-15 12:31:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-23 18:57:05 +01:00
|
|
|
func (t *Transactor) HashTransaction(args SendTxArgs) (validatedArgs SendTxArgs, hash types.Hash, err error) {
|
2019-02-21 10:53:39 +01:00
|
|
|
if !args.Valid() {
|
|
|
|
return validatedArgs, hash, ErrInvalidSendTxArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
validatedArgs = args
|
|
|
|
|
|
|
|
t.addrLock.LockAddr(args.From)
|
|
|
|
defer func() {
|
|
|
|
t.addrLock.UnlockAddr(args.From)
|
|
|
|
}()
|
|
|
|
|
|
|
|
nonce, err := t.getTransactionNonce(validatedArgs)
|
|
|
|
if err != nil {
|
|
|
|
return validatedArgs, hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
|
|
|
if args.GasPrice == nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
|
|
|
gasPrice, err = t.gasCalculator.SuggestGasPrice(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return validatedArgs, hash, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := big.NewInt(int64(t.networkID))
|
|
|
|
value := (*big.Int)(args.Value)
|
|
|
|
|
|
|
|
var gas uint64
|
|
|
|
if args.Gas == nil {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2019-12-19 19:27:27 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
gethTo common.Address
|
|
|
|
gethToPtr *common.Address
|
|
|
|
)
|
|
|
|
if args.To != nil {
|
|
|
|
gethTo = common.Address(*args.To)
|
|
|
|
gethToPtr = &gethTo
|
|
|
|
}
|
2019-02-21 10:53:39 +01:00
|
|
|
gas, err = t.gasCalculator.EstimateGas(ctx, ethereum.CallMsg{
|
2019-12-19 19:27:27 +01:00
|
|
|
From: common.Address(args.From),
|
|
|
|
To: gethToPtr,
|
2019-02-21 10:53:39 +01:00
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return validatedArgs, hash, err
|
|
|
|
}
|
|
|
|
if gas < defaultGas {
|
|
|
|
t.log.Info("default gas will be used because estimated is lower", "estimated", gas, "default", defaultGas)
|
|
|
|
gas = defaultGas
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
|
|
|
|
newNonce := hexutil.Uint64(nonce)
|
|
|
|
newGas := hexutil.Uint64(gas)
|
|
|
|
validatedArgs.Nonce = &newNonce
|
|
|
|
validatedArgs.GasPrice = (*hexutil.Big)(gasPrice)
|
|
|
|
validatedArgs.Gas = &newGas
|
|
|
|
|
|
|
|
tx := t.buildTransaction(validatedArgs)
|
2019-11-23 18:57:05 +01:00
|
|
|
hash = types.Hash(gethtypes.NewEIP155Signer(chainID).Hash(tx))
|
2019-02-21 10:53:39 +01:00
|
|
|
|
|
|
|
return validatedArgs, hash, nil
|
|
|
|
}
|
|
|
|
|
2018-04-09 10:18:22 +02:00
|
|
|
// make sure that only account which created the tx can complete it
|
|
|
|
func (t *Transactor) validateAccount(args SendTxArgs, selectedAccount *account.SelectedExtKey) error {
|
|
|
|
if selectedAccount == nil {
|
|
|
|
return account.ErrNoAccountSelected
|
|
|
|
}
|
|
|
|
|
2018-08-16 13:37:53 +02:00
|
|
|
if !bytes.Equal(args.From.Bytes(), selectedAccount.Address.Bytes()) {
|
|
|
|
return ErrInvalidTxSender
|
2018-04-09 10:18:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:57:05 +01:00
|
|
|
func (t *Transactor) validateAndPropagate(selectedAccount *account.SelectedExtKey, args SendTxArgs) (hash types.Hash, err error) {
|
2018-06-12 18:50:25 +02:00
|
|
|
if err = t.validateAccount(args, selectedAccount); err != nil {
|
2018-04-09 10:18:22 +02:00
|
|
|
return hash, err
|
|
|
|
}
|
2018-08-16 13:37:53 +02:00
|
|
|
|
2018-04-09 10:18:22 +02:00
|
|
|
if !args.Valid() {
|
|
|
|
return hash, ErrInvalidSendTxArgs
|
|
|
|
}
|
|
|
|
t.addrLock.LockAddr(args.From)
|
|
|
|
var localNonce uint64
|
|
|
|
if val, ok := t.localNonce.Load(args.From); ok {
|
|
|
|
localNonce = val.(uint64)
|
|
|
|
}
|
|
|
|
var nonce uint64
|
|
|
|
defer func() {
|
|
|
|
// nonce should be incremented only if tx completed without error
|
|
|
|
// if upstream node returned nonce higher than ours we will stick to it
|
|
|
|
if err == nil {
|
|
|
|
t.localNonce.Store(args.From, nonce+1)
|
|
|
|
}
|
|
|
|
t.addrLock.UnlockAddr(args.From)
|
|
|
|
|
|
|
|
}()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2019-12-19 19:27:27 +01:00
|
|
|
nonce, err = t.pendingNonceProvider.PendingNonceAt(ctx, common.Address(args.From))
|
2018-04-09 10:18:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
// if upstream node returned nonce higher than ours we will use it, as it probably means
|
|
|
|
// that another client was used for sending transactions
|
|
|
|
if localNonce > nonce {
|
|
|
|
nonce = localNonce
|
|
|
|
}
|
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
|
|
|
if args.GasPrice == nil {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
|
|
|
gasPrice, err = t.gasCalculator.SuggestGasPrice(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := big.NewInt(int64(t.networkID))
|
|
|
|
value := (*big.Int)(args.Value)
|
|
|
|
|
|
|
|
var gas uint64
|
|
|
|
if args.Gas == nil {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2019-12-19 19:27:27 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
gethTo common.Address
|
|
|
|
gethToPtr *common.Address
|
|
|
|
)
|
|
|
|
if args.To != nil {
|
|
|
|
gethTo = common.Address(*args.To)
|
|
|
|
gethToPtr = &gethTo
|
|
|
|
}
|
2018-04-09 10:18:22 +02:00
|
|
|
gas, err = t.gasCalculator.EstimateGas(ctx, ethereum.CallMsg{
|
2019-12-19 19:27:27 +01:00
|
|
|
From: common.Address(args.From),
|
|
|
|
To: gethToPtr,
|
2018-04-09 10:18:22 +02:00
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
if gas < defaultGas {
|
2018-08-16 13:37:53 +02:00
|
|
|
t.log.Info("default gas will be used because estimated is lower", "estimated", gas, "default", defaultGas)
|
2018-04-09 10:18:22 +02:00
|
|
|
gas = defaultGas
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
|
2019-12-19 19:27:27 +01:00
|
|
|
tx := t.buildTransactionWithOverrides(nonce, value, gas, gasPrice, args)
|
2019-02-21 10:53:39 +01:00
|
|
|
|
2019-11-23 18:57:05 +01:00
|
|
|
signedTx, err := gethtypes.SignTx(tx, gethtypes.NewEIP155Signer(chainID), selectedAccount.AccountKey.PrivateKey)
|
2018-04-09 10:18:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2018-06-06 09:35:32 +02:00
|
|
|
|
2018-04-09 10:18:22 +02:00
|
|
|
if err := t.sender.SendTransaction(ctx, signedTx); err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
2019-11-23 18:57:05 +01:00
|
|
|
return types.Hash(signedTx.Hash()), nil
|
2018-04-09 10:18:22 +02:00
|
|
|
}
|
2019-02-21 10:53:39 +01:00
|
|
|
|
2019-11-23 18:57:05 +01:00
|
|
|
func (t *Transactor) buildTransaction(args SendTxArgs) *gethtypes.Transaction {
|
2019-02-21 10:53:39 +01:00
|
|
|
nonce := uint64(*args.Nonce)
|
|
|
|
value := (*big.Int)(args.Value)
|
|
|
|
gas := uint64(*args.Gas)
|
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
|
|
|
|
2019-12-19 19:27:27 +01:00
|
|
|
return t.buildTransactionWithOverrides(nonce, value, gas, gasPrice, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) buildTransactionWithOverrides(nonce uint64, value *big.Int, gas uint64, gasPrice *big.Int, args SendTxArgs) *gethtypes.Transaction {
|
2019-11-23 18:57:05 +01:00
|
|
|
var tx *gethtypes.Transaction
|
2019-02-21 10:53:39 +01:00
|
|
|
|
|
|
|
if args.To != nil {
|
2019-12-19 19:27:27 +01:00
|
|
|
tx = gethtypes.NewTransaction(nonce, common.Address(*args.To), value, gas, gasPrice, args.GetInput())
|
2019-02-21 10:53:39 +01:00
|
|
|
t.logNewTx(args, gas, gasPrice, value)
|
|
|
|
} else {
|
2019-11-23 18:57:05 +01:00
|
|
|
tx = gethtypes.NewContractCreation(nonce, value, gas, gasPrice, args.GetInput())
|
2019-02-21 10:53:39 +01:00
|
|
|
t.logNewContract(args, gas, gasPrice, value, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) getTransactionNonce(args SendTxArgs) (newNonce uint64, err error) {
|
|
|
|
var (
|
|
|
|
localNonce uint64
|
|
|
|
remoteNonce uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
// get the local nonce
|
|
|
|
if val, ok := t.localNonce.Load(args.From); ok {
|
|
|
|
localNonce = val.(uint64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the remote nonce
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2019-12-19 19:27:27 +01:00
|
|
|
remoteNonce, err = t.pendingNonceProvider.PendingNonceAt(ctx, common.Address(args.From))
|
2019-02-21 10:53:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return newNonce, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if upstream node returned nonce higher than ours we will use it, as it probably means
|
|
|
|
// that another client was used for sending transactions
|
|
|
|
if remoteNonce > localNonce {
|
|
|
|
newNonce = remoteNonce
|
|
|
|
} else {
|
|
|
|
newNonce = localNonce
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNonce, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) logNewTx(args SendTxArgs, gas uint64, gasPrice *big.Int, value *big.Int) {
|
|
|
|
t.log.Info("New transaction",
|
|
|
|
"From", args.From,
|
|
|
|
"To", *args.To,
|
|
|
|
"Gas", gas,
|
|
|
|
"GasPrice", gasPrice,
|
|
|
|
"Value", value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) logNewContract(args SendTxArgs, gas uint64, gasPrice *big.Int, value *big.Int, nonce uint64) {
|
|
|
|
t.log.Info("New contract",
|
|
|
|
"From", args.From,
|
|
|
|
"Gas", gas,
|
|
|
|
"GasPrice", gasPrice,
|
|
|
|
"Value", value,
|
|
|
|
"Contract address", crypto.CreateAddress(args.From, nonce),
|
|
|
|
)
|
|
|
|
}
|