Skip to content

Commit

Permalink
Fix exit tool
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Apr 22, 2021
1 parent 27a7309 commit c570c43
Show file tree
Hide file tree
Showing 32 changed files with 1,298 additions and 171 deletions.
4 changes: 3 additions & 1 deletion core/bin/data_restore/src/contract/default.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethabi::ParamType;

use zksync_types::{AccountId, BlockNumber, ZkSyncOp};
use zksync_types::{AccountId, BlockNumber, ZkSyncOp, H256};

use crate::rollup_ops::RollupOpsBlock;

Expand Down Expand Up @@ -43,6 +43,8 @@ pub fn rollup_ops_blocks_from_bytes(input_data: Vec<u8>) -> Result<RollupOpsBloc
block_num: BlockNumber(block_num.as_u32()),
ops,
fee_account,
timestamp: None,
previous_block_root_hash: H256::default(),
};
Ok(block)
} else {
Expand Down
47 changes: 38 additions & 9 deletions core/bin/data_restore/src/contract/v4.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ethabi::{ParamType, Token};

use crate::{contract::default::get_rollup_ops_from_data, rollup_ops::RollupOpsBlock};
use zksync_types::{AccountId, BlockNumber};
use zksync_types::{AccountId, BlockNumber, H256};

fn decode_commitment_parameters(input_data: Vec<u8>) -> anyhow::Result<Vec<Token>> {
let commit_operation = ParamType::Tuple(vec![
Expand All @@ -16,12 +16,12 @@ fn decode_commitment_parameters(input_data: Vec<u8>) -> anyhow::Result<Vec<Token
Box::new(ParamType::Uint(32)), // uint32 _feeAccount,
]);
let stored_block = ParamType::Tuple(vec![
Box::new(ParamType::Uint(32)), // uint32 _block_number
Box::new(ParamType::Uint(64)), // uint32 _number_of_processed_prior_ops
Box::new(ParamType::FixedBytes(32)), // bytes32 processable_ops_hash
Box::new(ParamType::Uint(32)), // uint32 blockNumber
Box::new(ParamType::Uint(64)), // uint32 priorityOperations
Box::new(ParamType::FixedBytes(32)), // bytes32 pendingOnchainOperationsHash
Box::new(ParamType::Uint(256)), // uint256 timestamp
Box::new(ParamType::FixedBytes(32)), // bytes32 eth_encoded_root
Box::new(ParamType::FixedBytes(32)), // commitment
Box::new(ParamType::FixedBytes(32)), // bytes32 stateHash
Box::new(ParamType::FixedBytes(32)), // bytes32 commitment
]);
ethabi::decode(
vec![stored_block, ParamType::Array(Box::new(commit_operation))].as_slice(),
Expand All @@ -36,13 +36,34 @@ fn decode_commitment_parameters(input_data: Vec<u8>) -> anyhow::Result<Vec<Token
}

pub fn rollup_ops_blocks_from_bytes(data: Vec<u8>) -> anyhow::Result<Vec<RollupOpsBlock>> {
let fee_account_argument_id = 5;
let op_block_number_argument_id = 4;
let root_hash_argument_id = 0;
let public_data_argument_id = 1;
let timestamp_argument_id = 2;
let op_block_number_argument_id = 4;
let fee_account_argument_id = 5;

// ID of `eth_encoded_root` in `StoredBlockInfo`.
let previous_block_root_hash_argument_id = 4;

let decoded_commitment_parameters = decode_commitment_parameters(data)?;
assert_eq!(decoded_commitment_parameters.len(), 2);

let mut previous_block_root_hash =
if let ethabi::Token::Tuple(prev_stored) = &decoded_commitment_parameters[0] {
if let ethabi::Token::FixedBytes(root_hash) =
&prev_stored[previous_block_root_hash_argument_id]
{
H256::from_slice(&root_hash)
} else {
panic!("can't parse root hash param: {:#?}", prev_stored);
}
} else {
panic!(
"can't parse root hash param: {:#?}",
decoded_commitment_parameters
);
};

// Destruct deserialized parts of transaction input data for getting operations
// Input data consists of stored block and operations
// Transform operations to RollupBlock
Expand All @@ -51,20 +72,28 @@ pub fn rollup_ops_blocks_from_bytes(data: Vec<u8>) -> anyhow::Result<Vec<RollupO
for operation in operations {
if let ethabi::Token::Tuple(operation) = operation {
if let (
ethabi::Token::FixedBytes(root_hash),
ethabi::Token::Uint(fee_acc),
ethabi::Token::Bytes(public_data),
ethabi::Token::Uint(block_number),
ethabi::Token::Uint(timestamp),
) = (
&operation[root_hash_argument_id],
&operation[fee_account_argument_id],
&operation[public_data_argument_id],
&operation[op_block_number_argument_id],
&operation[timestamp_argument_id],
) {
let ops = get_rollup_ops_from_data(public_data.as_slice())?;
blocks.push(RollupOpsBlock {
block_num: BlockNumber(block_number.as_u32()),
ops,
fee_account: AccountId(fee_acc.as_u32()),
})
timestamp: Some(timestamp.as_u64()),
previous_block_root_hash,
});

previous_block_root_hash = H256::from_slice(&root_hash);
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
Expand Down
5 changes: 4 additions & 1 deletion core/bin/data_restore/src/data_restore_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct DataRestoreDriver<T: Transport, I> {
/// Expected root hash to be observed after restoring process. Only
/// available in finite mode, and intended for tests.
pub final_hash: Option<Fr>,
pub available_block_chunk_sizes: Vec<usize>,
phantom_data: PhantomData<I>,
}

Expand Down Expand Up @@ -98,6 +99,7 @@ where
finite_mode: bool,
final_hash: Option<Fr>,
zksync_contract: ZkSyncDeployedContract<T>,
available_block_chunk_sizes: Vec<usize>,
) -> Self {
let governance_contract = {
let abi = governance_contract();
Expand All @@ -121,6 +123,7 @@ where
finite_mode,
final_hash,
phantom_data: Default::default(),
available_block_chunk_sizes,
}
}

Expand Down Expand Up @@ -356,7 +359,7 @@ where
for op_block in new_ops_blocks {
let (block, acc_updates) = self
.tree_state
.update_tree_states_from_ops_block(&op_block)
.update_tree_states_from_ops_block(&op_block, &self.available_block_chunk_sizes)
.expect("Updating tree state: cant update tree from operations");
blocks.push(block);
updates.push(acc_updates);
Expand Down
12 changes: 9 additions & 3 deletions core/bin/data_restore/src/database_storage_interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;
use zksync_storage::{data_restore::records::NewBlockEvent, StorageProcessor};
use zksync_types::{
aggregated_operations::{BlocksCommitOperation, BlocksExecuteOperation},
AccountId, BlockNumber, Token, TokenGenesisListItem, TokenId,
AccountId, BlockNumber, Token, TokenGenesisListItem, TokenId, H256,
{block::Block, AccountUpdate, AccountUpdates, ZkSyncOp},
};

Expand Down Expand Up @@ -61,11 +61,17 @@ impl<'a> DatabaseStorageInteractor<'a> {
#[async_trait::async_trait]
impl StorageInteractor for DatabaseStorageInteractor<'_> {
async fn save_rollup_ops(&mut self, blocks: &[RollupOpsBlock]) {
let mut ops: Vec<(BlockNumber, &ZkSyncOp, AccountId)> = vec![];
let mut ops: Vec<(BlockNumber, &ZkSyncOp, AccountId, Option<u64>, H256)> = vec![];

for block in blocks {
for op in &block.ops {
ops.push((block.block_num, op, block.fee_account));
ops.push((
block.block_num,
op,
block.fee_account,
block.timestamp,
block.previous_block_root_hash,
));
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/bin/data_restore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub struct ContractsConfig {
governance_addr: Address,
genesis_tx_hash: H256,
contract_addr: Address,
upgrade_gatekeeper_addr: Address,
available_block_chunk_sizes: Vec<usize>,
}

Expand All @@ -72,7 +71,6 @@ impl ContractsConfig {
governance_addr: contracts_opts.governance_addr,
genesis_tx_hash: contracts_opts.genesis_tx_hash,
contract_addr: contracts_opts.contract_addr,
upgrade_gatekeeper_addr: contracts_opts.upgrade_gatekeeper_addr,
available_block_chunk_sizes: chain_opts.state_keeper.block_chunk_sizes,
}
}
Expand All @@ -96,6 +94,8 @@ async fn main() {
.map(|path| ContractsConfig::from_file(&path))
.unwrap_or_else(ContractsConfig::from_env);

vlog::info!("Using the following config: {:#?}", config);

let finite_mode = opt.finite;
let final_hash = if finite_mode {
opt.final_hash
Expand All @@ -114,6 +114,7 @@ async fn main() {
finite_mode,
final_hash,
contract,
config.available_block_chunk_sizes,
);

let mut interactor = DatabaseStorageInteractor::new(storage);
Expand Down
6 changes: 5 additions & 1 deletion core/bin/data_restore/src/rollup_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zksync_types::operations::ZkSyncOp;
use crate::contract;
use crate::eth_tx_helpers::{get_ethereum_transaction, get_input_data_from_ethereum_transaction};
use crate::events::BlockEvent;
use zksync_types::{AccountId, BlockNumber};
use zksync_types::{AccountId, BlockNumber, H256};

/// Description of a Rollup operations block
#[derive(Debug, Clone)]
Expand All @@ -16,6 +16,10 @@ pub struct RollupOpsBlock {
pub ops: Vec<ZkSyncOp>,
/// Fee account
pub fee_account: AccountId,
/// Timestamp
pub timestamp: Option<u64>,
/// Previous block root hash.
pub previous_block_root_hash: H256,
}

impl RollupOpsBlock {
Expand Down
2 changes: 2 additions & 0 deletions core/bin/data_restore/src/storage_interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ pub fn stored_ops_block_into_ops_block(op_block: &StoredRollupOpsBlock) -> Rollu
block_num: op_block.block_num,
ops: op_block.ops.clone(),
fee_account: op_block.fee_account,
timestamp: op_block.timestamp,
previous_block_root_hash: op_block.previous_block_root_hash,
}
}
8 changes: 8 additions & 0 deletions core/bin/data_restore/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
]);

let eth = Eth::new(transport.clone());
let available_block_chunk_sizes = vec![10, 32, 72, 156, 322, 654];
let mut driver = DataRestoreDriver::new(
Web3::new(transport.clone()),
[1u8; 20].into(),
Expand All @@ -389,6 +390,7 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
true,
None,
ZkSyncDeployedContract::version4(eth, [1u8; 20].into()),
available_block_chunk_sizes,
);

driver.run_state_update(&mut interactor).await;
Expand All @@ -414,6 +416,7 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
// Nullify the state of driver
let eth = Eth::new(transport.clone());

let available_block_chunk_sizes = vec![10, 32, 72, 156, 322, 654];
let mut driver = DataRestoreDriver::new(
Web3::new(transport.clone()),
[1u8; 20].into(),
Expand All @@ -422,6 +425,7 @@ async fn test_run_state_update(mut storage: StorageProcessor<'_>) {
true,
None,
ZkSyncDeployedContract::version4(eth, [1u8; 20].into()),
available_block_chunk_sizes,
);

// Load state from db and check it
Expand Down Expand Up @@ -585,6 +589,7 @@ async fn test_with_inmemory_storage() {
let web3 = Web3::new(transport.clone());

let eth = Eth::new(transport.clone());
let available_block_chunk_sizes = vec![10, 32, 72, 156, 322, 654];
let mut driver = DataRestoreDriver::new(
web3.clone(),
[1u8; 20].into(),
Expand All @@ -593,6 +598,7 @@ async fn test_with_inmemory_storage() {
true,
None,
ZkSyncDeployedContract::version4(eth, [1u8; 20].into()),
available_block_chunk_sizes,
);

driver.run_state_update(&mut interactor).await;
Expand All @@ -611,6 +617,7 @@ async fn test_with_inmemory_storage() {

// Nullify the state of driver
let eth = Eth::new(transport.clone());
let available_block_chunk_sizes = vec![10, 32, 72, 156, 322, 654];
let mut driver = DataRestoreDriver::new(
web3.clone(),
[1u8; 20].into(),
Expand All @@ -619,6 +626,7 @@ async fn test_with_inmemory_storage() {
true,
None,
ZkSyncDeployedContract::version4(eth, [1u8; 20].into()),
available_block_chunk_sizes,
);

// Load state from db and check it
Expand Down
Loading

0 comments on commit c570c43

Please sign in to comment.