Skip to content

Commit

Permalink
sync from l1sequenece -1 (#25)
Browse files Browse the repository at this point in the history
* sync from l1sequenece -1

* sync from l1sequence -1

* rename startheights and calculate processed heights properly
  • Loading branch information
sh-cha authored Sep 30, 2024
1 parent 4e8ac2c commit c3442f5
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 68 deletions.
38 changes: 21 additions & 17 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions challenger/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions challenger/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions executor/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions executor/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
58 changes: 31 additions & 27 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions executor/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions node/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions provider/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions provider/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion provider/host/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c3442f5

Please sign in to comment.