Skip to content
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
231 changes: 149 additions & 82 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ tokio = { version = "1.36", features = ["macros", "rt-multi-thread", "sync", "si
pin-project-lite = "0.2"

# subxt
scale-value = "0.14.1"
subxt = { version = "0.35.1", features = ["substrate-compat"] }
scale-value = "0.16.0"
subxt = { version = "0.37.0", features = ["substrate-compat"] }

# polkadot-sdk
frame-election-provider-support = "31.0.0"
Expand Down
6 changes: 4 additions & 2 deletions src/commands/emergency_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use clap::Parser;
use codec::Encode;
use sp_core::hexdisplay::HexDisplay;
use std::io::Write;
use subxt::tx::TxPayload;
use subxt::tx::Payload;

#[derive(Debug, Clone, Parser)]
#[cfg_attr(test, derive(PartialEq))]
Expand Down Expand Up @@ -88,7 +88,9 @@ where
}

let call = epm::set_emergency_result(supports.clone())?;
let encoded_call = call.encode_call_data(&client.chain_api().metadata())?;
let encoded_call = call
.encode_call_data(&client.chain_api().metadata())
.map_err(|e| Error::Subxt(e.into()))?;
let encoded_supports = supports.encode();

// write results to files.
Expand Down
44 changes: 23 additions & 21 deletions src/commands/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl FromStr for SubmissionStrategy {
let percent: u32 = percent.parse().map_err(|e| format!("{:?}", e))?;
Self::ClaimBetterThan(Perbill::from_percent(percent))
} else {
return Err(s.into())
return Err(s.into());
};
Ok(res)
}
Expand Down Expand Up @@ -424,7 +424,7 @@ where
e,
at.number
);
return Err(e)
return Err(e);
},
};

Expand Down Expand Up @@ -487,7 +487,7 @@ where

if let Some(submission) = submission {
if &submission.who == us {
return Err(Error::AlreadySubmitted)
return Err(Error::AlreadySubmitted);
}
}
}
Expand All @@ -503,7 +503,7 @@ async fn ensure_solution_passes_strategy(
) -> Result<(), Error> {
// don't care about current scores.
if matches!(strategy, SubmissionStrategy::Always) {
return Ok(())
return Ok(());
}

let addr = runtime::storage().election_provider_multi_phase().signed_submission_indices();
Expand Down Expand Up @@ -533,19 +533,19 @@ async fn submit_and_watch_solution<T: MinerConfig + Send + Sync + 'static>(
) -> Result<(), Error> {
let tx = epm::signed_solution(RawSolution { solution, score, round })?;

// session == epoch
let session_len = client
.chain_api()
.constants()
.at(&runtime::constants().babe().epoch_duration())?;

// By default we are using the epoch length as the mortality length.
// If that doesn't is available then we just use the default mortality provided by subxt.
//
// TODO: https://github.com/paritytech/polkadot-staking-miner/issues/730
//
// The extrinsic mortality length is static and it doesn't know when the signed phase ends.
let xt_cfg = DefaultExtrinsicParamsBuilder::default()
.nonce(nonce)
.mortal(at, session_len)
.build();
let xt_cfg = if let Ok(len) =
client.chain_api().constants().at(&runtime::constants().babe().epoch_duration())
{
DefaultExtrinsicParamsBuilder::default().nonce(nonce).mortal(at, len).build()
} else {
DefaultExtrinsicParamsBuilder::default().nonce(nonce).build()
};

let xt = client.chain_api().tx().create_signed(&tx, &*signer, xt_cfg).await?;

Expand Down Expand Up @@ -580,23 +580,25 @@ async fn submit_and_watch_solution<T: MinerConfig + Send + Sync + 'static>(
return Err(Error::Other(format!(
"No SolutionStored event found at {:?}",
in_block.block_hash()
)))
)));
}
},
Listen::Finalized => {
let finalized = tx_progress.wait_for_finalized_success().await?;
let finalized_block = tx_progress.wait_for_finalized().await?;
let block_hash = finalized_block.block_hash();
let finalized_success = finalized_block.wait_for_success().await?;

let solution_stored = finalized
let solution_stored = finalized_success
.find_first::<runtime::election_provider_multi_phase::events::SolutionStored>(
);

if let Ok(Some(_)) = solution_stored {
log::info!(target: LOG_TARGET, "Finalized at {:?}", finalized.block_hash());
log::info!(target: LOG_TARGET, "Finalized at {:?}", block_hash);
} else {
return Err(Error::Other(format!(
"No SolutionStored event found at {:?}",
finalized.block_hash()
)))
block_hash,
)));
}
},
};
Expand Down Expand Up @@ -659,7 +661,7 @@ async fn dry_run_works(rpc: &RpcClient) -> Result<(), Error> {
"dry-run requires a RPC endpoint with `--rpc-methods unsafe`; \
either connect to another RPC endpoint or disable dry-run"
.to_string(),
))
));
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/epm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ where
let max_weight: Weight = T::MaxWeight::get();

if est_weight.all_lt(max_weight) {
return Ok(Self { state: State { voters, voters_by_stake }, _marker: PhantomData })
return Ok(Self { state: State { voters, voters_by_stake }, _marker: PhantomData });
}

let Some((_, idx)) = voters_by_stake.pop_first() else { break };
Expand All @@ -146,7 +146,7 @@ where
targets.remove(&rm);
}

return Err(Error::Feasibility("Failed to pre-trim weight < T::MaxLength".to_string()))
return Err(Error::Feasibility("Failed to pre-trim weight < T::MaxLength".to_string()));
}

/// Clone the state and trim it, so it get can be reverted.
Expand All @@ -156,7 +156,7 @@ where

for _ in 0..n {
let Some((_, idx)) = voters_by_stake.pop_first() else {
return Err(Error::Feasibility("Failed to pre-trim len".to_string()))
return Err(Error::Feasibility("Failed to pre-trim len".to_string()));
};
let rm = voters[idx].0.clone();

Expand Down Expand Up @@ -222,7 +222,8 @@ fn read_constant<'a, T: serde::Deserialize<'a>>(
.constants()
.at(&subxt::dynamic::constant(epm_name, constant))
.map_err(|e| invalid_metadata_error(constant.to_string(), e))?
.to_value()?;
.to_value()
.map_err(|e| Error::Subxt(e.into()))?;

scale_value::serde::from_value::<_, T>(val).map_err(|e| {
Error::InvalidMetadata(format!("Decoding `{}` failed {}", std::any::type_name::<T>(), e))
Expand Down Expand Up @@ -390,7 +391,7 @@ where
solution,
score,
solution_or_snapshot_size,
})
});
}

prometheus::on_trim_attempt();
Expand Down Expand Up @@ -513,7 +514,7 @@ fn to_scale_value<T: scale_info::TypeInfo + 'static + Encode>(val: T) -> Result<

let bytes = val.encode();

decode_as_type(&mut bytes.as_ref(), &ty_id, &types)
decode_as_type(&mut bytes.as_ref(), ty_id, &types)
.map(|v| v.remove_context())
.map_err(|e| {
Error::DynamicTransaction(format!(
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn kill_main_task_if_critical_err(tx: &tokio::sync::mpsc::UnboundedSender<Er
"Failed to downcast RPC error; this is a bug please file an issue"
.to_string(),
));
return
return;
},
};

Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async fn runtime_upgrade_task(client: ChainClient, tx: oneshot::Sender<Error>) {
Ok(u) => u,
Err(e) => {
let _ = tx.send(e.into());
return
return;
},
};

Expand All @@ -228,10 +228,10 @@ async fn runtime_upgrade_task(client: ChainClient, tx: oneshot::Sender<Error>) {
Ok(u) => u,
Err(e) => {
let _ = tx.send(e.into());
return
return;
},
};
continue
continue;
},
};

Expand All @@ -240,7 +240,7 @@ async fn runtime_upgrade_task(client: ChainClient, tx: oneshot::Sender<Error>) {
Ok(()) => {
if let Err(e) = epm::update_metadata_constants(&client) {
let _ = tx.send(e);
return
return;
}
prometheus::on_runtime_upgrade();
log::info!(target: LOG_TARGET, "upgrade to version: {} successful", version);
Expand Down
12 changes: 6 additions & 6 deletions src/static_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub mod westend {
active_voters,
desired_targets.try_into().expect("Desired targets < u16::MAX"),
) else {
return Weight::MAX
return Weight::MAX;
};

// Mock a RawSolution to get the correct weight without having to do the heavy work.
Expand All @@ -131,7 +131,7 @@ pub mod westend {
if raw.solution.voter_count() != active_voters as usize ||
raw.solution.unique_targets().len() != desired_targets as usize
{
return Weight::MAX
return Weight::MAX;
}

futures::executor::block_on(epm::runtime_api_solution_weight(
Expand Down Expand Up @@ -177,7 +177,7 @@ pub mod polkadot {
active_voters,
desired_targets.try_into().expect("Desired targets < u16::MAX"),
) else {
return Weight::MAX
return Weight::MAX;
};

// Mock a RawSolution to get the correct weight without having to do the heavy work.
Expand All @@ -189,7 +189,7 @@ pub mod polkadot {
if raw.solution.voter_count() != active_voters as usize ||
raw.solution.unique_targets().len() != desired_targets as usize
{
return Weight::MAX
return Weight::MAX;
}

futures::executor::block_on(epm::runtime_api_solution_weight(
Expand Down Expand Up @@ -235,7 +235,7 @@ pub mod kusama {
active_voters,
desired_targets.try_into().expect("Desired targets < u16::MAX"),
) else {
return Weight::MAX
return Weight::MAX;
};

// Mock a RawSolution to get the correct weight without having to do the heavy work.
Expand All @@ -247,7 +247,7 @@ pub mod kusama {
if raw.solution.voter_count() != active_voters as usize ||
raw.solution.unique_targets().len() != desired_targets as usize
{
return Weight::MAX
return Weight::MAX;
}

futures::executor::block_on(epm::runtime_api_solution_weight(
Expand Down
4 changes: 2 additions & 2 deletions tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub async fn wait_for_mined_solution(ws_url: &str) -> anyhow::Result<SolutionSto

while let Some(block) = blocks_sub.next().await {
if now.elapsed() > MAX_DURATION_FOR_SUBMIT_SOLUTION {
break
break;
}

let block = block?;
Expand All @@ -204,7 +204,7 @@ pub async fn wait_for_mined_solution(ws_url: &str) -> anyhow::Result<SolutionSto
let ev = ev?;

if let Some(solution_ev) = ev.as_event::<SolutionStored>()? {
return Ok(solution_ev)
return Ok(solution_ev);
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ async fn submit_monitor_works_basic() {
test_submit_solution(Target::Node(Chain::Westend)).await;
}

// TODO: https://github.com/paritytech/polkadot-staking-miner/issues/806
#[tokio::test]
#[ignore]
async fn default_trimming_works() {
Expand Down Expand Up @@ -89,7 +88,7 @@ async fn has_trimming_output(miner: &mut KillChildOnDrop) -> bool {
}

if now.elapsed() > MAX_DURATION_FOR_SUBMIT_SOLUTION {
break
break;
}
}

Expand Down