Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: release v1.3 #732

Merged
merged 12 commits into from
Dec 15, 2023
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ The format is based on [Keep a Changelog].

[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/

## [v1.3.0] - 2023-12-14
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved

The main changes of this release are as follows:
- Change the binary name to `polkadot-staking-miner` to publish on crates.io.
- Temporarly disable runtime upgrades more information below.
- Bump rust MSRV to 1.74
- Change `submit_signed_solution` extrinsic to be mortal
- A runtime upgrade bug was discovered and is fixed in this release.

### Runtime upgrade bug fixed.

Recently, we noticed that it may be possible that the runtime upgrades won't
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
upgrade the metadata because the actual runtime upgrade is applied to the block
after 'state_subscribeRuntimeVersion' emits an event.
For that reason, the staking-miner now subscribes to `system().last_runtime_upgrade()` instead to fix that.
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved

### [Changed]
- refactor: make solution extrinsic mortal ([#728](https://github.com/paritytech/staking-miner-v2/pull/728))
- chore(deps): bump subxt, subxt-signer, scale-value ([#726](https://github.com/paritytech/staking-miner-v2/pull/726))
- chore(deps): bump clap from 4.4.10 to 4.4.11 ([#721](https://github.com/paritytech/staking-miner-v2/pull/721))
- chore(deps): bump tokio from 1.34.0 to 1.35.0 ([#724](https://github.com/paritytech/staking-miner-v2/pull/724))
- chore(deps): bump once_cell from 1.18.0 to 1.19.0 ([#722](https://github.com/paritytech/staking-miner-v2/pull/722))
- refactor: use `subxt-signer` to reduce the number of deps ([#720](https://github.com/paritytech/staking-miner-v2/pull/720))
- chore(deps): bump clap from 4.4.8 to 4.4.10 ([#719](https://github.com/paritytech/staking-miner-v2/pull/719))
- rename project to polkadot-staking-miner ([#717](https://github.com/paritytech/staking-miner-v2/pull/717))

## [v1.2.0] - 2023-11-23

The major changes of this release:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polkadot-staking-miner"
version = "1.2.0"
version = "1.3.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
rust-version = "1.74.0"
Expand Down Expand Up @@ -51,4 +51,4 @@ regex = "1"

[features]
default = []
slow-tests = []
slow-tests = []
10 changes: 8 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct Client {
rpc: RpcClient,
/// Access to chain APIs such as storage, events etc.
chain_api: ChainClient,
/// Raw RPC client.
raw_rpc: RawRpcClient,
}

impl Client {
Expand Down Expand Up @@ -36,8 +38,7 @@ impl Client {
};

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

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

/// Get a reference to the RPC interface exposed by subxt.
Expand All @@ -49,4 +50,9 @@ impl Client {
pub fn chain_api(&self) -> &ChainClient {
&self.chain_api
}

/// Get a reference to the raw rpc client API.
pub fn raw_rpc(&self) -> &RawRpcClient {
&self.raw_rpc
}
}
108 changes: 73 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ mod prometheus;
mod static_types;

use clap::Parser;
use codec::Decode;
use error::Error;
use futures::future::{BoxFuture, FutureExt};
use prelude::*;
use std::str::FromStr;
use subxt::backend::rpc::RpcSubscription;
use tokio::sync::oneshot;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -127,7 +129,7 @@ async fn main() -> Result<(), Error> {
// Start a new tokio task to perform the runtime updates in the background.
// if this fails then the miner will be stopped and has to be re-started.
let (tx_upgrade, rx_upgrade) = oneshot::channel::<Error>();
tokio::spawn(runtime_upgrade_task(client.chain_api().clone(), tx_upgrade));
tokio::spawn(runtime_upgrade_task(client.clone(), tx_upgrade, runtime_version.spec_version));

let res = any_runtime!(chain, {
let fut = match command {
Expand Down Expand Up @@ -156,7 +158,7 @@ async fn main() -> Result<(), Error> {
run_command(fut, rx_upgrade).await
});

log::info!(target: LOG_TARGET, "round of execution finished. outcome = {:?}", res);
log::debug!(target: LOG_TARGET, "round of execution finished. outcome = {:?}", res);
res
}

Expand Down Expand Up @@ -206,49 +208,85 @@ async fn run_command(
}

/// Runs until the RPC connection fails or updating the metadata failed.
async fn runtime_upgrade_task(api: ChainClient, tx: oneshot::Sender<Error>) {
let updater = api.updater();
async fn runtime_upgrade_task(client: Client, tx: oneshot::Sender<Error>, mut spec_version: u32) {
use sp_core::storage::StorageChangeSet;

async fn new_update_stream(
client: &Client,
) -> Result<RpcSubscription<StorageChangeSet<Hash>>, subxt::Error> {
use sp_core::Bytes;
use subxt::rpc_params;

let storage_key = Bytes(runtime::storage().system().last_runtime_upgrade().to_root_bytes());

client
.raw_rpc()
.subscribe(
"state_subscribeStorage",
rpc_params![vec![storage_key]],
"state_unsubscribeStorage",
)
.await
}

let mut update_stream = match updater.runtime_updates().await {
Ok(u) => u,
let mut update_stream = match new_update_stream(&client).await {
Ok(s) => s,
Err(e) => {
let _ = tx.send(e.into());
return
_ = tx.send(e.into());
return;
},
};

loop {
// if the runtime upgrade subscription fails then try establish a new one and if it fails quit.
let update = match update_stream.next().await {
Some(Ok(update)) => update,
_ => {
log::warn!(target: LOG_TARGET, "Runtime upgrade subscription failed");
update_stream = match updater.runtime_updates().await {
Ok(u) => u,
Err(e) => {
let _ = tx.send(e.into());
return
},
let close_err = loop {
let change_set = match update_stream.next().await {
Some(Ok(changes)) => changes,
Some(Err(err)) => break err.into(),
None => {
update_stream = match new_update_stream(&client).await {
Ok(sub) => sub,
Err(err) => break err.into(),
};
continue
continue;
},
};

let version = update.runtime_version().spec_version;
match updater.apply_update(update) {
Ok(()) => {
if let Err(e) = epm::update_metadata_constants(&api) {
let _ = tx.send(e);
return
}
prometheus::on_runtime_upgrade();
log::info!(target: LOG_TARGET, "upgrade to v{} successful", version);
},
Err(e) => {
log::debug!(target: LOG_TARGET, "upgrade to v{} failed: {:?}", version, e);
},
let at = change_set.block;
assert!(change_set.changes.len() < 2, "Only one storage change per runtime upgrade");
let Some(bytes) = change_set.changes.get(0).and_then(|v| v.1.clone()) else { continue };
let next: runtime::runtime_types::frame_system::LastRuntimeUpgradeInfo =
match Decode::decode(&mut bytes.0.as_ref()) {
Ok(n) => n,
Err(e) => break e.into(),
};

if next.spec_version > spec_version {
let metadata = match client.rpc().state_get_metadata(Some(at)).await {
Ok(m) => m,
Err(err) => break err.into(),
};

let runtime_version = match client.rpc().state_get_runtime_version(Some(at)).await {
Ok(r) => r,
Err(err) => break err.into(),
};

client.chain_api().set_metadata(metadata);
client.chain_api().set_runtime_version(subxt::backend::RuntimeVersion {
spec_version: runtime_version.spec_version,
transaction_version: runtime_version.transaction_version,
});

spec_version = next.spec_version;
prometheus::on_runtime_upgrade();

log::info!(target: LOG_TARGET, "Runtime upgraded to v{spec_version}");
if let Err(e) = epm::update_metadata_constants(client.chain_api()) {
break e;
}
}
}
};

let _ = tx.send(close_err.into());
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl From<subxt_rpc::RuntimeVersion> for RuntimeVersion {
}
}

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
pub struct RuntimeVersion {
pub spec_name: String,
pub impl_name: String,
Expand Down
2 changes: 2 additions & 0 deletions src/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ mod hidden {
))
.unwrap()
});
#[allow(unused)]
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
static RUNTIME_UPGRADES: Lazy<Counter> = Lazy::new(|| {
register_counter!(opts!(
"staking_miner_runtime_upgrades",
Expand All @@ -178,6 +179,7 @@ mod hidden {
.unwrap()
});

#[allow(unused)]
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
pub fn on_runtime_upgrade() {
RUNTIME_UPGRADES.inc();
}
Expand Down