From a3d3fc99cb92776e10e24883475e65af08af4910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 31 May 2024 09:36:55 +0200 Subject: [PATCH 1/4] allow overwritting safe and finalized number of blocks --- ethtxmanager/config.go | 10 ++++++++++ ethtxmanager/ethtxmanager.go | 16 ++++++++++++++-- test/main.go | 24 +++++++++++++----------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/ethtxmanager/config.go b/ethtxmanager/config.go index 1bd4897..233f44d 100644 --- a/ethtxmanager/config.go +++ b/ethtxmanager/config.go @@ -66,4 +66,14 @@ type Config struct { // Log configuration Log log.Config `mapstructure:"Log"` + + // OverwriteSafeStatusL1NumberOfBlocks is 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 + OverwriteSafeStatusL1NumberOfBlocks uint64 `mapstructure:"OverwriteSafeStatusL1NumberOfBlocks"` + + // OverwriteFinalizedStatusL1NumberOfBlocks is 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 + OverwriteFinalizedStatusL1NumberOfBlocks uint64 `mapstructure:"OverwriteFinalizedStatusL1NumberOfBlocks"` } diff --git a/ethtxmanager/ethtxmanager.go b/ethtxmanager/ethtxmanager.go index 0a9d534..ad0159f 100644 --- a/ethtxmanager/ethtxmanager.go +++ b/ethtxmanager/ethtxmanager.go @@ -497,6 +497,11 @@ func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error { log.Debugf("found %v mined monitored tx to process", len(mTxs)) + currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx) + if err != nil { + return fmt.Errorf("failed to get latest block number: %v", err) + } + // Get Safe block Number safeL1BlockNumberFetch := l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.SafeBlockNumber, 0) safeBlockNumber, err := safeL1BlockNumberFetch.GetSafeBlockNumber(ctx, c.etherman) @@ -505,7 +510,8 @@ func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error { } for _, mTx := range mTxs { - if mTx.BlockNumber.Uint64() <= safeBlockNumber { + if (c.cfg.OverwriteSafeStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= safeBlockNumber) || + (c.cfg.OverwriteSafeStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.OverwriteSafeStatusL1NumberOfBlocks) { mTxLogger := createMonitoredTxLogger(mTx) mTxLogger.Infof("safe") mTx.Status = MonitoredTxStatusSafe @@ -530,6 +536,11 @@ func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error { log.Debugf("found %v safe monitored tx to process", len(mTxs)) + currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx) + if err != nil { + return fmt.Errorf("failed to get latest block number: %v", err) + } + // Get Finalized block Number safeL1BlockNumberFetch := l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.FinalizedBlockNumber, 0) finaLizedBlockNumber, err := safeL1BlockNumberFetch.GetSafeBlockNumber(ctx, c.etherman) @@ -538,7 +549,8 @@ func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error { } for _, mTx := range mTxs { - if mTx.BlockNumber.Uint64() <= finaLizedBlockNumber { + if (c.cfg.OverwriteFinalizedStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= finaLizedBlockNumber) || + (c.cfg.OverwriteFinalizedStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.OverwriteFinalizedStatusL1NumberOfBlocks) { mTxLogger := createMonitoredTxLogger(mTx) mTxLogger.Infof("finalized") mTx.Status = MonitoredTxStatusFinalized diff --git a/test/main.go b/test/main.go index 7805c18..4d48dc9 100644 --- a/test/main.go +++ b/test/main.go @@ -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, + OverwriteSafeStatusL1NumberOfBlocks: 0, + OverwriteFinalizedStatusL1NumberOfBlocks: 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{}, From ec872b71fa1d8349d1df23b1e4be6c2f2ca802ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 31 May 2024 13:04:42 +0200 Subject: [PATCH 2/4] rename config params --- ethtxmanager/config.go | 8 ++++---- ethtxmanager/ethtxmanager.go | 8 ++++---- test/main.go | 26 +++++++++++++------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ethtxmanager/config.go b/ethtxmanager/config.go index 233f44d..e2902e7 100644 --- a/ethtxmanager/config.go +++ b/ethtxmanager/config.go @@ -67,13 +67,13 @@ type Config struct { // Log configuration Log log.Config `mapstructure:"Log"` - // OverwriteSafeStatusL1NumberOfBlocks is the number of blocks to consider a tx as safe + // 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 - OverwriteSafeStatusL1NumberOfBlocks uint64 `mapstructure:"OverwriteSafeStatusL1NumberOfBlocks"` + SafeStatusL1NumberOfBlocks uint64 `mapstructure:"SafeStatusL1NumberOfBlocks"` - // OverwriteFinalizedStatusL1NumberOfBlocks is the number of blocks to consider a tx as finalized + // 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 - OverwriteFinalizedStatusL1NumberOfBlocks uint64 `mapstructure:"OverwriteFinalizedStatusL1NumberOfBlocks"` + FinalizedStatusL1NumberOfBlocks uint64 `mapstructure:"FinalizedStatusL1NumberOfBlocks"` } diff --git a/ethtxmanager/ethtxmanager.go b/ethtxmanager/ethtxmanager.go index ad0159f..beb0f41 100644 --- a/ethtxmanager/ethtxmanager.go +++ b/ethtxmanager/ethtxmanager.go @@ -510,8 +510,8 @@ func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error { } for _, mTx := range mTxs { - if (c.cfg.OverwriteSafeStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= safeBlockNumber) || - (c.cfg.OverwriteSafeStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.OverwriteSafeStatusL1NumberOfBlocks) { + if (c.cfg.SafeStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= safeBlockNumber) || + (c.cfg.SafeStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.SafeStatusL1NumberOfBlocks) { mTxLogger := createMonitoredTxLogger(mTx) mTxLogger.Infof("safe") mTx.Status = MonitoredTxStatusSafe @@ -549,8 +549,8 @@ func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error { } for _, mTx := range mTxs { - if (c.cfg.OverwriteFinalizedStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= finaLizedBlockNumber) || - (c.cfg.OverwriteFinalizedStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.OverwriteFinalizedStatusL1NumberOfBlocks) { + if (c.cfg.FinalizedStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= finaLizedBlockNumber) || + (c.cfg.FinalizedStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.FinalizedStatusL1NumberOfBlocks) { mTxLogger := createMonitoredTxLogger(mTx) mTxLogger.Infof("finalized") mTx.Status = MonitoredTxStatusFinalized diff --git a/test/main.go b/test/main.go index 4d48dc9..506bdd1 100644 --- a/test/main.go +++ b/test/main.go @@ -21,19 +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, - OverwriteSafeStatusL1NumberOfBlocks: 0, - OverwriteFinalizedStatusL1NumberOfBlocks: 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{}, From 0177beccdd9ff688cb9c1b572e66c86b35bfe1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 31 May 2024 13:07:07 +0200 Subject: [PATCH 3/4] only get current block number if needed --- ethtxmanager/ethtxmanager.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ethtxmanager/ethtxmanager.go b/ethtxmanager/ethtxmanager.go index beb0f41..47062f3 100644 --- a/ethtxmanager/ethtxmanager.go +++ b/ethtxmanager/ethtxmanager.go @@ -497,9 +497,12 @@ func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error { log.Debugf("found %v mined monitored tx to process", len(mTxs)) - currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx) - if err != nil { - return fmt.Errorf("failed to get latest block number: %v", err) + currentBlockNumber := uint64(0) + if c.cfg.SafeStatusL1NumberOfBlocks > 0 { + currentBlockNumber, err = c.etherman.GetLatestBlockNumber(ctx) + if err != nil { + return fmt.Errorf("failed to get latest block number: %v", err) + } } // Get Safe block Number @@ -536,9 +539,12 @@ func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error { log.Debugf("found %v safe monitored tx to process", len(mTxs)) - currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx) - if err != nil { - return fmt.Errorf("failed to get latest block number: %v", err) + currentBlockNumber := uint64(0) + if c.cfg.SafeStatusL1NumberOfBlocks > 0 { + currentBlockNumber, err = c.etherman.GetLatestBlockNumber(ctx) + if err != nil { + return fmt.Errorf("failed to get latest block number: %v", err) + } } // Get Finalized block Number From f965b524b4046fde09131292fc590c123a57ac0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 31 May 2024 13:15:48 +0200 Subject: [PATCH 4/4] refactor --- ethtxmanager/ethtxmanager.go | 44 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/ethtxmanager/ethtxmanager.go b/ethtxmanager/ethtxmanager.go index 47062f3..f6f6715 100644 --- a/ethtxmanager/ethtxmanager.go +++ b/ethtxmanager/ethtxmanager.go @@ -497,24 +497,26 @@ func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error { log.Debugf("found %v mined monitored tx to process", len(mTxs)) - currentBlockNumber := uint64(0) + safeBlockNumber := uint64(0) if c.cfg.SafeStatusL1NumberOfBlocks > 0 { - currentBlockNumber, err = c.etherman.GetLatestBlockNumber(ctx) + // 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) } - } - // 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 = 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 { - if (c.cfg.SafeStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= safeBlockNumber) || - (c.cfg.SafeStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.SafeStatusL1NumberOfBlocks) { + if mTx.BlockNumber.Uint64() <= safeBlockNumber { mTxLogger := createMonitoredTxLogger(mTx) mTxLogger.Infof("safe") mTx.Status = MonitoredTxStatusSafe @@ -539,24 +541,26 @@ func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error { log.Debugf("found %v safe monitored tx to process", len(mTxs)) - currentBlockNumber := uint64(0) + finaLizedBlockNumber := uint64(0) if c.cfg.SafeStatusL1NumberOfBlocks > 0 { - currentBlockNumber, err = c.etherman.GetLatestBlockNumber(ctx) + // 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) } - } - // 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 = 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 { - if (c.cfg.FinalizedStatusL1NumberOfBlocks == 0 && mTx.BlockNumber.Uint64() <= finaLizedBlockNumber) || - (c.cfg.FinalizedStatusL1NumberOfBlocks > 0 && mTx.BlockNumber.Uint64() <= currentBlockNumber-c.cfg.FinalizedStatusL1NumberOfBlocks) { + if mTx.BlockNumber.Uint64() <= finaLizedBlockNumber { mTxLogger := createMonitoredTxLogger(mTx) mTxLogger.Infof("finalized") mTx.Status = MonitoredTxStatusFinalized