Skip to content

Commit

Permalink
initialize grpc server
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Nov 25, 2024
1 parent da52a06 commit bbd7099
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ katana-codecs-derive = { path = "crates/katana/storage/codecs/derive" }
katana-core = { path = "crates/katana/core", default-features = false }
katana-db = { path = "crates/katana/storage/db" }
katana-executor = { path = "crates/katana/executor" }
katana-grpc = { path = "crates/katana/grpc" }
katana-node = { path = "crates/katana/node", default-features = false }
katana-node-bindings = { path = "crates/katana/node-bindings" }
katana-pipeline = { path = "crates/katana/pipeline" }
Expand Down
11 changes: 9 additions & 2 deletions crates/katana/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ repository.workspace = true
version.workspace = true

[dependencies]
tonic.workspace = true
katana-primitives.workspace = true

[dev-dependencies]
prost.workspace = true
thiserror.workspace = true
tonic.workspace = true

[build-dependencies]
tonic-build.workspace = true
wasm-tonic-build.workspace = true

[features]
client = [ ]
default = [ "server" ]
server = [ ]
5 changes: 3 additions & 2 deletions crates/katana/grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let feature_server = std::env::var("CARGO_FEATURE_SERVER");

tonic_build::configure()
.build_server(feature_server.is_ok())
.build_client(feature_client.is_ok())
// .build_server(feature_server.is_ok())
// .build_client(feature_client.is_ok())
.build_transport(true)
.file_descriptor_set_path(out_dir.join("starknet_descriptor.bin"))
.compile(&["proto/starknet.proto"], &["proto"])?;

Expand Down
2 changes: 1 addition & 1 deletion crates/katana/grpc/proto/starknet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ message BlockHashAndNumberResponse {
message ChainIdRequest {}

message ChainIdResponse {
string chain_id = 1;
types.Felt chain_id = 1;
}

message SyncingRequest {}
Expand Down
7 changes: 6 additions & 1 deletion crates/katana/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ message BlockID {
oneof identifier {
uint64 number = 1 [json_name = "block_number"];
Felt hash = 2 [json_name = "block_hash"];
string tag = 3 [json_name = "block_tag"];
BlockTag tag = 3 [json_name = "block_tag"];
}
}

enum BlockTag {
PENDING = 0;
LATEST = 1;
}

enum SimulationFlag {
SKIP_FEE_CHARGE = 0;
SKIP_EXECUTE = 1;
Expand Down
78 changes: 77 additions & 1 deletion crates/katana/grpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
//! gRPC implementations.
use katana_primitives::{
block::{BlockIdOrTag, BlockTag},
ContractAddress, Felt,
};

pub mod api {
tonic::include_proto!("starknet");
}

pub mod types {
tonic::include_proto!("types");
}

pub use api::starknet_server::Starknet as StarknetApi;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Decode(#[from] ::prost::DecodeError),
}

impl TryFrom<types::Felt> for Felt {
type Error = Error;

fn try_from(value: types::Felt) -> Result<Self, Self::Error> {
if value.value.len() > 32 {
panic!("doesn't fit")
}

Ok(Self::from_bytes_be_slice(&value.value))
}
}

impl From<Felt> for types::Felt {
fn from(value: Felt) -> Self {
Self { value: value.to_bytes_be().to_vec() }
}
}

impl TryFrom<types::Felt> for ContractAddress {
type Error = Error;

fn try_from(value: types::Felt) -> Result<Self, Self::Error> {
Ok(Self::new(Felt::try_from(value)?))
}
}

impl From<types::BlockTag> for BlockTag {
fn from(value: types::BlockTag) -> Self {
match value {
types::BlockTag::Latest => Self::Latest,
types::BlockTag::Pending => Self::Pending,
}
}
}

impl TryFrom<types::BlockId> for BlockIdOrTag {
type Error = Error;

fn try_from(value: types::BlockId) -> Result<Self, Self::Error> {
use types::block_id::Identifier;

let Some(id) = value.identifier else { panic!("missing id") };

match id {
Identifier::Number(num) => Ok(Self::Number(num)),
Identifier::Hash(hash) => {
let felt = Felt::try_from(hash)?;
Ok(Self::Hash(felt))
}
Identifier::Tag(tag) => {
let tag = types::BlockTag::try_from(tag)?;
Ok(Self::Tag(BlockTag::from(tag)))
}
}
}
}
3 changes: 3 additions & 0 deletions crates/katana/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ version.workspace = true

[dependencies]
anyhow.workspace = true
async-trait.workspace = true
dojo-metrics.workspace = true
futures.workspace = true
jsonrpsee = { workspace = true, features = [ "server" ] }
katana-core.workspace = true
katana-executor.workspace = true
katana-grpc.workspace = true
katana-pool.workspace = true
katana-primitives.workspace = true
katana-provider.workspace = true
Expand All @@ -24,6 +26,7 @@ metrics.workspace = true
starknet.workspace = true
thiserror.workspace = true
tokio.workspace = true
tonic.workspace = true
tracing.workspace = true
url.workspace = true

Expand Down
Loading

0 comments on commit bbd7099

Please sign in to comment.