Skip to content

Commit

Permalink
Merge branch 'development' into mempool-efficient-gossip
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Sep 19, 2023
2 parents 8819392 + 78b07fd commit 78ded37
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 122 deletions.
12 changes: 3 additions & 9 deletions Cargo.lock

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

75 changes: 25 additions & 50 deletions applications/tari_indexer/src/json_rpc/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ use tari_dan_storage::consensus_models::Decision;
use tari_epoch_manager::{base_layer::EpochManagerHandle, EpochManagerReader};
use tari_indexer_client::types::{
AddAddressRequest,
AddPeerRequest,
AddPeerResponse,
DeleteAddressRequest,
GetEpochManagerStatsResponse,
GetIdentityResponse,
GetNonFungibleCountRequest,
GetNonFungiblesRequest,
GetNonFungiblesResponse,
Expand All @@ -60,12 +64,6 @@ use tari_indexer_client::types::{
SubmitTransactionRequest,
SubmitTransactionResponse,
};
use tari_validator_node_client::types::{
AddPeerRequest,
AddPeerResponse,
GetEpochManagerStatsResponse,
GetIdentityResponse,
};
use tari_validator_node_rpc::client::{SubstateResult, TariCommsValidatorNodeClientFactory, TransactionResultStatus};

use super::json_encoding::{encode_execute_result_into_json, encode_finalized_result_into_json};
Expand Down Expand Up @@ -166,10 +164,7 @@ impl JsonRpcHandlers {
let response = json!({ "vns": vns });
Ok(JsonRpcResponse::success(answer_id, response))
},
Err(e) => Err(Self::generic_error_response(
answer_id,
format!("Failed to get all vns: {}", e),
)),
Err(e) => Err(Self::internal_error(answer_id, format!("Failed to get all vns: {}", e))),
}
}

Expand All @@ -195,18 +190,18 @@ impl JsonRpcHandlers {
&tari_comms::net_address::PeerAddressSource::Config,
)
.await
.map_err(|e| Self::generic_error_response(answer_id, format!("Could not update peer: {}", e)))?;
.map_err(|e| Self::internal_error(answer_id, format!("Could not update peer: {}", e)))?;
if wait_for_dial {
let _conn = connectivity
.dial_peer(node_id)
.await
.map_err(|e| Self::generic_error_response(answer_id, e.to_string()))?;
.map_err(|e| Self::internal_error(answer_id, e.to_string()))?;
} else {
// Dial without waiting
connectivity
.request_many_dials(Some(node_id))
.await
.map_err(|e| Self::generic_error_response(answer_id, e.to_string()))?;
.map_err(|e| Self::internal_error(answer_id, e.to_string()))?;
}

Ok(JsonRpcResponse::success(answer_id, AddPeerResponse {}))
Expand All @@ -221,7 +216,7 @@ impl JsonRpcHandlers {
},
Err(e) => {
warn!(target: LOG_TARGET, "Failed to get comms stats: {}", e);
Err(Self::generic_error_response(
Err(Self::internal_error(
answer_id,
format!("Failed to get comms stats: {}", e),
))
Expand Down Expand Up @@ -275,7 +270,7 @@ impl JsonRpcHandlers {
.await
.map_err(|e| {
warn!(target: LOG_TARGET, "Error getting substate: {}", e);
Self::generic_error_response(answer_id, format!("Error getting substate: {}", e))
Self::internal_error(answer_id, format!("Error getting substate: {}", e))
})? {
Some(substate_resp) => Ok(JsonRpcResponse::success(answer_id, GetSubstateResponse {
address: substate_resp.address,
Expand Down Expand Up @@ -367,7 +362,7 @@ impl JsonRpcHandlers {
.await
.map_err(|e| {
warn!(target: LOG_TARGET, "Error getting substate: {}", e);
Self::generic_error_response(answer_id, format!("Error getting substate: {}", e))
Self::internal_error(answer_id, format!("Error getting substate: {}", e))
})?
.ok_or_else(|| {
JsonRpcResponse::error(
Expand Down Expand Up @@ -402,7 +397,7 @@ impl JsonRpcHandlers {
Ok(addresses) => Ok(JsonRpcResponse::success(answer_id, addresses)),
Err(e) => {
warn!(target: LOG_TARGET, "Error getting addresses: {}", e);
Err(Self::generic_error_response(
Err(Self::internal_error(
answer_id,
format!("Error getting addresses: {}", e),
))
Expand All @@ -422,10 +417,7 @@ impl JsonRpcHandlers {
Ok(_) => Ok(JsonRpcResponse::success(answer_id, ())),
Err(e) => {
warn!(target: LOG_TARGET, "Error adding address: {}", e);
Err(Self::generic_error_response(
answer_id,
format!("Error adding address: {}", e),
))
Err(Self::internal_error(answer_id, format!("Error adding address: {}", e)))
},
}
}
Expand All @@ -438,7 +430,7 @@ impl JsonRpcHandlers {
Ok(_) => Ok(JsonRpcResponse::success(answer_id, ())),
Err(e) => {
warn!(target: LOG_TARGET, "Error deleting address: {}", e);
Err(Self::generic_error_response(
Err(Self::internal_error(
answer_id,
format!("Error deleting address: {}", e),
))
Expand All @@ -453,7 +445,7 @@ impl JsonRpcHandlers {
Ok(_) => Ok(JsonRpcResponse::success(answer_id, ())),
Err(e) => {
warn!(target: LOG_TARGET, "Error clearing addresses: {}", e);
Err(Self::generic_error_response(
Err(Self::internal_error(
answer_id,
format!("Error clearing addresses: {}", e),
))
Expand All @@ -470,7 +462,7 @@ impl JsonRpcHandlers {
Ok(collections) => Ok(JsonRpcResponse::success(answer_id, collections)),
Err(e) => {
warn!(target: LOG_TARGET, "Error getting non fungible collections: {}", e);
Err(Self::generic_error_response(
Err(Self::internal_error(
answer_id,
format!("Error getting non fungible collections: {}", e),
))
Expand All @@ -487,7 +479,7 @@ impl JsonRpcHandlers {
.await
.map_err(|e| {
warn!(target: LOG_TARGET, "Error getting non fungible count: {}", e);
Self::generic_error_response(answer_id, format!("Error getting non fungible count: {}", e))
Self::internal_error(answer_id, format!("Error getting non fungible count: {}", e))
})?;

Ok(JsonRpcResponse::success(answer_id, count))
Expand Down Expand Up @@ -576,22 +568,9 @@ impl JsonRpcHandlers {
)
})?;

let is_valid = self.epoch_manager.is_epoch_active(current_epoch).await.map_err(|err| {
JsonRpcResponse::error(
answer_id,
JsonRpcError::new(
JsonRpcErrorReason::InternalError,
format!("Epoch is not valid:{}", err),
json::Value::Null,
),
)
})?;

let response = GetEpochManagerStatsResponse {
current_epoch,
current_block_height,
is_valid,
committee_shard: None,
};
Ok(JsonRpcResponse::success(answer_id, response))
}
Expand Down Expand Up @@ -641,17 +620,13 @@ impl JsonRpcHandlers {
Self::error_response(answer_id, JsonRpcErrorReason::ApplicationError(404), details)
}

fn internal_error<T: Display>(answer_id: i64, details: T) -> JsonRpcResponse {
error!(target: LOG_TARGET, "Internal error: {}", details);
Self::error_response(answer_id, JsonRpcErrorReason::InternalError, "Something went wrong")
}

// TODO: pass the error in here and log it instead of "squashing" it (switch to using Self::internal_error)
fn generic_error_response(answer_id: i64, error: String) -> JsonRpcResponse {
Self::error_response(
answer_id,
JsonRpcErrorReason::InternalError,
format!("Something went wrong: {}", error),
)
fn internal_error<T: Display>(answer_id: i64, error: T) -> JsonRpcResponse {
let msg = if cfg!(debug_assertions) || option_env!("CI").is_some() {
error.to_string()
} else {
error!(target: LOG_TARGET, "Internal error: {}", error);
"Something went wrong".to_string()
};
Self::error_response(answer_id, JsonRpcErrorReason::InternalError, msg)
}
}
15 changes: 8 additions & 7 deletions applications/tari_validator_node/src/json_rpc/jrpc_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use axum_jrpc::{
// answer_id,
// JsonRpcError::new(
// JsonRpcErrorReason::InvalidParams,
// "Invalid argument".to_string(),
// format!("Invalid argument: {}", err),
// serde_json::Value::Null,
// ),
// )
Expand All @@ -45,14 +45,15 @@ use axum_jrpc::{

pub fn internal_error<T: Display>(answer_id: i64) -> impl Fn(T) -> JsonRpcResponse {
move |err| {
log::error!(target: LOG_TARGET, "🚨 Internal error: {}", err);
let msg = if cfg!(debug_assertions) || option_env!("CI").is_some() {
err.to_string()
} else {
log::error!(target: LOG_TARGET, "🚨 Internal error: {}", err);
"Something went wrong".to_string()
};
JsonRpcResponse::error(
answer_id,
JsonRpcError::new(
JsonRpcErrorReason::InternalError,
"Internal error".to_string(),
serde_json::Value::Null,
),
JsonRpcError::new(JsonRpcErrorReason::InternalError, msg, serde_json::Value::Null),
)
}
}
5 changes: 5 additions & 0 deletions clients/tari_indexer_client/src/json_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
AddPeerRequest,
AddPeerResponse,
DeleteAddressRequest,
GetEpochManagerStatsResponse,
GetNonFungiblesRequest,
GetNonFungiblesResponse,
GetSubstateRequest,
Expand Down Expand Up @@ -110,6 +111,10 @@ impl IndexerJsonRpcClient {
self.send_request("get_non_fungibles", req).await
}

pub async fn get_epoch_manager_stats(&mut self) -> Result<GetEpochManagerStatsResponse, IndexerClientError> {
self.send_request("get_epoch_manager_stats", ()).await
}

async fn send_request<T: Serialize, R: DeserializeOwned>(
&mut self,
method: &str,
Expand Down
14 changes: 14 additions & 0 deletions clients/tari_indexer_client/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use serde_with::{serde_as, DisplayFromStr};
use tari_common_types::types::PublicKey;
use tari_dan_common_types::Epoch;
use tari_dan_storage::consensus_models::Decision;
use tari_engine_types::{
commit_result::ExecuteResult,
Expand Down Expand Up @@ -84,6 +85,13 @@ pub enum IndexerTransactionFinalizedResult {
},
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetIdentityResponse {
pub node_id: String,
pub public_key: PublicKey,
pub public_addresses: Vec<Multiaddr>,
}

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddAddressRequest {
Expand Down Expand Up @@ -137,3 +145,9 @@ pub struct AddPeerRequest {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddPeerResponse {}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetEpochManagerStatsResponse {
pub current_epoch: Epoch,
pub current_block_height: u64,
}
2 changes: 1 addition & 1 deletion dan_layer/engine/tests/templates/buggy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"

[dependencies]
tari_template_abi = { path = "../../../../template_abi", default-features = false, features = ["alloc"] }
tari_bor = { path = "../../../../tari_bor", default-features = false }
tari_bor = { path = "../../../../tari_bor", default-features = false, features = ["alloc"] }
lol_alloc = "0.4.0"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/tari_bor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "BSD-3-Clause"
[dependencies]
# git rev: include fix from https://github.com/enarx/ciborium/pull/80
ciborium = { git = "https://github.com/enarx/ciborium.git", rev = "114614d2a61102eb2321c68e53799d1e6f087aef", default-features = false }
ciborium-io = { version = "0.2.1", default-features = false }
ciborium-io = { git = "https://github.com/enarx/ciborium.git", rev = "114614d2a61102eb2321c68e53799d1e6f087aef", default-features = false }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }

[dev-dependencies]
Expand Down
34 changes: 34 additions & 0 deletions dan_layer/tari_bor/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

#[cfg(not(feature = "std"))]
use alloc::{format, string::String};

#[derive(Debug, Clone)]
pub struct BorError(String);

impl BorError {
pub fn new(str: String) -> Self {
Self(str)
}
}

#[cfg(feature = "std")]
impl std::fmt::Display for BorError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(feature = "std")]
impl std::error::Error for BorError {
fn description(&self) -> &str {
&self.0
}
}

impl From<ciborium::value::Error> for BorError {
fn from(value: ciborium::value::Error) -> Self {
BorError(format!("{}", value))
}
}
Loading

0 comments on commit 78ded37

Please sign in to comment.