Skip to content

Commit

Permalink
Rework lock inprogress method
Browse files Browse the repository at this point in the history
  • Loading branch information
dshulyak committed Jan 24, 2018
1 parent e7171b5 commit 9c6421d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
13 changes: 6 additions & 7 deletions geth/transactions/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,18 @@ func (q *TxQueue) Get(id common.QueuedTxID) (*common.QueuedTx, error) {
return nil, ErrQueuedTxIDNotFound
}

// LockInprogress returns transaction and locks it as inprogress
func (q *TxQueue) LockInprogress(id common.QueuedTxID) (*common.QueuedTx, error) {
// LockInprogress returns error if transaction is already inprogress.
func (q *TxQueue) LockInprogress(id common.QueuedTxID) error {
q.mu.Lock()
defer q.mu.Unlock()

if tx, ok := q.transactions[id]; ok {
if _, ok := q.transactions[id]; ok {
if _, inprogress := q.inprogress[id]; inprogress {
return tx, ErrQueuedTxInProgress
return ErrQueuedTxInProgress
}
q.inprogress[id] = empty{}
return tx, nil
return nil
}
return nil, ErrQueuedTxIDNotFound
return ErrQueuedTxIDNotFound
}

// Remove removes transaction by transaction identifier
Expand Down
6 changes: 3 additions & 3 deletions geth/transactions/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func (s *QueueTestSuite) TearDownTest() {
func (s *QueueTestSuite) TestLockInprogressTransaction() {
tx := common.CreateTransaction(context.Background(), common.SendTxArgs{})
s.NoError(s.queue.Enqueue(tx))
enquedTx, err := s.queue.LockInprogress(tx.ID)
enquedTx, err := s.queue.Get(tx.ID)
s.NoError(err)
s.NoError(s.queue.LockInprogress(tx.ID))
s.Equal(tx, enquedTx)

// verify that tx was marked as being inprogress
_, err = s.queue.LockInprogress(tx.ID)
s.Equal(ErrQueuedTxInProgress, err)
s.Equal(ErrQueuedTxInProgress, s.queue.LockInprogress(tx.ID))
}

func (s *QueueTestSuite) TestGetTransaction() {
Expand Down
8 changes: 5 additions & 3 deletions geth/transactions/txqueue_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ func (m *Manager) WaitForTransaction(tx *common.QueuedTx) error {
}

// CompleteTransaction instructs backend to complete sending of a given transaction.
// TODO(adam): investigate a possible bug that calling this method multiple times with the same Transaction ID
// results in sending multiple transactions.
func (m *Manager) CompleteTransaction(id common.QueuedTxID, password string) (hash gethcommon.Hash, err error) {
log.Info("complete transaction", "id", id)
tx, err := m.txQueue.LockInprogress(id)
tx, err := m.txQueue.Get(id)
if err != nil {
log.Warn("error getting a queued transaction", "err", err)
return hash, err
}
if err := m.txQueue.LockInprogress(id); err != nil {
log.Warn("can't process transaction", "err", err)
return hash, err
}
account, err := m.validateAccount(tx)
if err != nil {
m.txDone(tx, hash, err)
Expand Down

0 comments on commit 9c6421d

Please sign in to comment.