Skip to content

Commit

Permalink
Merge pull request #6 from lambdaclass/validium-setup
Browse files Browse the repository at this point in the history
Validium setup
  • Loading branch information
igamigo authored Nov 8, 2023
2 parents 0c22dcc + 5e03baf commit 017cc81
Show file tree
Hide file tree
Showing 16 changed files with 1,781 additions and 794 deletions.
1,925 changes: 1,665 additions & 260 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"core/bin/verification_key_generator_and_server",
"core/bin/verified_sources_fetcher",
"core/bin/zksync_server",
"zksync_full_stack",
# Libraries
"core/lib/zksync_core",
"core/lib/basic_types",
Expand Down
480 changes: 0 additions & 480 deletions Executor.sol

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion contracts
3 changes: 2 additions & 1 deletion core/lib/types/src/aggregated_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn l1_batch_range_from_batches(
pub struct L1BatchCommitOperation {
pub last_committed_l1_batch: L1BatchWithMetadata,
pub l1_batches: Vec<L1BatchWithMetadata>,
pub is_validium: bool,
}

impl L1BatchCommitOperation {
Expand All @@ -41,7 +42,7 @@ impl L1BatchCommitOperation {
.iter()
.map(|x| {
println!("constructing pubdata for commit op");
L1BatchWithMetadata::l1_commit_data(x)
L1BatchWithMetadata::l1_commit_data(x, self.is_validium)
})
.collect();
vec![stored_batch_info, Token::Array(l1_batches_to_commit)]
Expand Down
37 changes: 24 additions & 13 deletions core/lib/types/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl L1BatchWithMetadata {
])
}

pub fn l1_commit_data(&self) -> Token {
pub fn l1_commit_data(&self, validium: bool) -> Token {
if self.header.protocol_version.unwrap().is_pre_boojum() {
Token::Tuple(vec![
Token::Uint(U256::from(self.header.number.0)),
Expand Down Expand Up @@ -192,20 +192,19 @@ impl L1BatchWithMetadata {
.to_vec(),
),
Token::Bytes(self.metadata.l2_l1_messages_compressed.clone()),
Token::Bytes(self.construct_pubdata()),
Token::Bytes(self.construct_pubdata(validium)),
])
}
}

pub fn l1_commit_data_size(&self) -> usize {
crate::ethabi::encode(&[Token::Array(vec![self.l1_commit_data()])]).len()
pub fn l1_commit_data_size(&self, validium: bool) -> usize {
crate::ethabi::encode(&[Token::Array(vec![self.l1_commit_data(validium)])]).len()
}

/// Packs all pubdata needed for batch commitment in boojum into one bytes array. The packing contains the
/// following: logs, messages, bytecodes, and compressed state diffs.
/// This data is currently part of calldata but will be submitted as part of the blob section post EIP-4844.
pub fn construct_pubdata(&self) -> Vec<u8> {
println!("entered construct_pubdata()");
pub fn construct_pubdata(&self, validium: bool) -> Vec<u8> {
let mut res: Vec<u8> = vec![];

// Process and Pack Logs
Expand All @@ -214,14 +213,26 @@ impl L1BatchWithMetadata {
res.extend(l2_to_l1_log.0.to_bytes());
}

// Process and Pack Msgs
res.extend((self.header.l2_to_l1_messages.len() as u32).to_be_bytes());
for msg in &self.header.l2_to_l1_messages {
res.extend((msg.len() as u32).to_be_bytes());
res.extend(msg);
}
if validium {
res.extend(vec![1u8, 2u8, 3u8, 9u8]); // To check on eth_getTransactionByHash for the commit op
} else {
// Process and Pack Msgs
res.extend((self.header.l2_to_l1_messages.len() as u32).to_be_bytes());
for msg in &self.header.l2_to_l1_messages {
res.extend((msg.len() as u32).to_be_bytes());
res.extend(msg);
}

res.extend(vec![1u8, 2u8, 3u8, 9u8]); // to check on eth_getTransactionByHash for the commit op
// Process and Pack Bytecodes
res.extend((self.factory_deps.len() as u32).to_be_bytes());
for bytecode in &self.factory_deps {
res.extend((bytecode.len() as u32).to_be_bytes());
res.extend(bytecode);
}

// Extend with Compressed StateDiffs
res.extend(&self.metadata.state_diffs_compressed);
}

res
}
Expand Down
13 changes: 11 additions & 2 deletions core/lib/zksync_core/src/consistency_checker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use std::{env, time::Duration};

use zksync_contracts::PRE_BOOJUM_COMMIT_FUNCTION;
use zksync_dal::ConnectionPool;
Expand Down Expand Up @@ -35,6 +35,15 @@ impl ConsistencyChecker {

async fn check_commitments(&self, batch_number: L1BatchNumber) -> Result<bool, error::Error> {
let mut storage = self.db.access_storage().await.unwrap();
let validium: bool = env::var("VALIDIUM")
.map(|v| {
println!("env var validium: {}", v);
match v.as_str() {
"true" => true,
_ => false,
}
})
.unwrap_or(false);

let storage_l1_batch = storage
.blocks_dal()
Expand Down Expand Up @@ -127,7 +136,7 @@ impl ConsistencyChecker {

let commitment = &commitments[batch_number.0 as usize - first_batch_number];

Ok(commitment == &block_metadata.l1_commit_data())
Ok(commitment == &block_metadata.l1_commit_data(validium))
}

async fn last_committed_batch(&self) -> L1BatchNumber {
Expand Down
8 changes: 7 additions & 1 deletion core/lib/zksync_core/src/eth_sender/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ pub struct Aggregator {
execute_criteria: Vec<Box<dyn L1BatchPublishCriterion>>,
config: SenderConfig,
blob_store: Box<dyn ObjectStore>,
validium: bool,
}

impl Aggregator {
pub fn new(config: SenderConfig, blob_store: Box<dyn ObjectStore>) -> Self {
pub fn new(config: SenderConfig, blob_store: Box<dyn ObjectStore>, validium: bool) -> Self {
Self {
commit_criteria: vec![
Box::from(NumberCriterion {
Expand All @@ -43,6 +44,7 @@ impl Aggregator {
Box::from(DataSizeCriterion {
op: AggregatedActionType::Commit,
data_limit: config.max_eth_tx_data_size,
validium,
}),
Box::from(TimestampDeadlineCriterion {
op: AggregatedActionType::Commit,
Expand Down Expand Up @@ -85,6 +87,7 @@ impl Aggregator {
],
config,
blob_store,
validium,
}
}

Expand Down Expand Up @@ -128,6 +131,7 @@ impl Aggregator {
last_sealed_l1_batch_number,
base_system_contracts_hashes,
protocol_version_id,
self.validium,
)
.await
.map(AggregatedOperation::Commit)
Expand Down Expand Up @@ -167,6 +171,7 @@ impl Aggregator {
last_sealed_batch: L1BatchNumber,
base_system_contracts_hashes: BaseSystemContractsHashes,
protocol_version_id: ProtocolVersionId,
is_validium: bool,
) -> Option<L1BatchCommitOperation> {
let mut blocks_dal = storage.blocks_dal();
let last_committed_l1_batch = blocks_dal
Expand Down Expand Up @@ -218,6 +223,7 @@ impl Aggregator {
batches.map(|batches| L1BatchCommitOperation {
last_committed_l1_batch,
l1_batches: batches,
is_validium,
})
}

Expand Down
7 changes: 4 additions & 3 deletions core/lib/zksync_core/src/eth_sender/publish_criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl L1BatchPublishCriterion for GasCriterion {
pub struct DataSizeCriterion {
pub op: AggregatedActionType,
pub data_limit: usize,
pub validium: bool,
}

#[async_trait]
Expand All @@ -216,12 +217,12 @@ impl L1BatchPublishCriterion for DataSizeCriterion {
let mut data_size_left = self.data_limit - STORED_BLOCK_INFO_SIZE;

for (index, l1_batch) in consecutive_l1_batches.iter().enumerate() {
if data_size_left < l1_batch.l1_commit_data_size() {
if data_size_left < l1_batch.l1_commit_data_size(self.validium) {
if index == 0 {
panic!(
"L1 batch #{} requires {} data, which is more than the range limit of {}",
l1_batch.header.number,
l1_batch.l1_commit_data_size(),
l1_batch.l1_commit_data_size(self.validium),
self.data_limit
);
}
Expand All @@ -237,7 +238,7 @@ impl L1BatchPublishCriterion for DataSizeCriterion {
METRICS.block_aggregation_reason[&(self.op, "data_size").into()].inc();
return Some(output);
}
data_size_left -= l1_batch.l1_commit_data_size();
data_size_left -= l1_batch.l1_commit_data_size(self.validium);
}

None
Expand Down
11 changes: 11 additions & 0 deletions core/lib/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::upper_case_acronyms, clippy::derive_partial_eq_without_eq)]

use std::env;
use std::{net::Ipv4Addr, str::FromStr, sync::Arc, time::Instant};

use anyhow::Context as _;
Expand Down Expand Up @@ -583,11 +584,21 @@ pub async fn initialize_components(
let eth_client =
PKSigningClient::from_config(&eth_sender, &contracts_config, &eth_client_config);
let nonce = eth_client.pending_nonce("eth_sender").await.unwrap();
let validium: bool = env::var("VALIDIUM")
.map(|v| {
println!("validium env var is {}", v);
match v.as_str() {
"true" => true,
_ => false,
}
})
.unwrap_or(false);
let eth_tx_aggregator_actor = EthTxAggregator::new(
eth_sender.sender.clone(),
Aggregator::new(
eth_sender.sender.clone(),
store_factory.create_store().await,
validium,
),
contracts_config.validator_timelock_addr,
contracts_config.l1_multicall3_addr,
Expand Down
10 changes: 8 additions & 2 deletions infrastructure/zk/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export async function deployL1(args: any[]) {
await utils.spawn(`${baseCommand} deploy-no-build ${args.join(' ')} | tee deployL1.log`);
const deployLog = fs.readFileSync('deployL1.log').toString();
const envVars = [
'VALIDIUM',
'CONTRACTS_CREATE2_FACTORY_ADDR',
'CONTRACTS_ADMIN_FACET_ADDR',
'CONTRACTS_DIAMOND_UPGRADE_INIT_ADDR',
Expand Down Expand Up @@ -152,8 +153,13 @@ export async function deployL1(args: any[]) {
fs.writeFileSync('deployed_contracts.log', updatedContracts);
}

export async function redeployL1(args: any[]) {
await deployL1(args);
export async function redeployL1(args: any[], validium: boolean) {
if (validium) {
await deployL1([...args, '--validium']);
} else {
await deployL1(args);
}

await verifyL1Contracts();
}

Expand Down
1 change: 1 addition & 0 deletions infrastructure/zk/src/hyperchain_wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async function initHyperchain() {
const initArgs: InitArgs = {
skipSubmodulesCheckout: false,
skipEnvSetup: true,
validium: false,
deployerL1ContractInputArgs: ['--private-key', deployerPrivateKey, '--governor-address', governorAddress],
governorPrivateKeyArgs: ['--private-key', governorPrivateKey],
deployerL2ContractInput: {
Expand Down
24 changes: 17 additions & 7 deletions infrastructure/zk/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function init(initArgs: InitArgs = DEFAULT_ARGS) {
const {
skipSubmodulesCheckout,
skipEnvSetup,
validium,
testTokens,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
deployerL1ContractInputArgs,
Expand Down Expand Up @@ -51,8 +52,8 @@ export async function init(initArgs: InitArgs = DEFAULT_ARGS) {
}
await announced('Deploying L1 verifier', contract.deployVerifier([]));
await announced('Reloading env', env.reload());
await announced('Deploying L1 contracts', contract.redeployL1(governorPrivateKeyArgs, validium));
await announced('Running server genesis setup', server.genesisFromSources());
await announced('Deploying L1 contracts', contract.redeployL1(governorPrivateKeyArgs));
await announced('Initializing validator', contract.initializeValidator(governorPrivateKeyArgs));
await announced('Initialize L1 allow list', contract.initializeL1AllowList(governorPrivateKeyArgs));
await announced(
Expand All @@ -72,7 +73,7 @@ export async function init(initArgs: InitArgs = DEFAULT_ARGS) {

// A smaller version of `init` that "resets" the localhost environment, for which `init` was already called before.
// It does less and runs much faster.
export async function reinit() {
export async function reinit(validium: boolean) {
await announced('Setting up containers', up());
await announced('Compiling JS packages', run.yarn());
await announced('Compile l2 contracts', compiler.compileAll());
Expand All @@ -84,7 +85,7 @@ export async function reinit() {
await announced('Deploying L1 verifier', contract.deployVerifier([]));
await announced('Reloading env', env.reload());
await announced('Running server genesis setup', server.genesisFromSources());
await announced('Deploying L1 contracts', contract.redeployL1([]));
await announced('Deploying L1 contracts', contract.redeployL1([], validium));
await announced('Initializing L1 Allow list', contract.initializeL1AllowList());
await announced('Deploying L2 contracts', contract.deployL2([], true, true));
await announced('Initializing L2 WETH token', contract.initializeWethToken());
Expand All @@ -93,13 +94,13 @@ export async function reinit() {
}

// A lightweight version of `init` that sets up local databases, generates genesis and deploys precompiled contracts
export async function lightweightInit() {
export async function lightweightInit(validium: boolean) {
await announced('Clean rocksdb', clean('db'));
await announced('Clean backups', clean('backups'));
await announced('Deploying L1 verifier', contract.deployVerifier([]));
await announced('Reloading env', env.reload());
await announced('Running server genesis setup', server.genesisFromBinary());
await announced('Deploying L1 contracts', contract.redeployL1([]));
await announced('Deploying L1 contracts', contract.redeployL1([], validium));
await announced('Initializing validator', contract.initializeValidator());
await announced('Initializing L1 Allow list', contract.initializeL1AllowList());
await announced('Deploying L2 contracts', contract.deployL2([], true, false));
Expand Down Expand Up @@ -144,6 +145,7 @@ async function checkEnv() {
export interface InitArgs {
skipSubmodulesCheckout: boolean;
skipEnvSetup: boolean;
validium: boolean;
deployerL1ContractInputArgs: any[];
governorPrivateKeyArgs: any[];
deployerL2ContractInput: {
Expand All @@ -160,6 +162,7 @@ export interface InitArgs {
const DEFAULT_ARGS: InitArgs = {
skipSubmodulesCheckout: false,
skipEnvSetup: false,
validium: false,
deployerL1ContractInputArgs: [],
governorPrivateKeyArgs: [],
deployerL2ContractInput: { args: [], includePaymaster: true, includeL2WETH: true },
Expand All @@ -169,11 +172,13 @@ const DEFAULT_ARGS: InitArgs = {
export const initCommand = new Command('init')
.option('--skip-submodules-checkout')
.option('--skip-env-setup')
.option('--validium')
.description('perform zksync network initialization for development')
.action(async (cmd: Command) => {
const initArgs: InitArgs = {
skipSubmodulesCheckout: cmd.skipSubmodulesCheckout,
skipEnvSetup: cmd.skipEnvSetup,
validium: cmd.validium,
deployerL1ContractInputArgs: [],
governorPrivateKeyArgs: [],
deployerL2ContractInput: { args: [], includePaymaster: true, includeL2WETH: true },
Expand All @@ -182,8 +187,13 @@ export const initCommand = new Command('init')
await init(initArgs);
});
export const reinitCommand = new Command('reinit')
.option('--validium')
.description('"reinitializes" network. Runs faster than `init`, but requires `init` to be executed prior')
.action(reinit);
.action(async (cmd: Command) => {
await reinit(cmd.validium);
});
export const lightweightInitCommand = new Command('lightweight-init')
.description('perform lightweight zksync network initialization for development')
.action(lightweightInit);
.action(async (cmd: Command) => {
await lightweightInit(cmd.validium);
});
6 changes: 3 additions & 3 deletions infrastructure/zk/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function server(rebuildTree: boolean, uring: boolean, components?:
if (components) {
options += ` --components=${components}`;
}
await utils.spawn(`cargo run --bin zksync_server ${options}`);
await utils.spawn(`cargo run --bin zksync_server --release ${options}`);
}

export async function externalNode(reinit: boolean = false) {
Expand All @@ -45,7 +45,7 @@ export async function externalNode(reinit: boolean = false) {
clean(path.dirname(process.env.EN_MERKLE_TREE_PATH!));
}

await utils.spawn('cargo run --bin zksync_external_node');
await utils.spawn('cargo run --release --bin zksync_external_node');
}

async function create_genesis(cmd: string) {
Expand Down Expand Up @@ -111,7 +111,7 @@ async function create_genesis(cmd: string) {
}

export async function genesisFromSources() {
await create_genesis('cargo run --bin zksync_server -- --genesis');
await create_genesis('cargo run --bin zksync_server --release -- --genesis');
}

export async function genesisFromBinary() {
Expand Down
Loading

0 comments on commit 017cc81

Please sign in to comment.