2018-01-26 05:59:21 +00:00
|
|
|
package transactions
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
"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-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().
|
|
|
|
DefaultTxSendCompletionTimeout = 300
|
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 {
|
2017-09-04 12:56:58 +00:00
|
|
|
nodeManager common.NodeManager
|
|
|
|
accountManager common.AccountManager
|
2018-01-26 05:59:21 +00:00
|
|
|
txQueue *queue.TxQueue
|
2018-01-18 16:55:17 +00:00
|
|
|
ethTxClient EthTransactor
|
|
|
|
addrLock *AddrLocker
|
2018-01-26 05:59:21 +00:00
|
|
|
notify bool
|
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{
|
2017-09-04 12:56:58 +00:00
|
|
|
nodeManager: nodeManager,
|
|
|
|
accountManager: accountManager,
|
2018-01-26 05:59:21 +00:00
|
|
|
txQueue: queue.New(),
|
2018-01-18 16:55:17 +00:00
|
|
|
addrLock: &AddrLocker{},
|
2018-01-26 05:59:21 +00:00
|
|
|
notify: true,
|
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-26 05:59:21 +00:00
|
|
|
err := m.txQueue.Enqueue(tx)
|
|
|
|
if m.notify {
|
|
|
|
NotifyOnEnqueue(tx)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
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) {
|
|
|
|
m.txQueue.Done(tx.ID, hash, err) //nolint: errcheck
|
|
|
|
if m.notify {
|
|
|
|
NotifyOnReturn(tx)
|
|
|
|
}
|
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.
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) WaitForTransaction(tx *common.QueuedTx) error {
|
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
|
|
|
|
select {
|
|
|
|
case <-tx.Done:
|
|
|
|
case <-time.After(DefaultTxSendCompletionTimeout * time.Second):
|
2018-01-26 05:59:21 +00:00
|
|
|
m.txDone(tx, gethcommon.Hash{}, queue.ErrQueuedTxTimedOut)
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
return tx.Err
|
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
|
|
|
|
}
|
|
|
|
account, err := m.validateAccount(tx)
|
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-01-26 05:59:21 +00:00
|
|
|
// Send the transaction finally.
|
|
|
|
hash, err = m.completeTransaction(tx, account, password)
|
|
|
|
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-01-26 05:59:21 +00:00
|
|
|
func (m *Manager) validateAccount(tx *common.QueuedTx) (*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-26 05:59:21 +00:00
|
|
|
return selectedAccount, nil
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
func (m *Manager) completeTransaction(queuedTx *common.QueuedTx, selectedAccount *common.SelectedExtKey, password string) (hash gethcommon.Hash, err error) {
|
2018-01-18 16:55:17 +00:00
|
|
|
log.Info("complete transaction", "id", queuedTx.ID)
|
2018-01-26 05:59:21 +00:00
|
|
|
log.Info("verifying account password for transaction", "id", queuedTx.ID)
|
2017-09-04 12:56:58 +00:00
|
|
|
config, err := m.nodeManager.NodeConfig()
|
|
|
|
if err != nil {
|
2018-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
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-01-26 05:59:21 +00:00
|
|
|
return hash, err
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-01-18 16:55:17 +00:00
|
|
|
m.addrLock.LockAddr(queuedTx.Args.From)
|
|
|
|
defer m.addrLock.UnlockAddr(queuedTx.Args.From)
|
2018-01-26 05:59:21 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
defer cancel()
|
2018-01-18 16:55:17 +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
|
|
|
}
|
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-01-18 16:55:17 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
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 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
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-01-18 16:55:17 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
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
|
2017-09-27 00:50:41 +00:00
|
|
|
func (m *Manager) CompleteTransactions(ids []common.QueuedTxID, password string) map[common.QueuedTxID]common.RawCompleteTransactionResult {
|
2017-09-04 12:56:58 +00:00
|
|
|
results := make(map[common.QueuedTxID]common.RawCompleteTransactionResult)
|
|
|
|
for _, txID := range ids {
|
|
|
|
txHash, txErr := m.CompleteTransaction(txID, password)
|
|
|
|
results[txID] = common.RawCompleteTransactionResult{
|
|
|
|
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-26 05:59:21 +00:00
|
|
|
err = m.txQueue.Done(id, gethcommon.Hash{}, queue.ErrQueuedTxDiscarded)
|
|
|
|
if m.notify {
|
|
|
|
NotifyOnReturn(tx)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.WaitForTransaction(tx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Hash.Hex(), nil
|
|
|
|
}
|