Subscribe to Runtime upgrades for proper extrinsic construction (#513)

* subxt: Add subscription to runtime upgrades

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Synchronize and expose inner `RuntimeVersion` of the `Client`

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Add runtime update example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Expose `RuntimeVersion` as locked

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Expose `Metadata` as locked

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt/storage: Use locked metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Use parking lot RwLock variant for locked metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Utilize locked metadata variant

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt/transaction: Use locked metadata variant

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update subxt to use locked version of the Metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Add runtime update client wrapper

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* examples: Modify runtime update example

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix clippy

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix cargo check

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Keep consistency with cargo check fix

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Remove unnecessary Arc

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Remove MetadataInner and use parking_lot::RwLock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update polkadot.rs

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update polkadot.rs generation comment

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Switch to async::Mutex

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Block executor while decoding dynamic events

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Use async API to handle async locking

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Remove unused dependencies

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update examples and integration-tests

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Fix test deadlock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Revert back to sync lock

Revert "Fix test deadlock"

This reverts commit 4a79933df23e81573611cb14be6c5b5b2b56d4df.

Revert "Update examples and integration-tests"

This reverts commit 5423f6eb4131582909d5a4ca70adff75e27cdd0e.

Revert "Remove unused dependencies"

This reverts commit e8ecbabb5b01a7ba4ae83b8bde36295a3f64daf7.

Revert "codegen: Use async API to handle async locking"

This reverts commit ced4646541c431adcb973369b1061b7b3cbfaae1.

Revert "subxt: Block executor while decoding dynamic events"

This reverts commit 8b3ba4a5eabb29f77ac1ca671450956fc479a33d.

Revert "subxt: Switch to async::Mutex"

This reverts commit f5bde9b79394a6bf61b6b9daefc36ceaa84b82be.

* subxt: Perform RuntimeVersion update before fetching metadata

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Reintroduce MetadataInner

* Use parking lot instead of std::RwLock

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt: Reduce lock metadata time when decoding events

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* codegen: Update `validate_metdata` locking pattern

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* subxt/examples: Update polkadot download link

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* tests: Wrap metadata in a helper function

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update examples/examples/subscribe_runtime_updates.rs

Co-authored-by: James Wilson <james@jsdw.me>

* subxt/updates: Update runtime if version is different

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

Co-authored-by: James Wilson <james@jsdw.me>
This commit is contained in:
Alexandru Vasile
2022-05-11 14:44:43 +03:00
committed by GitHub
parent 199cfa744b
commit 115073a33d
30 changed files with 6444 additions and 3369 deletions
+6 -1
View File
@@ -100,7 +100,12 @@ pub fn generate_calls(
&self,
#( #call_fn_args, )*
) -> Result<::subxt::SubmittableExtrinsic<'a, T, X, #struct_name, DispatchError, root_mod::Event>, ::subxt::BasicError> {
if self.client.metadata().call_hash::<#struct_name>()? == [#(#call_hash,)*] {
let runtime_call_hash = {
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
metadata.call_hash::<#struct_name>()?
};
if runtime_call_hash == [#(#call_hash,)*] {
let call = #struct_name { #( #call_args, )* };
Ok(::subxt::SubmittableExtrinsic::new(self.client, call))
} else {
+4 -2
View File
@@ -49,8 +49,10 @@ pub fn generate_constants(
quote! {
#( #[doc = #docs ] )*
pub fn #fn_name(&self) -> ::core::result::Result<#return_ty, ::subxt::BasicError> {
if self.client.metadata().constant_hash(#pallet_name, #constant_name)? == [#(#constant_hash,)*] {
let pallet = self.client.metadata().pallet(#pallet_name)?;
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
if metadata.constant_hash(#pallet_name, #constant_name)? == [#(#constant_hash,)*] {
let pallet = metadata.pallet(#pallet_name)?;
let constant = pallet.constant(#constant_name)?;
let value = ::subxt::codec::Decode::decode(&mut &constant.value[..])?;
Ok(value)
+7 -2
View File
@@ -312,7 +312,12 @@ impl RuntimeGenerator {
X: ::subxt::extrinsic::ExtrinsicParams<T>,
{
pub fn validate_metadata(&'a self) -> Result<(), ::subxt::MetadataError> {
if self.client.metadata().metadata_hash(&PALLETS) != [ #(#metadata_hash,)* ] {
let runtime_metadata_hash = {
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
metadata.metadata_hash(&PALLETS)
};
if runtime_metadata_hash != [ #(#metadata_hash,)* ] {
Err(::subxt::MetadataError::IncompatibleMetadata)
} else {
Ok(())
@@ -341,7 +346,7 @@ impl RuntimeGenerator {
}
impl <'a, T: ::subxt::Config> EventsApi<'a, T> {
pub async fn at(&self, block_hash: T::Hash) -> Result<::subxt::events::Events<'a, T, Event>, ::subxt::BasicError> {
pub async fn at(&self, block_hash: T::Hash) -> Result<::subxt::events::Events<T, Event>, ::subxt::BasicError> {
::subxt::events::at::<T, Event>(self.client, block_hash).await
}
+12 -2
View File
@@ -271,7 +271,12 @@ fn generate_storage_entry_fns(
&self,
block_hash: ::core::option::Option<T::Hash>,
) -> ::core::result::Result<::subxt::KeyIter<'a, T, #entry_struct_ident #lifetime_param>, ::subxt::BasicError> {
if self.client.metadata().storage_hash::<#entry_struct_ident>()? == [#(#storage_hash,)*] {
let runtime_storage_hash = {
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
metadata.storage_hash::<#entry_struct_ident>()?
};
if runtime_storage_hash == [#(#storage_hash,)*] {
self.client.storage().iter(block_hash).await
} else {
Err(::subxt::MetadataError::IncompatibleMetadata.into())
@@ -300,7 +305,12 @@ fn generate_storage_entry_fns(
#( #key_args, )*
block_hash: ::core::option::Option<T::Hash>,
) -> ::core::result::Result<#return_ty, ::subxt::BasicError> {
if self.client.metadata().storage_hash::<#entry_struct_ident>()? == [#(#storage_hash,)*] {
let runtime_storage_hash = {
let locked_metadata = self.client.metadata();
let metadata = locked_metadata.read();
metadata.storage_hash::<#entry_struct_ident>()?
};
if runtime_storage_hash == [#(#storage_hash,)*] {
let entry = #constructor;
self.client.storage().#fetch(&entry, block_hash).await
} else {