Skip to content

feat(permissionless batches): batch production toolkit and operator recovery #1555

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

Draft
wants to merge 135 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
135 commits
Select commit Hold shift + click to select a range
dca69ce
implement first steps for minimal recovery to permissionlessly produc…
jonastheis Oct 23, 2024
2e09118
Merge remote-tracking branch 'origin/develop' into jt/permissionless-…
jonastheis Oct 24, 2024
496314f
add config for recovery mode
jonastheis Oct 24, 2024
c12d380
structure code and implement restoreMinimalPreviousState and fetchL2B…
jonastheis Oct 24, 2024
c329959
produce chunks from specified L2 blocks and batch from chunks
jonastheis Oct 24, 2024
71f240b
start implementation of restoring full previous state for relayer
jonastheis Oct 25, 2024
acc7083
implement processFinalizedBatch
jonastheis Oct 28, 2024
59ea991
handle batches
jonastheis Oct 29, 2024
2df07a9
introduce ForceL1MessageCount to config to be able to set a custom L1…
jonastheis Nov 6, 2024
0f5ebf3
add Dockerfile for relayer in permissionless batches mode
jonastheis Nov 6, 2024
f96af8e
add docker compose file to spin up all necessary services together
jonastheis Nov 6, 2024
16a471d
move docker file to build/dockerfiles
jonastheis Nov 19, 2024
d25094b
add coordinator-cron and proving-service-bundle
jonastheis Nov 19, 2024
603feed
add bundle creation
jonastheis Nov 19, 2024
5ddd6d6
add permissionless-batches/conf/ to dockerignore files
jonastheis Nov 20, 2024
5ff6fd0
refactor cmd/permissionless_batches/app
jonastheis Nov 20, 2024
0108873
implement RecoveryNeeded functionality to allow re-running without ov…
jonastheis Nov 20, 2024
a6f914a
refactor cmd/rollup_relayer/app and split into FullRecovery struct
jonastheis Nov 20, 2024
0123502
clean up
jonastheis Nov 21, 2024
596d9fe
introduce profiles to docker-compose.yml
jonastheis Nov 21, 2024
d85bdf5
initial instructions in README.md
jonastheis Nov 21, 2024
a9eac08
address review comments
jonastheis Nov 22, 2024
606162e
add dummy configuration and documentation for permissionless batch pr…
jonastheis Nov 25, 2024
30c0201
ignore /conf folder from git
jonastheis Nov 25, 2024
ec9d862
add documentation for operator recovery
jonastheis Nov 26, 2024
6ef4775
address review comments
jonastheis Nov 26, 2024
41606fe
support first version of CodecV6 in relayer and add functionality to …
jonastheis Dec 30, 2024
0c0c417
adjust Sender to support multiple blobs when sending a transaction
jonastheis Dec 31, 2024
78c9963
implement batch submission of multiple batches per transaction and co…
jonastheis Dec 31, 2024
940fde0
implement missing part of commit logic for CodecV6
jonastheis Dec 31, 2024
ca8d930
feat(rollup-relayer): add Euclid support
omerfirmak Jan 9, 2025
421afe9
deprecate halo2 provers, start openvm prover
omerfirmak Jan 27, 2025
b460d4a
Merge remote-tracking branch 'origin/omerfirmak/mpt' into feat/use-co…
jonastheis Jan 28, 2025
182f8e3
update go.mod to latest da-codec version V7
jonastheis Jan 28, 2025
783b965
update go.mod to latest da-codec version V7
jonastheis Feb 5, 2025
5a479c3
address review comments
jonastheis Feb 5, 2025
310abdd
add InitialL1MessageQueueHash and LastL1MessageQueueHash to chunk and…
jonastheis Feb 5, 2025
2efbbd7
add support for bundles with CodecV7
jonastheis Feb 5, 2025
e713424
update contextIDFromBatches prefix to v7
jonastheis Feb 5, 2025
f4e17bc
fix ci
jonastheis Feb 5, 2025
99c0a9f
update to latest da-codec
jonastheis Feb 10, 2025
8db5339
fix issues with chunks when handling CodecV7
jonastheis Feb 10, 2025
69a80d4
add testCommitBatchAndFinalizeBundleCodecV7 for relayer
jonastheis Feb 10, 2025
5f22950
feat(prover): euclid prover first draft
omerfirmak Feb 5, 2025
9412c7f
feat(coordinator): abstract proof types behind an interface
omerfirmak Feb 6, 2025
c3a3bad
feat(rollup-relayer): add Euclid support
omerfirmak Jan 9, 2025
2de45f0
feat(coordinator): add euclid proof types
omerfirmak Feb 6, 2025
f01af24
fix: potentially flaky testResubmitZeroGasPriceTransaction
omerfirmak Feb 7, 2025
2bd0655
feat: Add Euclid verifier
omerfirmak Feb 7, 2025
848d3a6
fix: re-enable supported forks check
omerfirmak Feb 7, 2025
e5ad9c6
feat: allow stopping fake finalization at fork boundary
omerfirmak Feb 13, 2025
b424cef
feat: add CPU prover dockerfile
omerfirmak Feb 13, 2025
ac17696
feat: update contracts to euclid version
omerfirmak Feb 13, 2025
d503d4a
fix: base64 encode VKs
omerfirmak Feb 17, 2025
bf08436
fix: prover using wrong resources for batch/bundle circuits
omerfirmak Feb 17, 2025
83c73f8
fix: coordinator ignore euclid transition chunk
omerfirmak Feb 17, 2025
d3acd6b
adjust to recent CodecV7 changes. remove initialL1MessageQueueIndex a…
jonastheis Feb 19, 2025
f13863e
Merge remote-tracking branch 'origin/omerfirmak/euclid-prover' into f…
jonastheis Feb 20, 2025
8b57dd6
fixes after merge
jonastheis Feb 20, 2025
37924b0
feat: bump zkvm-prover to rc4
omerfirmak Feb 20, 2025
fbc14ac
address review comments
jonastheis Feb 20, 2025
867fda6
fix: add batch tasks details for v6 codec
omerfirmak Feb 20, 2025
2e9827a
fix: properly propagate verifier errors
omerfirmak Feb 21, 2025
01fa3b3
feat: use verifier-only types in coordinator
omerfirmak Feb 21, 2025
50ebf17
fix: generate evm proofs for bundles
omerfirmak Feb 21, 2025
e1a0bab
add new contract ABI and adjust submission to it
jonastheis Feb 24, 2025
bb9d404
fix: force byte arrays to be marshaled as JSON arrays
omerfirmak Feb 24, 2025
0125dd6
add new contract ABI
jonastheis Feb 27, 2025
121ce09
update config and adjust to new contract ABI
jonastheis Feb 27, 2025
ed394a6
make sure that all batches committed in the same tx are part of the s…
jonastheis Feb 27, 2025
a55de1f
feat: set code tx support (#1600)
colinlyguo Feb 27, 2025
89ede0d
build rollup images with --platform=linux/amd64
jonastheis Feb 27, 2025
782e019
Merge branch 'feat/use-codec-v6' of github.com:scroll-tech/scroll int…
jonastheis Feb 27, 2025
081d289
update da-codec dependency
jonastheis Feb 27, 2025
47a6c23
fix bug where chunk and batch blocks mismatch
jonastheis Feb 28, 2025
eb5758b
feat: bump to zkvm-prover rc5
roynalnaruto Feb 28, 2025
7353f30
update l2geth version in go.mod
jonastheis Feb 28, 2025
673777f
use go 1.22 in Dockerfile builder
jonastheis Feb 28, 2025
554a233
refactor: move euclid prover to new subdir
omerfirmak Mar 4, 2025
e27ab5a
Merge remote-tracking branch 'origin/develop' into omerfirmak/euclid-…
omerfirmak Mar 4, 2025
26a49cb
Merge remote-tracking branch 'origin/omerfirmak/euclid-prover' into f…
jonastheis Mar 6, 2025
8ea4315
update go.work.sum
jonastheis Mar 6, 2025
55b32e1
add debug log message
jonastheis Mar 6, 2025
d6674e8
add configuration parameter maxChunksPerBatch for batch proposer
jonastheis Mar 6, 2025
a1c4562
add configuration parameter maxChunksPerBatch for batch proposer
jonastheis Mar 6, 2025
c8b614f
unit tests fix
colinlyguo Mar 6, 2025
f91c999
fix batch proposer panic
Thegaram Mar 6, 2025
82dd5e0
feat(bridge-history): support codecv7
colinlyguo Mar 6, 2025
4333d51
Revert "feat(bridge-history): support codecv7"
colinlyguo Mar 6, 2025
b30f4d0
chore(zkvm-circuit): upgrade to 0.1.0-rc.6 (#1610)
colinlyguo Mar 7, 2025
03c63a6
update batch/chunk details for phase2
omerfirmak Mar 7, 2025
9f4c9ee
fix: prover version (#1611)
yiweichi Mar 7, 2025
101cc46
update dependencies
colinlyguo Mar 7, 2025
a868bc1
simplify version checks
colinlyguo Mar 7, 2025
8b08a57
Revert "simplify version checks"
colinlyguo Mar 7, 2025
f1ea4b3
process task with CodecV7, add check for BlobDataProof length and add…
jonastheis Mar 8, 2025
b7e7d1a
Merge remote-tracking branch 'origin/omerfirmak/euclid-prover' into f…
jonastheis Mar 8, 2025
94bee19
feat(bridge-history): support codecv7 (#1609)
colinlyguo Mar 8, 2025
f27ddb7
remove debug line
jonastheis Mar 8, 2025
da96331
tweak comments and some renamings
colinlyguo Mar 9, 2025
d79aaef
fix CI
jonastheis Mar 10, 2025
ab7038c
Merge branch 'feat/use-codec-v6' of github.com:scroll-tech/scroll int…
jonastheis Mar 10, 2025
47219f2
run goimports
jonastheis Mar 10, 2025
95adcc3
fix tests
jonastheis Mar 10, 2025
224546e
add new ABI and add more tests
jonastheis Mar 10, 2025
0799dd4
fix linter
jonastheis Mar 10, 2025
7a70e37
fix test
jonastheis Mar 10, 2025
57d50b7
Merge branch 'develop' into omerfirmak/euclid-prover
omerfirmak Mar 10, 2025
20dffe4
chore: auto version bump [bot]
omerfirmak Mar 10, 2025
867307d
Merge remote-tracking branch 'origin/omerfirmak/euclid-prover' into f…
jonastheis Mar 10, 2025
c9f6e8c
Merge remote-tracking branch 'origin/develop' into feat/use-codec-v6
jonastheis Mar 10, 2025
9b462e4
go mod tidy
jonastheis Mar 10, 2025
d57e6b0
chore: auto version bump [bot]
jonastheis Mar 10, 2025
4d677b3
address review comments
jonastheis Mar 10, 2025
90d1563
Merge remote-tracking branch 'origin/develop' into feat/use-codec-v6
jonastheis Mar 10, 2025
d987931
go mod tidy
jonastheis Mar 10, 2025
38b3239
chore: auto version bump [bot]
jonastheis Mar 10, 2025
e99a851
Merge branch 'feat/use-codec-v6' of github.com:scroll-tech/scroll int…
jonastheis Mar 10, 2025
bb6ee2c
Merge branch 'develop' into feat/use-codec-v6
jonastheis Mar 10, 2025
2d84478
Merge remote-tracking branch 'origin/feat/use-codec-v6' into jt/permi…
jonastheis Mar 10, 2025
a8511d5
adjust minimal recovery to changes from CodecV7
jonastheis Mar 11, 2025
a16b906
add submission of permissionless batch
jonastheis Mar 12, 2025
942f883
Merge remote-tracking branch 'origin/develop' into jt/permissionless-…
jonastheis Mar 12, 2025
495921c
Merge remote-tracking branch 'origin/develop' into jt/permissionless-…
jonastheis Mar 24, 2025
6414767
update da-codec to latest version
jonastheis Mar 24, 2025
596fabf
update l2geth to latest version
jonastheis Mar 24, 2025
81df41c
add better error handling and wait for transaction confirmation
jonastheis Mar 26, 2025
c2152b1
fix issue with replaced l2geth dependency
jonastheis Mar 27, 2025
8009bd1
Merge branch 'develop' into jt/permissionless-batches-recovery
yiweichi Apr 11, 2025
9d54bc2
Merge branch 'develop' into jt/permissionless-batches-recovery
yiweichi Apr 24, 2025
ac3305c
Merge branch 'develop' into jt/permissionless-batches-recovery
yiweichi Apr 29, 2025
9d09283
morty/permissionless batches recovery (#1639)
yiweichi May 28, 2025
d3d5f1f
Merge branch 'develop' into jt/permissionless-batches-recovery
jonastheis Jun 2, 2025
22a6a6e
Merge remote-tracking branch 'origin/develop' into jt/permissionless-…
jonastheis Jun 5, 2025
867c101
implement decoding of codecv7 batches and insertion of chunks, batche…
jonastheis Jun 5, 2025
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
335 changes: 335 additions & 0 deletions rollup/cmd/permissionless_batches/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
package app

import (
"context"
"fmt"
"os"
"os/signal"

"github.com/prometheus/client_golang/prometheus"
"github.com/scroll-tech/da-codec/encoding"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core"
"github.com/scroll-tech/go-ethereum/ethclient"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rollup/l1"
"github.com/urfave/cli/v2"
"gorm.io/gorm"

"scroll-tech/common/database"
"scroll-tech/common/observability"
"scroll-tech/common/utils"
"scroll-tech/common/version"
"scroll-tech/database/migrate"
"scroll-tech/rollup/internal/config"
"scroll-tech/rollup/internal/controller/watcher"
"scroll-tech/rollup/internal/orm"
)

var app *cli.App

func init() {
// Set up rollup-relayer app info.
app = cli.NewApp()
app.Action = action
app.Name = "permissionless-batches"
app.Usage = "The Scroll Rollup Relayer for permissionless batch production"
app.Version = version.Version
app.Flags = append(app.Flags, utils.CommonFlags...)
app.Flags = append(app.Flags, utils.RollupRelayerFlags...)
app.Commands = []*cli.Command{}
app.Before = func(ctx *cli.Context) error {
return utils.LogSetup(ctx)
}
}

func action(ctx *cli.Context) error {
// Load config file.
cfgFile := ctx.String(utils.ConfigFileFlag.Name)
cfg, err := config.NewConfig(cfgFile)
if err != nil {
log.Crit("failed to load config file", "config file", cfgFile, "error", err)
}

subCtx, cancel := context.WithCancel(ctx.Context)
defer cancel()

db, err := initDB(cfg)
if err != nil {
return fmt.Errorf("failed to init db: %w", err)
}
defer func() {
if err = database.CloseDB(db); err != nil {
log.Crit("failed to close db connection", "error", err)
}
}()

registry := prometheus.DefaultRegisterer
observability.Server(ctx, db)

genesisPath := ctx.String(utils.Genesis.Name)
genesis, err := utils.ReadGenesis(genesisPath)
if err != nil {
log.Crit("failed to read genesis", "genesis file", genesisPath, "error", err)
}

chunkProposer := watcher.NewChunkProposer(subCtx, cfg.L2Config.ChunkProposerConfig, genesis.Config, db, registry)
batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, genesis.Config, db, registry)
//bundleProposer := watcher.NewBundleProposer(subCtx, cfg.L2Config.BundleProposerConfig, genesis.Config, db, registry)

fmt.Println(cfg.L1Config)
fmt.Println(cfg.L2Config)
fmt.Println(cfg.DBConfig)
fmt.Println(cfg.RecoveryConfig)

// Restore minimal previous state required to be able to create new chunks, batches and bundles.
latestFinalizedChunk, latestFinalizedBatch, err := restoreMinimalPreviousState(cfg, chunkProposer, batchProposer)
if err != nil {
return fmt.Errorf("failed to restore minimal previous state: %w", err)
}

// Fetch and insert the missing blocks from the last block in the latestFinalizedBatch to the latest L2 block.
fromBlock := latestFinalizedChunk.EndBlockNumber + 1
toBlock, err := fetchL2Blocks(subCtx, cfg, genesis, db, registry, fromBlock, cfg.RecoveryConfig.L2BlockHeightLimit)
if err != nil {
return fmt.Errorf("failed to fetch L2 blocks: %w", err)
}

fmt.Println(latestFinalizedChunk.Index, latestFinalizedBatch.Index, fromBlock, toBlock)

// Create chunks for L2 blocks.
log.Info("Creating chunks for L2 blocks", "from", fromBlock, "to", toBlock)

var latestChunk *orm.Chunk
var count int
for {
if err = chunkProposer.ProposeChunk(); err != nil {
return fmt.Errorf("failed to propose chunk: %w", err)
}
count++

latestChunk, err = chunkProposer.ChunkORM().GetLatestChunk(subCtx)
if err != nil {
return fmt.Errorf("failed to get latest latestFinalizedChunk: %w", err)
}

log.Info("Chunk created", "index", latestChunk.Index, "hash", latestChunk.Hash, "StartBlockNumber", latestChunk.StartBlockNumber, "EndBlockNumber", latestChunk.EndBlockNumber, "TotalL1MessagesPoppedBefore", latestChunk.TotalL1MessagesPoppedBefore)

// We have created chunks for all available L2 blocks.
if latestChunk.EndBlockNumber >= toBlock {
break
}
}

log.Info("Chunks created", "count", count, "latest latestFinalizedChunk", latestChunk.Index, "hash", latestChunk.Hash, "StartBlockNumber", latestChunk.StartBlockNumber, "EndBlockNumber", latestChunk.EndBlockNumber, "TotalL1MessagesPoppedBefore", latestChunk.TotalL1MessagesPoppedBefore)

// Create batch for the created chunks. We only allow 1 batch it needs to be submitted (and finalized) with a proof in a single step.
log.Info("Creating batch for chunks", "from", latestFinalizedChunk.Index+1, "to", latestChunk.Index)

batchProposer.TryProposeBatch()
latestBatch, err := batchProposer.BatchORM().GetLatestBatch(subCtx)
if err != nil {
return fmt.Errorf("failed to get latest latestFinalizedBatch: %w", err)
}

if latestBatch.EndChunkIndex != latestChunk.Index {
return fmt.Errorf("latest chunk in produced batch %d != %d, too many L2 blocks - specify less L2 blocks and retry again", latestBatch.EndChunkIndex, latestChunk.Index)
}

log.Info("Batch created", "index", latestBatch.Index, "hash", latestBatch.Hash, "StartChunkIndex", latestBatch.StartChunkIndex, "EndChunkIndex", latestBatch.EndChunkIndex)

// Catch CTRL-C to ensure a graceful shutdown.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

// Wait until the interrupt signal is received from an OS signal.
<-interrupt

return nil
}

// Run rollup relayer cmd instance.
func Run() {
if err := app.Run(os.Args); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func initDB(cfg *config.Config) (*gorm.DB, error) {
// init db connection
db, err := database.InitDB(cfg.DBConfig)
if err != nil {
log.Crit("failed to init db connection", "err", err)
}

// make sure we are starting from a fresh DB
sqlDB, err := db.DB()
if err != nil {
return nil, fmt.Errorf("failed ")
}

// reset and init DB
var v int64
err = migrate.Rollback(sqlDB, &v)
if err != nil {
return nil, fmt.Errorf("failed to rollback db: %w", err)
}

err = migrate.Migrate(sqlDB)
if err != nil {
return nil, fmt.Errorf("failed to migrate db: %w", err)
}

return db, nil
}

func fetchL2Blocks(ctx context.Context, cfg *config.Config, genesis *core.Genesis, db *gorm.DB, registry prometheus.Registerer, fromBlock uint64, l2BlockHeightLimit uint64) (uint64, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that move these logics to controller layer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5ff6fd0

if l2BlockHeightLimit > 0 && fromBlock > l2BlockHeightLimit {
return 0, fmt.Errorf("fromBlock (latest finalized L2 block) is higher than specified L2BlockHeightLimit: %d > %d", fromBlock, l2BlockHeightLimit)
}

log.Info("Fetching L2 blocks with", "fromBlock", fromBlock, "l2BlockHeightLimit", l2BlockHeightLimit)

// Init l2geth connection
l2client, err := ethclient.Dial(cfg.L2Config.Endpoint)
if err != nil {
return 0, fmt.Errorf("failed to connect to L2geth at RPC=%s: %w", cfg.L2Config.Endpoint, err)
}

l2Watcher := watcher.NewL2WatcherClient(ctx, l2client, cfg.L2Config.Confirmations, cfg.L2Config.L2MessageQueueAddress, cfg.L2Config.WithdrawTrieRootSlot, genesis.Config, db, registry)

// Fetch and insert the missing blocks from the last block in the batch to the latest L2 block.
latestL2Block, err := l2Watcher.Client.BlockNumber(context.Background())
if err != nil {
return 0, fmt.Errorf("failed to get latest L2 block number: %w", err)
}

log.Info("Latest L2 block number", "latest L2 block", latestL2Block)

if l2BlockHeightLimit > latestL2Block {
return 0, fmt.Errorf("l2BlockHeightLimit is higher than the latest L2 block number, not all blocks are available in L2geth: %d > %d", l2BlockHeightLimit, latestL2Block)
}

toBlock := latestL2Block
if l2BlockHeightLimit > 0 {
toBlock = l2BlockHeightLimit
}

err = l2Watcher.GetAndStoreBlocks(context.Background(), fromBlock, toBlock)
if err != nil {
return 0, fmt.Errorf("failed to get and store blocks: %w", err)
}

log.Info("Fetched L2 blocks from", "fromBlock", fromBlock, "toBlock", toBlock)

return toBlock, nil
}

// restoreMinimalPreviousState restores the minimal previous state required to be able to create new chunks, batches and bundles.
func restoreMinimalPreviousState(cfg *config.Config, chunkProposer *watcher.ChunkProposer, batchProposer *watcher.BatchProposer) (*orm.Chunk, *orm.Batch, error) {
log.Info("Restoring previous state with", "L1 block height", cfg.RecoveryConfig.L1BlockHeight, "latest finalized batch", cfg.RecoveryConfig.LatestFinalizedBatch)

// TODO: make these parameters -> part of genesis config?
scrollChainAddress := common.HexToAddress("0x2D567EcE699Eabe5afCd141eDB7A4f2D0D6ce8a0")
l1MessageQueueAddress := common.HexToAddress("0xF0B2293F5D834eAe920c6974D50957A1732de763")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read from config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5ff6fd0


l1Client, err := ethclient.Dial(cfg.L1Config.Endpoint)
if err != nil {
return nil, nil, fmt.Errorf("failed to connect to L1 client: %w", err)
}
reader, err := l1.NewReader(context.Background(), l1.Config{
ScrollChainAddress: scrollChainAddress,
L1MessageQueueAddress: l1MessageQueueAddress,
}, l1Client)
if err != nil {
return nil, nil, fmt.Errorf("failed to create L1 reader: %w", err)
}

// 1. Sanity check user input: Make sure that the user's L1 block height is not higher than the latest finalized block number.
latestFinalizedL1Block, err := reader.GetLatestFinalizedBlockNumber()
if err != nil {
return nil, nil, fmt.Errorf("failed to get latest finalized L1 block number: %w", err)
}
if cfg.RecoveryConfig.L1BlockHeight > latestFinalizedL1Block {
return nil, nil, fmt.Errorf("specified L1 block height is higher than the latest finalized block number: %d > %d", cfg.RecoveryConfig.L1BlockHeight, latestFinalizedL1Block)
}

log.Info("Latest finalized L1 block number", "latest finalized L1 block", latestFinalizedL1Block)

// 2. Make sure that the specified batch is indeed finalized on the L1 rollup contract and is the latest finalized batch.
// TODO: enable check
//latestFinalizedBatch, err := reader.LatestFinalizedBatch(latestFinalizedL1Block)
//if cfg.RecoveryConfig.LatestFinalizedBatch != latestFinalizedBatch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious about why setting cfg.RecoveryConfig.LatestFinalizedBatch instead of using reader.LatestFinalizedBatch as the start point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, can probably remove it and make the configuration a bit easier. initially, I wanted the user to specify L1 block and the latest finalized batch so that the user knows where the (minimal) recovery process is starting from and there's no "magic" happening (e.g. if there's another batch committed in the meantime).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced another config parameter for testing purposes to override this check force_latest_finalized_batch. The user now still needs to specify the L1 block height and latest finalized batch to not have any surprises/magic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.

// return nil, nil, fmt.Errorf("batch %d is not the latest finalized batch: %d", cfg.RecoveryConfig.LatestFinalizedBatch, latestFinalizedBatch)
//}

var batchCommitEvent *l1.CommitBatchEvent
err = reader.FetchRollupEventsInRangeWithCallback(cfg.RecoveryConfig.L1BlockHeight, latestFinalizedL1Block, func(event l1.RollupEvent) bool {
if event.Type() == l1.CommitEventType && event.BatchIndex().Uint64() == cfg.RecoveryConfig.LatestFinalizedBatch {
batchCommitEvent = event.(*l1.CommitBatchEvent)
return false
}

return true
})
if batchCommitEvent == nil {
return nil, nil, fmt.Errorf("commit event not found for batch %d", cfg.RecoveryConfig.LatestFinalizedBatch)
}

log.Info("Found commit event for batch", "batch", batchCommitEvent.BatchIndex(), "hash", batchCommitEvent.BatchHash(), "L1 block height", batchCommitEvent.BlockNumber(), "L1 tx hash", batchCommitEvent.TxHash())

// 3. Fetch commit tx data for latest finalized batch.
args, err := reader.FetchCommitTxData(batchCommitEvent)
if err != nil {
return nil, nil, fmt.Errorf("failed to fetch commit tx data: %w", err)
}

codec, err := encoding.CodecFromVersion(encoding.CodecVersion(args.Version))
if err != nil {
return nil, nil, fmt.Errorf("failed to get codec: %w", err)
}

daChunksRawTxs, err := codec.DecodeDAChunksRawTx(args.Chunks)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode DA chunks: %w", err)
}
lastChunk := daChunksRawTxs[len(daChunksRawTxs)-1]
lastBlockInBatch := lastChunk.Blocks[len(lastChunk.Blocks)-1].Number()

log.Info("Last L2 block in batch", "batch", batchCommitEvent.BatchIndex(), "L2 block", lastBlockInBatch)

// 4. Get the L1 messages count after the latest finalized batch.
l1MessagesCount, err := reader.FinalizedL1MessageQueueIndex(latestFinalizedL1Block)
if err != nil {
return nil, nil, fmt.Errorf("failed to get L1 messages count: %w", err)
}
// TODO: remove this. only for testing
l1MessagesCount = 220853

log.Info("L1 messages count after latest finalized batch", "batch", batchCommitEvent.BatchIndex(), "count", l1MessagesCount)

// 5. Insert minimal state to DB.
chunk, err := chunkProposer.ChunkORM().InsertChunkRaw(context.Background(), codec.Version(), lastChunk, l1MessagesCount)
if err != nil {
return nil, nil, fmt.Errorf("failed to insert chunk raw: %w", err)
}

log.Info("Inserted last finalized chunk to DB", "chunk", chunk.Index, "hash", chunk.Hash, "StartBlockNumber", chunk.StartBlockNumber, "EndBlockNumber", chunk.EndBlockNumber, "TotalL1MessagesPoppedBefore", chunk.TotalL1MessagesPoppedBefore)

batch, err := batchProposer.BatchORM().InsertBatchRaw(context.Background(), batchCommitEvent.BatchIndex(), batchCommitEvent.BatchHash(), codec.Version(), chunk)
if err != nil {
return nil, nil, fmt.Errorf("failed to insert batch raw: %w", err)
}

log.Info("Inserted last finalized batch to DB", "batch", batch.Index, "hash", batch.Hash)

return chunk, batch, nil
}

//docker run --rm -it \
// -e POSTGRES_HOST_AUTH_METHOD=trust \
// -e POSTGRES_DB=scroll \
// -v ${PWD}/db_data:/var/lib/postgresql/data \
// -p 5432:5432 \
// postgres
7 changes: 7 additions & 0 deletions rollup/cmd/permissionless_batches/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "scroll-tech/rollup/cmd/permissionless_batches/app"

func main() {
app.Run()
}
Loading