Skip to content

Commit

Permalink
tree starts specific index when initializing the height
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha committed Aug 8, 2024
1 parent 82fb185 commit 70e7626
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion executor/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (bs *BatchSubmitter) Initialize(startHeight uint64, host hostNode, bridgeIn

fileFlag := os.O_CREATE | os.O_RDWR
// if the node has already processed blocks, append to the file
if bs.node.GetHeight()-1 != startHeight {
if !bs.node.HeightInitialized() {
fileFlag |= os.O_APPEND
}

Expand Down
5 changes: 3 additions & 2 deletions executor/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Child struct {

nextOutputTime time.Time
finalizingBlockHeight uint64
startTreeIndex uint64

cfg nodetypes.NodeConfig
db types.DB
Expand Down Expand Up @@ -109,14 +110,14 @@ func GetCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) {
})
}

func (ch *Child) Initialize(startHeight uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
func (ch *Child) Initialize(startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
err := ch.node.Initialize(startHeight)
if err != nil {
return err
}
ch.startTreeIndex = startOutputIndex
ch.host = host
ch.bridgeInfo = bridgeInfo

ch.registerHandlers()
return nil
}
Expand Down
10 changes: 8 additions & 2 deletions executor/child/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,14 @@ func (ch *Child) handleInitiateWithdrawal(l2Sequence uint64, from string, to str
}

func (ch *Child) prepareTree(blockHeight uint64) error {
if blockHeight == 1 {
return ch.mk.InitializeWorkingTree(1, 1)
if ch.startTreeIndex != 0 {
ch.logger.Info("initiate tree", zap.Uint64("index", ch.startTreeIndex))
err := ch.mk.InitializeWorkingTree(ch.startTreeIndex, 1)
if err != nil {
return err
}
ch.startTreeIndex = 0
return nil
}

err := ch.mk.LoadWorkingTree(blockHeight - 1)
Expand Down
10 changes: 5 additions & 5 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval),
)

hostStartHeight, childStartHeight, batchStartHeight, err := executor.getStartHeights()
hostStartHeight, childStartHeight, startOutputIndex, batchStartHeight, err := executor.getStartHeights(int64(bridgeInfo.BridgeId))
if err != nil {
panic(err)
}
Expand All @@ -96,7 +96,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
if err != nil {
panic(err)
}
err = executor.child.Initialize(childStartHeight, executor.host, bridgeInfo)
err = executor.child.Initialize(childStartHeight, startOutputIndex, executor.host, bridgeInfo)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -207,15 +207,15 @@ func (ex *Executor) makeDANode(bridgeId int64) (executortypes.DANode, error) {
return nil, fmt.Errorf("unsupported chain id for DA: %s", ophosttypes.BatchInfo_ChainType_name[int32(batchInfo.BatchInfo.ChainType)])
}

func (ex *Executor) getStartHeights() (l1StartHeight uint64, l2StartHeight uint64, batchStartHeight uint64, err error) {
func (ex *Executor) getStartHeights(bridgeId int64) (l1StartHeight uint64, l2StartHeight uint64, startOutputIndex uint64, batchStartHeight uint64, err error) {
if ex.cfg.L2StartHeight != 0 {
l1StartHeight, l2StartHeight, err = ex.host.QueryHeightsOfOutputTxWithL2BlockNumber(uint64(ex.cfg.L2StartHeight))
l1StartHeight, l2StartHeight, startOutputIndex, err = ex.host.QueryHeightsOfOutputTxWithL2BlockNumber(bridgeId, uint64(ex.cfg.L2StartHeight))
}

if ex.cfg.BatchStartWithL2Height {
batchStartHeight = l2StartHeight
} else {
batchStartHeight = uint64(ex.cfg.BatchStartHeight)
}
return l1StartHeight, l2StartHeight, batchStartHeight, err
return l1StartHeight, l2StartHeight, startOutputIndex, batchStartHeight, err
}
25 changes: 14 additions & 11 deletions executor/host/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,48 @@ func (h Host) QueryBatchInfos() (*ophosttypes.QueryBatchInfosResponse, error) {
return h.ophostQueryClient.BatchInfos(ctx, req)
}

func (h Host) QueryHeightsOfOutputTxWithL2BlockNumber(l2BlockNumber uint64) (uint64, uint64, error) {
func (h Host) QueryHeightsOfOutputTxWithL2BlockNumber(bridgeId int64, l2BlockNumber uint64) (uint64, uint64, uint64, error) {
ctx, cancel := rpcclient.GetQueryContext(0)
defer cancel()

query := fmt.Sprintf("%s.%s = %d AND %s.%s <= %d", ophosttypes.EventTypeProposeOutput,
ophosttypes.AttributeKeyBridgeId,
h.bridgeId,
bridgeId,
ophosttypes.EventTypeProposeOutput,
ophosttypes.AttributeKeyL2BlockNumber,
l2BlockNumber,
)

perPage := 1
res, err := h.node.GetRPCClient().TxSearch(ctx, query, false, nil, &perPage, "desc")
if err != nil {
return 0, 0, err
return 0, 0, 0, err
}
if len(res.Txs) == 0 {
// no output tx found
return 0, 0, nil
return 0, 0, 0, nil
}

l2StartHeight := uint64(0)
LOOP:
outputIndex := uint64(0)
for _, event := range res.Txs[0].TxResult.Events {
if event.Type == ophosttypes.EventTypeProposeOutput {
for _, attr := range event.Attributes {
if attr.Key == ophosttypes.AttributeKeyL2BlockNumber {
l2StartHeight, err = strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return 0, 0, err
return 0, 0, 0, err
}
} else if attr.Key == ophosttypes.AttributeKeyOutputIndex {
outputIndex, err = strconv.ParseUint(attr.Value, 10, 64)
if err != nil {
return 0, 0, 0, err
}
break LOOP
}
}
}
}
if l2StartHeight == 0 {
return 0, 0, fmt.Errorf("something wrong: l2 block number not found in the output tx")
if l2StartHeight == 0 || outputIndex == 0 {
return 0, 0, 0, fmt.Errorf("something wrong: l2 block number not found in the output tx")
}
return uint64(res.Txs[0].Height), l2StartHeight, nil
return uint64(res.Txs[0].Height), l2StartHeight, outputIndex + 1, nil
}
5 changes: 5 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Node struct {
rawBlockHandler nodetypes.RawBlockHandlerFn

// status info
startHeightInitialized bool
lastProcessedBlockHeight uint64
running bool
}
Expand Down Expand Up @@ -96,6 +97,10 @@ func (n *Node) Initialize(startHeight uint64) error {
return n.loadSyncInfo(startHeight)
}

func (n *Node) HeightInitialized() bool {
return n.startHeightInitialized
}

func (n *Node) Start(ctx context.Context) {
if n.running {
return
Expand Down

0 comments on commit 70e7626

Please sign in to comment.