Skip to content

Commit

Permalink
fix: dont solely depend on the information from the L1 client to pred…
Browse files Browse the repository at this point in the history
…ict whether the transaction is committed
  • Loading branch information
bendanzhentan committed Jan 30, 2024
1 parent 92e4758 commit b4474ad
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions core/pending_txn_check.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package core

import "time"

// txNonceAndTimestamp represents the nonce and sending timestamp of transaction.
type txNonceAndTimestamp struct {
nonce uint64
timestamp time.Time
}

// PendingTxnCheck records the nonce and sending timestamp of transactions, in order to prevent re-processing
// the same withdrawals.
type PendingTxnCheck struct {
inner map[uint]uint64 // #{withdrawalId=>nonce}
inner map[uint]txNonceAndTimestamp // #{withdrawalId=>txNonceAndTimestamp}
}

// NewPendingTxsManager creates a new PendingTxnCheck
func NewPendingTxsManager() *PendingTxnCheck {
return &PendingTxnCheck{inner: make(map[uint]uint64)}
return &PendingTxnCheck{inner: make(map[uint]txNonceAndTimestamp)}
}

// IsPendingTxn checks whether there is pending transaction for the specific event id.
Expand All @@ -17,13 +27,16 @@ func (c *PendingTxnCheck) IsPendingTxn(id uint) bool {

// AddPendingTxn adds a pending item.
func (c *PendingTxnCheck) AddPendingTxn(id uint, nonce uint64) {
c.inner[id] = nonce
c.inner[id] = txNonceAndTimestamp{nonce: nonce, timestamp: time.Now()}
}

// Prune removes the transactions with staled nonce.
// Prune removes the transactions with chainNonce and the current timestamp.
func (c *PendingTxnCheck) Prune(chainNonce uint64) {
for id, nonce := range c.inner {
if nonce <= chainNonce {
const DurationSafelyPrunePendingTxs = 5 * time.Minute
timeSafelyPrunePendingTxs := time.Now().Add(-1 * DurationSafelyPrunePendingTxs)

for id, nt := range c.inner {
if nt.nonce <= chainNonce && nt.timestamp.After(timeSafelyPrunePendingTxs) {
delete(c.inner, id)
}
}
Expand Down

0 comments on commit b4474ad

Please sign in to comment.