Skip to content

Commit

Permalink
Merge #1396
Browse files Browse the repository at this point in the history
1396: Fix data restore for new contracts r=popzxc a=Deniallugo

This code has been tested on Rinkeby. and the roots for the blocks were correct, also I checked several balances and they were correct. Of course, we have to trust the tests, but that was additional proof. 

Co-authored-by: Danil Lugovskoy <[email protected]>
Co-authored-by: Danil <[email protected]>
  • Loading branch information
bors-matterlabs-dev[bot] and Deniallugo authored Feb 16, 2021
2 parents af11a92 + 1517114 commit 81da602
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 382 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 6 additions & 49 deletions core/bin/data_restore/src/contract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use web3::api::Eth;
use web3::contract::Options;
use web3::types::{Address, BlockId, BlockNumber, U256};
use web3::types::{Address, BlockId, U256};
use web3::Transport;

use zksync_contracts::{
Expand All @@ -20,18 +20,10 @@ pub struct ZkSyncDeployedContract<T: Transport> {
pub web3_contract: web3::contract::Contract<T>,
pub abi: ethabi::Contract,
pub version: ZkSyncContractVersion,
pub from: BlockNumber,
pub to: BlockNumber,
}

impl<T: Transport> ZkSyncDeployedContract<T> {
/// Returns total number of verified blocks on Rollup contract
///
/// # Arguments
///
/// * `web3` - Web3 provider url
/// * `zksync_contract` - Rollup contract
///
pub async fn get_total_verified_blocks(&self) -> u32 {
use ZkSyncContractVersion::*;
let func = match self.version {
Expand All @@ -51,79 +43,44 @@ impl<T: Transport> ZkSyncDeployedContract<T> {
.as_u32()
}

pub fn version0(
eth: Eth<T>,
address: Address,
from: BlockNumber,
to: BlockNumber,
) -> ZkSyncDeployedContract<T> {
pub fn version0(eth: Eth<T>, address: Address) -> ZkSyncDeployedContract<T> {
let abi = zksync_contract_v0();
ZkSyncDeployedContract {
web3_contract: web3::contract::Contract::new(eth, address, abi.clone()),
abi,
version: ZkSyncContractVersion::V0,
from,
to,
}
}
pub fn version1(
eth: Eth<T>,
address: Address,
from: BlockNumber,
to: BlockNumber,
) -> ZkSyncDeployedContract<T> {
pub fn version1(eth: Eth<T>, address: Address) -> ZkSyncDeployedContract<T> {
let abi = zksync_contract_v1();
ZkSyncDeployedContract {
web3_contract: web3::contract::Contract::new(eth, address, abi.clone()),
abi,
version: ZkSyncContractVersion::V1,
from,
to,
}
}
pub fn version2(
eth: Eth<T>,
address: Address,
from: BlockNumber,
to: BlockNumber,
) -> ZkSyncDeployedContract<T> {
pub fn version2(eth: Eth<T>, address: Address) -> ZkSyncDeployedContract<T> {
let abi = zksync_contract_v2();
ZkSyncDeployedContract {
web3_contract: web3::contract::Contract::new(eth, address, abi.clone()),
abi,
version: ZkSyncContractVersion::V2,
from,
to,
}
}
pub fn version3(
eth: Eth<T>,
address: Address,
from: BlockNumber,
to: BlockNumber,
) -> ZkSyncDeployedContract<T> {
pub fn version3(eth: Eth<T>, address: Address) -> ZkSyncDeployedContract<T> {
let abi = zksync_contract_v3();
ZkSyncDeployedContract {
web3_contract: web3::contract::Contract::new(eth, address, abi.clone()),
abi,
version: ZkSyncContractVersion::V3,
from,
to,
}
}
pub fn version4(
eth: Eth<T>,
address: Address,
from: BlockNumber,
to: BlockNumber,
) -> ZkSyncDeployedContract<T> {
pub fn version4(eth: Eth<T>, address: Address) -> ZkSyncDeployedContract<T> {
let abi = zksync_contract();
ZkSyncDeployedContract {
web3_contract: web3::contract::Contract::new(eth, address, abi.clone()),
abi,
version: ZkSyncContractVersion::V4,
from,
to,
}
}
}
64 changes: 31 additions & 33 deletions core/bin/data_restore/src/contract/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn decode_commitment_parameters(input_data: Vec<u8>) -> anyhow::Result<Vec<Token
let commit_operation = ParamType::Tuple(vec![
Box::new(ParamType::FixedBytes(32)), // bytes32 encoded_root,
Box::new(ParamType::Bytes), // bytes calldata _publicData,
Box::new(ParamType::Uint(256)), // uint64 _timestamp,
Box::new(ParamType::Uint(256)), // uint256 _timestamp,
Box::new(ParamType::Array(Box::new(ParamType::Tuple(vec![
Box::new(ParamType::Bytes), // bytes eht_witness
Box::new(ParamType::Uint(32)), //uint32 public_data_offset
Expand All @@ -18,7 +18,7 @@ fn decode_commitment_parameters(input_data: Vec<u8>) -> anyhow::Result<Vec<Token
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::FixedBytes(32)), // bytes32 processable_ops_hash
Box::new(ParamType::Uint(256)), // uint256 timestamp
Box::new(ParamType::FixedBytes(32)), // bytes32 eth_encoded_root
Box::new(ParamType::FixedBytes(32)), // commitment
Expand All @@ -37,44 +37,42 @@ 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 public_data_argument_id = 1;

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

if let (ethabi::Token::Tuple(block), ethabi::Token::Array(operations)) = (
&decoded_commitment_parameters[0],
&decoded_commitment_parameters[1],
) {
// Destruct deserialized parts of transaction input data for getting operations
// Input data consists of stored block and operations
// Transform operations to RollupBlock
if let ethabi::Token::Array(operations) = &decoded_commitment_parameters[1] {
let mut blocks = vec![];
if let ethabi::Token::Uint(block_num) = block[0] {
for operation in operations {
if let ethabi::Token::Tuple(operation) = operation {
if let (ethabi::Token::Uint(fee_acc), ethabi::Token::Bytes(public_data)) = (
&operation[fee_account_argument_id],
&operation[public_data_argument_id],
) {
let ops = get_rollup_ops_from_data(public_data.as_slice())?;
blocks.push(RollupOpsBlock {
block_num: BlockNumber(block_num.as_u32()),
ops,
fee_account: AccountId(fee_acc.as_u32()),
})
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"can't parse operation parameters",
)
.into());
}
for operation in operations {
if let ethabi::Token::Tuple(operation) = operation {
if let (
ethabi::Token::Uint(fee_acc),
ethabi::Token::Bytes(public_data),
ethabi::Token::Uint(block_number),
) = (
&operation[fee_account_argument_id],
&operation[public_data_argument_id],
&operation[op_block_number_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()),
})
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"can't parse operation parameters",
)
.into());
}
}
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"can't parse block parameters",
)
.into());
}
Ok(blocks)
} else {
Expand Down Expand Up @@ -128,7 +126,7 @@ mod test {
let blocks = rollup_ops_blocks_from_bytes(input_data[4..].to_vec()).unwrap();
assert_eq!(blocks.len(), 1);
let block = blocks[0].clone();
assert_eq!(block.block_num, BlockNumber(24));
assert_eq!(block.block_num, BlockNumber(25));
assert_eq!(block.fee_account, AccountId(0));
assert_eq!(block.ops.len(), 5);
}
Expand Down
Loading

0 comments on commit 81da602

Please sign in to comment.