Skip to content

Commit fb022cb

Browse files
committed
main merged
2 parents d27ac2e + ee2fb78 commit fb022cb

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

configs/args/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"rpc_external": false,
8383
"rpc_admin": false,
8484
"rpc_admin_external": false,
85+
"rpc_unsafe": false,
8586
"rpc_max_request_size": 15,
8687
"rpc_max_response_size": 15,
8788
"rpc_max_subscriptions_per_connection": 1024,

madara/crates/client/rpc/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ pub struct Starknet {
830830
storage_proof_config: StorageProofConfig,
831831
pub(crate) block_prod_handle: Option<mc_block_production::BlockProductionHandle>,
832832
pub ctx: ServiceContext,
833+
pub(crate) rpc_unsafe_enabled: bool,
833834
}
834835

835836
impl Starknet {
@@ -849,12 +850,17 @@ impl Starknet {
849850
block_prod_handle,
850851
ctx,
851852
pre_v0_9_preconfirmed_as_pending: false,
853+
rpc_unsafe_enabled: false,
852854
}
853855
}
854856

855857
pub fn set_pre_v0_9_preconfirmed_as_pending(&mut self, value: bool) {
856858
self.pre_v0_9_preconfirmed_as_pending = value;
857859
}
860+
861+
pub fn set_rpc_unsafe_enabled(&mut self, value: bool) {
862+
self.rpc_unsafe_enabled = value;
863+
}
858864
}
859865

860866
/// Returns the RpcModule merged with all the supported RPC versions.

madara/crates/client/rpc/src/versions/admin/v0_1_0/methods/write.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use mp_rpc::v0_9_0::{
1212
};
1313
use mp_transactions::{L1HandlerTransactionResult, L1HandlerTransactionWithFee};
1414
use mp_utils::service::MadaraServiceId;
15+
use mp_block::header::CustomHeader;
1516

1617
#[async_trait]
1718
impl MadaraWriteRpcApiV0_1_0Server for Starknet {
@@ -89,22 +90,22 @@ impl MadaraWriteRpcApiV0_1_0Server for Starknet {
8990
}
9091

9192
/// Force revert chain to a previous block by hash.
92-
/// Only works in full node mode.
93+
/// Only available when unsafe RPC methods are enabled.
9394
async fn revert_to(&self, block_hash: Felt) -> RpcResult<()> {
94-
// Only allow revert when in full node mode
95-
if self.ctx.service_status(MadaraServiceId::BlockProduction).is_off() {
96-
self.backend.revert_to(&block_hash).map_err(StarknetRpcApiError::from)?;
97-
// TODO(heemankv, 04-11-25): We should spend time in ruling out the two sources of truth problem for ChainTip
98-
// For now, we have to manually fetch Chain Tip from DB and update this in backend
99-
let fresh_chain_tip = self.backend.db.get_chain_tip().unwrap();
100-
let backend_chain_tip = mc_db::ChainTip::from_storage(fresh_chain_tip);
101-
self.backend.chain_tip.send_replace(backend_chain_tip);
102-
Ok(())
103-
} else {
104-
Err(StarknetRpcApiError::ErrUnexpectedError {
105-
error: "This method is only available in full node mode".to_string().into(),
106-
})?
95+
// Check if unsafe RPC methods are enabled
96+
if !self.rpc_unsafe_enabled {
97+
return Err(StarknetRpcApiError::ErrUnexpectedError {
98+
error: "This method requires the --rpc-unsafe flag to be enabled".to_string().into()
99+
}.into());
107100
}
101+
102+
self.backend.revert_to(&block_hash).map_err(StarknetRpcApiError::from)?;
103+
let fresh_chain_tip = self.backend.db.get_chain_tip()
104+
.context("Failed to get chain tip after revert")
105+
.map_err(StarknetRpcApiError::from)?;
106+
let backend_chain_tip = mc_db::ChainTip::from_storage(fresh_chain_tip);
107+
self.backend.chain_tip.send_replace(backend_chain_tip);
108+
Ok(())
108109
}
109110

110111
async fn add_l1_handler_message(
@@ -121,6 +122,13 @@ impl MadaraWriteRpcApiV0_1_0Server for Starknet {
121122
}
122123

123124
async fn set_block_header(&self, custom_block_headers: CustomHeader) -> RpcResult<()> {
125+
// Check if unsafe RPC methods are enabled
126+
if !self.rpc_unsafe_enabled {
127+
return Err(StarknetRpcApiError::ErrUnexpectedError {
128+
error: "This method requires the --rpc-unsafe flag to be enabled".to_string().into()
129+
}.into());
130+
}
131+
124132
self.backend.set_custom_header(custom_block_headers);
125133
Ok(())
126134
}

madara/node/src/cli/rpc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ pub struct RpcParams {
9191
#[arg(env = "MADARA_RPC_ADMIN_EXTERNAL", long, default_value_t = false)]
9292
pub rpc_admin_external: bool,
9393

94+
/// Enables unsafe admin RPC methods. This includes dangerous methods like
95+
/// `revertTo` and `setCustomBlockHeader` that can modify the blockchain state.
96+
/// Use with extreme caution. Requires `--rpc-admin` to be enabled.
97+
#[arg(env = "MADARA_RPC_UNSAFE", long, default_value_t = false, requires = "rpc_admin")]
98+
pub rpc_unsafe: bool,
99+
94100
/// Set the maximum RPC request payload size for both HTTP and WebSockets in mebibytes.
95101
#[arg(env = "MADARA_RPC_MAX_REQUEST_SIZE", long, default_value_t = RPC_DEFAULT_MAX_REQUEST_SIZE_MIB)]
96102
pub rpc_max_request_size: u32,

madara/node/src/service/rpc/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl Service for RpcService {
7676
let block_prod_handle = self.block_prod_handle.clone();
7777

7878
let pre_v0_9_preconfirmed_as_pending = self.config.rpc_pre_v0_9_preconfirmed_as_pending;
79+
let rpc_unsafe_enabled = self.config.rpc_unsafe;
7980

8081
runner.service_loop(move |ctx| async move {
8182
let submit_tx = Arc::new(submit_tx_provider.make(ctx.clone()));
@@ -88,6 +89,7 @@ impl Service for RpcService {
8889
ctx.clone(),
8990
);
9091
starknet.set_pre_v0_9_preconfirmed_as_pending(pre_v0_9_preconfirmed_as_pending);
92+
starknet.set_rpc_unsafe_enabled(rpc_unsafe_enabled);
9193

9294
let metrics = RpcMetrics::register()?;
9395

0 commit comments

Comments
 (0)