Skip to content

Commit

Permalink
refactor(torii-grpc): gzip compression & query opt (#2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo authored Oct 18, 2024
1 parent 1d67c67 commit 90a6580
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 344 deletions.
202 changes: 88 additions & 114 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,16 @@ warp = "0.3"

# gRPC
prost = "0.12"
tonic = { version = "0.11", features = [ "tls", "tls-roots" ] }
tonic = { version = "0.11", features = [ "tls", "tls-roots", "gzip" ] }
tonic-build = "0.11"
tonic-reflection = "0.11"
tonic-web = "0.11"

# WASM-compatible gRPC deps
tonic-web-wasm-client = "0.4.0"
wasm-prost = { version = "0.11.9", package = "prost" }
wasm-tonic = { version = "0.9.2", default-features = false, features = [ "codegen", "gzip", "prost" ], package = "tonic" }
wasm-tonic-build = { version = "0.9.2", default-features = false, features = [ "prost" ], package = "tonic-build" }
tonic-web-wasm-client = "0.6.0"
wasm-prost = { version = "0.13", package = "prost" }
wasm-tonic = { version = "0.12", default-features = false, features = [ "codegen", "gzip", "prost" ], package = "tonic" }
wasm-tonic-build = { version = "0.12", default-features = false, features = [ "prost" ], package = "tonic-build" }

alloy-primitives = { version = "0.8.3", default-features = false }
alloy-sol-types = { version = "0.8.3", default-features = false }
Expand Down
8 changes: 4 additions & 4 deletions crates/dojo-types/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub struct EnumOption {
}

impl Enum {
pub fn option(&self) -> Result<String, EnumError> {
pub fn option(&self) -> Result<&EnumOption, EnumError> {
let option: usize = if let Some(option) = self.option {
option as usize
} else {
Expand All @@ -338,7 +338,7 @@ impl Enum {
return Err(EnumError::OptionInvalid);
}

Ok(self.options[option].name.clone())
Ok(&self.options[option])
}

pub fn set_option(&mut self, name: &str) -> Result<(), EnumError> {
Expand All @@ -352,7 +352,7 @@ impl Enum {
}

pub fn to_sql_value(&self) -> Result<String, EnumError> {
self.option()
self.option().map(|option| option.name.clone())
}
}

Expand Down Expand Up @@ -448,7 +448,7 @@ fn format_member(m: &Member) -> String {
}
} else if let Ty::Enum(e) = &m.ty {
match e.option() {
Ok(option) => str.push_str(&format!(" = {option}")),
Ok(option) => str.push_str(&format!(" = {}", option.name)),
Err(_) => str.push_str(" = Invalid Option"),
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/torii/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ wasm-tonic.workspace = true

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
prost.workspace = true
tower-http.workspace = true
http.workspace = true
sqlx.workspace = true
tokio.workspace = true
tokio-stream = "0.1.14"
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.build_server(false)
.build_client(feature_client.is_ok())
.file_descriptor_set_path(out_dir.join("world_descriptor.bin"))
.compile(&["proto/world.proto"], &["proto"])?;
.compile_protos(&["proto/world.proto"], &["proto"])?;
} else {
tonic_build::configure()
.build_server(feature_server.is_ok())
Expand Down
49 changes: 18 additions & 31 deletions crates/torii/grpc/proto/schema.proto
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
syntax = "proto3";
package types;

enum PrimitiveType {
U8 = 0;
U16 = 1;
U32 = 2;
U64 = 3;
U128 = 4;
U256 = 5;
USIZE = 6;
BOOL = 7;
FELT252 = 8;
CLASS_HASH = 9;
CONTRACT_ADDRESS = 10;
I8 = 11;
I16 = 12;
I32 = 13;
I64 = 14;
I128 = 15;
}

message EnumOption {
string name = 1;
Ty ty = 2;
Expand All @@ -32,8 +13,24 @@ message Enum {
}

message Primitive {
PrimitiveType type = 1;
Value value = 2;
oneof primitive_type {
int32 i8 = 1;
int32 i16 = 2;
int32 i32 = 3;
int64 i64 = 4;
bytes i128 = 5;
uint32 u8 = 6;
uint32 u16 = 7;
uint32 u32 = 8;
uint64 u64 = 9;
bytes u128 = 10;
bytes u256 = 11;
uint32 usize = 12;
bool bool = 13;
bytes felt252 = 14;
bytes class_hash = 15;
bytes contract_address = 16;
}
}

message Struct {
Expand Down Expand Up @@ -61,13 +58,3 @@ message Member {
Ty ty = 2;
bool key = 3;
}

message Value {
oneof value_type {
string string_value = 2;
int64 int_value = 3;
uint64 uint_value = 4;
bool bool_value = 5;
bytes byte_value = 6;
}
}
1 change: 1 addition & 0 deletions crates/torii/grpc/proto/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ message Query {
Clause clause = 1;
uint32 limit = 2;
uint32 offset = 3;
bool dont_include_hashed_keys = 4;
}

message EventQuery {
Expand Down
8 changes: 8 additions & 0 deletions crates/torii/grpc/proto/world.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ service World {
// Retrieve entities
rpc RetrieveEntities (RetrieveEntitiesRequest) returns (RetrieveEntitiesResponse);

// Retrieve entities as a stream
rpc RetrieveEntitiesStreaming (RetrieveEntitiesRequest) returns (stream RetrieveEntitiesStreamingResponse);

// Subscribe to entity updates.
rpc SubscribeEventMessages (SubscribeEntitiesRequest) returns (stream SubscribeEntityResponse);

Expand Down Expand Up @@ -98,6 +101,11 @@ message RetrieveEntitiesResponse {
uint32 total_count = 2;
}

message RetrieveEntitiesStreamingResponse {
types.Entity entity = 1;
uint32 remaining_count = 2;
}

message RetrieveEventsRequest {
// The events to retrieve
types.EventQuery query = 1;
Expand Down
9 changes: 7 additions & 2 deletions crates/torii/grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::num::ParseIntError;
use futures_util::stream::MapOk;
use futures_util::{Stream, StreamExt, TryStreamExt};
use starknet::core::types::{Felt, FromStrError, StateDiff, StateUpdate};
use tonic::codec::CompressionEncoding;
#[cfg(not(target_arch = "wasm32"))]
use tonic::transport::Endpoint;

Expand Down Expand Up @@ -55,7 +56,9 @@ impl WorldClient {
let channel = endpoint.connect().await.map_err(Error::Transport)?;
Ok(Self {
_world_address: world_address,
inner: world_client::WorldClient::with_origin(channel, endpoint.uri().clone()),
inner: world_client::WorldClient::with_origin(channel, endpoint.uri().clone())
.accept_compressed(CompressionEncoding::Gzip)
.send_compressed(CompressionEncoding::Gzip),
})
}

Expand All @@ -64,7 +67,9 @@ impl WorldClient {
pub async fn new(endpoint: String, _world_address: Felt) -> Result<Self, Error> {
Ok(Self {
_world_address,
inner: world_client::WorldClient::new(tonic_web_wasm_client::Client::new(endpoint)),
inner: world_client::WorldClient::new(tonic_web_wasm_client::Client::new(endpoint))
.accept_compressed(CompressionEncoding::Gzip)
.send_compressed(CompressionEncoding::Gzip),
})
}

Expand Down
Loading

0 comments on commit 90a6580

Please sign in to comment.