From 163f8435817414537b24184c8ae39611928c9b94 Mon Sep 17 00:00:00 2001 From: Chenxing Li Date: Thu, 11 Apr 2024 11:52:03 +0800 Subject: [PATCH] Configurable mine submission gas limit (#41) * Configurable mine submission gas limit * cargo fmt * More log * update * cargo clippy --- Cargo.lock | 1 + node/miner/Cargo.toml | 1 + node/miner/src/config.rs | 5 ++++- node/miner/src/submitter.rs | 31 ++++++++++++++++++++++++++++++- node/src/config/convert.rs | 4 +++- node/src/config/mod.rs | 1 + 6 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d92a51..d0494e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4425,6 +4425,7 @@ dependencies = [ "contract-interface", "ethereum-types 0.14.1", "ethers", + "hex", "lazy_static", "network", "rand 0.8.5", diff --git a/node/miner/Cargo.toml b/node/miner/Cargo.toml index 58d9e3a..3fafda9 100644 --- a/node/miner/Cargo.toml +++ b/node/miner/Cargo.toml @@ -20,3 +20,4 @@ ethers = "^2" lazy_static = "1.4" async-trait = "0.1.56" shared_types = { path = "../shared_types" } +hex = "0.4" \ No newline at end of file diff --git a/node/miner/src/config.rs b/node/miner/src/config.rs index 8531c4f..01078d0 100644 --- a/node/miner/src/config.rs +++ b/node/miner/src/config.rs @@ -1,4 +1,4 @@ -use ethereum_types::{Address, H256}; +use ethereum_types::{Address, H256, U256}; use ethers::core::k256::SecretKey; use ethers::middleware::SignerMiddleware; use ethers::providers::Http; @@ -13,6 +13,7 @@ pub struct MinerConfig { pub(crate) rpc_endpoint_url: String, pub(crate) mine_address: Address, pub(crate) flow_address: Address, + pub(crate) submission_gas: Option, } pub type MineServiceMiddleware = SignerMiddleware, LocalWallet>; @@ -24,6 +25,7 @@ impl MinerConfig { rpc_endpoint_url: String, mine_address: Address, flow_address: Address, + submission_gas: Option, ) -> Option { match (miner_id, miner_key) { (Some(miner_id), Some(miner_key)) => Some(MinerConfig { @@ -32,6 +34,7 @@ impl MinerConfig { rpc_endpoint_url, mine_address, flow_address, + submission_gas, }), _ => None, } diff --git a/node/miner/src/submitter.rs b/node/miner/src/submitter.rs index d01d287..bd5ec03 100644 --- a/node/miner/src/submitter.rs +++ b/node/miner/src/submitter.rs @@ -1,6 +1,9 @@ use contract_interface::PoraAnswer; use contract_interface::{PoraMine, ZgsFlow}; +use ethereum_types::U256; +use ethers::contract::ContractCall; use ethers::providers::PendingTransaction; +use hex::ToHex; use shared_types::FlowRangeProof; use std::sync::Arc; use storage::log_store::Store; @@ -18,6 +21,7 @@ pub struct Submitter { mine_answer_receiver: mpsc::UnboundedReceiver, mine_contract: PoraMine, flow_contract: ZgsFlow, + default_gas_limit: Option, store: Arc>, } @@ -31,12 +35,14 @@ impl Submitter { ) { let mine_contract = PoraMine::new(config.mine_address, provider.clone()); let flow_contract = ZgsFlow::new(config.flow_address, provider); + let default_gas_limit = config.submission_gas; let submitter = Submitter { mine_answer_receiver, mine_contract, flow_contract, store, + default_gas_limit, }; executor.spawn( async move { Box::pin(submitter.start()).await }, @@ -97,12 +103,35 @@ impl Submitter { }; trace!("submit_answer: answer={:?}", answer); - let submission_call = self.mine_contract.submit(answer).legacy(); + let mut submission_call: ContractCall<_, _> = self.mine_contract.submit(answer).legacy(); + + if let Some(gas_limit) = self.default_gas_limit { + submission_call = submission_call.gas(gas_limit); + } + + if let Some(calldata) = submission_call.calldata() { + debug!( + "Submission transaction calldata: {}", + calldata.encode_hex::() + ); + } + + debug!("Local construct tx: {:?}", &submission_call.tx); + debug!( + "Estimate gas result: {:?}", + submission_call.estimate_gas().await + ); + let pending_transaction: PendingTransaction<'_, _> = submission_call .send() .await .map_err(|e| format!("Fail to send mine answer transaction: {:?}", e))?; + debug!( + "Signed submission transaction hash: {:?}", + pending_transaction.tx_hash() + ); + let receipt = pending_transaction .retries(SUBMISSION_RETIES) .await diff --git a/node/src/config/convert.rs b/node/src/config/convert.rs index 2f3a6ce..6720436 100644 --- a/node/src/config/convert.rs +++ b/node/src/config/convert.rs @@ -1,7 +1,7 @@ #![allow(clippy::field_reassign_with_default)] use crate::ZgsConfig; -use ethereum_types::H256; +use ethereum_types::{H256, U256}; use log_entry_sync::{CacheConfig, ContractAddress, LogSyncConfig}; use miner::MinerConfig; use network::NetworkConfig; @@ -139,12 +139,14 @@ impl ZgsConfig { } else { None }; + let submission_gas = self.miner_submission_gas.map(U256::from); Ok(MinerConfig::new( miner_id, miner_key, self.blockchain_rpc_endpoint.clone(), mine_address, flow_address, + submission_gas, )) } diff --git a/node/src/config/mod.rs b/node/src/config/mod.rs index 74f5c0f..6400294 100644 --- a/node/src/config/mod.rs +++ b/node/src/config/mod.rs @@ -63,6 +63,7 @@ build_config! { (mine_contract_address, (String), "".to_string()) (miner_id, (Option), None) (miner_key, (Option), None) + (miner_submission_gas, (Option), None) } #[derive(Debug, Default, Deserialize)]