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
796 changes: 468 additions & 328 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = "A tool to submit NPoS election solutions for substrate based chai
resolver = "2"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.7.4" }
codec = { package = "parity-scale-codec", version = "3.7.5" }
scale-info = { package = "scale-info", version = "2.11.6" }
clap = { version = "4.5", features = ["derive", "env"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
Expand All @@ -31,8 +31,8 @@ pin-project-lite = "0.2"

# subxt
scale-value = "0.18"
subxt = { version = "0.41.0", features = ["reconnecting-rpc-client"] }
subxt-rpcs = "0.41.0"
subxt = { version = "0.42.1", features = ["reconnecting-rpc-client"] }
subxt-rpcs = "0.42.1"


# polkadot-sdk
Expand All @@ -42,9 +42,10 @@ polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", features =
"sp-npos-elections",
"sp-core",
"sp-runtime",
"sp-staking",
"pallet-election-provider-multi-phase",
"pallet-election-provider-multi-block",
], branch = "master" }
], branch = "donal-ahm" }

# prometheus
prometheus = "0.14"
Expand Down
Binary file modified artifacts/multi_block.scale
Binary file not shown.
8 changes: 6 additions & 2 deletions src/commands/legacy/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
dynamic::legacy as dynamic,
error::Error,
prelude::{
AccountId, ChainClient, ExtrinsicParamsBuilder, Hash, Header, LOG_TARGET, RpcClient,
AccountId, ChainClient, Config, ExtrinsicParamsBuilder, Hash, Header, LOG_TARGET, RpcClient,
},
prometheus,
runtime::legacy as runtime,
Expand All @@ -31,6 +31,7 @@ use crate::{
wait_for_in_block,
},
};

use codec::{Decode, Encode};
use futures::future::TryFutureExt;
use jsonrpsee::core::ClientError as JsonRpseeError;
Expand All @@ -40,6 +41,7 @@ use polkadot_sdk::{
sp_npos_elections,
};
use std::sync::Arc;
use subxt::config::Hasher;
use subxt::{backend::legacy::rpc_methods::DryRunResult, config::Header as _};
use tokio::sync::Mutex;

Expand Down Expand Up @@ -152,7 +154,9 @@ where
+ 'static,
T::Solution: Send,
{
let block_hash = at.hash();
let hasher = <Config as subxt::Config>::Hasher::new(&client.chain_api().metadata());
let encoded_header = at.encode();
let block_hash = hasher.hash(&encoded_header);
log::trace!(target: LOG_TARGET, "new event at #{:?} ({:?})", at.number(), block_hash);

// NOTE: as we try to send at each block then the nonce is used guard against
Expand Down
21 changes: 12 additions & 9 deletions src/commands/multi_block/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use polkadot_sdk::{
sp_npos_elections::ElectionScore,
};
use std::{collections::HashSet, sync::Arc};
use subxt::config::Header;
use tokio::sync::Mutex;

pub async fn monitor_cmd<T>(
Expand Down Expand Up @@ -74,12 +73,12 @@ where
let mut prev_block_signed_phase = false;

loop {
let at = tokio::select! {
let (at, block_hash) = tokio::select! {
maybe_block = subscription.next() => {
match maybe_block {
Some(block_result) => {
match block_result {
Ok(block) => block.header().clone(),
Ok(block) => (block.header().clone(), block.hash()),
Err(e) => {
// Handle reconnection case with the reconnecting RPC client
if e.is_disconnected_will_reconnect() {
Expand Down Expand Up @@ -110,17 +109,17 @@ where
// Early exit optimization: check the phase before calling BlockDetails::new(), where we
// we fetch `storage_at()`, `round()`, and `desired_targets()`.
// This approach saves us 3 RPC calls.
let storage = utils::storage_at(Some(at.hash()), client.chain_api()).await?;
let storage = utils::storage_at(Some(block_hash), client.chain_api()).await?;
let phase = storage
.fetch_or_default(&runtime::storage().multi_block().current_phase())
.fetch_or_default(&runtime::storage().multi_block_election().current_phase())
.await?;

if !matches!(phase, Phase::Signed(_) | Phase::Snapshot(_)) {
log::trace!(target: LOG_TARGET, "Phase {:?} - nothing to do", phase);
log::trace!(target: LOG_TARGET, "Block #{}, Phase {:?} - nothing to do", at.number, phase);
continue;
}

let state = BlockDetails::new(&client, at, phase).await?;
let state = BlockDetails::new(&client, at, phase, block_hash).await?;
let account_info = state
.storage
.fetch(&runtime::storage().system().account(signer.account_id()))
Expand Down Expand Up @@ -392,7 +391,11 @@ async fn score_better(
strategy: SubmissionStrategy,
) -> Result<bool, Error> {
let scores = storage
.fetch_or_default(&runtime::storage().multi_block_signed().sorted_scores(round))
.fetch_or_default(
&runtime::storage()
.multi_block_election_signed()
.sorted_scores(round),
)
.await?;

if scores
Expand All @@ -416,7 +419,7 @@ async fn get_submission(
let maybe_submission = storage
.fetch(
&runtime::storage()
.multi_block_signed()
.multi_block_election_signed()
.submission_metadata_storage(round, who),
)
.await?;
Expand Down
19 changes: 14 additions & 5 deletions src/commands/multi_block/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
static_types::multi_block as static_types,
utils,
};

use polkadot_sdk::{
frame_election_provider_support, frame_support::BoundedVec,
pallet_election_provider_multi_block::unsigned::miner::MinerConfig,
Expand All @@ -17,7 +18,6 @@ use std::{
collections::{BTreeMap, HashSet},
sync::{Arc, RwLock},
};
use subxt::config::Header as _;

pub type TargetSnapshotPageOf<T> =
BoundedVec<AccountId, <T as MinerConfig>::TargetSnapshotPerBlock>;
Expand Down Expand Up @@ -145,14 +145,23 @@ pub struct BlockDetails {
}

impl BlockDetails {
pub async fn new(client: &Client, at: Header, phase: Phase) -> Result<Self, Error> {
let storage = utils::storage_at(Some(at.hash()), client.chain_api()).await?;
pub async fn new(
client: &Client,
at: Header,
phase: Phase,
block_hash: Hash,
) -> Result<Self, Error> {
let storage = utils::storage_at(Some(block_hash), client.chain_api()).await?;
let round = storage
.fetch_or_default(&runtime::storage().multi_block().round())
.fetch_or_default(&runtime::storage().multi_block_election().round())
.await?;

let desired_targets = storage
.fetch(&runtime::storage().multi_block().desired_targets(round))
.fetch(
&runtime::storage()
.multi_block_election()
.desired_targets(round),
)
.await?
.unwrap_or(0);

Expand Down
10 changes: 5 additions & 5 deletions src/dynamic/multi_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ pub(crate) async fn submit<T: MinerConfig + Send + Sync + 'static>(
// but it's performed for registering the score only once.
let tx = utils::wait_tx_in_block_for_strategy(tx_status, listen).await?;
let events = tx.wait_for_success().await?;
if !events.has::<runtime::multi_block_signed::events::Registered>()? {
if !events.has::<runtime::multi_block_election_signed::events::Registered>()? {
return Err(Error::MissingTxEvent("Register score".to_string()));
};

Expand Down Expand Up @@ -537,7 +537,7 @@ async fn submit_pages_batch<T: MinerConfig + 'static>(
let event = event?;

if let Some(solution_stored) =
event.as_event::<runtime::multi_block_signed::events::Stored>()?
event.as_event::<runtime::multi_block_election_signed::events::Stored>()?
{
let page = solution_stored.2;

Expand Down Expand Up @@ -682,7 +682,7 @@ pub(crate) async fn inner_submit_pages_chunked<T: MinerConfig + 'static>(

/// Submit a bail transaction to revert incomplete submissions
pub(crate) async fn bail(client: &Client, signer: &Signer, listen: Listen) -> Result<(), Error> {
let bail_tx = runtime::tx().multi_block_signed().bail();
let bail_tx = runtime::tx().multi_block_election_signed().bail();
let nonce = client
.rpc()
.system_account_next_index(signer.account_id())
Expand All @@ -709,7 +709,7 @@ async fn validate_signed_phase_or_bail(
) -> Result<bool, Error> {
let storage = utils::storage_at_head(client, listen).await?;
let current_phase = storage
.fetch_or_default(&runtime::storage().multi_block().current_phase())
.fetch_or_default(&runtime::storage().multi_block_election().current_phase())
.await?;

// Import Phase enum from runtime types
Expand All @@ -728,7 +728,7 @@ async fn validate_signed_phase_or_bail(
let maybe_submission = storage
.fetch(
&runtime::storage()
.multi_block_signed()
.multi_block_election_signed()
.submission_metadata_storage(round, signer.account_id()),
)
.await?;
Expand Down
6 changes: 3 additions & 3 deletions src/dynamic/pallet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<T: DeserializeOwned + std::fmt::Display> PalletConstant<T> {
}

pub mod multi_block {
pub const NAME: &str = "MultiBlock";
pub const NAME: &str = "MultiBlockElection";

pub mod constants {
use super::{super::*, *};
Expand All @@ -90,7 +90,7 @@ pub mod multi_block {
}

pub mod multi_block_verifier {
pub const NAME: &str = "MultiBlockVerifier";
pub const NAME: &str = "MultiBlockElectionVerifier";

pub mod constants {
use super::{super::*, *};
Expand Down Expand Up @@ -132,7 +132,7 @@ pub mod election_provider_multi_phase {
}

pub mod multi_block_signed {
pub const NAME: &str = "MultiBlockSigned";
pub const NAME: &str = "MultiBlockElectionSigned";

pub mod tx {
use super::{super::*, *};
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// The header type. We re-export it here, but we can easily get it from block as well.
pub type Header =
subxt::config::substrate::SubstrateHeader<u32, subxt::config::substrate::BlakeTwo256>;
subxt::config::substrate::SubstrateHeader<u32, subxt::config::substrate::DynamicHasher256>;
/// The header type. We re-export it here, but we can easily get it from block as well.
pub type Hash = subxt::utils::H256;
/// Default URI to connect to.
Expand Down
8 changes: 8 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
substitute_type(
path = "sp_npos_elections::ElectionScore",
with = "::subxt::utils::Static<polkadot_sdk::sp_npos_elections::ElectionScore>"
),
substitute_type(
path = "sp_staking::PagedExposureMetadata<Balance>",
with = "::subxt::utils::Static<u32>"
),
substitute_type(
path = "sp_staking::ExposurePage<AccountId, Balance>",
with = "::subxt::utils::Static<u32>"
)
)]
pub mod multi_block {}
Expand Down
4 changes: 2 additions & 2 deletions tests/multi_block_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use polkadot_staking_miner::{
prelude::ChainClient,
runtime::multi_block::{
self as runtime,
multi_block_signed::events::{Registered, Rewarded, Stored},
multi_block_election_signed::events::{Registered, Rewarded, Stored},
},
};
use std::collections::HashSet;
Expand Down Expand Up @@ -101,7 +101,7 @@ pub async fn wait_for_mined_solution(port: u16) -> anyhow::Result<()> {
let mut blocks_sub = api.blocks().subscribe_finalized().await?;
let pages = api
.constants()
.at(&runtime::constants().multi_block().pages())
.at(&runtime::constants().multi_block_election().pages())
.unwrap();

'outer: while let Some(block) = blocks_sub.next().await {
Expand Down
Loading