2018-01-26 05:59:21 +00:00
|
|
|
package queue
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-04 12:56:58 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
2017-09-18 08:53:08 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2018-01-26 05:59:21 +00:00
|
|
|
"github.com/status-im/status-go/geth/account"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-09-04 12:56:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-04 12:56:58 +00:00
|
|
|
// DefaultTxQueueCap defines how many items can be queued.
|
|
|
|
DefaultTxQueueCap = int(35)
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
var (
|
2018-01-05 20:58:17 +00:00
|
|
|
// ErrQueuedTxExist - transaction was already enqueued
|
|
|
|
ErrQueuedTxExist = errors.New("transaction already exist in queue")
|
2017-10-20 09:06:22 +00:00
|
|
|
//ErrQueuedTxIDNotFound - error transaction hash not found
|
|
|
|
ErrQueuedTxIDNotFound = errors.New("transaction hash not found")
|
2018-01-05 20:58:17 +00:00
|
|
|
//ErrQueuedTxInProgress - error transaction is in progress
|
2017-10-20 09:06:22 +00:00
|
|
|
ErrQueuedTxInProgress = errors.New("transaction is in progress")
|
|
|
|
//ErrInvalidCompleteTxSender - error transaction with invalid sender
|
|
|
|
ErrInvalidCompleteTxSender = errors.New("transaction can only be completed by the same account which created it")
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
// remove from queue on any error (except for transient ones) and propagate
|
|
|
|
// defined as map[string]bool because errors from ethclient returned wrapped as jsonError
|
|
|
|
var transientErrs = map[string]bool{
|
|
|
|
keystore.ErrDecrypt.Error(): true, // wrong password
|
|
|
|
ErrInvalidCompleteTxSender.Error(): true, // completing tx create from another account
|
|
|
|
account.ErrNoAccountSelected.Error(): true, // account not selected
|
|
|
|
}
|
|
|
|
|
|
|
|
type empty struct{}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// TxQueue is capped container that holds pending transactions
|
|
|
|
type TxQueue struct {
|
2018-01-26 05:59:21 +00:00
|
|
|
mu sync.RWMutex // to guard transactions map
|
|
|
|
transactions map[common.QueuedTxID]*common.QueuedTx
|
|
|
|
inprogress map[common.QueuedTxID]empty
|
|
|
|
|
|
|
|
// TODO(dshulyak) research why eviction is done in separate goroutine
|
2017-09-04 12:56:58 +00:00
|
|
|
evictableIDs chan common.QueuedTxID
|
|
|
|
enqueueTicker chan struct{}
|
|
|
|
|
|
|
|
// when this channel is closed, all queue channels processing must cease (incoming queue, processing queued items etc)
|
|
|
|
stopped chan struct{}
|
|
|
|
stoppedGroup sync.WaitGroup // to make sure that all routines are stopped
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
// New creates a transaction queue.
|
|
|
|
func New() *TxQueue {
|
2017-09-04 12:56:58 +00:00
|
|
|
log.Info("initializing transaction queue")
|
|
|
|
return &TxQueue{
|
|
|
|
transactions: make(map[common.QueuedTxID]*common.QueuedTx),
|
2018-01-26 05:59:21 +00:00
|
|
|
inprogress: make(map[common.QueuedTxID]empty),
|
2017-09-04 12:56:58 +00:00
|
|
|
evictableIDs: make(chan common.QueuedTxID, DefaultTxQueueCap), // will be used to evict in FIFO
|
|
|
|
enqueueTicker: make(chan struct{}),
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Start starts enqueue and eviction loops
|
|
|
|
func (q *TxQueue) Start() {
|
|
|
|
log.Info("starting transaction queue")
|
|
|
|
|
|
|
|
if q.stopped != nil {
|
|
|
|
return
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
q.stopped = make(chan struct{})
|
2018-01-26 05:59:21 +00:00
|
|
|
q.stoppedGroup.Add(1)
|
2017-09-04 12:56:58 +00:00
|
|
|
go q.evictionLoop()
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Stop stops transaction enqueue and eviction loops
|
|
|
|
func (q *TxQueue) Stop() {
|
|
|
|
log.Info("stopping transaction queue")
|
|
|
|
|
|
|
|
if q.stopped == nil {
|
|
|
|
return
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
close(q.stopped) // stops all processing loops (enqueue, eviction etc)
|
|
|
|
q.stoppedGroup.Wait()
|
|
|
|
q.stopped = nil
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
log.Info("finally stopped transaction queue")
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// evictionLoop frees up queue to accommodate another transaction item
|
|
|
|
func (q *TxQueue) evictionLoop() {
|
|
|
|
defer HaltOnPanic()
|
|
|
|
evict := func() {
|
2018-01-26 05:59:21 +00:00
|
|
|
if q.Count() >= DefaultTxQueueCap { // eviction is required to accommodate another/last item
|
2017-09-04 12:56:58 +00:00
|
|
|
q.Remove(<-q.evictableIDs)
|
|
|
|
}
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(250 * time.Millisecond): // do not wait for manual ticks, check queue regularly
|
|
|
|
evict()
|
|
|
|
case <-q.enqueueTicker: // when manually requested
|
|
|
|
evict()
|
|
|
|
case <-q.stopped:
|
|
|
|
log.Info("transaction queue's eviction loop stopped")
|
|
|
|
q.stoppedGroup.Done()
|
|
|
|
return
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Reset is to be used in tests only, as it simply creates new transaction map, w/o any cleanup of the previous one
|
|
|
|
func (q *TxQueue) Reset() {
|
|
|
|
q.mu.Lock()
|
|
|
|
defer q.mu.Unlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
q.transactions = make(map[common.QueuedTxID]*common.QueuedTx)
|
|
|
|
q.evictableIDs = make(chan common.QueuedTxID, DefaultTxQueueCap)
|
2018-01-26 05:59:21 +00:00
|
|
|
q.inprogress = make(map[common.QueuedTxID]empty)
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue enqueues incoming transaction
|
|
|
|
func (q *TxQueue) Enqueue(tx *common.QueuedTx) error {
|
|
|
|
log.Info(fmt.Sprintf("enqueue transaction: %s", tx.ID))
|
2018-01-05 20:58:17 +00:00
|
|
|
q.mu.RLock()
|
|
|
|
if _, ok := q.transactions[tx.ID]; ok {
|
|
|
|
q.mu.RUnlock()
|
|
|
|
return ErrQueuedTxExist
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-01-05 20:58:17 +00:00
|
|
|
q.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-01-05 20:58:17 +00:00
|
|
|
// we can't hold a lock in this part
|
|
|
|
log.Debug("notifying eviction loop")
|
2017-09-04 12:56:58 +00:00
|
|
|
q.enqueueTicker <- struct{}{} // notify eviction loop that we are trying to insert new item
|
2018-01-05 20:58:17 +00:00
|
|
|
q.evictableIDs <- tx.ID // this will block when we hit DefaultTxQueueCap
|
|
|
|
log.Debug("notified eviction loop")
|
2017-09-04 12:56:58 +00:00
|
|
|
|
|
|
|
q.mu.Lock()
|
|
|
|
q.transactions[tx.ID] = tx
|
|
|
|
q.mu.Unlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// notify handler
|
|
|
|
log.Info("calling txEnqueueHandler")
|
|
|
|
return nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Get returns transaction by transaction identifier
|
|
|
|
func (q *TxQueue) Get(id common.QueuedTxID) (*common.QueuedTx, error) {
|
|
|
|
q.mu.RLock()
|
|
|
|
defer q.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
if tx, ok := q.transactions[id]; ok {
|
|
|
|
return tx, nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
return nil, ErrQueuedTxIDNotFound
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
// LockInprogress returns error if transaction is already inprogress.
|
|
|
|
func (q *TxQueue) LockInprogress(id common.QueuedTxID) error {
|
|
|
|
q.mu.Lock()
|
|
|
|
defer q.mu.Unlock()
|
|
|
|
if _, ok := q.transactions[id]; ok {
|
|
|
|
if _, inprogress := q.inprogress[id]; inprogress {
|
|
|
|
return ErrQueuedTxInProgress
|
|
|
|
}
|
|
|
|
q.inprogress[id] = empty{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrQueuedTxIDNotFound
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Remove removes transaction by transaction identifier
|
|
|
|
func (q *TxQueue) Remove(id common.QueuedTxID) {
|
|
|
|
q.mu.Lock()
|
|
|
|
defer q.mu.Unlock()
|
2018-01-26 05:59:21 +00:00
|
|
|
q.remove(id)
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
func (q *TxQueue) remove(id common.QueuedTxID) {
|
2017-09-04 12:56:58 +00:00
|
|
|
delete(q.transactions, id)
|
2018-01-26 05:59:21 +00:00
|
|
|
delete(q.inprogress, id)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
// Done removes transaction from queue if no error or error is not transient
|
|
|
|
// and notify subscribers
|
|
|
|
func (q *TxQueue) Done(id common.QueuedTxID, hash gethcommon.Hash, err error) error {
|
2017-09-18 08:53:08 +00:00
|
|
|
q.mu.Lock()
|
|
|
|
defer q.mu.Unlock()
|
2018-01-26 05:59:21 +00:00
|
|
|
tx, ok := q.transactions[id]
|
|
|
|
if !ok {
|
|
|
|
return ErrQueuedTxIDNotFound
|
2017-09-18 08:53:08 +00:00
|
|
|
}
|
2018-01-26 05:59:21 +00:00
|
|
|
q.done(tx, hash, err)
|
2017-09-18 08:53:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:59:21 +00:00
|
|
|
func (q *TxQueue) done(tx *common.QueuedTx, hash gethcommon.Hash, err error) {
|
|
|
|
delete(q.inprogress, tx.ID)
|
|
|
|
// hash is updated only if err is nil, but transaction is not removed from a queue
|
|
|
|
if err == nil {
|
2018-01-05 20:58:17 +00:00
|
|
|
q.transactions[tx.ID].Result <- common.TransactionResult{Hash: hash, Error: err}
|
2018-01-26 05:59:21 +00:00
|
|
|
q.remove(tx.ID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, transient := transientErrs[err.Error()]; !transient {
|
2018-01-05 20:58:17 +00:00
|
|
|
q.transactions[tx.ID].Result <- common.TransactionResult{Error: err}
|
2018-01-26 05:59:21 +00:00
|
|
|
q.remove(tx.ID)
|
|
|
|
}
|
2017-09-18 08:53:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Count returns number of currently queued transactions
|
|
|
|
func (q *TxQueue) Count() int {
|
|
|
|
q.mu.RLock()
|
|
|
|
defer q.mu.RUnlock()
|
|
|
|
return len(q.transactions)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// Has checks whether transaction with a given identifier exists in queue
|
|
|
|
func (q *TxQueue) Has(id common.QueuedTxID) bool {
|
|
|
|
q.mu.RLock()
|
|
|
|
defer q.mu.RUnlock()
|
|
|
|
_, ok := q.transactions[id]
|
|
|
|
return ok
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|