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
wip
  • Loading branch information
krzysztofziobro committed Jan 2, 2023
commit e45f3614f933cc80da825524443896e8f9edfb7c
156 changes: 82 additions & 74 deletions aleph-client/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@ use subxt::{
SubstrateConfig,
};

use crate::{api, sp_weights::weight_v2::Weight, BlockHash, Call, KeyPair, SubxtClient, TxStatus};
use crate::{
api, sp_weights::weight_v2::Weight, AccountId, BlockHash, Call, KeyPair, SubxtClient, TxStatus,
};

const DEFAULT_RETRIES: u32 = 10;
const RETRY_WAIT_SECS: u64 = 1;
#[derive(Clone)]
pub struct Connection {
client: SubxtClient,
}

pub async fn create_connection(address: &str) -> Connection {
create_connection_with_retries(address, DEFAULT_RETRIES).await
pub struct SignedConnection {
connection: Connection,
signer: KeyPair,
}

async fn create_connection_with_retries(address: &str, mut retries: u32) -> Connection {
loop {
let client = SubxtClient::from_url(&address).await;
match (retries, client) {
(_, Ok(client)) => return Connection { client },
(0, Err(e)) => panic!("{:?}", e),
_ => {
sleep(Duration::from_secs(RETRY_WAIT_SECS));
retries -= 1;
}
}
}
#[derive(Clone)]
pub struct RootConnection {
connection: SignedConnection,
}

pub trait AsConnection: Sync {
Expand All @@ -44,7 +40,7 @@ pub trait AsSigned: Sync {
}

#[async_trait::async_trait]
pub trait ConnectionExt: AsConnection {
pub trait ConnectionApi: AsConnection {
async fn get_storage_entry<T: DecodeWithMetadata + Sync, Defaultable: Sync, Iterable: Sync>(
&self,
addrs: &StaticStorageAddress<T, Yes, Defaultable, Iterable>,
Expand All @@ -64,19 +60,22 @@ pub trait ConnectionExt: AsConnection {
async fn rpc_call<R: Decode>(&self, func_name: String, params: RpcParams) -> anyhow::Result<R>;
}

#[derive(Clone)]
pub struct Connection {
client: SubxtClient,
}
#[async_trait::async_trait]
pub trait SignedConnectionApi: AsSigned {
async fn send_tx<Call: TxPayload + Send + Sync>(
&self,
tx: Call,
status: TxStatus,
) -> anyhow::Result<BlockHash>;

pub struct SignedConnection {
connection: Connection,
signer: KeyPair,
}
async fn send_tx_with_params<Call: TxPayload + Send + Sync>(
&self,
tx: Call,
params: BaseExtrinsicParamsBuilder<SubstrateConfig, PlainTip>,
status: TxStatus,
) -> anyhow::Result<BlockHash>;

#[derive(Clone)]
pub struct RootConnection {
connection: SignedConnection,
fn account_id(&self) -> &AccountId;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -129,27 +128,9 @@ impl AsConnection for &Connection {
}
}

impl AsConnection for SignedConnection {
fn as_connection(&self) -> &Connection {
&self.connection
}
}

impl AsConnection for &SignedConnection {
fn as_connection(&self) -> &Connection {
&self.connection
}
}

impl AsConnection for RootConnection {
fn as_connection(&self) -> &Connection {
self.connection.as_connection()
}
}

impl AsConnection for &RootConnection {
impl<S: AsSigned> AsConnection for S {
fn as_connection(&self) -> &Connection {
self.connection.as_connection()
&self.as_signed().connection
}
}

Expand Down Expand Up @@ -178,7 +159,7 @@ impl AsSigned for &RootConnection {
}

#[async_trait::async_trait]
impl<C: AsConnection> ConnectionExt for C {
impl<C: AsConnection> ConnectionApi for C {
async fn get_storage_entry<T: DecodeWithMetadata + Sync, Defaultable: Sync, Iterable: Sync>(
&self,
addrs: &StaticStorageAddress<T, Yes, Defaultable, Iterable>,
Expand Down Expand Up @@ -220,26 +201,9 @@ impl<C: AsConnection> ConnectionExt for C {
}
}

impl Connection {
pub(crate) fn as_client(&self) -> &SubxtClient {
&self.client
}
}

impl SignedConnection {
pub async fn new(address: &str, signer: KeyPair) -> Self {
Self::from_connection(create_connection(address).await, signer)
}

pub fn from_connection(connection: Connection, signer: KeyPair) -> Self {
Self { connection, signer }
}

pub fn account_id(&self) -> &subxt::ext::sp_runtime::AccountId32 {
self.signer.account_id()
}

pub async fn send_tx<Call: TxPayload>(
#[async_trait::async_trait]
impl<S: AsSigned> SignedConnectionApi for S {
async fn send_tx<Call: TxPayload + Send + Sync>(
&self,
tx: Call,
status: TxStatus,
Expand All @@ -248,7 +212,7 @@ impl SignedConnection {
.await
}

pub async fn send_tx_with_params<Call: TxPayload>(
async fn send_tx_with_params<Call: TxPayload + Send + Sync>(
&self,
tx: Call,
params: BaseExtrinsicParamsBuilder<SubstrateConfig, PlainTip>,
Expand All @@ -259,11 +223,10 @@ impl SignedConnection {
}

let progress = self
.connection
.as_connection()
.as_client()
.tx()
.sign_and_submit_then_watch(&tx, &self.signer, params)
.sign_and_submit_then_watch(&tx, self.as_signed().signer(), params)
.await
.map_err(|e| anyhow!("Failed to submit transaction: {:?}", e))?;

Expand All @@ -278,6 +241,51 @@ impl SignedConnection {
Ok(hash)
}

fn account_id(&self) -> &AccountId {
self.as_signed().signer().account_id()
}
}

impl Connection {
const DEFAULT_RETRIES: u32 = 10;
const RETRY_WAIT_SECS: u64 = 1;

pub async fn new(address: &str) -> Connection {
Self::new_with_retries(address, Self::DEFAULT_RETRIES).await
}

async fn new_with_retries(address: &str, mut retries: u32) -> Connection {
loop {
let client = SubxtClient::from_url(&address).await;
match (retries, client) {
(_, Ok(client)) => return Connection { client },
(0, Err(e)) => panic!("{:?}", e),
_ => {
sleep(Duration::from_secs(Self::RETRY_WAIT_SECS));
retries -= 1;
}
}
}
}

pub(crate) fn as_client(&self) -> &SubxtClient {
&self.client
}
}

impl SignedConnection {
pub async fn new(address: &str, signer: KeyPair) -> Self {
Self::from_connection(Connection::new(address).await, signer)
}

pub fn from_connection(connection: Connection, signer: KeyPair) -> Self {
Self { connection, signer }
}

pub fn signer(&self) -> &KeyPair {
&self.signer
}

pub async fn try_as_root(&self) -> anyhow::Result<RootConnection> {
let temp = self.clone();
RootConnection::try_from_connection(temp.connection, temp.signer).await
Expand All @@ -286,7 +294,7 @@ impl SignedConnection {

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

pub async fn try_from_connection(
Expand Down
31 changes: 18 additions & 13 deletions aleph-client/src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use crate::{
contract_transcode::Value,
pallets::contract::{ContractCallArgs, ContractRpc, ContractsUserApi},
sp_weights::weight_v2::Weight,
AccountId, ConnectionExt, SignedConnection, TxStatus,
AccountId, ConnectionApi, SignedConnectionApi, TxStatus,
};

/// Represents a contract instantiated on the chain.
Expand Down Expand Up @@ -82,7 +82,7 @@ impl ContractInstance {
/// Reads the value of a read-only, 0-argument call via RPC.
pub async fn contract_read0<
T: TryFrom<ConvertibleValue, Error = anyhow::Error>,
C: ConnectionExt,
C: ConnectionApi,
>(
&self,
conn: &C,
Expand All @@ -95,7 +95,7 @@ impl ContractInstance {
pub async fn contract_read<
S: AsRef<str> + Debug,
T: TryFrom<ConvertibleValue, Error = anyhow::Error>,
C: ConnectionExt,
C: ConnectionApi,
>(
&self,
conn: &C,
Expand Down Expand Up @@ -123,35 +123,40 @@ impl ContractInstance {
}

/// Executes a 0-argument contract call.
pub async fn contract_exec0(&self, conn: &SignedConnection, message: &str) -> Result<()> {
self.contract_exec::<String>(conn, message, &[]).await
pub async fn contract_exec0<C: SignedConnectionApi>(
&self,
conn: &C,
message: &str,
) -> Result<()> {
self.contract_exec::<C, String>(conn, message, &[]).await
}

/// Executes a contract call.
pub async fn contract_exec<S: AsRef<str> + Debug>(
pub async fn contract_exec<C: SignedConnectionApi, S: AsRef<str> + Debug>(
&self,
conn: &SignedConnection,
conn: &C,
message: &str,
args: &[S],
) -> Result<()> {
self.contract_exec_value(conn, message, args, 0).await
self.contract_exec_value::<C, S>(conn, message, args, 0)
.await
}

/// Executes a 0-argument contract call sending the given amount of value with it.
pub async fn contract_exec_value0(
pub async fn contract_exec_value0<C: SignedConnectionApi>(
&self,
conn: &SignedConnection,
conn: &C,
message: &str,
value: u128,
) -> Result<()> {
self.contract_exec_value::<String>(conn, message, &[], value)
self.contract_exec_value::<C, String>(conn, message, &[], value)
.await
}

/// Executes a contract call sending the given amount of value with it.
pub async fn contract_exec_value<S: AsRef<str> + Debug>(
pub async fn contract_exec_value<C: SignedConnectionApi, S: AsRef<str> + Debug>(
&self,
conn: &SignedConnection,
conn: &C,
message: &str,
args: &[S],
value: u128,
Expand Down
4 changes: 2 additions & 2 deletions aleph-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub type BlockHash = H256;
pub(crate) type SubxtClient = OnlineClient<AlephConfig>;

pub use connections::{
create_connection, AsConnection, AsSigned, Connection, ConnectionExt, RootConnection,
SignedConnection, SudoCall,
AsConnection, Connection, ConnectionApi, RootConnection, SignedConnection, SignedConnectionApi,
SudoCall,
};

#[derive(Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions aleph-client/src/pallets/aleph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
pallet_aleph::pallet::Call::schedule_finality_version_change,
AccountId, AlephKeyPair, BlockHash,
Call::Aleph,
ConnectionExt, Pair, RootConnection, SudoCall, TxStatus,
ConnectionApi, Pair, RootConnection, SudoCall, TxStatus,
};

#[async_trait::async_trait]
Expand Down Expand Up @@ -68,7 +68,7 @@ impl AlephSudoApi for RootConnection {
}

#[async_trait::async_trait]
impl<C: ConnectionExt> AlephRpc for C {
impl<C: ConnectionApi> AlephRpc for C {
async fn emergency_finalize(
&self,
number: BlockNumber,
Expand Down
4 changes: 2 additions & 2 deletions aleph-client/src/pallets/author.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use codec::Decode;

use crate::{aleph_runtime::SessionKeys, ConnectionExt};
use crate::{aleph_runtime::SessionKeys, ConnectionApi};

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

#[async_trait::async_trait]
impl<C: ConnectionExt> AuthorRpc for C {
impl<C: ConnectionApi> AuthorRpc for C {
async fn author_rotate_keys(&self) -> anyhow::Result<SessionKeys> {
let bytes = self.as_connection().as_client().rpc().rotate_keys().await?;
SessionKeys::decode(&mut bytes.0.as_slice()).map_err(|e| e.into())
Expand Down
Loading