Skip to content

Commit

Permalink
[feature] #4350: Expose the EventSet bitfields in schema
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 committed Mar 22, 2024
1 parent 75d1705 commit b7ad31f
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 14 deletions.
41 changes: 36 additions & 5 deletions data_model/derive/src/event_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,26 @@ impl ToTokens for EventSetEnum {
variants,
} = self;

let flag_raw_values = variants
.iter()
.zip(0u32..)
.map(|(_, i)| quote!(1 << #i))
.collect::<Vec<_>>();

// definitions of consts for each event
let flag_defs = variants.iter().zip(0u32..).map(
let flag_defs = variants.iter().zip(flag_raw_values.iter()).map(
|(
EventSetVariant {
flag_ident,
event_ident,
..
},
i,
raw_value,
)| {
let doc = format!(" Matches [`{event_enum_ident}::{event_ident}`]");
quote! {
#[doc = #doc]
#vis const #flag_ident: Self = Self(1 << #i);
#vis const #flag_ident: Self = Self(#raw_value);
}
},
);
Expand Down Expand Up @@ -197,8 +203,7 @@ impl ToTokens for EventSetEnum {
// but it's the easiest way to make sure those traits are implemented
parity_scale_codec::Decode,
parity_scale_codec::Encode,
// TODO: we probably want to represent the bit values for each variant in the schema
iroha_schema::IntoSchema,
iroha_schema::TypeId,
)]
#[repr(transparent)]
#[doc = #doc]
Expand Down Expand Up @@ -380,6 +385,32 @@ impl ToTokens for EventSetEnum {
deserializer.deserialize_seq(Visitor)
}
}


impl iroha_schema::IntoSchema for #set_ident {
fn type_name() -> iroha_schema::Ident {
<Self as iroha_schema::TypeId>::id()
}

fn update_schema_map(metamap: &mut iroha_schema::MetaMap) {
if !metamap.contains_key::<Self>() {
if !metamap.contains_key::<u32>() {
<u32 as iroha_schema::IntoSchema>::update_schema_map(metamap);
}
metamap.insert::<Self>(iroha_schema::Metadata::Bitmap(iroha_schema::BitmapMeta {
repr: core::any::TypeId::of::<u32>(),
masks: vec![
#(
iroha_schema::BitmapMask {
name: String::from(#flag_names),
mask: #flag_raw_values,
},
)*
],
}));
}
}
}
})
}
}
Expand Down
252 changes: 243 additions & 9 deletions docs/source/references/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,57 @@
}
]
},
"AccountEventSet": "u32",
"AccountEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "AnyAsset",
"mask": 1
},
{
"name": "Created",
"mask": 2
},
{
"name": "Deleted",
"mask": 4
},
{
"name": "AuthenticationAdded",
"mask": 8
},
{
"name": "AuthenticationRemoved",
"mask": 16
},
{
"name": "PermissionAdded",
"mask": 32
},
{
"name": "PermissionRemoved",
"mask": 64
},
{
"name": "RoleRevoked",
"mask": 128
},
{
"name": "RoleGranted",
"mask": 256
},
{
"name": "MetadataInserted",
"mask": 512
},
{
"name": "MetadataRemoved",
"mask": 1024
}
]
}
},
"AccountId": {
"Struct": [
{
Expand Down Expand Up @@ -310,7 +360,41 @@
}
]
},
"AssetDefinitionEventSet": "u32",
"AssetDefinitionEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Created",
"mask": 1
},
{
"name": "MintabilityChanged",
"mask": 2
},
{
"name": "OwnerChanged",
"mask": 4
},
{
"name": "Deleted",
"mask": 8
},
{
"name": "MetadataInserted",
"mask": 16
},
{
"name": "MetadataRemoved",
"mask": 32
},
{
"name": "TotalQuantityChanged",
"mask": 64
}
]
}
},
"AssetDefinitionId": {
"Struct": [
{
Expand Down Expand Up @@ -393,7 +477,37 @@
}
]
},
"AssetEventSet": "u32",
"AssetEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Created",
"mask": 1
},
{
"name": "Deleted",
"mask": 2
},
{
"name": "Added",
"mask": 4
},
{
"name": "Removed",
"mask": 8
},
{
"name": "MetadataInserted",
"mask": 16
},
{
"name": "MetadataRemoved",
"mask": 32
}
]
}
},
"AssetId": {
"Struct": [
{
Expand Down Expand Up @@ -631,7 +745,25 @@
}
]
},
"ConfigurationEventSet": "u32",
"ConfigurationEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Changed",
"mask": 1
},
{
"name": "Created",
"mask": 2
},
{
"name": "Deleted",
"mask": 4
}
]
}
},
"Container": {
"Enum": [
{
Expand Down Expand Up @@ -830,7 +962,41 @@
}
]
},
"DomainEventSet": "u32",
"DomainEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "AnyAccount",
"mask": 1
},
{
"name": "AnyAssetDefinition",
"mask": 2
},
{
"name": "Created",
"mask": 4
},
{
"name": "Deleted",
"mask": 8
},
{
"name": "MetadataInserted",
"mask": 16
},
{
"name": "MetadataRemoved",
"mask": 32
},
{
"name": "OwnerChanged",
"mask": 64
}
]
}
},
"DomainId": {
"Struct": [
{
Expand Down Expand Up @@ -1000,7 +1166,17 @@
}
]
},
"ExecutorEventSet": "u32",
"ExecutorEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Upgraded",
"mask": 1
}
]
}
},
"Fail": {
"Struct": [
{
Expand Down Expand Up @@ -2364,7 +2540,21 @@
}
]
},
"PeerEventSet": "u32",
"PeerEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Added",
"mask": 1
},
{
"name": "Removed",
"mask": 2
}
]
}
},
"PeerId": {
"Struct": [
{
Expand Down Expand Up @@ -3145,7 +3335,29 @@
}
]
},
"RoleEventSet": "u32",
"RoleEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Created",
"mask": 1
},
{
"name": "Deleted",
"mask": 2
},
{
"name": "PermissionRemoved",
"mask": 4
},
{
"name": "PermissionAdded",
"mask": 8
}
]
}
},
"RoleId": {
"Struct": [
{
Expand Down Expand Up @@ -3863,7 +4075,29 @@
}
]
},
"TriggerEventSet": "u32",
"TriggerEventSet": {
"Bitmap": {
"repr": "u32",
"masks": [
{
"name": "Created",
"mask": 1
},
{
"name": "Deleted",
"mask": 2
},
{
"name": "Extended",
"mask": 4
},
{
"name": "Shortened",
"mask": 8
}
]
}
},
"TriggerId": {
"Struct": [
{
Expand Down

0 comments on commit b7ad31f

Please sign in to comment.