Skip to content

Commit

Permalink
Feature: Query At block (#76)
Browse files Browse the repository at this point in the history
* feat: add at to block

* chore: remove info

* refactor: Update method names to improve clarity

* chore: fix clipply errors

* refactor: Update method signatures and logic for better type handling

* style: update log message levels for clarity

* style: remove unnecessary log messages

* chore: fix clippy
  • Loading branch information
S0c5 authored Aug 19, 2024
1 parent a53acea commit 864afab
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 202 deletions.
1 change: 0 additions & 1 deletion libwallet/examples/persisted_in_keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
let vault = vault::OSKeyring::<String>::new(TEST_USER, Language::default());

let mut wallet = Wallet::new(vault);


wallet.unlock(None, pin).await?;

Expand Down
8 changes: 4 additions & 4 deletions libwallet/src/vault/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::marker::PhantomData;
use crate::{
mnemonic::{Language, Mnemonic},
util::{seed_from_entropy, Pin},
vault::utils::{AccountSigner, RootAccount},
Vault,
vault::utils::{ RootAccount, AccountSigner }
};
use arrayvec::ArrayVec;
use keyring;
Expand All @@ -16,7 +16,7 @@ pub struct OSKeyring<S> {
entry: keyring::Entry,
root: Option<RootAccount>,
auto_generate: Option<Language>,
_phantom: PhantomData<S>
_phantom: PhantomData<S>,
}

impl<S> OSKeyring<S> {
Expand All @@ -28,7 +28,7 @@ impl<S> OSKeyring<S> {
entry: keyring::Entry::new(SERVICE, &uname),
root: None,
auto_generate: lang.into(),
_phantom: PhantomData::default()
_phantom: PhantomData::default(),
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ impl<S: AsRef<str>> Vault for OSKeyring<S> {
.and_then(|l| self.generate(pin, l))
})
.map(|r| {
let acc = AccountSigner::new(account.as_ref().map(|x| x.as_ref())).unlock(&r);
let acc = AccountSigner::new(account.as_ref().map(|x| x.as_ref())).unlock(&r);
self.root = Some(r);
acc
})
Expand Down
10 changes: 8 additions & 2 deletions sube/examples/pallet_communities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ async fn main() -> Result<()> {

if let Response::ValueSet(value) = response {
let data = serde_json::to_value(&value).expect("to be serializable");
println!("Collection {}", serde_json::to_string_pretty(&data).expect("it must return an str"));
println!(
"Collection {}",
serde_json::to_string_pretty(&data).expect("it must return an str")
);
}

let result = sube!("https://kreivo.io/system/account/0x12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b").await?;

if let Response::Value(value) = result {
let data = serde_json::to_value(&value).expect("to be serializable");
println!("Account info: {}", serde_json::to_string_pretty(&data).expect("it must return an str"));
println!(
"Account info: {}",
serde_json::to_string_pretty(&data).expect("it must return an str")
);
}

Ok(())
Expand Down
20 changes: 20 additions & 0 deletions sube/examples/query_at_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use env_logger;
use serde_json;
use sube::{sube, ExtrinsicBody, Response, Result, SubeBuilder};

#[async_std::main]
async fn main() -> Result<()> {
env_logger::init();

let result = sube!("ws://127.0.0.1:12281/system/account/0x12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b?at=2067321").await?;

if let Response::Value(value) = result {
let data = serde_json::to_value(&value).expect("to be serializable");
println!(
"Account info: {}",
serde_json::to_string_pretty(&data).expect("it must return an str")
);
}

Ok(())
}
7 changes: 3 additions & 4 deletions sube/examples/query_membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ async fn main() -> sube::Result<()> {

let query = format!(
"ws://127.0.0.1:12281/communityMemberships/account/{}/{}",
"0x12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b",
1
"0x12840f0626ac847d41089c4e05cf0719c5698af1e3bb87b66542de70b2de4b2b", 1
);

let r = sube!(&query).await?;

if let Response::ValueSet(ref v) = r {
Expand All @@ -23,4 +22,4 @@ async fn main() -> sube::Result<()> {
}

Ok(())
}
}
7 changes: 3 additions & 4 deletions sube/examples/query_preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ async fn main() -> sube::Result<()> {

let query = format!(
"ws://127.0.0.1:12281/preimage/preimageFor/{}/{}",
"0x6b172c3695dca229e71c0bca790f5991b68f8eee96334e842312a0a7d4a46c6c",
30
"0x6b172c3695dca229e71c0bca790f5991b68f8eee96334e842312a0a7d4a46c6c", 30
);

let r = sube!(&query).await?;

if let Response::Value(ref v) = r {
Expand All @@ -23,4 +22,4 @@ async fn main() -> sube::Result<()> {
}

Ok(())
}
}
5 changes: 4 additions & 1 deletion sube/examples/query_referendum_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ async fn main() -> Result<()> {
if let Response::Value(ref v) = r {
let json_value = serde_json::to_value(v).expect("it must to be an valid Value");
println!("Raw JSON value: {:?}", json_value);
println!("Info: {}", serde_json::to_string_pretty(&json_value).expect("it must return an str"));
println!(
"Info: {}",
serde_json::to_string_pretty(&json_value).expect("it must return an str")
);
}
Ok(())
}
54 changes: 31 additions & 23 deletions sube/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::rpc::RpcClient;
use crate::ws::Backend as WSBackend;
use crate::{
meta::BlockInfo, Backend, Error, ExtrinsicBody, Metadata, Response, Result as SubeResult,
Signer, StorageKey,
Signer,
};
use crate::{prelude::*, Offline, StorageChangeSet};
use crate::{prelude::*, Offline, RawKey, RawValue};

use core::future::{Future, IntoFuture};
use url::Url;
Expand Down Expand Up @@ -62,6 +62,12 @@ impl<'a> SubeBuilder<'a, (), ()> {
let Self { url, metadata, .. } = self;

let url = chain_string_to_url(url.ok_or(Error::BadInput)?)?;

let block = url
.query_pairs()
.find(|(k, _)| k == "at")
.map(|(_, v)| v.parse::<u32>().expect("at query params must be a number"));

let path = url.path();

log::trace!("building the backend for {}", url);
Expand All @@ -71,7 +77,7 @@ impl<'a> SubeBuilder<'a, (), ()> {
Ok(match path {
"_meta" => Response::Meta(meta),
"_meta/registry" => Response::Registry(&meta.types),
_ => crate::query(&backend, meta, path).await?,
_ => crate::query(&backend, meta, path, block).await?,
})
}
}
Expand Down Expand Up @@ -270,26 +276,38 @@ enum AnyBackend {
}

impl Backend for &AnyBackend {
async fn query_storage_at(
async fn get_storage_items(
&self,
keys: Vec<String>,
block: Option<String>,
) -> crate::Result<Vec<StorageChangeSet>> {
keys: Vec<RawKey>,
block: Option<u32>,
) -> crate::Result<impl Iterator<Item = (RawKey, RawValue)>> {
let result: Box<dyn Iterator<Item = (RawKey, RawValue)>> = match self {
#[cfg(any(feature = "http", feature = "http-web"))]
AnyBackend::Http(b) => Box::new(b.get_storage_items(keys, block).await?),
#[cfg(feature = "ws")]
AnyBackend::Ws(b) => Box::new(b.get_storage_items(keys, block).await?),
AnyBackend::_Offline(b) => Box::new(b.get_storage_items(keys, block).await?),
};

Ok(result)
}

async fn get_storage_item(&self, key: RawKey, block: Option<u32>) -> crate::Result<Vec<u8>> {
match self {
#[cfg(any(feature = "http", feature = "http-web"))]
AnyBackend::Http(b) => b.query_storage_at(keys, block).await,
AnyBackend::Http(b) => b.get_storage_item(key, block).await,
#[cfg(feature = "ws")]
AnyBackend::Ws(b) => b.query_storage_at(keys, block).await,
AnyBackend::_Offline(b) => b.query_storage_at(keys, block).await,
AnyBackend::Ws(b) => b.get_storage_item(key, block).await,
AnyBackend::_Offline(b) => b.get_storage_item(key, block).await,
}
}

async fn get_keys_paged(
&self,
from: &StorageKey,
from: RawKey,
size: u16,
to: Option<&StorageKey>,
) -> crate::Result<Vec<String>> {
to: Option<RawKey>,
) -> crate::Result<Vec<RawKey>> {
match self {
#[cfg(any(feature = "http", feature = "http-web"))]
AnyBackend::Http(b) => b.get_keys_paged(from, size, to).await,
Expand Down Expand Up @@ -328,16 +346,6 @@ impl Backend for &AnyBackend {
AnyBackend::_Offline(b) => b.block_info(at).await,
}
}

async fn query_storage(&self, key: &StorageKey) -> SubeResult<Vec<u8>> {
match self {
#[cfg(any(feature = "http", feature = "http-web"))]
AnyBackend::Http(b) => b.query_storage(key).await,
#[cfg(feature = "ws")]
AnyBackend::Ws(b) => b.query_storage(key).await,
AnyBackend::_Offline(b) => b.query_storage(key).await,
}
}
}

#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion sube/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Rpc for Backend {
})
.send()
.await
.map_err(|err| rpc::Error::Transport(Box::new(err)))?;
.map_err(|err| rpc::error::Error::Transport(Box::new(err)))?;

let status = res.status();
let res = if status.is_success() {
Expand Down
Loading

0 comments on commit 864afab

Please sign in to comment.