2024-08-12 03:35:21 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-04-11 03:52:03 +00:00
|
|
|
use ethereum_types::{Address, H256, U256};
|
2024-01-03 10:24:52 +00:00
|
|
|
use ethers::core::k256::SecretKey;
|
|
|
|
use ethers::middleware::SignerMiddleware;
|
|
|
|
use ethers::providers::Http;
|
2024-08-12 03:35:21 +00:00
|
|
|
use ethers::providers::HttpRateLimitRetryPolicy;
|
2024-01-03 10:24:52 +00:00
|
|
|
use ethers::providers::Middleware;
|
|
|
|
use ethers::providers::Provider;
|
2024-08-12 03:35:21 +00:00
|
|
|
use ethers::providers::RetryClient;
|
|
|
|
use ethers::providers::RetryClientBuilder;
|
2024-01-03 10:24:52 +00:00
|
|
|
use ethers::signers::LocalWallet;
|
|
|
|
use ethers::signers::Signer;
|
2024-05-31 05:11:06 +00:00
|
|
|
use storage::config::ShardConfig;
|
2024-01-03 10:24:52 +00:00
|
|
|
|
|
|
|
pub struct MinerConfig {
|
2024-04-27 03:15:57 +00:00
|
|
|
pub(crate) miner_id: Option<H256>,
|
2024-01-03 10:24:52 +00:00
|
|
|
pub(crate) miner_key: H256,
|
|
|
|
pub(crate) rpc_endpoint_url: String,
|
|
|
|
pub(crate) mine_address: Address,
|
|
|
|
pub(crate) flow_address: Address,
|
2024-04-11 03:52:03 +00:00
|
|
|
pub(crate) submission_gas: Option<U256>,
|
2024-04-23 04:52:39 +00:00
|
|
|
pub(crate) cpu_percentage: u64,
|
|
|
|
pub(crate) iter_batch: usize,
|
2024-05-31 05:11:06 +00:00
|
|
|
pub(crate) shard_config: ShardConfig,
|
2024-08-12 03:35:21 +00:00
|
|
|
pub(crate) rate_limit_retries: u32,
|
|
|
|
pub(crate) timeout_retries: u32,
|
|
|
|
pub(crate) initial_backoff: u64,
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 03:35:21 +00:00
|
|
|
pub type MineServiceMiddleware = SignerMiddleware<Arc<Provider<RetryClient<Http>>>, LocalWallet>;
|
2024-01-03 10:24:52 +00:00
|
|
|
|
|
|
|
impl MinerConfig {
|
2024-04-23 04:52:39 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2024-01-03 10:24:52 +00:00
|
|
|
pub fn new(
|
|
|
|
miner_id: Option<H256>,
|
|
|
|
miner_key: Option<H256>,
|
|
|
|
rpc_endpoint_url: String,
|
|
|
|
mine_address: Address,
|
|
|
|
flow_address: Address,
|
2024-04-11 03:52:03 +00:00
|
|
|
submission_gas: Option<U256>,
|
2024-04-23 04:52:39 +00:00
|
|
|
cpu_percentage: u64,
|
|
|
|
iter_batch: usize,
|
2024-05-31 05:11:06 +00:00
|
|
|
shard_config: ShardConfig,
|
2024-08-12 03:35:21 +00:00
|
|
|
rate_limit_retries: u32,
|
|
|
|
timeout_retries: u32,
|
|
|
|
initial_backoff: u64,
|
2024-01-03 10:24:52 +00:00
|
|
|
) -> Option<MinerConfig> {
|
2024-04-27 03:15:57 +00:00
|
|
|
miner_key.map(|miner_key| MinerConfig {
|
|
|
|
miner_id,
|
|
|
|
miner_key,
|
|
|
|
rpc_endpoint_url,
|
|
|
|
mine_address,
|
|
|
|
flow_address,
|
|
|
|
submission_gas,
|
|
|
|
cpu_percentage,
|
|
|
|
iter_batch,
|
2024-04-30 06:34:44 +00:00
|
|
|
shard_config,
|
2024-08-12 03:35:21 +00:00
|
|
|
rate_limit_retries,
|
|
|
|
timeout_retries,
|
|
|
|
initial_backoff,
|
2024-04-27 03:15:57 +00:00
|
|
|
})
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn make_provider(&self) -> Result<MineServiceMiddleware, String> {
|
2024-08-12 03:35:21 +00:00
|
|
|
let provider = Arc::new(Provider::new(
|
|
|
|
RetryClientBuilder::default()
|
|
|
|
.rate_limit_retries(self.rate_limit_retries)
|
|
|
|
.timeout_retries(self.timeout_retries)
|
|
|
|
.initial_backoff(Duration::from_millis(self.initial_backoff))
|
|
|
|
.build(
|
|
|
|
Http::from_str(&self.rpc_endpoint_url)
|
|
|
|
.map_err(|e| format!("Cannot parse blockchain endpoint: {:?}", e))?,
|
|
|
|
Box::new(HttpRateLimitRetryPolicy),
|
|
|
|
),
|
|
|
|
));
|
2024-01-03 10:24:52 +00:00
|
|
|
let chain_id = provider
|
|
|
|
.get_chainid()
|
|
|
|
.await
|
|
|
|
.map_err(|e| format!("Unable to get chain_id: {:?}", e))?;
|
|
|
|
let secret_key = SecretKey::from_bytes(self.miner_key.as_ref().into())
|
|
|
|
.map_err(|e| format!("Cannot parse private key: {:?}", e))?;
|
|
|
|
let signer = LocalWallet::from(secret_key).with_chain_id(chain_id.as_u64());
|
|
|
|
let middleware = SignerMiddleware::new(provider, signer);
|
2024-08-12 03:35:21 +00:00
|
|
|
|
2024-01-03 10:24:52 +00:00
|
|
|
Ok(middleware)
|
|
|
|
}
|
|
|
|
}
|