Skip to content

Commit

Permalink
Make sp_core and sp_runtime dependencies optional, and bump to latest (
Browse files Browse the repository at this point in the history
…#760)

* begin porting over traits; remove Config use of Hash

* port over the Header bits that we need

* sp_core_hashing where possible, move Verify to PairSigner, remove unused errors

* tidy up Config things and move related bits into one place

* fix codegen

* copy Era over

* move AccountId, Address, Signer to Signer trait and a pass over fixing examples

* impl MultiAddress, MultiSignature, AccountId32 and add back to Config (for decoding later)

* Copy over StorageKey, StorageData, StorageChangeSet

* subxt core compiling with no sp_core or sp_runtime

* Get examples compiling

* pass over fixing tests

* cargo fmt

* clippy tweaks and update polkadot.rs

* fix codegen docs

* port over special DigestItem encoding/decoding

* clippy and doc fixes

* cargo fmt and example fix

* more cargo fmt-ing...

* substrate-extra to substrate-compat

* cargo.toml comments

* simplify PairSigner trait bounds

* move RPC types to a separate file

* fix docs

* Add some tests for things and other PR feedback

* bump to latest sp deps

* avoid needing substrate-compat feature in a test
  • Loading branch information
jsdw authored Jan 10, 2023
1 parent ea5daa4 commit b316301
Show file tree
Hide file tree
Showing 47 changed files with 2,646 additions and 1,724 deletions.
16 changes: 8 additions & 8 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,23 @@ impl RuntimeGenerator {
),
(
"sp_core::crypto::AccountId32",
parse_quote!(#crate_path::ext::sp_core::crypto::AccountId32),
parse_quote!(#crate_path::utils::AccountId32),
),
(
"sp_runtime::multiaddress::MultiAddress",
parse_quote!(#crate_path::utils::MultiAddress),
),
(
"primitive_types::H160",
parse_quote!(#crate_path::ext::sp_core::H160),
parse_quote!(#crate_path::utils::H160),
),
(
"primitive_types::H256",
parse_quote!(#crate_path::ext::sp_core::H256),
parse_quote!(#crate_path::utils::H256),
),
(
"primitive_types::H512",
parse_quote!(#crate_path::ext::sp_core::H512),
),
(
"sp_runtime::multiaddress::MultiAddress",
parse_quote!(#crate_path::ext::sp_runtime::MultiAddress),
parse_quote!(#crate_path::utils::H512),
),
(
"frame_support::traits::misc::WrapperKeepOpaque",
Expand Down
4 changes: 3 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ description = "Subxt example usage"
[dev-dependencies]
subxt = { path = "../subxt" }
tokio = { version = "1.8", features = ["rt-multi-thread", "macros", "time"] }
sp-keyring = "7.0.0"
sp-keyring = "12.0.0"
futures = "0.3.13"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] }
hex = "0.4.3"
tracing-subscriber = "0.3.11"
sp-core = { version = "11.0.0", default-features = false }
sp-runtime = { version = "12.0.0" }
14 changes: 8 additions & 6 deletions examples/examples/balance_transfer_with_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

use sp_keyring::AccountKeyring;
use subxt::{
tx::{
Era,
PairSigner,
PlainTip,
PolkadotExtrinsicParamsBuilder as Params,
config::{
polkadot::{
Era,
PlainTip,
PolkadotExtrinsicParamsBuilder as Params,
},
PolkadotConfig,
},
tx::PairSigner,
OnlineClient,
PolkadotConfig,
};

#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/concurrent_storage_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod polkadot {}
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = OnlineClient::<PolkadotConfig>::new().await?;

let addr = AccountKeyring::Bob.to_account_id();
let addr = AccountKeyring::Bob.to_account_id().into();

// Construct storage addresses to access:
let staking_bonded = polkadot::storage().staking().bonded(&addr);
Expand Down
10 changes: 4 additions & 6 deletions examples/examples/custom_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
use sp_keyring::AccountKeyring;
use subxt::{
config::{
substrate::SubstrateExtrinsicParams,
Config,
SubstrateConfig,
},
tx::{
PairSigner,
SubstrateExtrinsicParams,
},
tx::PairSigner,
OnlineClient,
};

Expand All @@ -34,10 +32,10 @@ impl Config for MyConfig {
type Index = u64;
type BlockNumber = <SubstrateConfig as Config>::BlockNumber;
type Hash = <SubstrateConfig as Config>::Hash;
type Hashing = <SubstrateConfig as Config>::Hashing;
type Hasher = <SubstrateConfig as Config>::Hasher;
type Header = <SubstrateConfig as Config>::Header;
type AccountId = <SubstrateConfig as Config>::AccountId;
type Address = <SubstrateConfig as Config>::Address;
type Header = <SubstrateConfig as Config>::Header;
type Signature = <SubstrateConfig as Config>::Signature;
// ExtrinsicParams makes use of the index type, so we need to adjust it
// too to align with our modified index type, above:
Expand Down
12 changes: 5 additions & 7 deletions examples/examples/fetch_staking_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@
//! polkadot --dev --tmp
//! ```

use sp_core::{
sr25519,
Pair,
};
use sp_keyring::AccountKeyring;
use subxt::{
ext::{
sp_core::{
sr25519,
Pair,
},
sp_runtime::AccountId32,
},
utils::AccountId32,
OnlineClient,
PolkadotConfig,
};
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// threshold
1,
// other signatories
vec![signer_account_id],
vec![signer_account_id.into()],
// maybe timepoint
None,
// call
Expand Down
9 changes: 2 additions & 7 deletions examples/examples/rpc_call_subscribe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
//! ```

use subxt::{
ext::sp_runtime::{
generic::Header,
traits::BlakeTwo256,
},
rpc::Subscription,
config::Header,
OnlineClient,
PolkadotConfig,
};
Expand All @@ -28,8 +24,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
OnlineClient::<PolkadotConfig>::from_url("wss://rpc.polkadot.io:443").await?;

// For non-finalised blocks use `.subscribe_blocks()`
let mut blocks: Subscription<Header<u32, BlakeTwo256>> =
api.rpc().subscribe_finalized_block_headers().await?;
let mut blocks = api.rpc().subscribe_finalized_block_headers().await?;

while let Some(Ok(block)) = blocks.next().await {
println!(
Expand Down
2 changes: 1 addition & 1 deletion metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ description = "Command line utilities for checking metadata compatibility betwee
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full"] }
frame-metadata = "15.0.0"
scale-info = "2.0.0"
sp-core = "7.0.0"
sp-core-hashing = "6.0.0"

[dev-dependencies]
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
Expand Down
4 changes: 2 additions & 2 deletions metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ enum TypeBeingHashed {
}

/// Hashing function utilized internally.
fn hash(bytes: &[u8]) -> [u8; 32] {
sp_core::hashing::twox_256(bytes)
fn hash(data: &[u8]) -> [u8; 32] {
sp_core_hashing::twox_256(data)
}

/// XOR two hashes together. If we have two pseudorandom hashes, then this will
Expand Down
32 changes: 26 additions & 6 deletions subxt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ description = "Submit extrinsics (transactions) to a substrate node via RPC"
keywords = ["parity", "substrate", "blockchain"]

[features]
default = ["jsonrpsee-ws"]
default = ["jsonrpsee-ws", "substrate-compat"]

# Activate this feature to pull in extra Substrate dependencies which make it
# possible to provide a proper extrinsic Signer implementation (PairSigner).
substrate-compat = [
"sp-core",
"sp-runtime"
]

# Activate this to expose functionality only used for integration testing.
# The exposed functionality is subject to breaking changes at any point,
Expand All @@ -32,23 +39,32 @@ scale-info = "2.0.0"
scale-value = "0.6.0"
scale-bits = "0.3"
scale-decode = "0.4.0"
futures = { version = "0.3.13", default-features = false }
futures = { version = "0.3.13", default-features = false, features = ["std"] }
hex = "0.4.3"
jsonrpsee = { version = "0.16", optional = true, features = ["jsonrpsee-types"] }
serde = { version = "1.0.124", features = ["derive"] }
serde_json = { version = "1.0.64", features = ["raw_value"] }
thiserror = "1.0.24"
tracing = "0.1.34"
parking_lot = "0.12.0"
frame-metadata = "15.0.0"
derivative = "2.2.0"

subxt-macro = { version = "0.25.0", path = "../macro" }
subxt-metadata = { version = "0.25.0", path = "../metadata" }

sp-core = { version = "7.0.0", default-features = false }
sp-runtime = "7.0.0"
# Provides some deserialization, types like U256/H256 and hashing impls like twox/blake256:
impl-serde = { version = "0.4.0" }
primitive-types = { version = "0.12.0", default-features = false, features = ["codec", "scale-info", "serde"] }
sp-core-hashing = "6.0.0"

frame-metadata = "15.0.0"
derivative = "2.2.0"
# For ss58 encoding AccountId32 to serialize them properly:
base58 = { version = "0.2.0" }
blake2 = { version = "0.10.4", default-features = false }

# These are only included is "substrate-compat" is enabled.
sp-core = { version = "11.0.0", default-features = false, optional = true }
sp-runtime = { version = "12.0.0", optional = true }

[target.wasm32-unknown-unknown.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -58,3 +74,7 @@ bitvec = "1"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] }
scale-info = { version = "2.0.0", features = ["bit-vec"] }
tokio = { version = "1.8", features = ["macros", "time", "rt-multi-thread"] }
sp-core = { version = "11.0.0", default-features = false }
sp-runtime = { version = "12.0.0" }
sp-keyring = "12.0.0"
sp-version = "10.0.0"
16 changes: 8 additions & 8 deletions subxt/src/blocks/block_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ use crate::{
OfflineClientT,
OnlineClientT,
},
config::{
Config,
Hasher,
Header,
},
error::{
BlockError,
Error,
},
events,
rpc::ChainBlockResponse,
Config,
rpc::types::ChainBlockResponse,
};
use derivative::Derivative;
use futures::lock::Mutex as AsyncMutex;
use sp_runtime::traits::{
Hash,
Header,
};
use std::sync::Arc;

/// A representation of a block.
Expand Down Expand Up @@ -56,7 +56,7 @@ where

/// Return the block number.
pub fn number(&self) -> T::BlockNumber {
*self.header().number()
self.header().number()
}

/// Return the entire block header.
Expand Down Expand Up @@ -170,7 +170,7 @@ where
pub async fn events(&self) -> Result<ExtrinsicEvents<T>, Error> {
let events =
get_events(&self.client, self.block_hash, &self.cached_events).await?;
let ext_hash = T::Hashing::hash_of(&self.bytes);
let ext_hash = T::Hasher::hash_of(&self.bytes);
Ok(ExtrinsicEvents::new(ext_hash, self.index, events))
}
}
Expand Down
10 changes: 6 additions & 4 deletions subxt/src/blocks/blocks_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use super::Block;
use crate::{
client::OnlineClientT,
config::{
Config,
Header,
},
error::{
BlockError,
Error,
},
utils::PhantomDataSendSync,
Config,
};
use derivative::Derivative;
use futures::{
Expand All @@ -19,7 +22,6 @@ use futures::{
Stream,
StreamExt,
};
use sp_runtime::traits::Header;
use std::{
future::Future,
pin::Pin,
Expand Down Expand Up @@ -137,7 +139,7 @@ where
.rpc()
.header(Some(last_finalized_block_hash))
.await?
.map(|h| (*h.number()).into());
.map(|h| h.number().into());

let sub = client.rpc().subscribe_finalized_block_headers().await?;

Expand Down Expand Up @@ -203,7 +205,7 @@ where
};

// We want all previous details up to, but not including this current block num.
let end_block_num = (*header.number()).into();
let end_block_num = header.number().into();

// This is one after the last block we returned details for last time.
let start_block_num = last_block_num.map(|n| n + 1).unwrap_or(end_block_num);
Expand Down
2 changes: 1 addition & 1 deletion subxt/src/client/offline_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
blocks::BlocksClient,
constants::ConstantsClient,
events::EventsClient,
rpc::RuntimeVersion,
rpc::types::RuntimeVersion,
storage::StorageClient,
tx::TxClient,
Config,
Expand Down
6 changes: 4 additions & 2 deletions subxt/src/client/online_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use crate::{
error::Error,
events::EventsClient,
rpc::{
types::{
RuntimeVersion,
Subscription,
},
Rpc,
RpcClientT,
RuntimeVersion,
Subscription,
},
storage::StorageClient,
tx::TxClient,
Expand Down
Loading

0 comments on commit b316301

Please sign in to comment.