Skip to content

Commit

Permalink
Merge branch 'dev' into vb-956-jsonrpcsigner-rust-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbochok authored Oct 7, 2020
2 parents abf51e4 + f65a7c3 commit da3a9f2
Show file tree
Hide file tree
Showing 24 changed files with 553 additions and 287 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ ci-check:
@ci-check.sh

integration-testkit:
@bin/integration-testkit.sh
@bin/integration-testkit.sh $(filter-out $@,$(MAKECMDGOALS))

integration-simple:
@cd core/tests/ts-tests && yarn && yarn simple $(filter-out $@,$(MAKECMDGOALS))
Expand Down
2 changes: 1 addition & 1 deletion bin/deploy-testkit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ then
fi

cd contracts;
yarn deploy-testkit
yarn deploy-testkit $@
34 changes: 29 additions & 5 deletions bin/integration-testkit.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/bin/bash

USAGE='Usage: zksync integration-testkit [ fast | block-sizes-test ]
Used to run testkit tests with geth configured for fast block execution.
Options:
fast (default) | block-sizes-test select tests to run
'
COMMAND=${1:-fast}

. .setup_env

set -e
Expand Down Expand Up @@ -30,8 +41,21 @@ fi
export ETH_NETWORK="test"
make build-contracts

cargo run --bin zksync_testkit --release
cargo run --bin gas_price_test --release
cargo run --bin migration_test --release
cargo run --bin revert_blocks_test --release
cargo run --bin exodus_test --release
case $COMMAND in
block-sizes-test)
cargo run --bin block_sizes_test --release
;;
fast)
cargo run --bin zksync_testkit --release
cargo run --bin gas_price_test --release
cargo run --bin migration_test --release
cargo run --bin revert_blocks_test --release
cargo run --bin exodus_test --release
;;
-h|--h)
echo "$USAGE" && exit 0
;;
*)
echo "$USAGE" && exit 1
;;
esac
16 changes: 14 additions & 2 deletions contracts/scripts/deploy-testkit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import {ethers, Wallet} from "ethers";
import {Deployer, readContractCode, readTestContracts} from "../src.ts/deploy";
import {Deployer, readContractCode, readTestContracts, readProductionContracts} from "../src.ts/deploy";
import {deployContract} from "ethereum-waffle";
import {ArgumentParser} from "argparse";

(async () => {
const parser = new ArgumentParser({
version: "0.1.0",
addHelp: true,
description: "Deploy testkit contracts",
});
parser.addArgument("--prodContracts", {required: false, help: "deploy production contracts", action: "storeTrue"});
parser.addArgument("--genesisRoot", {required: true, help: "genesis root"});
const args = parser.parseArgs(process.argv.slice(2));
process.env.GENESIS_ROOT = args.genesisRoot;

if (process.env.ETH_NETWORK !== "test") {
console.error("This deploy script is only for localhost-test network");
process.exit(1);
Expand All @@ -12,7 +23,8 @@ import {deployContract} from "ethereum-waffle";
provider.pollingInterval = 10;

const deployWallet = ethers.Wallet.fromMnemonic(process.env.TEST_MNEMONIC, "m/44'/60'/0'/0/0").connect(provider);
const deployer = new Deployer({deployWallet, contracts: readTestContracts(), verbose: true});
const contracts = args.prodContracts ? readProductionContracts() : readTestContracts();
const deployer = new Deployer({deployWallet, contracts, verbose: true});
await deployer.deployAll();
const governance = deployer.governanceContract(deployWallet);
await (await governance.setValidator(deployWallet.address, true)).wait();
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/init-faucet-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function main() {
console.log("Deposit successful");

if (! await faucetWallet.isSigningKeySet()) {
const setSigningKey = await faucetWallet.setSigningKey();
const setSigningKey = await faucetWallet.setSigningKey({ feeToken: "MLTT" });
await setSigningKey.awaitReceipt();
console.log("Signing key is set");
}
Expand Down
192 changes: 7 additions & 185 deletions core/bin/server/src/prover_server/witness_generator.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
// Built-in
use std::{thread, time};
// External
use anyhow::format_err;
use futures::channel::mpsc;
// Workspace deps
use crate::panic_notify::ThreadPanicNotify;
use std::time::Instant;
use zksync_circuit::witness::{
utils::{SigDataInput, WitnessBuilder},
ChangePubkeyOffChainWitness, CloseAccountWitness, DepositWitness, ForcedExitWitness,
FullExitWitness, TransferToNewWitness, TransferWitness, WithdrawWitness, Witness,
};
use zksync_crypto::franklin_crypto::bellman::pairing::ff::PrimeField;
use zksync_crypto::params::{account_tree_depth, CHUNK_BIT_WIDTH};
use zksync_crypto::{circuit::CircuitAccountTree, Fr};
use zksync_circuit::witness::utils::build_block_witness;
use zksync_crypto::circuit::CircuitAccountTree;
use zksync_crypto::params::account_tree_depth;
use zksync_prover_utils::prover_data::ProverData;
use zksync_state::state::CollectedFee;
use zksync_storage::StorageProcessor;
use zksync_types::block::Block;
use zksync_types::{BlockNumber, ZkSyncOp};
use zksync_types::BlockNumber;

/// The essential part of this structure is `maintain` function
/// which runs forever and adds data to the database.
Expand Down Expand Up @@ -195,34 +188,30 @@ impl WitnessGenerator {
async fn prepare_witness_and_save_it(&self, block: Block) -> Result<(), anyhow::Error> {
let timer = Instant::now();
let mut storage = self.conn_pool.access_storage_fragile().await?;
let mut transaction = storage.start_transaction().await?;

let mut circuit_account_tree = self
.load_account_tree(block.block_number - 1, &mut transaction)
.load_account_tree(block.block_number - 1, &mut storage)
.await?;
log::trace!(
"Witness generator loading circuit account tree {}s",
timer.elapsed().as_secs()
);

let timer = Instant::now();
let witness =
build_prover_block_data(&mut circuit_account_tree, &mut transaction, &block).await?;
let witness: ProverData = build_block_witness(&mut circuit_account_tree, &block)?.into();
log::trace!(
"Witness generator witness build {}s",
timer.elapsed().as_secs()
);

transaction
storage
.prover_schema()
.store_witness(
block.block_number,
serde_json::to_value(witness).expect("Witness serialize to json"),
)
.await?;

transaction.commit().await?;

Ok(())
}

Expand Down Expand Up @@ -273,173 +262,6 @@ impl WitnessGenerator {
}
}

async fn build_prover_block_data(
account_tree: &mut CircuitAccountTree,
transaction: &mut zksync_storage::StorageProcessor<'_>,
block: &Block,
) -> Result<ProverData, anyhow::Error> {
let block_number = block.block_number;
let block_size = block.block_chunks_size;

log::info!("building prover data for block {}", &block_number);

let mut witness_accum = WitnessBuilder::new(account_tree, block.fee_account, block_number);

let ops = transaction
.chain()
.block_schema()
.get_block_operations(block_number)
.await
.map_err(|e| anyhow::format_err!("failed to get block operations {}", e))?;

let mut operations = vec![];
let mut pub_data = vec![];
let mut fees = vec![];
for op in ops {
match op {
ZkSyncOp::Deposit(deposit) => {
let deposit_witness =
DepositWitness::apply_tx(&mut witness_accum.account_tree, &deposit);

let deposit_operations = deposit_witness.calculate_operations(());
operations.extend(deposit_operations);
pub_data.extend(deposit_witness.get_pubdata());
}
ZkSyncOp::Transfer(transfer) => {
let transfer_witness =
TransferWitness::apply_tx(&mut witness_accum.account_tree, &transfer);

let input =
SigDataInput::from_transfer_op(&transfer).map_err(|e| format_err!("{}", e))?;
let transfer_operations = transfer_witness.calculate_operations(input);

operations.extend(transfer_operations);
fees.push(CollectedFee {
token: transfer.tx.token,
amount: transfer.tx.fee,
});
pub_data.extend(transfer_witness.get_pubdata());
}
ZkSyncOp::TransferToNew(transfer_to_new) => {
let transfer_to_new_witness = TransferToNewWitness::apply_tx(
&mut witness_accum.account_tree,
&transfer_to_new,
);

let input = SigDataInput::from_transfer_to_new_op(&transfer_to_new)
.map_err(|e| format_err!("{}", e))?;
let transfer_to_new_operations =
transfer_to_new_witness.calculate_operations(input);

operations.extend(transfer_to_new_operations);
fees.push(CollectedFee {
token: transfer_to_new.tx.token,
amount: transfer_to_new.tx.fee,
});
pub_data.extend(transfer_to_new_witness.get_pubdata());
}
ZkSyncOp::Withdraw(withdraw) => {
let withdraw_witness =
WithdrawWitness::apply_tx(&mut witness_accum.account_tree, &withdraw);

let input =
SigDataInput::from_withdraw_op(&withdraw).map_err(|e| format_err!("{}", e))?;
let withdraw_operations = withdraw_witness.calculate_operations(input);

operations.extend(withdraw_operations);
fees.push(CollectedFee {
token: withdraw.tx.token,
amount: withdraw.tx.fee,
});
pub_data.extend(withdraw_witness.get_pubdata());
}
ZkSyncOp::Close(close) => {
let close_account_witness =
CloseAccountWitness::apply_tx(&mut witness_accum.account_tree, &close);

let input =
SigDataInput::from_close_op(&close).map_err(|e| format_err!("{}", e))?;
let close_account_operations = close_account_witness.calculate_operations(input);

operations.extend(close_account_operations);
pub_data.extend(close_account_witness.get_pubdata());
}
ZkSyncOp::FullExit(full_exit_op) => {
let success = full_exit_op.withdraw_amount.is_some();

let full_exit_witness = FullExitWitness::apply_tx(
&mut witness_accum.account_tree,
&(*full_exit_op, success),
);

let full_exit_operations = full_exit_witness.calculate_operations(());

operations.extend(full_exit_operations);
pub_data.extend(full_exit_witness.get_pubdata());
}
ZkSyncOp::ChangePubKeyOffchain(change_pkhash_op) => {
let change_pkhash_witness = ChangePubkeyOffChainWitness::apply_tx(
&mut witness_accum.account_tree,
&change_pkhash_op,
);

let input = SigDataInput::from_change_pubkey_op(&change_pkhash_op)
.map_err(|e| format_err!("{}", e))?;
let change_pkhash_operations = change_pkhash_witness.calculate_operations(input);

operations.extend(change_pkhash_operations);
fees.push(CollectedFee {
token: change_pkhash_op.tx.fee_token,
amount: change_pkhash_op.tx.fee,
});
pub_data.extend(change_pkhash_witness.get_pubdata());
}
ZkSyncOp::ForcedExit(forced_exit) => {
let forced_exit_witness =
ForcedExitWitness::apply_tx(&mut witness_accum.account_tree, &forced_exit);

let input = SigDataInput::from_forced_exit_op(&forced_exit)
.map_err(|e| format_err!("{}", e))?;
let forced_exit_operations = forced_exit_witness.calculate_operations(input);

operations.extend(forced_exit_operations);
fees.push(CollectedFee {
token: forced_exit.tx.token,
amount: forced_exit.tx.fee,
});
pub_data.extend(forced_exit_witness.get_pubdata());
}
ZkSyncOp::Noop(_) => {} // Noops are handled below
}
}

witness_accum.add_operation_with_pubdata(operations, pub_data);
witness_accum.extend_pubdata_with_noops(block_size);
assert_eq!(witness_accum.pubdata.len(), CHUNK_BIT_WIDTH * block_size);
assert_eq!(witness_accum.operations.len(), block_size);

witness_accum.collect_fees(&fees);
assert_eq!(
witness_accum
.root_after_fees
.expect("root_after_fees not present"),
block.new_root_hash
);
witness_accum.calculate_pubdata_commitment();

Ok(ProverData {
public_data_commitment: witness_accum.pubdata_commitment.unwrap(),
old_root: witness_accum.initial_root_hash,
initial_used_subtree_root: witness_accum.initial_used_subtree_root_hash,
new_root: block.new_root_hash,
validator_address: Fr::from_str(&block.fee_account.to_string()).expect("failed to parse"),
operations: witness_accum.operations,
validator_balances: witness_accum.fee_account_balances.unwrap(),
validator_audit_path: witness_accum.fee_account_audit_path.unwrap(),
validator_account: witness_accum.fee_account_witness.unwrap(),
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions core/lib/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rust-crypto = "0.2"
num = { version = "0.2", features = ["serde"] }
log = "0.4"
serde = "1.0.90"
anyhow = "1.0"

[dev-dependencies]
zksync_test_account = { path = "../../tests/test_account", version = "1.0" }
Expand Down
Loading

0 comments on commit da3a9f2

Please sign in to comment.