2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
2018-02-07 22:23:57 +00:00
|
|
|
"sync"
|
2017-09-04 12:56:58 +00:00
|
|
|
"time"
|
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
2017-09-04 12:56:58 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
|
|
|
"github.com/status-im/status-go/geth/log"
|
2018-02-07 22:23:57 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/status-im/status-go/geth/transactions/queue"
|
2017-09-04 12:56:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SendTxDefaultErrorCode is sent by default, when error is not nil, but type is unknown/unexpected.
|
|
|
|
SendTxDefaultErrorCode = SendTransactionDefaultErrorCode
|
2018-01-26 05:59:21 +00:00
|
|
|
// DefaultTxSendCompletionTimeout defines how many seconds to wait before returning result in sentTransaction().
|
2018-01-05 20:58:17 +00:00
|
|
|
DefaultTxSendCompletionTimeout = 300 * time.Second
|
2018-01-18 16:55:17 +00:00
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
defaultGas = 90000
|
2018-01-18 16:55:17 +00:00
|
|
|
defaultTimeout = time.Minute
|
2017-09-04 12:56:58 +00:00
|
|
|
)
|
|
|
|
|
2017-09-27 00:50:41 +00:00
|
|
|
// Manager provides means to manage internal Status Backend (injected into LES)
|
|
|
|
type Manager struct {
|
2018-01-05 20:58:17 +00:00
|
|
|
nodeManager common.NodeManager
|
|
|
|
accountManager common.AccountManager
|
|
|
|
txQueue *queue.TxQueue
|
|
|
|
ethTxClient EthTransactor
|
|
|
|
notify bool
|
|
|
|
completionTimeout time.Duration
|
2018-02-05 16:28:21 +00:00
|
|
|
rpcCallTimeout time.Duration
|
2018-02-07 22:23:57 +00:00
|
|
|
|
|
|
|
addrLock *AddrLocker
|
|
|
|
localNonce sync.Map
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 00:50:41 +00:00
|
|
|
// NewManager returns a new Manager.
|
|
|
|
func NewManager(nodeManager common.NodeManager, accountManager common.AccountManager) *Manager {
|
|
|
|
return &Manager{
|
2018-01-05 20:58:17 +00:00
|
|
|
nodeManager: nodeManager,
|
|
|
|
accountManager: accountManager,
|
|
|
|
txQueue: queue.New(),
|
|
|
|
addrLock: &AddrLocker{},
|
|
|
|
notify: true,
|
|
|
|
completionTimeout: DefaultTxSendCompletionTimeout,
|
2018-02-05 16:28:21 +00:00
|
|
|
rpcCallTimeout: defaultTimeout,
|
2018-02-07 22:23:57 +00:00
|
|
|
localNonce: sync.Map{},
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
// DisableNotificactions turns off notifications on enqueue and return of tx.
|
|
|
|
// It is not thread safe and must be called only before manager is started.
|
|
|
|
func (m *Manager) DisableNotificactions() {
|
|
|
|
m.notify = false
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Start starts accepting new transactions into the queue.
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) Start() {
|
|
|
|
log.Info("start Manager")
|
2018-01-18 16:55:17 +00:00
|
|
|
m.ethTxClient = NewEthTxClient(m.nodeManager.RPCClient())
|
2017-09-04 12:56:58 +00:00
|
|
|
m.txQueue.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops accepting new transactions into the queue.
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) Stop() {
|
|
|
|
log.Info("stop Manager")
|
2017-09-04 12:56:58 +00:00
|
|
|
m.txQueue.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionQueue returns a reference to the queue.
|
2018-01-22 09:31:24 +00:00
|
|
|
func (m *Manager) TransactionQueue() *queue.TxQueue {
|
2017-09-04 12:56:58 +00:00
|
|
|
return m.txQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueueTransaction puts a transaction into the queue.
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) QueueTransaction(tx *common.QueuedTx) error {
|
2017-09-04 12:56:58 +00:00
|
|
|
to := "<nil>"
|
|
|
|
if tx.Args.To != nil {
|
|
|
|
to = tx.Args.To.Hex()
|
|
|
|
}
|
|
|
|
log.Info("queue a new transaction", "id", tx.ID, "from", tx.Args.From.Hex(), "to", to)
|
2018-01-05 20:58:17 +00:00
|
|
|
if err := m.txQueue.Enqueue(tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if m.notify {
|
|
|
|
NotifyOnEnqueue(tx)
|
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
return nil
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
func (m *Manager) txDone(tx *common.QueuedTx, hash gethcommon.Hash, err error) {
|
2018-01-05 20:58:17 +00:00
|
|
|
if err := m.txQueue.Done(tx.ID, hash, err); err == queue.ErrQueuedTxIDNotFound {
|
|
|
|
log.Warn("transaction is already removed from a queue", tx.ID)
|
|
|
|
return
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if m.notify {
|
2018-01-05 20:58:17 +00:00
|
|
|
NotifyOnReturn(tx, err)
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForTransaction adds a transaction to the queue and blocks
|
|
|
|
// until it's completed, discarded or times out.
|
2018-01-05 20:58:17 +00:00
|
|
|
func (m *Manager) WaitForTransaction(tx *common.QueuedTx) common.TransactionResult {
|
2017-09-04 12:56:58 +00:00
|
|
|
log.Info("wait for transaction", "id", tx.ID)
|
|
|
|
// now wait up until transaction is:
|
|
|
|
// - completed (via CompleteQueuedTransaction),
|
|
|
|
// - discarded (via DiscardQueuedTransaction)
|
|
|
|
// - or times out
|
2018-01-05 20:58:17 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case rst := <-tx.Result:
|
|
|
|
return rst
|
|
|
|
case <-time.After(m.completionTimeout):
|
|
|
|
m.txDone(tx, gethcommon.Hash{}, ErrQueuedTxTimedOut)
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteTransaction instructs backend to complete sending of a given transaction.
|
2018-01-26 05:59:21 +00:00
|
|
|
func (m *Manager) CompleteTransaction(id common.QueuedTxID, password string) (hash gethcommon.Hash, err error) {
|
2017-09-04 12:56:58 +00:00
|
|
|
log.Info("complete transaction", "id", id)
|
2018-01-26 05:59:21 +00:00
|
|
|
tx, err := m.txQueue.Get(id)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
log.Warn("error getting a queued transaction", "err", err)
|
|
|
|
return hash, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
if err := m.txQueue.LockInprogress(id); err != nil {
|
|
|
|
log.Warn("can't process transaction", "err", err)
|
|
|
|
return hash, err
|
|
|
|
}
|
2018-02-07 22:23:57 +00:00
|
|
|
config, err := m.nodeManager.NodeConfig()
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
account, err := m.validateAccount(config, tx, password)
|
2017-10-20 09:06:22 +00:00
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
m.txDone(tx, hash, err)
|
|
|
|
return hash, err
|
2017-09-18 08:53:08 +00:00
|
|
|
}
|
2018-02-07 22:23:57 +00:00
|
|
|
hash, err = m.completeTransaction(config, account, tx)
|
2018-01-26 05:59:21 +00:00
|
|
|
log.Info("finally completed transaction", "id", tx.ID, "hash", hash, "err", err)
|
|
|
|
m.txDone(tx, hash, err)
|
|
|
|
return hash, err
|
|
|
|
}
|
2017-09-18 08:53:08 +00:00
|
|
|
|
2018-02-07 22:23:57 +00:00
|
|
|
func (m *Manager) validateAccount(config *params.NodeConfig, tx *common.QueuedTx, password string) (*common.SelectedExtKey, error) {
|
2017-09-04 12:56:58 +00:00
|
|
|
selectedAccount, err := m.accountManager.SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("failed to get a selected account", "err", err)
|
2018-01-26 05:59:21 +00:00
|
|
|
return nil, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
// make sure that only account which created the tx can complete it
|
2018-01-26 05:59:21 +00:00
|
|
|
if tx.Args.From.Hex() != selectedAccount.Address.Hex() {
|
|
|
|
log.Warn("queued transaction does not belong to the selected account", "err", queue.ErrInvalidCompleteTxSender)
|
|
|
|
return nil, queue.ErrInvalidCompleteTxSender
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-01-18 16:55:17 +00:00
|
|
|
_, err = m.accountManager.VerifyAccountPassword(config.KeyStoreDir, selectedAccount.Address.String(), password)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-01-18 16:55:17 +00:00
|
|
|
log.Warn("failed to verify account", "account", selectedAccount.Address.String(), "error", err.Error())
|
2018-02-07 22:23:57 +00:00
|
|
|
return nil, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-02-07 22:23:57 +00:00
|
|
|
return selectedAccount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) completeTransaction(config *params.NodeConfig, selectedAccount *common.SelectedExtKey, queuedTx *common.QueuedTx) (hash gethcommon.Hash, err error) {
|
|
|
|
log.Info("complete transaction", "id", queuedTx.ID)
|
2018-01-18 16:55:17 +00:00
|
|
|
m.addrLock.LockAddr(queuedTx.Args.From)
|
2018-02-07 22:23:57 +00:00
|
|
|
var localNonce uint64
|
|
|
|
if val, ok := m.localNonce.Load(queuedTx.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 {
|
|
|
|
m.localNonce.Store(queuedTx.Args.From, nonce+1)
|
|
|
|
}
|
|
|
|
m.addrLock.UnlockAddr(queuedTx.Args.From)
|
|
|
|
|
|
|
|
}()
|
2018-02-05 16:28:21 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), m.rpcCallTimeout)
|
2018-01-26 05:59:21 +00:00
|
|
|
defer cancel()
|
2018-02-07 22:23:57 +00:00
|
|
|
nonce, err = m.ethTxClient.PendingNonceAt(ctx, queuedTx.Args.From)
|
2017-10-20 09:06:22 +00:00
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-02-07 22:23:57 +00:00
|
|
|
// 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
|
|
|
|
}
|
2017-09-25 16:04:40 +00:00
|
|
|
args := queuedTx.Args
|
2018-01-18 16:55:17 +00:00
|
|
|
gasPrice := (*big.Int)(args.GasPrice)
|
2017-09-25 16:04:40 +00:00
|
|
|
if args.GasPrice == nil {
|
2018-02-05 16:28:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), m.rpcCallTimeout)
|
2018-01-18 16:55:17 +00:00
|
|
|
defer cancel()
|
|
|
|
gasPrice, err = m.ethTxClient.SuggestGasPrice(ctx)
|
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
chainID := big.NewInt(int64(config.NetworkID))
|
2017-09-25 16:04:40 +00:00
|
|
|
data := []byte(args.Data)
|
|
|
|
value := (*big.Int)(args.Value)
|
|
|
|
toAddr := gethcommon.Address{}
|
|
|
|
if args.To != nil {
|
|
|
|
toAddr = *args.To
|
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
gas := (*big.Int)(args.Gas)
|
|
|
|
if args.Gas == nil {
|
2018-02-05 16:28:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), m.rpcCallTimeout)
|
2018-01-18 16:55:17 +00:00
|
|
|
defer cancel()
|
|
|
|
gas, err = m.ethTxClient.EstimateGas(ctx, ethereum.CallMsg{
|
|
|
|
From: args.From,
|
|
|
|
To: args.To,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Value: value,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
|
|
|
if gas.Cmp(big.NewInt(defaultGas)) == -1 {
|
|
|
|
log.Info("default gas will be used. estimated gas", gas, "is lower than", defaultGas)
|
|
|
|
gas = big.NewInt(defaultGas)
|
|
|
|
}
|
2017-09-19 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
log.Info(
|
|
|
|
"preparing raw transaction",
|
|
|
|
"from", args.From.Hex(),
|
|
|
|
"to", toAddr.Hex(),
|
|
|
|
"gas", gas,
|
|
|
|
"gasPrice", gasPrice,
|
|
|
|
"value", value,
|
|
|
|
)
|
2018-01-18 16:55:17 +00:00
|
|
|
tx := types.NewTransaction(nonce, toAddr, value, gas, gasPrice, data)
|
|
|
|
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), selectedAccount.AccountKey.PrivateKey)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-02-05 16:28:21 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), m.rpcCallTimeout)
|
2018-01-18 16:55:17 +00:00
|
|
|
defer cancel()
|
|
|
|
if err := m.ethTxClient.SendTransaction(ctx, signedTx); err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
return signedTx.Hash(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteTransactions instructs backend to complete sending of multiple transactions
|
2018-01-05 20:58:17 +00:00
|
|
|
func (m *Manager) CompleteTransactions(ids []common.QueuedTxID, password string) map[common.QueuedTxID]common.TransactionResult {
|
|
|
|
results := make(map[common.QueuedTxID]common.TransactionResult)
|
2017-09-04 12:56:58 +00:00
|
|
|
for _, txID := range ids {
|
|
|
|
txHash, txErr := m.CompleteTransaction(txID, password)
|
2018-01-05 20:58:17 +00:00
|
|
|
results[txID] = common.TransactionResult{
|
2017-09-04 12:56:58 +00:00
|
|
|
Hash: txHash,
|
|
|
|
Error: txErr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscardTransaction discards a given transaction from transaction queue
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) DiscardTransaction(id common.QueuedTxID) error {
|
2018-01-26 05:59:21 +00:00
|
|
|
tx, err := m.txQueue.Get(id)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
err = m.txQueue.Done(id, gethcommon.Hash{}, ErrQueuedTxDiscarded)
|
2018-01-26 05:59:21 +00:00
|
|
|
if m.notify {
|
2018-01-05 20:58:17 +00:00
|
|
|
NotifyOnReturn(tx, ErrQueuedTxDiscarded)
|
2018-01-26 05:59:21 +00:00
|
|
|
}
|
|
|
|
return err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscardTransactions discards given multiple transactions from transaction queue
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) DiscardTransactions(ids []common.QueuedTxID) map[common.QueuedTxID]common.RawDiscardTransactionResult {
|
2017-09-04 12:56:58 +00:00
|
|
|
results := make(map[common.QueuedTxID]common.RawDiscardTransactionResult)
|
|
|
|
|
|
|
|
for _, txID := range ids {
|
|
|
|
err := m.DiscardTransaction(txID)
|
|
|
|
if err != nil {
|
|
|
|
results[txID] = common.RawDiscardTransactionResult{
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
// SendTransactionRPCHandler is a handler for eth_sendTransaction method.
|
|
|
|
// It accepts one param which is a slice with a map of transaction params.
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) SendTransactionRPCHandler(ctx context.Context, args ...interface{}) (interface{}, error) {
|
2017-09-25 16:04:40 +00:00
|
|
|
log.Info("SendTransactionRPCHandler called")
|
|
|
|
// TODO(adam): it's a hack to parse arguments as common.RPCCall can do that.
|
|
|
|
// We should refactor parsing these params to a separate struct.
|
|
|
|
rpcCall := common.RPCCall{Params: args}
|
2018-01-26 05:59:21 +00:00
|
|
|
tx := common.CreateTransaction(ctx, rpcCall.ToSendTxArgs())
|
2017-09-25 16:04:40 +00:00
|
|
|
if err := m.QueueTransaction(tx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
rst := m.WaitForTransaction(tx)
|
|
|
|
if rst.Error != nil {
|
|
|
|
return nil, rst.Error
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
return rst.Hash.Hex(), nil
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|