Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(block-reverter): only require private key for sending revert transactions #1579

Merged
merged 9 commits into from
Apr 9, 2024
20 changes: 16 additions & 4 deletions core/bin/block_reverter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zksync_core::block_reverter::{
};
use zksync_dal::{ConnectionPool, Core};
use zksync_env_config::FromEnv;
use zksync_types::{L1BatchNumber, U256};
use zksync_types::{Address, L1BatchNumber, U256};

#[derive(Debug, Parser)]
#[command(author = "Matter Labs", version, about = "Block revert utility", long_about = None)]
Expand All @@ -26,6 +26,9 @@ enum Command {
/// Displays the values as a JSON object, so that they are machine-readable.
#[arg(long)]
json: bool,
/// Operator address.
#[arg(long = "operator-address")]
operator_address: Address,
},
/// Sends revert transaction to L1.
#[command(name = "send-eth-transaction")]
Expand Down Expand Up @@ -70,6 +73,7 @@ enum Command {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let command = Cli::parse().command;
let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let log_format: vlog::LogFormat = observability_config
Expand All @@ -96,7 +100,15 @@ async fn main() -> anyhow::Result<()> {
);
let contracts = ContractsConfig::from_env().context("ContractsConfig::from_env()")?;
let postgres_config = PostgresConfig::from_env().context("PostgresConfig::from_env()")?;
let config = BlockReverterEthConfig::new(eth_sender, contracts);
let operator_address = if let Command::Display {
operator_address, ..
} = &command
{
Some(operator_address)
} else {
None
};
let config = BlockReverterEthConfig::new(eth_sender, contracts, operator_address.copied());

let connection_pool = ConnectionPool::<Core>::builder(
postgres_config.master_url()?,
Expand All @@ -114,8 +126,8 @@ async fn main() -> anyhow::Result<()> {
L1ExecutedBatchesRevert::Disallowed,
);

match Cli::parse().command {
Command::Display { json } => {
match command {
Command::Display { json, .. } => {
let suggested_values = block_reverter.suggested_values().await;
if json {
println!("{}", serde_json::to_string(&suggested_values).unwrap());
Expand Down
35 changes: 21 additions & 14 deletions core/lib/zksync_core/src/block_reverter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zksync_types::{
types::{BlockId, BlockNumber},
Web3,
},
L1BatchNumber, PackedEthSignature, H160, H256, U256,
Address, L1BatchNumber, H160, H256, U256,
};

bitflags! {
Expand All @@ -45,29 +45,27 @@ pub enum L1ExecutedBatchesRevert {
#[derive(Debug)]
pub struct BlockReverterEthConfig {
eth_client_url: String,
reverter_private_key: H256,
reverter_address: H160,
reverter_private_key: Option<H256>,
reverter_address: Option<Address>,
diamond_proxy_addr: H160,
validator_timelock_addr: H160,
default_priority_fee_per_gas: u64,
}

impl BlockReverterEthConfig {
pub fn new(eth_config: ETHConfig, contract: ContractsConfig) -> Self {
pub fn new(
eth_config: ETHConfig,
contract: ContractsConfig,
reverter_address: Option<Address>,
) -> Self {
#[allow(deprecated)]
// `BlockReverter` doesn't support non env configs yet
let pk = eth_config
.sender
.expect("eth_sender_config")
.private_key()
.expect("Private key is required for block reversion");
let operator_address = PackedEthSignature::address_from_private_key(&pk)
.expect("Failed to get address from private key");
let pk = eth_config.sender.expect("eth_sender_config").private_key();

Self {
eth_client_url: eth_config.web3_url,
reverter_private_key: pk,
reverter_address: operator_address,
reverter_address,
diamond_proxy_addr: contract.diamond_proxy_addr,
validator_timelock_addr: contract.validator_timelock_addr,
default_priority_fee_per_gas: eth_config
Expand Down Expand Up @@ -339,7 +337,11 @@ impl BlockReverter {

let web3 = Web3::new(Http::new(&eth_config.eth_client_url).unwrap());
let contract = zksync_contract();
let signer = PrivateKeySigner::new(eth_config.reverter_private_key);
let signer = PrivateKeySigner::new(
eth_config
.reverter_private_key
.expect("Private key is required to send revert transaction"),
);
let chain_id = web3.eth().chain_id().await.unwrap().as_u64();

let revert_function = contract
Expand Down Expand Up @@ -453,7 +455,12 @@ impl BlockReverter {
let web3 = Web3::new(Http::new(&eth_config.eth_client_url).unwrap());
let nonce = web3
.eth()
.transaction_count(eth_config.reverter_address, Some(BlockNumber::Pending))
.transaction_count(
eth_config
.reverter_address
.expect("Need to provide operator address to suggest revertion values"),
Some(BlockNumber::Pending),
)
.await
.unwrap()
.as_u64();
Expand Down
7 changes: 6 additions & 1 deletion core/tests/revert-test/tests/revert-and-restart-en.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ describe('Block reverting test', function () {
await mainNode.terminate();

console.log('Ask block_reverter to suggest to which L1 batch we should revert');
const values_json = runBlockReverter(['print-suggested-values', '--json']);
const values_json = runBlockReverter([
'print-suggested-values',
'--json',
'--operator-address',
'0xde03a0B5963f75f1C8485B355fF6D30f3093BDE7'
]);
console.log(`values = ${values_json}`);
const values = parseSuggestedValues(values_json);
assert(lastExecuted.eq(values.lastExecutedL1BatchNumber));
Expand Down
2 changes: 1 addition & 1 deletion core/tests/revert-test/tests/revert-and-restart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('Block reverting test', function () {
step('revert blocks', async () => {
const executedProcess = await utils.exec(
'cd $ZKSYNC_HOME && ' +
'RUST_LOG=off cargo run --bin block_reverter --release -- print-suggested-values --json'
'RUST_LOG=off cargo run --bin block_reverter --release -- print-suggested-values --json --operator-address 0xde03a0B5963f75f1C8485B355fF6D30f3093BDE7'
montekki marked this conversation as resolved.
Show resolved Hide resolved
// ^ Switch off logs to not pollute the output JSON
);
const suggestedValuesOutput = executedProcess.stdout;
Expand Down
Loading