Skip to content

Commit

Permalink
chore: update deps to publish on crates.io (#715)
Browse files Browse the repository at this point in the history
* update subxt

* update deps to crates.io

* cleanup

* revert runtime api usage

* add missing files

* strip metadata

* downgrade metadata work with polkadot

* remove unused staking miner tests
  • Loading branch information
niklasad1 authored Nov 23, 2023
1 parent e4e464a commit 0f274c7
Show file tree
Hide file tree
Showing 15 changed files with 1,626 additions and 804 deletions.
1,968 changes: 1,399 additions & 569 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0" }
scale-info = { package = "scale-info", version = "2.10.0" }
clap = { version = "4.4", features = ["derive", "env"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
jsonrpsee = { version = "0.16", features = ["ws-client"] }
jsonrpsee = { version = "0.20", features = ["ws-client"] }
log = "0.4"
serde = "1.0"
serde_json = "1.0"
Expand All @@ -19,15 +19,15 @@ tokio = { version = "1.34", features = ["macros", "rt-multi-thread", "sync", "si
pin-project-lite = "0.2"

# subxt
subxt = "0.29"
scale-value = "0.10.0"
subxt = { version = "0.32.1", features = ["substrate-compat"] }
scale-value = "0.12.0"

# polkadot-sdk
frame-election-provider-support = { git = "https://github.com/paritytech/polkadot-sdk" }
pallet-election-provider-multi-phase = { git = "https://github.com/paritytech/polkadot-sdk" }
sp-npos-elections = { git = "https://github.com/paritytech/polkadot-sdk/" }
frame-support = { git = "https://github.com/paritytech/polkadot-sdk" }
sp-runtime = { git = "https://github.com/paritytech/polkadot-sdk" }
frame-election-provider-support = "26.0.0"
pallet-election-provider-multi-phase = "25.0.0"
sp-npos-elections = "24.0.0"
frame-support = "26.0.0"
sp-runtime = "29.0.0"

# prometheus
prometheus = "0.13"
Expand All @@ -37,10 +37,9 @@ once_cell = "1.18"
[dev-dependencies]
anyhow = "1"
assert_cmd = "2.0"
sp-storage = { git = "https://github.com/paritytech/polkadot-sdk" }
sp-storage = "17.0.0"
regex = "1"

[features]
default = []
slow-tests = []
staking-miner-playground-tests = []
Binary file modified artifacts/metadata.scale
Binary file not shown.
67 changes: 67 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::prelude::*;
use jsonrpsee::ws_client::WsClientBuilder;
use subxt::backend::rpc::RpcClient as RawRpcClient;

/// Wraps the subxt interfaces to make it easy to use for the staking-miner.
#[derive(Clone, Debug)]
pub struct Client {
/// Access to typed rpc calls from subxt.
rpc: RpcClient,
/// Access to chain APIs such as storage, events etc.
chain_api: ChainClient,
/// Raw RPC client.
raw_rpc: RawRpcClient,
}

impl Client {
pub async fn new(uri: &str) -> Result<Self, subxt::Error> {
log::debug!(target: LOG_TARGET, "attempting to connect to {:?}", uri);

let rpc = loop {
match WsClientBuilder::default()
.max_request_size(u32::MAX)
.max_response_size(u32::MAX)
.request_timeout(std::time::Duration::from_secs(600))
.build(&uri)
.await
{
Ok(rpc) => break RawRpcClient::new(rpc),
Err(e) => {
log::warn!(
target: LOG_TARGET,
"failed to connect to client due to {:?}, retrying soon..",
e,
);
},
};
tokio::time::sleep(std::time::Duration::from_millis(2_500)).await;
};

let chain_api = ChainClient::from_rpc_client(rpc.clone()).await?;

Ok(Self { rpc: RpcClient::new(rpc.clone()), raw_rpc: rpc, chain_api })
}

/// Get a reference to the RPC interface exposed by subxt.
pub fn rpc(&self) -> &RpcClient {
&self.rpc
}

/// Get a reference to the chain API.
pub fn chain_api(&self) -> &ChainClient {
&self.chain_api
}

// This is exposed until a new version of subxt is released.
pub async fn rpc_system_account_next_index<T>(
&self,
account_id: &T,
) -> Result<u64, subxt::Error>
where
T: serde::Serialize,
{
self.raw_rpc
.request("system_accountNextIndex", subxt::rpc_params![&account_id])
.await
}
}
25 changes: 14 additions & 11 deletions src/commands/dry_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
use pallet_election_provider_multi_phase::RawSolution;

use crate::{
epm, error::Error, helpers::storage_at, opt::Solver, prelude::*, signer::Signer, static_types,
client::Client, epm, error::Error, helpers::storage_at, opt::Solver, prelude::*,
signer::Signer, static_types,
};
use clap::Parser;
use codec::Encode;
Expand Down Expand Up @@ -56,21 +57,21 @@ pub struct DryRunConfig {
pub seed_or_path: Option<String>,
}

pub async fn dry_run_cmd<T>(api: SubxtClient, config: DryRunConfig) -> Result<(), Error>
pub async fn dry_run_cmd<T>(client: Client, config: DryRunConfig) -> Result<(), Error>
where
T: MinerConfig<AccountId = AccountId, MaxVotesPerVoter = static_types::MaxVotesPerVoter>
+ Send
+ Sync
+ 'static,
T::Solution: Send,
{
let storage = storage_at(config.at, &api).await?;
let storage = storage_at(config.at, client.chain_api()).await?;
let round = storage
.fetch_or_default(&runtime::storage().election_provider_multi_phase().round())
.await?;

let miner_solution = epm::fetch_snapshot_and_mine_solution::<T>(
&api,
client.chain_api(),
config.at,
config.solver,
round,
Expand Down Expand Up @@ -106,14 +107,16 @@ where

log::info!(target: LOG_TARGET, "Loaded account {}, {:?}", signer, account_info);

let nonce = api.rpc().system_account_next_index(signer.account_id()).await?;
let nonce = client.rpc_system_account_next_index(signer.account_id()).await?;
let tx = epm::signed_solution(raw_solution)?;
let xt =
api.tx()
.create_signed_with_nonce(&tx, &*signer, nonce, ExtrinsicParams::default())?;
let dry_run_bytes = api.rpc().dry_run(xt.encoded(), config.at).await?;

let dry_run_result = dry_run_bytes.into_dry_run_result(&api.metadata())?;
let xt = client.chain_api().tx().create_signed_with_nonce(
&tx,
&*signer,
nonce,
Default::default(),
)?;
let dry_run_bytes = client.rpc().dry_run(xt.encoded(), config.at).await?;
let dry_run_result = dry_run_bytes.into_dry_run_result(&client.chain_api().metadata())?;

log::info!(target: LOG_TARGET, "dry-run outcome is {:?}", dry_run_result);
}
Expand Down
12 changes: 7 additions & 5 deletions src/commands/emergency_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

//! The emergency-solution command.
use crate::{epm, error::Error, helpers::storage_at, opt::Solver, prelude::*, static_types};
use crate::{
client::Client, epm, error::Error, helpers::storage_at, opt::Solver, prelude::*, static_types,
};
use clap::Parser;
use codec::Encode;
use sp_core::hexdisplay::HexDisplay;
Expand All @@ -39,7 +41,7 @@ pub struct EmergencySolutionConfig {
}

pub async fn emergency_solution_cmd<T>(
api: SubxtClient,
client: Client,
config: EmergencySolutionConfig,
) -> Result<(), Error>
where
Expand All @@ -53,14 +55,14 @@ where
static_types::MaxWinners::set(max_winners);
}

let storage = storage_at(config.at, &api).await?;
let storage = storage_at(config.at, client.chain_api()).await?;

let round = storage
.fetch_or_default(&runtime::storage().election_provider_multi_phase().round())
.await?;

let miner_solution = epm::fetch_snapshot_and_mine_solution::<T>(
&api,
client.chain_api(),
config.at,
config.solver,
round,
Expand All @@ -87,7 +89,7 @@ where
}

let call = epm::set_emergency_result(supports.clone())?;
let encoded_call = call.encode_call_data(&api.metadata())?;
let encoded_call = call.encode_call_data(&client.chain_api().metadata())?;
let encoded_supports = supports.encode();

// write results to files.
Expand Down
Loading

0 comments on commit 0f274c7

Please sign in to comment.