Add block-centric Storage API (#774)

* blocks: Add storage method

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

* Add support for runtime API calls and expose it to the blocks API

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

* storage: Add storage type for block centric API

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

* Adjust subxt to the new Storage interface

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

* Fix clippy

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

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-01-20 12:49:19 +02:00
committed by GitHub
parent 4155850063
commit e4e9562b45
17 changed files with 608 additions and 388 deletions
+12 -2
View File
@@ -126,7 +126,10 @@ async fn fetch_keys() {
let addr = node_runtime::storage().system().account_root();
let keys = api
.storage()
.fetch_keys(&addr.to_root_bytes(), 4, None, None)
.at(None)
.await
.unwrap()
.fetch_keys(&addr.to_root_bytes(), 4, None)
.await
.unwrap();
assert_eq!(keys.len(), 4)
@@ -138,7 +141,14 @@ async fn test_iter() {
let api = ctx.client();
let addr = node_runtime::storage().system().account_root();
let mut iter = api.storage().iter(addr, 10, None).await.unwrap();
let mut iter = api
.storage()
.at(None)
.await
.unwrap()
.iter(addr, 10)
.await
.unwrap();
let mut i = 0;
while iter.next().await.unwrap().is_some() {
i += 1;
+44 -12
View File
@@ -42,11 +42,15 @@ async fn tx_basic_transfer() -> Result<(), subxt::Error> {
let alice_pre = api
.storage()
.fetch_or_default(&alice_account_addr, None)
.at(None)
.await?
.fetch_or_default(&alice_account_addr)
.await?;
let bob_pre = api
.storage()
.fetch_or_default(&bob_account_addr, None)
.at(None)
.await?
.fetch_or_default(&bob_account_addr)
.await?;
let tx = node_runtime::tx().balances().transfer(bob_address, 10_000);
@@ -75,11 +79,15 @@ async fn tx_basic_transfer() -> Result<(), subxt::Error> {
let alice_post = api
.storage()
.fetch_or_default(&alice_account_addr, None)
.at(None)
.await?
.fetch_or_default(&alice_account_addr)
.await?;
let bob_post = api
.storage()
.fetch_or_default(&bob_account_addr, None)
.at(None)
.await?
.fetch_or_default(&bob_account_addr)
.await?;
assert!(alice_pre.data.free - 10_000 >= alice_post.data.free);
@@ -113,11 +121,15 @@ async fn tx_dynamic_transfer() -> Result<(), subxt::Error> {
let alice_pre = api
.storage()
.fetch_or_default(&alice_account_addr, None)
.at(None)
.await?
.fetch_or_default(&alice_account_addr)
.await?;
let bob_pre = api
.storage()
.fetch_or_default(&bob_account_addr, None)
.at(None)
.await?
.fetch_or_default(&bob_account_addr)
.await?;
let tx = subxt::dynamic::tx(
@@ -159,11 +171,15 @@ async fn tx_dynamic_transfer() -> Result<(), subxt::Error> {
let alice_post = api
.storage()
.fetch_or_default(&alice_account_addr, None)
.at(None)
.await?
.fetch_or_default(&alice_account_addr)
.await?;
let bob_post = api
.storage()
.fetch_or_default(&bob_account_addr, None)
.at(None)
.await?
.fetch_or_default(&bob_account_addr)
.await?;
let alice_pre_free = alice_pre
@@ -214,7 +230,9 @@ async fn multiple_transfers_work_nonce_incremented() -> Result<(), subxt::Error>
let bob_pre = api
.storage()
.fetch_or_default(&bob_account_addr, None)
.at(None)
.await?
.fetch_or_default(&bob_account_addr)
.await?;
let tx = node_runtime::tx()
@@ -233,7 +251,9 @@ async fn multiple_transfers_work_nonce_incremented() -> Result<(), subxt::Error>
let bob_post = api
.storage()
.fetch_or_default(&bob_account_addr, None)
.at(None)
.await?
.fetch_or_default(&bob_account_addr)
.await?;
assert_eq!(bob_pre.data.free + 30_000, bob_post.data.free);
@@ -246,7 +266,14 @@ async fn storage_total_issuance() {
let api = ctx.client();
let addr = node_runtime::storage().balances().total_issuance();
let total_issuance = api.storage().fetch_or_default(&addr, None).await.unwrap();
let total_issuance = api
.storage()
.at(None)
.await
.unwrap()
.fetch_or_default(&addr)
.await
.unwrap();
assert_ne!(total_issuance, 0);
}
@@ -274,7 +301,12 @@ async fn storage_balance_lock() -> Result<(), subxt::Error> {
let locks_addr = node_runtime::storage().balances().locks(bob);
let locks = api.storage().fetch_or_default(&locks_addr, None).await?;
let locks = api
.storage()
.at(None)
.await?
.fetch_or_default(&locks_addr)
.await?;
assert_eq!(
locks.0,
@@ -222,13 +222,23 @@ async fn tx_call() {
.contracts()
.contract_info_of(&contract);
let contract_info = cxt.client().storage().fetch(&info_addr, None).await;
let contract_info = cxt
.client()
.storage()
.at(None)
.await
.unwrap()
.fetch(&info_addr)
.await;
assert!(contract_info.is_ok());
let keys = cxt
.client()
.storage()
.fetch_keys(&info_addr.to_bytes(), 10, None, None)
.at(None)
.await
.unwrap()
.fetch_keys(&info_addr.to_bytes(), 10, None)
.await
.unwrap()
.iter()
+16 -3
View File
@@ -150,7 +150,13 @@ async fn chill_works_for_controller_only() -> Result<(), Error> {
.await?;
let ledger_addr = node_runtime::storage().staking().ledger(alice.account_id());
let ledger = api.storage().fetch(&ledger_addr, None).await?.unwrap();
let ledger = api
.storage()
.at(None)
.await?
.fetch(&ledger_addr)
.await?
.unwrap();
assert_eq!(alice_stash.account_id(), &ledger.stash);
let chill_tx = node_runtime::tx().staking().chill();
@@ -232,7 +238,9 @@ async fn storage_current_era() -> Result<(), Error> {
let current_era_addr = node_runtime::storage().staking().current_era();
let _current_era = api
.storage()
.fetch(&current_era_addr, None)
.at(None)
.await?
.fetch(&current_era_addr)
.await?
.expect("current era always exists");
Ok(())
@@ -243,7 +251,12 @@ async fn storage_era_reward_points() -> Result<(), Error> {
let ctx = test_context().await;
let api = ctx.client();
let reward_points_addr = node_runtime::storage().staking().eras_reward_points(0);
let current_era_result = api.storage().fetch(&reward_points_addr, None).await;
let current_era_result = api
.storage()
.at(None)
.await?
.fetch(&reward_points_addr)
.await;
assert!(current_era_result.is_ok());
Ok(())
@@ -24,7 +24,9 @@ async fn storage_account() -> Result<(), subxt::Error> {
let account_info = api
.storage()
.fetch_or_default(&account_info_addr, None)
.at(None)
.await?
.fetch_or_default(&account_info_addr)
.await;
assert_matches!(account_info, Ok(_));
@@ -14,7 +14,10 @@ async fn storage_get_current_timestamp() {
let timestamp = api
.storage()
.fetch(&node_runtime::storage().timestamp().now(), None)
.at(None)
.await
.unwrap()
.fetch(&node_runtime::storage().timestamp().now())
.await;
assert!(timestamp.is_ok())
+13 -3
View File
@@ -21,7 +21,12 @@ async fn storage_plain_lookup() -> Result<(), subxt::Error> {
wait_for_blocks(&api).await;
let addr = node_runtime::storage().timestamp().now();
let entry = api.storage().fetch_or_default(&addr, None).await?;
let entry = api
.storage()
.at(None)
.await?
.fetch_or_default(&addr)
.await?;
assert!(entry > 0);
Ok(())
@@ -45,7 +50,12 @@ async fn storage_map_lookup() -> Result<(), subxt::Error> {
// Look up the nonce for the user (we expect it to be 1).
let nonce_addr = node_runtime::storage().system().account(alice);
let entry = api.storage().fetch_or_default(&nonce_addr, None).await?;
let entry = api
.storage()
.at(None)
.await?
.fetch_or_default(&nonce_addr)
.await?;
assert_eq!(entry.nonce, 1);
Ok(())
@@ -113,7 +123,7 @@ async fn storage_n_map_storage_lookup() -> Result<(), subxt::Error> {
// The actual test; look up this approval in storage:
let addr = node_runtime::storage().assets().approvals(99, alice, bob);
let entry = api.storage().fetch(&addr, None).await?;
let entry = api.storage().at(None).await?.fetch(&addr).await?;
assert_eq!(entry.map(|a| a.amount), Some(123));
Ok(())
}