Skip to content

Commit

Permalink
feature: Partial implementation for Package state
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Aug 10, 2023
1 parent 69bddd6 commit 24aaef8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 147 deletions.
79 changes: 33 additions & 46 deletions radix-engine/src/blueprints/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ pub trait SortedIndexEntryContent: Sized {
}
}


/// Generates types and typed-interfaces for native blueprints and their
/// interaction with the substate store.
///
/// * For fields, assumes the existence of a type called:
/// * `<BlueprintIdent><FieldIdent>FieldV1`
/// * For collections, assumes the existence of types called:
/// * `<BlueprintIdent><CollectionIdent>ValueV1`
///
///
/// In future, resolve the `x_type` fields as a $x:tt and then
/// map in other macros into:
/// ```
Expand All @@ -66,7 +65,7 @@ pub trait SortedIndexEntryContent: Sized {
/// the_type: x,
/// },
/// Instance {
/// ident:
/// ident:
/// },
/// StaticMultiVersioned(V1, V2),
/// ```
Expand Down Expand Up @@ -444,10 +443,9 @@ mod helper_macros {
}
) => {
// Don't output any types for an instance schema
};
// TODO - Add support for some kind of StaticMultiVersioned type here
}; // TODO - Add support for some kind of StaticMultiVersioned type here
}

#[allow(unused)]
pub(crate) use generate_content_type_aliases;

Expand Down Expand Up @@ -478,37 +476,31 @@ mod helper_macros {
));
};
}

#[allow(unused)]
pub(crate) use generate_system_substate_type_alias;

macro_rules! map_collection_schema {
(KeyValue, $aggregator:ident, $key_type:tt, $key_content_alias:ident, $value_type:tt, $value_content_alias:ident, $can_own:expr$(,)?) => {
BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: map_type_ref!($aggregator, $key_type, $key_content_alias),
value: map_type_ref!($aggregator, $value_type, $value_content_alias),
can_own: $can_own,
},
)
BlueprintCollectionSchema::KeyValueStore(BlueprintKeyValueSchema {
key: map_type_ref!($aggregator, $key_type, $key_content_alias),
value: map_type_ref!($aggregator, $value_type, $value_content_alias),
can_own: $can_own,
})
};
(Index, $aggregator:ident, $key_type:tt, $key_content_alias:ident, $value_type:tt, $value_content_alias:ident, $can_own:expr$(,)?) => {
BlueprintCollectionSchema::Index(
BlueprintKeyValueSchema {
key: map_type_ref!($aggregator, $key_type, $key_content_alias),
value: map_type_ref!($aggregator, $value_type, $value_content_alias),
can_own: $can_own,
},
)
BlueprintCollectionSchema::Index(BlueprintKeyValueSchema {
key: map_type_ref!($aggregator, $key_type, $key_content_alias),
value: map_type_ref!($aggregator, $value_type, $value_content_alias),
can_own: $can_own,
})
};
(SortedIndex, $aggregator:ident, $key_type:tt, $key_content_alias:ident, $value_type:tt, $value_content_alias:ident, $can_own:expr$(,)?) => {
BlueprintCollectionSchema::SortedIndex(
BlueprintKeyValueSchema {
key: map_type_ref!($aggregator, $key_type, $key_content_alias),
value: map_type_ref!($aggregator, $value_type, $value_content_alias),
can_own: $can_own,
},
)
BlueprintCollectionSchema::SortedIndex(BlueprintKeyValueSchema {
key: map_type_ref!($aggregator, $key_type, $key_content_alias),
value: map_type_ref!($aggregator, $value_type, $value_content_alias),
can_own: $can_own,
})
};
($unknown_system_substate_type:ident, $aggregator:ident, $collection_key_type:tt, $collection_value_type:tt, $collection_can_own:expr$(,)?) => {
compile_error!(concat!(
Expand All @@ -518,7 +510,7 @@ mod helper_macros {
));
};
}

#[allow(unused)]
pub(crate) use map_collection_schema;

Expand All @@ -531,9 +523,7 @@ mod helper_macros {
},
$content_alias:ident$(,)?
) => {
TypeRef::Static(
$aggregator.add_child_type_and_descendents::<$content_alias>(),
)
TypeRef::Static($aggregator.add_child_type_and_descendents::<$content_alias>())
};
(
$aggregator:ident,
Expand All @@ -544,9 +534,7 @@ mod helper_macros {
},
$content_alias:ident$(,)?
) => {
TypeRef::Static(
$aggregator.add_child_type_and_descendents::<$content_alias>(),
)
TypeRef::Static($aggregator.add_child_type_and_descendents::<$content_alias>())
};
(
$aggregator:ident,
Expand All @@ -558,13 +546,12 @@ mod helper_macros {
$content_alias:ident$(,)?
) => {
compile_error!("Instance schemas not yet supported - close though!")
};
// TODO - Add support for some kind of StaticMultiVersioned type here
}; // TODO - Add support for some kind of StaticMultiVersioned type here
}

#[allow(unused)]
pub(crate) use map_type_ref;

macro_rules! map_entry_substate_to_kv_entry {
(KeyValue, $entry_substate:ident) => {
paste::paste! {
Expand Down Expand Up @@ -595,7 +582,7 @@ mod helper_macros {
}
};
}

#[allow(unused)]
pub(crate) use map_entry_substate_to_kv_entry;
}
Expand All @@ -607,18 +594,18 @@ mod tests {
// Check that the below compiles
#[derive(Debug, PartialEq, Eq, Sbor)]
pub struct PackageRoyaltyFieldV1;

#[derive(Debug, PartialEq, Eq, Sbor)]
pub struct PackageBlueprintDefinitionValueV1;

#[derive(Debug, PartialEq, Eq, Sbor)]
pub struct PackageMyCoolIndexValueV1;

#[derive(Debug, PartialEq, Eq, Sbor)]
pub struct PackageMyCoolSortedIndexValueV1;

use radix_engine_interface::blueprints::package::*;

declare_native_blueprint_state! {
blueprint_ident: Package,
blueprint_snake_case: package,
Expand Down Expand Up @@ -667,5 +654,5 @@ mod tests {
can_own: true,
},
}
}
}
}
2 changes: 1 addition & 1 deletion radix-engine/src/blueprints/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod package;
mod substates;

pub use substates::*;
pub use package::*;
pub use substates::*;
103 changes: 6 additions & 97 deletions radix-engine/src/blueprints/package/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ use native_sdk::resource::NativeVault;
use native_sdk::resource::ResourceManager;
use radix_engine_interface::api::node_modules::auth::AuthAddresses;
use radix_engine_interface::api::node_modules::metadata::MetadataInit;
use radix_engine_interface::api::{
ClientApi, FieldValue, KVEntry, LockFlags, ObjectModuleId, OBJECT_HANDLE_SELF,
};
use radix_engine_interface::api::*;
pub use radix_engine_interface::blueprints::package::*;
use radix_engine_interface::blueprints::resource::{require, Bucket};
use radix_engine_interface::schema::{
BlueprintCollectionSchema, BlueprintEventSchemaInit, BlueprintFunctionsSchemaInit,
BlueprintKeyValueSchema, BlueprintSchemaInit, BlueprintStateSchemaInit, FieldSchema,
FunctionSchemaInit, TypeRef,
};
use radix_engine_interface::schema::*;
use sbor::LocalTypeIndex;
use syn::Ident;

Expand All @@ -42,6 +36,8 @@ pub use radix_engine_interface::blueprints::package::{
PackageInstrumentedCodeSubstate, PackageOriginalCodeSubstate, PackageRoyaltyAccumulatorSubstate,
};

use super::*;

pub const PACKAGE_ROYALTY_FEATURE: &str = "package_royalty";

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
Expand Down Expand Up @@ -843,91 +839,7 @@ impl PackageNativePackage {
pub fn definition() -> PackageDefinition {
let mut aggregator = TypeAggregator::<ScryptoCustomTypeKind>::new();

let mut fields = Vec::new();
fields.push(FieldSchema::if_feature(
aggregator.add_child_type_and_descendents::<PackageRoyaltyAccumulatorSubstate>(),
PACKAGE_ROYALTY_FEATURE,
));

let mut collections = Vec::new();
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(
aggregator.add_child_type_and_descendents::<BlueprintVersionKey>(),
),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<BlueprintDefinition>(),
),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(
aggregator.add_child_type_and_descendents::<BlueprintVersionKey>(),
),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<BlueprintDependencies>(),
),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(aggregator.add_child_type_and_descendents::<Hash>()),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<ScryptoSchema>(),
),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(
aggregator.add_child_type_and_descendents::<BlueprintVersionKey>(),
),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<PackageRoyaltyConfig>(),
),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(
aggregator.add_child_type_and_descendents::<BlueprintVersionKey>(),
),
value: TypeRef::Static(aggregator.add_child_type_and_descendents::<AuthConfig>()),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(aggregator.add_child_type_and_descendents::<Hash>()),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<PackageVmTypeSubstate>(),
),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(aggregator.add_child_type_and_descendents::<Hash>()),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<PackageOriginalCodeSubstate>(),
),
can_own: false,
},
));
collections.push(BlueprintCollectionSchema::KeyValueStore(
BlueprintKeyValueSchema {
key: TypeRef::Static(aggregator.add_child_type_and_descendents::<Hash>()),
value: TypeRef::Static(
aggregator.add_child_type_and_descendents::<PackageInstrumentedCodeSubstate>(),
),
can_own: false,
},
));
let state = PackageStateSchemaInit::create_schema_init(&mut aggregator);

let mut functions = BTreeMap::new();
functions.insert(
Expand Down Expand Up @@ -999,10 +911,7 @@ impl PackageNativePackage {
schema: BlueprintSchemaInit {
generics: vec![],
schema,
state: BlueprintStateSchemaInit {
fields,
collections,
},
state,
events: BlueprintEventSchemaInit::default(),
functions: BlueprintFunctionsSchemaInit {
functions,
Expand Down
6 changes: 3 additions & 3 deletions radix-engine/src/blueprints/package/substates.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::blueprints::macros::*;
use super::*;
use crate::blueprints::macros::*;
use crate::types::*;

declare_native_blueprint_state!{
declare_native_blueprint_state! {
blueprint_ident: Package,
blueprint_snake_case: package,
instance_schema_types: [],
Expand Down Expand Up @@ -164,4 +164,4 @@ pub struct PackageCodeOriginalCodeValueV1 {
#[sbor(transparent)]
pub struct PackageCodeInstrumentedCodeValueV1 {
pub code: Vec<u8>,
}
}

0 comments on commit 24aaef8

Please sign in to comment.