Skip to content

Commit

Permalink
chore(gsdk): upgrade to subxt-0.37.0 (#4151)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored Aug 20, 2024
1 parent 855a4b7 commit 3e9ae98
Show file tree
Hide file tree
Showing 22 changed files with 1,694 additions and 1,062 deletions.
1,072 changes: 722 additions & 350 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ serde = { version = "^1", default-features = false }
serde_json = "^1"
serde_yaml = "0.8.26"
sha-1 = "0.10.1"
subxt = "0.32.1"
subxt-metadata = "0.32.1"
subxt-codegen = "0.32.1"
subxt = "0.37.0"
subxt-metadata = "0.37.0"
subxt-codegen = "0.37.0"
syn = "2.0.71"
thiserror = "1.0.62"
tokio = { version = "1.38.0" }
Expand Down Expand Up @@ -303,9 +303,13 @@ ethexe-rpc = { path = "ethexe/rpc", default-features = false }
ethexe-common = { path = "ethexe/common" }

# Common executors between `sandbox-host` and `calc-stack-height`
sandbox-wasmer = { package = "wasmer", version = "4.3.4", features = ["singlepass"] }
sandbox-wasmer = { package = "wasmer", version = "4.3.4", features = [
"singlepass",
] }
sandbox-wasmer-types = { package = "wasmer-types", version = "4.3.4" }
sandbox-wasmi = { package = "wasmi", git = "https://github.com/gear-tech/wasmi", branch = "v0.13.2-sign-ext", features = ["virtual_memory"] }
sandbox-wasmi = { package = "wasmi", git = "https://github.com/gear-tech/wasmi", branch = "v0.13.2-sign-ext", features = [
"virtual_memory",
] }

# Substrate deps
binary-merkle-tree = { version = "4.0.0-dev", git = "https://github.com/gear-tech/polkadot-sdk.git", branch = "gear-v1.4.0", default-features = false }
Expand Down Expand Up @@ -526,11 +530,11 @@ wasm-opt = "0.116" # utils/wasm-builde
wasmprinter = "0.2" # utils/wasm-gen
whoami = "1.5.1" # gcli
fail = "0.5" # gear-common
scale-value = "^0.12" # gsdk
scale-value = "^0.16" # gsdk
heck = "0.5.0" # gsdk-api-gen
etc = "0.1.19" # gcli
toml_edit = "0.22.12" # crates-io
scale-decode = "0.9.0" # gsdk
scale-decode = "0.13.0" # gsdk
directories = "5.0.1" # utils/key-finder
num-traits = { version = "0.2", default-features = false } # gear-core
glob = "0.3.1" # cargo-gbuild
Expand Down
16 changes: 9 additions & 7 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,11 +1336,11 @@ impl GearApi {
.await
}

fn process_set_code(&self, events: &ExtrinsicEvents<GearConfig>) -> Result<H256> {
fn process_set_code(&self, events: &ExtrinsicEvents<GearConfig>) -> Result<()> {
for event in events.iter() {
let event = event?.as_root_event::<Event>()?;
if let Event::System(SystemEvent::CodeUpdated) = event {
return Ok(events.block_hash());
return Ok(());
}
}

Expand All @@ -1354,7 +1354,7 @@ impl GearApi {
/// [`pallet_system::set_code`](https://crates.parity.io/frame_system/pallet/struct.Pallet.html#method.set_code)
/// extrinsic.
pub async fn set_code(&self, code: impl AsRef<[u8]>) -> Result<H256> {
let events = self
let (block_hash, events) = self
.0
.calls
.sudo_unchecked_weight(
Expand All @@ -1367,7 +1367,8 @@ impl GearApi {
},
)
.await?;
self.process_set_code(&events)
self.process_set_code(&events)?;
Ok(block_hash)
}

/// Upgrade the runtime by reading the code from the file located at the
Expand All @@ -1387,7 +1388,7 @@ impl GearApi {
/// [`pallet_system::set_code_without_checks`](https://crates.parity.io/frame_system/pallet/struct.Pallet.html#method.set_code_without_checks)
/// extrinsic.
pub async fn set_code_without_checks(&self, code: impl AsRef<[u8]>) -> Result<H256> {
let events = self
let (block_hash, events) = self
.0
.calls
.sudo_unchecked_weight(
Expand All @@ -1400,7 +1401,8 @@ impl GearApi {
},
)
.await?;
self.process_set_code(&events)
self.process_set_code(&events)?;
Ok(block_hash)
}

/// Upgrade the runtime by reading the code from the file located at the
Expand Down Expand Up @@ -1438,7 +1440,7 @@ impl GearApi {
},
)
.await?;
Ok(events.block_hash())
Ok(events.0)
}

/// Same as [`upload_code`](Self::upload_code), but upload code
Expand Down
3 changes: 3 additions & 0 deletions gclient/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub enum Error {
/// A wrapper around [`subxt::error::Error`].
#[error(transparent)]
Subxt(SubxtError),
/// Subxt core error
#[error(transparent)]
SubxtCore(#[from] subxt::ext::subxt_core::Error),
/// Occurs when an event of the expected type cannot be found.
#[error("Expected event wasn't found")]
EventNotFound,
Expand Down
20 changes: 9 additions & 11 deletions gclient/src/api/listener/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ use super::EventProcessor;
use crate::{Error, Result};
use async_trait::async_trait;
use gsdk::{
config::GearConfig,
ext::sp_core::H256,
metadata::{gear::Event as GearEvent, Event},
subscription::BlockEvents,
Blocks,
};
use subxt::events::Events;

/// Event listener that allows catching and processing events propagated through
/// the network.
Expand Down Expand Up @@ -53,8 +52,8 @@ impl EventProcessor for EventListener {
}

async fn proc<T>(&mut self, predicate: impl Fn(Event) -> Option<T> + Copy) -> Result<T> {
while let Some(events) = self.0.next_events().await {
if let Some(res) = self.proc_events_inner(events?, predicate) {
while let Some(events) = self.0.next_events().await? {
if let Some(res) = self.proc_events_inner(events, predicate) {
return Ok(res);
}
}
Expand All @@ -69,8 +68,8 @@ impl EventProcessor for EventListener {
) -> Result<Vec<T>> {
let mut res = vec![];

while let Some(events) = self.0.next_events().await {
for event in events?.iter() {
while let Some(events) = self.0.next_events().await? {
for event in events.iter() {
if let Some(data) = predicate(event?.as_root_event::<Event>()?) {
res.push(data);
}
Expand All @@ -93,8 +92,7 @@ impl EventListener {
/// [`QueueNotProcessed`](https://docs.gear.rs/pallet_gear/pallet/enum.Event.html#variant.QueueNotProcessed)
/// event.
pub async fn queue_processing_reverted(&mut self) -> Result<H256> {
while let Some(events) = self.0.next_events().await {
let events = events?;
while let Some(events) = self.0.next_events().await? {
let events_bh = events.block_hash();

if let Some(res) = self.proc_events_inner(events, |e| {
Expand All @@ -113,8 +111,8 @@ impl EventListener {
Ok(self
.0
.next_events()
.await
.ok_or(Error::EventNotFound)??
.await?
.ok_or(Error::EventNotFound)?
.block_hash())
}

Expand All @@ -135,7 +133,7 @@ impl EventListener {

fn proc_events_inner<T>(
&mut self,
events: Events<GearConfig>,
events: BlockEvents,
predicate: impl Fn(Event) -> Option<T>,
) -> Option<T> {
events
Expand Down
10 changes: 3 additions & 7 deletions gsdk/api-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ proc-macro2.workspace = true
quote.workspace = true
parity-scale-codec.workspace = true
scale-info.workspace = true
subxt-codegen.workspace = true
syn = { workspace = true, features = ["full", "parsing"] }
heck.workspace = true
hex.workspace = true
# NOTE: only required by this package.
#
# Same version from `/Cargo.lock`
frame-metadata = "15.1.0"
subxt-metadata.workspace = true
subxt-codegen.workspace = true
# TODO: inherit from workspace
#
# use "2.0.15" because subxt-codegen-0.29.0 requires it.
syn = { version = "2.0.71", features = ["full", "parsing"] }
frame-metadata = "16.0.0"
47 changes: 19 additions & 28 deletions gsdk/api-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use std::{
env, fs,
io::{self, Write},
};
use subxt_codegen::{DerivesRegistry, RuntimeGenerator, TypeSubstitutes};
use subxt_metadata::Metadata;
use subxt_codegen::{CodegenBuilder, Metadata};
use syn::{parse_quote, Fields, ItemEnum, ItemImpl, ItemMod, Variant};

const RUNTIME_WASM: &str = "RUNTIME_WASM";
Expand Down Expand Up @@ -120,41 +119,30 @@ fn metadata() -> Vec<u8> {
}

fn generate_runtime_types(metadata: Metadata) -> TokenStream {
let generator = RuntimeGenerator::new(metadata);
let runtime_types_mod = parse_quote!(
pub mod runtime_types {}
);

let crate_path = Default::default();

let mut derives = DerivesRegistry::new();
derives.extend_for_all(
[
parse_quote!(Debug),
parse_quote!(crate::gp::Encode),
parse_quote!(crate::gp::Decode),
parse_quote!(crate::gp::DecodeAsType),
],
[],
);
let mut builder = CodegenBuilder::new();
builder.set_additional_global_derives(vec![
parse_quote!(Debug),
parse_quote!(crate::gp::Encode),
parse_quote!(crate::gp::Decode),
parse_quote!(crate::gp::DecodeAsType),
]);

for ty in [
parse_quote!(gprimitives::CodeId),
parse_quote!(gprimitives::MessageId),
parse_quote!(gprimitives::ActorId),
parse_quote!(gprimitives::ReservationId),
] {
derives.extend_for_type(ty, [parse_quote!(Copy)], []);
builder.add_derives_for_type(ty, [parse_quote!(Copy)], true);
}

generator
.generate_runtime_types(
runtime_types_mod,
derives,
TypeSubstitutes::with_default_substitutes(&crate_path),
crate_path,
true,
)
builder.runtime_types_only();
builder.disable_default_derives();
builder.set_target_module(parse_quote! {
pub mod runtime_types {}
});
builder
.generate(metadata)
.expect("Failed to generate runtime types")
}

Expand Down Expand Up @@ -362,6 +350,9 @@ fn generate_impls(metadata: &Metadata) -> TokenStream {
"staking" => quote! {
pub use super::runtime_types::#pallet::pallet::pallet::Event;
},
"referenda" => quote! {
pub use super::runtime_types::#pallet::pallet::Event1 as Event;
},
"fellowship_referenda" => quote! {
pub use super::runtime_types::#pallet::pallet::Event2 as Event;
},
Expand Down
13 changes: 6 additions & 7 deletions gsdk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
client::Rpc, config::GearConfig, metadata::Event, signer::Signer, Blocks, Events, Result,
TxInBlock,
client::Rpc, config::GearConfig, metadata::Event, signer::Signer, Blocks, Events, TxInBlock,
};
use anyhow::Result;
use core::ops::{Deref, DerefMut};
use std::result::Result as StdResult;
use subxt::{Error, OnlineClient};
use subxt::OnlineClient;

/// Gear api wrapper.
#[derive(Clone)]
Expand Down Expand Up @@ -108,8 +107,8 @@ impl Api {
tx.fetch_events()
.await?
.iter()
.map(|e| -> StdResult<Event, Error> { e?.as_root_event::<Event>() })
.collect::<StdResult<Vec<Event>, Error>>()
.map(|e| -> Result<Event> { e?.as_root_event::<Event>().map_err(Into::into) })
.collect::<Result<Vec<Event>>>()
.map_err(Into::into)
}

Expand All @@ -122,7 +121,7 @@ impl Api {

/// New signer from api
pub fn signer(self, suri: &str, passwd: Option<&str>) -> Result<Signer> {
Signer::new(self, suri, passwd)
Signer::new(self, suri, passwd).map_err(Into::into)
}
}

Expand Down
1 change: 1 addition & 0 deletions gsdk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub type Header = SubstrateHeader<u32, BlakeTwo256>;
pub struct GearConfig;

impl Config for GearConfig {
type AssetId = ();
type Hash = sp_core::H256;
type Hasher = BlakeTwo256;
type AccountId = sp_runtime::AccountId32;
Expand Down
Loading

0 comments on commit 3e9ae98

Please sign in to comment.