Compare commits

..

1 Commits

Author SHA1 Message Date
0g-peterzhb
b5eec9ae46
Merge f456773b72 into 53449e1faa 2024-08-09 17:13:41 +08:00
3 changed files with 12 additions and 55 deletions

View File

@ -1,16 +1,9 @@
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use ethereum_types::{Address, H256, U256}; use ethereum_types::{Address, H256, U256};
use ethers::core::k256::SecretKey; use ethers::core::k256::SecretKey;
use ethers::middleware::SignerMiddleware; use ethers::middleware::SignerMiddleware;
use ethers::providers::Http; use ethers::providers::Http;
use ethers::providers::HttpRateLimitRetryPolicy;
use ethers::providers::Middleware; use ethers::providers::Middleware;
use ethers::providers::Provider; use ethers::providers::Provider;
use ethers::providers::RetryClient;
use ethers::providers::RetryClientBuilder;
use ethers::signers::LocalWallet; use ethers::signers::LocalWallet;
use ethers::signers::Signer; use ethers::signers::Signer;
use storage::config::ShardConfig; use storage::config::ShardConfig;
@ -25,12 +18,9 @@ pub struct MinerConfig {
pub(crate) cpu_percentage: u64, pub(crate) cpu_percentage: u64,
pub(crate) iter_batch: usize, pub(crate) iter_batch: usize,
pub(crate) shard_config: ShardConfig, pub(crate) shard_config: ShardConfig,
pub(crate) rate_limit_retries: u32,
pub(crate) timeout_retries: u32,
pub(crate) initial_backoff: u64,
} }
pub type MineServiceMiddleware = SignerMiddleware<Arc<Provider<RetryClient<Http>>>, LocalWallet>; pub type MineServiceMiddleware = SignerMiddleware<Provider<Http>, LocalWallet>;
impl MinerConfig { impl MinerConfig {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -44,9 +34,6 @@ impl MinerConfig {
cpu_percentage: u64, cpu_percentage: u64,
iter_batch: usize, iter_batch: usize,
shard_config: ShardConfig, shard_config: ShardConfig,
rate_limit_retries: u32,
timeout_retries: u32,
initial_backoff: u64,
) -> Option<MinerConfig> { ) -> Option<MinerConfig> {
miner_key.map(|miner_key| MinerConfig { miner_key.map(|miner_key| MinerConfig {
miner_id, miner_id,
@ -58,24 +45,12 @@ impl MinerConfig {
cpu_percentage, cpu_percentage,
iter_batch, iter_batch,
shard_config, shard_config,
rate_limit_retries,
timeout_retries,
initial_backoff,
}) })
} }
pub(crate) async fn make_provider(&self) -> Result<MineServiceMiddleware, String> { pub(crate) async fn make_provider(&self) -> Result<MineServiceMiddleware, String> {
let provider = Arc::new(Provider::new( let provider = Provider::<Http>::try_from(&self.rpc_endpoint_url)
RetryClientBuilder::default() .map_err(|e| format!("Can not parse blockchain endpoint: {:?}", e))?;
.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),
),
));
let chain_id = provider let chain_id = provider
.get_chainid() .get_chainid()
.await .await
@ -84,7 +59,6 @@ impl MinerConfig {
.map_err(|e| format!("Cannot parse private key: {:?}", e))?; .map_err(|e| format!("Cannot parse private key: {:?}", e))?;
let signer = LocalWallet::from(secret_key).with_chain_id(chain_id.as_u64()); let signer = LocalWallet::from(secret_key).with_chain_id(chain_id.as_u64());
let middleware = SignerMiddleware::new(provider, signer); let middleware = SignerMiddleware::new(provider, signer);
Ok(middleware) Ok(middleware)
} }
} }

View File

@ -1,13 +1,11 @@
use anyhow::{bail, Result}; use anyhow::{anyhow, bail, Result};
use contract_interface::ChunkLinearReward; use contract_interface::ChunkLinearReward;
use ethereum_types::Address; use ethereum_types::Address;
use ethers::prelude::{Http, Provider}; use ethers::prelude::{Http, Provider};
use ethers::providers::{HttpRateLimitRetryPolicy, RetryClient, RetryClientBuilder};
use miner::MinerMessage; use miner::MinerMessage;
use rand::Rng; use rand::Rng;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use storage::config::{ShardConfig, SHARD_CONFIG_KEY}; use storage::config::{ShardConfig, SHARD_CONFIG_KEY};
@ -36,16 +34,17 @@ pub struct PrunerConfig {
pub rpc_endpoint_url: String, pub rpc_endpoint_url: String,
pub reward_address: Address, pub reward_address: Address,
pub rate_limit_retries: u32,
pub timeout_retries: u32,
pub initial_backoff: u64,
} }
impl PrunerConfig { impl PrunerConfig {
fn start_prune_size(&self) -> u64 { fn start_prune_size(&self) -> u64 {
(self.max_num_sectors as f32 * PRUNE_THRESHOLD) as u64 (self.max_num_sectors as f32 * PRUNE_THRESHOLD) as u64
} }
fn make_provider(&self) -> Result<Provider<Http>> {
Provider::<Http>::try_from(&self.rpc_endpoint_url)
.map_err(|e| anyhow!("Can not parse blockchain endpoint: {:?}", e))
}
} }
pub struct Pruner { pub struct Pruner {
@ -58,7 +57,7 @@ pub struct Pruner {
sender: mpsc::UnboundedSender<PrunerMessage>, sender: mpsc::UnboundedSender<PrunerMessage>,
miner_sender: Option<broadcast::Sender<MinerMessage>>, miner_sender: Option<broadcast::Sender<MinerMessage>>,
reward_contract: ChunkLinearReward<Arc<Provider<RetryClient<Http>>>>, reward_contract: ChunkLinearReward<Provider<Http>>,
} }
impl Pruner { impl Pruner {
@ -74,18 +73,8 @@ impl Pruner {
let (first_rewardable_chunk, first_tx_seq) = get_first_rewardable_chunk(store.as_ref()) let (first_rewardable_chunk, first_tx_seq) = get_first_rewardable_chunk(store.as_ref())
.await? .await?
.unwrap_or((0, 0)); .unwrap_or((0, 0));
let reward_contract =
let provider = Arc::new(Provider::new( ChunkLinearReward::new(config.reward_address, Arc::new(config.make_provider()?));
RetryClientBuilder::default()
.rate_limit_retries(config.rate_limit_retries)
.timeout_retries(config.timeout_retries)
.initial_backoff(Duration::from_millis(config.initial_backoff))
.build(
Http::from_str(&config.rpc_endpoint_url)?,
Box::new(HttpRateLimitRetryPolicy),
),
));
let reward_contract = ChunkLinearReward::new(config.reward_address, Arc::new(provider));
let (tx, rx) = mpsc::unbounded_channel(); let (tx, rx) = mpsc::unbounded_channel();
let pruner = Pruner { let pruner = Pruner {
config, config,

View File

@ -181,9 +181,6 @@ impl ZgsConfig {
cpu_percentage, cpu_percentage,
iter_batch, iter_batch,
shard_config, shard_config,
self.rate_limit_retries,
self.timeout_retries,
self.initial_backoff,
)) ))
} }
@ -219,9 +216,6 @@ impl ZgsConfig {
batch_wait_time: Duration::from_millis(self.prune_batch_wait_time_ms), batch_wait_time: Duration::from_millis(self.prune_batch_wait_time_ms),
rpc_endpoint_url: self.blockchain_rpc_endpoint.clone(), rpc_endpoint_url: self.blockchain_rpc_endpoint.clone(),
reward_address, reward_address,
rate_limit_retries: self.rate_limit_retries,
timeout_retries: self.timeout_retries,
initial_backoff: self.initial_backoff,
})) }))
} else { } else {
Ok(None) Ok(None)