Skip to content

Commit

Permalink
[JSON-RPC] - RPC Gateway server using paritytech/jsonrpsee (MystenLab…
Browse files Browse the repository at this point in the history
…s#1256)

* json-rpc server using Parity JSON-RPC

* ported over a few more endpoints

* use jsonrpsee

* add rest of the endpoints

* RpcGatewayClient impl for wallet to communicate with the JSON-RPC server

* add unit tests

* rename endpoints

* add more logs
add ACCESS_CONTROL_ALLOW_ORIGIN support via env var
change method names to camel case and module name to sui

* address PR comments

* address PR comments

* remove unnecessary mut ref from gateway api and removed Arc Mutex wrap from RPCGateway

* fixup after rebase

* address PR comments

* rename bin to rpc-server

* add openssl to allow list

* deny.toml fix
  • Loading branch information
patrickkuo authored Apr 19, 2022
1 parent 90afabd commit 1fe5b58
Show file tree
Hide file tree
Showing 20 changed files with 1,039 additions and 66 deletions.
14 changes: 7 additions & 7 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ allow = [
"MPL-2.0",
"ISC",
"CC0-1.0",
# "LicenseRef-ring",
"LicenseRef-ring",
"BSL-1.0",
#"Apache-2.0 WITH LLVM-exception",
]
Expand Down Expand Up @@ -139,12 +139,12 @@ exceptions = [
# Each entry is a crate relative path, and the (opaque) hash of its contents
#{ path = "LICENSE", hash = 0xbd0eed23 }
#]
# [[licenses.clarify]]
# name = "ring"
# expression = "LicenseRef-ring"
# license-files = [
# { path = "LICENSE", hash = 0xbd0eed23 },
# ]
[[licenses.clarify]]
name = "ring"
expression = "LicenseRef-ring"
license-files = [
{ path = "LICENSE", hash = 0xbd0eed23 },
]
[[licenses.clarify]]
name = "encoding_rs"
version = "*"
Expand Down
10 changes: 9 additions & 1 deletion sui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ move-bytecode-utils = { git = "https://github.com/move-language/move", rev = "2e
move-unit-test = { git = "https://github.com/move-language/move", rev = "2e7c37edada44436805e047dd26724a26c07635a" }

once_cell = "1.9.0"
reqwest = { version = "0.11.10", features=["json","serde_json", "blocking"]}
reqwest = { version = "0.11.10", features = ["json", "serde_json", "blocking"] }

jsonrpsee = { version = "0.10.1", features = ["full"] }

jsonrpsee-proc-macros = "0.10.1"

[dev-dependencies]
tracing-test = "0.2.1"
Expand All @@ -86,6 +90,10 @@ path = "src/sui-move.rs"
name = "rest_server"
path = "src/rest_server.rs"

[[bin]]
name = "rpc-server"
path = "src/rpc_server.rs"

[[bin]]
name = "validator"
path = "src/validator.rs"
2 changes: 1 addition & 1 deletion sui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub const DEFAULT_STARTING_PORT: u16 = 10000;
static PORT_ALLOCATOR: Lazy<Mutex<PortAllocator>> =
Lazy::new(|| Mutex::new(PortAllocator::new(DEFAULT_STARTING_PORT)));

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AuthorityInfo {
#[serde(serialize_with = "bytes_as_hex", deserialize_with = "bytes_from_hex")]
pub name: AuthorityName,
Expand Down
10 changes: 8 additions & 2 deletions sui/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ use sui_network::network::NetworkClient;
use sui_network::transport;
use sui_types::base_types::AuthorityName;
use sui_types::committee::Committee;
use sui_types::error::SuiResult;

use crate::config::{AuthorityInfo, Config};
use crate::rest_gateway::RestGatewayClient;
use crate::rpc_gateway_client::RpcGatewayClient;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum GatewayType {
Embedded(GatewayConfig),
Rest(String),
RPC(String),
}

impl Display for GatewayType {
Expand Down Expand Up @@ -54,13 +55,17 @@ impl Display for GatewayType {
writeln!(writer, "Gateway Type : RestAPI")?;
writeln!(writer, "Gateway URL : {}", url)?;
}
GatewayType::RPC(url) => {
writeln!(writer, "Gateway Type : JSON-RPC")?;
writeln!(writer, "Gateway URL : {}", url)?;
}
}
write!(f, "{}", writer)
}
}

impl GatewayType {
pub fn init(&self) -> SuiResult<GatewayClient> {
pub fn init(&self) -> Result<GatewayClient, anyhow::Error> {
Ok(match self {
GatewayType::Embedded(config) => {
let path = config.db_folder_path.clone();
Expand All @@ -69,6 +74,7 @@ impl GatewayType {
Box::new(GatewayState::new(path, committee, authority_clients)?)
}
GatewayType::Rest(url) => Box::new(RestGatewayClient { url: url.clone() }),
GatewayType::RPC(url) => Box::new(RpcGatewayClient::new(url.clone())?),
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions sui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub mod config;
pub mod gateway;
pub mod keystore;
pub mod rest_gateway;
pub mod rpc_gateway;
pub mod rpc_gateway_client;
pub mod shell;
pub mod sui_commands;
pub mod sui_json;
Expand Down
14 changes: 7 additions & 7 deletions sui/src/rest_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct RestGatewayClient {
#[allow(unused_variables)]
impl GatewayAPI for RestGatewayClient {
async fn execute_transaction(
&mut self,
&self,
tx: Transaction,
) -> Result<TransactionResponse, anyhow::Error> {
let url = format!("{}/api/", self.url);
Expand All @@ -54,7 +54,7 @@ impl GatewayAPI for RestGatewayClient {
}

async fn transfer_coin(
&mut self,
&self,
signer: SuiAddress,
object_id: ObjectID,
gas_payment: ObjectID,
Expand Down Expand Up @@ -87,7 +87,7 @@ impl GatewayAPI for RestGatewayClient {
}

async fn move_call(
&mut self,
&self,
signer: SuiAddress,
package_object_ref: ObjectRef,
module: Identifier,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl GatewayAPI for RestGatewayClient {
}

async fn publish(
&mut self,
&self,
signer: SuiAddress,
package_bytes: Vec<Vec<u8>>,
gas_object_ref: ObjectRef,
Expand All @@ -151,7 +151,7 @@ impl GatewayAPI for RestGatewayClient {
}

async fn split_coin(
&mut self,
&self,
signer: SuiAddress,
coin_object_id: ObjectID,
split_amounts: Vec<u64>,
Expand All @@ -170,7 +170,7 @@ impl GatewayAPI for RestGatewayClient {
}

async fn merge_coins(
&mut self,
&self,
signer: SuiAddress,
primary_coin: ObjectID,
coin_to_merge: ObjectID,
Expand All @@ -195,7 +195,7 @@ impl GatewayAPI for RestGatewayClient {
}

async fn get_owned_objects(
&mut self,
&self,
account_addr: SuiAddress,
) -> Result<Vec<ObjectRef>, anyhow::Error> {
let url = format!("{}/api/objects?address={}", self.url, account_addr);
Expand Down
2 changes: 1 addition & 1 deletion sui/src/rest_gateway/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl TransactionBytes {
}

pub fn to_data(self) -> Result<TransactionData, anyhow::Error> {
TransactionData::from_signable_bytes(base64::decode(self.tx_bytes)?)
TransactionData::from_signable_bytes(&base64::decode(self.tx_bytes)?)
}
}

Expand Down
12 changes: 6 additions & 6 deletions sui/src/rest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async fn get_objects(
ctx: Arc<RequestContext<ServerContext>>,
query: Query<GetObjectsRequest>,
) -> Result<HttpResponseOk<ObjectResponse>, HttpError> {
let mut gateway = ctx.context().gateway.lock().await;
let gateway = ctx.context().gateway.lock().await;
let get_objects_params = query.into_inner();
let address = get_objects_params.address;
let address = &decode_bytes_hex(address.as_str()).map_err(|error| {
Expand Down Expand Up @@ -301,7 +301,7 @@ async fn new_transfer(
ctx: Arc<RequestContext<ServerContext>>,
request: TypedBody<TransferTransactionRequest>,
) -> Result<HttpResponseOk<TransactionBytes>, HttpError> {
let mut gateway = ctx.context().gateway.lock().await;
let gateway = ctx.context().gateway.lock().await;
let request = request.into_inner();

let tx_data = async {
Expand Down Expand Up @@ -334,7 +334,7 @@ async fn split_coin(
ctx: Arc<RequestContext<ServerContext>>,
request: TypedBody<SplitCoinRequest>,
) -> Result<HttpResponseOk<TransactionBytes>, HttpError> {
let mut gateway = ctx.context().gateway.lock().await;
let gateway = ctx.context().gateway.lock().await;
let request = request.into_inner();

let tx_data = async {
Expand Down Expand Up @@ -365,7 +365,7 @@ async fn merge_coin(
ctx: Arc<RequestContext<ServerContext>>,
request: TypedBody<MergeCoinRequest>,
) -> Result<HttpResponseOk<TransactionBytes>, HttpError> {
let mut gateway = ctx.context().gateway.lock().await;
let gateway = ctx.context().gateway.lock().await;
let request = request.into_inner();

let tx_data = async {
Expand Down Expand Up @@ -489,11 +489,11 @@ async fn execute_transaction(
response: TypedBody<SignedTransaction>,
) -> Result<HttpResponseOk<JsonResponse<TransactionResponse>>, HttpError> {
let response = response.into_inner();
let mut gateway = ctx.context().gateway.lock().await;
let gateway = ctx.context().gateway.lock().await;

let response: Result<_, anyhow::Error> = async {
let data = base64::decode(response.tx_bytes)?;
let data = TransactionData::from_signable_bytes(data)?;
let data = TransactionData::from_signable_bytes(&data)?;

let mut signature_bytes = base64::decode(response.signature)?;
let mut pub_key_bytes = base64::decode(response.pub_key)?;
Expand Down
Loading

0 comments on commit 1fe5b58

Please sign in to comment.