Skip to content

Commit 94e63f5

Browse files
committed
bug fix initializing tree
1 parent 1d4832a commit 94e63f5

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

executor/child/status.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ type Status struct {
1313
LastFinalizedDepositL1Sequence uint64 `json:"last_finalized_deposit_l1_sequence"`
1414
LastWithdrawalL2Sequence uint64 `json:"last_withdrawal_l2_sequence"`
1515
WorkingTreeIndex uint64 `json:"working_tree_index"`
16-
LastOutputSubmissionTime time.Time `json:"last_output_submission_time"`
17-
NextOutputSubmissionTime time.Time `json:"next_output_submission_time"`
16+
// if it is not 0, we are syncing
17+
FinalizingBlockHeight uint64 `json:"finalizing_block_height"`
18+
LastOutputSubmissionTime time.Time `json:"last_output_submission_time"`
19+
NextOutputSubmissionTime time.Time `json:"next_output_submission_time"`
1820
}
1921

2022
func (ch Child) GetStatus() Status {
@@ -25,6 +27,7 @@ func (ch Child) GetStatus() Status {
2527
LastFinalizedDepositL1Sequence: ch.lastFinalizedDepositL1Sequence,
2628
LastWithdrawalL2Sequence: ch.mk.GetWorkingTreeLeafCount() + ch.mk.GetStartLeafIndex() - 1,
2729
WorkingTreeIndex: ch.mk.GetWorkingTreeIndex(),
30+
FinalizingBlockHeight: ch.finalizingBlockHeight,
2831
LastOutputSubmissionTime: ch.lastOutputTime,
2932
NextOutputSubmissionTime: ch.nextOutputTime,
3033
}

executor/child/withdraw.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ func (ch *Child) handleInitiateWithdrawal(l2Sequence uint64, from string, to str
8686
return nil
8787
}
8888

89-
func (ch *Child) prepareTree(blockHeight uint64) error {
90-
if ch.initializeTreeFn != nil {
91-
var err error
92-
ch.initializeTree.Do(func() {
89+
func (ch *Child) prepareTree(blockHeight uint64) (err error) {
90+
ch.initializeTree.Do(func() {
91+
if ch.initializeTreeFn != nil {
9392
err = ch.initializeTreeFn()
94-
})
93+
}
94+
})
95+
if err != nil {
9596
return err
9697
}
9798

98-
err := ch.mk.LoadWorkingTree(blockHeight - 1)
99+
err = ch.mk.LoadWorkingTree(blockHeight - 1)
99100
if err == dbtypes.ErrNotFound {
100101
// must not happened
101102
panic(fmt.Errorf("working tree not found at height: %d, current: %d", blockHeight-1, blockHeight))
@@ -116,7 +117,7 @@ func (ch *Child) prepareOutput(ctx context.Context) error {
116117
// TODO: maybe not return error here and roll back
117118
return fmt.Errorf("output does not exist at index: %d", workingOutputIndex-1)
118119
}
119-
120+
ch.lastOutputTime = output.OutputProposal.L1BlockTime
120121
ch.nextOutputTime = output.OutputProposal.L1BlockTime.Add(ch.bridgeInfo.BridgeConfig.SubmissionInterval * 2 / 3)
121122
}
122123

@@ -134,6 +135,12 @@ func (ch *Child) prepareOutput(ctx context.Context) error {
134135
}
135136

136137
func (ch *Child) handleTree(blockHeight uint64, latestHeight uint64, blockId []byte, blockHeader cmtproto.Header) (kvs []types.RawKV, storageRoot []byte, err error) {
138+
// panic if we are syncing and passed the finalizing block height
139+
// this must not happened
140+
if ch.finalizingBlockHeight != 0 && ch.finalizingBlockHeight < blockHeight {
141+
panic(fmt.Errorf("working tree not found at height: %d, current: %d", blockHeight-1, blockHeight))
142+
}
143+
137144
// finalize working tree if we are fully synced or block time is over next output time
138145
if ch.finalizingBlockHeight == blockHeight ||
139146
(ch.finalizingBlockHeight == 0 &&
@@ -166,8 +173,8 @@ func (ch *Child) handleTree(blockHeight uint64, latestHeight uint64, blockId []b
166173
}
167174

168175
ch.finalizingBlockHeight = 0
169-
ch.nextOutputTime = blockHeader.Time.Add(ch.bridgeInfo.BridgeConfig.SubmissionInterval * 2 / 3)
170176
ch.lastOutputTime = blockHeader.Time
177+
ch.nextOutputTime = blockHeader.Time.Add(ch.bridgeInfo.BridgeConfig.SubmissionInterval * 2 / 3)
171178
}
172179

173180
err = ch.mk.SaveWorkingTree(blockHeight)

0 commit comments

Comments
 (0)