diff --git a/utils/staking-miner/src/main.rs b/utils/staking-miner/src/main.rs index fd1281e2ec59..fc7c018ba72d 100644 --- a/utils/staking-miner/src/main.rs +++ b/utils/staking-miner/src/main.rs @@ -249,6 +249,7 @@ enum Error { AlreadySubmitted, VersionMismatch, StrategyNotSatisfied, + Other(String), } impl From for Error { diff --git a/utils/staking-miner/src/monitor.rs b/utils/staking-miner/src/monitor.rs index 34e522b5b050..0553693a84ef 100644 --- a/utils/staking-miner/src/monitor.rs +++ b/utils/staking-miner/src/monitor.rs @@ -138,6 +138,21 @@ async fn ensure_no_better_solution( } } +async fn get_latest_head( + rpc: &SharedRpcClient, + mode: &str, +) -> Result> { + if mode == "head" { + match rpc.block_hash(None).await { + Ok(Some(hash)) => Ok(hash), + Ok(None) => Err(Error::Other("Best head not found".into())), + Err(e) => Err(e.into()), + } + } else { + rpc.finalized_head().await.map_err(Into::into) + } +} + macro_rules! monitor_cmd_for { ($runtime:tt) => { paste::paste! { /// The monitor command. @@ -291,13 +306,21 @@ macro_rules! monitor_cmd_for { ($runtime:tt) => { paste::paste! { let rpc1 = rpc.clone(); let rpc2 = rpc.clone(); + let latest_head = match get_latest_head::(&rpc, &config.listen).await { + Ok(hash) => hash, + Err(e) => { + log::debug!(target: LOG_TARGET, "Skipping to submit at block {}; {}", at.number, e); + return; + } + }; + let ensure_no_better_fut = tokio::spawn(async move { - ensure_no_better_solution::(&rpc1, hash, score, config.submission_strategy, + ensure_no_better_solution::(&rpc1, latest_head, score, config.submission_strategy, SignedMaxSubmissions::get()).await }); let ensure_signed_phase_fut = tokio::spawn(async move { - ensure_signed_phase::(&rpc2, hash).await + ensure_signed_phase::(&rpc2, latest_head).await }); // Run the calls in parallel and return once all has completed or any failed. diff --git a/utils/staking-miner/src/rpc.rs b/utils/staking-miner/src/rpc.rs index b609e197b49d..4cf10f9ffef7 100644 --- a/utils/staking-miner/src/rpc.rs +++ b/utils/staking-miner/src/rpc.rs @@ -68,6 +68,16 @@ pub trait RpcApi { #[method(name = "system_dryRun")] async fn dry_run(&self, extrinsic: &Bytes, at: Option) -> RpcResult; + /// Get hash of the n-th block in the canon chain. + /// + /// By default returns latest block hash. + #[method(name = "chain_getBlockHash", aliases = ["chain_getHead"], blocking)] + fn block_hash(&self, hash: Option) -> RpcResult>; + + /// Get hash of the last finalized block in the canon chain. + #[method(name = "chain_getFinalizedHead", aliases = ["chain_getFinalisedHead"], blocking)] + fn finalized_head(&self) -> RpcResult; + /// Submit an extrinsic to watch. /// /// See [`TransactionStatus`](sc_transaction_pool_api::TransactionStatus) for details on