Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eaf06e3

Browse files
jonastheisyiweichi
andauthoredApr 28, 2025··
feat(L1Reader): change to NextUnfinalizedL1MessageQueueIndex and add GetFinalizedStateRootByBatchIndex (#1160)
* change FinalizedL1MessageQueueIndex to NextUnfinalizedL1MessageQueueIndex * feat(L1Reader): add GetFinalizedStateRootByBatchIndex (#1165) * feat(L1Reader): add GetFinalizedStateRootByBatchIndex * fix: FinalizedL1MessageQueueIndex return type * fix: get state root batchIndex type --------- Co-authored-by: Morty <[email protected]>
1 parent 5e956fe commit eaf06e3

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed
 

‎rollup/l1/reader.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
finalizeBatchEventName = "FinalizeBatch"
2424
nextUnfinalizedQueueIndex = "nextUnfinalizedQueueIndex"
2525
lastFinalizedBatchIndex = "lastFinalizedBatchIndex"
26+
finalizedStateRoots = "finalizedStateRoots"
2627

2728
defaultRollupEventsFetchBlockRange = 100
2829
)
@@ -72,7 +73,7 @@ func NewReader(ctx context.Context, config Config, l1Client Client) (*Reader, er
7273
return &reader, nil
7374
}
7475

75-
func (r *Reader) FinalizedL1MessageQueueIndex(blockNumber uint64) (uint64, error) {
76+
func (r *Reader) NextUnfinalizedL1MessageQueueIndex(blockNumber uint64) (uint64, error) {
7677
data, err := r.l1MessageQueueABI.Pack(nextUnfinalizedQueueIndex)
7778
if err != nil {
7879
return 0, fmt.Errorf("failed to pack %s: %w", nextUnfinalizedQueueIndex, err)
@@ -92,11 +93,7 @@ func (r *Reader) FinalizedL1MessageQueueIndex(blockNumber uint64) (uint64, error
9293
}
9394

9495
next := parsedResult.Uint64()
95-
if next == 0 {
96-
return 0, nil
97-
}
98-
99-
return next - 1, nil
96+
return next, nil
10097
}
10198

10299
func (r *Reader) LatestFinalizedBatchIndex(blockNumber uint64) (uint64, error) {
@@ -121,6 +118,28 @@ func (r *Reader) LatestFinalizedBatchIndex(blockNumber uint64) (uint64, error) {
121118
return parsedResult.Uint64(), nil
122119
}
123120

121+
func (r *Reader) GetFinalizedStateRootByBatchIndex(blockNumber uint64, batchIndex uint64) (common.Hash, error) {
122+
data, err := r.scrollChainABI.Pack(finalizedStateRoots, big.NewInt(int64(batchIndex)))
123+
if err != nil {
124+
return common.Hash{}, fmt.Errorf("failed to pack %s: %w", finalizedStateRoots, err)
125+
}
126+
127+
result, err := r.client.CallContract(r.ctx, ethereum.CallMsg{
128+
To: &r.config.ScrollChainAddress,
129+
Data: data,
130+
}, new(big.Int).SetUint64(blockNumber))
131+
if err != nil {
132+
return common.Hash{}, fmt.Errorf("failed to call %s: %w", finalizedStateRoots, err)
133+
}
134+
135+
var parsedResult common.Hash
136+
if err = r.scrollChainABI.UnpackIntoInterface(&parsedResult, finalizedStateRoots, result); err != nil {
137+
return common.Hash{}, fmt.Errorf("failed to unpack result: %w", err)
138+
}
139+
140+
return parsedResult, nil
141+
}
142+
124143
// GetLatestFinalizedBlockNumber fetches the block number of the latest finalized block from the L1 chain.
125144
func (r *Reader) GetLatestFinalizedBlockNumber() (uint64, error) {
126145
header, err := r.client.HeaderByNumber(r.ctx, big.NewInt(int64(rpc.FinalizedBlockNumber)))

0 commit comments

Comments
 (0)
Please sign in to comment.