Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
reviews
  • Loading branch information
kostekIV committed Nov 18, 2022
commit a3c75ebb949681ad419257d42f77e8cf5b150a36
24 changes: 16 additions & 8 deletions aleph-client/src/connections.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use codec::Decode;
use log::info;
use subxt::{
Expand Down Expand Up @@ -58,19 +59,19 @@ impl Connection {
Self { client }
}

pub async fn get_storage_entry<T: DecodeWithMetadata, _0, _1>(
pub async fn get_storage_entry<T: DecodeWithMetadata, Defaultable, Iterable>(
&self,
addrs: &StaticStorageAddress<T, Yes, _0, _1>,
addrs: &StaticStorageAddress<T, Yes, Defaultable, Iterable>,
at: Option<BlockHash>,
) -> T::Target {
self.get_storage_entry_maybe(addrs, at)
.await
.expect("There should be a value")
}

pub async fn get_storage_entry_maybe<T: DecodeWithMetadata, _0, _1>(
pub async fn get_storage_entry_maybe<T: DecodeWithMetadata, Defaultable, Iterable>(
&self,
addrs: &StaticStorageAddress<T, Yes, _0, _1>,
addrs: &StaticStorageAddress<T, Yes, Defaultable, Iterable>,
at: Option<BlockHash>,
) -> Option<T::Target> {
info!(target: "aleph-client", "accessing storage at {}::{} at block {:?}", addrs.pallet_name(), addrs.entry_name(), at);
Expand Down Expand Up @@ -140,20 +141,27 @@ impl SignedConnection {
}

impl RootConnection {
pub async fn new(address: String, root: KeyPair) -> Result<Self, ()> {
pub async fn new(address: String, root: KeyPair) -> anyhow::Result<Self> {
RootConnection::try_from_connection(Connection::new(address).await, root).await
}

pub async fn try_from_connection(connection: Connection, signer: KeyPair) -> Result<Self, ()> {
pub async fn try_from_connection(
connection: Connection,
signer: KeyPair,
) -> anyhow::Result<Self> {
let root_address = api::storage().sudo().key();

let root = match connection.client.storage().fetch(&root_address, None).await {
Ok(Some(account)) => account,
_ => return Err(()),
_ => return Err(anyhow!("Could not read sudo key from chain")),
};

if root != *signer.account_id() {
return Err(());
return Err(anyhow!(
"Provided account is not a sudo on chain. sudo key - {}, provided: {}",
root,
signer.account_id()
));
}

Ok(Self {
Expand Down
4 changes: 2 additions & 2 deletions aleph-client/src/pallets/elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub trait ElectionsApi {

#[async_trait::async_trait]
pub trait ElectionsSudoApi {
async fn change_ban_config(
async fn set_ban_config(
&self,
minimal_expected_performance: Option<u8>,
underperformed_session_count_threshold: Option<u32>,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl ElectionsApi for Connection {

#[async_trait::async_trait]
impl ElectionsSudoApi for RootConnection {
async fn change_ban_config(
async fn set_ban_config(
&self,
minimal_expected_performance: Option<u8>,
underperformed_session_count_threshold: Option<u32>,
Expand Down
30 changes: 15 additions & 15 deletions aleph-client/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
pallet::pallet::{
Call::{bond, force_new_era, nominate, set_staking_configs},
ConfigOp,
ConfigOp::Set,
ConfigOp::{Noop, Set},
},
EraRewardPoints, Exposure, RewardDestination, StakingLedger, ValidatorPrefs,
},
Expand Down Expand Up @@ -99,8 +99,8 @@ pub trait StakingSudoApi {
async fn force_new_era(&self, status: TxStatus) -> anyhow::Result<BlockHash>;
async fn set_staking_config(
&self,
minimal_nominator_bond: u128,
minimal_validator_bond: u128,
minimal_nominator_bond: Option<u128>,
minimal_validator_bond: Option<u128>,
max_nominators_count: Option<u32>,
max_validators_count: Option<u32>,
status: TxStatus,
Expand Down Expand Up @@ -270,23 +270,23 @@ impl StakingSudoApi for RootConnection {

async fn set_staking_config(
&self,
min_nominator_bond: u128,
min_validator_bond: u128,
min_nominator_bond: Option<u128>,
min_validator_bond: Option<u128>,
max_nominator_count: Option<u32>,
max_validator_count: Option<u32>,
status: TxStatus,
) -> anyhow::Result<BlockHash> {
fn convert<T>(arg: Option<T>) -> ConfigOp<T> {
match arg {
Some(v) => Set(v),
None => Noop,
}
}
let call = Staking(set_staking_configs {
min_nominator_bond: Set(min_nominator_bond),
min_validator_bond: Set(min_validator_bond),
max_nominator_count: match max_nominator_count {
None => ConfigOp::Noop,
Some(m) => Set(m),
},
max_validator_count: match max_validator_count {
None => ConfigOp::Noop,
Some(m) => Set(m),
},
min_nominator_bond: convert(min_nominator_bond),
min_validator_bond: convert(min_validator_bond),
max_nominator_count: convert(max_nominator_count),
max_validator_count: convert(max_validator_count),
chill_threshold: ConfigOp::Noop,
min_commission: ConfigOp::Noop,
});
Expand Down
4 changes: 2 additions & 2 deletions aleph-client/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait BlocksApi {

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

#[async_trait::async_trait]
Expand Down Expand Up @@ -64,7 +64,7 @@ impl BlocksApi for Connection {

#[async_trait::async_trait]
impl SessionEraApi for Connection {
async fn get_era_for_session(&self, session: SessionIndex) -> EraIndex {
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
}
Expand Down
2 changes: 1 addition & 1 deletion bin/cliain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ConnectionConfig {
Connection::new(self.node_endpoint.clone()).await
}

pub async fn get_singed_connection(&self) -> SignedConnection {
pub async fn get_signed_connection(&self) -> SignedConnection {
SignedConnection::new(
self.node_endpoint.clone(),
keypair_from_string(&self.signer_seed),
Expand Down
28 changes: 14 additions & 14 deletions bin/cliain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async fn main() {
initial_stake_tokens,
} => {
bond(
cfg.get_singed_connection().await,
cfg.get_signed_connection().await,
initial_stake_tokens,
controller_account,
)
Expand All @@ -105,17 +105,17 @@ async fn main() {
set_emergency_finalizer(cfg.get_root_connection().await, finalizer).await;
}
Command::SetKeys { new_keys } => {
set_keys(cfg.get_singed_connection().await, new_keys).await
set_keys(cfg.get_signed_connection().await, new_keys).await
}
Command::Validate {
commission_percentage,
} => validate(cfg.get_singed_connection().await, commission_percentage).await,
} => validate(cfg.get_signed_connection().await, commission_percentage).await,
Command::Transfer {
amount_in_tokens,
to_account,
} => {
transfer(
cfg.get_singed_connection().await,
cfg.get_signed_connection().await,
amount_in_tokens,
to_account,
)
Expand All @@ -126,7 +126,7 @@ async fn main() {
beneficiary,
} => {
treasury_propose(
cfg.get_singed_connection().await,
cfg.get_signed_connection().await,
amount_in_tokens,
beneficiary,
)
Expand Down Expand Up @@ -170,9 +170,9 @@ async fn main() {
Command::UpdateRuntime { runtime } => {
update_runtime(cfg.get_root_connection().await, runtime).await
}
Command::Vest => vest(cfg.get_singed_connection().await).await,
Command::Vest => vest(cfg.get_signed_connection().await).await,
Command::VestOther { vesting_account } => {
vest_other(cfg.get_singed_connection().await, vesting_account).await
vest_other(cfg.get_signed_connection().await, vesting_account).await
}
Command::VestedTransfer {
to_account,
Expand All @@ -181,17 +181,17 @@ async fn main() {
starting_block,
} => {
vested_transfer(
cfg.get_singed_connection().await,
cfg.get_signed_connection().await,
to_account,
amount_in_tokens,
per_block,
starting_block,
)
.await
}
Command::Nominate { nominee } => nominate(cfg.get_singed_connection().await, nominee).await,
Command::Nominate { nominee } => nominate(cfg.get_signed_connection().await, nominee).await,
Command::ContractInstantiateWithCode(command) => {
match instantiate_with_code(cfg.get_singed_connection().await, command).await {
match instantiate_with_code(cfg.get_signed_connection().await, command).await {
Ok(result) => println!(
"{}",
serde_json::to_string(&result).expect("Can't encode the result as JSON")
Expand All @@ -200,19 +200,19 @@ async fn main() {
};
}
Command::ContractUploadCode(command) => {
match upload_code(cfg.get_singed_connection().await, command).await {
match upload_code(cfg.get_signed_connection().await, command).await {
Ok(result) => println!("{:?}", result),
Err(why) => error!("Contract upload failed {:?}", why),
}
}
Command::ContractCall(command) => {
match call(cfg.get_singed_connection().await, command).await {
match call(cfg.get_signed_connection().await, command).await {
Ok(result) => println!("{:?}", result),
Err(why) => error!("Contract call failed {:?}", why),
}
}
Command::ContractInstantiate(command) => {
match instantiate(cfg.get_singed_connection().await, command).await {
match instantiate(cfg.get_signed_connection().await, command).await {
Ok(result) => println!("{:?}", result),
Err(why) => error!("Contract instantiate failed {:?}", why),
}
Expand All @@ -224,7 +224,7 @@ async fn main() {
)
}
Command::ContractRemoveCode(command) => {
match remove_code(cfg.get_singed_connection().await, command).await {
match remove_code(cfg.get_signed_connection().await, command).await {
Ok(result) => println!("{:?}", result),
Err(why) => error!("Contract remove code failed {:?}", why),
}
Expand Down
4 changes: 2 additions & 2 deletions bin/cliain/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ pub async fn set_staking_limits(
) {
root_connection
.set_staking_config(
minimal_nominator_stake_tokens as u128 * TOKEN,
minimal_validator_stake_tokens as u128 * TOKEN,
Some(minimal_nominator_stake_tokens as u128 * TOKEN),
Some(minimal_validator_stake_tokens as u128 * TOKEN),
max_nominators_count,
max_validators_count,
TxStatus::Finalized,
Expand Down
16 changes: 2 additions & 14 deletions e2e-tests/src/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,8 @@ pub async fn check_underperformed_validator_reason(
.get_ban_info_for_validator(validator.clone(), None)
.await;

match (validator_ban_info.as_ref(), expected_info) {
(Some(info), Some(expected_info)) => {
// terrible hack for now :(
assert_eq!(info.reason.encode(), expected_info.reason.encode());
assert_eq!(info.start, expected_info.start);

validator_ban_info
}
(None, None) => None,
_ => panic!(
"expected infos to be equal: expected {:?}, got {:?}",
expected_info, validator_ban_info
),
}
assert_eq!(validator_ban_info.as_ref(), expected_info);
validator_ban_info
}

pub async fn check_ban_info_for_validator(
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::accounts::{get_sudo_key, get_validators_keys, get_validators_seeds, N
#[clap(version = "1.0")]
pub struct Config {
/// WS endpoint address of the node to connect to
#[clap(long, default_value = "127.0.0.1:9943")]
#[clap(long, default_value = "ws://127.0.0.1:9943")]
pub node: String,

/// Test cases to run.
Expand Down
14 changes: 2 additions & 12 deletions e2e-tests/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::collections::{HashMap, HashSet};

use aleph_client::{
account_from_keypair,
aleph_runtime::SessionKeys,
api::runtime_types,
pallets::{
author::AuthorRpc,
balances::{BalanceUserApi, BalanceUserBatchExtApi},
Expand Down Expand Up @@ -37,19 +35,11 @@ type RewardPoint = u32;
pub async fn set_invalid_keys_for_validator(
controller_connection: &SignedConnection,
) -> anyhow::Result<()> {
const ZERO_SESSION_KEYS: SessionKeys = SessionKeys {
// todo: clean this
aura: runtime_types::sp_consensus_aura::sr25519::app_sr25519::Public(
runtime_types::sp_core::sr25519::Public([0; 32]),
),
aleph: runtime_types::primitives::app::Public(runtime_types::sp_core::ed25519::Public(
[0; 32],
)),
};
let zero_session_keys = [0; 64].to_vec().into();

// wait until our node is forced to use new keys, i.e. current session + 2
controller_connection
.set_keys(ZERO_SESSION_KEYS, TxStatus::InBlock)
.set_keys(zero_session_keys, TxStatus::InBlock)
.await
.unwrap();
controller_connection
Expand Down
6 changes: 2 additions & 4 deletions e2e-tests/src/test/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,8 @@ pub async fn clearing_session_count(config: &Config) -> anyhow::Result<()> {
let (root_connection, reserved_validators, non_reserved_validators, _) =
setup_test(config).await?;

info!(target: "aleph-client", "changing ban config");

root_connection
.change_ban_config(None, Some(3), Some(2), None, TxStatus::InBlock)
.set_ban_config(None, Some(3), Some(2), None, TxStatus::InBlock)
.await?;

let validator_to_disable =
Expand Down Expand Up @@ -294,7 +292,7 @@ pub async fn ban_threshold(config: &Config) -> anyhow::Result<()> {

// Change ban config to require prohibitively high performance from all validators.
root_connection
.change_ban_config(
.set_ban_config(
Some(MIN_EXPECTED_PERFORMANCE),
None,
None,
Expand Down
Loading