Skip to content

Commit

Permalink
[Move] box type layout vectors and increase max type nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Sep 10, 2024
1 parent a911368 commit e1ffa26
Show file tree
Hide file tree
Showing 34 changed files with 266 additions and 204 deletions.
14 changes: 8 additions & 6 deletions crates/sui-core/src/unit_tests/subscription_handler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,23 @@ impl TestEvent {
fn layout() -> MoveStructLayout {
MoveStructLayout {
type_: Self::type_(),
fields: vec![
fields: Box::new(vec![
MoveFieldLayout::new(ident_str!("creator").to_owned(), MoveTypeLayout::Address),
MoveFieldLayout::new(
ident_str!("name").to_owned(),
MoveTypeLayout::Struct(UTF8String::layout()),
MoveTypeLayout::Struct(Box::new(UTF8String::layout())),
),
MoveFieldLayout::new(
ident_str!("data").to_owned(),
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U64)),
),
MoveFieldLayout::new(
ident_str!("coins").to_owned(),
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::Struct(GasCoin::layout()))),
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::Struct(Box::new(
GasCoin::layout(),
)))),
),
],
]),
}
}
}
Expand Down Expand Up @@ -131,10 +133,10 @@ impl UTF8String {
fn layout() -> MoveStructLayout {
MoveStructLayout {
type_: Self::type_(),
fields: vec![MoveFieldLayout::new(
fields: Box::new(vec![MoveFieldLayout::new(
ident_str!("bytes").to_owned(),
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
)],
)]),
}
}
}
4 changes: 2 additions & 2 deletions crates/sui-graphql-rpc/src/types/move_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ impl TryFrom<A::MoveTypeLayout> for MoveTypeLayout {
TL::Address => Self::Address,

TL::Vector(v) => Self::Vector(Box::new(Self::try_from(*v)?)),
TL::Struct(s) => Self::Struct(s.try_into()?),
TL::Enum(e) => Self::Enum(e.try_into()?),
TL::Struct(s) => Self::Struct((*s).try_into()?),
TL::Enum(e) => Self::Enum((*e).try_into()?),
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-graphql-rpc/src/types/move_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,13 @@ mod tests {

macro_rules! struct_layout {
($type:literal { $($name:literal : $layout:expr),* $(,)?}) => {
A::MoveTypeLayout::Struct(S {
A::MoveTypeLayout::Struct(Box::new(S {
type_: StructTag::from_str($type).expect("Failed to parse struct"),
fields: vec![$(MoveFieldLayout {
fields: Box::new(vec![$(MoveFieldLayout {
name: ident_str!($name).to_owned(),
layout: $layout,
}),*]
})
}),*])
}))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sui-indexer/src/handlers/checkpoint_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ async fn get_move_struct_layout_map(
move_core_types::annotated_value::MoveStructLayout,
),
IndexerError,
>((struct_tag, move_struct_layout))
>((struct_tag, *move_struct_layout))
}
})
.collect::<Vec<_>>();
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-indexer/src/models/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl StoredObject {
)),
}?;

Ok(ObjectRead::Exists(oref, object, Some(move_struct_layout)))
Ok(ObjectRead::Exists(oref, object, Some(*move_struct_layout)))
}

pub fn get_object_ref(&self) -> Result<ObjectRef, IndexerError> {
Expand Down
55 changes: 26 additions & 29 deletions crates/sui-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl SuiJsonValue {
with one field of address or u8 vector type"
),
},
MoveTypeLayout::Struct(MoveStructLayout { type_, .. }) if type_ == &ID::type_() => {
MoveTypeLayout::Struct(struct_layout) if struct_layout.type_ == ID::type_() => {
Ok(R::MoveValue::Struct(R::MoveStruct(vec![
Self::to_move_value(val, &inner_vec[0].layout.clone())?,
])))
Expand Down Expand Up @@ -277,27 +277,26 @@ impl SuiJsonValue {
R::MoveValue::U256(convert_string_to_u256(s.as_str())?)
}
// For ascii and utf8 strings
(
JsonValue::String(s),
MoveTypeLayout::Struct(MoveStructLayout { type_, fields: _ }),
) if is_move_string_type(type_) => {
(JsonValue::String(s), MoveTypeLayout::Struct(struct_layout))
if is_move_string_type(&struct_layout.type_) =>
{
R::MoveValue::Vector(s.as_bytes().iter().copied().map(R::MoveValue::U8).collect())
}
// For ID
(JsonValue::String(s), MoveTypeLayout::Struct(MoveStructLayout { type_, fields }))
if type_ == &ID::type_() =>
(JsonValue::String(s), MoveTypeLayout::Struct(struct_layout))
if struct_layout.type_ == ID::type_() =>
{
if fields.len() != 1 {
if struct_layout.fields.len() != 1 {
bail!(
"Cannot convert string arg {s} to {type_} which is expected to be a struct with one field"
"Cannot convert string arg {s} to {} which is expected to be a struct with one field", struct_layout.type_
);
};
let addr = SuiAddress::from_str(s)?;
R::MoveValue::Address(addr.into())
}
(JsonValue::Object(o), MoveTypeLayout::Struct(MoveStructLayout { fields, .. })) => {
(JsonValue::Object(o), MoveTypeLayout::Struct(struct_layout)) => {
let mut field_values = vec![];
for layout in fields {
for layout in struct_layout.fields.iter() {
let field = o
.get(layout.name.as_str())
.ok_or_else(|| anyhow!("Missing field {} for struct {ty}", layout.name))?;
Expand All @@ -306,10 +305,8 @@ impl SuiJsonValue {
R::MoveValue::Struct(R::MoveStruct(field_values))
}
// Unnest fields
(value, MoveTypeLayout::Struct(MoveStructLayout { fields, .. }))
if fields.len() == 1 =>
{
Self::to_move_value(value, &fields[0].layout)?
(value, MoveTypeLayout::Struct(struct_layout)) if struct_layout.fields.len() == 1 => {
Self::to_move_value(value, &struct_layout.fields[0].layout)?
}
(JsonValue::String(s), MoveTypeLayout::Vector(t)) => {
match &**t {
Expand All @@ -332,8 +329,8 @@ impl SuiJsonValue {
};
R::MoveValue::Vector(vec.iter().copied().map(R::MoveValue::U8).collect())
}
MoveTypeLayout::Struct(MoveStructLayout { fields: inner, .. }) => {
Self::handle_inner_struct_layout(inner, val, ty, s)?
MoveTypeLayout::Struct(struct_layout) => {
Self::handle_inner_struct_layout(&struct_layout.fields, val, ty, s)?
}
_ => bail!("Cannot convert string arg {s} to {ty}"),
}
Expand Down Expand Up @@ -594,36 +591,36 @@ pub fn primitive_type(
if resolved_struct == RESOLVED_ASCII_STR {
(
true,
Some(MoveTypeLayout::Struct(MoveStructLayout {
Some(MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: resolved_to_struct(RESOLVED_ASCII_STR),
fields: vec![MoveFieldLayout::new(
fields: Box::new(vec![MoveFieldLayout::new(
ident_str!("bytes").into(),
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
)],
})),
)]),
}))),
)
} else if resolved_struct == RESOLVED_UTF8_STR {
// both structs structs representing strings have one field - a vector of type u8
(
true,
Some(MoveTypeLayout::Struct(MoveStructLayout {
Some(MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: resolved_to_struct(RESOLVED_UTF8_STR),
fields: vec![MoveFieldLayout::new(
fields: Box::new(vec![MoveFieldLayout::new(
ident_str!("bytes").into(),
MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
)],
})),
)]),
}))),
)
} else if resolved_struct == RESOLVED_SUI_ID {
(
true,
Some(MoveTypeLayout::Struct(MoveStructLayout {
Some(MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: resolved_to_struct(RESOLVED_SUI_ID),
fields: vec![MoveFieldLayout::new(
fields: Box::new(vec![MoveFieldLayout::new(
ident_str!("bytes").into(),
MoveTypeLayout::Address,
)],
})),
)]),
}))),
)
} else {
(false, None)
Expand Down
50 changes: 25 additions & 25 deletions crates/sui-json/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,18 @@ fn test_from_str() {
fn test_sui_call_arg_string_type() {
let arg1 = bcs::to_bytes("Some String").unwrap();

let string_layout = Some(MoveTypeLayout::Struct(MoveStructLayout {
let string_layout = Some(MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: StructTag {
address: MOVE_STDLIB_ADDRESS,
module: STD_ASCII_MODULE_NAME.into(),
name: STD_ASCII_STRUCT_NAME.into(),
type_params: vec![],
},
fields: vec![MoveFieldLayout {
fields: Box::new(vec![MoveFieldLayout {
name: ident_str!("bytes").into(),
layout: MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
}],
}));
}]),
})));
let v = SuiJsonValue::from_bcs_bytes(string_layout.as_ref(), &arg1).unwrap();

assert_eq!(json! {"Some String"}, v.to_json_value());
Expand All @@ -639,31 +639,31 @@ fn test_sui_call_arg_string_type() {
fn test_sui_call_arg_option_type() {
let arg1 = bcs::to_bytes(&Some("Some String")).unwrap();

let string_layout = MoveTypeLayout::Struct(MoveStructLayout {
let string_layout = MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: StructTag {
address: MOVE_STDLIB_ADDRESS,
module: STD_ASCII_MODULE_NAME.into(),
name: STD_ASCII_STRUCT_NAME.into(),
type_params: vec![],
},
fields: vec![MoveFieldLayout {
fields: Box::new(vec![MoveFieldLayout {
name: ident_str!("bytes").into(),
layout: MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
}],
});
}]),
}));

let option_layout = MoveTypeLayout::Struct(MoveStructLayout {
let option_layout = MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: StructTag {
address: MOVE_STDLIB_ADDRESS,
module: STD_OPTION_MODULE_NAME.into(),
name: STD_OPTION_STRUCT_NAME.into(),
type_params: vec![],
},
fields: vec![MoveFieldLayout {
fields: Box::new(vec![MoveFieldLayout {
name: ident_str!("vec").into(),
layout: MoveTypeLayout::Vector(Box::new(string_layout.clone())),
}],
});
}]),
}));

let v = SuiJsonValue::from_bcs_bytes(Some(option_layout).as_ref(), &arg1).unwrap();

Expand All @@ -680,7 +680,7 @@ fn test_sui_call_arg_option_type() {

#[test]
fn test_convert_struct() {
let layout = MoveTypeLayout::Struct(GasCoin::layout());
let layout = MoveTypeLayout::Struct(Box::new(GasCoin::layout()));

let value = json!({"id":"0xf1416fe18c7baa1673187375777a7606708481311cb3548509ec91a5871c6b9a", "balance": "1000000"});
let sui_json = SuiJsonValue::new(value).unwrap();
Expand All @@ -702,18 +702,18 @@ fn test_convert_struct() {
fn test_convert_string_vec() {
let test_vec = vec!["0xbbb", "test_str"];
let bcs = bcs::to_bytes(&test_vec).unwrap();
let string_layout = MoveTypeLayout::Struct(MoveStructLayout {
let string_layout = MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: StructTag {
address: MOVE_STDLIB_ADDRESS,
module: STD_ASCII_MODULE_NAME.into(),
name: STD_ASCII_STRUCT_NAME.into(),
type_params: vec![],
},
fields: vec![MoveFieldLayout {
fields: Box::new(vec![MoveFieldLayout {
name: ident_str!("bytes").into(),
layout: MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
}],
});
}]),
}));

let layout = MoveTypeLayout::Vector(Box::new(string_layout));

Expand All @@ -737,31 +737,31 @@ fn test_string_vec_df_name_child_id_eq() {
]
});

let string_layout = MoveTypeLayout::Struct(MoveStructLayout {
let string_layout = MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: StructTag {
address: MOVE_STDLIB_ADDRESS,
module: STD_ASCII_MODULE_NAME.into(),
name: STD_ASCII_STRUCT_NAME.into(),
type_params: vec![],
},
fields: vec![MoveFieldLayout {
fields: Box::new(vec![MoveFieldLayout {
name: ident_str!("bytes").into(),
layout: MoveTypeLayout::Vector(Box::new(MoveTypeLayout::U8)),
}],
});
}]),
}));

let layout = MoveTypeLayout::Struct(MoveStructLayout {
let layout = MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_: StructTag {
address: MOVE_STDLIB_ADDRESS,
module: STD_ASCII_MODULE_NAME.into(),
name: STD_ASCII_STRUCT_NAME.into(),
type_params: vec![],
},
fields: vec![MoveFieldLayout::new(
fields: Box::new(vec![MoveFieldLayout::new(
Identifier::from_str("labels").unwrap(),
MoveTypeLayout::Vector(Box::new(string_layout)),
)],
});
)]),
}));

let sui_json = SuiJsonValue::new(name).unwrap();
let bcs2 = sui_json.to_bcs_bytes(&layout).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-open-rpc/src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ impl RpcExampleProvider {
},
MoveStructLayout {
type_: struct_tag,
fields: Vec::new(),
fields: Box::new(Vec::new()),
},
)
.unwrap(),
Expand Down
10 changes: 5 additions & 5 deletions crates/sui-package-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,10 @@ impl<'l> ResolutionContext<'l> {
}

(
MoveTypeLayout::Struct(MoveStructLayout {
MoveTypeLayout::Struct(Box::new(MoveStructLayout {
type_,
fields: resolved_fields,
}),
fields: Box::new(resolved_fields),
})),
field_depth + 1,
)
}
Expand All @@ -1481,10 +1481,10 @@ impl<'l> ResolutionContext<'l> {
}

(
MoveTypeLayout::Enum(MoveEnumLayout {
MoveTypeLayout::Enum(Box::new(MoveEnumLayout {
type_,
variants: resolved_variants,
}),
})),
field_depth + 1,
)
}
Expand Down
Loading

0 comments on commit e1ffa26

Please sign in to comment.