From b7ad31fb4aa502600acc73aa34f17d76f1c1dbf5 Mon Sep 17 00:00:00 2001 From: Nikita Strygin Date: Fri, 22 Mar 2024 14:55:09 +0300 Subject: [PATCH] [feature] #4350: Expose the EventSet bitfields in schema Signed-off-by: Nikita Strygin --- data_model/derive/src/event_set.rs | 41 ++++- docs/source/references/schema.json | 252 +++++++++++++++++++++++++++-- 2 files changed, 279 insertions(+), 14 deletions(-) diff --git a/data_model/derive/src/event_set.rs b/data_model/derive/src/event_set.rs index e6ed8c573a1..5c728906d31 100644 --- a/data_model/derive/src/event_set.rs +++ b/data_model/derive/src/event_set.rs @@ -131,20 +131,26 @@ impl ToTokens for EventSetEnum { variants, } = self; + let flag_raw_values = variants + .iter() + .zip(0u32..) + .map(|(_, i)| quote!(1 << #i)) + .collect::>(); + // 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); } }, ); @@ -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] @@ -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 { + ::id() + } + + fn update_schema_map(metamap: &mut iroha_schema::MetaMap) { + if !metamap.contains_key::() { + if !metamap.contains_key::() { + ::update_schema_map(metamap); + } + metamap.insert::(iroha_schema::Metadata::Bitmap(iroha_schema::BitmapMeta { + repr: core::any::TypeId::of::(), + masks: vec![ + #( + iroha_schema::BitmapMask { + name: String::from(#flag_names), + mask: #flag_raw_values, + }, + )* + ], + })); + } + } + } }) } } diff --git a/docs/source/references/schema.json b/docs/source/references/schema.json index b6d93a29fdc..496bb8eec28 100644 --- a/docs/source/references/schema.json +++ b/docs/source/references/schema.json @@ -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": [ { @@ -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": [ { @@ -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": [ { @@ -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": [ { @@ -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": [ { @@ -1000,7 +1166,17 @@ } ] }, - "ExecutorEventSet": "u32", + "ExecutorEventSet": { + "Bitmap": { + "repr": "u32", + "masks": [ + { + "name": "Upgraded", + "mask": 1 + } + ] + } + }, "Fail": { "Struct": [ { @@ -2364,7 +2540,21 @@ } ] }, - "PeerEventSet": "u32", + "PeerEventSet": { + "Bitmap": { + "repr": "u32", + "masks": [ + { + "name": "Added", + "mask": 1 + }, + { + "name": "Removed", + "mask": 2 + } + ] + } + }, "PeerId": { "Struct": [ { @@ -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": [ { @@ -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": [ {