Skip to content

Commit

Permalink
Restructure the client A BIT to reduce Arc usage (#394)
Browse files Browse the repository at this point in the history
* Clean up the client and reduce Arc usage

* little bit more

* Move sync handle up

* Hopefully fix wasm client

* Try to fix mqtt drop

* Just remove the drop impl

* changelog

* Update sdk/CHANGELOG.md

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Alexandcoats and thibault-martinez authored May 7, 2023
1 parent c53b9a1 commit c72b659
Show file tree
Hide file tree
Showing 99 changed files with 466 additions and 387 deletions.
2 changes: 1 addition & 1 deletion bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn generate_addresses() -> Result<()> {
}"#
.to_string();

let client = ClientBuilder::new().from_json(&client_config)?.finish()?;
let client = ClientBuilder::new().from_json(&client_config)?.finish().await?;

let secret_manager = format!(
"{{\"mnemonic\":\"{}\"}}",
Expand Down
12 changes: 8 additions & 4 deletions bindings/nodejs/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ impl Finalize for ClientMethodHandler {}

impl ClientMethodHandler {
pub fn new(channel: Channel, options: String) -> Arc<Self> {
let client = ClientBuilder::new()
.from_json(&options)
.expect("error initializing client")
.finish()
let runtime = tokio::runtime::Runtime::new().expect("error initializing client");
let client = runtime
.block_on(
ClientBuilder::new()
.from_json(&options)
.expect("error initializing client")
.finish(),
)
.expect("error initializing client");

Arc::new(Self { channel, client })
Expand Down
11 changes: 7 additions & 4 deletions bindings/python/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ pub struct Client {
/// Create client for python-side usage.
#[pyfunction]
pub fn create_client(options: Option<String>) -> Result<Client> {
let client = match options {
Some(options) => ClientBuilder::new().from_json(&options)?.finish()?,
None => ClientBuilder::new().finish()?,
};
let runtime = tokio::runtime::Runtime::new()?;
let client = runtime.block_on(async move {
Result::Ok(match options {
Some(options) => ClientBuilder::new().from_json(&options)?.finish().await?,
None => ClientBuilder::new().finish().await?,
})
})?;

Ok(Client { client })
}
Expand Down
25 changes: 14 additions & 11 deletions bindings/wasm/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::rc::Rc;

use iota_sdk_bindings_core::{
call_client_method,
iota_sdk::client::{Client, ClientBuilder},
Expand All @@ -16,22 +14,27 @@ use crate::{ArrayString, PromiseString};
/// The Client method handler.
#[wasm_bindgen(js_name = ClientMethodHandler)]
pub struct ClientMethodHandler {
pub(crate) client: Rc<Client>,
pub(crate) client: Client,
}

/// Creates a method handler with the given client options.
#[wasm_bindgen(js_name = createClient)]
#[allow(non_snake_case)]
pub fn create_client(clientOptions: String) -> Result<ClientMethodHandler, JsValue> {
let client = ClientBuilder::new()
.from_json(&clientOptions)
.map_err(|err| err.to_string())?
.finish()
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.map_err(|err| err.to_string())?;

Ok(ClientMethodHandler {
client: Rc::new(client),
})
let client = runtime.block_on(async move {
ClientBuilder::new()
.from_json(&clientOptions)
.map_err(|err| err.to_string())?
.finish()
.await
.map_err(|err| err.to_string())
})?;

Ok(ClientMethodHandler { client })
}

/// Handles a method, returns the response as a JSON-encoded string.
Expand All @@ -40,7 +43,7 @@ pub fn create_client(clientOptions: String) -> Result<ClientMethodHandler, JsVal
#[wasm_bindgen(js_name = callClientMethodAsync)]
#[allow(non_snake_case)]
pub fn call_client_method_async(method: String, methodHandler: &ClientMethodHandler) -> Result<PromiseString, JsValue> {
let client: Rc<Client> = Rc::clone(&methodHandler.client);
let client: Client = methodHandler.client.clone();

let promise: js_sys::Promise = future_to_promise(async move {
let method: ClientMethod = serde_json::from_str(&method).map_err(|err| err.to_string())?;
Expand Down
4 changes: 1 addition & 3 deletions bindings/wasm/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ pub async fn get_client(method_handler: &WalletMethodHandler) -> Result<ClientMe
.block_on(async move { wallet.as_ref().expect("wallet got destroyed").get_client().await })
.map_err(|e| e.to_string())?;

Ok(ClientMethodHandler {
client: Rc::new(client),
})
Ok(ClientMethodHandler { client })
}

/// Handles a method, returns the response as a JSON-encoded string.
Expand Down
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- rename `Client::try_get_outputs()` into `Client::get_outputs_ignore_errors()`;
- rename `Client::try_get_outputs_metadata()` into `Client::get_outputs_metadata_ignore_errors()`;
- MQTT connections to a node using https will now use wss/tls with native certificates;
- `ClientBuilder::finish` is now async;

### Removed

Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/01_generate_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ async fn main() -> Result<()> {
// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;
.finish()
.await?;

let secret_manager =
SecretManager::try_from_mnemonic(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap())?;
Expand Down
5 changes: 3 additions & 2 deletions sdk/examples/client/02_get_address_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! In this example we will get the outputs of an address that have no additional unlock conditions and sum the amounts
//! and native tokens.
//!
//!
//! `cargo run --example 02_get_address_balance --release`
use iota_sdk::client::{
Expand All @@ -23,7 +23,8 @@ async fn main() -> Result<()> {
// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;
.finish()
.await?;

let secret_manager =
MnemonicSecretManager::try_from_mnemonic(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap())?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/07_mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async fn main() -> Result<()> {
let client = Client::builder()
.with_node("https://api.testnet.shimmer.network")?
// .with_mqtt_broker_options(BrokerOptions::new().use_ws(false))
.finish()?;
.finish()
.await?;

let (tx, rx) = channel();
let tx = Arc::new(Mutex::new(tx));
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/00_block_no_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Create and send the block.
let block = client.block().finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/01_block_confirmation_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Create and send a block.
let block = client.block().finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/02_block_custom_parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Use tips as custom parents.
let parents = client.get_tips().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/03_block_custom_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Create a custom payload.
let tagged_data_payload = TaggedDataPayload::new(b"Your tag".to_vec(), b"Your data".to_vec())?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/04_block_tagged_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Create and send the block with tag and data.
let block = client
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/block/custom_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ async fn main() -> Result<()> {
// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;
.finish()
.await?;

// First address from the seed below is atoi1qzt0nhsf38nh6rs4p6zs5knqp6psgha9wsv74uajqgjmwc75ugupx3y7x0r
let secret_manager =
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<()> {
let node_url = std::env::var("NODE_URL").unwrap();
let faucet_url = std::env::var("FAUCET_URL").unwrap();

let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager =
SecretManager::try_from_mnemonic(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap())?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/block/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() -> Result<()> {

let node_url = std::env::var("NODE_URL").unwrap();

let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager =
SecretManager::try_from_mnemonic(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1").unwrap())?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/get_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ async fn main() -> Result<()> {
let client = Client::builder()
.with_node(&node_url)?
.with_pow_worker_count(1)
.finish()?;
.finish()
.await?;

// Fetch a block ID from the node.
let block_id = client.get_tips().await?[0];
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/client/high_level/consolidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

//! In this example we will consolidate all funds in a range of addresses.
//!
//!
//! `cargo run --example consolidation --release`
use iota_sdk::client::{
Expand All @@ -22,7 +22,7 @@ async fn main() -> Result<()> {

let address_range = 0u32..150;
// Create a client instance
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

let secret_manager =
SecretManager::try_from_hex_seed(&std::env::var("NON_SECURE_USE_OF_DEVELOPMENT_SEED_1").unwrap())?;
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/client/high_level/inputs_from_transaction_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

//! In this example we will fetch all inputs from a given transaction id.
//!
//!
//! `cargo run --example inputs_from_transaction_id --release`
use iota_sdk::client::{block::payload::transaction::TransactionId, Client, Result};
Expand All @@ -14,7 +14,7 @@ async fn main() -> Result<()> {

let node_url = std::env::var("NODE_URL").unwrap();

let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

let transaction_id =
"0xaf7579fb57746219561072c2cc0e4d0fbb8d493d075bd21bf25ae81a450c11ef".parse::<TransactionId>()?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/ledger_nano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ async fn main() -> Result<()> {
// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;
.finish()
.await?;

let ledger_nano = LedgerSecretManager::new(false);

Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/ledger_nano_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async fn main() -> Result<()> {
// Create a client instance
let client = Client::builder()
.with_node(&node_url)? // Insert your node URL here
.finish()?;
.finish()
.await?;

let secret_manager = SecretManager::LedgerNano(LedgerSecretManager::new(true));

Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/client/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

//! TODO: Example description
//!
//!
//! `cargo run --example logger --release`
use iota_sdk::client::{Client, Result};
Expand All @@ -27,7 +27,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Get node info.
let info = client.get_info().await?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/node_api_core/00_get_health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async fn main() -> Result<()> {
let client = Client::builder()
.with_node(&node_url)?
.with_ignore_node_health()
.finish()?;
.finish()
.await?;

// Get node health.
let health = client.get_health(&node_url).await?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/node_api_core/01_get_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async fn main() -> Result<()> {
let client = Client::builder()
.with_node(&node_url)?
.with_ignore_node_health()
.finish()?;
.finish()
.await?;

// Get routes.
let routes = client.get_routes().await?;
Expand Down
3 changes: 2 additions & 1 deletion sdk/examples/client/node_api_core/02_get_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ async fn main() -> Result<()> {
let client = Client::builder()
.with_node(&node_url)?
.with_ignore_node_health()
.finish()?;
.finish()
.await?;

// Get node info.
let info = client.get_info().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/03_get_tips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Get tips.
let tips = client.get_tips().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/04_post_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Create the block.
let block = client.block().finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/05_post_block_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Create the block.
let block = client.block().finish().await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/06_get_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Take the block ID from command line argument or...
let block_id = if let Some(Ok(block_id)) = std::env::args().nth(2).map(|s| BlockId::from_str(&s)) {
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/07_get_block_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Take the block ID from command line argument or...
let block_id = if let Some(Ok(block_id)) = std::env::args().nth(2).map(|s| BlockId::from_str(&s)) {
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/node_api_core/08_get_block_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() -> Result<()> {
});

// Create a client with that node.
let client = Client::builder().with_node(&node_url)?.finish()?;
let client = Client::builder().with_node(&node_url)?.finish().await?;

// Fetch a block ID from the node.
let block_id = client.get_tips().await?[0];
Expand Down
Loading

0 comments on commit c72b659

Please sign in to comment.