Skip to content

Commit 70e7626

Browse files
committed
tree starts specific index when initializing the height
1 parent 82fb185 commit 70e7626

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

executor/batch/batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (bs *BatchSubmitter) Initialize(startHeight uint64, host hostNode, bridgeIn
129129

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

executor/child/child.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Child struct {
5151

5252
nextOutputTime time.Time
5353
finalizingBlockHeight uint64
54+
startTreeIndex uint64
5455

5556
cfg nodetypes.NodeConfig
5657
db types.DB
@@ -109,14 +110,14 @@ func GetCodec(bech32Prefix string) (codec.Codec, client.TxConfig, error) {
109110
})
110111
}
111112

112-
func (ch *Child) Initialize(startHeight uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
113+
func (ch *Child) Initialize(startHeight uint64, startOutputIndex uint64, host hostNode, bridgeInfo opchildtypes.BridgeInfo) error {
113114
err := ch.node.Initialize(startHeight)
114115
if err != nil {
115116
return err
116117
}
118+
ch.startTreeIndex = startOutputIndex
117119
ch.host = host
118120
ch.bridgeInfo = bridgeInfo
119-
120121
ch.registerHandlers()
121122
return nil
122123
}

executor/child/withdraw.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ func (ch *Child) handleInitiateWithdrawal(l2Sequence uint64, from string, to str
8686
}
8787

8888
func (ch *Child) prepareTree(blockHeight uint64) error {
89-
if blockHeight == 1 {
90-
return ch.mk.InitializeWorkingTree(1, 1)
89+
if ch.startTreeIndex != 0 {
90+
ch.logger.Info("initiate tree", zap.Uint64("index", ch.startTreeIndex))
91+
err := ch.mk.InitializeWorkingTree(ch.startTreeIndex, 1)
92+
if err != nil {
93+
return err
94+
}
95+
ch.startTreeIndex = 0
96+
return nil
9197
}
9298

9399
err := ch.mk.LoadWorkingTree(blockHeight - 1)

executor/executor.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
8787
zap.Duration("submission_interval", bridgeInfo.BridgeConfig.SubmissionInterval),
8888
)
8989

90-
hostStartHeight, childStartHeight, batchStartHeight, err := executor.getStartHeights()
90+
hostStartHeight, childStartHeight, startOutputIndex, batchStartHeight, err := executor.getStartHeights(int64(bridgeInfo.BridgeId))
9191
if err != nil {
9292
panic(err)
9393
}
@@ -96,7 +96,7 @@ func NewExecutor(cfg *executortypes.Config, db types.DB, sv *server.Server, logg
9696
if err != nil {
9797
panic(err)
9898
}
99-
err = executor.child.Initialize(childStartHeight, executor.host, bridgeInfo)
99+
err = executor.child.Initialize(childStartHeight, startOutputIndex, executor.host, bridgeInfo)
100100
if err != nil {
101101
panic(err)
102102
}
@@ -207,15 +207,15 @@ func (ex *Executor) makeDANode(bridgeId int64) (executortypes.DANode, error) {
207207
return nil, fmt.Errorf("unsupported chain id for DA: %s", ophosttypes.BatchInfo_ChainType_name[int32(batchInfo.BatchInfo.ChainType)])
208208
}
209209

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

215215
if ex.cfg.BatchStartWithL2Height {
216216
batchStartHeight = l2StartHeight
217217
} else {
218218
batchStartHeight = uint64(ex.cfg.BatchStartHeight)
219219
}
220-
return l1StartHeight, l2StartHeight, batchStartHeight, err
220+
return l1StartHeight, l2StartHeight, startOutputIndex, batchStartHeight, err
221221
}

executor/host/query.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,48 @@ func (h Host) QueryBatchInfos() (*ophosttypes.QueryBatchInfosResponse, error) {
6161
return h.ophostQueryClient.BatchInfos(ctx, req)
6262
}
6363

64-
func (h Host) QueryHeightsOfOutputTxWithL2BlockNumber(l2BlockNumber uint64) (uint64, uint64, error) {
64+
func (h Host) QueryHeightsOfOutputTxWithL2BlockNumber(bridgeId int64, l2BlockNumber uint64) (uint64, uint64, uint64, error) {
6565
ctx, cancel := rpcclient.GetQueryContext(0)
6666
defer cancel()
6767

6868
query := fmt.Sprintf("%s.%s = %d AND %s.%s <= %d", ophosttypes.EventTypeProposeOutput,
6969
ophosttypes.AttributeKeyBridgeId,
70-
h.bridgeId,
70+
bridgeId,
7171
ophosttypes.EventTypeProposeOutput,
7272
ophosttypes.AttributeKeyL2BlockNumber,
7373
l2BlockNumber,
7474
)
75-
7675
perPage := 1
7776
res, err := h.node.GetRPCClient().TxSearch(ctx, query, false, nil, &perPage, "desc")
7877
if err != nil {
79-
return 0, 0, err
78+
return 0, 0, 0, err
8079
}
8180
if len(res.Txs) == 0 {
8281
// no output tx found
83-
return 0, 0, nil
82+
return 0, 0, 0, nil
8483
}
8584

8685
l2StartHeight := uint64(0)
87-
LOOP:
86+
outputIndex := uint64(0)
8887
for _, event := range res.Txs[0].TxResult.Events {
8988
if event.Type == ophosttypes.EventTypeProposeOutput {
9089
for _, attr := range event.Attributes {
9190
if attr.Key == ophosttypes.AttributeKeyL2BlockNumber {
9291
l2StartHeight, err = strconv.ParseUint(attr.Value, 10, 64)
9392
if err != nil {
94-
return 0, 0, err
93+
return 0, 0, 0, err
94+
}
95+
} else if attr.Key == ophosttypes.AttributeKeyOutputIndex {
96+
outputIndex, err = strconv.ParseUint(attr.Value, 10, 64)
97+
if err != nil {
98+
return 0, 0, 0, err
9599
}
96-
break LOOP
97100
}
98101
}
99102
}
100103
}
101-
if l2StartHeight == 0 {
102-
return 0, 0, fmt.Errorf("something wrong: l2 block number not found in the output tx")
104+
if l2StartHeight == 0 || outputIndex == 0 {
105+
return 0, 0, 0, fmt.Errorf("something wrong: l2 block number not found in the output tx")
103106
}
104-
return uint64(res.Txs[0].Height), l2StartHeight, nil
107+
return uint64(res.Txs[0].Height), l2StartHeight, outputIndex + 1, nil
105108
}

node/node.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Node struct {
3636
rawBlockHandler nodetypes.RawBlockHandlerFn
3737

3838
// status info
39+
startHeightInitialized bool
3940
lastProcessedBlockHeight uint64
4041
running bool
4142
}
@@ -96,6 +97,10 @@ func (n *Node) Initialize(startHeight uint64) error {
9697
return n.loadSyncInfo(startHeight)
9798
}
9899

100+
func (n *Node) HeightInitialized() bool {
101+
return n.startHeightInitialized
102+
}
103+
99104
func (n *Node) Start(ctx context.Context) {
100105
if n.running {
101106
return

0 commit comments

Comments
 (0)