2018-04-09 08:18:22 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
2018-08-16 11:37:53 +00:00
|
|
|
"bytes"
|
2018-04-09 08:18:22 +00:00
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-04-09 08:18:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sendTxTimeout defines how many seconds to wait before returning result in sentTransaction().
|
2018-04-10 10:02:54 +00:00
|
|
|
sendTxTimeout = 300 * time.Second
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
defaultGas = 90000
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 11:37:53 +00:00
|
|
|
func NewTransactor() *Transactor {
|
2018-04-09 08:18:22 +00:00
|
|
|
return &Transactor{
|
2018-08-16 11:37:53 +00:00
|
|
|
addrLock: &AddrLocker{},
|
|
|
|
sendTxTimeout: sendTxTimeout,
|
|
|
|
localNonce: sync.Map{},
|
|
|
|
log: log.New("package", "status-go/transactions.Manager"),
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNetworkID selects a correct network.
|
|
|
|
func (t *Transactor) SetNetworkID(networkID uint64) {
|
|
|
|
t.networkID = networkID
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// SetRPC sets RPC params, a client and a timeout
|
|
|
|
func (t *Transactor) SetRPC(rpcClient *rpc.Client, timeout time.Duration) {
|
2018-04-09 08:18:22 +00:00
|
|
|
rpcWrapper := newRPCWrapper(rpcClient)
|
|
|
|
t.sender = rpcWrapper
|
|
|
|
t.pendingNonceProvider = rpcWrapper
|
|
|
|
t.gasCalculator = rpcWrapper
|
2018-04-10 10:02:54 +00:00
|
|
|
t.rpcCallTimeout = timeout
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction is an implementation of eth_sendTransaction. It queues the tx to the sign queue.
|
2018-08-16 11:37:53 +00:00
|
|
|
func (t *Transactor) SendTransaction(sendArgs SendTxArgs, verifiedAccount *account.SelectedExtKey) (hash gethcommon.Hash, err error) {
|
|
|
|
hash, err = t.validateAndPropagate(verifiedAccount, sendArgs)
|
|
|
|
return
|
2018-04-09 08:18:22 +00: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 11:37:53 +00:00
|
|
|
if !bytes.Equal(args.From.Bytes(), selectedAccount.Address.Bytes()) {
|
|
|
|
return ErrInvalidTxSender
|
2018-04-09 08:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transactor) validateAndPropagate(selectedAccount *account.SelectedExtKey, args SendTxArgs) (hash gethcommon.Hash, err error) {
|
2018-06-12 16:50:25 +00:00
|
|
|
if err = t.validateAccount(args, selectedAccount); err != nil {
|
2018-04-09 08:18:22 +00:00
|
|
|
return hash, err
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
|
2018-04-09 08:18:22 +00: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()
|
|
|
|
nonce, err = t.pendingNonceProvider.PendingNonceAt(ctx, args.From)
|
|
|
|
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()
|
|
|
|
gas, err = t.gasCalculator.EstimateGas(ctx, ethereum.CallMsg{
|
|
|
|
From: args.From,
|
|
|
|
To: args.To,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Data: args.GetInput(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
if gas < defaultGas {
|
2018-08-16 11:37:53 +00:00
|
|
|
t.log.Info("default gas will be used because estimated is lower", "estimated", gas, "default", defaultGas)
|
2018-04-09 08:18:22 +00:00
|
|
|
gas = defaultGas
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tx *types.Transaction
|
|
|
|
if args.To != nil {
|
|
|
|
t.log.Info("New transaction",
|
|
|
|
"From", args.From,
|
|
|
|
"To", *args.To,
|
|
|
|
"Gas", gas,
|
|
|
|
"GasPrice", gasPrice,
|
|
|
|
"Value", value,
|
|
|
|
)
|
|
|
|
tx = types.NewTransaction(nonce, *args.To, value, gas, gasPrice, args.GetInput())
|
|
|
|
} else {
|
|
|
|
// contract creation is rare enough to log an expected address
|
|
|
|
t.log.Info("New contract",
|
|
|
|
"From", args.From,
|
|
|
|
"Gas", gas,
|
|
|
|
"GasPrice", gasPrice,
|
|
|
|
"Value", value,
|
|
|
|
"Contract address", crypto.CreateAddress(args.From, nonce),
|
|
|
|
)
|
|
|
|
tx = types.NewContractCreation(nonce, value, gas, gasPrice, args.GetInput())
|
|
|
|
}
|
|
|
|
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), selectedAccount.AccountKey.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), t.rpcCallTimeout)
|
|
|
|
defer cancel()
|
2018-06-06 07:35:32 +00:00
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
if err := t.sender.SendTransaction(ctx, signedTx); err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
return signedTx.Hash(), nil
|
|
|
|
}
|