Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): add missing INX model wrappers #1529

Open
wants to merge 25 commits into
base: shimmer-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ book

# vscode
**/.vscode/

**/.env
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions bee-inx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security -->

## 1.0.0-rc.1 - 2022-09-29

### Added

- Missing INX bindings
- Examples
- Documentation

## 1.0.0-beta.6 - 2022-09-26

### Added
Expand Down
3 changes: 2 additions & 1 deletion bee-inx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bee-inx"
version = "1.0.0-beta.6"
version = "1.0.0-rc.1"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = "Rust bindings for IOTA node extensions (INX)"
Expand All @@ -20,5 +20,6 @@ packable = { version = "0.6.2", default-features = false }

[dev-dependencies]
bee-block = { version = "1.0.0", path = "../bee-block", default-features = false, features = [ "inx", "rand" ] }
dotenvy = { version = "0.15.5", default-features = false }

tokio = { version = "1.20.1", default-features = false, features = [ "macros", "rt-multi-thread" ] }
84 changes: 84 additions & 0 deletions bee-inx/examples/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use bee_block::BlockId;
use bee_inx::{client::Inx, Error};
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Error> {
dotenvy::dotenv().ok();
let inx_connect_url = std::env::var("INX_CONNECT_URL").unwrap_or_else(|_| "http://localhost:9029".to_string());
let block_stream = std::env::var("BLOCK_STREAM").unwrap_or_else(|_| "blocks".to_string());
let read_block = std::env::var("READ_BLOCK")
.unwrap_or_else(|_| "false".to_string())
.parse::<bool>()
.unwrap();
let read_block_metadata = std::env::var("READ_BLOCK_METADATA")
.unwrap_or_else(|_| "false".to_string())
.parse::<bool>()
.unwrap();

let mut inx = Inx::connect(&inx_connect_url).await?;
println!("Connected via INX to node at {inx_connect_url}");

match block_stream.as_str() {
"blocks" => {
let mut block_stream = inx.listen_to_blocks().await?;
println!("Streaming blocks ... ");

while let Some(block) = block_stream.next().await {
let block = block?;
println!("{}", block.block_id);

fetch_block_and_metadata(&mut inx, block.block_id, read_block, read_block_metadata).await?;
}
}
"solid_blocks" => {
let mut block_stream = inx.listen_to_solid_blocks().await?;
println!("Streaming solid blocks ... ");

while let Some(block_metadata) = block_stream.next().await {
let block_metadata = block_metadata?;
println!("{}", block_metadata.block_id);

fetch_block_and_metadata(&mut inx, block_metadata.block_id, read_block, read_block_metadata).await?;
}
}
"referenced_blocks" => {
let mut block_stream = inx.listen_to_referenced_blocks().await?;
println!("Streaming referenced blocks ... ");

while let Some(block_metadata) = block_stream.next().await {
let block_metadata = block_metadata?;
println!("{}", block_metadata.block_id);

fetch_block_and_metadata(&mut inx, block_metadata.block_id, read_block, read_block_metadata).await?;
}
}
_ => {
panic!("unknown block stream variant: '{block_stream}'");
}
}

Ok(())
}

async fn fetch_block_and_metadata(
inx: &mut Inx,
block_id: BlockId,
read_block: bool,
read_block_metadata: bool,
) -> Result<(), Error> {
if read_block {
let raw_block = inx.read_block(block_id).await?;
println!("{:?}", raw_block.inner_unverified());
}

if read_block_metadata {
let block_metadata = inx.read_block_metadata(block_id).await?;
println!("{:?}", block_metadata);
}

Ok(())
}
89 changes: 76 additions & 13 deletions bee-inx/examples/milestone.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,96 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use bee_inx::{client, Error};
use bee_block::payload::milestone::MilestoneIndex;
use bee_inx::{client::Inx, Error};
use futures::StreamExt;

const INX_ADDRESS: &str = "http://localhost:9029";

#[tokio::main]
async fn main() -> Result<(), Error> {
let mut inx = client::Inx::connect(INX_ADDRESS.into()).await?;
let mut milestone_stream = inx.listen_to_confirmed_milestones((..).into()).await?;
dotenvy::dotenv().ok();
let inx_connect_url = std::env::var("INX_CONNECT_URL").unwrap_or_else(|_| "http://localhost:9029".to_string());
let milestone_stream = std::env::var("MILESTONE_STREAM").unwrap_or_else(|_| "confirmed_milestones".to_string());
let read_cone = std::env::var("READ_CONE")
.unwrap_or_else(|_| "false".to_string())
.parse::<bool>()
.unwrap();
let read_cone_metadata = std::env::var("READ_CONE_METADATA")
.unwrap_or_else(|_| "false".to_string())
.parse::<bool>()
.unwrap();

let mut inx = Inx::connect(&inx_connect_url).await?;
println!("Connected via INX to node at {inx_connect_url}");

match milestone_stream.as_str() {
"confirmed_milestones" => {
let mut milestone_stream = inx.listen_to_confirmed_milestones((..).into()).await?;
println!("Streaming confirmed milestones and protocol parameters... ");

while let Some(milestone_and_params) = milestone_stream.next().await {
let milestone_and_params = milestone_and_params?;
println!(
"{:?}{:?}",
milestone_and_params.milestone.milestone_info, milestone_and_params.current_protocol_parameters
);

let milestone_index = milestone_and_params.milestone.milestone_info.milestone_index;
fetch_cone_and_metadata(&mut inx, milestone_index, read_cone, read_cone_metadata).await?;
}
}
"latest_milestones" => {
println!("Streaming latest milestones... ");

// Listen to the milestones from the node.
while let Some(milestone_and_params) = milestone_stream.next().await {
let milestone_index = milestone_and_params?.milestone.milestone_info.milestone_index;
println!("Fetch cone of milestone {milestone_index}");
let mut milestone_stream = inx.listen_to_latest_milestones().await?;

while let Some(milestone) = milestone_stream.next().await {
let milestone = milestone?;
println!("{:?}", milestone.milestone_info);

let milestone_index = milestone.milestone_info.milestone_index;
fetch_cone_and_metadata(&mut inx, milestone_index, read_cone, read_cone_metadata).await?;
}
}
_ => {
panic!("unknown milestone stream variant: '{milestone_stream}'");
}
}

Ok(())
}

async fn fetch_cone_and_metadata(
inx: &mut Inx,
milestone_index: MilestoneIndex,
read_cone: bool,
read_cone_metadata: bool,
) -> Result<(), Error> {
if read_cone {
println!("Fetching cone for {milestone_index}...");

// Listen to blocks in the past cone of a milestone.
let mut cone_stream = inx.read_milestone_cone(milestone_index.0.into()).await?;
let mut count = 0usize;

while let Some(Ok(block_metadata)) = cone_stream.next().await {
println!("\t{}", block_metadata.metadata.block_id);
count += 1;
}

println!("Fetched {count} blocks in total.");
}

if read_cone_metadata {
println!("Fetching cone metadata for {milestone_index}...");

// Keep track of the number of blocks.
let mut cone_stream = inx.read_milestone_cone_metadata(milestone_index.0.into()).await?;
let mut count = 0usize;

while let Some(Ok(block_metadata)) = cone_stream.next().await {
println!("Received block with id `{}`", block_metadata.metadata.block_id);
println!("\t{}", block_metadata.block_id);
count += 1;
}

println!("Milestone `{:?}` contained {count} blocks", milestone_index);
println!("Fetched {count} blocks in total.");
}

Ok(())
Expand Down
44 changes: 44 additions & 0 deletions bee-inx/examples/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use bee_inx::{client, milestone::requests::MilestoneRequest, node::requests::NodeStatusRequest, Error};
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Error> {
dotenvy::dotenv().ok();
let inx_connect_url = std::env::var("INX_CONNECT_URL").unwrap_or_else(|_| "http://localhost:9029".to_string());

let mut inx = client::Inx::connect(&inx_connect_url).await?;
println!("Connected via INX to node at {inx_connect_url}");

let node_status = inx.read_node_status().await?;
println!("{:?}", node_status);

let node_configuration = inx.read_node_configuration().await?;
println!("{:?}", node_configuration);

let protocol_parameters = inx
.read_protocol_parameters(MilestoneRequest::MilestoneIndex(node_status.ledger_index))
.await?;
println!("{:?}", protocol_parameters);

const COOLDOWN_MS: u32 = 5000;
let mut node_status_stream = inx
.listen_to_node_status(NodeStatusRequest {
cooldown_in_milliseconds: COOLDOWN_MS,
})
.await?;
println!("Streaming current node status ... ");

while let Some(node_status) = node_status_stream.next().await {
let node_status = node_status?;

println!(
"healthy: {} | synced: {} | ledger_index: {}",
node_status.is_healthy, node_status.is_synced, node_status.ledger_index
);
}

Ok(())
}
31 changes: 31 additions & 0 deletions bee-inx/examples/utxo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use bee_inx::{client::Inx, Error, MilestoneRangeRequest};
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Error> {
dotenvy::dotenv().ok();
let inx_connect_url = std::env::var("INX_CONNECT_URL").unwrap_or_else(|_| "http://localhost:9029".to_string());

let mut inx = Inx::connect(&inx_connect_url).await?;
println!("Connected via INX to node at {inx_connect_url}");

let mut unspent_outputs = inx.read_unspent_outputs().await?;

let mut count = 0;
while let Some(_unspent_output) = unspent_outputs.next().await {
count += 1;
}
println!("Read {count} unspent outputs.");

let mut ledger_update_feed = inx.listen_to_ledger_updates(MilestoneRangeRequest::from(..)).await?;

while let Some(ledger_update) = ledger_update_feed.next().await {
let ledger_update = ledger_update?;
println!("{:?}", ledger_update);
}

Ok(())
}
Loading