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

ROLLUP-437: complete withdrawals configuration #104

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/bin/zksync_api/src/api_server/rest/v02/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ApiConfigData {
gov_contract: Address,
deposit_confirmations: u64,
zksync_version: ZksyncVersion,
complete_withdrawals: bool,
// TODO: server_version (ZKS-627)
}

Expand All @@ -35,6 +36,7 @@ impl ApiConfigData {
gov_contract: config.contracts.governance_addr,
deposit_confirmations: config.eth_watch.confirmations_for_eth_event,
zksync_version: ZksyncVersion::ContractV4,
complete_withdrawals: config.eth_sender.sender.complete_withdrawals,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/bin/zksync_eth_sender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ impl<DB: DatabaseInterface> ETHSender<DB> {
.encode_tx_data("proveBlocks", args.as_slice())
}
AggregatedOperation::ExecuteBlocks(operation) => {
let args = operation.get_eth_tx_args();
let args = operation.get_eth_tx_args(self.options.sender.complete_withdrawals);
self.rootstock
.encode_tx_data("executeBlocks", args.as_slice())
}
Expand Down
20 changes: 20 additions & 0 deletions core/bin/zksync_eth_sender/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ pub(crate) async fn default_eth_sender() -> ETHSender<MockDatabase> {
Vec::new(),
Vec::new(),
default_eth_parameters(),
false,
)
.await
}
Expand All @@ -393,6 +394,7 @@ pub(crate) async fn concurrent_eth_sender(max_txs_in_flight: u64) -> ETHSender<M
Vec::new(),
Vec::new(),
default_eth_parameters(),
false,
)
.await
}
Expand All @@ -413,6 +415,7 @@ pub(crate) async fn restored_eth_sender(
aggregated_operations,
unprocessed_operations,
eth_parameters,
false,
)
.await
}
Expand All @@ -424,6 +427,7 @@ async fn build_eth_sender(
aggregated_operations: Vec<(i64, AggregatedOperation)>,
unprocessed_operations: Vec<(i64, AggregatedOperation)>,
eth_parameters: ETHParams,
complete_withdrawals: bool,
) -> ETHSender<MockDatabase> {
let rootstock = RootstockGateway::Mock(MockEthereum::default());
let db = MockDatabase::with_restorable_state(
Expand All @@ -442,6 +446,7 @@ async fn build_eth_sender(
is_enabled: true,
operator_commit_eth_addr: Default::default(),
operator_private_key: Default::default(),
complete_withdrawals,
},
gas_price_limit: GasLimit {
default: 1000,
Expand All @@ -454,6 +459,21 @@ async fn build_eth_sender(
ETHSender::new(options, db, rootstock).await
}

/// Helper method for configurable creation of `ETHSender`.
pub(crate) async fn complete_withdrawals_eth_sender(
complete_withdrawals: bool,
) -> ETHSender<MockDatabase> {
build_eth_sender(
1,
Vec::new(),
Vec::new(),
Vec::new(),
default_eth_parameters(),
complete_withdrawals,
)
.await
}

/// Behaves the same as `ETHSender::sign_new_tx`, but does not affect nonce.
/// This method should be used to create expected tx copies which won't affect
/// the internal `ETHSender` state.
Expand Down
34 changes: 32 additions & 2 deletions core/bin/zksync_eth_sender/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Local uses
use self::mock::{
concurrent_eth_sender, create_signed_tx, default_eth_parameters, default_eth_sender,
restored_eth_sender,
complete_withdrawals_eth_sender, concurrent_eth_sender, create_signed_tx,
default_eth_parameters, default_eth_sender, restored_eth_sender,
};
use super::{transactions::TxCheckOutcome, ETHSender, TxCheckMode};
use web3::types::U64;
Expand Down Expand Up @@ -972,3 +972,33 @@ async fn concurrent_operations_order() {
}
}
}

#[tokio::test]
async fn complete_withdrawals_true() {
let eth_sender = complete_withdrawals_eth_sender(true).await;

let operations_count = 1;
let execute_operations = &test_data::EXECUTE_BLOCKS_OPERATIONS[..operations_count];
let op = &execute_operations.first().unwrap().1;

let raw_tx = eth_sender.operation_to_raw_tx(op);

let complete_withdrawals = raw_tx[..64].last().unwrap();

assert!(*complete_withdrawals == 1);
}

#[tokio::test]
async fn complete_withdrawals_false() {
let eth_sender = complete_withdrawals_eth_sender(false).await;

let operations_count = 1;
let execute_operations = &test_data::EXECUTE_BLOCKS_OPERATIONS[..operations_count];
let op = &execute_operations.first().unwrap().1;

let raw_tx = eth_sender.operation_to_raw_tx(op);

let complete_withdrawals = raw_tx[..64].last().unwrap();

assert!(*complete_withdrawals == 0);
}
50 changes: 47 additions & 3 deletions core/lib/config/src/configs/eth_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::time::Duration;
// External uses
use serde::Deserialize;
// Workspace uses
use zksync_types::{Address, H256};
use zksync_types::{network::Network, Address, H256};
// Local uses
use crate::envy_load;
use crate::{configs::chain::Eth, envy_load, ETHClientConfig};

/// Configuration for the Rootstock sender crate.
#[derive(Debug, Deserialize, Clone, PartialEq)]
Expand All @@ -18,8 +18,18 @@ pub struct ETHSenderConfig {

impl ETHSenderConfig {
pub fn from_env() -> Self {
let eth: Eth = envy_load!("eth", "CHAIN_ETH_");
let sender: Sender = envy_load!("eth_sender", "ETH_SENDER_SENDER_");
let client: ETHClientConfig = envy_load!("eth_client", "ETH_CLIENT_");

assert!(
!(sender.complete_withdrawals
&& (eth.network == Network::Mainnet || client.chain_id == 30)),
"The withdrawals cannot be automatic in mainnet"
);

Self {
sender: envy_load!("eth_sender", "ETH_SENDER_SENDER_"),
sender,
gas_price_limit: envy_load!(
"eth_sender.gas_price_limit",
"ETH_SENDER_GAS_PRICE_LIMIT_"
Expand All @@ -44,6 +54,8 @@ pub struct Sender {
pub max_txs_in_flight: u64,
/// Whether sender should interact with L1 or not.
pub is_enabled: bool,
/// Automatic withdrawals in execute aggregated operation
pub complete_withdrawals: bool,
}

impl Sender {
Expand Down Expand Up @@ -94,6 +106,7 @@ mod tests {
"c1783a9a8222e47778911c58bb5aac1343eb425159ff140799e0a283bfb8fa16",
),
operator_commit_eth_addr: addr("debe71e1de41fc77c44df4b6db940026e31b0e71"),
complete_withdrawals: false,
},
gas_price_limit: GasLimit {
default: 400000000000,
Expand All @@ -112,12 +125,17 @@ ETH_SENDER_SENDER_EXPECTED_WAIT_TIME_BLOCK="30"
ETH_SENDER_SENDER_TX_POLL_PERIOD="3"
ETH_SENDER_SENDER_MAX_TXS_IN_FLIGHT="3"
ETH_SENDER_SENDER_IS_ENABLED="true"
ETH_SENDER_SENDER_COMPLETE_WITHDRAWALS="false"
ETH_SENDER_SENDER_OPERATOR_PRIVATE_KEY="0xc1783a9a8222e47778911c58bb5aac1343eb425159ff140799e0a283bfb8fa16"
ETH_SENDER_SENDER_OPERATOR_COMMIT_ETH_ADDR="0xdebe71e1de41fc77c44df4b6db940026e31b0e71"
ETH_SENDER_GAS_PRICE_LIMIT_DEFAULT="400000000000"
ETH_SENDER_GAS_PRICE_LIMIT_UPDATE_INTERVAL="150"
ETH_SENDER_GAS_PRICE_LIMIT_SAMPLE_INTERVAL="15"
ETH_SENDER_GAS_PRICE_LIMIT_SCALE_FACTOR="1"
CHAIN_ETH_NETWORK="mainnet"
ETH_CLIENT_CHAIN_ID=30
ETH_CLIENT_GAS_PRICE_FACTOR="1"
ETH_CLIENT_WEB3_URL="http://127.0.0.1:4444"
"#;
set_env(config);

Expand All @@ -144,4 +162,30 @@ ETH_SENDER_GAS_PRICE_LIMIT_SCALE_FACTOR="1"
Duration::from_secs(config.gas_price_limit.sample_interval)
);
}

#[test]
#[should_panic(expected = "The withdrawals cannot be automatic in mainnet")]
fn from_env_mainnet() {
let config = r#"
ETH_SENDER_SENDER_WAIT_CONFIRMATIONS="1"
ETH_SENDER_SENDER_EXPECTED_WAIT_TIME_BLOCK="30"
ETH_SENDER_SENDER_TX_POLL_PERIOD="3"
ETH_SENDER_SENDER_MAX_TXS_IN_FLIGHT="3"
ETH_SENDER_SENDER_IS_ENABLED="true"
ETH_SENDER_SENDER_COMPLETE_WITHDRAWALS="true"
ETH_SENDER_SENDER_OPERATOR_PRIVATE_KEY="0xc1783a9a8222e47778911c58bb5aac1343eb425159ff140799e0a283bfb8fa16"
ETH_SENDER_SENDER_OPERATOR_COMMIT_ETH_ADDR="0xdebe71e1de41fc77c44df4b6db940026e31b0e71"
ETH_SENDER_GAS_PRICE_LIMIT_DEFAULT="400000000000"
ETH_SENDER_GAS_PRICE_LIMIT_UPDATE_INTERVAL="150"
ETH_SENDER_GAS_PRICE_LIMIT_SAMPLE_INTERVAL="15"
ETH_SENDER_GAS_PRICE_LIMIT_SCALE_FACTOR="1"
CHAIN_ETH_NETWORK="mainnet"
ETH_CLIENT_CHAIN_ID=30
ETH_CLIENT_GAS_PRICE_FACTOR="1"
ETH_CLIENT_WEB3_URL="http://127.0.0.1:4444"
"#;
set_env(config);

ETHSenderConfig::from_env();
}
}
6 changes: 2 additions & 4 deletions core/lib/types/src/aggregated_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,15 @@ impl BlocksExecuteOperation {
Token::Tuple(vec![stored_block, processable_ops_pubdata])
}

pub fn get_eth_tx_args(&self) -> Vec<Token> {
// Does not make automatic withdrawals in execute operation
let complete_withdrawals = Token::Bool(false);
pub fn get_eth_tx_args(&self, complete_withdrawals: bool) -> Vec<Token> {
vec![
Token::Array(
self.blocks
.iter()
.map(BlocksExecuteOperation::get_eth_tx_args_for_block)
.collect(),
),
complete_withdrawals,
Token::Bool(complete_withdrawals),
]
}

Expand Down
2 changes: 1 addition & 1 deletion core/tests/testkit/src/eth_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl RootstockAccount {
) -> Result<ETHExecResult, anyhow::Error> {
let data = self.main_contract_eth_client.encode_tx_data(
"executeBlocks",
execute_operation.get_eth_tx_args().as_slice(),
execute_operation.get_eth_tx_args(false).as_slice(),
antomor marked this conversation as resolved.
Show resolved Hide resolved
);

let signed_tx = self
Expand Down
3 changes: 3 additions & 0 deletions etc/env/base/eth_sender.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ tx_poll_period=1
max_txs_in_flight=1
# Whether sender should interact with L1 or not.
is_enabled=true
# Automatic withdrawals in execute aggregated operation
complete_withdrawals=false


[eth_sender.gas_price_limit]
# Gas price limit to be used by GasAdjuster until the statistics data is gathered.
Expand Down
2 changes: 1 addition & 1 deletion etc/env/mainnet/api.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ url="http://127.0.0.1:8080"
port=3001
use_https=false
url="https://127.0.0.1:3001"
chain_id=30

# Configuration for the JSON RPC server
[api.json_rpc]
Expand All @@ -59,6 +58,7 @@ ws_url="ws://127.0.0.1:3031"
port=3002
url="http://127.0.0.1:3002"
max_block_range=10
chain_id=30

# Configuration for the core private server.
[api.private]
Expand Down
2 changes: 1 addition & 1 deletion etc/env/mainnet/chain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ balance_tree_depth=32

[chain.eth]
# Name of the used RSK network (since Ethers uses localhost for different works as Rootstock, different RSK networks are differentiated by its chain id in eth_client.toml)
network="localhost"
network="mainnet"

[chain.state_keeper]
# Block sizes to be generated by server.
Expand Down
2 changes: 2 additions & 0 deletions etc/env/mainnet/eth_sender.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tx_poll_period=3
max_txs_in_flight=1
# Whether sender should interact with L1 or not.
is_enabled=true
# Automatic withdrawals in execute aggregated operation
complete_withdrawals=false

[eth_sender.gas_price_limit]
# Gas price limit to be used by GasAdjuster until the statistics data is gathered.
Expand Down
2 changes: 2 additions & 0 deletions etc/env/qa/eth_sender.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tx_poll_period=3
max_txs_in_flight=1
# Whether sender should interact with L1 or not.
is_enabled=true
# Automatic withdrawals in execute aggregated operation
complete_withdrawals=true

[eth_sender.gas_price_limit]
# Gas price limit to be used by GasAdjuster until the statistics data is gathered.
Expand Down
2 changes: 2 additions & 0 deletions etc/env/testnet/eth_sender.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tx_poll_period=3
max_txs_in_flight=1
# Whether sender should interact with L1 or not.
is_enabled=true
# Automatic withdrawals in execute aggregated operation
complete_withdrawals=true

[eth_sender.gas_price_limit]
# Gas price limit to be used by GasAdjuster until the statistics data is gathered.
Expand Down
2 changes: 2 additions & 0 deletions etc/env/zk_original_configuration/eth_sender.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tx_poll_period=3
max_txs_in_flight=3
# Whether sender should interact with L1 or not.
is_enabled=true
# Automatic withdrawals in execute aggregated operation
complete_withdrawals=false

[eth_sender.gas_price_limit]
# Gas price limit to be used by GasAdjuster until the statistics data is gathered.
Expand Down
Loading