From 25310b043b00f5005bf5ad75ecee4254e2995de3 Mon Sep 17 00:00:00 2001 From: sh-cha Date: Tue, 10 Dec 2024 15:09:33 +0900 Subject: [PATCH] skip calculating processed heights when nodes have synced height info --- challenger/challenger.go | 4 ++ executor/executor.go | 82 +++++++++++++++++++++------------------- node/node.go | 15 +++++--- 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/challenger/challenger.go b/challenger/challenger.go index 9bcdb11..bb39fcb 100644 --- a/challenger/challenger.go +++ b/challenger/challenger.go @@ -220,6 +220,10 @@ func (c *Challenger) RegisterQuerier() { } func (c *Challenger) getProcessedHeights(ctx types.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, err error) { + if c.host.Node().GetSyncedHeight() != 0 && c.child.Node().GetSyncedHeight() != 0 { + return 0, 0, 0, nil + } + var outputL1BlockNumber int64 // get the last submitted output height before the start height from the host if c.cfg.L2StartHeight != 0 { diff --git a/executor/executor.go b/executor/executor.go index d096ffc..98de697 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -188,56 +188,62 @@ func (ex *Executor) getNodeStartHeights(ctx types.Context, bridgeId uint64) (l1S var outputL1Height, outputL2Height int64 var outputIndex uint64 - // get the last submitted output height before the start height from the host - output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight) - if err != nil { - return 0, 0, 0, 0, errors.Wrap(err, "failed to query output by l2 block number") - } else if output != nil { - outputL1Height = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber) - outputL2Height = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber) - outputIndex = output.OutputIndex - } - - // use l1 start height from the config if auto set is disabled - if ex.cfg.DisableAutoSetL1Height { - l1StartHeight = ex.cfg.L1StartHeight - } else { - // get the bridge start height from the host - l1StartHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId) + if ex.host.Node().GetSyncedHeight() == 0 || ex.child.Node().GetSyncedHeight() == 0 { + // get the last submitted output height before the start height from the host + output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight) if err != nil { - return 0, 0, 0, 0, errors.Wrap(err, "failed to query create bridge height") + return 0, 0, 0, 0, errors.Wrap(err, "failed to query output by l2 block number") + } else if output != nil { + outputL1Height = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber) + outputL2Height = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber) + outputIndex = output.OutputIndex } + l2StartHeight = outputL2Height + 1 + startOutputIndex = outputIndex + 1 + } - childNextL1Sequence, err := ex.child.QueryNextL1Sequence(ctx, 0) - if err != nil { - return 0, 0, 0, 0, errors.Wrap(err, "failed to query next l1 sequence") - } + if ex.host.Node().GetSyncedHeight() == 0 { + // use l1 start height from the config if auto set is disabled + if ex.cfg.DisableAutoSetL1Height { + l1StartHeight = ex.cfg.L1StartHeight + } else { + // get the bridge start height from the host + l1StartHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId) + if err != nil { + return 0, 0, 0, 0, errors.Wrap(err, "failed to query create bridge height") + } - // query last NextL1Sequence tx height - depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, childNextL1Sequence) - if err != nil { - return 0, 0, 0, 0, errors.Wrap(err, "failed to query deposit tx height") - } else if depositTxHeight == 0 && childNextL1Sequence > 1 { - // if the deposit tx with next_l1_sequence is not found - // query deposit tx with next_l1_sequence-1 tx - depositTxHeight, err = ex.host.QueryDepositTxHeight(ctx, bridgeId, childNextL1Sequence-1) + childNextL1Sequence, err := ex.child.QueryNextL1Sequence(ctx, 0) + if err != nil { + return 0, 0, 0, 0, errors.Wrap(err, "failed to query next l1 sequence") + } + + // query last NextL1Sequence tx height + depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, childNextL1Sequence) if err != nil { return 0, 0, 0, 0, errors.Wrap(err, "failed to query deposit tx height") + } else if depositTxHeight == 0 && childNextL1Sequence > 1 { + // if the deposit tx with next_l1_sequence is not found + // query deposit tx with next_l1_sequence-1 tx + depositTxHeight, err = ex.host.QueryDepositTxHeight(ctx, bridgeId, childNextL1Sequence-1) + if err != nil { + return 0, 0, 0, 0, errors.Wrap(err, "failed to query deposit tx height") + } } - } - if l1StartHeight < depositTxHeight { - l1StartHeight = depositTxHeight - } + if l1StartHeight < depositTxHeight { + l1StartHeight = depositTxHeight + } - if outputL1Height != 0 && outputL1Height+1 < l1StartHeight { - l1StartHeight = outputL1Height + 1 + if outputL1Height != 0 && outputL1Height+1 < l1StartHeight { + l1StartHeight = outputL1Height + 1 + } } } - l2StartHeight = outputL2Height + 1 - startOutputIndex = outputIndex + 1 - batchStartHeight = ex.cfg.BatchStartHeight + if ex.batchSubmitter.Node().GetSyncedHeight() == 0 { + batchStartHeight = ex.cfg.BatchStartHeight + } return } diff --git a/node/node.go b/node/node.go index cc6c743..7ff4094 100644 --- a/node/node.go +++ b/node/node.go @@ -79,6 +79,14 @@ func NewNode(cfg nodetypes.NodeConfig, db types.DB, cdc codec.Codec, txConfig cl return nil, errors.Wrap(err, "failed to create broadcaster") } } + + syncedHeight, err := GetSyncInfo(n.db) + if errors.Is(err, dbtypes.ErrNotFound) { + syncedHeight = 0 + } else if err != nil { + return nil, errors.Wrap(err, "failed to load sync info") + } + n.UpdateSyncedHeight(syncedHeight) return n, nil } @@ -101,17 +109,14 @@ func (n *Node) Initialize(ctx types.Context, processedHeight int64, keyringConfi } } - syncedHeight, err := GetSyncInfo(n.db) // if not found, initialize the height - if errors.Is(err, dbtypes.ErrNotFound) { - syncedHeight = processedHeight + if n.GetSyncedHeight() == 0 { + n.UpdateSyncedHeight(processedHeight) n.startHeightInitialized = true ctx.Logger().Info("initialize height") } else if err != nil { return errors.Wrap(err, "failed to load sync info") } - - n.UpdateSyncedHeight(syncedHeight) ctx.Logger().Debug("load sync info", zap.Int64("synced_height", n.syncedHeight)) return nil }