Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aleph-client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion aleph-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "aleph_client"
version = "2.4.0"
# TODO bump major version when API stablize
version = "2.5.0"
edition = "2021"
license = "Apache 2.0"

Expand Down
13 changes: 13 additions & 0 deletions aleph-client/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{thread::sleep, time::Duration};
use anyhow::anyhow;
use codec::Decode;
use log::info;
use primitives::BlockNumber;
use subxt::{
ext::sp_core::Bytes,
metadata::DecodeWithMetadata,
Expand Down Expand Up @@ -113,6 +114,18 @@ impl Connection {

Ok(R::decode(&mut bytes.as_ref())?)
}

pub async fn get_block_number_inner(
Comment thread
pmikolajczyk41 marked this conversation as resolved.
Outdated
&self,
block: Option<BlockHash>,
) -> anyhow::Result<Option<BlockNumber>> {
self.client
.rpc()
.header(block)
.await
.map(|maybe_header| maybe_header.map(|header| header.number))
.map_err(|e| e.into())
}
}

impl SignedConnection {
Expand Down
8 changes: 4 additions & 4 deletions aleph-client/src/pallets/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::{aleph_runtime::SessionKeys, Connection};

#[async_trait::async_trait]
pub trait AuthorRpc {
async fn author_rotate_keys(&self) -> SessionKeys;
async fn author_rotate_keys(&self) -> anyhow::Result<SessionKeys>;
}

#[async_trait::async_trait]
impl AuthorRpc for Connection {
async fn author_rotate_keys(&self) -> SessionKeys {
let bytes = self.client.rpc().rotate_keys().await.unwrap();
async fn author_rotate_keys(&self) -> anyhow::Result<SessionKeys> {
let bytes = self.client.rpc().rotate_keys().await?;

SessionKeys::decode(&mut bytes.0.as_slice()).unwrap()
SessionKeys::decode(&mut bytes.0.as_slice()).map_err(|e| e.into())
}
}
8 changes: 7 additions & 1 deletion aleph-client/src/pallets/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,13 @@ impl ContractRpc for Connection {

let res: ContractExecResult<Balance> =
self.rpc_call("state_call".to_string(), params).await?;
let res = T::decode(&mut (res.result.unwrap().data.as_slice()))?;
let res = T::decode(
&mut (res
.result
.expect("Failed to decode execution result of the wasm code!")
.data
.as_slice()),
)?;

Ok(res)
}
Expand Down
6 changes: 3 additions & 3 deletions aleph-client/src/pallets/elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub trait ElectionsApi {
validator: AccountId,
at: Option<BlockHash>,
) -> Option<BanInfo>;
async fn get_session_period(&self) -> u32;
async fn get_session_period(&self) -> anyhow::Result<u32>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -164,10 +164,10 @@ impl ElectionsApi for Connection {
self.get_storage_entry_maybe(&addrs, at).await
}

async fn get_session_period(&self) -> u32 {
async fn get_session_period(&self) -> anyhow::Result<u32> {
let addrs = api::constants().elections().session_period();

self.client.constants().at(&addrs).unwrap()
self.client.constants().at(&addrs).map_err(|e| e.into())
}
}

Expand Down
12 changes: 6 additions & 6 deletions aleph-client/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub trait StakingApi {
at: Option<BlockHash>,
) -> Option<EraRewardPoints<AccountId>>;
async fn get_minimum_validator_count(&self, at: Option<BlockHash>) -> u32;
async fn get_session_per_era(&self) -> u32;
async fn get_session_per_era(&self) -> anyhow::Result<u32>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -113,7 +113,7 @@ pub trait StakingRawApi {
&self,
era: EraIndex,
at: Option<BlockHash>,
) -> Vec<StorageKey>;
) -> anyhow::Result<Vec<StorageKey>>;
async fn get_stakers_storage_keys_from_accounts(
&self,
era: EraIndex,
Expand Down Expand Up @@ -181,10 +181,10 @@ impl StakingApi for Connection {
self.get_storage_entry(&addrs, at).await
}

async fn get_session_per_era(&self) -> u32 {
async fn get_session_per_era(&self) -> anyhow::Result<u32> {
let addrs = api::constants().staking().sessions_per_era();

self.client.constants().at(&addrs).unwrap()
self.client.constants().at(&addrs).map_err(|e| e.into())
}
}

Expand Down Expand Up @@ -300,15 +300,15 @@ impl StakingRawApi for Connection {
&self,
era: EraIndex,
at: Option<BlockHash>,
) -> Vec<StorageKey> {
) -> anyhow::Result<Vec<StorageKey>> {
let key_addrs = api::storage().staking().eras_stakers_root();
let mut key = key_addrs.to_root_bytes();
StorageMapKey::new(era, StorageHasher::Twox64Concat).to_bytes(&mut key);
self.client
.storage()
.fetch_keys(&key, 10, None, at)
.await
.unwrap()
.map_err(|e| e.into())
}

async fn get_stakers_storage_keys_from_accounts(
Expand Down
10 changes: 5 additions & 5 deletions aleph-client/src/pallets/treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait TreasuryUserApi {

#[async_trait::async_trait]
pub trait TreasureApiExt {
async fn possible_treasury_payout(&self) -> Balance;
async fn possible_treasury_payout(&self) -> anyhow::Result<Balance>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -106,12 +106,12 @@ impl TreasurySudoApi for RootConnection {

#[async_trait::async_trait]
impl TreasureApiExt for Connection {
async fn possible_treasury_payout(&self) -> Balance {
let session_period = self.get_session_period().await;
let sessions_per_era = self.get_session_per_era().await;
async fn possible_treasury_payout(&self) -> anyhow::Result<Balance> {
let session_period = self.get_session_period().await?;
let sessions_per_era = self.get_session_per_era().await?;

let millisecs_per_era =
MILLISECS_PER_BLOCK * session_period as u64 * sessions_per_era as u64;
primitives::staking::era_payout(millisecs_per_era).1
Ok(primitives::staking::era_payout(millisecs_per_era).1)
}
}
12 changes: 10 additions & 2 deletions aleph-client/src/runtime_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ impl From<Vec<u8>> for SessionKeys {
fn from(bytes: Vec<u8>) -> Self {
assert_eq!(bytes.len(), 64);
Self {
aura: AuraPublic(SrPublic(bytes[..32].try_into().unwrap())),
aleph: AlephPublic(EdPublic(bytes[32..64].try_into().unwrap())),
aura: AuraPublic(SrPublic(
bytes[..32]
.try_into()
.expect("Failed to convert bytes slice to an Aura key!"),
)),
aleph: AlephPublic(EdPublic(
bytes[32..64]
.try_into()
.expect("Failed to convert bytes slice to an Aleph key!"),
)),
}
}
}
Expand Down
57 changes: 28 additions & 29 deletions aleph-client/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,63 @@ use crate::{

#[async_trait::async_trait]
pub trait BlocksApi {
async fn first_block_of_session(&self, session: SessionIndex) -> Option<BlockHash>;
async fn get_block_hash(&self, block: BlockNumber) -> Option<BlockHash>;
async fn get_best_block(&self) -> BlockNumber;
async fn get_finalized_block_hash(&self) -> BlockHash;
async fn get_block_number(&self, block: BlockHash) -> Option<BlockNumber>;
async fn first_block_of_session(
&self,
session: SessionIndex,
) -> anyhow::Result<Option<BlockHash>>;
async fn get_block_hash(&self, block: BlockNumber) -> anyhow::Result<Option<BlockHash>>;
async fn get_best_block(&self) -> anyhow::Result<Option<BlockNumber>>;
async fn get_finalized_block_hash(&self) -> anyhow::Result<BlockHash>;
async fn get_block_number(&self, block: BlockHash) -> anyhow::Result<Option<BlockNumber>>;
}

#[async_trait::async_trait]
pub trait SessionEraApi {
async fn get_active_era_for_session(&self, session: SessionIndex) -> EraIndex;
async fn get_active_era_for_session(&self, session: SessionIndex) -> anyhow::Result<EraIndex>;
}

#[async_trait::async_trait]
impl BlocksApi for Connection {
async fn first_block_of_session(&self, session: SessionIndex) -> Option<BlockHash> {
let period = self.get_session_period().await;
async fn first_block_of_session(
&self,
session: SessionIndex,
) -> anyhow::Result<Option<BlockHash>> {
let period = self.get_session_period().await?;
let block_num = period * session;

self.get_block_hash(block_num).await
}

async fn get_block_hash(&self, block: BlockNumber) -> Option<BlockHash> {
async fn get_block_hash(&self, block: BlockNumber) -> anyhow::Result<Option<BlockHash>> {
info!(target: "aleph-client", "querying block hash for number #{}", block);
self.client
.rpc()
.block_hash(Some(block.into()))
.await
.unwrap()
.map_err(|e| e.into())
}

async fn get_best_block(&self) -> BlockNumber {
self.client
.rpc()
.header(None)
.await
.unwrap()
.unwrap()
.number
async fn get_best_block(&self) -> anyhow::Result<Option<BlockNumber>> {
self.get_block_number_inner(None).await
}

async fn get_finalized_block_hash(&self) -> BlockHash {
self.client.rpc().finalized_head().await.unwrap()
}

async fn get_block_number(&self, block: BlockHash) -> Option<BlockNumber> {
async fn get_finalized_block_hash(&self) -> anyhow::Result<BlockHash> {
self.client
.rpc()
.header(Some(block))
.finalized_head()
.await
.unwrap()
.map(|h| h.number)
.map_err(|e| e.into())
}

async fn get_block_number(&self, block: BlockHash) -> anyhow::Result<Option<BlockNumber>> {
self.get_block_number_inner(Some(block)).await
Comment thread
pmikolajczyk41 marked this conversation as resolved.
}
}

#[async_trait::async_trait]
impl SessionEraApi for Connection {
async fn get_active_era_for_session(&self, session: SessionIndex) -> EraIndex {
let block = self.first_block_of_session(session).await;
self.get_active_era(block).await
async fn get_active_era_for_session(&self, session: SessionIndex) -> anyhow::Result<EraIndex> {
let block = self.first_block_of_session(session).await?;
Ok(self.get_active_era(block).await)
}
}
36 changes: 30 additions & 6 deletions aleph-client/src/waiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ pub trait WaitingExt {
impl AlephWaiting for Connection {
async fn wait_for_block<P: Fn(u32) -> bool + Send>(&self, predicate: P, status: BlockStatus) {
let mut block_sub = match status {
BlockStatus::Best => self.client.blocks().subscribe_best().await.unwrap(),
BlockStatus::Finalized => self.client.blocks().subscribe_finalized().await.unwrap(),
BlockStatus::Best => self
.client
.blocks()
.subscribe_best()
.await
.expect("Failed to subscribe to the best block stream!"),
BlockStatus::Finalized => self
.client
.blocks()
.subscribe_finalized()
.await
.expect("Failed to subscribe to the finalized block stream!"),
};

while let Some(Ok(block)) = block_sub.next().await {
Expand All @@ -54,8 +64,18 @@ impl AlephWaiting for Connection {
status: BlockStatus,
) -> T {
let mut block_sub = match status {
BlockStatus::Best => self.client.blocks().subscribe_best().await.unwrap(),
BlockStatus::Finalized => self.client.blocks().subscribe_finalized().await.unwrap(),
BlockStatus::Best => self
.client
.blocks()
.subscribe_best()
.await
.expect("Failed to subscribe to the best block stream!"),
BlockStatus::Finalized => self
.client
.blocks()
.subscribe_finalized()
.await
.expect("Failed to subscribe to the finalized block stream!"),
};

info!(target: "aleph-client", "waiting for event {}.{}", T::PALLET, T::EVENT);
Expand All @@ -67,7 +87,7 @@ impl AlephWaiting for Connection {
};

for event in events.iter() {
let event = event.unwrap();
let event = event.expect("Failed to obtain event from the block!");
if let Ok(Some(ev)) = event.as_event::<T>() {
if predicate(&ev) {
return ev;
Expand All @@ -81,7 +101,11 @@ impl AlephWaiting for Connection {

async fn wait_for_era(&self, era: EraIndex, status: BlockStatus) {
let addrs = aleph_zero::api::constants().staking().sessions_per_era();
let sessions_per_era = self.client.constants().at(&addrs).unwrap();
let sessions_per_era = self
.client
.constants()
.at(&addrs)
.expect("Failed to obtain sessions_per_era const!");
let first_session_in_era = era * sessions_per_era;

self.wait_for_session(first_session_in_era, status).await;
Expand Down
2 changes: 1 addition & 1 deletion benches/payout-stakers/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bin/cliain/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/cliain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cliain"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
license = "GPL-3.0-or-later"

Expand Down
Loading