Skip to content

Commit

Permalink
[rpc] Parse Struct tag for RPC API endpoints (MystenLabs#7624)
Browse files Browse the repository at this point in the history
Fixed MystenLabs#6120 by normalizing user
input
  • Loading branch information
666lcz authored Jan 25, 2023
1 parent 71bee75 commit 334b2fe
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
4 changes: 3 additions & 1 deletion crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std::time::Duration;
use std::{collections::HashMap, pin::Pin, sync::Arc};
use sui_config::node::AuthorityStorePruningConfig;
use sui_protocol_constants::{MAX_TX_GAS, STORAGE_GAS_PRICE};
use sui_types::parse_sui_struct_tag;
use tap::TapFallible;
use tokio::sync::mpsc::unbounded_channel;
use tokio::sync::oneshot;
Expand Down Expand Up @@ -2272,8 +2273,9 @@ impl AuthorityState {
.await?
}
EventQuery::MoveEvent(struct_name) => {
let normalized_struct_name = parse_sui_struct_tag(&struct_name)?.to_string();
es.events_by_move_event_struct_name(
&struct_name,
&normalized_struct_name,
tx_num,
event_num,
limit,
Expand Down
1 change: 1 addition & 0 deletions crates/sui-json-rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2160,6 +2160,7 @@ pub enum SuiEvent {
/// Move-specific event
#[serde(rename_all = "camelCase")]
MoveEvent {
// TODO: What's the best way to serialize this using `AccountAddress::short_str_lossless` ??
package_id: ObjectID,
transaction_module: String,
sender: SuiAddress,
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-json-rpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait CoinReadApi {
&self,
/// the owner's Sui address
owner: SuiAddress,
/// optional fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.
/// optional type name for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.
coin_type: Option<String>,
/// optional paging cursor
cursor: Option<ObjectID>,
Expand All @@ -75,7 +75,7 @@ pub trait CoinReadApi {
&self,
/// the owner's Sui address
owner: SuiAddress,
/// optional fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.
/// optional type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.
coin_type: Option<String>,
) -> RpcResult<Balance>;

Expand All @@ -91,15 +91,15 @@ pub trait CoinReadApi {
#[method(name = "getCoinMetadata")]
async fn get_coin_metadata(
&self,
/// fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
/// type name for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
coin_type: String,
) -> RpcResult<SuiCoinMetadata>;

/// Return total supply for a coin
#[method(name = "getTotalSupply")]
async fn get_total_supply(
&self,
/// fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
/// type name for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)
coin_type: String,
) -> RpcResult<Supply>;
}
Expand Down
21 changes: 13 additions & 8 deletions crates/sui-json-rpc/src/coin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl CoinReadApi {
async fn get_coins_internal(
&self,
owner: SuiAddress,
coin_type: Option<String>,
coin_type: Option<StructTag>,
cursor: Option<ObjectID>,
limit: Option<usize>,
) -> Result<CoinPage, Error> {
Expand All @@ -103,7 +103,7 @@ impl CoinReadApi {
fn get_owner_coin_iterator<'a>(
&'a self,
owner: SuiAddress,
coin_type: &'a Option<String>,
coin_type: &'a Option<StructTag>,
) -> Result<impl Iterator<Item = ObjectID> + '_, Error> {
Ok(self
.state
Expand Down Expand Up @@ -162,8 +162,10 @@ impl CoinReadApiServer for CoinReadApi {
cursor: Option<ObjectID>,
limit: Option<usize>,
) -> RpcResult<CoinPage> {
// Default coin_type to 0x2::sui::SUI
let coin_type = coin_type.or_else(|| Some(GAS::type_().to_string()));
let coin_type = Some(match coin_type {
Some(c) => parse_sui_struct_tag(&c)?,
None => GAS::type_(),
});
Ok(self
.get_coins_internal(owner, coin_type, cursor, limit)
.await?)
Expand All @@ -183,7 +185,10 @@ impl CoinReadApiServer for CoinReadApi {
owner: SuiAddress,
coin_type: Option<String>,
) -> RpcResult<Balance> {
let coin_type = coin_type.or_else(|| Some(GAS::type_().to_string()));
let coin_type = Some(match coin_type {
Some(c) => parse_sui_struct_tag(&c)?,
None => GAS::type_(),
});

// TODO: Add index to improve performance?
let coins = self.get_owner_coin_iterator(owner, &coin_type)?;
Expand All @@ -202,7 +207,7 @@ impl CoinReadApiServer for CoinReadApi {
}

Ok(Balance {
coin_type: coin_type.unwrap(),
coin_type: coin_type.unwrap().to_string(),
coin_object_count,
total_balance,
locked_balance,
Expand Down Expand Up @@ -287,10 +292,10 @@ impl CoinReadApiServer for CoinReadApi {
}
}

fn is_coin_type(type_: &StructTag, coin_type: &Option<String>) -> bool {
fn is_coin_type(type_: &StructTag, coin_type: &Option<StructTag>) -> bool {
if Coin::is_coin(type_) || LockedCoin::is_locked_coin(type_) {
return if let Some(coin_type) = coin_type {
matches!(type_.type_params.first(), Some(TypeTag::Struct(type_)) if &type_.to_string() == coin_type)
matches!(type_.type_params.first(), Some(TypeTag::Struct(type_)) if type_.to_canonical_string() == coin_type.to_canonical_string())
} else {
true
};
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@
},
{
"name": "coin_type",
"description": "optional fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.",
"description": "optional type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.",
"schema": {
"type": "string"
}
Expand Down Expand Up @@ -719,7 +719,7 @@
"params": [
{
"name": "coin_type",
"description": "fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)",
"description": "type name for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)",
"required": true,
"schema": {
"type": "string"
Expand Down Expand Up @@ -753,7 +753,7 @@
},
{
"name": "coin_type",
"description": "optional fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.",
"description": "optional type name for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC), default to 0x2::sui::SUI if not specified.",
"schema": {
"type": "string"
}
Expand Down Expand Up @@ -1567,7 +1567,7 @@
"params": [
{
"name": "coin_type",
"description": "fully qualified type names for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)",
"description": "type name for the coin (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC)",
"required": true,
"schema": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test:unit": "vitest run unit",
"test:e2e": "wait-on http://127.0.0.1:9123 -l --timeout 120000 && vitest run e2e",
"test:e2e:nowait": "vitest run e2e",
"prepare:e2e": "cargo build --bin sui-test-validator --bin sui --profile dev && RUST_LOG=info,sui=debug,anemo_tower=warn,consensus=off cargo run --bin sui-test-validator",
"prepare:e2e": "cargo build --bin sui-test-validator --bin sui --profile dev && RUST_LOG=info,sui=error,anemo_tower=warn,consensus=off cargo run --bin sui-test-validator",
"lint": "eslint './{src,test}/**.{ts,js}'",
"prepublishOnly": "pnpm build",
"size": "size-limit",
Expand Down
2 changes: 1 addition & 1 deletion sdk/typescript/test/e2e/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ export async function publishPackage(
const publishEvent = getEvents(publishTxn).filter(
(e: any) => 'publish' in e
)[0];
return publishEvent.publish.packageId;
return publishEvent.publish.packageId.replace(/^0x(0+)/, '$1');
}

0 comments on commit 334b2fe

Please sign in to comment.