Skip to content

Commit

Permalink
Make own version of RuntimeVersion to avoid mismatches (paritytech#395)
Browse files Browse the repository at this point in the history
* Make own version of RuntimeVersion to avoid mismatches

* cargo fmt

* remove unneeded Serialize bound on RuntimeVersion
  • Loading branch information
jsdw authored and 0623forbidden committed Feb 15, 2022
1 parent 1fcd43f commit ab20e27
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use futures::future;
use sp_runtime::traits::Hash;
pub use sp_runtime::traits::SignedExtension;
pub use sp_version::RuntimeVersion;

use crate::{
error::Error,
Expand All @@ -31,6 +30,7 @@ use crate::{
rpc::{
Rpc,
RpcClient,
RuntimeVersion,
SystemProperties,
},
storage::StorageClient,
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<T: Config> std::fmt::Debug for Client<T> {
.field("metadata", &"<Metadata>")
.field("events_decoder", &"<EventsDecoder>")
.field("properties", &self.properties)
.field("runtime_version", &self.runtime_version.to_string())
.field("runtime_version", &self.runtime_version)
.field("iter_page_size", &self.iter_page_size)
.finish()
}
Expand Down
2 changes: 1 addition & 1 deletion src/extrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pub use self::{
};

use sp_runtime::traits::SignedExtension;
use sp_version::RuntimeVersion;

use crate::{
rpc::RuntimeVersion,
Config,
Encoded,
Error,
Expand Down
64 changes: 62 additions & 2 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
// Related: https://github.com/paritytech/subxt/issues/66
#![allow(irrefutable_let_patterns)]

use std::sync::Arc;
use std::{
collections::HashMap,
sync::Arc,
};

use codec::{
Decode,
Expand Down Expand Up @@ -68,7 +71,6 @@ use sp_runtime::generic::{
Block,
SignedBlock,
};
use sp_version::RuntimeVersion;

use crate::{
error::Error,
Expand Down Expand Up @@ -165,6 +167,33 @@ pub enum SubstrateTransactionStatus<Hash, BlockHash> {
Invalid,
}

/// This contains the runtime version information necessary to make transactions, as obtained from
/// the RPC call `state_getRuntimeVersion`,
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RuntimeVersion {
/// Version of the runtime specification. A full-node will not attempt to use its native
/// runtime in substitute for the on-chain Wasm runtime unless all of `spec_name`,
/// `spec_version` and `authoring_version` are the same between Wasm and native.
pub spec_version: u32,

/// All existing dispatches are fully compatible when this number doesn't change. If this
/// number changes, then `spec_version` must change, also.
///
/// This number must change when an existing dispatchable (module ID, dispatch ID) is changed,
/// either through an alteration in its user-level semantics, a parameter
/// added/removed/changed, a dispatchable being removed, a module being removed, or a
/// dispatchable/module changing its index.
///
/// It need *not* change when a new module is added or when a dispatchable is added.
pub transaction_version: u32,

/// The other fields present may vary and aren't necessary for `subxt`; they are preserved in
/// this map.
#[serde(flatten)]
pub other: HashMap<String, serde_json::Value>,
}

/// Rpc client wrapper.
/// This is workaround because adding generic types causes the macros to fail.
#[derive(Clone)]
Expand Down Expand Up @@ -597,3 +626,34 @@ impl<T: Config> Rpc<T> {
Ok(self.client.request("author_hasKey", params).await?)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_deser_runtime_version() {
let val: RuntimeVersion = serde_json::from_str(
r#"{
"specVersion": 123,
"transactionVersion": 456,
"foo": true,
"wibble": [1,2,3]
}"#,
)
.expect("deserializing failed");

let mut m = std::collections::HashMap::new();
m.insert("foo".to_owned(), serde_json::json!(true));
m.insert("wibble".to_owned(), serde_json::json!([1, 2, 3]));

assert_eq!(
val,
RuntimeVersion {
spec_version: 123,
transaction_version: 456,
other: m
}
);
}
}

0 comments on commit ab20e27

Please sign in to comment.