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
A0-1608 fixes after conflicts
  • Loading branch information
Marcin-Radecki committed Jan 5, 2023
commit 9f9df33a162927b33f53a9c08c7954ff7d31f3eb
35 changes: 25 additions & 10 deletions aleph-client/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ pub trait ConnectionApi: Sync {
async fn rpc_call<R: Decode>(&self, func_name: String, params: RpcParams) -> anyhow::Result<R>;
}


/// Signed connection should be able to sends transactions to chain
#[async_trait::async_trait]
pub trait SignedConnectionApi: ConnectionApi {

/// Send a transaction to a chain. It waits for a given tx `status`.
/// * `tx` - encoded transaction payload
/// * `status` - a [`TxStatus`] for a tx to wait for
Expand All @@ -126,7 +124,6 @@ pub trait SignedConnectionApi: ConnectionApi {
status: TxStatus,
) -> anyhow::Result<BlockHash>;


/// Send a transaction to a chain. It waits for a given tx `status`.
/// * `tx` - encoded transaction payload
/// * `params` - optional tx params e.g. tip
Expand All @@ -140,8 +137,13 @@ pub trait SignedConnectionApi: ConnectionApi {
status: TxStatus,
) -> anyhow::Result<BlockHash>;

/// Returns account id which signs this connection
fn account_id(&self) -> &AccountId;

/// Returns a [`KeyPair`] which signs this connection
fn signer(&self) -> &KeyPair;

/// Tries to convert [`SignedConnection`] as [`RootConnection`]
async fn try_as_root(&self) -> anyhow::Result<RootConnection>;
}

Expand Down Expand Up @@ -311,10 +313,16 @@ impl Connection {
const DEFAULT_RETRIES: u32 = 10;
const RETRY_WAIT_SECS: u64 = 1;

/// Creates new connection from a given url.
/// By default, it tries to connect 10 times, waiting 1 second between each unsuccessful attempt.
/// * `address` - address in websocket format, e.g. `ws://127.0.0.1:9943`
pub async fn new(address: &str) -> Connection {
Self::new_with_retries(address, Self::DEFAULT_RETRIES).await
}

/// Creates new connection from a given url and given number of connection attempts.
/// * `address` - address in websocket format, e.g. `ws://127.0.0.1:9943`
/// * `retries` - number of connection attempts
async fn new_with_retries(address: &str, mut retries: u32) -> Connection {
loop {
let client = SubxtClient::from_url(&address).await;
Expand All @@ -335,20 +343,34 @@ impl Connection {
}

impl SignedConnection {
/// Creates new signed connection from existing [`Connection`] object.
/// * `connection` - existing connection
/// * `signer` - a [`KeyPair`] of signing account
pub async fn new(address: &str, signer: KeyPair) -> Self {
Self::from_connection(Connection::new(address).await, signer)
}

/// Creates new signed connection from existing [`Connection`] object.
/// * `connection` - existing connection
/// * `signer` - a [`KeyPair`] of signing account
pub fn from_connection(connection: Connection, signer: KeyPair) -> Self {
Self { connection, signer }
}
}

impl RootConnection {
/// Creates new root connection from a given url.
/// It tries to connect 10 times, waiting 1 second between each unsuccessful attempt.
/// * `address` - address in websocket format, e.g. `ws://127.0.0.1:9943`
/// * `root` - a [`KeyPair`] of the Sudo account
pub async fn new(address: &str, root: KeyPair) -> anyhow::Result<Self> {
RootConnection::try_from_connection(Connection::new(address).await, root).await
}

/// Creates new root connection from a given [`Connection`] object. It validates whether given
/// key is really a sudo account
/// * `connection` - existing connection
/// * `signer` - a [`KeyPair`] of the Sudo account
pub async fn try_from_connection(
connection: Connection,
signer: KeyPair,
Expand Down Expand Up @@ -377,11 +399,4 @@ impl RootConnection {
connection: SignedConnection { connection, signer },
})
}

pub fn as_signed(&self) -> SignedConnection {
SignedConnection {
connection: self.connection.clone(),
signer: KeyPair::new(self.root.signer().clone()),
}
}
}
6 changes: 2 additions & 4 deletions aleph-client/src/pallets/multisig.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use primitives::{Balance, BlockNumber};
use primitives::BlockNumber;

use crate::{
api, api::runtime_types, sp_weights::weight_v2::Weight, AccountId, BlockHash,
SignedConnectionApi, TxStatus,
api, sp_weights::weight_v2::Weight, AccountId, BlockHash, SignedConnectionApi, TxStatus,
};

/// An alias for a call hash.
Expand All @@ -11,7 +10,6 @@ pub type CallHash = [u8; 32];
pub type Call = Vec<u8>;
/// An alias for a timepoint.
pub type Timepoint = api::runtime_types::pallet_multisig::Timepoint<BlockNumber>;
pub type Multisig = runtime_types::pallet_multisig::Multisig<BlockNumber, Balance, AccountId>;

/// Pallet multisig api.
#[async_trait::async_trait]
Expand Down
4 changes: 4 additions & 0 deletions aleph-client/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ pub trait BlocksApi {
async fn get_finalized_block_hash(&self) -> anyhow::Result<BlockHash>;

/// Returns number of a given block hash, if the given block exists, otherwise `None`
/// This is version that returns `Result`
/// * `block` - hash of the block to query its number
async fn get_block_number(&self, block: BlockHash) -> anyhow::Result<Option<BlockNumber>>;

/// Returns number of a given block hash, if the given block exists, otherwise `None`
/// * `block` - hash of the block to query its number
async fn get_block_number_opt(
&self,
block: Option<BlockHash>,
Expand Down