Skip to content

Commit

Permalink
Merge #1758
Browse files Browse the repository at this point in the history
1758: Remove last tx signer r=Deniallugo a=Deniallugo

Signed-off-by: deniallugo <[email protected]>

Co-authored-by: deniallugo <[email protected]>
  • Loading branch information
bors-matterlabs-dev[bot] and Deniallugo authored Jul 16, 2021
2 parents d67fa7a + 20dc482 commit 173bbdd
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 177 deletions.
33 changes: 0 additions & 33 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion core/bin/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ pub async fn run_core(
config.chain.state_keeper.block_chunk_sizes.clone(),
config.chain.state_keeper.miniblock_iterations as usize,
config.chain.state_keeper.fast_block_miniblock_iterations as usize,
config.chain.state_keeper.last_tx_signer_data(),
);
let state_keeper_task = start_state_keeper(state_keeper, pending_block);

Expand Down
122 changes: 3 additions & 119 deletions core/bin/zksync_core/src/state_keeper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ use itertools::Itertools;
use tokio::task::JoinHandle;
// Workspace uses
use zksync_crypto::{
convert::FeConvert,
ff::{self, PrimeField, PrimeFieldRepr},
params::{
ETH_TOKEN_ID, MIN_NFT_TOKEN_ID, NFT_STORAGE_ACCOUNT_ADDRESS, NFT_STORAGE_ACCOUNT_ID,
NFT_TOKEN_ID,
},
PrivateKey,
params::{MIN_NFT_TOKEN_ID, NFT_STORAGE_ACCOUNT_ADDRESS, NFT_STORAGE_ACCOUNT_ID, NFT_TOKEN_ID},
};
use zksync_state::state::{CollectedFee, OpSuccess, ZkSyncState};
use zksync_storage::ConnectionPool;
Expand All @@ -26,11 +21,10 @@ use zksync_types::{
PendingBlock as SendablePendingBlock,
},
gas_counter::GasCounter,
helpers::reverse_updates,
mempool::SignedTxVariant,
tx::{TxHash, ZkSyncTx},
Account, AccountId, AccountTree, AccountUpdate, AccountUpdates, Address, BlockNumber,
PriorityOp, SignedZkSyncTx, Token, TokenId, Transfer, TransferOp, H256, NFT,
PriorityOp, SignedZkSyncTx, Token, TokenId, H256, NFT,
};
// Local uses
use crate::{
Expand Down Expand Up @@ -82,16 +76,12 @@ impl PendingBlock {
available_chunks_sizes: &[usize],
previous_block_root_hash: H256,
timestamp: u64,
should_include_last_transfer: bool,
) -> Self {
// TransferOp chunks are subtracted to reserve space for last transfer.
let mut chunks_left = *available_chunks_sizes
let chunks_left = *available_chunks_sizes
.iter()
.max()
.expect("Expected at least one block chunks size");
if should_include_last_transfer {
chunks_left -= TransferOp::CHUNKS;
}
Self {
success_operations: Vec::new(),
failed_txs: Vec::new(),
Expand Down Expand Up @@ -140,9 +130,6 @@ pub struct ZkSyncStateKeeper {
success_txs_pending_len: usize,
/// Amount of failed transactions in the pending block at the last pending block synchronization step.
failed_txs_pending_len: usize,

/// ZK sync account that is used to create last transfer before sealing block (e.g. to change block hash)
tx_signer: Option<(Address, PrivateKey)>,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -413,7 +400,6 @@ impl ZkSyncStateKeeper {
available_block_chunk_sizes: Vec<usize>,
max_miniblock_iterations: usize,
fast_miniblock_iterations: usize,
tx_signer: Option<(Address, PrivateKey)>,
) -> Self {
assert!(!available_block_chunk_sizes.is_empty());

Expand Down Expand Up @@ -453,15 +439,13 @@ impl ZkSyncStateKeeper {
&available_block_chunk_sizes,
previous_root_hash,
system_time_timestamp(),
tx_signer.is_some(),
),
available_block_chunk_sizes,
max_miniblock_iterations,
fast_miniblock_iterations,

success_txs_pending_len: 0,
failed_txs_pending_len: 0,
tx_signer,
};

let root = keeper.state.root_hash();
Expand Down Expand Up @@ -1053,18 +1037,13 @@ impl ZkSyncStateKeeper {
.account_updates
.extend(fee_updates.into_iter());

// This last tx does not pay any fee
if let Err(e) = self.execute_transfer_to_change_block_hash() {
vlog::error!("Failed to execute transfer to change block hash: {}", e);
}
let mut pending_block = std::mem::replace(
&mut self.pending_block,
PendingBlock::new(
self.current_unprocessed_priority_op,
&self.available_block_chunk_sizes,
H256::default(),
system_time_timestamp(),
self.tx_signer.is_some(),
),
);
// Once block is sealed, we refresh the counters for the next block.
Expand Down Expand Up @@ -1203,102 +1182,7 @@ impl ZkSyncStateKeeper {
unprocessed_priority_op: self.current_unprocessed_priority_op,
}
}

/// Should be applied after fee is collected when block is being sealed.
fn execute_transfer_to_change_block_hash(&mut self) -> anyhow::Result<()> {
let (signer_id, signer_account, signer_pk) = {
let (address, pk) = if let Some((address, pk)) = self.tx_signer.as_ref() {
(address, pk)
} else {
return Ok(());
};
let (id, account) = self.state.get_account_by_address(&address).ok_or_else(|| {
anyhow::format_err!("Signer account is not in the tree: {:?}", address)
})?;
(id, account, pk)
};
let (target_id, target_account) = {
(
self.fee_account_id,
self.state
.get_account(self.fee_account_id)
.expect("Fee account must be present in the tree"),
)
};

let mut tx_value = 0u32;
let mut first_byte = self.state.root_hash().to_bytes()[0];
while first_byte > 0x1F {
tx_value += 1;
anyhow::ensure!(
signer_account.get_balance(ETH_TOKEN_ID) > tx_value.into(),
"Not enough balance on signer account"
);

let expected_updates = vec![
(
signer_id,
AccountUpdate::UpdateBalance {
old_nonce: signer_account.nonce,
new_nonce: signer_account.nonce + 1,
balance_update: (
ETH_TOKEN_ID,
signer_account.get_balance(ETH_TOKEN_ID),
signer_account.get_balance(ETH_TOKEN_ID) - tx_value,
),
},
),
(
target_id,
AccountUpdate::UpdateBalance {
old_nonce: target_account.nonce,
new_nonce: target_account.nonce,
balance_update: (
ETH_TOKEN_ID,
target_account.get_balance(ETH_TOKEN_ID),
target_account.get_balance(ETH_TOKEN_ID) + tx_value,
),
},
),
];
self.state.apply_account_updates(expected_updates.clone());

first_byte = self.state.root_hash().to_bytes()[0];

let reverse_updates = {
let mut rev_updates = expected_updates;
reverse_updates(&mut rev_updates);
rev_updates
};
self.state.apply_account_updates(reverse_updates);
}

if tx_value == 0 {
return Ok(());
}

let transfer = Transfer::new_signed(
signer_id,
signer_account.address,
target_account.address,
ETH_TOKEN_ID,
tx_value.into(),
0u32.into(),
signer_account.nonce,
Default::default(),
&signer_pk,
)?;

self.apply_tx(&SignedZkSyncTx {
tx: transfer.into(),
eth_sign_data: None,
})
.map_err(|_| anyhow::format_err!("Transaction execution failed"))?;

Ok(())
}
}

#[must_use]
pub fn start_state_keeper(
sk: ZkSyncStateKeeper,
Expand Down
2 changes: 0 additions & 2 deletions core/bin/zksync_core/src/state_keeper/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl StateKeeperTester {
vec![available_chunk_size],
max_iterations,
fast_iterations,
None,
);

Self {
Expand Down Expand Up @@ -254,7 +253,6 @@ fn test_create_incorrect_state_keeper() {
vec![1, 2, 2], // `available_block_chunk_sizes` must be strictly increasing.
MAX_ITERATIONS,
FAST_ITERATIONS,
None,
);
}

Expand Down
21 changes: 0 additions & 21 deletions core/lib/config/src/configs/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use serde::Deserialize;
/// Built-in uses
use std::time::Duration;
// Local uses
use zksync_crypto::{convert::FeConvert, priv_key_from_fs, Fs, PrivateKey};
use zksync_types::network::Network;
use zksync_types::Address;

Expand Down Expand Up @@ -89,9 +88,6 @@ pub struct StateKeeper {
pub block_prove_deadline: u64,
pub block_execute_deadline: u64,
pub max_aggregated_tx_gas: usize,
pub last_tx_signer_used: bool,
pub last_tx_signer_address: Address,
pub last_tx_signer_private_key: String,
}

impl StateKeeper {
Expand All @@ -111,17 +107,6 @@ impl StateKeeper {
pub fn block_execute_deadline(&self) -> Duration {
Duration::from_secs(self.block_execute_deadline)
}

pub fn last_tx_signer_data(&self) -> Option<(Address, PrivateKey)> {
if self.last_tx_signer_used {
let fs = Fs::from_hex(&self.last_tx_signer_private_key)
.expect("failed to parse private key");
let pk = priv_key_from_fs(fs);
Some((self.last_tx_signer_address, pk))
} else {
None
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -156,9 +141,6 @@ mod tests {
block_prove_deadline: 3_000,
block_execute_deadline: 4_000,
max_aggregated_tx_gas: 4_000_000,
last_tx_signer_used: false,
last_tx_signer_private_key: "0xaabbeecc".into(),
last_tx_signer_address: addr("da03a0b5963f75f1c8485b355ff6d30f3093bde7"),
},
}
}
Expand Down Expand Up @@ -187,9 +169,6 @@ CHAIN_STATE_KEEPER_BLOCK_COMMIT_DEADLINE="300"
CHAIN_STATE_KEEPER_BLOCK_PROVE_DEADLINE="3000"
CHAIN_STATE_KEEPER_BLOCK_EXECUTE_DEADLINE="4000"
CHAIN_STATE_KEEPER_MAX_AGGREGATED_TX_GAS="4000000"
CHAIN_STATE_KEEPER_LAST_TX_SIGNER_USED="false"
CHAIN_STATE_KEEPER_LAST_TX_SIGNER_ADDRESS="0xda03a0b5963f75f1c8485b355ff6d30f3093bde7"
CHAIN_STATE_KEEPER_LAST_TX_SIGNER_PRIVATE_KEY="0xaabbeecc"
"#;
set_env(config);

Expand Down
1 change: 0 additions & 1 deletion core/tests/testkit/src/state_keeper_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub fn spawn_state_keeper(
block_chunks_sizes,
max_miniblock_iterations,
max_miniblock_iterations,
None,
);

let (stop_state_keeper_sender, stop_state_keeper_receiver) = oneshot::channel::<()>();
Expand Down

0 comments on commit 173bbdd

Please sign in to comment.