Skip to content

Commit

Permalink
Add is_event field to MoveStruct in JSON representation of Move modul…
Browse files Browse the repository at this point in the history
…e ABI from API (aptos-labs#14246)
  • Loading branch information
banool authored Aug 12, 2024
1 parent 2a8b6bd commit 388ed63
Show file tree
Hide file tree
Showing 16 changed files with 2,026 additions and 1,922 deletions.
5 changes: 5 additions & 0 deletions api/doc/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -15282,6 +15282,7 @@
"required": [
"name",
"is_native",
"is_event",
"abilities",
"generic_type_params",
"fields"
Expand All @@ -15294,6 +15295,10 @@
"type": "boolean",
"description": "Whether the struct is a native struct of Move"
},
"is_event": {
"type": "boolean",
"description": "Whether the struct is marked with the #[event] annotation"
},
"abilities": {
"type": "array",
"description": "Abilities associated with the struct",
Expand Down
4 changes: 4 additions & 0 deletions api/doc/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11419,6 +11419,7 @@ components:
required:
- name
- is_native
- is_event
- abilities
- generic_type_params
- fields
Expand All @@ -11428,6 +11429,9 @@ components:
is_native:
type: boolean
description: Whether the struct is a native struct of Move
is_event:
type: boolean
description: 'Whether the struct is marked with the #[event] annotation'
abilities:
type: array
description: Abilities associated with the struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
{
"name": "GUID",
"is_native": false,
"is_event": false,
"abilities": [
"drop",
"store"
Expand All @@ -135,6 +136,7 @@
{
"name": "ID",
"is_native": false,
"is_event": false,
"abilities": [
"copy",
"drop",
Expand Down
19 changes: 19 additions & 0 deletions api/src/tests/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ async fn test_abi() {

assert_eq!(function["is_view"], false);
}

// Confirm that MyEvent is considered an event.
let structs = modules.as_array().unwrap()[0]["abi"]["structs"]
.as_array()
.unwrap();
let my_event = structs
.iter()
.find(|s| s["name"].as_str().unwrap() == "MyEvent")
.unwrap();

assert_eq!(my_event["is_event"], true);

// Confirm that State is not considered an event.
let my_struct = structs
.iter()
.find(|s| s["name"].as_str().unwrap() == "State")
.unwrap();

assert_eq!(my_struct["is_event"], false);
}
6 changes: 6 additions & 0 deletions api/src/tests/move/pack_abi/sources/test.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ module abi::test {
value: u64
}

#[event]
struct MyEvent has store {
value: u64
}

public fun public_function(s: &signer, state: State) {
move_to(s, state)
}

public entry fun public_entry_function(s1: &signer, s2: &signer, value: u64) {
move_to(s1, State { value });
move_to(s2, State { value });

}

entry fun private_entry_function(s: &signer, value: u64) {
Expand Down
12 changes: 12 additions & 0 deletions api/types/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ pub trait Bytecode {

fn function_is_view(&self, name: &IdentStr) -> bool;

fn struct_is_event(&self, name: &IdentStr) -> bool {
match self.metadata() {
Some(m) => match m.struct_attributes.get(name.as_str()) {
Some(attrs) => attrs.iter().any(|attr| attr.is_event()),
None => false,
},
None => false,
}
}

fn new_move_struct_field(&self, def: &FieldDefinition) -> MoveStructField {
MoveStructField {
name: self.identifier_at(def.name).to_owned().into(),
Expand Down Expand Up @@ -115,6 +125,7 @@ pub trait Bytecode {
},
};
let name = self.identifier_at(handle.name).to_owned();
let is_event = self.struct_is_event(&name);
let abilities = handle
.abilities
.into_iter()
Expand All @@ -128,6 +139,7 @@ pub trait Bytecode {
MoveStruct {
name: name.into(),
is_native,
is_event,
abilities,
generic_type_params,
fields,
Expand Down
2 changes: 2 additions & 0 deletions api/types/src/move_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,8 @@ pub struct MoveStruct {
pub name: IdentifierWrapper,
/// Whether the struct is a native struct of Move
pub is_native: bool,
/// Whether the struct is marked with the #[event] annotation
pub is_event: bool,
/// Abilities associated with the struct
pub abilities: Vec<MoveAbility>,
/// Generic types associated with the struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn convert_move_struct(move_struct: &MoveStruct) -> transaction::MoveStruct
transaction::MoveStruct {
name: move_struct.name.0.to_string(),
is_native: move_struct.is_native,
is_event: move_struct.is_event,
abilities: move_struct
.abilities
.iter()
Expand Down
1 change: 1 addition & 0 deletions protos/proto/aptos/transaction/v1/transaction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ message MoveFunction {
message MoveStruct {
string name = 1;
bool is_native = 2;
bool is_event = 6;
repeated MoveAbility abilities = 3;
repeated MoveStructGenericTypeParam generic_type_params = 4;
repeated MoveStructField fields = 5;
Expand Down
140 changes: 70 additions & 70 deletions protos/python/aptos_protos/aptos/transaction/v1/transaction_pb2.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -949,14 +949,23 @@ class MoveFunction(_message.Message):
) -> None: ...

class MoveStruct(_message.Message):
__slots__ = ["name", "is_native", "abilities", "generic_type_params", "fields"]
__slots__ = [
"name",
"is_native",
"is_event",
"abilities",
"generic_type_params",
"fields",
]
NAME_FIELD_NUMBER: _ClassVar[int]
IS_NATIVE_FIELD_NUMBER: _ClassVar[int]
IS_EVENT_FIELD_NUMBER: _ClassVar[int]
ABILITIES_FIELD_NUMBER: _ClassVar[int]
GENERIC_TYPE_PARAMS_FIELD_NUMBER: _ClassVar[int]
FIELDS_FIELD_NUMBER: _ClassVar[int]
name: str
is_native: bool
is_event: bool
abilities: _containers.RepeatedScalarFieldContainer[MoveAbility]
generic_type_params: _containers.RepeatedCompositeFieldContainer[
MoveStructGenericTypeParam
Expand All @@ -966,6 +975,7 @@ class MoveStruct(_message.Message):
self,
name: _Optional[str] = ...,
is_native: bool = ...,
is_event: bool = ...,
abilities: _Optional[_Iterable[_Union[MoveAbility, str]]] = ...,
generic_type_params: _Optional[
_Iterable[_Union[MoveStructGenericTypeParam, _Mapping]]
Expand Down
Loading

0 comments on commit 388ed63

Please sign in to comment.