Skip to content

Commit

Permalink
[GatewayAPI] - Moving SuiJson to core and use it in the GatewayAPI (M…
Browse files Browse the repository at this point in the history
…ystenLabs#1652)

* moving SuiJson to core and use it in the GatewayAPI

* * SuiJson now work with type arg
* fixed a bug in adapter.rs which prevent fn<T>(T) move function working.
* Changed JSON-RPC server to accept TypeTagString instead of TypeTag struct

* update rpc spec

* address PR comments

* fixup after rebase
  • Loading branch information
patrickkuo authored May 3, 2022
1 parent c3ee146 commit caaf36a
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 209 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

28 changes: 15 additions & 13 deletions doc/src/build/json-rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,19 @@ the [Move](move.md#move) language):
```shell
curl --location --request POST $SUI_RPC_HOST \
--header 'Content-Type: application/json' \
--data-raw '{ "jsonrpc":"2.0",
"method":"sui_moveCall",
"params":["{{owner_address}}",
"0x2",
"GAS",
"transfer",
[],
["Pure": "{{coin_object_id_base64}}", "Pure": "{{to_address_base64}}"}],
"{{gas_object_id}}",
2000],
"id":1}' | json_pp
--data-raw '{ "jsonrpc": "2.0",
"method": "sui_moveCall",
"params": [
"{{owner_address}}",
"0000000000000000000000000000000000000002",
"Coin",
"transfer_",
["0x2::SUI::SUI"],
["0x{{coin_object_id}}",10000, "0x{{recipient_address}}"],
"{{gas_object_id}}",
2000
],
"id": 1 }' | json_pp
```

#### 2, Sign the transaction
Expand All @@ -221,13 +223,13 @@ signature. Gas usage is capped by the gas_budget. The `transfer`
function is described in more detail in
the [Sui Wallet](wallet.md#calling-move-code) documentation.

Calling the `transfer` function in the `GAS` module serves the same
Calling the `transfer_` function in the `Coin` module serves the same
purpose as the native coin transfer ([`sui_transferCoin`](#sui_transfercoin)), and is mostly used for illustration
purposes as native transfer is more efficient when it's applicable
(i.e., we are transferring coins rather than non-coin
objects). Consequently, you should fill out argument placeholders
(`{{owner_address}}`, `{{coin_object_id}`, etc.) the same way you
would for [`sui_transferCoin`](#sui_transfercoin) - please not additional
would for [`sui_transferCoin`](#sui_transfercoin) - please note additional
`0x` prepended to function arguments.

To learn more about what `args` are accepted in a Move call, refer to the [SuiJSON](sui-json.md) documentation.
Expand Down
49 changes: 7 additions & 42 deletions sui/open_rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@
"name": "type_arguments",
"summary": "",
"description": "",
"required": true,
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/TypeTag"
"$ref": "#/components/schemas/TypeTagString"
}
}
},
Expand All @@ -160,7 +161,7 @@
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/RpcCallArg"
"$ref": "#/components/schemas/SuiJsonValue"
}
}
},
Expand Down Expand Up @@ -1456,46 +1457,6 @@
}
}
},
"RpcCallArg": {
"oneOf": [
{
"type": "object",
"required": [
"Pure"
],
"properties": {
"Pure": {
"$ref": "#/components/schemas/Base64"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"ImmOrOwnedObject"
],
"properties": {
"ImmOrOwnedObject": {
"$ref": "#/components/schemas/ObjectID"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"SharedObject"
],
"properties": {
"SharedObject": {
"$ref": "#/components/schemas/ObjectID"
}
},
"additionalProperties": false
}
]
},
"SequenceNumber": {
"type": "integer",
"format": "uint64",
Expand Down Expand Up @@ -1637,6 +1598,7 @@
"SuiAddress": {
"$ref": "#/components/schemas/Hex"
},
"SuiJsonValue": {},
"TransactionBytes": {
"type": "object",
"required": [
Expand Down Expand Up @@ -2099,6 +2061,9 @@
"additionalProperties": false
}
]
},
"TypeTagString": {
"type": "string"
}
}
}
Expand Down
1 change: 0 additions & 1 deletion sui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ pub mod rpc_gateway;
pub mod rpc_gateway_client;
pub mod shell;
pub mod sui_commands;
pub mod sui_json;
pub mod wallet_commands;
36 changes: 13 additions & 23 deletions sui/src/rpc_gateway.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::rpc_gateway::responses::SuiTypeTag;
use crate::{
config::{GatewayConfig, PersistedConfig},
rpc_gateway::responses::{GetObjectInfoResponse, NamedObjectRef, ObjectResponse},
Expand All @@ -10,7 +11,7 @@ use async_trait::async_trait;
use ed25519_dalek::ed25519::signature::Signature;
use jsonrpsee::core::RpcResult;
use jsonrpsee_proc_macros::rpc;
use move_core_types::{identifier::Identifier, language_storage::TypeTag};
use move_core_types::identifier::Identifier;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::{base64, serde_as};
Expand All @@ -19,14 +20,15 @@ use sui_core::gateway_state::{
gateway_responses::{TransactionEffectsResponse, TransactionResponse},
GatewayClient, GatewayState, GatewayTxSeqNumber,
};
use sui_core::sui_json::SuiJsonValue;
use sui_open_rpc_macros::open_rpc;
use sui_types::{
base_types::{ObjectID, SuiAddress, TransactionDigest},
crypto,
crypto::SignableBytes,
json_schema,
json_schema::Base64,
messages::{CallArg, Transaction, TransactionData},
messages::{Transaction, TransactionData},
object::ObjectRead,
};
use tracing::debug;
Expand Down Expand Up @@ -75,10 +77,8 @@ pub trait RpcGateway {
package_object_id: ObjectID,
#[schemars(with = "json_schema::Identifier")] module: Identifier,
#[schemars(with = "json_schema::Identifier")] function: Identifier,
#[schemars(with = "Option<Vec<json_schema::TypeTag>>")] type_arguments: Option<
Vec<TypeTag>,
>,
arguments: Vec<RpcCallArg>,
type_arguments: Vec<SuiTypeTag>,
arguments: Vec<SuiJsonValue>,
gas_object_id: ObjectID,
gas_budget: u64,
) -> RpcResult<TransactionBytes>;
Expand Down Expand Up @@ -313,8 +313,8 @@ impl RpcGatewayServer for RpcGatewayImpl {
package_object_id: ObjectID,
module: Identifier,
function: Identifier,
type_arguments: Option<Vec<TypeTag>>,
rpc_arguments: Vec<RpcCallArg>,
type_arguments: Vec<SuiTypeTag>,
rpc_arguments: Vec<SuiJsonValue>,
gas_object_id: ObjectID,
gas_budget: u64,
) -> RpcResult<TransactionBytes> {
Expand All @@ -331,28 +331,18 @@ impl RpcGatewayServer for RpcGatewayImpl {
.await?
.reference()?;

// Fetch the objects for the object args
let mut arguments = Vec::with_capacity(rpc_arguments.len());
for rpc_arg in rpc_arguments {
arguments.push(match rpc_arg {
RpcCallArg::Pure(arg) => CallArg::Pure(arg.to_vec()),
RpcCallArg::SharedObject(id) => CallArg::SharedObject(id),
RpcCallArg::ImmOrOwnedObject(id) => {
let object_ref = self.gateway.get_object_info(id).await?.reference()?;
CallArg::ImmOrOwnedObject(object_ref)
}
})
}

self.gateway
.move_call(
signer,
package_object_ref,
module,
function,
type_arguments.unwrap_or_default(),
type_arguments
.into_iter()
.map(|tag| tag.try_into())
.collect::<Result<Vec<_>, _>>()?,
rpc_arguments,
gas_obj_ref,
arguments,
gas_budget,
)
.await
Expand Down
20 changes: 19 additions & 1 deletion sui/src/rpc_gateway/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use anyhow::anyhow;
use base64ct::{Base64, Encoding};

use move_core_types::language_storage::TypeTag;
use move_core_types::parser::parse_type_tag;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -110,3 +111,20 @@ impl MoveObjectType {
}
}
}

#[derive(Serialize, Deserialize, JsonSchema)]
#[serde(rename = "TypeTagString")]
pub struct SuiTypeTag(String);

impl TryInto<TypeTag> for SuiTypeTag {
type Error = anyhow::Error;
fn try_into(self) -> Result<TypeTag, Self::Error> {
parse_type_tag(&self.0)
}
}

impl From<TypeTag> for SuiTypeTag {
fn from(tag: TypeTag) -> Self {
Self(format!("{}", tag))
}
}
29 changes: 12 additions & 17 deletions sui/src/rpc_gateway_client.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::rpc_gateway::responses::ObjectResponse;
use crate::rpc_gateway::{
RpcCallArg, RpcGatewayClient as RpcGateway, SignedTransaction, TransactionBytes,
};
use anyhow::Error;
use async_trait::async_trait;
use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::TypeTag;
use tokio::runtime::Handle;

use sui_core::gateway_state::gateway_responses::{TransactionEffectsResponse, TransactionResponse};
use sui_core::gateway_state::{GatewayAPI, GatewayTxSeqNumber};
use sui_core::sui_json::SuiJsonValue;
use sui_types::base_types::{ObjectID, ObjectRef, SuiAddress, TransactionDigest};
use sui_types::json_schema::Base64;
use sui_types::messages::{CallArg, Transaction, TransactionData};
use sui_types::messages::{Transaction, TransactionData};
use sui_types::object::ObjectRead;
use tokio::runtime::Handle;

use crate::rpc_gateway::responses::ObjectResponse;
use crate::rpc_gateway::{RpcGatewayClient as RpcGateway, SignedTransaction, TransactionBytes};

pub struct RpcGatewayClient {
client: HttpClient,
Expand Down Expand Up @@ -69,27 +70,21 @@ impl GatewayAPI for RpcGatewayClient {
module: Identifier,
function: Identifier,
type_arguments: Vec<TypeTag>,
arguments: Vec<SuiJsonValue>,
gas_object_ref: ObjectRef,
arguments: Vec<CallArg>,
gas_budget: u64,
) -> Result<TransactionData, Error> {
let arguments = arguments
.into_iter()
.map(|arg| match arg {
CallArg::Pure(bytes) => RpcCallArg::Pure(Base64(bytes)),
CallArg::ImmOrOwnedObject((id, _, _)) => RpcCallArg::ImmOrOwnedObject(id),
CallArg::SharedObject(id) => RpcCallArg::SharedObject(id),
})
.collect();

let bytes: TransactionBytes = self
.client
.move_call(
signer,
package_object_ref.0,
module,
function,
Some(type_arguments),
type_arguments
.into_iter()
.map(|tag| tag.try_into())
.collect::<Result<Vec<_>, _>>()?,
arguments,
gas_object_ref.0,
gas_budget,
Expand Down
15 changes: 9 additions & 6 deletions sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::cli_tests::sui_network::start_test_network;
use anyhow::anyhow;
use move_core_types::identifier::Identifier;
use serde_json::{json, Value};
use std::{
collections::BTreeSet, fmt::Write, fs::read_dir, ops::Add, path::PathBuf, str, time::Duration,
};

use anyhow::anyhow;
use move_core_types::identifier::Identifier;
use serde_json::{json, Value};
use tracing_test::traced_test;

use sui::{
config::{
AccountConfig, Config, GatewayConfig, GatewayType, GenesisConfig, NetworkConfig,
Expand All @@ -16,18 +18,19 @@ use sui::{
},
keystore::KeystoreType,
sui_commands::{SuiCommand, SuiNetwork, SUI_AUTHORITY_KEYS},
sui_json::SuiJsonValue,
wallet_commands::{WalletCommandResult, WalletCommands, WalletContext},
};
use sui_core::gateway_state::gateway_responses::SwitchResponse;
use sui_core::sui_json::SuiJsonValue;
use sui_types::{
base_types::{ObjectID, SequenceNumber, SuiAddress},
crypto::{get_key_pair, random_key_pairs},
gas_coin::GasCoin,
messages::TransactionEffects,
object::{Object, ObjectRead, GAS_VALUE_FOR_TESTING},
};
use tracing_test::traced_test;

use crate::cli_tests::sui_network::start_test_network;

const TEST_DATA_DIR: &str = "src/unit_tests/data/";
const AIRDROP_SOURCE_CONTRACT_ADDRESS: &str = "bc4ca0eda7647a8ab7c2061c2e118a18a936f13d";
Expand Down
Loading

0 comments on commit caaf36a

Please sign in to comment.