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

Create a local devnet for zkevm #34

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
122 changes: 122 additions & 0 deletions core/allocs/hermez-dev.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,16 @@ func HermezCardonaInternalGenesisBlock() *types.Genesis {
}
}

func HermezLocalDevnetGenesisBlock() *types.Genesis {
return &types.Genesis{
Config: params.HermezLocalDevnetChainConfig,
Timestamp: 1676996964,
GasLimit: 0x0,
Difficulty: big.NewInt(0x0),
Alloc: readPrealloc("allocs/hermez-dev.json"),
}
}

// Pre-calculated version of:
//
// DevnetSignPrivateKey = crypto.HexToECDSA(sha256.Sum256([]byte("erigon devnet key")))
Expand Down Expand Up @@ -736,6 +746,8 @@ func GenesisBlockByChainName(chain string) *types.Genesis {
return ChiadoGenesisBlock()
case networkname.HermezMainnetChainName:
return HermezMainnetGenesisBlock()
case networkname.HermezLocalDevnetChainName:
return HermezLocalDevnetGenesisBlock()
case networkname.HermezTestnetChainName:
return HermezTestnetGenesisBlock()
case networkname.HermezBlueberryChainName:
Expand Down
96 changes: 75 additions & 21 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,30 +696,63 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
}

if backend.config.Zk != nil {
cfg := backend.config.Zk

// datastream
// Create client
log.Info("Starting datastream client...")
datastreamClient := client.NewClient(cfg.L2DataStreamerUrl)
// retry connection
for {
// Start client (connect to the server)
if err := datastreamClient.Start(); err == nil {
break
}
log.Warn(fmt.Sprintf("Error when starting datastream client, retrying... Error: %s", err))
}
//datastream end

etherMan := newEtherMan(cfg)
zkL1Syncer := syncer.NewL1Syncer(etherMan.EthClient, cfg.L1ContractAddress, cfg.L1BlockRange, cfg.L1QueryDelay)

backend.syncStages = stages2.NewDefaultZkStages(backend.sentryCtx, backend.chainDB, stack.Config().P2P, config, backend.sentriesClient, backend.notifications, backend.downloaderClient, allSnapshots, backend.agg, backend.forkValidator, backend.engine, zkL1Syncer, &datastreamClient)
// TODO: SEQ: move this check into stages2 or something like that, bad place to have it..
// entering ZK territory!
if sequencer.IsSequencer() {
// if we are sequencing transactions, we do the sequencing loop...

backend.syncStages = stages2.NewSequencerZkStages(
backend.sentryCtx,
backend.chainDB,
config,
backend.sentriesClient,
backend.notifications,
backend.downloaderClient,
allSnapshots,
backend.agg,
backend.forkValidator,
backend.engine,
)

backend.syncUnwindOrder = zkStages.ZkSequencerUnwindOrder

} else {
/*
if we are syncing from for the RPC, we do the normal ZK sync loop

ZZZZZZZ K K RRRRR PPPPP CCCC
Z K K R R P P C
Z KKK RRRR PPPP C
Z K K R R P C
ZZZZZZZ K K R R P CCCC

*/

cfg := backend.config.Zk
datastreamClient := initDataStreamClient(cfg)

etherMan := newEtherMan(cfg)
zkL1Syncer := syncer.NewL1Syncer(
etherMan.EthClient,
cfg.L1ContractAddress,
cfg.L1BlockRange,
cfg.L1QueryDelay,
)

backend.syncStages = stages2.NewDefaultZkStages(
backend.sentryCtx,
backend.chainDB,
config,
backend.sentriesClient,
backend.notifications,
backend.downloaderClient,
allSnapshots,
backend.agg,
backend.forkValidator,
backend.engine,
zkL1Syncer,
datastreamClient,
)

backend.syncUnwindOrder = zkStages.ZkUnwindOrder
}
// TODO: SEQ: prune order
Expand Down Expand Up @@ -750,6 +783,27 @@ func newEtherMan(cfg *ethconfig.Zk) *etherman.Client {
return em
}

// creates a datastream client with default parameters
func initDataStreamClient(cfg *ethconfig.Zk) *client.StreamClient {
// datastream
// Create client
log.Info("Starting datastream client...")
// retry connection
datastreamClient := client.NewClient(cfg.L2DataStreamerUrl)

for i := 0; i < 30; i++ {
// Start client (connect to the server)
if err := datastreamClient.Start(); err != nil {
log.Warn(fmt.Sprintf("Error when starting datastream client, retrying... Error: %s", err))
time.Sleep(1 * time.Second)
} else {
log.Info("Datastream client initialized...")
return datastreamClient
}
}
panic("datastream client could not be initialized")
}

func (backend *Ethereum) Init(stack *node.Node, config *ethconfig.Config) error {
ethBackendRPC, miningRPC, stateDiffClient := backend.ethBackendRPC, backend.miningRPC, backend.stateChangesClient
blockReader := backend.blockReader
Expand Down
2 changes: 1 addition & 1 deletion params/chainspecs/hermez-blueberry.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ChainName": "devnet",
"ChainName": "blueberry",
"chainId": 1440,
"consensus": "ethash",
"homesteadBlock": 0,
Expand Down
25 changes: 25 additions & 0 deletions params/chainspecs/hermez-dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"ChainName": "hermez-dev",
"chainId": 999999,
"consensus": "ethash",
"homesteadBlock": 0,
"daoForkBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 9999999999999999999999999999999999999999999999999,
"arrowGlacierBlock": 9999999999999999999999999999999999999999999999999,
"grayGlacierBlock": 9999999999999999999999999999999999999999999999999,
"terminalTotalDifficulty": 58750000000000000000000,
"terminalTotalDifficultyPassed": false,
"shanghaiTime": 9999999999999999999999999999999999999999999999999,
"cancunTime": 9999999999999999999999999999999999999999999999999,
"pragueTime": 9999999999999999999999999999999999999999999999999,
"ethash": {},
"mordorBlock":0
}
9 changes: 9 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var (
GnosisGenesisHash = libcommon.HexToHash("0x4f1dd23188aab3a76b463e4af801b52b1248ef073c648cbdc4c9333d3da79756")
ChiadoGenesisHash = libcommon.HexToHash("0xada44fd8d2ecab8b08f256af07ad3e777f17fb434f8f8e678b312f576212ba9a")
HermezMainnetGenesisHash = libcommon.HexToHash("0x3f86b09b43e3e49a41fc20a07579b79eba044253367817d5c241d23c0e2bc5c9")
HermezLocalDevnetGenesisHash = libcommon.HexToHash("0x65531c46ddeb39ba78d50f4eb6b87a279fa28bb6f683b327779392a4cd4770f3")
HermezTestnetGenesisHash = libcommon.HexToHash("0x13a14c4a8288e782863d7ce916d224546c69dc428fbfa7115a0cc33a27a05b26")
HermezBlueberryGenesisHash = libcommon.HexToHash("0x45cff376044a08f8f33f0244d589f2178ea4f4c5234d912daa2ff3c496b17862")
HermezCardonaGenesisHash = libcommon.HexToHash("0xf1a89745a08cfd1af739b93f960ad89546e3cec2a7babe8607703eb1e4e94b4d")
Expand Down Expand Up @@ -139,6 +140,8 @@ var (

HermezMainnetChainConfig = readChainSpec("chainspecs/hermez.json")

HermezLocalDevnetChainConfig = readChainSpec("chainspecs/hermez-dev.json")

HermezTestnetChainConfig = readChainSpec("chainspecs/hermez-testnet.json")

HermezBlueberryChainConfig = readChainSpec("chainspecs/hermez-blueberry.json")
Expand Down Expand Up @@ -229,6 +232,8 @@ func ChainConfigByChainName(chain string) *chain.Config {
return ChiadoChainConfig
case networkname.HermezMainnetChainName:
return HermezMainnetChainConfig
case networkname.HermezLocalDevnetChainName:
return HermezLocalDevnetChainConfig
case networkname.HermezTestnetChainName:
return HermezTestnetChainConfig
case networkname.HermezBlueberryChainName:
Expand Down Expand Up @@ -264,6 +269,8 @@ func GenesisHashByChainName(chain string) *libcommon.Hash {
return &ChiadoGenesisHash
case networkname.HermezMainnetChainName:
return &HermezMainnetGenesisHash
case networkname.HermezLocalDevnetChainName:
return &HermezLocalDevnetGenesisHash
case networkname.HermezTestnetChainName:
return &HermezTestnetGenesisHash
case networkname.HermezBlueberryChainName:
Expand Down Expand Up @@ -299,6 +306,8 @@ func ChainConfigByGenesisHash(genesisHash libcommon.Hash) *chain.Config {
return ChiadoChainConfig
case genesisHash == HermezMainnetGenesisHash:
return HermezMainnetChainConfig
case genesisHash == HermezLocalDevnetGenesisHash:
return HermezLocalDevnetChainConfig
case genesisHash == HermezTestnetGenesisHash:
return HermezTestnetChainConfig
case genesisHash == HermezBlueberryGenesisHash:
Expand Down
2 changes: 2 additions & 0 deletions params/networkname/network_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
GnosisChainName = "gnosis"
ChiadoChainName = "chiado"
HermezMainnetChainName = "hermez-mainnet"
HermezLocalDevnetChainName = "hermez-dev"
HermezTestnetChainName = "hermez-testnet"
HermezBlueberryChainName = "hermez-blueberry"
HermezCardonaChainName = "hermez-cardona"
Expand All @@ -29,6 +30,7 @@ var All = []string{
GnosisChainName,
ChiadoChainName,
HermezMainnetChainName,
HermezLocalDevnetChainName,
HermezTestnetChainName,
HermezBlueberryChainName,
HermezCardonaChainName,
Expand Down
7 changes: 5 additions & 2 deletions turbo/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/node/nodecfg"
"github.com/ledgerwatch/erigon/zk/sequencer"
)

var (
Expand Down Expand Up @@ -310,8 +311,10 @@ func ApplyFlagsForZkConfig(ctx *cli.Context, cfg *ethconfig.Config) {
}

checkFlag(utils.L2ChainIdFlag.Name, cfg.Zk.L2ChainId)
checkFlag(utils.L2RpcUrlFlag.Name, cfg.Zk.L2RpcUrl)
checkFlag(utils.L2DataStreamerUrlFlag.Name, cfg.Zk.L2DataStreamerUrl)
if !sequencer.IsSequencer() {
checkFlag(utils.L2RpcUrlFlag.Name, cfg.Zk.L2RpcUrl)
checkFlag(utils.L2DataStreamerUrlFlag.Name, cfg.Zk.L2DataStreamerUrl)
}
checkFlag(utils.L1ChainIdFlag.Name, cfg.Zk.L1ChainId)
checkFlag(utils.L1RpcUrlFlag.Name, cfg.Zk.L1RpcUrl)
checkFlag(utils.L1ContractAddressFlag.Name, cfg.Zk.L1ContractAddress.Hex())
Expand Down
97 changes: 0 additions & 97 deletions turbo/stages/stageloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon/zk/sequencer"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/chain"
Expand All @@ -37,9 +36,6 @@ import (
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
"github.com/ledgerwatch/erigon/turbo/stages/bodydownload"
"github.com/ledgerwatch/erigon/turbo/stages/headerdownload"
"github.com/ledgerwatch/erigon/zk/datastream/client"
zkStages "github.com/ledgerwatch/erigon/zk/stages"
"github.com/ledgerwatch/erigon/zk/syncer"
)

func SendPayloadStatus(hd *headerdownload.HeaderDownload, headBlockHash libcommon.Hash, err error) {
Expand Down Expand Up @@ -445,99 +441,6 @@ func NewDefaultStages(ctx context.Context,
runInTestMode)
}

func NewDefaultZkStages(ctx context.Context,
db kv.RwDB,
p2pCfg p2p.Config,
cfg *ethconfig.Config,
controlServer *sentry.MultiClient,
notifications *shards.Notifications,
snapDownloader proto_downloader.DownloaderClient,
snapshots *snapshotsync.RoSnapshots,
agg *state.AggregatorV3,
forkValidator *engineapi.ForkValidator,
engine consensus.Engine,
l1Syncer *syncer.L1Syncer,
datastreamClient *client.StreamClient,
) []*sync_stages.Stage {
dirs := cfg.Dirs
blockReader := snapshotsync.NewBlockReaderWithSnapshots(snapshots, cfg.TransactionsV3)
blockRetire := snapshotsync.NewBlockRetire(1, dirs.Tmp, snapshots, db, snapDownloader, notifications.Events)

// During Import we don't want other services like header requests, body requests etc. to be running.
// Hence we run it in the test mode.
runInTestMode := cfg.ImportMode

if sequencer.IsSequencer() {
return zkStages.SequencerZkStages(ctx,
stagedsync.StageCumulativeIndexCfg(db),
stagedsync.StageBlockHashesCfg(db, dirs.Tmp, controlServer.ChainConfig),
stagedsync.StageSendersCfg(db, controlServer.ChainConfig, false, dirs.Tmp, cfg.Prune, blockRetire, controlServer.Hd),
zkStages.StageSequenceBlocksCfg(
db,
cfg.Prune,
cfg.BatchSize,
nil,
controlServer.ChainConfig,
controlServer.Engine,
&vm.Config{},
notifications.Accumulator,
cfg.StateStream,
/*stateStream=*/ false,
cfg.HistoryV3,
dirs,
blockReader,
controlServer.Hd,
cfg.Genesis,
cfg.Sync,
agg,
cfg.Zk,
),
stagedsync.StageHashStateCfg(db, dirs, cfg.HistoryV3, agg),
zkStages.StageZkInterHashesCfg(db, true, true, false, dirs.Tmp, blockReader, controlServer.Hd, cfg.HistoryV3, agg, cfg.Zk),
stagedsync.StageHistoryCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageCallTracesCfg(db, cfg.Prune, 0, dirs.Tmp),
stagedsync.StageTxLookupCfg(db, cfg.Prune, dirs.Tmp, snapshots, controlServer.ChainConfig.Bor),
stagedsync.StageFinishCfg(db, dirs.Tmp, forkValidator),
runInTestMode)
}

return zkStages.DefaultZkStages(ctx,
zkStages.StageL1SyncerCfg(db, l1Syncer, cfg.Zk),
zkStages.StageBatchesCfg(db, datastreamClient),
stagedsync.StageCumulativeIndexCfg(db),
stagedsync.StageBlockHashesCfg(db, dirs.Tmp, controlServer.ChainConfig),
stagedsync.StageSendersCfg(db, controlServer.ChainConfig, false, dirs.Tmp, cfg.Prune, blockRetire, controlServer.Hd),
stagedsync.StageExecuteBlocksCfg(
db,
cfg.Prune,
cfg.BatchSize,
nil,
controlServer.ChainConfig,
controlServer.Engine,
&vm.Config{},
notifications.Accumulator,
cfg.StateStream,
/*stateStream=*/ false,
cfg.HistoryV3,
dirs,
blockReader,
controlServer.Hd,
cfg.Genesis,
cfg.Sync,
agg,
cfg.Zk,
),
stagedsync.StageHashStateCfg(db, dirs, cfg.HistoryV3, agg),
zkStages.StageZkInterHashesCfg(db, true, true, false, dirs.Tmp, blockReader, controlServer.Hd, cfg.HistoryV3, agg, cfg.Zk),
stagedsync.StageHistoryCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageCallTracesCfg(db, cfg.Prune, 0, dirs.Tmp),
stagedsync.StageTxLookupCfg(db, cfg.Prune, dirs.Tmp, snapshots, controlServer.ChainConfig.Bor),
stagedsync.StageFinishCfg(db, dirs.Tmp, forkValidator),
runInTestMode)
}

func NewInMemoryExecution(ctx context.Context, db kv.RwDB, cfg *ethconfig.Config, controlServer *sentry.MultiClient, dirs datadir.Dirs, notifications *shards.Notifications, snapshots *snapshotsync.RoSnapshots, agg *state.AggregatorV3) (*sync_stages.Sync, error) {
blockReader := snapshotsync.NewBlockReaderWithSnapshots(snapshots, cfg.TransactionsV3)

Expand Down
Loading