Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ON HOLD] Feature: Full declare_native_blueprint_state macro #1318

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0d23201
feature: Added versioned macro
dhedey Aug 8, 2023
fe5d5da
tweak: Update versioned macro
dhedey Aug 8, 2023
f487591
feature: Updates to defined_versioned
dhedey Aug 9, 2023
b13160b
feature: Add additional traits to `Versioned`
dhedey Aug 9, 2023
ed0a775
feature: `declare_native_blueprint_state!` macro
dhedey Aug 10, 2023
258b569
example: Add versioned schema
dhedey Aug 10, 2023
70e10b5
feature: Started implementing Package state
dhedey Aug 10, 2023
69bddd6
tweak: Add support for different type refs
dhedey Aug 10, 2023
24aaef8
feature: Partial implementation for Package state
dhedey Aug 10, 2023
b04288b
tweak: Add some clearer TODOs
dhedey Aug 10, 2023
c0adfeb
impl: Continue package impl of new types
dhedey Aug 11, 2023
f6b11f2
tweak: Change to using new types for keys and content
dhedey Aug 11, 2023
a615c68
wip: Continue fixing for package
dhedey Aug 12, 2023
3284bc1
feature: Further updates to `declare_native_blueprint_state`
dhedey Aug 14, 2023
534681e
fix: Misc fixes
dhedey Aug 14, 2023
f026718
fix: Further tweaks and fixes
dhedey Aug 15, 2023
b8141a2
fix: Various fixes
dhedey Aug 15, 2023
e081804
tweak: Update names of new-types to XPayload not XContent
dhedey Aug 15, 2023
d5317f8
Merge branch 'develop' into feature/key-data-model-versioning
dhedey Aug 17, 2023
5fa5b54
tidy: Remove unused model
dhedey Aug 17, 2023
9baed9a
tweak: Minor refactor to macros
dhedey Aug 17, 2023
e50cbf5
refactor: Move blueprint model/macros around
dhedey Aug 17, 2023
dc4b02f
refactor: Further minor moves / refactors
dhedey Aug 17, 2023
fd78605
tweak: Add support for generics in declare_native_blueprint_state
dhedey Aug 17, 2023
6c6c077
tweak: Output separate method for substate flashing
dhedey Aug 17, 2023
c407711
tweak: Updates to the content traits
dhedey Aug 18, 2023
6244912
feature: Improvements to key and content traits and models
dhedey Aug 21, 2023
f57d6bc
Merge branch 'develop' into feature/key-data-model-versioning
dhedey Aug 21, 2023
97b8cad
tweak: Minor tweaks to mapped physical partition definition
dhedey Aug 21, 2023
792a283
fix: Minor fix
dhedey Aug 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/faucet.wasm
Binary file not shown.
Binary file modified assets/flash_loan.wasm
Binary file not shown.
Binary file modified assets/genesis_helper.wasm
Binary file not shown.
Binary file modified assets/metadata.wasm
Binary file not shown.
Binary file modified assets/radiswap.wasm
Binary file not shown.
10 changes: 6 additions & 4 deletions radix-engine-common/src/data/scrypto/custom_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::internal_prelude::*;

pub type ScryptoTypeKind<L> = TypeKind<ScryptoCustomTypeKind, L>;
pub type ScryptoSchema = Schema<ScryptoCustomSchema>;
pub type VersionedScryptoSchema = VersionedSchema<ScryptoCustomSchema>;
pub type ScryptoTypeData<L> = TypeData<ScryptoCustomTypeKind, L>;

/// A schema for the values that a codec can decode / views as valid
Expand Down Expand Up @@ -202,12 +203,13 @@ impl CustomSchema for ScryptoCustomSchema {
}

pub trait HasSchemaHash {
fn generate_schema_hash(&self) -> Hash;
fn generate_schema_hash(&self) -> SchemaHash;
}

impl HasSchemaHash for Schema<ScryptoCustomSchema> {
fn generate_schema_hash(&self) -> Hash {
hash(scrypto_encode(self).unwrap())
// TODO(David) - change to hash the VersionedScryptoSchema when we use that in substates
impl HasSchemaHash for ScryptoSchema {
fn generate_schema_hash(&self) -> SchemaHash {
SchemaHash::from(hash(scrypto_encode(self).unwrap()))
}
}

Expand Down
2 changes: 2 additions & 0 deletions radix-engine-common/src/data/scrypto/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub type ScryptoDecoder<'a> = VecDecoder<'a, ScryptoCustomValueKind>;
pub type ScryptoTraverser<'a> = VecTraverser<'a, ScryptoCustomTraversal>;
pub type ScryptoValueKind = ValueKind<ScryptoCustomValueKind>;
pub type ScryptoValue = Value<ScryptoCustomValueKind, ScryptoCustomValue>;
pub type RawScryptoValue<'a> = RawValue<'a, ScryptoCustomExtension>;
pub type RawScryptoPayload<'a> = RawPayload<'a, ScryptoCustomExtension>;

// The following trait "aliases" are to be used in parameters.
//
Expand Down
11 changes: 7 additions & 4 deletions radix-engine-common/src/types/type_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::crypto::Hash;
use crate::Sbor;
use sbor::LocalTypeIndex;
use crate::internal_prelude::*;

define_wrapped_hash!(
/// Represents a particular schema under a package
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice macro!

SchemaHash
);

/// An Identifier for a structural type. This can be treated almost
/// like a pointer as two equivalent type identifiers will map to
/// the same schema
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Sbor)]
pub struct TypeIdentifier(pub Hash, pub LocalTypeIndex);
pub struct TypeIdentifier(pub SchemaHash, pub LocalTypeIndex);

/// A reference to the type to substitute with for the case of
/// generics.
Expand Down
125 changes: 24 additions & 101 deletions radix-engine-interface/src/blueprints/package/substates.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
use crate::api::CollectionIndex;
use crate::blueprints::package::BlueprintType;
use crate::schema::*;
use crate::types::*;
use crate::*;
use radix_engine_common::crypto::Hash;
use radix_engine_interface::blueprints::resource::Vault;
use sbor::rust::fmt;
use sbor::rust::fmt::{Debug, Formatter};
use sbor::rust::prelude::*;
use crate::internal_prelude::*;

pub const PACKAGE_CODE_ID: u64 = 0u64;
pub const RESOURCE_CODE_ID: u64 = 1u64;
Expand All @@ -22,90 +14,35 @@ pub const ROLE_ASSIGNMENT_CODE_ID: u64 = 12u64;
pub const POOL_CODE_ID: u64 = 13u64;
pub const TRANSACTION_TRACKER_CODE_ID: u64 = 14u64;

pub const PACKAGE_FIELDS_PARTITION_OFFSET: PartitionOffset = PartitionOffset(0u8);

pub const PACKAGE_BLUEPRINTS_PARTITION_OFFSET: PartitionOffset = PartitionOffset(1u8);
pub const PACKAGE_BLUEPRINTS_COLLECTION_INDEX: CollectionIndex = 0u8;

pub const PACKAGE_BLUEPRINT_DEPENDENCIES_PARTITION_OFFSET: PartitionOffset = PartitionOffset(2u8);
pub const PACKAGE_BLUEPRINT_DEPENDENCIES_COLLECTION_INDEX: CollectionIndex = 1u8;

// There is no offset for the package schema collection as it is directly mapped to SCHEMAS_PARTITION
pub const PACKAGE_SCHEMAS_COLLECTION_INDEX: CollectionIndex = 2u8;

pub const PACKAGE_ROYALTY_PARTITION_OFFSET: PartitionOffset = PartitionOffset(3u8);
pub const PACKAGE_ROYALTY_COLLECTION_INDEX: CollectionIndex = 3u8;

pub const PACKAGE_AUTH_TEMPLATE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(4u8);
pub const PACKAGE_AUTH_TEMPLATE_COLLECTION_INDEX: CollectionIndex = 4u8;

pub const PACKAGE_VM_TYPE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(5u8);
pub const PACKAGE_VM_TYPE_COLLECTION_INDEX: CollectionIndex = 5u8;

pub const PACKAGE_ORIGINAL_CODE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(6u8);
pub const PACKAGE_ORIGINAL_CODE_COLLECTION_INDEX: CollectionIndex = 6u8;

pub const PACKAGE_INSTRUMENTED_CODE_PARTITION_OFFSET: PartitionOffset = PartitionOffset(7u8);
pub const PACKAGE_INSTRUMENTED_CODE_COLLECTION_INDEX: CollectionIndex = 7u8;
define_wrapped_hash!(
/// Represents a particular instance of code under a package
CodeHash
);

#[derive(Copy, Debug, Clone, PartialEq, Eq, Sbor)]
pub enum VmType {
Native,
ScryptoV1,
}

#[derive(Debug, Clone, Sbor, PartialEq, Eq)]
pub struct PackageVmTypeSubstate {
pub vm_type: VmType,
}

#[derive(Clone, Sbor, PartialEq, Eq)]
pub struct PackageOriginalCodeSubstate {
pub code: Vec<u8>,
}

#[derive(Clone, Sbor, PartialEq, Eq)]
pub struct PackageInstrumentedCodeSubstate {
pub code: Vec<u8>,
}

impl Debug for PackageOriginalCodeSubstate {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("PackageOriginalCodeSubstate")
.field("len", &self.code.len())
.finish()
}
}

impl Debug for PackageInstrumentedCodeSubstate {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("PackageInstrumentedCodeSubstate")
.field("len", &self.code.len())
.finish()
}
}

#[derive(Debug, PartialEq, Eq, ScryptoSbor)]
pub struct PackageRoyaltyAccumulatorSubstate {
/// The vault for collecting package royalties.
pub royalty_vault: Vault,
}

impl Clone for PackageRoyaltyAccumulatorSubstate {
fn clone(&self) -> Self {
Self {
royalty_vault: Vault(self.royalty_vault.0.clone()),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Sbor)]
pub enum BlueprintPayloadDef {
Static(TypeIdentifier), // Fully Resolved type is defined in package
Generic(u8), // Fully Resolved type is mapped directly to a generic defined by instance
// TODO: How to represent a structure containing a generic?
}

impl BlueprintPayloadDef {
pub fn from_type_ref(type_ref: TypeRef<LocalTypeIndex>, schema_hash: SchemaHash) -> Self {
match type_ref {
TypeRef::Static(type_index) => {
BlueprintPayloadDef::Static(TypeIdentifier(schema_hash, type_index))
}
TypeRef::Generic(index) => BlueprintPayloadDef::Generic(index),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Sbor)]
pub struct FunctionSchema {
pub receiver: Option<ReceiverInfo>,
Expand Down Expand Up @@ -160,7 +97,7 @@ pub struct BlueprintDependencies {

#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
pub struct PackageExport {
pub code_hash: Hash,
pub code_hash: CodeHash,
pub export_name: String,
}

Expand Down Expand Up @@ -339,7 +276,7 @@ pub struct IndexedStateSchema {

impl IndexedStateSchema {
pub fn from_schema(
schema_hash: Hash,
schema_hash: SchemaHash,
schema: BlueprintStateSchemaInit,
system_mappings: BTreeMap<usize, PartitionNumber>,
) -> Self {
Expand All @@ -350,20 +287,10 @@ impl IndexedStateSchema {
let schema_fields = schema
.fields
.into_iter()
.map(|field_schema| {
let pointer = match field_schema.field {
TypeRef::Static(type_index) => {
BlueprintPayloadDef::Static(TypeIdentifier(schema_hash, type_index))
}
TypeRef::Generic(instance_index) => {
BlueprintPayloadDef::Generic(instance_index)
}
};
FieldSchema {
field: pointer,
condition: field_schema.condition,
transience: field_schema.transience,
}
.map(|field_schema| FieldSchema {
field: BlueprintPayloadDef::from_type_ref(field_schema.field, schema_hash),
condition: field_schema.condition,
transience: field_schema.transience,
})
.collect();
fields = Some((
Expand All @@ -375,12 +302,8 @@ impl IndexedStateSchema {

let mut collections = Vec::new();
for (collection_index, collection_schema) in schema.collections.into_iter().enumerate() {
let schema = collection_schema.map(|type_ref| match type_ref {
TypeRef::Static(type_index) => {
BlueprintPayloadDef::Static(TypeIdentifier(schema_hash, type_index))
}
TypeRef::Generic(generic_index) => BlueprintPayloadDef::Generic(generic_index),
});
let schema = collection_schema
.map(|type_ref| BlueprintPayloadDef::from_type_ref(type_ref, schema_hash));

if let Some(partition_num) = system_mappings.get(&collection_index) {
collections.push((PartitionDescription::Physical(*partition_num), schema));
Expand Down
4 changes: 4 additions & 0 deletions radix-engine-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ pub mod prelude {
non_fungible_data_update_roles, recall_roles, role_entry, roles2, rule, withdraw_roles,
};
}

pub(crate) mod internal_prelude {
pub use crate::prelude::*;
}
28 changes: 0 additions & 28 deletions radix-engine-interface/src/types/node_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,13 @@ pub enum ComponentField {
State0,
}

#[repr(u8)]
#[derive(Debug, Clone, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord, FromRepr)]
pub enum PackageField {
Royalty,
}

#[repr(u8)]
#[derive(Debug, Clone, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord, FromRepr)]
pub enum FungibleResourceManagerField {
Divisibility,
TotalSupply,
}

#[repr(u8)]
#[derive(Debug, Clone, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord, FromRepr)]
pub enum PackagePartitionOffset {
Fields,
Blueprints,
BlueprintDependencies,
RoyaltyConfig,
AuthConfig,
VmType,
OriginalCode,
InstrumentedCode,
}

impl TryFrom<u8> for PackagePartitionOffset {
type Error = ();

fn try_from(offset: u8) -> Result<Self, Self::Error> {
Self::from_repr(offset).ok_or(())
}
}

#[repr(u8)]
#[derive(Debug, Clone, Sbor, PartialEq, Eq, Hash, PartialOrd, Ord, FromRepr)]
pub enum NonFungibleResourceManagerPartitionOffset {
Expand Down Expand Up @@ -320,7 +293,6 @@ substate_key!(TypeInfoField);
substate_key!(RoyaltyField);
substate_key!(RoleAssignmentField);
substate_key!(ComponentField);
substate_key!(PackageField);
substate_key!(FungibleResourceManagerField);
substate_key!(FungibleVaultField);
substate_key!(FungibleBucketField);
Expand Down
Loading
Loading