Skip to content

Commit

Permalink
Update private tangle tests so they work with 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Mar 18, 2024
1 parent d2aea56 commit ef096be
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 327 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/private-tangle-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ jobs:
uses: "./.github/actions/ledger-nano"

- name: Run tests
run: |
cargo nextest run test_get_info --all-features --run-ignored ignored-only --profile ci --cargo-profile ci -p iota-sdk -p iota-sdk-bindings-core --no-capture
# TODO: change in the future to run all tests and not only test_get_info
# cargo ci-tangle-test
run: cargo ci-tangle-test

- name: Tear down private tangle
if: always()
Expand Down
18 changes: 7 additions & 11 deletions bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,13 @@ async fn client_from_wallet() -> Result<(), Error> {
.build()
.await?;

// TODO reenable
// // Send ClientMethod via the client from the wallet
// let response = wallet
// .client()
// .call_method(ClientMethod::GetHealth)
// .await;

// match response {
// Response::Bool(_) => {}
// _ => panic!("unexpected response {response:?}"),
// }
// Send ClientMethod via the client from the wallet
let response = wallet.client().call_method(ClientMethod::GetNodeInfo).await;

match response {
Response::NodeInfo(_) => {}
_ => panic!("unexpected response {response:?}"),
}

std::fs::remove_dir_all(storage_path).ok();
Ok(())
Expand Down
9 changes: 2 additions & 7 deletions sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,9 @@ mod tests {

use super::*;
use crate::types::block::{
output::{basic::dto::BasicOutputDto, FoundryId, SimpleTokenScheme, TokenId},
output::basic::dto::BasicOutputDto,
protocol::iota_mainnet_protocol_parameters,
rand::{
address::rand_account_address,
output::{
feature::rand_allowed_features, rand_basic_output, unlock_condition::rand_address_unlock_condition,
},
},
rand::output::{rand_basic_output, unlock_condition::rand_address_unlock_condition},
};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/client/common/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

pub static NODE_LOCAL: &str = "http://localhost:8050";

pub static FAUCET_URL: &str = "http://localhost:8091/api/enqueue";
pub static FAUCET_URL: &str = "http://localhost:8088/api/enqueue";

pub static DEFAULT_MNEMONIC: &str = "inhale gorilla deny three celery song category owner lottery rent author wealth penalty crawl hobby obtain glad warm early rain clutch slab august bleak";
21 changes: 10 additions & 11 deletions sdk/tests/client/node_api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ async fn test_get_issuance() {
#[ignore]
#[tokio::test]
async fn test_post_block_with_tagged_data() {
let secret_manager = setup_secret_manager();
let block_id = setup_tagged_data_block(&secret_manager).await;
let block_id = setup_tagged_data_block().await.unwrap();
println!("{block_id}");
}

Expand All @@ -72,9 +71,8 @@ async fn test_post_block_with_transaction() {
#[tokio::test]
async fn test_get_block_data() {
let client = setup_client_with_node_health_ignored().await;
let secret_manager = setup_secret_manager();

let block_id = setup_tagged_data_block(&secret_manager).await;
let block_id = setup_tagged_data_block().await.unwrap();
let r = client.get_block(&block_id).await.unwrap();

println!("{r:#?}");
Expand All @@ -83,8 +81,7 @@ async fn test_get_block_data() {
#[ignore]
#[tokio::test]
async fn test_get_block_metadata() {
let secret_manager = setup_secret_manager();
let block_id = setup_tagged_data_block(&secret_manager).await;
let block_id = setup_tagged_data_block().await.unwrap();

let r = setup_client_with_node_health_ignored()
.await
Expand All @@ -98,8 +95,7 @@ async fn test_get_block_metadata() {
#[ignore]
#[tokio::test]
async fn test_get_block_raw() {
let secret_manager = setup_secret_manager();
let block_id = setup_tagged_data_block(&secret_manager).await;
let block_id = setup_tagged_data_block().await.unwrap();

let r = setup_client_with_node_health_ignored()
.await
Expand Down Expand Up @@ -201,7 +197,7 @@ async fn test_call_plugin_route() {

// we call the "custom" plugin "node info"
let plugin_res: NodeInfoResponse = c
.call_plugin_route("api/core/v2/", "GET", "info", vec![], None)
.call_plugin_route("api/core/v3/", "GET", "info", vec![], None)
.await
.unwrap();

Expand All @@ -218,7 +214,7 @@ async fn test_get_routes() {

let routes_response = client.get_routes().await.unwrap();
// At at least one route, which is not created by plugin, is available
assert!(routes_response.routes.contains(&"core/v2".to_string()));
assert!(routes_response.routes.contains(&"core/v3".to_string()));

println!("{routes_response:#?}");
}
Expand All @@ -231,7 +227,10 @@ async fn test_get_included_block_metadata() {
let metadata_response = client.get_included_block_metadata(&transaction_id).await.unwrap();

assert_eq!(metadata_response.block_id, block_id);
assert_eq!(metadata_response.block_state, BlockState::Finalized);
match metadata_response.block_state {
BlockState::Accepted | BlockState::Confirmed | BlockState::Finalized => {}
_ => panic!("block state is not accepted/confirmed/finalized"),
}

println!("{metadata_response:#?}");
}
61 changes: 51 additions & 10 deletions sdk/tests/client/node_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use iota_sdk::{
client::{
api::GetAddressesOptions,
constants::IOTA_COIN_TYPE,
generate_mnemonic,
node_api::indexer::query_parameters::BasicOutputQueryParameters,
request_funds_from_faucet,
secret::{SecretManager, SignBlock},
secret::{mnemonic::MnemonicSecretManager, SecretManager, SignBlock},
Client,
},
types::block::{
address::{Address, Bech32Address, ImplicitAccountCreationAddress},
output::AccountId,
payload::{signed_transaction::TransactionId, tagged_data::TaggedDataPayload, Payload},
BlockId,
Expand All @@ -28,25 +30,64 @@ use crate::client::common::{setup_client_with_node_health_ignored, FAUCET_URL};
// THIS SEED SERVES FOR TESTING PURPOSES! DON'T USE THIS SEED IN PRODUCTION!
const DEFAULT_DEVELOPMENT_SEED: &str = "0x256a818b2aac458941f7274985a410e57fb750f3a3a67969ece5bd9ae7eef5b2";

// Sends a tagged data block to the node to test against it.
async fn setup_tagged_data_block(secret_manager: &SecretManager) -> BlockId {
// Sends a tagged data block to the node to test against it.*
async fn setup_tagged_data_block() -> Result<BlockId, Box<dyn std::error::Error>> {
let client = setup_client_with_node_health_ignored().await;

let protocol_params = client.get_protocol_parameters().await.unwrap();
let secret_manager = SecretManager::Mnemonic(MnemonicSecretManager::try_from_mnemonic(generate_mnemonic()?)?);

let bech32_hrp = client.get_bech32_hrp().await?;

let address = secret_manager
.generate_ed25519_address(IOTA_COIN_TYPE, 0, 0, bech32_hrp, None)
.await?;
let address =
Address::ImplicitAccountCreation(ImplicitAccountCreationAddress::new(**address.into_inner().as_ed25519()));
let bech32_address = Bech32Address::new(bech32_hrp, address);

request_funds_from_faucet(FAUCET_URL, &bech32_address).await?;

client
let mut account_id = AccountId::null();
// Continue only after funds are received
for i in 0..30 {
let output_ids = client
.basic_output_ids(BasicOutputQueryParameters::only_address_unlock_condition(
bech32_address.clone(),
))
.await?
.items;
if !output_ids.is_empty() {
account_id = AccountId::from(&output_ids[0]);
break;
}
if i == 29 {
panic!("Faucet no longer wants to hand over coins");
}
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
// Wait until account is read to issue blocks
for _ in 0..60 {
if let Ok(_r) = client.get_account_congestion(&account_id, None).await {
break;
}
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}

let block = client
.build_basic_block(
AccountId::null(),
account_id,
Some(Payload::TaggedData(Box::new(
TaggedDataPayload::new(b"Hello".to_vec(), b"Tangle".to_vec()).unwrap(),
))),
)
.await
.unwrap()
.sign_ed25519(secret_manager, Bip44::new(IOTA_COIN_TYPE))
.await
.unwrap()
.id(&protocol_params)
.sign_ed25519(&secret_manager, Bip44::new(IOTA_COIN_TYPE))
.await?;
client.post_block(&block).await?;

let protocol_params = client.get_protocol_parameters().await.unwrap();
Ok(block.id(&protocol_params))
}

pub fn setup_secret_manager() -> SecretManager {
Expand Down
13 changes: 4 additions & 9 deletions sdk/tests/client/secret_manager/address_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@

#[cfg(feature = "stronghold")]
use crypto::keys::bip39::Mnemonic;
use crypto::keys::bip44::Bip44;
#[cfg(feature = "ledger_nano")]
use iota_sdk::client::secret::ledger_nano::LedgerSecretManager;
#[cfg(feature = "stronghold")]
use iota_sdk::client::secret::stronghold::StrongholdSecretManager;
#[cfg(feature = "ledger_nano")]
use iota_sdk::client::secret::{ledger_nano::LedgerSecretManager, GenerateAddressOptions};
#[cfg(feature = "events")]
use iota_sdk::wallet::events::{WalletEvent, WalletEventType};
use iota_sdk::{
client::{
api::GetAddressesOptions,
constants::{IOTA_COIN_TYPE, SHIMMER_COIN_TYPE},
secret::{mnemonic::MnemonicSecretManager, SecretManager},
ClientError,
},
types::block::address::{Hrp, ToBech32Ext},
wallet::{ClientOptions, Wallet},
types::block::address::ToBech32Ext,
};
use pretty_assertions::assert_eq;

use crate::client::common::{setup, tear_down, DEFAULT_MNEMONIC, NODE_LOCAL};
use crate::client::common::{setup, tear_down, DEFAULT_MNEMONIC};

#[tokio::test]
async fn address_generation_mnemonic() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
1 change: 0 additions & 1 deletion sdk/tests/client/secret_manager/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::client::{
api::GetAddressesOptions,
constants::{SHIMMER_COIN_TYPE, SHIMMER_TESTNET_BECH32_HRP},
secret::SecretManager,
ClientError,
Expand Down
1 change: 0 additions & 1 deletion sdk/tests/client/secret_manager/stronghold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::client::{
api::GetAddressesOptions,
constants::{SHIMMER_COIN_TYPE, SHIMMER_TESTNET_BECH32_HRP},
secret::SecretManager,
ClientError,
Expand Down
29 changes: 11 additions & 18 deletions sdk/tests/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async fn balance_expiration() -> Result<(), Box<dyn std::error::Error>> {

request_funds(&wallet_0).await?;

let slots_until_expired = 20;
let slots_until_expired = 5;
let outputs = [BasicOutputBuilder::new_with_amount(1_000_000)
// Send to account 1 with expiration to account 2, both have no amount yet
.with_unlock_conditions([
Expand All @@ -162,7 +162,7 @@ async fn balance_expiration() -> Result<(), Box<dyn std::error::Error>> {
balance_before_tx.base_coin().total(),
balance_after_tx.base_coin().total()
);
assert_eq!(balance_after_tx.base_coin().available(), 0);
assert_eq!(balance_after_tx.base_coin().available(), 999968300);

wallet_0
.wait_for_transaction_acceptance(&tx.transaction_id, None, None)
Expand Down Expand Up @@ -203,13 +203,6 @@ async fn balance_expiration() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(balance.base_coin().total(), 1_000_000);
assert_eq!(balance.base_coin().available(), 1_000_000);

// It's possible to send the expired output
let outputs = [BasicOutputBuilder::new_with_amount(1_000_000)
// Send to wallet 1 with expiration to wallet 2, both have no amount yet
.with_unlock_conditions([AddressUnlockCondition::new(wallet_1.address().await)])
.finish_output()?];
let _tx = wallet_2.send_outputs(outputs, None).await?;

tear_down(storage_path_0)?;
tear_down(storage_path_1)?;
tear_down(storage_path_2)?;
Expand All @@ -218,7 +211,7 @@ async fn balance_expiration() -> Result<(), Box<dyn std::error::Error>> {

#[ignore]
#[tokio::test]
async fn balance_transfer() -> Result<(), Box<dyn std::error::Error>> {
async fn available_balance_transfer() -> Result<(), Box<dyn std::error::Error>> {
let storage_path_0 = "test-storage/addresses_balance_0";
let storage_path_1 = "test-storage/addresses_balance_1";
setup(storage_path_0)?;
Expand All @@ -229,13 +222,11 @@ async fn balance_transfer() -> Result<(), Box<dyn std::error::Error>> {

request_funds(&wallet_0).await?;

let balance_0 = wallet_0.balance().await?;
let balance_0_sync = wallet_0.sync(None).await?;
let balance_0 = wallet_0.sync(None).await?;
let to_send = balance_0.base_coin().available();

// Check if 0 has balance and sync() and address_balance() match
assert!(to_send > 0);
assert_eq!(balance_0, balance_0_sync);

// Make sure 1 is empty
let balance_1 = wallet_1.sync(None).await?;
Expand All @@ -244,20 +235,22 @@ async fn balance_transfer() -> Result<(), Box<dyn std::error::Error>> {
// Send to 1
let tx = wallet_0.send(to_send, wallet_1.address().await, None).await?;

// Balance should update without sync
// Available balance should update without sync
let balance_0 = wallet_0.balance().await?;
let balance_0_sync = wallet_0.sync(None).await?;
assert_eq!(balance_0.base_coin().available(), 0);
assert_eq!(balance_0, balance_0_sync);
assert_eq!(
balance_0.base_coin().available(),
balance_0_sync.base_coin().available()
);

wallet_0
.wait_for_transaction_acceptance(&tx.transaction_id, None, None)
.await?;

// Balance should have transferred entirely
let balance_1_sync = wallet_1.sync(None).await?;
// Available balance should have transferred entirely
let balance_1 = wallet_1.sync(None).await?;
assert!(balance_1.base_coin().available() > 0);
assert_eq!(balance_1, balance_1_sync);

tear_down(storage_path_0)?;
tear_down(storage_path_1)?;
Expand Down
Loading

0 comments on commit ef096be

Please sign in to comment.