Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(genesis): Add genesis config generator (#1671)
## 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
1 parent
c818be3
commit 45164fa
Showing
8 changed files
with
201 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, ¶ms).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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters