Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow overwriting safe and finalized number of blocks #40

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ethtxmanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ type Config struct {

// Log configuration
Log log.Config `mapstructure:"Log"`

// SafeStatusL1NumberOfBlocks overwrites the number of blocks to consider a tx as safe
// overwriting the default value provided by the network
// 0 means that the default value will be used
SafeStatusL1NumberOfBlocks uint64 `mapstructure:"SafeStatusL1NumberOfBlocks"`

// FinalizedStatusL1NumberOfBlocks overwrites the number of blocks to consider a tx as finalized
// overwriting the default value provided by the network
// 0 means that the default value will be used
FinalizedStatusL1NumberOfBlocks uint64 `mapstructure:"FinalizedStatusL1NumberOfBlocks"`
}
42 changes: 32 additions & 10 deletions ethtxmanager/ethtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,22 @@ func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error {

log.Debugf("found %v mined monitored tx to process", len(mTxs))

// Get Safe block Number
safeL1BlockNumberFetch := l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.SafeBlockNumber, 0)
safeBlockNumber, err := safeL1BlockNumberFetch.GetSafeBlockNumber(ctx, c.etherman)
if err != nil {
return fmt.Errorf("failed to get safe block number: %v", err)
safeBlockNumber := uint64(0)
if c.cfg.SafeStatusL1NumberOfBlocks > 0 {
// Overwrite the number of blocks to consider a tx as safe
currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to get latest block number: %v", err)
}

safeBlockNumber = currentBlockNumber - c.cfg.SafeStatusL1NumberOfBlocks
} else {
// Get Safe block Number
safeL1BlockNumberFetch := l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.SafeBlockNumber, 0)
safeBlockNumber, err = safeL1BlockNumberFetch.GetSafeBlockNumber(ctx, c.etherman)
if err != nil {
return fmt.Errorf("failed to get safe block number: %v", err)
}
}

for _, mTx := range mTxs {
Expand Down Expand Up @@ -530,11 +541,22 @@ func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error {

log.Debugf("found %v safe monitored tx to process", len(mTxs))

// Get Finalized block Number
safeL1BlockNumberFetch := l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.FinalizedBlockNumber, 0)
finaLizedBlockNumber, err := safeL1BlockNumberFetch.GetSafeBlockNumber(ctx, c.etherman)
if err != nil {
return fmt.Errorf("failed to get finalized block number: %v", err)
finaLizedBlockNumber := uint64(0)
if c.cfg.SafeStatusL1NumberOfBlocks > 0 {
// Overwrite the number of blocks to consider a tx as finalized
currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to get latest block number: %v", err)
}

finaLizedBlockNumber = currentBlockNumber - c.cfg.FinalizedStatusL1NumberOfBlocks
} else {
// Get Network Default value
safeL1BlockNumberFetch := l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.FinalizedBlockNumber, 0)
finaLizedBlockNumber, err = safeL1BlockNumberFetch.GetSafeBlockNumber(ctx, c.etherman)
if err != nil {
return fmt.Errorf("failed to get finalized block number: %v", err)
}
}

for _, mTx := range mTxs {
Expand Down
24 changes: 13 additions & 11 deletions test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ var (

func main() {
config := ethtxmanager.Config{
FrequencyToMonitorTxs: types.Duration{Duration: 1 * time.Second},
WaitTxToBeMined: types.Duration{Duration: 2 * time.Minute},
GetReceiptMaxTime: types.Duration{Duration: 10 * time.Second},
GetReceiptWaitInterval: types.Duration{Duration: 250 * time.Millisecond},
ForcedGas: 0,
GasPriceMarginFactor: 1,
MaxGasPriceLimit: 0,
PersistenceFilename: "ethtxmanager-persistence.json",
ReadPendingL1Txs: false,
Log: log.Config{Level: "info", Environment: "development", Outputs: []string{"stderr"}},
PrivateKeys: []types.KeystoreFileConfig{{Path: "test.keystore", Password: "testonly"}},
FrequencyToMonitorTxs: types.Duration{Duration: 1 * time.Second},
WaitTxToBeMined: types.Duration{Duration: 2 * time.Minute},
GetReceiptMaxTime: types.Duration{Duration: 10 * time.Second},
GetReceiptWaitInterval: types.Duration{Duration: 250 * time.Millisecond},
ForcedGas: 0,
GasPriceMarginFactor: 1,
MaxGasPriceLimit: 0,
SafeStatusL1NumberOfBlocks: 0,
FinalizedStatusL1NumberOfBlocks: 0,
PersistenceFilename: "ethtxmanager-persistence.json",
ReadPendingL1Txs: false,
Log: log.Config{Level: "info", Environment: "development", Outputs: []string{"stderr"}},
PrivateKeys: []types.KeystoreFileConfig{{Path: "test.keystore", Password: "testonly"}},
Etherman: etherman.Config{
URL: "http://localhost:8545",
HTTPHeaders: map[string]string{},
Expand Down
Loading