Skip to content

Commit

Permalink
feat(genesis): Add genesis config generator (#1671)
Browse files Browse the repository at this point in the history
## What ❔

* Tool to create new genesis.yaml file
* Update the genesis.yaml to v23

## Why ❔

* To automate the process, before we introduce hyperchains.

---------

Signed-off-by: Danil <deniallugo@gmail.com>
Co-authored-by: Danil <deniallugo@gmail.com>
  • Loading branch information
mm-zk and Deniallugo committed Apr 12, 2024
1 parent c818be3 commit 45164fa
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 16 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -9,6 +9,7 @@ members = [
"core/bin/system-constants-generator",
"core/bin/verified_sources_fetcher",
"core/bin/zksync_server",
"core/bin/genesis_generator",
# Node services
"core/node/node_framework",
# Libraries
Expand Down
34 changes: 34 additions & 0 deletions core/bin/genesis_generator/Cargo.toml
@@ -0,0 +1,34 @@
[package]
name = "genesis_generator"
version.workspace = true
edition.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
keywords.workspace = true
categories.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
zksync_config.workspace = true
zksync_env_config.workspace = true
zksync_protobuf_config.workspace = true
zksync_utils.workspace = true
zksync_types.workspace = true
zksync_core.workspace = true
zksync_dal.workspace = true
zksync_contracts.workspace = true
zksync_protobuf.workspace = true


anyhow.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
serde.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing.workspace = true
futures.workspace = true
clap = { workspace = true, features = ["derive"] }

113 changes: 113 additions & 0 deletions core/bin/genesis_generator/src/main.rs
@@ -0,0 +1,113 @@
/// Each protocol upgrade required to update genesis config values.
/// This tool generates the new correct genesis file that could be used for the new chain
/// Please note, this tool update only yaml file, if you are still use env based configuration,
/// update env values correspondingly
use std::fs;

use anyhow::Context as _;
use clap::Parser;
use serde_yaml::Serializer;
use zksync_config::{GenesisConfig, PostgresConfig};
use zksync_contracts::BaseSystemContracts;
use zksync_core::{
genesis::{insert_genesis_batch, GenesisParams},
temp_config_store::decode_yaml_repr,
};
use zksync_dal::{ConnectionPool, Core, CoreDal};
use zksync_env_config::FromEnv;
use zksync_protobuf::{
build::{prost_reflect, prost_reflect::ReflectMessage},
ProtoRepr,
};
use zksync_protobuf_config::proto::genesis::Genesis;
use zksync_types::ProtocolVersionId;

const DEFAULT_GENESIS_FILE_PATH: &str = "./etc/env/file_based//genesis.yaml";

#[derive(Debug, Parser)]
#[command(author = "Matter Labs", version, about = "zkSync operator node", long_about = None)]
struct Cli {
#[arg(long)]
config_path: Option<std::path::PathBuf>,
#[arg(long, default_value = "false")]
check: bool,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let opt = Cli::parse();

let postgres_config = match opt.config_path {
None => PostgresConfig::from_env()?,
Some(path) => {
let yaml =
std::fs::read_to_string(&path).with_context(|| path.display().to_string())?;
let config =
decode_yaml_repr::<zksync_protobuf_config::proto::general::GeneralConfig>(&yaml)
.context("failed decoding general YAML config")?;
config
.postgres_config
.context("Postgres config must exist")?
}
};

let yaml = std::fs::read_to_string(DEFAULT_GENESIS_FILE_PATH)
.with_context(|| DEFAULT_GENESIS_FILE_PATH.to_string())?;
let original_genesis = decode_yaml_repr::<Genesis>(&yaml)?;
let db_url = postgres_config.master_url()?;
let new_genesis = generate_new_config(db_url, original_genesis.clone()).await?;
if opt.check {
assert_eq!(&original_genesis, &new_genesis);
println!("Genesis config is up to date");
return Ok(());
}
let data = encode_yaml(&Genesis::build(&new_genesis))?;
fs::write(DEFAULT_GENESIS_FILE_PATH, data)?;
Ok(())
}

async fn generate_new_config(
db_url: &str,
genesis_config: GenesisConfig,
) -> anyhow::Result<GenesisConfig> {
let pool = ConnectionPool::<Core>::singleton(db_url)
.build()
.await
.context("failed to build connection_pool")?;
let mut storage = pool.connection().await.context("connection()")?;
let mut transaction = storage.start_transaction().await?;

if !transaction.blocks_dal().is_genesis_needed().await? {
anyhow::bail!("Please cleanup database for regenerating genesis")
}

let base_system_contracts = BaseSystemContracts::load_from_disk().hashes();
let mut updated_genesis = GenesisConfig {
protocol_version: Some(ProtocolVersionId::latest() as u16),
genesis_root_hash: None,
rollup_last_leaf_index: None,
genesis_commitment: None,
bootloader_hash: Some(base_system_contracts.bootloader),
default_aa_hash: Some(base_system_contracts.default_aa),
..genesis_config
};

let params = GenesisParams::load_genesis_params(updated_genesis.clone())?;
let batch_params = insert_genesis_batch(&mut transaction, &params).await?;

updated_genesis.genesis_commitment = Some(batch_params.commitment);
updated_genesis.genesis_root_hash = Some(batch_params.root_hash);
updated_genesis.rollup_last_leaf_index = Some(batch_params.rollup_last_leaf_index);
Ok(updated_genesis)
}

/// Encodes a generated proto message to json for arbitrary Proto.
pub(crate) fn encode_yaml<T: ReflectMessage>(x: &T) -> anyhow::Result<String> {
let mut serializer = Serializer::new(vec![]);
let opts = prost_reflect::SerializeOptions::new()
.use_proto_field_name(true)
.stringify_64_bit_integers(true);
x.transcode_to_dynamic()
.serialize_with_options(&mut serializer, &opts)?;
Ok(String::from_utf8_lossy(&serializer.into_inner()?).to_string())
}
4 changes: 2 additions & 2 deletions core/lib/config/src/configs/genesis.rs
Expand Up @@ -3,7 +3,7 @@ use zksync_basic_types::{Address, L1ChainId, L2ChainId, H256};

use crate::configs::chain::L1BatchCommitDataGeneratorMode;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SharedBridge {
pub bridgehub_proxy_addr: Address,
pub state_transition_proxy_addr: Address,
Expand All @@ -12,7 +12,7 @@ pub struct SharedBridge {

/// This config represents the genesis state of the chain.
/// Each chain has this config immutable and we update it only during the protocol upgrade
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct GenesisConfig {
// TODO make fields non optional, once we fully moved to file based configs.
// Now for backward compatibility we keep it optional
Expand Down
2 changes: 1 addition & 1 deletion core/lib/protobuf_config/src/genesis.rs
Expand Up @@ -125,7 +125,7 @@ impl ProtoRepr for proto::Genesis {
Self {
genesis_root: this.genesis_root_hash.map(|x| format!("{:?}", x)),
genesis_rollup_leaf_index: this.rollup_last_leaf_index,
genesis_batch_commitment: this.genesis_root_hash.map(|x| format!("{:?}", x)),
genesis_batch_commitment: this.genesis_commitment.map(|x| format!("{:?}", x)),
genesis_protocol_version: this.protocol_version.map(|x| x as u32),
default_aa_hash: this.default_aa_hash.map(|x| format!("{:?}", x)),
bootloader_hash: this.bootloader_hash.map(|x| format!("{:?}", x)),
Expand Down
27 changes: 14 additions & 13 deletions etc/env/file_based/genesis.yaml
@@ -1,19 +1,20 @@
bootloader_hash: 0x010007ede999d096c84553fb514d3d6ca76fbf39789dda76bfeda9f3ae06236e
default_aa_hash: 0x0100055b041eb28aff6e3a6e0f37c31fd053fc9ef142683b05e5f0aee6934066
genesis_root: 0x1f50e4eda06a68d96a0272ba4581a342df2227ad12c23759ab7d78157950e69a
genesis_batch_commitment: 0x84d7b576b9374729e0b38439c97aaf5074335e2a8d1c7a2e4581c1c1ec611631
genesis_rollup_leaf_index: 46
genesis_protocol_version: 22
l1_chain_id: 9
l2_chain_id: 270
fee_account: 0x0000000000000000000000000000000000000001
l1_batch_commit_data_generator_mode: Rollup
genesis_root: 0x1920e0154aa7649a645e7931b84796bfec22b58250778b828d9a5b8c7d32f661
genesis_rollup_leaf_index: '50'
genesis_batch_commitment: 0xc64914ac5697bf6a73b9cca890ef013a83f8415573f2829b29111598410852a6
genesis_protocol_version: 23
default_aa_hash: 0x01000563ee5d0abdf9fc65f748a700d2c377aadebc16d5bf21105f8a94eff028
bootloader_hash: 0x0100089d6cf001d52b9e64572fa9b3555211f56a2ad66784f495c4192a88b477
l1_chain_id: '9'
l2_chain_id: '270'
fee_account: '0x0000000000000000000000000000000000000001'
prover:
recursion_leaf_level_vk_hash: 0x400a4b532c6f072c00d1806ef299300d4c104f4ac55bd8698ade78894fcadc0a
recursion_node_level_vk_hash: 0x5a3ef282b21e12fe1f4438e5bb158fc5060b160559c5158c6389d62d9fe3d080
recursion_scheduler_level_vk_hash: 0x063c6fb5c70404c2867f413a8e35563ad3d040b1ad8c11786231bfdba7b472c7
recursion_node_level_vk_hash: 0x5a3ef282b21e12fe1f4438e5bb158fc5060b160559c5158c6389d62d9fe3d080
recursion_leaf_level_vk_hash: 0x400a4b532c6f072c00d1806ef299300d4c104f4ac55bd8698ade78894fcadc0a
recursion_circuits_set_vks_hash: '0x0000000000000000000000000000000000000000000000000000000000000000'
dummy_verifier: true
shared_bridge:
state_transition_proxy_addr: 0x87d456da9ed212eb49d80d96afb44afddf36adf8
bridgehub_proxy_addr: 0xdd6fa5c14e7550b4caf2aa2818d24c69cbc347e5
state_transition_proxy_addr: 0x87d456da9ed212eb49d80d96afb44afddf36adf8
transparent_proxy_admin_addr: 0xc957c0e82d3bafb5ad46ffbcc66900648784eb05
l1_batch_commit_data_generator_mode: Rollup
13 changes: 13 additions & 0 deletions infrastructure/zk/src/run.ts
Expand Up @@ -93,6 +93,11 @@ export async function loadtest(...args: string[]) {
await utils.spawn(`cargo run --release --bin loadnext -- ${args.join(' ')}`);
}

export async function genesisConfigGenerator(...args: string[]) {
console.log(args);
await utils.spawn(`cargo run --release --bin genesis_generator -- ${args.join(' ')}`);
}

export async function readVariable(address: string, contractName: string, variableName: string, file?: string) {
if (file === undefined)
await utils.spawn(
Expand Down Expand Up @@ -137,6 +142,14 @@ command
.description('get the revert reason for ethereum transaction')
.action(revertReason);

command
.command('genesis-config-generator [options...]')
.description('run the genesis-config-generator')
.allowUnknownOption()
.action(async (options: string[]) => {
await genesisConfigGenerator(...options);
});

command
.command('loadtest [options...]')
.description('run the loadtest')
Expand Down

0 comments on commit 45164fa

Please sign in to comment.