Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2e22b79
dont use String for creating connections
krzysztofziobro Dec 27, 2022
20aafa6
do not wrap client
krzysztofziobro Dec 27, 2022
1f10b03
wip
krzysztofziobro Dec 28, 2022
2f5cda9
should work as old one
krzysztofziobro Dec 28, 2022
f5b769f
hide connection
krzysztofziobro Dec 28, 2022
44a4e9a
hide the rest
krzysztofziobro Dec 28, 2022
1472fd7
Don not hide clone
krzysztofziobro Dec 28, 2022
40225ca
Merge branch 'main' into A0-1613-improve-connection
krzysztofziobro Dec 29, 2022
168ce19
bump
krzysztofziobro Dec 29, 2022
0247e81
wip
krzysztofziobro Dec 30, 2022
4c1a6eb
wrap client
krzysztofziobro Dec 30, 2022
e41d124
rename client
krzysztofziobro Dec 30, 2022
7968aea
add Clone
krzysztofziobro Dec 30, 2022
9a979f6
Merge branch 'main' into A0-1613-improve-connection
krzysztofziobro Dec 30, 2022
50d7071
more general adder
krzysztofziobro Dec 30, 2022
dba9561
Add AsSigned
krzysztofziobro Dec 30, 2022
d700129
add impls for references
krzysztofziobro Dec 30, 2022
e45f361
wip
krzysztofziobro Jan 2, 2023
e6839b8
change TreasurySudoApi
krzysztofziobro Jan 2, 2023
8ec4625
Merge branch 'main' into A0-1613-improve-connection
krzysztofziobro Jan 2, 2023
3bf05d5
add methods to SignedConnectionApi
krzysztofziobro Jan 2, 2023
6b259b2
Merge branch 'A0-1613-improve-connection' of github.com:Cardinal-Cryp…
krzysztofziobro Jan 2, 2023
23d5a58
remove unnecessary casts
krzysztofziobro Jan 2, 2023
5600e11
Merge branch 'main' into A0-1613-improve-connection
krzysztofziobro Jan 2, 2023
b7ad771
review part 1
krzysztofziobro Jan 2, 2023
d156320
Merge branch 'A0-1613-improve-connection' of github.com:Cardinal-Cryp…
krzysztofziobro Jan 2, 2023
ef5625d
review part 2
krzysztofziobro Jan 2, 2023
af4d737
Make AsConnection visible only in crate
krzysztofziobro Jan 3, 2023
d15be1b
Merge branch 'main' into A0-1613-improve-connection
krzysztofziobro Jan 3, 2023
70db49a
Remove unnecessary dependencies on ConnectionApi
krzysztofziobro Jan 4, 2023
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
Merge branch 'main' into A0-1613-improve-connection
  • Loading branch information
krzysztofziobro committed Dec 29, 2022
commit 40225ca581cc8daebca82f7d086800ae2882b847
5 changes: 2 additions & 3 deletions aleph-client/src/pallets/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ pub trait AuthorRpc {

#[async_trait::async_trait]
impl<C: ConnectionExt> AuthorRpc for C {
async fn author_rotate_keys(&self) -> SessionKeys {
let bytes = self.as_connection().rpc().rotate_keys().await.unwrap();

async fn author_rotate_keys(&self) -> anyhow::Result<SessionKeys> {
let bytes = self.as_connection().rpc().rotate_keys().await?;
SessionKeys::decode(&mut bytes.0.as_slice()).map_err(|e| e.into())
}
}
6 changes: 4 additions & 2 deletions aleph-client/src/pallets/elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ impl<C: ConnectionExt> ElectionsApi for C {

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

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

Expand Down
6 changes: 4 additions & 2 deletions aleph-client/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ impl<C: ConnectionExt> StakingApi for C {

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

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

Expand Down
7 changes: 3 additions & 4 deletions aleph-client/src/pallets/treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@ impl TreasurySudoApi for RootConnection {

#[async_trait::async_trait]
impl<C: ConnectionExt> TreasureApiExt for C {
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;
Ok(primitives::staking::era_payout(millisecs_per_era).1)
Expand Down
56 changes: 28 additions & 28 deletions aleph-client/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,24 @@ pub trait BlocksApi {
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 fn get_block_number_opt(
&self,
block: Option<BlockHash>,
) -> anyhow::Result<Option<BlockNumber>>;
}

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

impl Connection {
async fn get_block_number_inner(
&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())
}
}

#[async_trait::async_trait]
impl<C: ConnectionExt> BlocksApi for C {
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
Expand All @@ -55,32 +48,39 @@ impl<C: ConnectionExt> BlocksApi for C {
.map_err(|e| e.into())
}

async fn get_best_block(&self) -> BlockNumber {
async fn get_best_block(&self) -> anyhow::Result<Option<BlockNumber>> {
self.get_block_number_opt(None).await
}

async fn get_finalized_block_hash(&self) -> anyhow::Result<BlockHash> {
self.as_connection()
.rpc()
.finalized_head()
.await
.map_err(|e| e.into())
}

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

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

async fn get_block_number(&self, block: BlockHash) -> anyhow::Result<Option<BlockNumber>> {
self.get_block_number_opt(Some(block)).await
}
}

#[async_trait::async_trait]
impl<C: ConnectionExt> SessionEraApi for C {
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)
}
}
14 changes: 9 additions & 5 deletions aleph-client/src/waiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ impl<C: ConnectionExt> AlephWaiting for C {
.blocks()
.subscribe_best()
.await
.unwrap(),
.expect("Failed to subscribe to the best block stream!"),
BlockStatus::Finalized => self
.as_connection()
.blocks()
.subscribe_finalized()
.await
.unwrap(),
.expect("Failed to subscribe to the finalized block stream!"),
};

while let Some(Ok(block)) = block_sub.next().await {
Expand All @@ -69,13 +69,13 @@ impl<C: ConnectionExt> AlephWaiting for C {
.blocks()
.subscribe_best()
.await
.unwrap(),
.expect("Failed to subscribe to the best block stream!"),
BlockStatus::Finalized => self
.as_connection()
.blocks()
.subscribe_finalized()
.await
.unwrap(),
.expect("Failed to subscribe to the finalized block stream!"),
};

info!(target: "aleph-client", "waiting for event {}.{}", T::PALLET, T::EVENT);
Expand All @@ -101,7 +101,11 @@ impl<C: ConnectionExt> AlephWaiting for C {

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.as_connection().constants().at(&addrs).unwrap();
let sessions_per_era = self
.as_connection()
.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 bin/cliain/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn prepare_keys(
)
.await
.unwrap();
let new_keys = connection.author_rotate_keys().await;
let new_keys = connection.author_rotate_keys().await?;
connection
.as_signed()
.set_keys(new_keys, TxStatus::Finalized)
Expand Down
15 changes: 8 additions & 7 deletions e2e-tests/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn set_invalid_keys_for_validator(
pub(super) async fn reset_validator_keys(
controller_connection: &SignedConnection,
) -> anyhow::Result<()> {
let validator_keys = controller_connection.author_rotate_keys().await;
let validator_keys = controller_connection.author_rotate_keys().await?;
controller_connection
.set_keys(validator_keys, TxStatus::InBlock)
.await
Expand Down Expand Up @@ -161,7 +161,7 @@ pub async fn check_points(
members_per_session: u32,
max_relative_difference: f64,
) -> anyhow::Result<()> {
let session_period = connection.get_session_period().await;
let session_period = connection.get_session_period().await?;

info!("Era: {} | session: {}.", era, session);

Expand All @@ -172,11 +172,12 @@ pub async fn check_points(
.wait_for_block(|n| n >= end_of_session_block, BlockStatus::Finalized)
.await;

let beginning_of_session_block_hash =
connection.get_block_hash(beginning_of_session_block).await;
let end_of_session_block_hash = connection.get_block_hash(end_of_session_block).await;
let beginning_of_session_block_hash = connection
.get_block_hash(beginning_of_session_block)
.await?;
let end_of_session_block_hash = connection.get_block_hash(end_of_session_block).await?;
let before_end_of_session_block_hash =
connection.get_block_hash(end_of_session_block - 1).await;
connection.get_block_hash(end_of_session_block - 1).await?;
info!(
"End-of-session block hash: {:?}.",
end_of_session_block_hash
Expand Down Expand Up @@ -322,7 +323,7 @@ pub async fn setup_validators(
root_connection.wait_for_n_eras(1, BlockStatus::Best).await;
let session = root_connection.get_session(None).await;

let first_block_in_session = root_connection.first_block_of_session(session).await;
let first_block_in_session = root_connection.first_block_of_session(session).await?;
let network_validators = root_connection
.get_current_era_validators(first_block_in_session)
.await;
Expand Down
5 changes: 4 additions & 1 deletion e2e-tests/src/test/era_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ pub async fn era_validators() -> anyhow::Result<()> {
"Non-reserved validators set is not properly updated in the next era."
);

let block_number = connection.get_best_block().await;
let block_number = connection
.get_best_block()
.await?
.ok_or(anyhow!("Failed to retrieve best block number!"))?;
connection
.wait_for_block(|n| n >= block_number, BlockStatus::Finalized)
.await;
Expand Down
10 changes: 8 additions & 2 deletions e2e-tests/src/test/finalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ pub async fn finalization() -> anyhow::Result<()> {
let config = setup_test();
let connection = config.create_root_connection().await;

let finalized = connection.get_finalized_block_hash().await;
let finalized_number = connection.get_block_number(finalized).await.unwrap();
let finalized = connection.get_finalized_block_hash().await?;
let finalized_number = connection
.get_block_number(finalized)
.await?
.ok_or(anyhow!(
"Failed to retrieve block number for hash {finalized:?}"
))?;

connection
.wait_for_block(|n| n > finalized_number, BlockStatus::Finalized)
.await;
Expand Down
10 changes: 5 additions & 5 deletions e2e-tests/src/test/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn points_basic() -> anyhow::Result<()> {
);

for session in start_session..end_session {
let era = connection.get_active_era_for_session(session).await;
let era = connection.get_active_era_for_session(session).await?;
let (members_active, members_bench) = get_and_test_members_for_session(
&connection,
committee_size.clone(),
Expand Down Expand Up @@ -97,7 +97,7 @@ pub async fn points_stake_change() -> anyhow::Result<()> {
);

for session in start_session..end_session {
let era = connection.get_active_era_for_session(session).await;
let era = connection.get_active_era_for_session(session).await?;
let (members_active, members_bench) = get_and_test_members_for_session(
&connection,
committee_size.clone(),
Expand Down Expand Up @@ -147,7 +147,7 @@ pub async fn disable_node() -> anyhow::Result<()> {
);

for session in start_session..end_session {
let era = root_connection.get_active_era_for_session(session).await;
let era = root_connection.get_active_era_for_session(session).await?;
let (members_active, members_bench) = get_and_test_members_for_session(
&controller_connection,
committee_size.clone(),
Expand Down Expand Up @@ -182,7 +182,7 @@ pub async fn force_new_era() -> anyhow::Result<()> {

let connection = config.get_first_signed_connection().await;
let root_connection = config.create_root_connection().await;
let start_era = connection.get_active_era_for_session(start_session).await;
let start_era = connection.get_active_era_for_session(start_session).await?;

info!("Start | era: {}, session: {}", start_era, start_session);

Expand Down Expand Up @@ -223,7 +223,7 @@ pub async fn change_stake_and_force_new_era() -> anyhow::Result<()> {
let connection = config.get_first_signed_connection().await;
let root_connection = config.create_root_connection().await;

let start_era = connection.get_active_era_for_session(start_session).await;
let start_era = connection.get_active_era_for_session(start_session).await?;
info!("Start | era: {}, session: {}", start_era, start_session);

validators_bond_extra_stakes(
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/src/test/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub async fn staking_new_validator() -> anyhow::Result<()> {
&stash_account, &controller_account, &bonded_controller_account
);

let validator_keys = root_connection.author_rotate_keys().await;
let validator_keys = root_connection.author_rotate_keys().await?;
let controller_connection =
SignedConnection::new(node, KeyPair::new(controller.signer().clone())).await;
controller_connection
Expand Down
3 changes: 2 additions & 1 deletion e2e-tests/src/test/treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub async fn channeling_fee_and_tip() -> anyhow::Result<()> {
let (connection, to) = setup_for_transfer(config).await;

let (treasury_balance_before, issuance_before) = balance_info(&connection).await;
let possible_treasury_gain_from_staking = connection.possible_treasury_payout().await;
let possible_treasury_gain_from_staking = connection.possible_treasury_payout().await?;

let (fee, _) = current_fees(&connection, to, Some(tip), transfer_amount).await;
let (treasury_balance_after, issuance_after) = balance_info(&connection).await;

Expand Down
6 changes: 5 additions & 1 deletion e2e-tests/src/test/validators_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ pub async fn change_validators() -> anyhow::Result<()> {
},
committee_size_after
);
let block_number = connection.get_best_block().await;

let block_number = connection
.get_best_block()
.await?
.ok_or(anyhow!("Failed to retrieve best block!"))?;
connection
.wait_for_block(|n| n >= block_number, BlockStatus::Finalized)
.await;
Expand Down
7 changes: 5 additions & 2 deletions e2e-tests/src/test/validators_rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn validators_rotate() -> anyhow::Result<()> {

for session in current_session..current_session + TEST_LENGTH {
let elected = connection
.get_validators(connection.first_block_of_session(session).await)
.get_validators(connection.first_block_of_session(session).await?)
.await;

let non_reserved = get_members_subset_for_session(
Expand Down Expand Up @@ -99,7 +99,10 @@ pub async fn validators_rotate() -> anyhow::Result<()> {
let min_elected = non_reserved_count.values().min().unwrap();
assert!(max_elected - min_elected <= 1);

let block_number = connection.get_best_block().await;
let block_number = connection
.get_best_block()
.await?
.ok_or(anyhow!("Failed to retrieve best block number!"))?;
connection
.wait_for_block(|n| n >= block_number, BlockStatus::Finalized)
.await;
Expand Down
11 changes: 7 additions & 4 deletions e2e-tests/src/test/version_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ pub async fn schedule_version_change() -> anyhow::Result<()> {
.wait_for_session(session_after_upgrade + 1, BlockStatus::Finalized)
.await;

let block_number = connection.get_best_block().await;
let block_number = connection
.get_best_block()
.await?
.ok_or(anyhow!("Failed to retrieve best block number!"))?;
connection
.wait_for_block(|n| n >= block_number, BlockStatus::Finalized)
.await;
Expand Down Expand Up @@ -83,11 +86,11 @@ pub async fn schedule_doomed_version_change_and_verify_finalization_stopped() ->
.wait_for_session(session_after_upgrade + 1, BlockStatus::Best)
.await;

let last_finalized_block = session_for_upgrade * connection.get_session_period().await - 1;
let last_finalized_block = session_for_upgrade * connection.get_session_period().await? - 1;

let connection = connection;
let finalized_block_head = connection.get_finalized_block_hash().await;
let finalized_block = connection.get_block_number(finalized_block_head).await;
let finalized_block_head = connection.get_finalized_block_hash().await?;
let finalized_block = connection.get_block_number(finalized_block_head).await?;

let finalized_block = match finalized_block {
Some(block) => block,
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.