Skip to content

Commit

Permalink
fix(configs): Make genesis fields optional (#1555)
Browse files Browse the repository at this point in the history
## What ❔

Make genesis fields optional. 

## Why ❔

For backward compatibility with env variables, we have to keep some
genesis fields optional. We will replace it with required fields, once
we move to the new config system

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.

Signed-off-by: Danil <deniallugo@gmail.com>
  • Loading branch information
Deniallugo committed Apr 2, 2024
1 parent 2b27290 commit 2d0ef46
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 91 deletions.
22 changes: 12 additions & 10 deletions core/lib/config/src/configs/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ pub struct SharedBridge {
/// Each chain has this config immutable and we update it only during the protocol upgrade
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GenesisConfig {
pub protocol_version: u16,
pub genesis_root_hash: H256,
pub rollup_last_leaf_index: u64,
pub genesis_commitment: H256,
pub bootloader_hash: H256,
pub default_aa_hash: H256,
// TODO make fields non optional, once we fully moved to file based configs.
// Now for backward compatibility we keep it optional
pub protocol_version: Option<u16>,
pub genesis_root_hash: Option<H256>,
pub rollup_last_leaf_index: Option<u64>,
pub genesis_commitment: Option<H256>,
pub bootloader_hash: Option<H256>,
pub default_aa_hash: Option<H256>,
pub l1_chain_id: L1ChainId,
pub l2_chain_id: L2ChainId,
pub recursion_node_level_vk_hash: H256,
Expand All @@ -35,8 +37,8 @@ pub struct GenesisConfig {
impl GenesisConfig {
pub fn for_tests() -> Self {
GenesisConfig {
genesis_root_hash: H256::repeat_byte(0x01),
rollup_last_leaf_index: 26,
genesis_root_hash: Some(H256::repeat_byte(0x01)),
rollup_last_leaf_index: Some(26),
recursion_scheduler_level_vk_hash: H256::repeat_byte(0x02),
fee_account: Default::default(),
shared_bridge: Some(SharedBridge {
Expand All @@ -47,11 +49,11 @@ impl GenesisConfig {
recursion_node_level_vk_hash: H256::repeat_byte(0x03),
recursion_leaf_level_vk_hash: H256::repeat_byte(0x04),
recursion_circuits_set_vks_hash: H256::repeat_byte(0x05),
genesis_commitment: H256::repeat_byte(0x17),
genesis_commitment: Some(H256::repeat_byte(0x17)),
bootloader_hash: Default::default(),
default_aa_hash: Default::default(),
l1_chain_id: L1ChainId(9),
protocol_version: 22,
protocol_version: Some(22),
l2_chain_id: L2ChainId::default(),
dummy_verifier: false,
l1_batch_commit_data_generator_mode: L1BatchCommitDataGeneratorMode::Rollup,
Expand Down
24 changes: 6 additions & 18 deletions core/lib/env_config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,12 @@ impl FromEnv for GenesisConfig {

#[allow(deprecated)]
Ok(GenesisConfig {
protocol_version: contracts_config
.genesis_protocol_version
.context("Protocol version is required for genesis")?,
genesis_root_hash: contracts_config
.genesis_root
.context("genesis_root_hash required for genesis")?,
rollup_last_leaf_index: contracts_config
.genesis_rollup_leaf_index
.context("rollup_last_leaf_index required for genesis")?,
genesis_commitment: contracts_config
.genesis_batch_commitment
.context("genesis_commitment required for genesis")?,
bootloader_hash: state_keeper
.bootloader_hash
.context("Bootloader hash required for genesis")?,
default_aa_hash: state_keeper
.default_aa_hash
.context("Default aa hash required for genesis")?,
protocol_version: contracts_config.genesis_protocol_version,
genesis_root_hash: contracts_config.genesis_root,
rollup_last_leaf_index: contracts_config.genesis_rollup_leaf_index,
genesis_commitment: contracts_config.genesis_batch_commitment,
bootloader_hash: state_keeper.bootloader_hash,
default_aa_hash: state_keeper.default_aa_hash,
l1_chain_id: network_config.network.chain_id(),
l2_chain_id: network_config.zksync_network_id,
recursion_node_level_vk_hash: contracts_config.fri_recursion_node_level_vk_hash,
Expand Down
57 changes: 34 additions & 23 deletions core/lib/protobuf_config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,34 @@ impl ProtoRepr for proto::Genesis {
None
};
Ok(Self::Type {
protocol_version: required(&self.genesis_protocol_version)
.map(|x| *x as u16)
.context("protocol_version")?,
genesis_root_hash: required(&self.genesis_root)
.and_then(|x| parse_h256(x))
.context("genesis_root_hash")?,
rollup_last_leaf_index: *required(&self.genesis_rollup_leaf_index)
.context("rollup_last_leaf_index")?,
genesis_commitment: required(&self.genesis_batch_commitment)
.and_then(|x| parse_h256(x))
.context("genesis_commitment")?,
bootloader_hash: required(&self.bootloader_hash)
.and_then(|x| parse_h256(x))
.context("bootloader_hash")?,
default_aa_hash: required(&self.default_aa_hash)
.and_then(|x| parse_h256(x))
.context("default_aa_hash")?,
protocol_version: Some(
required(&self.genesis_protocol_version)
.map(|x| *x as u16)
.context("protocol_version")?,
),
genesis_root_hash: Some(
required(&self.genesis_root)
.and_then(|x| parse_h256(x))
.context("genesis_root_hash")?,
),
rollup_last_leaf_index: Some(
*required(&self.genesis_rollup_leaf_index).context("rollup_last_leaf_index")?,
),
genesis_commitment: Some(
required(&self.genesis_batch_commitment)
.and_then(|x| parse_h256(x))
.context("genesis_commitment")?,
),
bootloader_hash: Some(
required(&self.bootloader_hash)
.and_then(|x| parse_h256(x))
.context("bootloader_hash")?,
),
default_aa_hash: Some(
required(&self.default_aa_hash)
.and_then(|x| parse_h256(x))
.context("default_aa_hash")?,
),
l1_chain_id: required(&self.l1_chain_id)
.map(|x| L1ChainId(*x))
.context("l1_chain_id")?,
Expand Down Expand Up @@ -112,12 +123,12 @@ impl ProtoRepr for proto::Genesis {
});

Self {
genesis_root: Some(format!("{:?}", this.genesis_root_hash)),
genesis_rollup_leaf_index: Some(this.rollup_last_leaf_index),
genesis_batch_commitment: Some(format!("{:?}", this.genesis_root_hash)),
genesis_protocol_version: Some(this.protocol_version as u32),
default_aa_hash: Some(format!("{:?}", this.default_aa_hash)),
bootloader_hash: Some(format!("{:?}", this.bootloader_hash)),
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_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)),
fee_account: Some(format!("{:?}", this.fee_account)),
l1_chain_id: Some(this.l1_chain_id.0),
l2_chain_id: Some(this.l2_chain_id.as_u64()),
Expand Down
24 changes: 13 additions & 11 deletions core/lib/zksync_core/src/api_server/web3/namespaces/en.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,30 @@ impl EnNamespace {
};

let config = GenesisConfig {
protocol_version,
genesis_root_hash: H256::from_slice(
protocol_version: Some(protocol_version),
genesis_root_hash: Some(H256::from_slice(
&genesis_batch.hash.context("Genesis is not finished")?,
)),
rollup_last_leaf_index: Some(
genesis_batch
.rollup_last_leaf_index
.context("Genesis is not finished")? as u64,
),
rollup_last_leaf_index: genesis_batch
.rollup_last_leaf_index
.context("Genesis is not finished")? as u64,
genesis_commitment: H256::from_slice(
genesis_commitment: Some(H256::from_slice(
&genesis_batch
.commitment
.context("Genesis is not finished")?,
),
bootloader_hash: H256::from_slice(
)),
bootloader_hash: Some(H256::from_slice(
&genesis_batch
.bootloader_code_hash
.context("Genesis is not finished")?,
),
default_aa_hash: H256::from_slice(
)),
default_aa_hash: Some(H256::from_slice(
&genesis_batch
.default_aa_code_hash
.context("Genesis is not finished")?,
),
)),
l1_chain_id: self.state.api_config.l1_chain_id,

l2_chain_id: self.state.api_config.l2_chain_id,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/consistency_checker/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ async fn checker_processes_pre_boojum_batches(
let pool = ConnectionPool::<Core>::test_pool().await;
let mut storage = pool.connection().await.unwrap();
let genesis_params = GenesisParams::load_genesis_params(GenesisConfig {
protocol_version: PRE_BOOJUM_PROTOCOL_VERSION as u16,
protocol_version: Some(PRE_BOOJUM_PROTOCOL_VERSION as u16),
..mock_genesis_config()
})
.unwrap();
Expand Down
71 changes: 48 additions & 23 deletions core/lib/zksync_core/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub enum GenesisError {
DBError(#[from] SqlxError),
#[error("Error: {0}")]
Other(#[from] anyhow::Error),
#[error("Field: {0} required for genesis")]
MalformedConfig(&'static str),
}

#[derive(Debug, Clone)]
Expand All @@ -96,8 +98,12 @@ impl GenesisParams {
system_contracts: Vec<DeployedContract>,
) -> Result<GenesisParams, GenesisError> {
let base_system_contracts_hashes = BaseSystemContractsHashes {
bootloader: config.bootloader_hash,
default_aa: config.default_aa_hash,
bootloader: config
.bootloader_hash
.ok_or(GenesisError::MalformedConfig("bootloader_hash"))?,
default_aa: config
.default_aa_hash
.ok_or(GenesisError::MalformedConfig("default_aa_hash"))?,
};
if base_system_contracts_hashes != base_system_contracts.hashes() {
return Err(GenesisError::BaseSystemContractsHashes(Box::new(
Expand All @@ -111,8 +117,9 @@ impl GenesisParams {
// if the version doesn't exist
let _: ProtocolVersionId = config
.protocol_version
.ok_or(GenesisError::MalformedConfig("protocol_version"))?
.try_into()
.map_err(|_| GenesisError::ProtocolVersion(config.protocol_version))?;
.map_err(|_| GenesisError::ProtocolVersion(config.protocol_version.unwrap()))?;
Ok(GenesisParams {
base_system_contracts,
system_contracts,
Expand All @@ -139,6 +146,7 @@ impl GenesisParams {
// It's impossible to instantiate Genesis params with wrong protocol version
self.config
.protocol_version
.expect("Protocol version must be set")
.try_into()
.expect("Protocol version must be correctly initialized for genesis")
}
Expand All @@ -152,12 +160,12 @@ pub fn mock_genesis_config() -> GenesisConfig {
let first_l1_verifier_config = L1VerifierConfig::default();

GenesisConfig {
protocol_version: ProtocolVersionId::latest() as u16,
genesis_root_hash: Default::default(),
rollup_last_leaf_index: 26,
protocol_version: Some(ProtocolVersionId::latest() as u16),
genesis_root_hash: Some(Default::default()),
rollup_last_leaf_index: Some(26),
genesis_commitment: Default::default(),
bootloader_hash: base_system_contracts_hashes.bootloader,
default_aa_hash: base_system_contracts_hashes.default_aa,
bootloader_hash: Some(base_system_contracts_hashes.bootloader),
default_aa_hash: Some(base_system_contracts_hashes.default_aa),
l1_chain_id: L1ChainId(9),
l2_chain_id: L2ChainId::default(),
recursion_node_level_vk_hash: first_l1_verifier_config.params.recursion_node_level_vk_hash,
Expand Down Expand Up @@ -220,8 +228,14 @@ pub async fn insert_genesis_batch(
let rollup_last_leaf_index = metadata.leaf_count + 1;

let base_system_contract_hashes = BaseSystemContractsHashes {
bootloader: genesis_params.config.bootloader_hash,
default_aa: genesis_params.config.default_aa_hash,
bootloader: genesis_params
.config
.bootloader_hash
.ok_or(GenesisError::MalformedConfig("bootloader"))?,
default_aa: genesis_params
.config
.default_aa_hash
.ok_or(GenesisError::MalformedConfig("default_aa_hash"))?,
};
let commitment_input = CommitmentInput::for_genesis_batch(
genesis_root_hash,
Expand Down Expand Up @@ -267,23 +281,34 @@ pub async fn ensure_genesis_state(
commitment,
rollup_last_leaf_index,
} = insert_genesis_batch(&mut transaction, genesis_params).await?;
if genesis_params.config.genesis_root_hash != root_hash {
return Err(GenesisError::RootHash(
genesis_params.config.genesis_root_hash,
root_hash,
));

let expected_root_hash = genesis_params
.config
.genesis_root_hash
.ok_or(GenesisError::MalformedConfig("genesis_root_hash"))?;
let expected_commitment = genesis_params
.config
.genesis_commitment
.ok_or(GenesisError::MalformedConfig("expected_commitment"))?;
let expected_rollup_last_leaf_index =
genesis_params
.config
.rollup_last_leaf_index
.ok_or(GenesisError::MalformedConfig(
"expected_rollup_last_leaf_index",
))?;

if expected_root_hash != root_hash {
return Err(GenesisError::RootHash(expected_root_hash, root_hash));
}

if genesis_params.config.genesis_commitment != commitment {
return Err(GenesisError::Commitment(
genesis_params.config.genesis_commitment,
commitment,
));
if expected_commitment != commitment {
return Err(GenesisError::Commitment(expected_commitment, commitment));
}

if genesis_params.config.rollup_last_leaf_index != rollup_last_leaf_index {
if expected_rollup_last_leaf_index != rollup_last_leaf_index {
return Err(GenesisError::LeafIndexes(
genesis_params.config.rollup_last_leaf_index,
expected_rollup_last_leaf_index,
rollup_last_leaf_index,
));
}
Expand Down Expand Up @@ -646,7 +671,7 @@ mod tests {
let pool = ConnectionPool::<Core>::test_pool().await;
let mut conn = pool.connection().await.unwrap();
let params = GenesisParams::load_genesis_params(GenesisConfig {
protocol_version: ProtocolVersionId::Version10 as u16,
protocol_version: Some(ProtocolVersionId::Version10 as u16),
..mock_genesis_config()
})
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/state_keeper/io/common/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ async fn getting_batch_version_with_genesis() {
let pool = ConnectionPool::<Core>::test_pool().await;
let mut storage = pool.connection().await.unwrap();
let genesis_params = GenesisParams::load_genesis_params(GenesisConfig {
protocol_version: ProtocolVersionId::Version5 as u16,
protocol_version: Some(ProtocolVersionId::Version5 as u16),
..mock_genesis_config()
})
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions core/lib/zksync_core/src/sync_layer/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async fn create_genesis_params(
) -> anyhow::Result<GenesisParams> {
let config = client.fetch_genesis_config().await?;
let base_system_contracts_hashes = BaseSystemContractsHashes {
bootloader: config.bootloader_hash,
default_aa: config.default_aa_hash,
bootloader: config.bootloader_hash.context("Genesis is not finished")?,
default_aa: config.default_aa_hash.context("Genesis is not finished")?,
};

if zksync_chain_id != config.l2_chain_id {
Expand Down
4 changes: 2 additions & 2 deletions etc/env/file_based/genesis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ default_aa_hash: 0x0100055b041eb28aff6e3a6e0f37c31fd053fc9ef142683b05e5f0aee6934
genesis_root: 0x436cf80dd02a7e1a1df65be6ec9ea231ccec97c44f4c8c9cd2aa26c2feb074cd
genesis_batch_commitment: 0x938016208176c5a49d47c8aa582b5d18afc4f159dfa099087770e0796948fd1a
genesis_rollup_leaf_index: 26
genesis_protocol_version: 21
genesis_protocol_version: 22
l1_chain_id: 9
l2_chain_id: 270
fee_account: 0x0000000000000000000000000000000000000001
l1_batch_commit_data_generator_mode:
l1_batch_commit_data_generator_mode: Rollup
prover:
recursion_leaf_level_vk_hash: 0x400a4b532c6f072c00d1806ef299300d4c104f4ac55bd8698ade78894fcadc0a
recursion_node_level_vk_hash: 0x5a3ef282b21e12fe1f4438e5bb158fc5060b160559c5158c6389d62d9fe3d080
Expand Down

0 comments on commit 2d0ef46

Please sign in to comment.