Skip to content

Commit

Permalink
[GraphQL/MovePackage] Linkage Field (MystenLabs#14424)
Browse files Browse the repository at this point in the history
## Description

Lists the packages transitive dependencies. Exposing this as its own
field because the BCS representation of a package doesn't contain this
information (it contains only the modules).

This maps how this field is represented in RPC 1.0

## Test Plan

Tested with GraphiQL:


![image](https://github.com/MystenLabs/sui/assets/332275/cb829b4c-e1c8-4c14-b331-1c386391a87e)

## Stack

- MystenLabs#14422 
- MystenLabs#14423
  • Loading branch information
amnn authored Oct 27, 2023
1 parent aba7dc7 commit 56b3d20
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 13 deletions.
22 changes: 22 additions & 0 deletions crates/sui-graphql-rpc/schema/current_progress_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,24 @@ type GenesisTransaction {



"""
Information used by a package to link to a specific version of its dependency.
"""
type Linkage {
"""
The ID on-chain of the first version of the dependency.
"""
originalId: SuiAddress!
"""
The ID on-chain of the version of the dependency that this package depends on.
"""
upgradedId: SuiAddress!
"""
The version of the dependency that this package depends on.
"""
version: Int!
}

"""
The contents of a Move Value, corresponding to the following recursive type:
Expand Down Expand Up @@ -371,6 +389,10 @@ type MovePackage {
module(name: String!): MoveModule
moduleConnection(first: Int, after: String, last: Int, before: String): MoveModuleConnection
"""
The transitive dependencies of this package.
"""
linkage: [Linkage!]
"""
BCS representation of the package's modules. Modules appear as a sequence of pairs (module
name, followed by module bytes), in alphabetic order by module name.
"""
Expand Down
7 changes: 7 additions & 0 deletions crates/sui-graphql-rpc/schema/draft_target_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -977,11 +977,18 @@ type MovePackage {
before: String,
): MoveModuleConnection

linkage: [Linkage!]
bcs: Base64

asObject: Object!
}

type Linkage {
originalId: SuiAddress!
upgradedId: SuiAddress!
version: Int!
}

type MoveModuleId {
package: MovePackage!
name: String!
Expand Down
58 changes: 45 additions & 13 deletions crates/sui-graphql-rpc/src/types/move_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
use super::base64::Base64;
use super::move_module::MoveModule;
use super::object::Object;
use super::sui_address::SuiAddress;
use crate::context_data::db_data_provider::validate_cursor_pagination;
use crate::error::code::INTERNAL_SERVER_ERROR;
use crate::error::{graphql_error, Error};
use async_graphql::connection::{Connection, Edge};
use async_graphql::*;
use move_binary_format::CompiledModule;
use sui_types::object::Object as NativeSuiObject;
use sui_types::Identifier;
use sui_types::{
move_package::MovePackage as NativeMovePackage, object::Object as NativeSuiObject, Identifier,
};

const DEFAULT_PAGE_SIZE: usize = 10;

Expand All @@ -20,6 +22,19 @@ pub(crate) struct MovePackage {
pub native_object: NativeSuiObject,
}

/// Information used by a package to link to a specific version of its dependency.
#[derive(SimpleObject)]
struct Linkage {
/// The ID on-chain of the first version of the dependency.
original_id: SuiAddress,

/// The ID on-chain of the version of the dependency that this package depends on.
upgraded_id: SuiAddress,

/// The version of the dependency that this package depends on.
version: u64,
}

#[allow(unreachable_code)]
#[allow(unused_variables)]
#[Object]
Expand Down Expand Up @@ -135,20 +150,26 @@ impl MovePackage {
Ok(None)
}

/// The transitive dependencies of this package.
async fn linkage(&self) -> Result<Option<Vec<Linkage>>> {
let linkage = self
.as_native_package()?
.linkage_table()
.iter()
.map(|(&runtime_id, upgrade_info)| Linkage {
original_id: runtime_id.into(),
upgraded_id: upgrade_info.upgraded_id.into(),
version: upgrade_info.upgraded_version.value(),
})
.collect();

Ok(Some(linkage))
}

/// BCS representation of the package's modules. Modules appear as a sequence of pairs (module
/// name, followed by module bytes), in alphabetic order by module name.
async fn bcs(&self) -> Result<Option<Base64>> {
let modules = self
.native_object
.data
.try_as_package()
.ok_or_else(|| {
Error::Internal(format!(
"Failed to convert native object to move package: {}",
self.native_object.id(),
))
})?
.serialized_module_map();
let modules = self.as_native_package()?.serialized_module_map();

let bcs = bcs::to_bytes(modules).map_err(|_| {
Error::Internal(format!(
Expand All @@ -164,3 +185,14 @@ impl MovePackage {
Some(Object::from(&self.native_object))
}
}

impl MovePackage {
fn as_native_package(&self) -> Result<&NativeMovePackage> {
Ok(self.native_object.data.try_as_package().ok_or_else(|| {
Error::Internal(format!(
"Failed to convert native object to move package: {}",
self.native_object.id(),
))
})?)
}
}
7 changes: 7 additions & 0 deletions crates/sui-graphql-rpc/src/types/sui_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::str::FromStr;
use async_graphql::*;
use move_core_types::account_address::AccountAddress;
use serde::{Deserialize, Serialize};
use sui_types::base_types::ObjectID;
use thiserror::Error;

const SUI_ADDRESS_LENGTH: usize = 32;
Expand Down Expand Up @@ -89,6 +90,12 @@ impl From<AccountAddress> for SuiAddress {
}
}

impl From<ObjectID> for SuiAddress {
fn from(value: ObjectID) -> Self {
SuiAddress(value.into_bytes())
}
}

impl FromStr for SuiAddress {
type Err = FromStrError;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@ type GenesisTransaction {



"""
Information used by a package to link to a specific version of its dependency.
"""
type Linkage {
"""
The ID on-chain of the first version of the dependency.
"""
originalId: SuiAddress!
"""
The ID on-chain of the version of the dependency that this package depends on.
"""
upgradedId: SuiAddress!
"""
The version of the dependency that this package depends on.
"""
version: Int!
}

"""
The contents of a Move Value, corresponding to the following recursive type:

Expand Down Expand Up @@ -375,6 +393,10 @@ type MovePackage {
module(name: String!): MoveModule
moduleConnection(first: Int, after: String, last: Int, before: String): MoveModuleConnection
"""
The transitive dependencies of this package.
"""
linkage: [Linkage!]
"""
BCS representation of the package's modules. Modules appear as a sequence of pairs (module
name, followed by module bytes), in alphabetic order by module name.
"""
Expand Down

0 comments on commit 56b3d20

Please sign in to comment.