Skip to content

Commit

Permalink
First version of InxRequester (#90)
Browse files Browse the repository at this point in the history
* Initial version

* Address comments

* Fix tests
  • Loading branch information
grtlr committed Apr 28, 2022
1 parent 04a8484 commit 6e2a16c
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 138 deletions.
5 changes: 4 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[alias]
ci-build = "build --all-targets --all-features --release"
ci-build-all = "build --all-targets --all-features --release"
ci-build-inx = "build --all-targets --features inx --release"
ci-build-api = "build --all-targets --features api --release"

ci-clippy = "clippy --all-targets --all-features -- -D warnings"
ci-doctest = "test --doc --all-features --release"
ci-doc = "doc --all-features --no-deps --document-private-items"
Expand Down
16 changes: 14 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,22 @@ jobs:

- uses: Swatinem/rust-cache@v1

- name: Build
- name: Build with all features
uses: actions-rs/cargo@v1
with:
command: ci-build
command: ci-build-all

- name: Build with INX only
if: contains(matrix.os, 'ubuntu')
uses: actions-rs/cargo@v1
with:
command: ci-build-inx

- name: Build with API only
if: contains(matrix.os, 'ubuntu')
uses: actions-rs/cargo@v1
with:
command: ci-build-api

- name: Test
uses: actions-rs/cargo@v1
Expand Down
22 changes: 16 additions & 6 deletions Cargo.lock

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

31 changes: 19 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/iotaledger/inx-chronicle"
license = "Apache-2.0"
keywords = ["iota", "storage", "permanode", "chronicle", "inx"]
homepage = "https://www.iota.org"
rust-version = "1.60"

[lib]
name = "chronicle"
Expand All @@ -29,7 +30,7 @@ dotenv = { version = "0.15", default-features = false }
env_logger = { version = "0.9", default-features = false, features = ["termcolor", "atty", "humantime"] }
futures = { version = "0.3", default-features = false }
hex = { version = "0.4", default-features = false }
inx = { git = "https://github.com/iotaledger/inx", version = "0.2", default-features = false, features = ["types"] }
humantime-serde = { version = "1.1", default-features = false }
log = { version = "0.4", default-features = false }
mongodb = { version = "2.2", default-features = false, features = ["tokio-runtime"] }
serde = { version = "1.0", features = ["derive"], default-features = false }
Expand All @@ -39,7 +40,6 @@ time = { version = "0.3", default-features = false, features = ["std"] }
tokio = { version = "1.17", default-features = false, features = [ "macros", "rt-multi-thread", "signal" ] }
tokio-stream = { version = "0.1", default-features = false }
toml = { version = "0.5", default-features = false }
tonic = { version = "0.7", default-features = false }
url = { version = "2.2", default-features = false }
uuid = { version = "1.0", default-features = false, features = ["v4"] }

Expand All @@ -50,6 +50,9 @@ serde_urlencoded = { version = "0.7", default-features = false, optional = true
tower = { version = "0.4", default-features = false, optional = true }
tower-http = { version = "0.3", default-features = false, features = ["cors", "catch-panic", "trace"], optional = true }

# INX
inx = { git = "https://github.com/iotaledger/inx", version = "0.2", default-features = false, features = ["types"], optional = true }

# Stardust types
bee-message-stardust = { package = "bee-message", version = "0.2", default-features = false, features = ["std", "serde", "dto"], optional = true }
bee-rest-api-stardust = { package = "bee-rest-api", version = "0.2", default-features = false, optional = true }
Expand All @@ -62,18 +65,19 @@ bee-rest-api-stardust = { package = "bee-rest-api", git = "https://github.com/io
[features]
default = [
"api-analytics",
"api-explorer",
"api-indexer",
"api-node",
"api-explorer",
"api-indexer",
"api-node",
"inx",
"stardust",
]
api = [
"axum",
"dep:axum",
"derive_more/from",
"hyper",
"serde_urlencoded",
"tower",
"tower-http",
"dep:hyper",
"dep:serde_urlencoded",
"dep:tower",
"dep:tower-http",
]
api-analytics = [
"api",
Expand All @@ -87,7 +91,10 @@ api-indexer = [
api-node = [
"api",
]
inx = [
"dep:inx",
]
stardust = [
"bee-message-stardust",
"bee-rest-api-stardust",
"dep:bee-message-stardust",
"dep:bee-rest-api-stardust",
]
1 change: 1 addition & 0 deletions bin/inx-chronicle/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ password = "root"

[inx]
address = "http://localhost:9029"
connection_retry_interval = "5s"
2 changes: 1 addition & 1 deletion bin/inx-chronicle/src/broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl HandleEvent<inx::proto::Milestone> for Broker {
milestone: inx::proto::Milestone,
_state: &mut Self::State,
) -> Result<(), Self::Error> {
log::trace!("Received Stardust Milestone Event");
log::debug!("Received Stardust Milestone Event");
match stardust::milestone::MilestoneRecord::try_from(milestone) {
Ok(rec) => self.db.upsert_one(rec).await?,
Err(e) => {
Expand Down
5 changes: 3 additions & 2 deletions bin/inx-chronicle/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
use std::{fs, path::Path};

use chronicle::db::MongoConfig;
#[cfg(feature = "stardust")]
use chronicle::inx::InxConfig;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[cfg(feature = "inx")]
use crate::inx::InxConfig;

#[derive(Error, Debug)]
pub enum ConfigError {
#[error("failed to read file: {0}")]
Expand Down
15 changes: 10 additions & 5 deletions src/inx/mod.rs → bin/inx-chronicle/src/inx/config.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod error;
use std::time::Duration;

use inx::{client::InxClient, tonic::Channel};
use serde::{Deserialize, Serialize};

pub use self::error::InxError;
pub use super::InxWorkerError;

/// A builder to establish a connection to INX.
#[must_use]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct InxConfig {
/// The bind address of node's INX interface.
pub address: String,
/// The time that has to pass until a new connection attempt is made.
#[serde(with = "humantime_serde")]
pub connection_retry_interval: Duration,
}

impl Default for InxConfig {
fn default() -> Self {
Self {
address: "http://localhost:9029".into(),
connection_retry_interval: Duration::from_secs(5),
}
}
}
Expand All @@ -29,19 +33,20 @@ impl InxConfig {
pub fn new(address: impl Into<String>) -> Self {
Self {
address: address.into(),
..Default::default()
}
}

/// Constructs an [`InxClient`] by consuming the [`InxConfig`].
pub async fn build(&self) -> Result<InxClient<Channel>, InxError> {
pub async fn build(&self) -> Result<InxClient<Channel>, InxWorkerError> {
let url = url::Url::parse(&self.address)?;

if url.scheme() != "http" {
return Err(InxError::InvalidAddress(self.address.clone()));
return Err(InxWorkerError::InvalidAddress(self.address.clone()));
}

InxClient::connect(self.address.clone())
.await
.map_err(InxError::ConnectionError)
.map_err(InxWorkerError::ConnectionError)
}
}
28 changes: 28 additions & 0 deletions bin/inx-chronicle/src/inx/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use thiserror::Error;

use super::listener::InxListenerError;

#[derive(Debug, Error)]
pub enum InxWorkerError {
#[error("failed to establish connection: {0}")]
ConnectionError(inx::tonic::Error),
#[error("failed to answer")]
FailedToAnswerRequest,
#[error("expected INX address with format `http://<address>:<port>`, but found `{0}`")]
InvalidAddress(String),
#[error(transparent)]
ListenerError(#[from] InxListenerError),
#[error("the broker actor is not running")]
MissingBroker,
#[error(transparent)]
ParsingAddressFailed(#[from] url::ParseError),
#[error(transparent)]
Read(#[from] inx::tonic::Status),
#[error(transparent)]
Runtime(#[from] crate::RuntimeError),
#[error(transparent)]
TransportFailed(#[from] inx::tonic::Error),
}
Loading

0 comments on commit 6e2a16c

Please sign in to comment.