Skip to content

Commit

Permalink
chore: message-root-support (#2706)
Browse files Browse the repository at this point in the history
## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## 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`.

---------

Co-authored-by: Stanislav Breadless <[email protected]>
  • Loading branch information
kelemeno and StanislavBreadless authored Aug 30, 2024
1 parent 07fdecc commit 91f3cdc
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 235 deletions.
2 changes: 1 addition & 1 deletion contracts
Submodule contracts updated 78 files
+1 −1 .github/workflows/l1-contracts-ci.yaml
+37 −7 .github/workflows/l2-contracts-ci.yaml
+3 −0 .gitmodules
+1 −0 .solhintignore
+7 −6 l1-contracts/contracts/bridge/L1AssetRouter.sol
+1 −3 l1-contracts/contracts/bridge/L1NativeTokenVault.sol
+2 −7 l1-contracts/contracts/bridge/interfaces/IL1AssetRouter.sol
+75 −37 l1-contracts/contracts/bridgehub/Bridgehub.sol
+14 −0 l1-contracts/contracts/bridgehub/IBridgehub.sol
+0 −2 l1-contracts/contracts/bridgehub/IMessageRoot.sol
+10 −23 l1-contracts/contracts/bridgehub/MessageRoot.sol
+6 −1 l1-contracts/contracts/bridgehub/STMDeploymentTracker.sol
+4 −1 l1-contracts/contracts/common/L2ContractAddresses.sol
+3 −3 l1-contracts/contracts/common/libraries/DataEncoding.sol
+2 −2 l1-contracts/contracts/dev-contracts/test/DummyBridgehub.sol
+3 −3 l1-contracts/contracts/dev-contracts/test/DummySharedBridge.sol
+3 −3 l1-contracts/contracts/state-transition/IStateTransitionManager.sol
+14 −13 l1-contracts/contracts/state-transition/StateTransitionManager.sol
+51 −11 l1-contracts/contracts/state-transition/chain-deps/facets/Admin.sol
+3 −3 l1-contracts/contracts/state-transition/chain-interfaces/IAdmin.sol
+20 −0 l1-contracts/contracts/state-transition/libraries/PriorityTree.sol
+12 −4 l1-contracts/deploy-scripts/Gateway.s.sol
+17 −3 l1-contracts/deploy-scripts/GenerateForceDeploymentsData.s.sol
+2 −2 l1-contracts/package.json
+2 −4 l1-contracts/scripts/sync-layer.ts
+16 −4 l1-contracts/src.ts/deploy.ts
+1 −2 l1-contracts/src.ts/utils.ts
+90 −4 l1-contracts/test/foundry/integration/GatewayTests.t.sol
+23 −23 l1-contracts/test/foundry/integration/deploy-scripts/script-out/output-deploy-l1.toml
+4 −2 l1-contracts/test/foundry/unit/concrete/Bridgehub/MessageRoot.t.sol
+2 −4 l1-contracts/test/foundry/unit/concrete/Bridgehub/experimental_bridge.t.sol
+8 −8 l1-contracts/test/foundry/unit/concrete/Bridges/L1SharedBridge/L1SharedBridgeBase.t.sol
+11 −11 l1-contracts/test/foundry/unit/concrete/Bridges/L1SharedBridge/L1SharedBridgeHyperEnabled.t.sol
+4 −4 l1-contracts/test/foundry/unit/concrete/Bridges/L1SharedBridge/L1SharedBridgeLegacy.t.sol
+4 −9 l1-contracts/test/foundry/unit/concrete/Bridges/L1SharedBridge/_L1SharedBridge_Shared.t.sol
+2 −1 l1-contracts/test/unit_tests/gateway.spec.ts
+1 −1 l2-contracts/contracts/ForceDeployUpgrader.sol
+36 −0 l2-contracts/contracts/L2ContractHelper.sol
+1 −1 l2-contracts/contracts/bridge/L2AssetRouter.sol
+1 −1 l2-contracts/contracts/bridge/L2NativeTokenVault.sol
+1 −1 l2-contracts/contracts/bridge/L2SharedBridgeLegacy.sol
+1 −1 l2-contracts/contracts/bridge/L2StandardERC20.sol
+1 −1 l2-contracts/contracts/bridge/L2WrappedBaseToken.sol
+1 −1 l2-contracts/contracts/bridge/interfaces/IL2AssetHandler.sol
+1 −1 l2-contracts/contracts/bridge/interfaces/IL2AssetRouter.sol
+1 −1 l2-contracts/contracts/bridge/interfaces/IL2NativeTokenVault.sol
+1 −1 l2-contracts/contracts/bridge/interfaces/IL2SharedBridgeLegacy.sol
+1 −1 l2-contracts/contracts/common/libraries/DataEncoding.sol
+1 −1 l2-contracts/contracts/data-availability/DAErrors.sol
+1 −1 l2-contracts/contracts/data-availability/RollupL2DAValidator.sol
+1 −1 l2-contracts/contracts/data-availability/StateDiffL2DAValidator.sol
+1 −1 l2-contracts/contracts/data-availability/ValidiumL2DAValidator.sol
+70 −0 l2-contracts/contracts/dev-contracts/VerifierRecursiveTest.sol
+70 −0 l2-contracts/contracts/dev-contracts/VerifierTest.sol
+1 −1 l2-contracts/contracts/interfaces/IL2DAValidator.sol
+1,710 −0 l2-contracts/contracts/verifier/Verifier.sol
+28 −0 l2-contracts/contracts/verifier/chain-interfaces/IVerifier.sol
+18 −4 l2-contracts/foundry.toml
+2 −2 l2-contracts/hardhat.config.ts
+1 −0 l2-contracts/lib/@matterlabs
+0 −1 l2-contracts/lib/openzeppelin-contracts
+0 −1 l2-contracts/lib/openzeppelin-contracts-upgradeable
+1 −0 l2-contracts/lib/openzeppelin-contracts-upgradeable-v4
+1 −0 l2-contracts/lib/openzeppelin-contracts-v4
+1 −0 l2-contracts/package.json
+1 −36 l2-contracts/src/deploy-shared-bridge-on-l2-through-l1.ts
+0 −179 l2-contracts/test/erc20.test.ts
+135 −0 l2-contracts/test/foundry/unit/erc20/L2Erc20BridgeTest.t.sol
+156 −0 l2-contracts/test/foundry/unit/utils/Utils.sol
+197 −0 l2-contracts/test/foundry/unit/verifier/Verifier.t.sol
+55 −0 l2-contracts/test/foundry/unit/verifier/VerifierRecursive.t.sol
+118 −0 l2-contracts/test/foundry/unit/weth/WETH.t.sol
+0 −57 l2-contracts/test/test-utils.ts
+0 −132 l2-contracts/test/weth.test.ts
+1 −0 lib/@matterlabs/zksync-contracts
+8 −0 tools/README.md
+1 −12 tools/data/verifier_contract_template.txt
+46 −2 tools/src/main.rs
198 changes: 99 additions & 99 deletions core/lib/multivm/src/versions/vm_latest/tests/constants.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use ethabi::Token;
use itertools::Itertools;
use zksync_types::{
get_immutable_key, get_l2_message_root_init_logs, AccountTreeId, StorageKey, StorageLog,
StorageLogKind, H256, IMMUTABLE_SIMULATOR_STORAGE_ADDRESS, L2_BRIDGEHUB_ADDRESS,
L2_MESSAGE_ROOT_ADDRESS,
};
// use ethabi::Token;
// use itertools::Itertools;
// use zksync_types::{
// get_immutable_key, get_l2_message_root_init_logs, AccountTreeId, StorageKey, StorageLog,
// StorageLogKind, H256, IMMUTABLE_SIMULATOR_STORAGE_ADDRESS, L2_BRIDGEHUB_ADDRESS,
// L2_MESSAGE_ROOT_ADDRESS,
// };

use crate::{
interface::{TxExecutionMode, VmExecutionMode, VmInterface},
vm_latest::{
tests::{
tester::{DeployContractsTx, TxType, VmTesterBuilder},
utils::read_message_root,
},
HistoryEnabled,
},
vm_m5::storage::Storage,
};
// use crate::{
// // interface::{TxExecutionMode, VmExecutionMode, VmInterface},
// vm_latest::{
// tests::{
// tester::{DeployContractsTx, TxType, VmTesterBuilder},
// utils::read_message_root,
// },
// HistoryEnabled,
// },
// vm_m5::storage::Storage,
// };
/// Some of the constants of the system are implicitly calculated, but they may affect the code and so
/// we added additional checks on them to keep any unwanted changes of those apparent.
#[test]
Expand All @@ -27,94 +27,94 @@ fn test_that_bootloader_encoding_space_is_large_enoguh() {
assert!(encoding_space >= 330000, "Bootloader tx space is too small");
}

/// Test that checks that the initial logs for the L2 Message Root are correct
#[test]
fn test_l2_message_root_init_logs() {
let mut vm = VmTesterBuilder::new(HistoryEnabled)
.with_empty_in_memory_storage()
.with_execution_mode(TxExecutionMode::VerifyExecute)
.with_random_rich_accounts(1)
.build();
// Test that checks that the initial logs for the L2 Message Root are correct
// #[test]
// fn test_l2_message_root_init_logs() {
// let mut vm = VmTesterBuilder::new(HistoryEnabled)
// .with_empty_in_memory_storage()
// .with_execution_mode(TxExecutionMode::VerifyExecute)
// .with_random_rich_accounts(1)
// .build();

let message_root_bytecode = read_message_root();
let account = &mut vm.rich_accounts[0];
let DeployContractsTx { tx, address, .. } = account.get_deploy_tx(
&message_root_bytecode,
Some(&[Token::Address(L2_BRIDGEHUB_ADDRESS)]),
TxType::L2,
);
// let message_root_bytecode = read_message_root();
// let account = &mut vm.rich_accounts[0];
// let DeployContractsTx { tx, address, .. } = account.get_deploy_tx(
// &message_root_bytecode,
// Some(&[Token::Address(L2_BRIDGEHUB_ADDRESS)]),
// TxType::L2,
// );

vm.vm.push_transaction(tx);
let result = vm.vm.execute(VmExecutionMode::OneTx);
assert!(!result.result.is_failed(), "Transaction wasn't successful");
// vm.vm.push_transaction(tx);
// let result = vm.vm.execute(VmExecutionMode::OneTx);
// assert!(!result.result.is_failed(), "Transaction wasn't successful");

// That's the only key in the immutable simulator that should be changed. It depends on the address
// of the deployed contract, so we check that the way it was generated for a random deployed contract is the same.
let expected_change_immutable_key = get_immutable_key(&address, H256::zero());
let expected_genesis_immutable_key = get_immutable_key(&L2_MESSAGE_ROOT_ADDRESS, H256::zero());
// // That's the only key in the immutable simulator that should be changed. It depends on the address
// // of the deployed contract, so we check that the way it was generated for a random deployed contract is the same.
// let expected_change_immutable_key = get_immutable_key(&address, H256::zero());
// let expected_genesis_immutable_key = get_immutable_key(&L2_MESSAGE_ROOT_ADDRESS, H256::zero());

let mut expected_init_logs = get_l2_message_root_init_logs()
.into_iter()
.map(|x| StorageLog {
// We unify all the logs to all have the same kind
kind: StorageLogKind::InitialWrite,
key: x.key,
value: x.value,
})
.collect::<Vec<_>>();
// let mut expected_init_logs = get_l2_message_root_init_logs()
// .into_iter()
// .map(|x| StorageLog {
// // We unify all the logs to all have the same kind
// kind: StorageLogKind::InitialWrite,
// key: x.key,
// value: x.value,
// })
// .collect::<Vec<_>>();

let ordering = |a: &StorageLog, b: &StorageLog| match a.key.cmp(&b.key) {
std::cmp::Ordering::Equal => a.value.cmp(&b.value),
other => other,
};
// let ordering = |a: &StorageLog, b: &StorageLog| match a.key.cmp(&b.key) {
// std::cmp::Ordering::Equal => a.value.cmp(&b.value),
// other => other,
// };

expected_init_logs.sort_by(ordering);
// expected_init_logs.sort_by(ordering);

let correct_init_logs = vm
.vm
.storage
.borrow_mut()
.get_modified_storage_keys()
.iter()
.filter_map(|(&storage_key, &value)| {
if *storage_key.address() == address {
Some(StorageLog {
kind: StorageLogKind::InitialWrite,
key: StorageKey::new(
// Note, that it in the end we will compare those with the genesis logs that
// have the `L2_MESSAGE_ROOT_ADDRESS` as the address
AccountTreeId::new(L2_MESSAGE_ROOT_ADDRESS),
*storage_key.key(),
),
value,
})
} else if *storage_key.address() == IMMUTABLE_SIMULATOR_STORAGE_ADDRESS {
assert!(
*storage_key.key() == expected_change_immutable_key,
"Incorrect immutable key has been changed"
);
// let correct_init_logs = vm
// .vm
// .storage
// .borrow_mut()
// .get_modified_storage_keys()
// .iter()
// .filter_map(|(&storage_key, &value)| {
// if *storage_key.address() == address {
// Some(StorageLog {
// kind: StorageLogKind::InitialWrite,
// key: StorageKey::new(
// // Note, that it in the end we will compare those with the genesis logs that
// // have the `L2_MESSAGE_ROOT_ADDRESS` as the address
// AccountTreeId::new(L2_MESSAGE_ROOT_ADDRESS),
// *storage_key.key(),
// ),
// value,
// })
// } else if *storage_key.address() == IMMUTABLE_SIMULATOR_STORAGE_ADDRESS {
// assert!(
// *storage_key.key() == expected_change_immutable_key,
// "Incorrect immutable key has been changed"
// );

Some(StorageLog {
kind: StorageLogKind::InitialWrite,
key: StorageKey::new(
AccountTreeId::new(IMMUTABLE_SIMULATOR_STORAGE_ADDRESS),
// For comparison to work, we replace the immutable key with the one that is used for genesis
expected_genesis_immutable_key,
),
value,
})
} else {
None
}
})
.sorted_by(ordering)
.collect::<Vec<_>>();
// Some(StorageLog {
// kind: StorageLogKind::InitialWrite,
// key: StorageKey::new(
// AccountTreeId::new(IMMUTABLE_SIMULATOR_STORAGE_ADDRESS),
// // For comparison to work, we replace the immutable key with the one that is used for genesis
// expected_genesis_immutable_key,
// ),
// value,
// })
// } else {
// None
// }
// })
// .sorted_by(ordering)
// .collect::<Vec<_>>();

assert_eq!(expected_init_logs, correct_init_logs);
// assert_eq!(expected_init_logs, correct_init_logs);

let batch_result = vm.vm.execute(VmExecutionMode::Batch);
assert!(
!batch_result.result.is_failed(),
"Transaction wasn't successful"
);
}
// let batch_result = vm.vm.execute(VmExecutionMode::Batch);
// assert!(
// !batch_result.result.is_failed(),
// "Transaction wasn't successful"
// );
// }
10 changes: 5 additions & 5 deletions core/lib/multivm/src/versions/vm_latest/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ pub(crate) fn read_nonce_holder_tester() -> Vec<u8> {
read_bytecode("etc/contracts-test-data/artifacts-zk/contracts/custom-account/nonce-holder-test.sol/NonceHolderTest.json")
}

pub(crate) fn read_message_root() -> Vec<u8> {
read_bytecode(
"contracts/l1-contracts/artifacts-zk/contracts/bridgehub/MessageRoot.sol/MessageRoot.json",
)
}
// pub(crate) fn read_message_root() -> Vec<u8> {
// read_bytecode(
// "contracts/l1-contracts/artifacts-zk/contracts/bridgehub/MessageRoot.sol/MessageRoot.json",
// )
// }

pub(crate) fn read_error_contract() -> Vec<u8> {
read_bytecode(
Expand Down
33 changes: 27 additions & 6 deletions core/lib/types/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,28 +195,49 @@ pub struct L2ToL1LogProof {
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LeafAggProof {
pub leaf_chain_proof: LeafChainProof,
pub chain_agg_proof: ChainAggProof,
pub local_msg_root: H256,
pub sl_batch_number: U256,
pub sl_chain_id: U256,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LeafChainProof {
pub batch_leaf_proof: Vec<H256>,
pub batch_leaf_proof_mask: U256,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ChainAggProof {
pub chain_id_leaf_proof: Vec<H256>,
pub chain_id_leaf_proof_mask: U256,
pub local_msg_root: H256,
pub sl_batch_number: U256,
pub sl_chain_id: U256,
}

impl LeafAggProof {
pub fn encode(self) -> (u32, Vec<H256>) {
let mut encoded_result = vec![];

let LeafAggProof {
batch_leaf_proof,
batch_leaf_proof_mask,
chain_id_leaf_proof_mask,
leaf_chain_proof,
chain_agg_proof,
sl_batch_number,
sl_chain_id,
..
} = self;

let LeafChainProof {
batch_leaf_proof,
batch_leaf_proof_mask,
} = leaf_chain_proof;

let ChainAggProof {
chain_id_leaf_proof: _,
chain_id_leaf_proof_mask,
} = chain_agg_proof;

let batch_leaf_proof_len = batch_leaf_proof.len() as u32;

encoded_result.push(u256_to_h256(batch_leaf_proof_mask));
Expand Down
20 changes: 10 additions & 10 deletions core/lib/types/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ fn get_system_context_init_logs(chain_id: L2ChainId) -> Vec<StorageLog> {
/// and so for now the correctness of those values is tested in a unit tests within the multivm crate.
pub fn get_l2_message_root_init_logs() -> Vec<StorageLog> {
let slots_values = vec![
(
"8e94fed44239eb2314ab7a406345e6c5a8f0ccedf3b600de3d004e672c33abf4",
"0000000000000000000000000000000000000000000000000000000000000001",
),
// (
// "8e94fed44239eb2314ab7a406345e6c5a8f0ccedf3b600de3d004e672c33abf4",
// "0000000000000000000000000000000000000000000000000000000000000001",
// ),
(
"0000000000000000000000000000000000000000000000000000000000000007",
"0000000000000000000000000000000000000000000000000000000000000001",
Expand Down Expand Up @@ -199,11 +199,11 @@ pub fn get_l2_message_root_init_logs() -> Vec<StorageLog> {
}

pub fn get_system_contracts_init_logs(chain_id: L2ChainId) -> Vec<StorageLog> {
let system_context_init_logs = get_system_context_init_logs(chain_id);
let l2_message_root_init_logs = get_l2_message_root_init_logs();
get_system_context_init_logs(chain_id)
// let l2_message_root_init_logs = get_l2_message_root_init_logs();

system_context_init_logs
.into_iter()
.chain(l2_message_root_init_logs)
.collect()
// system_context_init_logs
// .into_iter()
// .chain(l2_message_root_init_logs)
// .collect()
}
Loading

0 comments on commit 91f3cdc

Please sign in to comment.