Skip to content

Commit

Permalink
[adapter][move] Relax object/pure arg ordering. remove return values (M…
Browse files Browse the repository at this point in the history
…ystenLabs#1474)

* [adapter][move] Relax object/pure arg ordering. remove return values

- Relaxed ordering on entry points, so object and pure arguments can be intermixed
- Added new enum CallArg to support this
- Removed return values from entry points
- Removed CallResult
  • Loading branch information
tnowacki authored Apr 21, 2022
1 parent 3b1f738 commit e312802
Show file tree
Hide file tree
Showing 32 changed files with 796 additions and 1,074 deletions.
7 changes: 4 additions & 3 deletions sui/src/benchmark/transaction_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ fn make_transfer_transaction(
module: ident_str!("SUI").to_owned(),
function: ident_str!("transfer").to_owned(),
type_arguments: Vec::new(),
object_arguments: vec![object_ref],
shared_object_arguments: vec![],
pure_arguments: vec![bcs::to_bytes(&AccountAddress::from(recipient)).unwrap()],
arguments: vec![
CallArg::ImmOrOwnedObject(object_ref),
CallArg::Pure(bcs::to_bytes(&AccountAddress::from(recipient)).unwrap()),
],
})
} else {
SingleTransactionKind::Transfer(Transfer {
Expand Down
32 changes: 13 additions & 19 deletions sui/src/rest_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde_json::json;
use sui_core::gateway_state::gateway_responses::TransactionResponse;
use sui_core::gateway_state::{GatewayAPI, GatewayTxSeqNumber};
use sui_types::base_types::{encode_bytes_hex, ObjectID, ObjectRef, SuiAddress, TransactionDigest};
use sui_types::messages::{CertifiedTransaction, Transaction, TransactionData};
use sui_types::messages::{CallArg, CertifiedTransaction, Transaction, TransactionData};
use sui_types::object::ObjectRead;

use crate::rest_gateway::requests::{
Expand All @@ -25,6 +25,8 @@ use crate::rest_gateway::requests::{
};
use crate::rest_gateway::responses::{NamedObjectRef, ObjectResponse, TransactionBytes};

use self::requests::CallRequestArg;

pub mod requests;
pub mod responses;

Expand Down Expand Up @@ -95,29 +97,23 @@ impl GatewayAPI for RestGatewayClient {
function: Identifier,
type_arguments: Vec<TypeTag>,
gas_object_ref: ObjectRef,
object_arguments: Vec<ObjectRef>,
shared_object_arguments: Vec<ObjectID>,
pure_arguments: Vec<Vec<u8>>,
arguments: Vec<CallArg>,
gas_budget: u64,
) -> Result<TransactionData, anyhow::Error> {
let type_arg = type_arguments
.iter()
.map(|arg| arg.to_string())
.collect::<Vec<_>>();

let object_arguments = object_arguments
.iter()
.map(|(object_id, _, _)| object_id.to_hex())
.collect();

let shared_object_arguments = shared_object_arguments
let arguments = arguments
.iter()
.map(|object_id| object_id.to_hex())
.collect();

let pure_arguments = pure_arguments
.iter()
.map(|s| Base64::encode_string(s))
.map(|arg| match arg {
CallArg::Pure(bytes) => CallRequestArg::Pure(Base64::encode_string(bytes)),
CallArg::ImmOrOwnedObject((id, _, _)) => {
CallRequestArg::ImmOrOwnedObject(id.to_hex())
}
CallArg::SharedObject(id) => CallRequestArg::SharedObject(id.to_hex()),
})
.collect();

let request = CallRequest {
Expand All @@ -126,11 +122,9 @@ impl GatewayAPI for RestGatewayClient {
module: module.into_string(),
function: function.into_string(),
type_arguments: Some(type_arg),
pure_arguments,
arguments,
gas_object_id: gas_object_ref.0.to_hex(),
gas_budget,
object_arguments,
shared_object_arguments,
};
let tx: TransactionBytes = self.post("move_call", request).await?;
Ok(tx.to_data()?)
Expand Down
19 changes: 13 additions & 6 deletions sui/src/rest_gateway/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ pub struct SignedTransaction {
pub pub_key: String,
}

/// Byte representation of the arguments
#[derive(Deserialize, Serialize, JsonSchema)]
pub enum CallRequestArg {
/// Base64 encoded non-object argument
Pure(String),
/// Object arguments
ImmOrOwnedObject(String),
/// Shared object arguments
SharedObject(String),
}

/// Request containing the information required to create a move module call transaction.
#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
Expand All @@ -62,16 +73,12 @@ pub struct CallRequest {
pub function: String,
/// Optional; The argument types to be parsed
pub type_arguments: Option<Vec<String>>,
/// Required; Byte representation of the arguments, Base64 encoded
pub pure_arguments: Vec<String>,
/// Required; Encoded representation of the arguments, both object and non-object
pub arguments: Vec<CallRequestArg>,
/// Required; Hex code as string representing the gas object id
pub gas_object_id: String,
/// Required; Gas budget required as a cap for gas usage
pub gas_budget: u64,
/// Required; Object arguments
pub object_arguments: Vec<String>,
/// Required; Share object arguments
pub shared_object_arguments: Vec<String>,
}

/// Request containing the address of which object are to be retrieved.
Expand Down
36 changes: 15 additions & 21 deletions sui/src/rest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use serde::{Deserialize, Serialize};
use sui::config::PersistedConfig;
use sui::gateway_config::GatewayConfig;
use sui::rest_gateway::requests::{
CallRequest, GetObjectInfoRequest, GetObjectSchemaRequest, GetObjectsRequest,
CallRequest, CallRequestArg, GetObjectInfoRequest, GetObjectSchemaRequest, GetObjectsRequest,
GetTransactionDetailsRequest, MergeCoinRequest, PublishRequest, SignedTransaction,
SplitCoinRequest, SyncRequest, TransferTransactionRequest,
};
Expand All @@ -40,7 +40,7 @@ use sui_core::gateway_state::{GatewayClient, GatewayState};
use sui_types::base_types::*;
use sui_types::crypto::SignableBytes;
use sui_types::crypto::{self};
use sui_types::messages::{CertifiedTransaction, Transaction, TransactionData};
use sui_types::messages::{CallArg, CertifiedTransaction, Transaction, TransactionData};
use sui_types::object::Object as SuiObject;
use sui_types::object::ObjectRead;

Expand Down Expand Up @@ -586,23 +586,19 @@ async fn handle_move_call(
let (gas_obj_ref, _, _) = get_object_info(gateway, gas_object_id).await?;

// Fetch the objects for the object args
let mut object_args_refs = Vec::new();
for obj_id in call_params.object_arguments {
let (object_ref, _, _) = get_object_info(gateway, ObjectID::try_from(obj_id)?).await?;
object_args_refs.push(object_ref);
let mut arguments = Vec::with_capacity(call_params.arguments.len());
for arg in call_params.arguments {
arguments.push(match arg {
CallRequestArg::Pure(arg) => {
CallArg::Pure(Base64::decode_vec(&arg).map_err(|e| anyhow!(e))?)
}
CallRequestArg::SharedObject(id) => CallArg::SharedObject(ObjectID::try_from(id)?),
CallRequestArg::ImmOrOwnedObject(id) => {
let (object_ref, _, _) = get_object_info(gateway, ObjectID::try_from(id)?).await?;
CallArg::ImmOrOwnedObject(object_ref)
}
})
}
let pure_arguments = call_params
.pure_arguments
.iter()
.map(|s| Base64::decode_vec(s))
.collect::<Result<_, _>>()
.map_err(|e| anyhow!(e))?;

let shared_object_arguments = call_params
.shared_object_arguments
.into_iter()
.map(ObjectID::try_from)
.collect::<Result<_, _>>()?;

gateway
.move_call(
Expand All @@ -612,9 +608,7 @@ async fn handle_move_call(
function.to_owned(),
type_args.clone(),
gas_obj_ref,
object_args_refs,
shared_object_arguments,
pure_arguments,
arguments,
gas_budget,
)
.await
Expand Down
38 changes: 21 additions & 17 deletions sui/src/rpc_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use move_core_types::language_storage::TypeTag;
use serde::Deserialize;
use serde::Serialize;
use serde_with::serde_as;
use sui_types::messages::CallArg;
use tracing::debug;

use serde_with::base64::Base64;
Expand All @@ -30,6 +31,13 @@ use crate::gateway_config::GatewayConfig;
use crate::rest_gateway::responses::GetObjectInfoResponse;
use crate::rest_gateway::responses::{NamedObjectRef, ObjectResponse};

#[derive(Serialize, Deserialize)]
pub enum RpcCallArg {
Pure(Base64EncodedBytes),
ImmOrOwnedObject(ObjectID),
SharedObject(ObjectID),
}

#[rpc(server, client, namespace = "sui")]
pub trait RpcGateway {
#[method(name = "getObjectTypedInfo")]
Expand All @@ -53,11 +61,9 @@ pub trait RpcGateway {
module: Identifier,
function: Identifier,
type_arguments: Vec<TypeTag>,
pure_arguments: Vec<Base64EncodedBytes>,
arguments: Vec<RpcCallArg>,
gas_object_id: ObjectID,
gas_budget: u64,
object_arguments: Vec<ObjectID>,
shared_object_arguments: Vec<ObjectID>,
) -> RpcResult<TransactionBytes>;

#[method(name = "publish")]
Expand Down Expand Up @@ -284,11 +290,9 @@ impl RpcGatewayServer for RpcGatewayImpl {
module: Identifier,
function: Identifier,
type_arguments: Vec<TypeTag>,
pure_arguments: Vec<Base64EncodedBytes>,
rpc_arguments: Vec<RpcCallArg>,
gas_object_id: ObjectID,
gas_budget: u64,
object_arguments: Vec<ObjectID>,
shared_object_arguments: Vec<ObjectID>,
) -> RpcResult<TransactionBytes> {
let data = async {
let package_object_ref = self
Expand All @@ -304,15 +308,17 @@ impl RpcGatewayServer for RpcGatewayImpl {
.reference()?;

// Fetch the objects for the object args
let mut object_args_refs = Vec::new();
for obj_id in object_arguments {
let object_ref = self.gateway.get_object_info(obj_id).await?.reference()?;
object_args_refs.push(object_ref);
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)
}
})
}
let pure_arguments = pure_arguments
.iter()
.map(|arg| arg.to_vec())
.collect::<Vec<_>>();

self.gateway
.move_call(
Expand All @@ -322,9 +328,7 @@ impl RpcGatewayServer for RpcGatewayImpl {
function,
type_arguments,
gas_obj_ref,
object_args_refs,
shared_object_arguments,
pure_arguments,
arguments,
gas_budget,
)
.await
Expand Down
22 changes: 11 additions & 11 deletions sui/src/rpc_gateway_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use crate::rest_gateway::responses::ObjectResponse;
use crate::rpc_gateway::{
Base64EncodedBytes, RpcGatewayClient as RpcGateway, SignedTransaction, TransactionBytes,
Base64EncodedBytes, RpcCallArg, RpcGatewayClient as RpcGateway, SignedTransaction,
TransactionBytes,
};
use anyhow::Error;
use async_trait::async_trait;
Expand All @@ -13,7 +14,7 @@ use move_core_types::language_storage::TypeTag;
use sui_core::gateway_state::gateway_responses::TransactionResponse;
use sui_core::gateway_state::{GatewayAPI, GatewayTxSeqNumber};
use sui_types::base_types::{ObjectID, ObjectRef, SuiAddress, TransactionDigest};
use sui_types::messages::{CertifiedTransaction, Transaction, TransactionData};
use sui_types::messages::{CallArg, CertifiedTransaction, Transaction, TransactionData};
use sui_types::object::ObjectRead;
use tokio::runtime::Handle;

Expand Down Expand Up @@ -69,15 +70,16 @@ impl GatewayAPI for RpcGatewayClient {
function: Identifier,
type_arguments: Vec<TypeTag>,
gas_object_ref: ObjectRef,
object_arguments: Vec<ObjectRef>,
shared_object_arguments: Vec<ObjectID>,
pure_arguments: Vec<Vec<u8>>,
arguments: Vec<CallArg>,
gas_budget: u64,
) -> Result<TransactionData, Error> {
let pure_arguments = pure_arguments.into_iter().map(Base64EncodedBytes).collect();
let object_arguments = object_arguments
let arguments = arguments
.into_iter()
.map(|object_ref| object_ref.0)
.map(|arg| match arg {
CallArg::Pure(bytes) => RpcCallArg::Pure(Base64EncodedBytes(bytes)),
CallArg::ImmOrOwnedObject((id, _, _)) => RpcCallArg::ImmOrOwnedObject(id),
CallArg::SharedObject(id) => RpcCallArg::SharedObject(id),
})
.collect();

let bytes: TransactionBytes = self
Expand All @@ -88,11 +90,9 @@ impl GatewayAPI for RpcGatewayClient {
module,
function,
type_arguments,
pure_arguments,
arguments,
gas_object_ref.0,
gas_budget,
object_arguments,
shared_object_arguments,
)
.await?;
bytes.to_data()
Expand Down
Loading

0 comments on commit e312802

Please sign in to comment.