Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rollup): sync finalized batches from L1 #515

Merged
merged 78 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
6ac3aca
feat(rollup): sync finalized batches from L1
colinlyguo Sep 12, 2023
d9dd48e
tweak
colinlyguo Sep 18, 2023
68414d6
address comments
colinlyguo Sep 18, 2023
fbdab78
address comments
colinlyguo Sep 18, 2023
d28757d
tweak
colinlyguo Sep 18, 2023
ba66df0
address comments
colinlyguo Sep 18, 2023
f0d4560
Update rollup/eventwatcher/eventwatcher.go
colinlyguo Sep 18, 2023
c0d74ea
address comments
colinlyguo Sep 18, 2023
7dcdbf6
address comments
colinlyguo Sep 18, 2023
50d7086
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
colinlyguo Sep 19, 2023
01e17d6
address comments
colinlyguo Sep 19, 2023
728cdc0
add s.rollupSyncService.Stop()
colinlyguo Sep 19, 2023
898f577
add block chunk batch tests
colinlyguo Sep 19, 2023
817e57e
fix goimports-lint
colinlyguo Sep 19, 2023
3760ff7
address comments
colinlyguo Sep 19, 2023
fe0adc2
address comments
colinlyguo Sep 19, 2023
0c12b6c
remove
colinlyguo Sep 19, 2023
3dd650a
add TestUnpackLog test
colinlyguo Sep 19, 2023
a1d9bfa
add decodeChunkRanges tests
colinlyguo Sep 20, 2023
74ead5a
add commit batch version check
colinlyguo Sep 20, 2023
41263b2
fix goimport & golint
colinlyguo Sep 20, 2023
9473ddb
tweak
colinlyguo Sep 20, 2023
3ea8173
fixes
colinlyguo Sep 20, 2023
cc8f0c6
add TestRollupSyncServiceStartAndStop
colinlyguo Sep 20, 2023
4ab9118
add TestGetChunkRanges
colinlyguo Sep 20, 2023
6f7b8ab
address comments
colinlyguo Sep 20, 2023
1f238e1
add TestValidateBatch
colinlyguo Sep 20, 2023
a441768
add TestCalculateFinalizedBatchMeta
colinlyguo Sep 20, 2023
65629da
address comments
colinlyguo Sep 20, 2023
c08faf3
address comments
colinlyguo Sep 20, 2023
419ae6f
fix
colinlyguo Sep 20, 2023
11e3af5
address comments
colinlyguo Sep 20, 2023
2dd75f4
address comments
colinlyguo Sep 20, 2023
2611c9f
fix
colinlyguo Sep 20, 2023
fc47a5c
tweak log
colinlyguo Sep 20, 2023
3b5f6d8
fix
colinlyguo Sep 20, 2023
61bb87b
tweak logs
colinlyguo Sep 20, 2023
32d7ec3
increase wait time
colinlyguo Sep 21, 2023
059200b
add --rollup.verify flag
colinlyguo Sep 21, 2023
5ce9e2d
add flag check in s.rollupSyncService.Stop()
colinlyguo Sep 21, 2023
8e52842
bump version
colinlyguo Sep 21, 2023
2a1ad7c
refactor
colinlyguo Sep 21, 2023
da08854
tweak configs
colinlyguo Sep 21, 2023
da57bf6
tweak
colinlyguo Sep 21, 2023
d849529
refactor getLocalInfoForBatch
colinlyguo Sep 21, 2023
fbae9a5
use syscall.Kill(os.Getpid(), syscall.SIGTERM) to shutdown
colinlyguo Sep 21, 2023
d6a2624
refactor decodeChunkBlockRanges
colinlyguo Sep 21, 2023
29b2a2a
nit
colinlyguo Sep 21, 2023
de88b8d
address comments
colinlyguo Sep 23, 2023
d5c1350
address comments
colinlyguo Sep 23, 2023
64655ca
add WithdrawRoot and StateRoot in FinalizedBatchMeta
colinlyguo Sep 23, 2023
41f07c7
address comments
colinlyguo Sep 23, 2023
3523a5e
address comments
colinlyguo Sep 23, 2023
ac7bdb3
add ctx.Err() check in retry
colinlyguo Sep 23, 2023
ade861f
add configs in gen_config.go
colinlyguo Sep 27, 2023
7d112c4
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
colinlyguo Sep 27, 2023
1865aae
bump version
colinlyguo Sep 27, 2023
b2c2ea2
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
colinlyguo Oct 9, 2023
708c02e
fix goimport & golint
colinlyguo Oct 9, 2023
e5d0780
try to fix goimport
colinlyguo Oct 9, 2023
1521634
revert change
colinlyguo Oct 9, 2023
a2903e3
fix goimport & golint
colinlyguo Oct 9, 2023
7d389ff
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
colinlyguo Oct 17, 2023
389bf3a
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
colinlyguo Oct 22, 2023
67284df
tweak
colinlyguo Oct 22, 2023
fcdcdef
bump version
colinlyguo Oct 24, 2023
f47b78f
add l2 block -> batch index mapping
colinlyguo Oct 24, 2023
70a3db7
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
0xmountaintop Oct 25, 2023
b807c73
Merge branch 'develop' into feat-sync-finalized-batches-from-L1
colinlyguo Oct 25, 2023
f141457
bump version
colinlyguo Oct 25, 2023
94b140e
add mainnet config
colinlyguo Oct 26, 2023
a626974
bump version
colinlyguo Oct 26, 2023
88eed96
address comments
colinlyguo Oct 26, 2023
17a217a
rename some vars
colinlyguo Oct 26, 2023
35f757d
feat: add more detailed logs in crash case (#547)
Thegaram Oct 26, 2023
f25c064
trigger ci
colinlyguo Oct 26, 2023
624749c
re-add batch finalization progress log
Thegaram Oct 26, 2023
a4682ba
remove block number -> batch index mapping
colinlyguo Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ var (
utils.L1ConfirmationsFlag,
utils.L1DeploymentBlockFlag,
utils.CircuitCapacityCheckEnabledFlag,
utils.RollupVerifyEnabledFlag,
}

rpcFlags = []cli.Flag{
Expand Down
13 changes: 13 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,12 @@ var (
Usage: "Enable circuit capacity check during block validation",
}

// Rollup verify service settings
RollupVerifyEnabledFlag = cli.BoolFlag{
Name: "rollup.verify",
Usage: "Enable verification of batch consistency between L1 and L2 in rollup",
}

// Max block range for `eth_getLogs` method
MaxBlockRangeFlag = cli.Int64Flag{
Name: "rpc.getlogs.maxrange",
Expand Down Expand Up @@ -1546,6 +1552,12 @@ func setCircuitCapacityCheck(ctx *cli.Context, cfg *ethconfig.Config) {
}
}

func setEnableRollupVerify(ctx *cli.Context, cfg *ethconfig.Config) {
if ctx.GlobalIsSet(RollupVerifyEnabledFlag.Name) {
cfg.EnableRollupVerify = ctx.GlobalBool(RollupVerifyEnabledFlag.Name)
}
}

func setMaxBlockRange(ctx *cli.Context, cfg *ethconfig.Config) {
if ctx.GlobalIsSet(MaxBlockRangeFlag.Name) {
cfg.MaxBlockRange = ctx.GlobalInt64(MaxBlockRangeFlag.Name)
Expand Down Expand Up @@ -1620,6 +1632,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
setWhitelist(ctx, cfg)
setLes(ctx, cfg)
setCircuitCapacityCheck(ctx, cfg)
setEnableRollupVerify(ctx, cfg)
setMaxBlockRange(ctx, cfg)

// Cap the cache allowance and tune the garbage collector
Expand Down
181 changes: 181 additions & 0 deletions core/rawdb/accessors_rollup_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package rawdb

import (
"bytes"
"math/big"

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rlp"
)

// ChunkBlockRange represents the range of blocks within a chunk.
type ChunkBlockRange struct {
StartBlockNumber uint64
EndBlockNumber uint64
}

// FinalizedBatchMeta holds metadata for finalized batches.
type FinalizedBatchMeta struct {
colinlyguo marked this conversation as resolved.
Show resolved Hide resolved
BatchHash common.Hash
TotalL1MessagePopped uint64 // total number of L1 messages popped before and in this batch.
StateRoot common.Hash
WithdrawRoot common.Hash
}

// WriteRollupEventSyncedL1BlockNumber stores the latest synced L1 block number related to rollup events in the database.
func WriteRollupEventSyncedL1BlockNumber(db ethdb.KeyValueWriter, l1BlockNumber uint64) {
value := big.NewInt(0).SetUint64(l1BlockNumber).Bytes()
if err := db.Put(rollupEventSyncedL1BlockNumberKey, value); err != nil {
log.Crit("failed to store rollup event synced L1 block number for rollup event", "err", err)
}
}

// ReadRollupEventSyncedL1BlockNumber fetches the highest synced L1 block number associated with rollup events from the database.
func ReadRollupEventSyncedL1BlockNumber(db ethdb.Reader) *uint64 {
data, err := db.Get(rollupEventSyncedL1BlockNumberKey)
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read rollup event synced L1 block number from database", "err", err)
}

number := new(big.Int).SetBytes(data)
if !number.IsUint64() {
log.Crit("unexpected rollup event synced L1 block number in database", "number", number)
}

rollupEventSyncedL1BlockNumber := number.Uint64()
return &rollupEventSyncedL1BlockNumber
}

// WriteBatchChunkRanges writes the block ranges for each chunk within a batch to the database.
// It serializes the chunk ranges using RLP and stores them under a key derived from the batch index.
func WriteBatchChunkRanges(db ethdb.KeyValueWriter, batchIndex uint64, chunkBlockRanges []*ChunkBlockRange) {
bytes, err := rlp.EncodeToBytes(chunkBlockRanges)
if err != nil {
log.Crit("failed to RLP encode batch chunk ranges", "batch index", batchIndex, "err", err)
}
if err := db.Put(batchChunkRangesKey(batchIndex), bytes); err != nil {
log.Crit("failed to store batch chunk ranges", "batch index", batchIndex, "err", err)
}
}

// DeleteBatchChunkRanges removes the block ranges of all chunks associated with a specific batch from the database.
// Note: Only non-finalized batches can be reverted.
func DeleteBatchChunkRanges(db ethdb.KeyValueWriter, batchIndex uint64) {
if err := db.Delete(batchChunkRangesKey(batchIndex)); err != nil {
log.Crit("failed to delete batch chunk ranges", "batch index", batchIndex, "err", err)
}
}

// ReadBatchChunkRanges retrieves the block ranges of all chunks associated with a specific batch from the database.
// It returns a list of ChunkBlockRange pointers, or nil if no chunk ranges are found for the given batch index.
func ReadBatchChunkRanges(db ethdb.Reader, batchIndex uint64) []*ChunkBlockRange {
data, err := db.Get(batchChunkRangesKey(batchIndex))
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read batch chunk ranges from database", "err", err)
}

cr := new([]*ChunkBlockRange)
if err := rlp.Decode(bytes.NewReader(data), cr); err != nil {
log.Crit("Invalid ChunkBlockRange RLP", "batch index", batchIndex, "data", data, "err", err)
}
return *cr
}

// WriteFinalizedBatchMeta stores the metadata of a finalized batch in the database.
func WriteFinalizedBatchMeta(db ethdb.KeyValueWriter, batchIndex uint64, finalizedBatchMeta *FinalizedBatchMeta) {
var err error
bytes, err := rlp.EncodeToBytes(finalizedBatchMeta)
if err != nil {
log.Crit("failed to RLP encode batch metadata", "batch index", batchIndex, "err", err)
}
if err := db.Put(batchMetaKey(batchIndex), bytes); err != nil {
log.Crit("failed to store batch metadata", "batch index", batchIndex, "err", err)
}
}

// ReadFinalizedBatchMeta fetches the metadata of a finalized batch from the database.
func ReadFinalizedBatchMeta(db ethdb.Reader, batchIndex uint64) *FinalizedBatchMeta {
data, err := db.Get(batchMetaKey(batchIndex))
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read finalized batch metadata from database", "err", err)
}

fbm := new(FinalizedBatchMeta)
if err := rlp.Decode(bytes.NewReader(data), fbm); err != nil {
log.Crit("Invalid FinalizedBatchMeta RLP", "batch index", batchIndex, "data", data, "err", err)
}
return fbm
}

// WriteFinalizedL2BlockNumber stores the highest finalized L2 block number in the database.
func WriteFinalizedL2BlockNumber(db ethdb.KeyValueWriter, l2BlockNumber uint64) {
value := big.NewInt(0).SetUint64(l2BlockNumber).Bytes()
if err := db.Put(finalizedL2BlockNumberKey, value); err != nil {
log.Crit("failed to store finalized L2 block number for rollup event", "err", err)
}
}

// ReadFinalizedL2BlockNumber fetches the highest finalized L2 block number from the database.
func ReadFinalizedL2BlockNumber(db ethdb.Reader) *uint64 {
data, err := db.Get(finalizedL2BlockNumberKey)
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read finalized L2 block number from database", "err", err)
}

number := new(big.Int).SetBytes(data)
if !number.IsUint64() {
log.Crit("unexpected finalized L2 block number in database", "number", number)
}

finalizedL2BlockNumber := number.Uint64()
return &finalizedL2BlockNumber
}

// WriteBlockNumberToBatchIndex stores the mapping from a block number to a batch index in the database.
func WriteBlockNumberToBatchIndex(db ethdb.KeyValueWriter, startBlockNumber uint64, endBlockNumber uint64, batchIndex uint64) {
if startBlockNumber > endBlockNumber {
log.Crit("Start block number cannot be greater than end block number", "startBlockNumber", startBlockNumber, "endBlockNumber", endBlockNumber)
return
}

value := big.NewInt(0).SetUint64(batchIndex).Bytes()
for blockNumber := startBlockNumber; blockNumber <= endBlockNumber; blockNumber++ {
if err := db.Put(blockNumberToBatchIndexKey(blockNumber), value); err != nil {
log.Crit("Failed to add block number to batch index mapping to batch", "startBlockNumber", startBlockNumber, "endBlockNumber", endBlockNumber, "batchIndex", batchIndex, "err", err)
return
}
}
}

// ReadBlockNumberToBatchIndex fetches the batch index associated with a block number from the database.
func ReadBlockNumberToBatchIndex(db ethdb.Reader, blockNumber uint64) *uint64 {
data, err := db.Get(blockNumberToBatchIndexKey(blockNumber))
if err != nil && isNotFoundErr(err) {
return nil
}
if err != nil {
log.Crit("failed to read batch index for block number from database", "blockNumber", blockNumber, "err", err)
}

number := new(big.Int).SetBytes(data)
if !number.IsUint64() {
log.Crit("unexpected batch index for block number in database", "blockNumber", blockNumber, "number", number)
}

batchIndex := number.Uint64()
return &batchIndex
}
Loading
Loading