Skip to content

Commit

Permalink
Tweak to use bool array on stale check
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <[email protected]>
  • Loading branch information
peterbroadhurst committed Apr 30, 2022
1 parent ae0eeac commit de49663
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
30 changes: 16 additions & 14 deletions internal/confirmations/confirmations.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type blockConfirmationManager struct {
bcmNotifications chan *Notification
highestBlockSeen uint64
pending map[string]*pendingItem
staleReceipts map[string]*pendingItem
staleReceipts map[string]bool
done chan struct{}
}

Expand All @@ -114,7 +114,7 @@ func NewBlockConfirmationManager(ctx context.Context, connectorAPI ffcapi.API) (
blockListenerStale: true,
bcmNotifications: make(chan *Notification, config.GetInt(tmconfig.ConfirmationsNotificationQueueLength)),
pending: make(map[string]*pendingItem),
staleReceipts: make(map[string]*pendingItem),
staleReceipts: make(map[string]bool),
}
bcm.ctx, bcm.cancelFunc = context.WithCancel(ctx)
bcm.blockCache, err = lru.New(config.GetInt(tmconfig.ConfirmationsBlockCacheSize))
Expand Down Expand Up @@ -377,15 +377,6 @@ func (bcm *blockConfirmationManager) confirmationsListener() {
}
}

// Mark receipts stale after duration
bcm.staleReceiptCheck()

// Perform any receipt checks required, due to new notifications, previously failed
// receipt checks, or processing block headers
for _, pending := range bcm.staleReceipts {
bcm.checkReceipt(pending)
}

// Do the poll
blockHashes, err := bcm.pollBlockListener()
if err != nil {
Expand All @@ -405,6 +396,17 @@ func (bcm *blockConfirmationManager) confirmationsListener() {
// Clear the notifications array now we've processed them (we keep the slice memory)
notifications = notifications[:0]

// Mark receipts stale after duration
bcm.staleReceiptCheck()

// Perform any receipt checks required, due to new notifications, previously failed
// receipt checks, or processing block headers
for pendingKey := range bcm.staleReceipts {
if pending, ok := bcm.pending[pendingKey]; ok {
bcm.checkReceipt(pending)
}
}

}

}
Expand All @@ -415,7 +417,7 @@ func (bcm *blockConfirmationManager) staleReceiptCheck() {
if pending.pType == pendingTypeTransaction && now.Sub(pending.lastReceiptcheck) > bcm.staleReceiptTimeout {
pendingKey := pending.getKey()
log.L(bcm.ctx).Infof("Marking receipt check stale for %s", pendingKey)
bcm.staleReceipts[pendingKey] = pending
bcm.staleReceipts[pendingKey] = true
}
}
}
Expand All @@ -433,7 +435,7 @@ func (bcm *blockConfirmationManager) processNotifications(notifications []*Notif
case NewTransaction:
newItem := n.transactionPendingItem()
bcm.addOrReplaceItem(newItem)
bcm.staleReceipts[newItem.getKey()] = newItem
bcm.staleReceipts[newItem.getKey()] = true
case RemovedEventLog:
bcm.removeItem(n.eventPendingItem())
case RemovedTransaction:
Expand Down Expand Up @@ -537,7 +539,7 @@ func (bcm *blockConfirmationManager) processBlock(block *BlockInfo) {
if pending, ok := bcm.pending[txKey]; ok {
if pending.blockHash != block.BlockHash {
log.L(bcm.ctx).Infof("Detected transaction %s added to block %d / %s - receipt check scheduled", txHash, block.BlockNumber, block.BlockHash)
bcm.staleReceipts[txKey] = pending
bcm.staleReceipts[txKey] = true
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions internal/confirmations/confirmations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,10 +1069,11 @@ func TestCheckReceiptNotFound(t *testing.T) {
pType: pendingTypeTransaction,
transactionHash: txHash,
}
bcm.staleReceipts[pendingKeyForTX(txHash)] = pending
bcm.pending[pending.getKey()] = pending
bcm.staleReceipts[pendingKeyForTX(txHash)] = true
bcm.checkReceipt(pending)

assert.Nil(t, bcm.staleReceipts[pendingKeyForTX(txHash)])
assert.False(t, bcm.staleReceipts[pendingKeyForTX(txHash)])

}

Expand All @@ -1087,10 +1088,11 @@ func TestCheckReceiptFail(t *testing.T) {
pType: pendingTypeTransaction,
transactionHash: txHash,
}
bcm.staleReceipts[pendingKeyForTX(txHash)] = pending
bcm.pending[pending.getKey()] = pending
bcm.staleReceipts[pendingKeyForTX(txHash)] = true
bcm.checkReceipt(pending)

assert.Equal(t, pending, bcm.staleReceipts[pendingKeyForTX(txHash)])
assert.True(t, bcm.staleReceipts[pendingKeyForTX(txHash)])

}

Expand All @@ -1112,10 +1114,11 @@ func TestCheckReceiptWalkFail(t *testing.T) {
pType: pendingTypeTransaction,
transactionHash: txHash,
}
bcm.staleReceipts[pendingKeyForTX(txHash)] = pending
bcm.pending[pending.getKey()] = pending
bcm.staleReceipts[pendingKeyForTX(txHash)] = true
bcm.checkReceipt(pending)

assert.Equal(t, pending, bcm.staleReceipts[pendingKeyForTX(txHash)])
assert.True(t, bcm.staleReceipts[pendingKeyForTX(txHash)])

}

Expand All @@ -1132,6 +1135,6 @@ func TestStaleReceiptCheck(t *testing.T) {
bcm.pending[pending.getKey()] = pending
bcm.staleReceiptCheck()

assert.Equal(t, pending, bcm.staleReceipts[pendingKeyForTX(txHash)])
assert.True(t, bcm.staleReceipts[pendingKeyForTX(txHash)])

}

0 comments on commit de49663

Please sign in to comment.