eth error factor out unused

This commit is contained in:
dr-frmr 2024-02-26 17:32:27 -03:00
parent 8d98f1a6e7
commit 7258e84978
No known key found for this signature in database
2 changed files with 51 additions and 68 deletions

View File

@ -456,9 +456,7 @@ async fn build_subscription(
params,
} = eth_action
else {
return Err(EthError::InvalidMethod(
"eth: only accepts subscribe logs requests".to_string(),
));
return Err(EthError::PermissionDenied); // will never hit
};
let Some(mut aps) = providers.get_mut(&chain_id) else {
return Err(EthError::NoRpcForChain);
@ -572,30 +570,23 @@ async fn maintain_subscription(
) -> Result<(), EthError> {
println!("provider: maintain_subscription\r");
loop {
match rx.recv().await {
Err(_e) => {
return Err(EthError::SubscriptionClosed(sub_id));
}
Ok(value) => {
let result: SubscriptionResult =
serde_json::from_str(value.get()).map_err(|_| {
EthError::RpcError(
"eth: failed to deserialize subscription result".to_string(),
)
})?;
kernel_message(
&our,
rand::random(),
target.clone(),
rsvp.clone(),
true,
None,
EthSubResult::Ok(EthSub { id: sub_id, result }),
&send_to_loop,
)
.await;
}
}
let value = rx
.recv()
.await
.map_err(|_| EthError::SubscriptionClosed(sub_id))?;
let result: SubscriptionResult =
serde_json::from_str(value.get()).map_err(|_| EthError::SubscriptionClosed(sub_id))?;
kernel_message(
&our,
rand::random(),
target.clone(),
rsvp.clone(),
true,
None,
EthSubResult::Ok(EthSub { id: sub_id, result }),
&send_to_loop,
)
.await;
}
}

View File

@ -65,20 +65,12 @@ pub enum EthError {
MalformedRequest,
/// No RPC provider for the chain
NoRpcForChain,
/// Underlying transport error
TransportError(String),
/// Subscription closed
SubscriptionClosed(u64),
/// The subscription ID was not found, so we couldn't unsubscribe.
SubscriptionNotFound,
/// Invalid method
InvalidMethod(String),
/// Invalid params
InvalidParams,
/// Permission denied
PermissionDenied,
/// Internal RPC error
RpcError(String),
/// RPC timed out
RpcTimeout,
}
@ -130,6 +122,39 @@ pub enum EthConfigResponse {
PermissionDenied,
}
/// Settings for our ETH provider
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AccessSettings {
pub public: bool, // whether or not other nodes can access through us
pub allow: HashSet<String>, // whitelist for access (only used if public == false)
pub deny: HashSet<String>, // blacklist for access (always used)
}
pub type SavedConfigs = Vec<ProviderConfig>;
/// Provider config. Can currently be a node or a ws provider instance.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProviderConfig {
pub chain_id: u64,
pub trusted: bool,
pub provider: NodeOrRpcUrl,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum NodeOrRpcUrl {
Node(crate::core::KnsUpdate),
RpcUrl(String),
}
impl std::cmp::PartialEq<str> for NodeOrRpcUrl {
fn eq(&self, other: &str) -> bool {
match self {
NodeOrRpcUrl::Node(kns) => kns.name == other,
NodeOrRpcUrl::RpcUrl(url) => url == other,
}
}
}
//
// Internal types
//
@ -166,36 +191,3 @@ pub fn to_static_str(method: &str) -> Option<&'static str> {
_ => None,
}
}
/// Settings for our ETH provider
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AccessSettings {
pub public: bool, // whether or not other nodes can access through us
pub allow: HashSet<String>, // whitelist for access (only used if public == false)
pub deny: HashSet<String>, // blacklist for access (always used)
}
pub type SavedConfigs = Vec<ProviderConfig>;
/// Provider config. Can currently be a node or a ws provider instance.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProviderConfig {
pub chain_id: u64,
pub trusted: bool,
pub provider: NodeOrRpcUrl,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum NodeOrRpcUrl {
Node(crate::core::KnsUpdate),
RpcUrl(String),
}
impl std::cmp::PartialEq<str> for NodeOrRpcUrl {
fn eq(&self, other: &str) -> bool {
match self {
NodeOrRpcUrl::Node(kns) => kns.name == other,
NodeOrRpcUrl::RpcUrl(url) => url == other,
}
}
}