This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yanhuangpai
committed
Mar 11, 2024
1 parent
9418327
commit 9536f4c
Showing
8 changed files
with
216 additions
and
19 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
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
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,49 @@ | ||
use serde_json::Value; | ||
use unc_primitives::types::{AccountId}; | ||
use unc_primitives::views::ChipView; | ||
|
||
#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)] | ||
#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")] | ||
pub enum RpcMinerChipsListError { | ||
#[error("Account not found")] | ||
UnknownAccount { error_message: String }, | ||
#[error("Chips info unavailable")] | ||
ChipsInfoUnavailable, | ||
#[error("The node reached its limits. Try again later. More details: {error_message}")] | ||
InternalError { error_message: String }, | ||
} | ||
|
||
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq)] | ||
pub struct RpcMinerChipsListRequest { | ||
pub account_id: AccountId, | ||
} | ||
|
||
#[derive(serde::Serialize, serde::Deserialize, Debug)] | ||
pub struct RpcMinerChipsListResponse { | ||
pub account_id: AccountId, | ||
pub chips: Vec<ChipView>, | ||
} | ||
|
||
impl From<RpcMinerChipsListError> for crate::errors::RpcError { | ||
fn from(error: RpcMinerChipsListError) -> Self { | ||
let error_data = match &error { | ||
RpcMinerChipsListError::UnknownAccount{ error_message } => Some(Value::String(error_message.to_string())), | ||
RpcMinerChipsListError::ChipsInfoUnavailable => { | ||
Some(Value::String("Chips info unavailable".to_string())) | ||
} | ||
RpcMinerChipsListError::InternalError { .. } => Some(Value::String(error.to_string())), | ||
}; | ||
|
||
let error_data_value = match serde_json::to_value(error) { | ||
Ok(value) => value, | ||
Err(err) => { | ||
return Self::new_internal_error( | ||
None, | ||
format!("Failed to serialize RpcValidatorError: {:?}", err), | ||
) | ||
} | ||
}; | ||
|
||
Self::new_internal_or_handler_error(error_data, error_data_value) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,3 +16,4 @@ pub mod status; | |
pub mod transactions; | ||
pub mod validator; | ||
pub mod provider; | ||
pub mod miner_chips_list; |
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,58 @@ | ||
use unc_primitives::types::{AccountId}; | ||
use serde_json::Value; | ||
|
||
use unc_client_primitives::types::{MinerChipsListError}; | ||
use unc_jsonrpc_primitives::errors::RpcParseError; | ||
use unc_jsonrpc_primitives::types::miner_chips_list::{ | ||
RpcMinerChipsListError, RpcMinerChipsListRequest, | ||
}; | ||
|
||
use super::{RpcFrom, RpcRequest}; | ||
|
||
impl RpcRequest for RpcMinerChipsListRequest { | ||
// fn parse(value: Value) -> Result<Self, RpcParseError> { | ||
// let block_height = value | ||
// .get("block_height") | ||
// .and_then(|v| v.as_u64()) | ||
// .ok_or_else(|| RpcParseError("block_height not found or not a u64".parse().unwrap()))?; | ||
// Ok(Self { block_height }) | ||
// } | ||
fn parse(value: Value) -> Result<Self, RpcParseError> { | ||
// Extract block_hash_str from value | ||
let account_id_str = value | ||
.get("account_id") | ||
.and_then(|v| v.as_str()) | ||
.ok_or_else(|| RpcParseError("account_id not found or not a string".parse().unwrap()))?; | ||
|
||
// Construct the CryptoHash from the decoded bytes | ||
let account_id : AccountId = account_id_str | ||
.parse() | ||
.map_err(|_| RpcParseError("Failed to parse epoch_id from base58".parse().unwrap()))?; | ||
|
||
Ok(Self { account_id }) | ||
} | ||
|
||
} | ||
|
||
impl RpcFrom<actix::MailboxError> for RpcMinerChipsListError { | ||
fn rpc_from(error: actix::MailboxError) -> Self { | ||
Self::InternalError { error_message: error.to_string() } | ||
} | ||
} | ||
|
||
impl RpcFrom<MinerChipsListError> for RpcMinerChipsListError { | ||
fn rpc_from(error: MinerChipsListError) -> Self { | ||
match error { | ||
MinerChipsListError::UnknownAccount{ error_message } => Self::UnknownAccount { error_message }, | ||
MinerChipsListError::ChipsInfoUnavailable => Self::ChipsInfoUnavailable, | ||
MinerChipsListError::IOError{ error_message } => Self::InternalError { error_message }, | ||
MinerChipsListError::Unreachable{ ref error_message } => { | ||
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", error_message); | ||
crate::metrics::RPC_UNREACHABLE_ERROR_COUNT | ||
.with_label_values(&["MinerChipsListError"]) | ||
.inc(); | ||
Self::InternalError { error_message: error.to_string() } | ||
} | ||
} | ||
} | ||
} |
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
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
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