Skip to content

Commit

Permalink
aggregated gas prices calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
dvush committed Feb 2, 2021
1 parent 7ca0c83 commit de695e6
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 32 deletions.
100 changes: 68 additions & 32 deletions core/tests/testkit/src/bin/gas_price_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ impl CostPerOperation {

pub fn report(&self, description: &str, report_grief: bool) {
let grief_info = if report_grief {
let mut info = String::from("perfect_grief_factor: ");
let mut info = String::from("\nuser gas cost over operator cost: ");
info.push_str(&self.asymptotic_grief_factor());
info
} else {
String::new()
};
println!(
"gas cost of {}: user_gas_cost: {} commit: {}, verify: {}, withdraw: {}, total: {}, {}",
"Gas cost of {}:\nuser_gas_cost: {}\ncommit: {}\nprove: {}\nexecute: {}\ntotal: {}{}",
description,
self.user_gas_cost,
self.commit_cost,
Expand All @@ -143,6 +143,7 @@ impl CostPerOperation {
self.total,
grief_info
);
println!()
}
}

Expand Down Expand Up @@ -246,12 +247,37 @@ async fn gas_price_test() {

let rng = &mut XorShiftRng::from_seed([0, 1, 2, 3]);

commit_cost_of_empty_block(&mut test_setup).await; // warmup, init some storage slots
let base_cost = commit_cost_of_empty_block(&mut test_setup).await;
println!(
"block operations base cost: commit: {} verify: {} withdrawals: {}",
base_cost.base_commit_cost, base_cost.base_verify_cost, base_cost.base_withdraw_cost
);
commit_cost_of_n_empty_blocks(&mut test_setup, 1).await; // warmup, init some storage slots
let base_cost = commit_cost_of_n_empty_blocks(&mut test_setup, 1).await;
{
// Aggregated blocks amortization info
let n_blocks = 5;
let base_cost_n_blocks = commit_cost_of_n_empty_blocks(&mut test_setup, n_blocks).await;
let commit_cost_per_block = (base_cost_n_blocks.base_commit_cost
- base_cost.base_commit_cost.clone())
/ (n_blocks - 1);
let commit_base_cost = &base_cost.base_commit_cost - &commit_cost_per_block;
let prove_cost_per_block = (base_cost_n_blocks.base_verify_cost
- base_cost.base_verify_cost.clone())
/ (n_blocks - 1);
let prove_base_cost = &base_cost.base_verify_cost - &prove_cost_per_block;
let execute_cost_per_block = (base_cost_n_blocks.base_withdraw_cost
- base_cost.base_withdraw_cost.clone())
/ (n_blocks - 1);
let execute_base_cost = &base_cost.base_withdraw_cost - &execute_cost_per_block;
println!("Cost of block operations (base_cost, cost_per_block):");
println!("NOTE: aggregated blocks(n) cost of tx = base_cost + cost_per_block*n");
println!(
"commit: ({}, {})\nprove: ({}, {})\nexecute: ({}, {})",
commit_base_cost,
commit_cost_per_block,
prove_base_cost,
prove_cost_per_block,
execute_base_cost,
execute_cost_per_block
);
println!();
}

commit_cost_of_deposits(&mut test_setup, 100, Token(0), rng)
.await
Expand Down Expand Up @@ -554,32 +580,42 @@ async fn commit_cost_of_full_exits(
CostsSample::new(n_full_exits, user_gas_cost, full_exits_execute_results)
}

async fn commit_cost_of_empty_block(test_setup: &mut TestSetup) -> BaseCost {
test_setup.start_block();
let empty_block_exec_results = test_setup
.execute_commit_and_verify_block()
async fn commit_cost_of_n_empty_blocks(test_setup: &mut TestSetup, n: usize) -> BaseCost {
let mut blocks = Vec::new();
for _ in 0..n {
test_setup.start_block();
let block = test_setup.execute_block().await;
assert_eq!(
block.block_chunks_size, MIN_BLOCK_SIZE_CHUNKS,
"block size mismatch"
);
blocks.push(block);
}
let base_commit_cost = test_setup
.commit_blocks(&blocks)
.await
.expect("Block execution failed");
assert_eq!(
empty_block_exec_results.block_size_chunks, MIN_BLOCK_SIZE_CHUNKS,
"block size mismatch"
);
.expect_success()
.gas_used
.map(u256_to_bigint)
.expect("commit gas used empty");
let base_verify_cost = test_setup
.prove_blocks(&blocks, None)
.await
.expect_success()
.gas_used
.map(u256_to_bigint)
.expect("prove gas used empty");
let base_withdraw_cost = test_setup
.execute_blocks_onchain(&blocks)
.await
.expect_success()
.gas_used
.map(u256_to_bigint)
.expect("execute gas used empty");
BaseCost {
base_commit_cost: empty_block_exec_results
.commit_result
.gas_used
.map(u256_to_bigint)
.expect("commit gas used empty"),
base_verify_cost: empty_block_exec_results
.verify_result
.gas_used
.map(u256_to_bigint)
.expect("commit gas used empty"),
base_withdraw_cost: empty_block_exec_results
.withdrawals_result
.gas_used
.map(u256_to_bigint)
.expect("commit gas used empty"),
base_commit_cost,
base_verify_cost,
base_withdraw_cost,
}
}

Expand Down
60 changes: 60 additions & 0 deletions core/tests/testkit/src/test_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,66 @@ impl TestSetup {
new_block
}

pub async fn execute_block(&mut self) -> Block {
self.state_keeper_request_sender
.clone()
.send(StateKeeperRequest::SealBlock)
.await
.expect("sk receiver dropped");

self.await_for_block_commit_request().await.block
}

pub async fn commit_blocks(&mut self, blocks: &[Block]) -> ETHExecResult {
assert!(!blocks.is_empty());
let block_commit_op = BlocksCommitOperation {
last_committed_block: self.last_committed_block.clone(),
blocks: blocks.to_vec(),
};
self.last_committed_block = blocks.last().unwrap().clone();
self.commit_account
.commit_block(&block_commit_op)
.await
.expect("block commit send tx")
}

pub async fn prove_blocks(
&mut self,
blocks: &[Block],
proof: Option<EncodedAggregatedProof>,
) -> ETHExecResult {
let proof = proof.unwrap_or_else(|| {
let mut default_proof = EncodedAggregatedProof::default();
default_proof.individual_vk_inputs = Vec::new();
default_proof.individual_vk_idxs = Vec::new();
for block in blocks {
let commitment = U256::from_big_endian(block.block_commitment.as_bytes());
default_proof.individual_vk_inputs.push(commitment);
default_proof.individual_vk_idxs.push(U256::from(0));
}
default_proof
});

let block_proof_op = BlocksProofOperation {
blocks: blocks.to_vec(),
proof,
};
self.commit_account
.verify_block(&block_proof_op)
.await
.expect("block verify send tx")
}

pub async fn execute_blocks_onchain(&mut self, blocks: &[Block]) -> ETHExecResult {
let block_execute_op = BlocksExecuteOperation {
blocks: blocks.to_vec(),
};
self.commit_account
.execute_block(&block_execute_op)
.await
.expect("execute block tx")
}

pub async fn execute_verify_commitments(
&mut self,
proof: BlocksProofOperation,
Expand Down

0 comments on commit de695e6

Please sign in to comment.