use std::sync::{Arc, RwLock};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use crate::{
node::InMemoryNodeInner,
node::ShowCalls,
node::ShowVMDetails,
node::{ShowGasDetails, ShowStorageLogs},
};
pub struct ConfigurationApiNamespace<S> {
node: Arc<RwLock<InMemoryNodeInner<S>>>,
}
impl<S> ConfigurationApiNamespace<S> {
pub fn new(node: Arc<RwLock<InMemoryNodeInner<S>>>) -> Self {
Self { node }
}
}
#[rpc]
pub trait ConfigurationApiNamespaceT {
#[rpc(name = "config_getShowCalls", returns = "String")]
fn config_get_show_calls(&self) -> Result<String>;
#[rpc(name = "config_getCurrentTimestamp", returns = "u64")]
fn config_get_current_timestamp(&self) -> Result<u64>;
#[rpc(name = "config_setShowCalls", returns = "String")]
fn config_set_show_calls(&self, value: String) -> Result<String>;
#[rpc(name = "config_setShowStorageLogs", returns = "String")]
fn config_set_show_storage_logs(&self, value: String) -> Result<String>;
#[rpc(name = "config_setShowVmDetails", returns = "String")]
fn config_set_show_vm_details(&self, value: String) -> Result<String>;
#[rpc(name = "config_setShowGasDetails", returns = "String")]
fn config_set_show_gas_details(&self, value: String) -> Result<String>;
#[rpc(name = "config_setResolveHashes", returns = "bool")]
fn config_set_resolve_hashes(&self, value: bool) -> Result<bool>;
}
impl<S: std::marker::Send + std::marker::Sync + 'static> ConfigurationApiNamespaceT
for ConfigurationApiNamespace<S>
{
fn config_get_show_calls(&self) -> Result<String> {
let reader = self.node.read().unwrap();
Ok(reader.show_calls.to_string())
}
fn config_get_current_timestamp(&self) -> Result<u64> {
let reader = self.node.read().unwrap();
Ok(reader.current_timestamp)
}
fn config_set_show_calls(&self, value: String) -> Result<String> {
let show_calls = match value.parse::<ShowCalls>() {
Ok(value) => value,
Err(_) => {
let reader = self.node.read().unwrap();
return Ok(reader.show_calls.to_string());
}
};
let mut inner = self.node.write().unwrap();
inner.show_calls = show_calls;
Ok(inner.show_calls.to_string())
}
fn config_set_show_storage_logs(&self, value: String) -> Result<String> {
let show_storage_logs = match value.parse::<ShowStorageLogs>() {
Ok(value) => value,
Err(_) => {
let reader = self.node.read().unwrap();
return Ok(reader.show_storage_logs.to_string());
}
};
let mut inner = self.node.write().unwrap();
inner.show_storage_logs = show_storage_logs;
Ok(inner.show_storage_logs.to_string())
}
fn config_set_show_vm_details(&self, value: String) -> Result<String> {
let show_vm_details = match value.parse::<ShowVMDetails>() {
Ok(value) => value,
Err(_) => {
let reader = self.node.read().unwrap();
return Ok(reader.show_vm_details.to_string());
}
};
let mut inner = self.node.write().unwrap();
inner.show_vm_details = show_vm_details;
Ok(inner.show_vm_details.to_string())
}
fn config_set_show_gas_details(&self, value: String) -> Result<String> {
let show_gas_details = match value.parse::<ShowGasDetails>() {
Ok(value) => value,
Err(_) => {
let reader = self.node.read().unwrap();
return Ok(reader.show_gas_details.to_string());
}
};
let mut inner = self.node.write().unwrap();
inner.show_gas_details = show_gas_details;
Ok(inner.show_gas_details.to_string())
}
fn config_set_resolve_hashes(&self, value: bool) -> Result<bool> {
let mut inner = self.node.write().unwrap();
inner.resolve_hashes = value;
Ok(inner.resolve_hashes)
}
}