From 9ceb3ee60bc64beb7375c44c1e1e846f8c93d09c Mon Sep 17 00:00:00 2001 From: Sledro Date: Fri, 7 Jun 2024 16:14:49 +0100 Subject: [PATCH 1/6] upload bundles directly after fetching --- rollup/rollup.go | 54 +++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/rollup/rollup.go b/rollup/rollup.go index a832bd9..e41de23 100644 --- a/rollup/rollup.go +++ b/rollup/rollup.go @@ -95,7 +95,7 @@ func (r *Rollup) CreateNextBlock() (*Block, error) { fetchTarget := head.L2Height + blocksToFetch fetchStart := head.L2Height + 1 - bundles, err := r.fetchBundles(fetchStart, fetchTarget) + bundles, pointers, err := r.fetchBundles(fetchStart, fetchTarget) if err != nil { return nil, fmt.Errorf("createNextBlock: Failed to fetch bundles: %w", err) } @@ -106,27 +106,16 @@ func (r *Rollup) CreateNextBlock() (*Block, error) { return nil, fmt.Errorf("createNextBlock: Failed to validate bundles: %w", err) } - // 7. upload the bundle to celestia - r.Opts.Logger.Info("Publishing bundles to Celestia", "bundles", len(bundles), "bundles_size", fetchStart-head.L2Height-1, "ll_height", llHeight, "ll_epoch", epoch) - pointers := make([]canonicalStateChainContract.CanonicalStateChainCelestiaPointer, 0) - for i, bundle := range bundles { - pointer, gasPrice, err := r.Celestia.PublishBundle(*bundle) - if err != nil { - return nil, fmt.Errorf("createNextBlock: Failed to publish bundle: %w", err) - } - r.Opts.Logger.Debug("Published bundle to Celestia", "gas_price", gasPrice, "bundle", i, "bundle_size", bundle.Size(), "celestia_tx", pointer.TxHash.Hex()) - pointers = append(pointers, canonicalStateChainContract.CanonicalStateChainCelestiaPointer{ - Height: pointer.Height, - ShareStart: big.NewInt(int64(pointer.ShareStart)), - ShareLen: uint16(pointer.ShareLen), - }) + if len(bundles) == 0 { + return nil, fmt.Errorf("createNextBlock: No bundles to publish") + } - // Delay between publishing bundles to Celestia to mitigate 'incorrect account sequence' errors - time.Sleep(20 * time.Second) + if len(pointers) == 0 { + return nil, fmt.Errorf("createNextBlock: No pointers to publish") } - if len(bundles) == 0 { - return nil, fmt.Errorf("createNextBlock: No bundles to publish") + if len(bundles) != len(pointers) { + return nil, fmt.Errorf("createNextBlock: Bundles and pointers are not the same length") } // 8. create the rollup header @@ -339,8 +328,9 @@ func (r *Rollup) GetBlockByHash(hash common.Hash) (*Block, error) { return &Block{CanonicalStateChainHeader: &header, Bundles: bundles}, nil } -func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, error) { +func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, []canonicalStateChainContract.CanonicalStateChainCelestiaPointer, error) { bundles := make([]*node.Bundle, 0) + pointers := make([]canonicalStateChainContract.CanonicalStateChainCelestiaPointer, 0) for fetchStart < fetchTarget && uint64(len(bundles)) < r.Opts.BundleCount { from := fetchStart @@ -352,17 +342,37 @@ func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, e bundle, err := r.fetchBundle(from, to) if err != nil { r.Opts.Logger.Error("Failed to fetch bundle", "from", from, "to", to, "error", err) + return nil, nil, err } else { bundles = append(bundles, bundle) } + // upload the bundle to celestia + r.Opts.Logger.Info("Publishing bundle to Celestia", "bundle", len(bundles), "bundle_size", bundle.Size(), "ll_height", bundle.Blocks[len(bundle.Blocks)-1].Number()) + + pointer, gasPrice, err := r.Celestia.PublishBundle(*bundle) + if err != nil { + return nil, nil, fmt.Errorf("createNextBlock: Failed to publish bundle: %w", err) + } + + r.Opts.Logger.Debug("Published bundle to Celestia", "gas_price", gasPrice, "bundle", len(bundles), "bundle_size", bundle.Size(), "celestia_tx", pointer.TxHash.Hex()) + + pointers = append(pointers, canonicalStateChainContract.CanonicalStateChainCelestiaPointer{ + Height: pointer.Height, + ShareStart: big.NewInt(int64(pointer.ShareStart)), + ShareLen: uint16(pointer.ShareLen), + }) + fetchStart = bundle.Blocks[len(bundle.Blocks)-1].Number().Uint64() + 1 } - return bundles, nil + return bundles, pointers, nil } func (r *Rollup) fetchBundle(from, to uint64) (*node.Bundle, error) { + + r.Opts.Logger.Info("Fetching bundle", "from", from, "to", to) + if r.Opts.Store { bundle, err := r.Node.Store.GetBundle(from, to) if err == nil { @@ -388,6 +398,8 @@ func (r *Rollup) fetchBundle(from, to uint64) (*node.Bundle, error) { } } + r.Opts.Logger.Info("Fetched bundle", "from", from, "expected_to", to, "actual_to", l2blocks[len(l2blocks)-1].Number(), "bundle_size", len(l2blocks)) + return bundle, nil } From a809defe3f0f8207fd078da703f35911f4bf5fff Mon Sep 17 00:00:00 2001 From: Sledro Date: Wed, 19 Jun 2024 20:50:44 +0100 Subject: [PATCH 2/6] improve logic --- rollup/rollup.go | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/rollup/rollup.go b/rollup/rollup.go index e41de23..b756208 100644 --- a/rollup/rollup.go +++ b/rollup/rollup.go @@ -333,29 +333,31 @@ func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, [ pointers := make([]canonicalStateChainContract.CanonicalStateChainCelestiaPointer, 0) for fetchStart < fetchTarget && uint64(len(bundles)) < r.Opts.BundleCount { + // Calculate the range of blocks to fetch from := fetchStart to := fetchStart + r.Opts.BundleSize - 1 + + // Ensure we don't fetch more than the fetchTarget if to > fetchTarget { to = fetchTarget } + // Fetch the next bundle from the layer two network bundle, err := r.fetchBundle(from, to) if err != nil { - r.Opts.Logger.Error("Failed to fetch bundle", "from", from, "to", to, "error", err) - return nil, nil, err - } else { - bundles = append(bundles, bundle) + r.Opts.Logger.Error("Failed to fetch bundle, retrying", "from", from, "to", to, "error", err) + continue } - // upload the bundle to celestia - r.Opts.Logger.Info("Publishing bundle to Celestia", "bundle", len(bundles), "bundle_size", bundle.Size(), "ll_height", bundle.Blocks[len(bundle.Blocks)-1].Number()) - - pointer, gasPrice, err := r.Celestia.PublishBundle(*bundle) + // Publish the bundle to the data availability layer (Celestia) + pointer, err := r.publishBundle(*bundle) if err != nil { - return nil, nil, fmt.Errorf("createNextBlock: Failed to publish bundle: %w", err) + r.Opts.Logger.Error("Failed to publish bundle, will refetch and retry", "from", from, "to", to, "error", err) + continue } - r.Opts.Logger.Debug("Published bundle to Celestia", "gas_price", gasPrice, "bundle", len(bundles), "bundle_size", bundle.Size(), "celestia_tx", pointer.TxHash.Hex()) + // If successfully fetched and published the bundle, add it to the list of bundles and pointers + bundles = append(bundles, bundle) pointers = append(pointers, canonicalStateChainContract.CanonicalStateChainCelestiaPointer{ Height: pointer.Height, @@ -363,12 +365,27 @@ func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, [ ShareLen: uint16(pointer.ShareLen), }) + // Update the fetchStart to the next block to begin fetching from fetchStart = bundle.Blocks[len(bundle.Blocks)-1].Number().Uint64() + 1 } return bundles, pointers, nil } +func (r *Rollup) publishBundle(bundle node.Bundle) (*node.CelestiaPointer, error) { + l := r.Opts.Logger.With("bundle_size", bundle.Size(), "first_l2_height", bundle.Blocks[0].Number(), "last_l2_height", bundle.Blocks[len(bundle.Blocks)-1].Number()) + l.Info("Publishing bundle to Celestia...") + + pointer, gasPrice, err := r.Celestia.PublishBundle(bundle) + if err != nil { + return nil, fmt.Errorf("publishBundle: Failed to publish bundle: %w", err) + } + + l.Info("Bundle published successfully!", "gas_price", gasPrice, "celestia_tx", pointer.TxHash.Hex()) + + return pointer, nil +} + func (r *Rollup) fetchBundle(from, to uint64) (*node.Bundle, error) { r.Opts.Logger.Info("Fetching bundle", "from", from, "to", to) From d97dcc60d9dee31482265496b1efbb78c76f9ac2 Mon Sep 17 00:00:00 2001 From: Sledro Date: Wed, 19 Jun 2024 21:44:22 +0100 Subject: [PATCH 3/6] fix logging --- rollup/info.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/rollup/info.go b/rollup/info.go index 1abbf0e..c238a86 100644 --- a/rollup/info.go +++ b/rollup/info.go @@ -36,7 +36,7 @@ func (r *Rollup) GetInfo() (*RollupInfo, error) { return nil, fmt.Errorf("failed to get rollup head: %w", err) } - r.Opts.Logger.Info("Latest Rollup Head", "head", latestRollupHead) + r.Opts.Logger.Info("Latest Rollup Head", "head", latestRollupHead.L2Height) // hash latest rollup head latestRollupHash, err := r.Ethereum.HashHeader(&latestRollupHead) @@ -88,12 +88,6 @@ type RollupBlockInfo struct { BundleSize uint64 `pretty:"Bundle Size"` *canonicalStateChainContract.CanonicalStateChainHeader `pretty:"Header"` - DataAvailability struct { - CelestiaHeight uint64 `pretty:"Celestia Height"` - CelestiaShareStart uint64 `pretty:"Shares Start"` - CelestiaShareLen uint64 `pretty:"Shares"` - } `pretty:"Data Availability"` - Distance struct { FromLatestInEpochs uint64 `pretty:"From Latest Epoch"` FromLatestInL1height uint64 `pretty:"From Latest L1 Height"` From 4cbee4f224738a5baab9e1c6f4726628ad810570 Mon Sep 17 00:00:00 2001 From: Sledro Date: Wed, 19 Jun 2024 21:54:40 +0100 Subject: [PATCH 4/6] log old and new gas price --- node/celestia.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/node/celestia.go b/node/celestia.go index f095201..1f66aca 100644 --- a/node/celestia.go +++ b/node/celestia.go @@ -166,10 +166,17 @@ func (c *CelestiaClient) PublishBundle(blocks Bundle) (*CelestiaPointer, float64 } // Increase gas price by 20% if the transaction fails - gasPrice *= 1.2 - fee = int64(gasPrice * float64(gasLimit)) - - c.logger.Warn("Failed to submit blob, retrying after delay", "delay", c.retryDelay, "attempt", i+1, "fee", fee, "gas_limit", gasLimit, "gas_price", gasPrice, "error", err) + newGasPrice := gasPrice * 1.2 + fee = int64(newGasPrice * float64(gasLimit)) + + c.logger.Warn("Failed to submit blob, retrying after delay", + "delay", c.retryDelay, + "attempt", i+1, + "fee", fee, + "gas_limit", gasLimit, + "old_gas_price", gasPrice, + "new_gas_price", newGasPrice, + "error", err) i++ From 714cdfd98feb40fa34b0b71305c76964120e0703 Mon Sep 17 00:00:00 2001 From: Sledro Date: Wed, 19 Jun 2024 21:57:49 +0100 Subject: [PATCH 5/6] do not increase gas price when cfg var is 0 --- node/celestia.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/celestia.go b/node/celestia.go index 1f66aca..dfb4774 100644 --- a/node/celestia.go +++ b/node/celestia.go @@ -143,7 +143,7 @@ func (c *CelestiaClient) PublishBundle(blocks Bundle) (*CelestiaPointer, float64 // gas price is defined by each node operator. 0.003 is a good default to be accepted gasPrice := c.GasPrice() - if c.gasPriceIncreasePercent != nil { + if c.gasPriceIncreasePercent.Int64() > 0 { apiPrice := gasPrice gasPrice *= 1 + float64(c.gasPriceIncreasePercent.Int64())/100 c.logger.Info("Gas price increased", "percent", c.gasPriceIncreasePercent, "old_gas_price", apiPrice, "new_gas_price", gasPrice) From 8fd12a2fecbf4e8d1439870edb68be7339670bdd Mon Sep 17 00:00:00 2001 From: Sledro Date: Thu, 20 Jun 2024 00:01:19 +0100 Subject: [PATCH 6/6] improve logging --- rollup/rollup.go | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/rollup/rollup.go b/rollup/rollup.go index 9e869b7..39fadbd 100644 --- a/rollup/rollup.go +++ b/rollup/rollup.go @@ -346,19 +346,26 @@ func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, [ to = fetchTarget } + l := r.Opts.Logger.With("bundle_num", len(bundles), "from", from, "to", to) + // Fetch the next bundle from the layer two network + l.Info("Fetching bundle...") bundle, err := r.fetchBundle(from, to) if err != nil { - r.Opts.Logger.Error("Failed to fetch bundle, retrying", "from", from, "to", to, "error", err) + l.Error("Failed to fetch bundle, retrying", "error", err) continue } + l = l.With("bundle_size", bundle.Size(), "actual_to", bundle.Blocks[len(bundle.Blocks)-1].Number()) + l.Info("Bundle fetched successfully!") // Publish the bundle to the data availability layer (Celestia) - pointer, err := r.publishBundle(*bundle) + l.Info("Publishing bundle to Celestia...") + pointer, gasPrice, err := r.Celestia.PublishBundle(*bundle) if err != nil { - r.Opts.Logger.Error("Failed to publish bundle, will refetch and retry", "from", from, "to", to, "error", err) + l.Error("Failed to publish bundle, will refetch and retry") continue } + l.Info("Bundle published successfully!", "gas_price", gasPrice, "celestia_tx", pointer.TxHash.Hex()) // If successfully fetched and published the bundle, add it to the list of bundles and pointers bundles = append(bundles, bundle) @@ -376,24 +383,8 @@ func (r *Rollup) fetchBundles(fetchStart, fetchTarget uint64) ([]*node.Bundle, [ return bundles, pointers, nil } -func (r *Rollup) publishBundle(bundle node.Bundle) (*node.CelestiaPointer, error) { - l := r.Opts.Logger.With("bundle_size", bundle.Size(), "first_l2_height", bundle.Blocks[0].Number(), "last_l2_height", bundle.Blocks[len(bundle.Blocks)-1].Number()) - l.Info("Publishing bundle to Celestia...") - - pointer, gasPrice, err := r.Celestia.PublishBundle(bundle) - if err != nil { - return nil, fmt.Errorf("publishBundle: Failed to publish bundle: %w", err) - } - - l.Info("Bundle published successfully!", "gas_price", gasPrice, "celestia_tx", pointer.TxHash.Hex()) - - return pointer, nil -} - func (r *Rollup) fetchBundle(from, to uint64) (*node.Bundle, error) { - r.Opts.Logger.Info("Fetching bundle", "from", from, "to", to) - if r.Opts.Store { bundle, err := r.Node.Store.GetBundle(from, to) if err == nil { @@ -419,8 +410,6 @@ func (r *Rollup) fetchBundle(from, to uint64) (*node.Bundle, error) { } } - r.Opts.Logger.Info("Fetched bundle", "from", from, "expected_to", to, "actual_to", l2blocks[len(l2blocks)-1].Number(), "bundle_size", len(l2blocks)) - return bundle, nil }