From c3442f5a2c6964c5fe013b2296822a43c1cf376f Mon Sep 17 00:00:00 2001 From: Hoon <48665813+sh-cha@users.noreply.github.com> Date: Mon, 30 Sep 2024 18:31:31 +0900 Subject: [PATCH] sync from l1sequenece -1 (#25) * sync from l1sequenece -1 * sync from l1sequence -1 * rename startheights and calculate processed heights properly --- challenger/challenger.go | 38 +++++++++++++------------ challenger/child/child.go | 4 +-- challenger/host/host.go | 4 +-- executor/batch/batch.go | 4 +-- executor/child/child.go | 4 +-- executor/executor.go | 58 +++++++++++++++++++++------------------ executor/host/host.go | 4 +-- node/db.go | 6 ++-- node/node.go | 4 +-- provider/child/child.go | 12 ++++---- provider/host/host.go | 4 +-- provider/host/query.go | 2 +- 12 files changed, 76 insertions(+), 68 deletions(-) diff --git a/challenger/challenger.go b/challenger/challenger.go index 1579951..4af9c54 100644 --- a/challenger/challenger.go +++ b/challenger/challenger.go @@ -96,16 +96,16 @@ func (c *Challenger) Initialize(ctx context.Context) error { zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval), ) - hostStartHeight, childStartHeight, startOutputIndex, err := c.getStartHeights(ctx, bridgeInfo.BridgeId) + hostProcessedHeight, childProcessedHeight, processedOutputIndex, err := c.getProcessedHeights(ctx, bridgeInfo.BridgeId) if err != nil { return err } - err = c.host.Initialize(ctx, hostStartHeight, c.child, bridgeInfo, c) + err = c.host.Initialize(ctx, hostProcessedHeight, c.child, bridgeInfo, c) if err != nil { return err } - err = c.child.Initialize(ctx, childStartHeight, startOutputIndex, c.host, bridgeInfo, c) + err = c.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, c.host, bridgeInfo, c) if err != nil { return err } @@ -192,9 +192,9 @@ func (c *Challenger) RegisterQuerier() { }) } -func (c *Challenger) getStartHeights(ctx context.Context, bridgeId uint64) (l1StartHeight int64, l2StartHeight int64, startOutputIndex uint64, err error) { +func (c *Challenger) getProcessedHeights(ctx context.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, err error) { // get the bridge start height from the host - l1StartHeight, err = c.host.QueryCreateBridgeHeight(ctx, bridgeId) + l1ProcessedHeight, err = c.host.QueryCreateBridgeHeight(ctx, bridgeId) if err != nil { return 0, 0, 0, err } @@ -205,27 +205,31 @@ func (c *Challenger) getStartHeights(ctx context.Context, bridgeId uint64) (l1St if err != nil { return 0, 0, 0, err } else if output != nil { - l1StartHeight = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber) - l2StartHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber) - startOutputIndex = output.OutputIndex + 1 + l1ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber) + l2ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber) + processedOutputIndex = output.OutputIndex } } - if l2StartHeight > 0 { + if l2ProcessedHeight > 0 { // get the last deposit tx height from the host - l1Sequence, err := c.child.QueryNextL1Sequence(ctx, l2StartHeight-1) + l1Sequence, err := c.child.QueryNextL1Sequence(ctx, l2ProcessedHeight-1) if err != nil { return 0, 0, 0, err } - depositTxHeight, err := c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1) + // query l1Sequence tx height + depositTxHeight, err := c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence) if err != nil { return 0, 0, 0, err + } else if depositTxHeight == 0 && l1Sequence > 1 { + // query l1Sequence - 1 tx height + depositTxHeight, err = c.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1) + if err != nil { + return 0, 0, 0, err + } } - if l1StartHeight > depositTxHeight { - l1StartHeight = depositTxHeight + if depositTxHeight >= 1 && depositTxHeight-1 < l1ProcessedHeight { + l1ProcessedHeight = depositTxHeight - 1 } } - if l2StartHeight == 0 { - startOutputIndex = 1 - } - return l1StartHeight, l2StartHeight, startOutputIndex, err + return l1ProcessedHeight, l2ProcessedHeight, processedOutputIndex, err } diff --git a/challenger/child/child.go b/challenger/child/child.go index fc91ac0..76d7fc5 100644 --- a/challenger/child/child.go +++ b/challenger/child/child.go @@ -56,8 +56,8 @@ func NewChildV1( } } -func (ch *Child) Initialize(ctx context.Context, startHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error { - _, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo) +func (ch *Child) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error { + _, err := ch.BaseChild.Initialize(ctx, processedHeight, startOutputIndex, bridgeInfo) if err != nil { return err } diff --git a/challenger/host/host.go b/challenger/host/host.go index a86863a..3b20393 100644 --- a/challenger/host/host.go +++ b/challenger/host/host.go @@ -55,8 +55,8 @@ func NewHostV1( } } -func (h *Host) Initialize(ctx context.Context, startHeight int64, child childNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error { - err := h.BaseHost.Initialize(ctx, startHeight, bridgeInfo) +func (h *Host) Initialize(ctx context.Context, processedHeight int64, child childNode, bridgeInfo opchildtypes.BridgeInfo, challenger challenger) error { + err := h.BaseHost.Initialize(ctx, processedHeight, bridgeInfo) if err != nil { return err } diff --git a/executor/batch/batch.go b/executor/batch/batch.go index 955d113..9a60c2b 100644 --- a/executor/batch/batch.go +++ b/executor/batch/batch.go @@ -96,8 +96,8 @@ func NewBatchSubmitterV1( return ch } -func (bs *BatchSubmitter) Initialize(ctx context.Context, startHeight int64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error { - err := bs.node.Initialize(ctx, startHeight) +func (bs *BatchSubmitter) Initialize(ctx context.Context, processedHeight int64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error { + err := bs.node.Initialize(ctx, processedHeight) if err != nil { return err } diff --git a/executor/child/child.go b/executor/child/child.go index 3250e8e..5504b20 100644 --- a/executor/child/child.go +++ b/executor/child/child.go @@ -53,8 +53,8 @@ func NewChildV1( } } -func (ch *Child) Initialize(ctx context.Context, startHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error { - l2Sequence, err := ch.BaseChild.Initialize(ctx, startHeight, startOutputIndex, bridgeInfo) +func (ch *Child) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error { + l2Sequence, err := ch.BaseChild.Initialize(ctx, processedHeight, startOutputIndex, bridgeInfo) if err != nil { return err } diff --git a/executor/executor.go b/executor/executor.go index 5f0719c..5d8ed6c 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -89,20 +89,20 @@ func (ex *Executor) Initialize(ctx context.Context) error { zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval), ) - hostStartHeight, childStartHeight, startOutputIndex, batchStartHeight, err := ex.getStartHeights(ctx, bridgeInfo.BridgeId) + hostProcessedHeight, childProcessedHeight, processedOutputIndex, batchProcessedHeight, err := ex.getProcessedHeights(ctx, bridgeInfo.BridgeId) if err != nil { return err } - err = ex.host.Initialize(ctx, hostStartHeight, ex.child, ex.batch, bridgeInfo) + err = ex.host.Initialize(ctx, hostProcessedHeight, ex.child, ex.batch, bridgeInfo) if err != nil { return err } - err = ex.child.Initialize(ctx, childStartHeight, startOutputIndex, ex.host, bridgeInfo) + err = ex.child.Initialize(ctx, childProcessedHeight, processedOutputIndex+1, ex.host, bridgeInfo) if err != nil { return err } - err = ex.batch.Initialize(ctx, batchStartHeight, ex.host, bridgeInfo) + err = ex.batch.Initialize(ctx, batchProcessedHeight, ex.host, bridgeInfo) if err != nil { return err } @@ -213,46 +213,50 @@ func (ex *Executor) makeDANode(ctx context.Context, bridgeInfo opchildtypes.Brid return nil, fmt.Errorf("unsupported chain id for DA: %s", ophosttypes.BatchInfo_ChainType_name[int32(batchInfo.BatchInfo.ChainType)]) } -func (ex *Executor) getStartHeights(ctx context.Context, bridgeId uint64) (l1StartHeight int64, l2StartHeight int64, startOutputIndex uint64, batchStartHeight int64, err error) { +func (ex *Executor) getProcessedHeights(ctx context.Context, bridgeId uint64) (l1ProcessedHeight int64, l2ProcessedHeight int64, processedOutputIndex uint64, batchProcessedHeight int64, err error) { // get the bridge start height from the host - l1StartHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId) + l1ProcessedHeight, err = ex.host.QueryCreateBridgeHeight(ctx, bridgeId) if err != nil { return 0, 0, 0, 0, err } - // get the last submitted output height before the start height from the host - if ex.cfg.L2StartHeight != 0 { - output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight) - if err != nil { - return 0, 0, 0, 0, err - } else if output != nil { - l1StartHeight = types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber) - l2StartHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber) - startOutputIndex = output.OutputIndex + 1 - } - } - // get the last deposit tx height from the host l1Sequence, err := ex.child.QueryNextL1Sequence(ctx, 0) if err != nil { return 0, 0, 0, 0, err } + // query l1Sequence tx height depositTxHeight, err := ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence) if err != nil { return 0, 0, 0, 0, err + } else if depositTxHeight == 0 && l1Sequence > 1 { + // query l1Sequence - 1 tx height + depositTxHeight, err = ex.host.QueryDepositTxHeight(ctx, bridgeId, l1Sequence-1) + if err != nil { + return 0, 0, 0, 0, err + } } - if l1StartHeight > depositTxHeight { - l1StartHeight = depositTxHeight + if depositTxHeight >= 1 && depositTxHeight-1 > l1ProcessedHeight { + l1ProcessedHeight = depositTxHeight - 1 } - if l2StartHeight == 0 { - startOutputIndex = 1 + // get the last submitted output height before the start height from the host + if ex.cfg.L2StartHeight != 0 { + output, err := ex.host.QueryOutputByL2BlockNumber(ctx, bridgeId, ex.cfg.L2StartHeight) + if err != nil { + return 0, 0, 0, 0, err + } else if output != nil { + l1BlockNumber := types.MustUint64ToInt64(output.OutputProposal.L1BlockNumber) + if l1BlockNumber < l1ProcessedHeight { + l1ProcessedHeight = l1BlockNumber + } + l2ProcessedHeight = types.MustUint64ToInt64(output.OutputProposal.L2BlockNumber) + processedOutputIndex = output.OutputIndex + } } + if ex.cfg.BatchStartHeight > 0 { - batchStartHeight = ex.cfg.BatchStartHeight - 1 - } - if l1StartHeight > 0 { - l1StartHeight-- + batchProcessedHeight = ex.cfg.BatchStartHeight - 1 } - return l1StartHeight, l2StartHeight, startOutputIndex, batchStartHeight, err + return l1ProcessedHeight, l2ProcessedHeight, processedOutputIndex, batchProcessedHeight, err } diff --git a/executor/host/host.go b/executor/host/host.go index c782d31..ee5ac20 100644 --- a/executor/host/host.go +++ b/executor/host/host.go @@ -61,8 +61,8 @@ func NewHostV1( } } -func (h *Host) Initialize(ctx context.Context, startHeight int64, child childNode, batch batchNode, bridgeInfo opchildtypes.BridgeInfo) error { - err := h.BaseHost.Initialize(ctx, startHeight, bridgeInfo) +func (h *Host) Initialize(ctx context.Context, processedHeight int64, child childNode, batch batchNode, bridgeInfo opchildtypes.BridgeInfo) error { + err := h.BaseHost.Initialize(ctx, processedHeight, bridgeInfo) if err != nil { return err } diff --git a/node/db.go b/node/db.go index b1c4da3..cee9fe9 100644 --- a/node/db.go +++ b/node/db.go @@ -15,12 +15,12 @@ func (n *Node) SetSyncInfo(height int64) { } } -func (n *Node) loadSyncInfo(startHeight int64) error { +func (n *Node) loadSyncInfo(processedHeight int64) error { data, err := n.db.Get(nodetypes.LastProcessedBlockHeightKey) if err == dbtypes.ErrNotFound { - n.SetSyncInfo(startHeight) + n.SetSyncInfo(processedHeight) n.startHeightInitialized = true - n.logger.Info("initialize sync info", zap.Int64("start_height", startHeight+1)) + n.logger.Info("initialize sync info", zap.Int64("start_height", processedHeight+1)) return nil } else if err != nil { return err diff --git a/node/node.go b/node/node.go index b346b45..bd6c2d0 100644 --- a/node/node.go +++ b/node/node.go @@ -85,7 +85,7 @@ func NewNode(cfg nodetypes.NodeConfig, db types.DB, logger *zap.Logger, cdc code // StartHeight is the height to start processing. // If it is 0, the latest height is used. // If the latest height exists in the database, this is ignored. -func (n *Node) Initialize(ctx context.Context, startHeight int64) (err error) { +func (n *Node) Initialize(ctx context.Context, processedHeight int64) (err error) { // check if node is catching up status, err := n.rpcClient.Status(ctx) if err != nil { @@ -102,7 +102,7 @@ func (n *Node) Initialize(ctx context.Context, startHeight int64) (err error) { } // load sync info - return n.loadSyncInfo(startHeight) + return n.loadSyncInfo(processedHeight) } func (n *Node) HeightInitialized() bool { diff --git a/provider/child/child.go b/provider/child/child.go index 2a77441..ec1b01d 100644 --- a/provider/child/child.go +++ b/provider/child/child.go @@ -89,15 +89,15 @@ func GetCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) { }) } -func (b *BaseChild) Initialize(ctx context.Context, startHeight int64, startOutputIndex uint64, bridgeInfo opchildtypes.BridgeInfo) (uint64, error) { - err := b.node.Initialize(ctx, startHeight) +func (b *BaseChild) Initialize(ctx context.Context, processedHeight int64, startOutputIndex uint64, bridgeInfo opchildtypes.BridgeInfo) (uint64, error) { + err := b.node.Initialize(ctx, processedHeight) if err != nil { return 0, err } var l2Sequence uint64 - if b.node.HeightInitialized() && startOutputIndex != 0 { - l2Sequence, err = b.QueryNextL2Sequence(ctx, startHeight) + if b.node.HeightInitialized() { + l2Sequence, err = b.QueryNextL2Sequence(ctx, processedHeight) if err != nil { return 0, err } @@ -107,14 +107,14 @@ func (b *BaseChild) Initialize(ctx context.Context, startHeight int64, startOutp return 0, err } - version := types.MustInt64ToUint64(startHeight) + version := types.MustInt64ToUint64(processedHeight) err = b.mk.DeleteFutureWorkingTrees(version + 1) if err != nil { return 0, err } b.initializeTreeFn = func(blockHeight int64) (bool, error) { - if startHeight+1 == blockHeight { + if processedHeight+1 == blockHeight { b.logger.Info("initialize tree", zap.Uint64("index", startOutputIndex)) err := b.mk.InitializeWorkingTree(startOutputIndex, 1) if err != nil { diff --git a/provider/host/host.go b/provider/host/host.go index 38607d7..6b3e26e 100644 --- a/provider/host/host.go +++ b/provider/host/host.go @@ -79,8 +79,8 @@ func GetCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) { }) } -func (b *BaseHost) Initialize(ctx context.Context, startHeight int64, bridgeInfo opchildtypes.BridgeInfo) error { - err := b.node.Initialize(ctx, startHeight) +func (b *BaseHost) Initialize(ctx context.Context, processedHeight int64, bridgeInfo opchildtypes.BridgeInfo) error { + err := b.node.Initialize(ctx, processedHeight) if err != nil { return err } diff --git a/provider/host/query.go b/provider/host/query.go index 109d6d0..39bb83b 100644 --- a/provider/host/query.go +++ b/provider/host/query.go @@ -210,5 +210,5 @@ func (b BaseHost) QueryDepositTxHeight(ctx context.Context, bridgeId uint64, l1S break } } - return 0, fmt.Errorf("failed to fetch deposit tx with l1 Sequence: %d", l1Sequence) + return 0, nil }