Skip to content

Commit

Permalink
event read api and refactor api server builder (MystenLabs#2831)
Browse files Browse the repository at this point in the history
  • Loading branch information
longbowlu authored Jun 29, 2022
1 parent cae031c commit 317485a
Show file tree
Hide file tree
Showing 11 changed files with 1,339 additions and 706 deletions.
6 changes: 4 additions & 2 deletions crates/generate-json-rpc-spec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use sui_json_rpc::SuiRpcModule;
use sui_json_rpc_api::rpc_types::{
GetObjectDataResponse, SuiObjectInfo, TransactionEffectsResponse, TransactionResponse,
};
use sui_json_rpc_api::EventApiOpenRpc;
use sui_json_rpc_api::EventReadApiOpenRpc;
use sui_json_rpc_api::EventStreamingApiOpenRpc;
use sui_json_rpc_api::RpcReadApiClient;
use sui_json_rpc_api::RpcTransactionBuilderClient;
use sui_json_rpc_api::TransactionBytes;
Expand Down Expand Up @@ -83,7 +84,8 @@ async fn main() {
open_rpc.add_module(ReadApi::rpc_doc_module());
open_rpc.add_module(FullNodeApi::rpc_doc_module());
open_rpc.add_module(BcsApiImpl::rpc_doc_module());
open_rpc.add_module(EventApiOpenRpc::module_doc());
open_rpc.add_module(EventStreamingApiOpenRpc::module_doc());
open_rpc.add_module(EventReadApiOpenRpc::module_doc());
open_rpc.add_module(GatewayWalletSyncApiImpl::rpc_doc_module());

match options.action {
Expand Down
58 changes: 57 additions & 1 deletion crates/sui-json-rpc-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,63 @@ impl TransactionBytes {

#[open_rpc(namespace = "sui", tag = "Event Subscription")]
#[rpc(server, client, namespace = "sui")]
pub trait EventApi {
pub trait EventStreamingApi {
#[subscription(name = "subscribeEvent", item = SuiEventEnvelope)]
fn subscribe_event(&self, filter: SuiEventFilter);
}

#[open_rpc(namespace = "sui", tag = "Event Read API")]
#[rpc(server, client, namespace = "sui")]
pub trait EventReadApi {
#[method(name = "getEventsByTransaction")]
async fn get_events_by_transaction(
&self,
digest: TransactionDigest,
) -> RpcResult<Vec<SuiEventEnvelope>>;

#[method(name = "getEventsByModule")]
async fn get_events_by_module(
&self,
package: ObjectID,
module: String,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>>;

#[method(name = "getEventsByEventType")]
async fn get_events_by_event_type(
&self,
event_type: String,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>>;

#[method(name = "getEventsBySender")]
async fn get_events_by_sender(
&self,
sender: SuiAddress,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>>;

#[method(name = "getEventsByObject")]
async fn get_events_by_object(
&self,
object: ObjectID,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>>;

#[method(name = "getEventsByOwner")]
async fn get_events_by_owner(
&self,
owner: SuiAddress,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>>;
}
122 changes: 112 additions & 10 deletions crates/sui-json-rpc/src/event_api.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::fmt::Display;
use std::sync::Arc;

use crate::SuiRpcModule;
use async_trait::async_trait;
use futures::{StreamExt, TryStream};
use jsonrpsee::core::RpcResult;
use jsonrpsee_core::error::SubscriptionClosed;
use jsonrpsee_core::server::rpc_module::RpcModule;
use jsonrpsee_core::server::rpc_module::{PendingSubscription, SubscriptionSink};
use serde::Serialize;
use tracing::warn;

use std::fmt::Display;
use std::sync::Arc;
use sui_core::authority::AuthorityState;
use sui_core::event_handler::EventHandler;
use sui_json_rpc_api::rpc_types::{SuiEvent, SuiEventEnvelope, SuiEventFilter};
use sui_json_rpc_api::EventApiServer;
use sui_json_rpc_api::EventReadApiServer;
use sui_json_rpc_api::EventStreamingApiServer;
use sui_open_rpc::Module;
use sui_types::base_types::{ObjectID, SuiAddress, TransactionDigest};
use tracing::warn;

pub struct EventApiImpl {
pub struct EventStreamingApiImpl {
state: Arc<AuthorityState>,
event_handler: Arc<EventHandler>,
}

impl EventApiImpl {
impl EventStreamingApiImpl {
pub fn new(state: Arc<AuthorityState>, event_handler: Arc<EventHandler>) -> Self {
Self {
state,
Expand All @@ -29,7 +33,8 @@ impl EventApiImpl {
}
}

impl EventApiServer for EventApiImpl {
#[async_trait]
impl EventStreamingApiServer for EventStreamingApiImpl {
fn subscribe_event(&self, pending: PendingSubscription, filter: SuiEventFilter) {
let filter = match filter.try_into() {
Ok(filter) => filter,
Expand Down Expand Up @@ -77,3 +82,100 @@ where
};
});
}

impl SuiRpcModule for EventStreamingApiImpl {
fn rpc(self) -> RpcModule<Self> {
self.into_rpc()
}

fn rpc_doc_module() -> Module {
sui_json_rpc_api::EventStreamingApiOpenRpc::module_doc()
}
}

#[allow(unused)]
pub struct EventReadApiImpl {
state: Arc<AuthorityState>,
event_handler: Arc<EventHandler>,
}

impl EventReadApiImpl {
pub fn new(state: Arc<AuthorityState>, event_handler: Arc<EventHandler>) -> Self {
Self {
state,
event_handler,
}
}
}

#[allow(unused)]
#[async_trait]
impl EventReadApiServer for EventReadApiImpl {
async fn get_events_by_transaction(
&self,
digest: TransactionDigest,
) -> RpcResult<Vec<SuiEventEnvelope>> {
Ok(vec![])
}

async fn get_events_by_module(
&self,
package: ObjectID,
module: String,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>> {
Ok(vec![])
}

async fn get_events_by_event_type(
&self,
event_type: String,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>> {
Ok(vec![])
}

async fn get_events_by_sender(
&self,
sender: SuiAddress,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>> {
Ok(vec![])
}

async fn get_events_by_object(
&self,
object: ObjectID,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>> {
Ok(vec![])
}

async fn get_events_by_owner(
&self,
owner: SuiAddress,
count: u64,
start_time: u64,
end_time: u64,
) -> RpcResult<Vec<SuiEventEnvelope>> {
Ok(vec![])
}
}

impl SuiRpcModule for EventReadApiImpl {
fn rpc(self) -> RpcModule<Self> {
self.into_rpc()
}

fn rpc_doc_module() -> Module {
sui_json_rpc_api::EventReadApiOpenRpc::module_doc()
}
}
Loading

0 comments on commit 317485a

Please sign in to comment.