From d27533d328bd126d80c84af345c4fd70a6174dfd Mon Sep 17 00:00:00 2001 From: 6r1d Date: Tue, 12 Sep 2023 08:30:34 +0300 Subject: [PATCH] [documentation]: #6 Move the Rust examples to this repository Signed-off-by: 6r1d --- Rust/Cargo.toml | 7 ++ Rust/examples/million_accounts_genesis.rs | 87 +++++++++++++++++++++++ Rust/src/main.rs | 3 + 3 files changed, 97 insertions(+) create mode 100644 Rust/Cargo.toml create mode 100644 Rust/examples/million_accounts_genesis.rs create mode 100644 Rust/src/main.rs diff --git a/Rust/Cargo.toml b/Rust/Cargo.toml new file mode 100644 index 0000000..3c9082c --- /dev/null +++ b/Rust/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "Iroha 2 examples" +version = "0.1.0" +edition = "2023" + +[dependencies] +iroha_client = { "git" = "https://github.com/hyperledger/iroha.git", branch = "iroha2-dev" } diff --git a/Rust/examples/million_accounts_genesis.rs b/Rust/examples/million_accounts_genesis.rs new file mode 100644 index 0000000..f2452b2 --- /dev/null +++ b/Rust/examples/million_accounts_genesis.rs @@ -0,0 +1,87 @@ +#![allow(missing_docs, clippy::pedantic, clippy::restriction)] + +use std::{thread, time::Duration}; + +use iroha::samples::{construct_validator, get_config}; +use iroha_data_model::prelude::*; +use iroha_genesis::{GenesisNetwork, RawGenesisBlock, RawGenesisBlockBuilder}; +use test_network::{ + get_key_pair, wait_for_genesis_committed, Peer as TestPeer, PeerBuilder, TestRuntime, +}; +use tokio::runtime::Runtime; + +fn generate_genesis(num_domains: u32) -> RawGenesisBlock { + let mut builder = RawGenesisBlockBuilder::new(); + + let key_pair = get_key_pair(); + for i in 0_u32..num_domains { + builder = builder + .domain(format!("wonderland-{i}").parse().expect("Valid")) + .account( + format!("Alice-{i}").parse().expect("Valid"), + key_pair.public_key().clone(), + ) + .asset( + format!("xor-{i}").parse().expect("Valid"), + AssetValueType::Quantity, + ) + .finish_domain(); + } + + builder + .validator( + construct_validator("../default_validator").expect("Failed to construct validator"), + ) + .build() +} + +fn main_genesis() { + let mut peer = ::new().expect("Failed to create peer"); + let configuration = get_config( + std::iter::once(peer.id.clone()).collect(), + Some(get_key_pair()), + ); + let rt = Runtime::test(); + let genesis = GenesisNetwork::from_configuration( + generate_genesis(1_000_000_u32), + Some(&configuration.genesis), + ) + .expect("genesis creation failed"); + + let builder = PeerBuilder::new() + .with_into_genesis(genesis) + .with_configuration(configuration); + + // This only submits the genesis. It doesn't check if the accounts + // are created, because that check is 1) not needed for what the + // test is actually for, 2) incredibly slow, making this sort of + // test impractical, 3) very likely to overflow memory on systems + // with less than 16GiB of free memory. + rt.block_on(builder.start_with_peer(&mut peer)); +} + +fn create_million_accounts_directly() { + let (_rt, _peer, test_client) = ::new().start_with_runtime(); + wait_for_genesis_committed(&vec![test_client.clone()], 0); + for i in 0_u32..1_000_000_u32 { + let domain_id: DomainId = format!("wonderland-{i}").parse().expect("Valid"); + let normal_account_id = AccountId::new( + format!("bob-{i}").parse().expect("Valid"), + domain_id.clone(), + ); + let create_domain = RegisterBox::new(Domain::new(domain_id)); + let create_account = RegisterBox::new(Account::new(normal_account_id.clone(), [])); + if test_client + .submit_all([create_domain, create_account]) + .is_err() + { + thread::sleep(Duration::from_millis(100)); + } + } + thread::sleep(Duration::from_secs(1000)); +} + +fn main() { + create_million_accounts_directly(); + main_genesis(); +} diff --git a/Rust/src/main.rs b/Rust/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/Rust/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}