Skip to content

Commit

Permalink
Data-restore works in container
Browse files Browse the repository at this point in the history
  • Loading branch information
popzxc committed Oct 22, 2020
1 parent aaf5872 commit f8f9a42
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 18 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*
!docker/prover/prover-entry.sh
!docker/exit-tool/exit-tool-entry.sh
!docker/exit-tool/configs
!docker/keybase-secrets/entrypoint.sh
!etc/env/dev.env.example
!etc/tokens
Expand Down
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.

1 change: 1 addition & 0 deletions core/bin/data_restore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ethabi = "12.0.0"
web3 = "0.13.0"
hex = "0.4"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.0"
env_logger = "0.6"
anyhow = "1.0"
Expand Down
64 changes: 50 additions & 14 deletions core/bin/data_restore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ pub mod storage_interactor;
pub mod tree_state;

use crate::data_restore_driver::DataRestoreDriver;
use serde::Deserialize;
use structopt::StructOpt;
use web3::transports::Http;
use zksync_config::ConfigurationOptions;
use zksync_crypto::convert::FeConvert;
use zksync_storage::ConnectionPool;
use zksync_types::{
tokens::{get_genesis_token_list, Token},
TokenId,
Address, TokenId, H256,
};

const ETH_BLOCKS_STEP: u64 = 1;
Expand Down Expand Up @@ -71,6 +72,43 @@ struct Opt {
/// Expected tree root hash after restoring. This argument is ignored if mode is not `finite`
#[structopt(long)]
final_hash: Option<String>,

/// Sets the web3 API to be used to interact with the Ethereum blockchain
#[structopt(long, name = "web3")]
web3_url: Option<String>,

/// Provides a path to the configuration file for data restore
#[structopt(long, name = "config")]
config_path: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ContractsConfig {
eth_network: String,
governance_addr: Address,
genesis_tx_hash: H256,
contract_addr: Address,
available_block_chunk_sizes: Vec<usize>,
}

impl ContractsConfig {
pub fn from_file(path: &str) -> Self {
let content =
std::fs::read_to_string(path).expect("Unable to find the specified config file");
serde_json::from_str(&content).expect("Invalid configuration file provided")
}

pub fn from_env() -> Self {
let config_opts = ConfigurationOptions::from_env();

Self {
eth_network: config_opts.eth_network,
governance_addr: config_opts.governance_eth_addr,
genesis_tx_hash: config_opts.genesis_tx_hash,
contract_addr: config_opts.contract_eth_addr,
available_block_chunk_sizes: config_opts.available_block_chunk_sizes,
}
}
}

#[tokio::main]
Expand All @@ -82,16 +120,14 @@ async fn main() {

let opt = Opt::from_args();

let web3_url = cli
.value_of("web3-url")
.map(|value| value.to_string())
.unwrap_or(config_opts.web3_url);
let web3_url = opt.web3_url.unwrap_or(config_opts.web3_url);

let transport = Http::new(&web3_url).expect("failed to start web3 transport");
let governance_addr = config_opts.governance_eth_addr;
let genesis_tx_hash = config_opts.genesis_tx_hash;
let contract_addr = config_opts.contract_eth_addr;
let available_block_chunk_sizes = config_opts.available_block_chunk_sizes;

let config = opt
.config_path
.map(|path| ContractsConfig::from_file(&path))
.unwrap_or_else(ContractsConfig::from_env);

let finite_mode = opt.finite;
let final_hash = if finite_mode {
Expand All @@ -104,11 +140,11 @@ async fn main() {
let mut driver = DataRestoreDriver::new(
connection_pool,
transport,
governance_addr,
contract_addr,
config.governance_addr,
config.contract_addr,
ETH_BLOCKS_STEP,
END_ETH_BLOCKS_OFFSET,
available_block_chunk_sizes,
config.available_block_chunk_sizes,
finite_mode,
final_hash,
);
Expand All @@ -117,9 +153,9 @@ async fn main() {
if opt.genesis {
// We have to load pre-defined tokens into the database before restoring state,
// since these tokens do not have a corresponding Ethereum events.
add_tokens_to_db(&driver.connection_pool, &config_opts.eth_network).await;
add_tokens_to_db(&driver.connection_pool, &config.eth_network).await;

driver.set_genesis_state(genesis_tx_hash).await;
driver.set_genesis_state(config.genesis_tx_hash).await;
}

if opt.continue_mode {
Expand Down
3 changes: 3 additions & 0 deletions docker/exit-tool/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ RUN cargo install --version=0.1.0-beta.1 sqlx-cli
# Copy workspace
COPY . .

# Copy data-restore configuration files
COPY docker/exit-tool/configs /usr/src/configs

# Build all the required zkSync binaries
RUN cargo build --release
RUN cargo build --release --example generate_exit_proof
Expand Down
7 changes: 7 additions & 0 deletions docker/exit-tool/configs/rinkeby.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"eth_network": "localhost",
"governance_addr": "0x2A0bD2E9D01024e714cDDD8a6b94eB9219663107",
"genesis_tx_hash": "0x0dc9b2387b0d3f80adcece49936ed3efec27c2845ed7ca72d01e567c872cfa1f",
"contract_addr": "0x04eff60dD5D6E171BdB808fb4af35689968F3172",
"available_block_chunk_sizes": [6,30,74,150,320,630]
}
4 changes: 2 additions & 2 deletions docker/exit-tool/exit-tool-entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ case $COMMAND in
exit 0
;;
run)
f cargo run --bin zksync_data_restore --release -- --genesis --finite
f ./target/release/zksync_data_restore --genesis --finite --config_path=/usr/src/configs/rinkeby.json
;;
continue)
f cargo run --bin zksync_data_restore --release -- --continue --finite
f ./target/release/zksync_data_restore --continue --finite --config_pathg=/usr/src/configs/rinkeby.json
;;
-h | --help)
echo "$USAGE"
Expand Down
5 changes: 3 additions & 2 deletions docker/exit-tool/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ WEB3_ADDR = $3
# TODO these values must be chosen for the expected network (mainnet / rinkeby / ropsten)
GENESIS_TX_HASH="0x0dc9b2387b0d3f80adcece49936ed3efec27c2845ed7ca72d01e567c872cfa1f"

docker run --net=host --mount type=bind,src=./setup,dst=/usr/src/zksync/keys/setup matterlabs/exit-tool:latest bash -c "exit-tool-entry.sh init"
docker run --net=host --mount type=bind,src=./setup,dst=/usr/src/zksync/keys/setup matterlabs/exit-tool:latest bash -c "exit-tool-entry.sh run $ACCOUNT_ID $TOKEN $WEB3_ADDR $GENESIS_TX_HASH"
# TODO: Do not use `$ZKSYNC_HOME` here
docker run --net=host --net=host -v $ZKSYNC_HOME/keys/setup:/usr/src/zksync/keys/setup matterlabs/exit-tool:latest bash -c "exit-tool-entry.sh init"
docker run --net=host --net=host -v $ZKSYNC_HOME/keys/setup:/usr/src/zksync/keys/setup matterlabs/exit-tool:latest bash -c "exit-tool-entry.sh run $ACCOUNT_ID $TOKEN $WEB3_ADDR $GENESIS_TX_HASH"

0 comments on commit f8f9a42

Please sign in to comment.