-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[documentation]: #6 Move the Rust examples to this repository
Signed-off-by: 6r1d <[email protected]>
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "iroha_2_examples" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
iroha_data_model = { "git" = "https://github.com/hyperledger/iroha.git", branch = "iroha2-dev" } | ||
iroha_client = { "git" = "https://github.com/hyperledger/iroha.git", branch = "iroha2-dev" } | ||
iroha_config = { "git" = "https://github.com/hyperledger/iroha.git", branch = "iroha2-dev" } | ||
|
||
eyre = "0.6.8" | ||
|
||
serde = { version = "1.0.151", default-features = false } | ||
serde_json = { version = "1.0.91", default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use eyre::{Error}; | ||
|
||
fn main() { | ||
account_definition_test() | ||
.expect("Account definition example is expected to work correctly"); | ||
|
||
println!("Account definition example works!"); | ||
} | ||
|
||
fn account_definition_test() -> Result<(), Error> { | ||
// #region account_definition_comparison | ||
use iroha_data_model::prelude::AccountId; | ||
|
||
// Create an `iroha_data_model::AccountId` instance | ||
// with a DomainId instance and a Domain ID for an account | ||
let longhand_account_id = AccountId::new("white_rabbit".parse()?, "looking_glass".parse()?); | ||
let account_id: AccountId = "white_rabbit@looking_glass" | ||
.parse() | ||
.expect("Valid, because the string contains no whitespace, has a single '@' character and is not empty after"); | ||
|
||
// Check that two ways to define an account match | ||
assert_eq!(account_id, longhand_account_id); | ||
|
||
// #endregion account_definition_comparison | ||
|
||
// Finish the test successfully | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::fs::File; | ||
use eyre::{Error, WrapErr}; | ||
use iroha_config::client::Configuration; | ||
|
||
fn main() { | ||
// #region rust_config_load | ||
let config_loc = "../configs/client/config.json"; | ||
let file = File::open(config_loc) | ||
.wrap_err("Unable to load the configuration file at `.....`") | ||
.expect("Config file is loading normally."); | ||
let config: Configuration = serde_json::from_reader(file) | ||
.wrap_err("Failed to parse `../configs/client/config.json`") | ||
.expect("Verified in tests"); | ||
// #endregion rust_config_load | ||
|
||
domain_registration_test(&config) | ||
.expect("Domain registration example is expected to work correctly"); | ||
|
||
println!("Domain registration example works!"); | ||
} | ||
|
||
fn domain_registration_test(config: &Configuration) -> Result<(), Error> { | ||
// #region domain_register_example_crates | ||
use iroha_client::client::Client; | ||
use iroha_data_model::{ | ||
metadata::UnlimitedMetadata, | ||
prelude::{Domain, DomainId, InstructionBox, RegisterBox}, | ||
}; | ||
// #endregion domain_register_example_crates | ||
|
||
// #region domain_register_example_create_domain | ||
// Create a domain Id | ||
let looking_glass: DomainId = "looking_glass".parse()?; | ||
// #endregion domain_register_example_create_domain | ||
|
||
// #region domain_register_example_create_isi | ||
// Create an ISI | ||
let create_looking_glass = RegisterBox::new(Domain::new(looking_glass)); | ||
// #endregion domain_register_example_create_isi | ||
|
||
// #region rust_client_create | ||
// Create an Iroha client | ||
let iroha_client: Client = Client::new(&config)?; | ||
// #endregion rust_client_create | ||
|
||
// #region domain_register_example_prepare_tx | ||
// Prepare a transaction | ||
let metadata = UnlimitedMetadata::default(); | ||
let instructions: Vec<InstructionBox> = vec![create_looking_glass.into()]; | ||
let tx = iroha_client | ||
.build_transaction(instructions, metadata) | ||
.wrap_err("Error building a domain registration transaction")?; | ||
// #endregion domain_register_example_prepare_tx | ||
|
||
// #region domain_register_example_submit_tx | ||
// Submit a prepared domain registration transaction | ||
iroha_client | ||
.submit_transaction(&tx) | ||
.wrap_err("Failed to submit transaction")?; | ||
// #endregion domain_register_example_submit_tx | ||
|
||
// Finish the test successfully | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::fs::File; | ||
use eyre::{Error, WrapErr}; | ||
use iroha_config::client::Configuration; | ||
|
||
fn main() { | ||
// #region rust_config_load | ||
let config_loc = "../configs/client/config.json"; | ||
let file = File::open(config_loc) | ||
.wrap_err("Unable to load the configuration file at `.....`") | ||
.expect("Config file is loading normally."); | ||
let config: Configuration = serde_json::from_reader(file) | ||
.wrap_err("Failed to parse `../configs/client/config.json`") | ||
.expect("Verified in tests"); | ||
// #endregion rust_config_load | ||
|
||
json_config_client_test(&config) | ||
.expect("JSON config client example is expected to work correctly"); | ||
|
||
println!("JSON client configuration test passed successfully!"); | ||
} | ||
|
||
fn json_config_client_test(config: &Configuration) -> Result<(), Error> { | ||
use iroha_client::client::Client; | ||
|
||
// Initialise a client with a provided config | ||
let _current_client: Client = Client::new(&config)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |