Skip to content

Commit

Permalink
get safe and finalized block numbers (#38)
Browse files Browse the repository at this point in the history
* get safe and finalized block numbers
* change consolidated to safe
  • Loading branch information
ToniRamirezM authored May 28, 2024
1 parent 5713258 commit 5e1c219
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 366 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ If the statuses are empty, all the statuses are considered.
- **Sent**: transaction was sent to L1
- **Failed**: the tx was already mined and failed with an error that can't be recovered automatically, ex: the data in the tx is invalid and the tx gets reverted
- **Mined**: the tx was already mined and the receipt status is Successful.
- **Finalized**: The tx was mined before the configured number of confirmation blocks.
- **Safe**: The tx was mined and is considered safe.
- **Finalized**: The tx was mined and is considered finalized.
6 changes: 6 additions & 0 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,9 @@ func (etherMan *Client) GetSuggestGasTipCap(ctx context.Context) (*big.Int, erro
gasTipCap, err := etherMan.EthClient.SuggestGasTipCap(ctx)
return gasTipCap, err
}

// HeaderByNumber returns a block header from the current canonical chain. If number is
// nil, the latest known header is returned.
func (etherMan *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
return etherMan.EthClient.HeaderByNumber(ctx, number)
}
4 changes: 0 additions & 4 deletions ethtxmanager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ type Config struct {
GetReceiptMaxTime types.Duration `mapstructure:"WaitReceiptMaxTime"`
// GetReceiptWaitInterval is the time to sleep before trying to get the receipt of the mined transaction
GetReceiptWaitInterval types.Duration `mapstructure:"WaitReceiptCheckInterval"`
// ConsolidationL1ConfirmationBlocks is the number of blocks to wait for a L1 tx to be consolidated
ConsolidationL1ConfirmationBlocks uint64 `mapstructure:"ConsolidationL1ConfirmationBlocks"`
// FinalizationL1ConfirmationBlocks is the number of blocks to wait for a L1 tx to be finalized
FinalizationL1ConfirmationBlocks uint64 `mapstructure:"FinalizationL1ConfirmationBlocks"`

// PrivateKeys defines all the key store files that are going
// to be read in order to provide the private keys to sign the L1 txs
Expand Down
64 changes: 35 additions & 29 deletions ethtxmanager/ethtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/log"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer/l1_check_block"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
Expand Down Expand Up @@ -355,13 +356,13 @@ func (c *Client) Result(ctx context.Context, id common.Hash) (MonitoredTxResult,
return c.buildResult(ctx, mTx)
}

// setStatusConsolidated sets the status of a monitored tx to MonitoredStatusConsolidated.
func (c *Client) setStatusConsolidated(ctx context.Context, id common.Hash) error {
// setStatusSafe sets the status of a monitored tx to MonitoredTxStatusSafe.
func (c *Client) setStatusSafe(ctx context.Context, id common.Hash) error {
mTx, err := c.storage.Get(ctx, id)
if err != nil {
return err
}
mTx.Status = MonitoredTxStatusConsolidated
mTx.Status = MonitoredTxStatusSafe
return c.storage.Update(ctx, mTx)
}

Expand Down Expand Up @@ -439,13 +440,13 @@ func (c *Client) Start() {
if err != nil {
c.logErrorAndWait("failed to monitor txs: %v", err)
}
err = c.waitMinedTxToBeConsolidated(context.Background())
err = c.waitMinedTxToBeSafe(context.Background())
if err != nil {
c.logErrorAndWait("failed to wait consolidated tx to be finalized: %v", err)
c.logErrorAndWait("failed to wait safe tx to be finalized: %v", err)
}
err = c.waitConsolidatedTxToBeFinalized(context.Background())
err = c.waitSafeTxToBeFinalized(context.Background())
if err != nil {
c.logErrorAndWait("failed to wait consolidated tx to be finalized: %v", err)
c.logErrorAndWait("failed to wait safe tx to be finalized: %v", err)
}
}
}
Expand Down Expand Up @@ -486,9 +487,8 @@ func (c *Client) monitorTxs(ctx context.Context) error {
return nil
}

// waitMinedTxToBeConsolidated checks all consolidated monitored txs and wait the number of
// l1 blocks configured to consolidated the tx
func (c *Client) waitMinedTxToBeConsolidated(ctx context.Context) error {
// waitMinedTxToBeSafe checks all mined monitored txs and wait to set the tx as safe
func (c *Client) waitMinedTxToBeSafe(ctx context.Context) error {
statusesFilter := []MonitoredTxStatus{MonitoredTxStatusMined}
mTxs, err := c.storage.GetByStatus(ctx, statusesFilter)
if err != nil {
Expand All @@ -497,16 +497,18 @@ func (c *Client) waitMinedTxToBeConsolidated(ctx context.Context) error {

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

currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx)
// 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 latest block number: %v", err)
return fmt.Errorf("failed to get safe block number: %v", err)
}

for _, mTx := range mTxs {
if mTx.BlockNumber.Uint64()+c.cfg.ConsolidationL1ConfirmationBlocks <= currentBlockNumber {
if mTx.BlockNumber.Uint64() <= safeBlockNumber {
mTxLogger := createMonitoredTxLogger(mTx)
mTxLogger.Infof("consolidated")
mTx.Status = MonitoredTxStatusConsolidated
mTxLogger.Infof("safe")
mTx.Status = MonitoredTxStatusSafe
err := c.storage.Update(ctx, mTx)
if err != nil {
return fmt.Errorf("failed to update mined monitored tx: %v", err)
Expand All @@ -517,30 +519,32 @@ func (c *Client) waitMinedTxToBeConsolidated(ctx context.Context) error {
return nil
}

// waitConsolidatedTxToBeFinalized checks all consolidated monitored txs and wait the number of
// waitSafeTxToBeFinalized checks all safe monitored txs and wait the number of
// l1 blocks configured to finalize the tx
func (c *Client) waitConsolidatedTxToBeFinalized(ctx context.Context) error {
statusesFilter := []MonitoredTxStatus{MonitoredTxStatusConsolidated}
func (c *Client) waitSafeTxToBeFinalized(ctx context.Context) error {
statusesFilter := []MonitoredTxStatus{MonitoredTxStatusSafe}
mTxs, err := c.storage.GetByStatus(ctx, statusesFilter)
if err != nil {
return fmt.Errorf("failed to get consolidated monitored txs: %v", err)
return fmt.Errorf("failed to get safe monitored txs: %v", err)
}

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

currentBlockNumber, err := c.etherman.GetLatestBlockNumber(ctx)
// 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 latest block number: %v", err)
return fmt.Errorf("failed to get finalized block number: %v", err)
}

for _, mTx := range mTxs {
if mTx.BlockNumber.Uint64()+c.cfg.FinalizationL1ConfirmationBlocks <= currentBlockNumber {
if mTx.BlockNumber.Uint64() <= finaLizedBlockNumber {
mTxLogger := createMonitoredTxLogger(mTx)
mTxLogger.Infof("finalized")
mTx.Status = MonitoredTxStatusFinalized
err := c.storage.Update(ctx, mTx)
if err != nil {
return fmt.Errorf("failed to update consolidated monitored tx: %v", err)
return fmt.Errorf("failed to update safe monitored tx: %v", err)
}
}
}
Expand Down Expand Up @@ -943,14 +947,14 @@ func (c *Client) ProcessPendingMonitoredTxs(ctx context.Context, resultHandler R

// if the result is confirmed, we set it as done do stop looking into this monitored tx
if result.Status == MonitoredTxStatusMined {
err := c.setStatusConsolidated(ctx, result.ID)
err := c.setStatusSafe(ctx, result.ID)
if err != nil {
mTxResultLogger.Errorf("failed to set monitored tx as consolidated, err: %v", err)
mTxResultLogger.Errorf("failed to set monitored tx as safe, err: %v", err)
// if something goes wrong at this point, we skip this result and move to the next.
// this result is going to be handled again in the next cycle by the outer loop.
continue
} else {
mTxResultLogger.Info("monitored tx consolidated")
mTxResultLogger.Info("monitored tx safe")
}
resultHandler(result)
continue
Expand Down Expand Up @@ -1018,8 +1022,10 @@ func (c *Client) MakeBlobSidecar(blobs []kzg4844.Blob) *types.BlobTxSidecar {
var proofs []kzg4844.Proof

for _, blob := range blobs {
c, _ := kzg4844.BlobToCommitment(blob)
p, _ := kzg4844.ComputeBlobProof(blob, c)
// avoid memory aliasing
auxBlob := blob
c, _ := kzg4844.BlobToCommitment(&auxBlob)
p, _ := kzg4844.ComputeBlobProof(&auxBlob, c)

commitments = append(commitments, c)
proofs = append(proofs, p)
Expand Down
1 change: 1 addition & 0 deletions ethtxmanager/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ethermanInterface interface {
GetLatestBlockNumber(ctx context.Context) (uint64, error)
GetHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
GetSuggestGasTipCap(ctx context.Context) (*big.Int, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
}

type storageInterface interface {
Expand Down
4 changes: 2 additions & 2 deletions ethtxmanager/monitoredtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const (
// status is Successful
MonitoredTxStatusMined = MonitoredTxStatus("mined")

// MonitoredTxStatusConsolidated means the tx was already mined N blocks ago
MonitoredTxStatusConsolidated = MonitoredTxStatus("consolidated")
// MonitoredTxStatusSafe means the tx was already mined N blocks ago
MonitoredTxStatusSafe = MonitoredTxStatus("safe")

// MonitoredTxStatusFinalized means the tx was already mined M (M > N) blocks ago
MonitoredTxStatusFinalized = MonitoredTxStatus("finalized")
Expand Down
58 changes: 39 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/0xPolygonHermez/zkevm-ethtx-manager

go 1.21
go 1.21.3

require (
github.com/ethereum/go-ethereum v1.13.14
github.com/0xPolygonHermez/zkevm-synchronizer-l1 v0.4.0
github.com/ethereum/go-ethereum v1.14.3
github.com/hermeznetwork/tracerr v0.3.2
github.com/holiman/uint256 v1.2.4
github.com/invopop/jsonschema v0.12.0
Expand All @@ -15,34 +16,51 @@ require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.11 // indirect
Expand All @@ -51,13 +69,15 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.15.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 5e1c219

Please sign in to comment.