Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ impl SurfpoolError {
Self(error)
}

pub fn disable_cheatcode(e: String) -> Self {
let mut error = Error::invalid_request();
error.data = Some(json!(format!("Unable to disable cheatcode: {}", e)));
Self(error)
}

pub fn enable_cheatcode(e: String) -> Self {
let mut error = Error::invalid_request();
error.data = Some(json!(format!("Unable to enable cheatcode: {}", e)));
Self(error)
}

pub fn set_account<T>(pubkey: Pubkey, e: T) -> Self
where
T: ToString,
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/rpc/accounts_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn test_set_and_get_supply() {
let setup = TestSetup::new(SurfpoolAccountsScanRpc);
let cheatcodes_rpc = SurfnetCheatcodesRpc;
let cheatcodes_rpc = SurfnetCheatcodesRpc::empty();

// test initial default values
let initial_supply = setup
Expand Down Expand Up @@ -959,7 +959,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn test_get_supply_exclude_accounts() {
let setup = TestSetup::new(SurfpoolAccountsScanRpc);
let cheatcodes_rpc = SurfnetCheatcodesRpc;
let cheatcodes_rpc = SurfnetCheatcodesRpc::empty();

// set supply with non-circulating accounts
let supply_update = SupplyUpdate {
Expand Down Expand Up @@ -1007,7 +1007,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn test_partial_supply_update() {
let setup = TestSetup::new(SurfpoolAccountsScanRpc);
let cheatcodes_rpc = SurfnetCheatcodesRpc;
let cheatcodes_rpc = SurfnetCheatcodesRpc::empty();

// set initial values
let initial_update = SupplyUpdate {
Expand Down Expand Up @@ -1051,7 +1051,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn test_set_supply_with_multiple_invalid_pubkeys() {
let setup = TestSetup::new(SurfpoolAccountsScanRpc);
let cheatcodes_rpc = SurfnetCheatcodesRpc;
let cheatcodes_rpc = SurfnetCheatcodesRpc::empty();
let invalid_pubkey = "invalid_pubkey";

// test with multiple invalid pubkeys - should fail on the first one
Expand Down Expand Up @@ -1082,7 +1082,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn test_set_supply_with_max_values() {
let setup = TestSetup::new(SurfpoolAccountsScanRpc);
let cheatcodes_rpc = SurfnetCheatcodesRpc;
let cheatcodes_rpc = SurfnetCheatcodesRpc::empty();

let supply_update = SupplyUpdate {
total: Some(u64::MAX),
Expand Down Expand Up @@ -1112,7 +1112,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread")]
async fn test_set_supply_large_valid_account_list() {
let setup = TestSetup::new(SurfpoolAccountsScanRpc);
let cheatcodes_rpc = SurfnetCheatcodesRpc;
let cheatcodes_rpc = SurfnetCheatcodesRpc::empty();

let large_account_list: Vec<String> = (0..100)
.map(|i| match i % 10 {
Expand Down
47 changes: 45 additions & 2 deletions crates/core/src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{future::Future, sync::Arc};
use std::{
future::Future,
sync::{Arc, Mutex},
};

use blake3::Hash;
use crossbeam_channel::Sender;
Expand All @@ -9,7 +12,7 @@ use jsonrpc_core::{
};
use jsonrpc_pubsub::{PubSubMetadata, Session};
use solana_clock::Slot;
use surfpool_types::{SimnetCommand, SimnetEvent, types::RpcConfig};
use surfpool_types::{CheatcodeConfig, SimnetCommand, SimnetEvent, types::RpcConfig};

use crate::{
PluginManagerCommand,
Expand Down Expand Up @@ -48,6 +51,7 @@ pub struct RunloopContext {
pub plugin_manager_commands_tx: Sender<PluginManagerCommand>,
pub remote_rpc_client: Option<SurfnetRemoteClient>,
pub rpc_config: RpcConfig,
pub cheatcode_config: Arc<Mutex<CheatcodeConfig>>,
}

pub struct SurfnetRpcContext<T> {
Expand Down Expand Up @@ -113,6 +117,7 @@ pub struct SurfpoolMiddleware {
pub plugin_manager_commands_tx: Sender<PluginManagerCommand>,
pub config: RpcConfig,
pub remote_rpc_client: Option<SurfnetRemoteClient>,
pub cheatcode_config: Arc<Mutex<CheatcodeConfig>>,
}

impl SurfpoolMiddleware {
Expand All @@ -129,6 +134,7 @@ impl SurfpoolMiddleware {
plugin_manager_commands_tx: plugin_manager_commands_tx.clone(),
config: config.clone(),
remote_rpc_client: remote_rpc_client.clone(),
cheatcode_config: CheatcodeConfig::new(),
}
}
}
Expand Down Expand Up @@ -171,7 +177,43 @@ impl Middleware<Option<RunloopContext>> for SurfpoolMiddleware {
plugin_manager_commands_tx: self.plugin_manager_commands_tx.clone(),
remote_rpc_client: self.remote_rpc_client.clone(),
rpc_config: self.config.clone(),
cheatcode_config: self.cheatcode_config.clone(),
});

// All surfnet cheatcodes will start with surfnet. If the request is a cheatcode, make sure it isn't disabled.
if method_name.starts_with("surfnet_")
&& let Some(meta_val) = meta.clone()
{
let Ok(meta_val) = meta_val.cheatcode_config.lock() else {
let error = Response::from(
Error {
code: ErrorCode::InternalError,
message: "An internal server error occured".to_string(),
data: None,
},
None,
);
warn!("Request rejected due to cheatsheet being disabled");

return Either::Left(Box::pin(async move { Some(error) }));
};
if meta_val.is_cheatcode_disabled(&method_name) {
let error = Response::from(
Error {
code: ErrorCode::InvalidRequest,
message: format!(
"Cheatcode rpc method: {method_name} is currently disabled"
),
data: None,
},
None,
);
warn!("Request rejected due to cheatcode rpc method being disabled");

return Either::Left(Box::pin(async move { Some(error) }));
}
}

Either::Left(Box::pin(next(request, meta).map(move |res| {
if let Some(Response::Single(output)) = &res {
if let jsonrpc_core::Output::Failure(failure) = output {
Expand Down Expand Up @@ -222,6 +264,7 @@ impl Middleware<Option<SurfpoolWebsocketMeta>> for SurfpoolWebsocketMiddleware {
plugin_manager_commands_tx: self.surfpool_middleware.plugin_manager_commands_tx.clone(),
remote_rpc_client: self.surfpool_middleware.remote_rpc_client.clone(),
rpc_config: self.surfpool_middleware.config.clone(),
cheatcode_config: self.surfpool_middleware.cheatcode_config.clone(),
};
let session = meta
.as_ref()
Expand Down
Loading
Loading