Skip to content

Commit

Permalink
refactor(katana-rpc): isolate dev JSON RPC API from katana namespace (
Browse files Browse the repository at this point in the history
#1544)

* feat: isolate dev JSON RPC API from katana impl

* feat: fix the changes

* feat: remove the duplicate endpoint in katana
  • Loading branch information
makluganteng authored Feb 17, 2024
1 parent 68e81f6 commit ca3b018
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 60 deletions.
4 changes: 2 additions & 2 deletions bin/katana/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ impl KatanaArgs {
}

pub fn server_config(&self) -> ServerConfig {
let mut apis = vec![ApiKind::Starknet];
let mut apis = vec![ApiKind::Starknet, ApiKind::Katana];
// only enable `katana` API in dev mode
if self.dev {
apis.push(ApiKind::Katana);
apis.push(ApiKind::Dev);
}

ServerConfig {
Expand Down
27 changes: 27 additions & 0 deletions crates/katana/rpc/rpc-api/src/dev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use katana_primitives::FieldElement;

#[cfg_attr(not(feature = "client"), rpc(server, namespace = "dev"))]
#[cfg_attr(feature = "client", rpc(client, server, namespace = "dev"))]
pub trait DevApi {
#[method(name = "generateBlock")]
async fn generate_block(&self) -> RpcResult<()>;

#[method(name = "nextBlockTimestamp")]
async fn next_block_timestamp(&self) -> RpcResult<()>;

#[method(name = "setNextBlockTimestamp")]
async fn set_next_block_timestamp(&self, timestamp: u64) -> RpcResult<()>;

#[method(name = "increaseNextBlockTimestamp")]
async fn increase_next_block_timestamp(&self, timestamp: u64) -> RpcResult<()>;

#[method(name = "setStorageAt")]
async fn set_storage_at(
&self,
contract_address: FieldElement,
key: FieldElement,
value: FieldElement,
) -> RpcResult<()>;
}
21 changes: 0 additions & 21 deletions crates/katana/rpc/rpc-api/src/katana.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use katana_primitives::FieldElement;
use katana_rpc_types::account::Account;

#[cfg_attr(not(feature = "client"), rpc(server, namespace = "katana"))]
#[cfg_attr(feature = "client", rpc(client, server, namespace = "katana"))]
pub trait KatanaApi {
#[method(name = "generateBlock")]
async fn generate_block(&self) -> RpcResult<()>;

#[method(name = "nextBlockTimestamp")]
async fn next_block_timestamp(&self) -> RpcResult<u64>;

#[method(name = "setNextBlockTimestamp")]
async fn set_next_block_timestamp(&self, timestamp: u64) -> RpcResult<()>;

#[method(name = "increaseNextBlockTimestamp")]
async fn increase_next_block_timestamp(&self, timestamp: u64) -> RpcResult<()>;

#[method(name = "predeployedAccounts")]
async fn predeployed_accounts(&self) -> RpcResult<Vec<Account>>;

#[method(name = "setStorageAt")]
async fn set_storage_at(
&self,
contract_address: FieldElement,
key: FieldElement,
value: FieldElement,
) -> RpcResult<()>;
}
2 changes: 2 additions & 0 deletions crates/katana/rpc/rpc-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod dev;
pub mod katana;
pub mod starknet;

Expand All @@ -6,4 +7,5 @@ pub mod starknet;
pub enum ApiKind {
Starknet,
Katana,
Dev,
}
55 changes: 55 additions & 0 deletions crates/katana/rpc/rpc/src/dev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::sync::Arc;

use jsonrpsee::core::{async_trait, Error};
use katana_core::sequencer::KatanaSequencer;
use katana_primitives::FieldElement;
use katana_rpc_api::dev::DevApiServer;
use katana_rpc_types::error::katana::KatanaApiError;

pub struct DevApi {
sequencer: Arc<KatanaSequencer>,
}

impl DevApi {
pub fn new(sequencer: Arc<KatanaSequencer>) -> Self {
Self { sequencer }
}
}

#[async_trait]
impl DevApiServer for DevApi {
async fn generate_block(&self) -> Result<(), Error> {
self.sequencer.block_producer().force_mine();
Ok(())
}

async fn next_block_timestamp(&self) -> Result<(), Error> {
// Ok(self.sequencer.backend().env.read().block.block_timestamp.0)
Ok(())
}

async fn set_next_block_timestamp(&self, timestamp: u64) -> Result<(), Error> {
self.sequencer
.set_next_block_timestamp(timestamp)
.map_err(|_| Error::from(KatanaApiError::FailedToChangeNextBlockTimestamp))
}

async fn increase_next_block_timestamp(&self, timestamp: u64) -> Result<(), Error> {
self.sequencer
.increase_next_block_timestamp(timestamp)
.map_err(|_| Error::from(KatanaApiError::FailedToChangeNextBlockTimestamp))
}

async fn set_storage_at(
&self,
_contract_address: FieldElement,
_key: FieldElement,
_value: FieldElement,
) -> Result<(), Error> {
// self.sequencer
// .set_storage_at(contract_address.into(), key, value)
// .await
// .map_err(|_| Error::from(KatanaApiError::FailedToUpdateStorage))
Ok(())
}
}
37 changes: 0 additions & 37 deletions crates/katana/rpc/rpc/src/katana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::sync::Arc;

use jsonrpsee::core::{async_trait, Error};
use katana_core::sequencer::KatanaSequencer;
use katana_primitives::FieldElement;
use katana_rpc_api::katana::KatanaApiServer;
use katana_rpc_types::account::Account;
use katana_rpc_types::error::katana::KatanaApiError;

pub struct KatanaApi {
sequencer: Arc<KatanaSequencer>,
Expand All @@ -19,28 +17,6 @@ impl KatanaApi {

#[async_trait]
impl KatanaApiServer for KatanaApi {
async fn generate_block(&self) -> Result<(), Error> {
self.sequencer.block_producer().force_mine();
Ok(())
}

async fn next_block_timestamp(&self) -> Result<u64, Error> {
// Ok(self.sequencer.backend().env.read().block.block_timestamp.0)
unimplemented!()
}

async fn set_next_block_timestamp(&self, timestamp: u64) -> Result<(), Error> {
self.sequencer
.set_next_block_timestamp(timestamp)
.map_err(|_| Error::from(KatanaApiError::FailedToChangeNextBlockTimestamp))
}

async fn increase_next_block_timestamp(&self, timestamp: u64) -> Result<(), Error> {
self.sequencer
.increase_next_block_timestamp(timestamp)
.map_err(|_| Error::from(KatanaApiError::FailedToChangeNextBlockTimestamp))
}

async fn predeployed_accounts(&self) -> Result<Vec<Account>, Error> {
Ok(self
.sequencer
Expand All @@ -51,17 +27,4 @@ impl KatanaApiServer for KatanaApi {
.map(|e| Account::new(*e.0, e.1))
.collect())
}

async fn set_storage_at(
&self,
_contract_address: FieldElement,
_key: FieldElement,
_value: FieldElement,
) -> Result<(), Error> {
// self.sequencer
// .set_storage_at(contract_address.into(), key, value)
// .await
// .map_err(|_| Error::from(KatanaApiError::FailedToUpdateStorage))
Ok(())
}
}
6 changes: 6 additions & 0 deletions crates/katana/rpc/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod config;
pub mod dev;
pub mod katana;
pub mod starknet;

Expand All @@ -16,11 +17,13 @@ use jsonrpsee::tracing::debug;
use jsonrpsee::types::Params;
use jsonrpsee::RpcModule;
use katana_core::sequencer::KatanaSequencer;
use katana_rpc_api::dev::DevApiServer;
use katana_rpc_api::katana::KatanaApiServer;
use katana_rpc_api::starknet::StarknetApiServer;
use katana_rpc_api::ApiKind;
use tower_http::cors::{Any, CorsLayer};

use crate::dev::DevApi;
use crate::katana::KatanaApi;
use crate::starknet::StarknetApi;

Expand All @@ -36,6 +39,9 @@ pub async fn spawn(sequencer: Arc<KatanaSequencer>, config: ServerConfig) -> Res
ApiKind::Katana => {
methods.merge(KatanaApi::new(sequencer.clone()).into_rpc())?;
}
ApiKind::Dev => {
methods.merge(DevApi::new(sequencer.clone()).into_rpc())?;
}
}
}

Expand Down

0 comments on commit ca3b018

Please sign in to comment.