Skip to content

Commit

Permalink
Emit UpgradeComplete event in data_restore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slumber committed May 23, 2021
1 parent 1aff80a commit 733dd38
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
6 changes: 5 additions & 1 deletion core/bin/data_restore/src/events_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ mod test {
use super::EventsState;
use web3::{
api::{Eth, Namespace},
types::Bytes,
types::{Bytes, H160},
};

use crate::contract::{ZkSyncContractVersion, ZkSyncDeployedContract};
Expand All @@ -461,6 +461,7 @@ mod test {
let mut events_state = EventsState::default();

let contract = ZkSyncDeployedContract::version4(Eth::new(FakeTransport), [1u8; 20].into());
let contract_addr = H160::from([1u8; 20]);

let block_verified_topic = contract
.abi
Expand All @@ -481,13 +482,15 @@ mod test {
let mut logs = vec![];
for i in 0..32 {
logs.push(create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(i).into()],
Bytes(vec![]),
i,
u32_to_32bytes(i).into(),
));
logs.push(create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(i).into()],
Bytes(vec![]),
Expand All @@ -508,6 +511,7 @@ mod test {
data.extend(&last_block_com);
data.extend(&last_block_ver);
let log = create_log(
contract_addr,
reverted_topic,
vec![u32_to_32bytes(3).into()],
Bytes(data),
Expand Down
76 changes: 57 additions & 19 deletions core/bin/data_restore/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ use num::BigUint;
use serde_json::{json, Value};
use web3::{
contract::tokens::Tokenize,
types::{Bytes, Transaction},
types::{Bytes, Transaction, H160},
RequestId, Transport, Web3,
};

use db_test_macro::test as db_test;
use zksync_config::ContractsConfig;
use zksync_contracts::{governance_contract, zksync_contract};
use zksync_contracts::{governance_contract, upgrade_gatekeeper, zksync_contract};
use zksync_crypto::Fr;
use zksync_storage::{
chain::account::AccountSchema, data_restore::DataRestoreSchema, StorageProcessor,
Expand Down Expand Up @@ -281,9 +280,12 @@ impl Transport for Web3Transport {

#[db_test]
async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
let mut transport = Web3Transport::new();
let contract_addr = H160::from([1u8; 20]);
let upgrade_gatekeeper_addr = H160::from([2u8; 20]);
// Use old contract version.
let init_contract_version: u32 = 3;

let contracts_config = ContractsConfig::from_env();
let mut transport = Web3Transport::new();

let mut interactor = DatabaseStorageInteractor::new(storage);
let contract = zksync_contract();
Expand All @@ -298,13 +300,15 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
block_verified_topic_string,
vec![
create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(1).into()],
Bytes(vec![]),
1,
u32_to_32bytes(1).into(),
),
create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(2).into()],
Bytes(vec![]),
Expand All @@ -323,13 +327,15 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
block_commit_topic_string,
vec![
create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(1).into()],
Bytes(vec![]),
1,
u32_to_32bytes(1).into(),
),
create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(2).into()],
Bytes(vec![]),
Expand All @@ -353,6 +359,7 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
transport.insert_logs(
new_token_topic_string,
vec![create_log(
contract_addr,
new_token_topic,
vec![[0; 32].into(), u32_to_32bytes(3).into()],
Bytes(vec![]),
Expand Down Expand Up @@ -386,9 +393,9 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
let eth = Eth::new(transport.clone());
let mut driver = DataRestoreDriver::new(
Web3::new(transport.clone()),
[1u8; 20].into(),
contracts_config.upgrade_gatekeeper_addr,
contracts_config.init_contract_version,
contract_addr,
upgrade_gatekeeper_addr,
init_contract_version,
ETH_BLOCKS_STEP,
END_ETH_BLOCKS_OFFSET,
true,
Expand Down Expand Up @@ -421,9 +428,9 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {

let mut driver = DataRestoreDriver::new(
Web3::new(transport.clone()),
[1u8; 20].into(),
contracts_config.upgrade_gatekeeper_addr,
contracts_config.init_contract_version,
contract_addr,
upgrade_gatekeeper_addr,
init_contract_version,
ETH_BLOCKS_STEP,
END_ETH_BLOCKS_OFFSET,
true,
Expand All @@ -439,44 +446,53 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {

#[tokio::test]
async fn test_with_inmemory_storage() {
let mut transport = Web3Transport::new();
let contract_addr = H160::from([1u8; 20]);
let upgrade_gatekeeper_addr = H160::from([2u8; 20]);
// Start with V3, upgrade it after a couple of blocks to V4.
let init_contract_version: u32 = 3;

let contracts_config = ContractsConfig::from_env();
let mut transport = Web3Transport::new();

let mut interactor = InMemoryStorageInteractor::new();
let contract = zksync_contract();
let gov_contract = governance_contract();
let upgrade_gatekeeper = upgrade_gatekeeper();

let block_verified_topic = contract
.event("BlockVerification")
.expect("Main contract abi error")
.signature();
let block_verified_topic_string = format!("{:?}", block_verified_topic);
// Starting from Eth block number 3 the version is upgraded.
transport.insert_logs(
block_verified_topic_string,
vec![
create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(1).into()],
Bytes(vec![]),
1,
u32_to_32bytes(1).into(),
),
create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(2).into()],
Bytes(vec![]),
2,
u32_to_32bytes(2).into(),
),
create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(3).into()],
Bytes(vec![]),
3,
u32_to_32bytes(3).into(),
),
create_log(
contract_addr,
block_verified_topic,
vec![u32_to_32bytes(4).into()],
Bytes(vec![]),
Expand All @@ -485,6 +501,23 @@ async fn test_with_inmemory_storage() {
),
],
);
// Save the event about finished upgrade in Eth block number 3.
// Additional topics and data don't matter.
let upgrade_complete_topic = upgrade_gatekeeper
.event("UpgradeComplete")
.expect("Upgrade gatekeeper abi error")
.signature();
transport.insert_logs(
format!("{:?}", upgrade_complete_topic),
vec![create_log(
upgrade_gatekeeper_addr,
upgrade_complete_topic,
Vec::new(),
Bytes(Vec::new()),
3,
H256::zero(),
)],
);

let block_committed_topic = contract
.event("BlockCommit")
Expand All @@ -495,27 +528,31 @@ async fn test_with_inmemory_storage() {
block_commit_topic_string,
vec![
create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(1).into()],
Bytes(vec![]),
1,
u32_to_32bytes(1).into(),
),
create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(2).into()],
Bytes(vec![]),
2,
u32_to_32bytes(2).into(),
),
create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(3).into()],
Bytes(vec![]),
3,
u32_to_32bytes(3).into(),
),
create_log(
contract_addr,
block_committed_topic,
vec![u32_to_32bytes(4).into()],
Bytes(vec![]),
Expand All @@ -539,6 +576,7 @@ async fn test_with_inmemory_storage() {
transport.insert_logs(
new_token_topic_string,
vec![create_log(
contract_addr,
new_token_topic,
vec![[0; 32].into(), u32_to_32bytes(3).into()],
Bytes(vec![]),
Expand Down Expand Up @@ -596,9 +634,9 @@ async fn test_with_inmemory_storage() {
let eth = Eth::new(transport.clone());
let mut driver = DataRestoreDriver::new(
web3.clone(),
[1u8; 20].into(),
contracts_config.upgrade_gatekeeper_addr,
contracts_config.init_contract_version,
contract_addr,
upgrade_gatekeeper_addr,
init_contract_version,
ETH_BLOCKS_STEP,
END_ETH_BLOCKS_OFFSET,
true,
Expand All @@ -624,9 +662,9 @@ async fn test_with_inmemory_storage() {
let eth = Eth::new(transport.clone());
let mut driver = DataRestoreDriver::new(
web3.clone(),
[1u8; 20].into(),
contracts_config.upgrade_gatekeeper_addr,
contracts_config.init_contract_version,
contract_addr,
upgrade_gatekeeper_addr,
init_contract_version,
ETH_BLOCKS_STEP,
END_ETH_BLOCKS_OFFSET,
true,
Expand Down
5 changes: 3 additions & 2 deletions core/bin/data_restore/src/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::future::Future;

use web3::{
types::H256,
types::{Bytes, Log},
types::{H160, H256},
RequestId, Transport,
};

Expand Down Expand Up @@ -34,6 +34,7 @@ pub(crate) fn u32_to_32bytes(value: u32) -> [u8; 32] {
}

pub(crate) fn create_log(
address: H160,
topic: H256,
additional_topics: Vec<H256>,
data: Bytes,
Expand All @@ -43,7 +44,7 @@ pub(crate) fn create_log(
let mut topics = vec![topic];
topics.extend(additional_topics);
Log {
address: [1u8; 20].into(),
address,
topics,
data,
block_hash: None,
Expand Down

0 comments on commit 733dd38

Please sign in to comment.