Skip to content

Commit

Permalink
Add WalletSyncApi (MystenLabs#2733)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored Jun 27, 2022
1 parent ee3140b commit da6f8be
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
5 changes: 3 additions & 2 deletions crates/generate-json-rpc-spec/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ use sui_config::genesis_config::GenesisConfig;
use sui_config::SUI_WALLET_CONFIG;
use sui_json::SuiJsonValue;
use sui_json_rpc::bcs_api::BcsApiImpl;
use sui_json_rpc::gateway_api::{RpcGatewayImpl, TransactionBuilderImpl};
use sui_json_rpc::gateway_api::{GatewayWalletSyncApiImpl, RpcGatewayImpl, TransactionBuilderImpl};
use sui_json_rpc::read_api::{FullNodeApi, ReadApi};
use sui_json_rpc::sui_rpc_doc;
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::QuorumDriverApiClient;
use sui_json_rpc_api::RpcReadApiClient;
use sui_json_rpc_api::RpcTransactionBuilderClient;
use sui_json_rpc_api::TransactionBytes;
use sui_json_rpc_api::WalletSyncApiClient;
use sui_types::base_types::{ObjectID, SuiAddress};
use sui_types::sui_serde::{Base64, Encoding};
use sui_types::SUI_FRAMEWORK_ADDRESS;
Expand Down Expand Up @@ -84,6 +84,7 @@ async fn main() {
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(GatewayWalletSyncApiImpl::rpc_doc_module());

match options.action {
Action::Print => {
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-gateway/src/rpc_gateway_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use sui_json_rpc_api::QuorumDriverApiClient;
use sui_json_rpc_api::RpcBcsApiClient;
use sui_json_rpc_api::RpcTransactionBuilderClient;
use sui_json_rpc_api::TransactionBytes;
use sui_json_rpc_api::WalletSyncApiClient;
use sui_types::base_types::{ObjectID, SuiAddress, TransactionDigest};
use sui_types::messages::{Transaction, TransactionData};
use sui_types::sui_serde::Base64;
Expand Down Expand Up @@ -80,7 +81,7 @@ impl GatewayAPI for RpcGatewayClient {

async fn sync_account_state(&self, account_addr: SuiAddress) -> Result<(), Error> {
self.client
.quorum_driver()
.wallet_sync_api()
.sync_account_state(account_addr)
.await?;
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-json-rpc-api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::QuorumDriverApiClient;
use crate::RpcFullNodeReadApiClient;
use crate::RpcReadApiClient;
use crate::RpcTransactionBuilderClient;
use crate::WalletSyncApiClient;

pub struct SuiRpcClient {
client: HttpClient,
Expand All @@ -24,6 +25,9 @@ impl SuiRpcClient {
pub fn quorum_driver(&self) -> &impl QuorumDriverApiClient {
&self.client
}
pub fn wallet_sync_api(&self) -> &impl WalletSyncApiClient {
&self.client
}
pub fn full_node_read_api(&self) -> &impl RpcFullNodeReadApiClient {
&self.client
}
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-json-rpc-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ pub trait QuorumDriverApi {
signature: Base64,
pub_key: Base64,
) -> RpcResult<TransactionResponse>;
}

#[open_rpc(namespace = "sui", tag = "Wallet Sync API")]
#[rpc(server, client, namespace = "sui")]
pub trait WalletSyncApi {
/// Synchronize client state with validators.
#[method(name = "syncAccountState")]
async fn sync_account_state(&self, address: SuiAddress) -> RpcResult<()>;
Expand Down
29 changes: 27 additions & 2 deletions crates/sui-json-rpc/src/gateway_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sui_json_rpc_api::rpc_types::{
use sui_json_rpc_api::rpc_types::{RPCTransactionRequestParams, SuiTypeTag};
use sui_json_rpc_api::{
QuorumDriverApiServer, RpcReadApiServer, RpcTransactionBuilderServer, TransactionBytes,
WalletSyncApiServer,
};
use sui_open_rpc::Module;
use sui_types::sui_serde::Base64;
Expand All @@ -31,6 +32,10 @@ pub struct RpcGatewayImpl {
client: GatewayClient,
}

pub struct GatewayWalletSyncApiImpl {
client: GatewayClient,
}

pub struct GatewayReadApiImpl {
client: GatewayClient,
}
Expand All @@ -44,6 +49,13 @@ impl RpcGatewayImpl {
Self { client }
}
}

impl GatewayWalletSyncApiImpl {
pub fn new(client: GatewayClient) -> Self {
Self { client }
}
}

impl GatewayReadApiImpl {
pub fn new(client: GatewayClient) -> Self {
Self { client }
Expand Down Expand Up @@ -73,21 +85,34 @@ impl QuorumDriverApiServer for RpcGatewayImpl {
.await;
Ok(result?)
}
}

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

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

#[async_trait]
impl WalletSyncApiServer for GatewayWalletSyncApiImpl {
async fn sync_account_state(&self, address: SuiAddress) -> RpcResult<()> {
debug!("sync_account_state : {}", address);
self.client.sync_account_state(address).await?;
Ok(())
}
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@
"name": "sui_syncAccountState",
"tags": [
{
"name": "Quorum Driver API"
"name": "Wallet Sync API"
}
],
"description": "Synchronize client state with validators.",
Expand Down
1 change: 1 addition & 0 deletions crates/sui/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sui_json_rpc_api::rpc_types::{
};
use sui_json_rpc_api::{
QuorumDriverApiClient, RpcReadApiClient, RpcTransactionBuilderClient, TransactionBytes,
WalletSyncApiClient,
};
use sui_types::sui_serde::Base64;
use sui_types::{
Expand Down
6 changes: 5 additions & 1 deletion crates/test-utils/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ use sui_config::PersistedConfig;
use sui_config::{Config, SUI_GATEWAY_CONFIG, SUI_NETWORK_CONFIG, SUI_WALLET_CONFIG};
use sui_core::gateway_state::GatewayMetrics;
use sui_gateway::create_client;
use sui_json_rpc::gateway_api::{GatewayReadApiImpl, RpcGatewayImpl, TransactionBuilderImpl};
use sui_json_rpc::gateway_api::{
GatewayReadApiImpl, GatewayWalletSyncApiImpl, RpcGatewayImpl, TransactionBuilderImpl,
};
use sui_json_rpc_api::QuorumDriverApiServer;
use sui_json_rpc_api::RpcReadApiServer;
use sui_json_rpc_api::RpcTransactionBuilderServer;
use sui_json_rpc_api::WalletSyncApiServer;
use sui_swarm::memory::Swarm;
use sui_types::base_types::SuiAddress;
const NUM_VALIDAOTR: usize = 4;
Expand Down Expand Up @@ -114,6 +117,7 @@ async fn start_rpc_gateway(
module.merge(RpcGatewayImpl::new(client.clone()).into_rpc())?;
module.merge(GatewayReadApiImpl::new(client.clone()).into_rpc())?;
module.merge(TransactionBuilderImpl::new(client.clone()).into_rpc())?;
module.merge(GatewayWalletSyncApiImpl::new(client.clone()).into_rpc())?;

let handle = server.start(module)?;
Ok((addr, handle))
Expand Down

0 comments on commit da6f8be

Please sign in to comment.