-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[audit] fix: avoid reprocessing withdrawals (#20)
* fix: avoid re-processing withdrawals 1. check pending nonce and chain nonce before processing 2. check recently processed using local records before processing * feat: update db types `BotDelegatedWithdrawal` 1. Rename L2ContractEvent to BotDelegatedWithdrawal 2. Add unique constraint idx_bot_delegated_withdrawals_transaction_hash_log_index_key 3. Add new field `InitiatedBlockNumber int64` to indicate the L2 number of initiated withdrawal transaction 3. Add new fields `ProvenTime *Time` and `FinalizedTime *Time` to indicate the local time of L1 proven transaction and finalized transaction 4. Modify the `FailureReason` to type `FailureReason *string` * improve: compare timings of proven and finalized more precisely 1. Determine the proven timing based on the `L2OutputOracle.latestBlockNumber` 2. Determine the finalized timing based on the db `proven_time` * bindings: update binding * feat: manage nonce locally * config: update bot contract
- Loading branch information
1 parent
fed3ec0
commit 3c81f35
Showing
7 changed files
with
310 additions
and
99 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core | ||
|
||
type PendingTxnCheck struct { | ||
inner map[uint]uint64 // #{withdrawalId=>nonce} | ||
} | ||
|
||
// NewPendingTxsManager creates a new PendingTxnCheck | ||
func NewPendingTxsManager() *PendingTxnCheck { | ||
return &PendingTxnCheck{inner: make(map[uint]uint64)} | ||
} | ||
|
||
// IsPendingTxn checks whether there is pending transaction for the specific event id. | ||
func (c *PendingTxnCheck) IsPendingTxn(id uint) bool { | ||
_, ok := c.inner[id] | ||
return ok | ||
} | ||
|
||
// AddPendingTxn adds a pending item. | ||
func (c *PendingTxnCheck) AddPendingTxn(id uint, nonce uint64) { | ||
c.inner[id] = nonce | ||
} | ||
|
||
// Prune removes the transactions with staled nonce. | ||
func (c *PendingTxnCheck) Prune(chainNonce uint64) { | ||
for id, nonce := range c.inner { | ||
if nonce <= chainNonce { | ||
delete(c.inner, id) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,28 @@ | ||
package core | ||
|
||
import "time" | ||
|
||
type L2ScannedBlock struct { | ||
Number int64 `gorm:"type:integer;primarykey"` | ||
} | ||
|
||
type L2ContractEvent struct { | ||
ID uint `gorm:"primarykey"` | ||
BlockTime int64 `gorm:"type:integer;not null;index:idx_l2_contract_events_block_time"` | ||
BlockHash string `gorm:"type:varchar(256);not null;uniqueIndex:idx_l2_contract_events_block_hash_log_index_key,priority:1;"` | ||
ContractAddress string `gorm:"type:varchar(256);not null"` | ||
TransactionHash string `gorm:"type:varchar(256);not null"` | ||
LogIndex int `gorm:"type:integer;not null;uniqueIndex:idx_l2_contract_events_block_hash_log_index_key,priority:2"` | ||
EventSignature string `gorm:"type:varchar(256);not null"` | ||
Proven bool `gorm:"type:boolean;not null;default:false"` | ||
Finalized bool `gorm:"type:boolean;not null;default:false"` | ||
FailureReason string `gorm:"type:text"` | ||
type BotDelegatedWithdrawal struct { | ||
// ID is the incrementing primary key. | ||
ID uint `gorm:"primarykey"` | ||
|
||
// TransactionHash and LogIndex are the L2 transaction hash and log index of the withdrawal event. | ||
TransactionHash string `gorm:"type:varchar(256);not null;uniqueIndex:idx_bot_delegated_withdrawals_transaction_hash_log_index_key,priority:1"` | ||
LogIndex int `gorm:"type:integer;not null;uniqueIndex:idx_bot_delegated_withdrawals_transaction_hash_log_index_key,priority:2"` | ||
|
||
// InitiatedBlockNumber is the l2 block number at which the withdrawal was initiated on L2. | ||
InitiatedBlockNumber int64 `gorm:"type:integer;not null;index:idx_withdrawals_initiated_block_number"` | ||
|
||
// ProvenTime is the local time at which the withdrawal was proven on L1. NULL if not yet proven. | ||
ProvenTime *time.Time `gorm:"type:datetime;index:idx_withdrawals_proven_time"` | ||
|
||
// FinalizedTime is the local time at which the withdrawal was finalized on L1. NULL if not yet finalized. | ||
FinalizedTime *time.Time `gorm:"type:datetime;index:idx_withdrawals_finalized_time"` | ||
|
||
// FailureReason is the reason for the withdrawal failure, including sending transaction error and off-chain configured filter error. NULL if not yet failed. | ||
FailureReason *string `gorm:"type:text"` | ||
} |