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] declare_native_blueprint_state extensions #1744

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions radix-clis/src/resim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ pub fn get_event_schema<S: SubstateDatabase>(
)
.unwrap()?;

let bp_interface = match bp_definition {
VersionedPackageBlueprintVersionDefinition::V1(blueprint) => blueprint.interface,
};
let bp_interface = bp_definition.into_latest().interface;

let event_def = bp_interface.events.get(event_name)?;
match event_def {
Expand Down
2 changes: 1 addition & 1 deletion radix-engine-interface/src/api/field_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub trait ClientFieldApi<E: Debug> {

fn field_read_typed<S: ScryptoDecode>(&mut self, handle: FieldHandle) -> Result<S, E> {
let buf = self.field_read(handle)?;
let typed_substate: S = scrypto_decode(&buf).map_err(|e| e).unwrap();
let typed_substate: S = scrypto_decode(&buf).unwrap();
Ok(typed_substate)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ mod test {
mutable_fields,
} = ds
{
let VersionedSchema::V1(s) = schema;
let s = schema.into_latest();
assert_eq!(s.type_kinds.len(), 1);
assert_eq!(s.type_metadata.len(), 1);
assert_eq!(s.type_validations.len(), 1);
Expand Down
7 changes: 1 addition & 6 deletions radix-engine-tests/tests/kernel/test_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,7 @@ fn references_read_from_state_are_visible_in_tests1() {
.with_component_state::<VersionedTwoResourcePoolState, _, _, _>(
radiswap_pool_component,
|state, env| {
let VersionedTwoResourcePoolState::V1(
radix_engine::blueprints::pool::v1::substates::two_resource_pool::Substate {
vaults: [(_, vault1), (_, _)],
..
},
) = state;
let [(_, vault1), (_, _)] = &mut state.into_latest_mut().vaults;
vault1.amount(env)
},
)
Expand Down
22 changes: 22 additions & 0 deletions radix-engine/src/blueprints/account/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,28 @@ impl AccountBlueprint {
Ok(())
}

pub fn set_default_deposit_rule_v2_example<Y>(
default: DefaultDepositRule,
api: AccountStateApi::<'_, Y>,
) -> Result<(), RuntimeError>
where
Y: ClientApi<RuntimeError>,
{
api.deposit_rule_field()
.open_readwrite()?
.into_latest_mut() // Could even have an `open_readwrite_latest() -> FieldLatestContentMut`
.default_deposit_rule = default;

Runtime::emit_event(
*api.raw_api(), // Could be replaced with api.emit_event
SetDefaultDepositRuleEvent {
default_deposit_rule: default,
},
)?;

Ok(())
}

pub fn set_resource_preference<Y>(
resource_address: ResourceAddress,
resource_preference: ResourcePreference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1247,12 +1247,9 @@ impl ConsensusManagerBlueprint {
// then let's be even more accurate here. This sort is stable, so if two validators tie, then the resultant order will be
// decided on sort key DESC.
top_registered_validators.sort_by(|(_, validator_1), (_, validator_2)| {
match (&validator_1.content, &validator_2.content) {
(
VersionedConsensusManagerRegisteredValidatorByStake::V1(validator1),
VersionedConsensusManagerRegisteredValidatorByStake::V1(validator2),
) => validator1.stake.cmp(&validator2.stake).reverse(),
}
let validator1 = validator_1.content.as_unique_latest_ref();
let validator2 = validator_2.content.as_unique_latest_ref();
validator1.stake.cmp(&validator2.stake).reverse()
});

let next_active_validator_set = ActiveValidatorSet {
Expand Down
134 changes: 134 additions & 0 deletions radix-engine/src/blueprints/models/feature_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,140 @@ pub trait HasFeatures: Debug {
}
}

/// For feature checks against a non-inner object
#[derive(Debug)]
pub enum FeatureChecks<TOwn: HasFeatures> {
None,
RequireAllSubstates,
ForFeatures { own_features: TOwn },
}

impl<T: HasFeatures> From<T> for FeatureChecks<T> {
fn from(value: T) -> Self {
FeatureChecks::ForFeatures {
own_features: value,
}
}
}

impl<TOwn: HasFeatures> FeatureChecks<TOwn> {
pub fn assert_valid(
&self,
substate_name: &'static str,
condition: &Condition,
is_present: bool,
) -> Result<(), RuntimeError> {
let is_valid = match self {
FeatureChecks::None => Ok(()),
FeatureChecks::RequireAllSubstates => {
if is_present {
Ok(())
} else {
Err(format!("Required all substates to be present, but {} was not present", substate_name))
}
},
FeatureChecks::ForFeatures { own_features } => {
match condition {
Condition::Always => {
if is_present {
Ok(())
} else {
Err(format!("Substate condition for {} required it to be always present, but it was not", substate_name))
}
}
Condition::IfFeature(feature) => {
let feature_enabled = own_features.feature_names_str().contains(&feature.as_str());
if feature_enabled && !is_present {
Err(format!("Substate condition for {} required it to be present when the feature {} was enabled, but it was absent", substate_name, feature))
} else if !feature_enabled && is_present {
Err(format!("Substate condition for {} required it to be absent when the feature {} was disabled, but it was present", substate_name, feature))
} else {
Ok(())
}
},
Condition::IfOuterFeature(_) => {
Err(format!("Substate condition for {} required an outer object feature, but the blueprint does not have an outer blueprint defined", substate_name))
}
}
},
};
is_valid.map_err(|error_message| {
RuntimeError::SystemError(SystemError::InvalidNativeSubstatesForFeature(error_message))
})
}
}

/// For feature checks against an inner object
pub enum InnerObjectFeatureChecks<TOwn, TOuter> {
None,
RequireAllSubstates,
ForFeatures {
own_features: TOwn,
outer_object_features: TOuter,
},
}

impl<TOwn: HasFeatures, TOuter: HasFeatures> InnerObjectFeatureChecks<TOwn, TOuter> {
pub fn assert_valid(
&self,
substate_name: &'static str,
condition: &Condition,
is_present: bool,
) -> Result<(), RuntimeError> {
let is_valid = match self {
Self::None => Ok(()),
Self::RequireAllSubstates => {
if is_present {
Ok(())
} else {
Err(format!(
"Required all substates to be present, but {} was not present",
substate_name
))
}
}
Self::ForFeatures {
own_features,
outer_object_features,
} => match condition {
Condition::Always => {
if is_present {
Ok(())
} else {
Err(format!("Substate condition for {} required it to be always present, but it was not", substate_name))
}
}
Condition::IfFeature(feature) => {
let feature_enabled =
own_features.feature_names_str().contains(&feature.as_str());
if feature_enabled && !is_present {
Err(format!("Substate condition for {} required it to be present when the feature {} was enabled, but it was absent", substate_name, feature))
} else if !feature_enabled && is_present {
Err(format!("Substate condition for {} required it to be absent when the feature {} was disabled, but it was present", substate_name, feature))
} else {
Ok(())
}
}
Condition::IfOuterFeature(feature) => {
let feature_enabled = outer_object_features
.feature_names_str()
.contains(&feature.as_str());
if feature_enabled && !is_present {
Err(format!("Substate condition for {} required it to be present when the outer object feature {} was enabled, but it was absent", substate_name, feature))
} else if !feature_enabled && is_present {
Err(format!("Substate condition for {} required it to be absent when the outer object feature {} was disabled, but it was present", substate_name, feature))
} else {
Ok(())
}
}
},
};
is_valid.map_err(|error_message| {
RuntimeError::SystemError(SystemError::InvalidNativeSubstatesForFeature(error_message))
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
Loading