Skip to content

Commit

Permalink
skip calculating processed heights when nodes have synced height info
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Dec 10, 2024
1 parent eb9c37c commit 25310b0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
4 changes: 4 additions & 0 deletions challenger/challenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
82 changes: 44 additions & 38 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
15 changes: 10 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit 25310b0

Please sign in to comment.