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
+11 -16
View File
@@ -25,6 +25,7 @@ use frame_metadata::{
StorageEntryMetadata,
META_RESERVED,
};
use parking_lot::RwLock;
use scale_info::{
form::PortableForm,
Type,
@@ -33,10 +34,7 @@ use scale_info::{
use std::{
collections::HashMap,
convert::TryFrom,
sync::{
Arc,
RwLock,
},
sync::Arc,
};
/// Metadata error.
@@ -83,12 +81,6 @@ pub enum MetadataError {
IncompatibleMetadata,
}
/// Runtime metadata.
#[derive(Clone, Debug)]
pub struct Metadata {
inner: Arc<MetadataInner>,
}
// We hide the innards behind an Arc so that it's easy to clone and share.
#[derive(Debug)]
struct MetadataInner {
@@ -105,6 +97,12 @@ struct MetadataInner {
cached_storage_hashes: HashCache,
}
/// Runtime metadata.
#[derive(Clone, Debug)]
pub struct Metadata {
inner: Arc<MetadataInner>,
}
impl Metadata {
/// Returns a reference to [`PalletMetadata`].
pub fn pallet(&self, name: &'static str) -> Result<&PalletMetadata, MetadataError> {
@@ -217,7 +215,7 @@ impl Metadata {
/// Obtain the unique hash for this metadata.
pub fn metadata_hash<T: AsRef<str>>(&self, pallets: &[T]) -> [u8; 32] {
if let Some(hash) = *self.inner.cached_metadata_hash.read().unwrap() {
if let Some(hash) = *self.inner.cached_metadata_hash.read() {
return hash
}
@@ -225,7 +223,7 @@ impl Metadata {
self.runtime_metadata(),
pallets,
);
*self.inner.cached_metadata_hash.write().unwrap() = Some(hash);
*self.inner.cached_metadata_hash.write() = Some(hash);
hash
}
@@ -547,10 +545,7 @@ mod tests {
let hash = metadata.metadata_hash(&["System"]);
// Check inner caching.
assert_eq!(
metadata.inner.cached_metadata_hash.read().unwrap().unwrap(),
hash
);
assert_eq!(metadata.inner.cached_metadata_hash.read().unwrap(), hash);
// The cache `metadata.inner.cached_metadata_hash` is already populated from
// the previous call. Therefore, changing the pallets argument must not