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

rework: Rework the manifest resource interpreter #1931

Conversation

dhedey
Copy link
Contributor

@dhedey dhedey commented Sep 28, 2024

Summary

Reworks some implementation of #1909 to be more powerful.

Copy link

Phylum Report Link

@dhedey dhedey force-pushed the feature/resource-movements-static-analysis-rework branch 2 times, most recently from d499922 to 1a9c066 Compare September 28, 2024 04:53
Copy link

github-actions bot commented Sep 28, 2024

Docker tags
docker.io/radixdlt/private-scrypto-builder:59e955e6f1

@dhedey dhedey force-pushed the feature/resource-movements-static-analysis-rework branch from 1a9c066 to 269382e Compare September 28, 2024 05:02
@dhedey dhedey force-pushed the feature/resource-movements-static-analysis-rework branch from e66bec1 to be98983 Compare September 28, 2024 15:11
Copy link

github-actions bot commented Sep 28, 2024

Benchmark for 59e955e

Click to view benchmark
Test Base PR %
costing::bench_prepare_wasm 45.4±0.21ms 45.2±0.28ms -0.44%
costing::decode_encoded_i8_array_to_manifest_raw_value 20.6±0.08ms 19.0±0.03ms -7.77%
costing::decode_encoded_i8_array_to_manifest_value 41.4±0.11ms 42.1±0.10ms +1.69%
costing::decode_encoded_tuple_array_to_manifest_raw_value 71.6±0.11ms 70.5±0.13ms -1.54%
costing::decode_encoded_tuple_array_to_manifest_value 101.0±0.21ms 102.5±0.18ms +1.49%
costing::decode_encoded_u8_array_to_manifest_raw_value 25.8±0.12µs 25.8±0.08µs 0.00%
costing::decode_encoded_u8_array_to_manifest_value 41.8±0.09ms 42.2±0.07ms +0.96%
costing::decode_rpd_to_manifest_raw_value 14.9±0.08µs 14.1±0.03µs -5.37%
costing::decode_rpd_to_manifest_value 11.1±0.03µs 11.1±0.04µs 0.00%
costing::deserialize_wasm 1286.0±3.59µs 1257.6±5.31µs -2.21%
costing::execute_transaction_creating_big_vec_substates 700.7±9.55ms 696.5±8.99ms -0.60%
costing::execute_transaction_reading_big_vec_substates 680.6±4.03ms 595.7±1.44ms -12.47%
costing::instantiate_flash_loan 903.1±586.07µs 971.4±1168.84µs +7.56%
costing::instantiate_radiswap 929.5±578.62µs 1092.5±1882.98µs +17.54%
costing::spin_loop 20.7±0.07ms 20.7±0.04ms 0.00%
costing::validate_sbor_payload 32.4±0.14µs 32.1±0.07µs -0.93%
costing::validate_sbor_payload_bytes 245.4±0.58ns 261.8±0.40ns +6.68%
costing::validate_secp256k1 76.8±0.12µs 76.6±0.10µs -0.26%
costing::validate_wasm 33.8±0.06ms 34.0±0.07ms +0.59%
decimal::add/0 8.4±0.02ns 8.4±0.01ns 0.00%
decimal::add/rust-native 9.9±0.01ns 9.9±0.01ns 0.00%
decimal::add/wasmi 231.4±0.57ns 222.5±0.36ns -3.85%
decimal::add/wasmi-call-native 2.1±0.00µs 2.1±0.00µs 0.00%
decimal::div/0 185.2±0.17ns 185.4±0.35ns +0.11%
decimal::from_string/0 155.5±0.36ns 158.4±0.41ns +1.86%
decimal::mul/0 149.6±0.42ns 149.8±0.59ns +0.13%
decimal::mul/rust-native 147.9±0.14ns 147.9±0.19ns 0.00%
decimal::mul/wasmi 12.1±0.06µs 11.9±0.10µs -1.65%
decimal::mul/wasmi-call-native 2.3±0.00µs 2.2±0.00µs -4.35%
decimal::pow/0 701.0±1.06ns 702.6±1.73ns +0.23%
decimal::pow/rust-native 669.7±1.28ns 664.6±0.43ns -0.76%
decimal::pow/wasmi 57.3±0.27µs 57.6±0.25µs +0.52%
decimal::pow/wasmi-call-native 2.5±0.01µs 2.5±0.01µs 0.00%
decimal::root/0 7.9±0.01µs 8.0±0.01µs +1.27%
decimal::sub/0 8.2±0.02ns 8.2±0.01ns 0.00%
decimal::to_string/0 449.0±0.77ns 443.4±1.09ns -1.25%
large_transaction_processing::prepare 2.6±0.00ms 2.6±0.01ms 0.00%
large_transaction_processing::prepare_and_decompile 6.9±0.02ms 7.0±0.04ms +1.45%
large_transaction_processing::prepare_and_decompile_and_recompile 29.1±2.56ms 32.2±0.20ms +10.65%
precise_decimal::add/0 8.8±0.02ns 8.8±0.03ns 0.00%
precise_decimal::add/rust-native 10.8±0.02ns 10.8±0.03ns 0.00%
precise_decimal::add/wasmi 275.0±0.93ns 271.4±0.52ns -1.31%
precise_decimal::add/wasmi-call-native 2.9±0.00µs 2.8±0.01µs -3.45%
precise_decimal::div/0 354.9±6.00ns 318.1±0.60ns -10.37%
precise_decimal::from_string/0 204.8±0.46ns 206.5±0.48ns +0.83%
precise_decimal::mul/0 377.7±0.59ns 365.6±0.86ns -3.20%
precise_decimal::mul/rust-native 320.6±0.25ns 321.4±0.75ns +0.25%
precise_decimal::mul/wasmi 35.3±0.27µs 34.1±0.06µs -3.40%
precise_decimal::mul/wasmi-call-native 3.2±0.01µs 3.2±0.01µs 0.00%
precise_decimal::pow/0 1927.8±1.89ns 1923.5±0.94ns -0.22%
precise_decimal::pow/rust-native 1527.8±1.09ns 1546.7±5.53ns +1.24%
precise_decimal::pow/wasmi 165.7±1.75µs 165.5±0.54µs -0.12%
precise_decimal::pow/wasmi-call-native 5.8±0.01µs 5.8±0.02µs 0.00%
precise_decimal::root/0 61.4±0.25µs 58.9±0.14µs -4.07%
precise_decimal::sub/0 9.1±0.10ns 9.0±0.18ns -1.10%
precise_decimal::to_string/0 693.5±1.16ns 702.8±1.48ns +1.34%
schema::validate_payload 366.6±0.97µs 365.8±0.35µs -0.22%
transaction::radiswap 5.2±0.02ms 5.2±0.02ms 0.00%
transaction::transfer 1894.5±6.60µs 1849.1±8.78µs -2.40%
transaction_validation::validate_manifest 43.1±0.14µs 43.1±0.06µs 0.00%
transaction_validation::verify_bls_2KB 1004.4±11.59µs 1012.9±19.99µs +0.85%
transaction_validation::verify_bls_32B 1004.2±11.03µs 1006.0±13.93µs +0.18%
transaction_validation::verify_ecdsa 74.8±0.38µs 74.6±0.10µs -0.27%
transaction_validation::verify_ed25519 57.6±0.89µs 55.3±0.23µs -3.99%

@dhedey dhedey marked this pull request as ready for review October 1, 2024 00:17
ValueKind::Array => Self::ManifestBuckets(
Vec::<ManifestBucket>::decode_body_with_value_kind(decoder, value_kind)?,
),
ValueKind::Custom(_) => {
Copy link
Member

Choose a reason for hiding this comment

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

Probably should be ValueKind::Custom(ManifestCustomValueKind::Expression)

Copy link
Contributor Author

@dhedey dhedey Oct 1, 2024

Choose a reason for hiding this comment

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

Could be - I think it's fine either way, this is just a hint to work out how to branch - it'll get verified in the Expression decode method anyway :)

@@ -364,6 +368,15 @@ impl CheckedAdd<Decimal> for Decimal {
}
}

impl SaturatingAdd<Decimal> for Decimal {
Copy link
Member

Choose a reason for hiding this comment

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

I assume that this is no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, but may as well keep it?

@@ -366,6 +366,15 @@ macro_rules! op_impl {
}
}

impl SaturatingAdd for $t
Copy link
Member

Choose a reason for hiding this comment

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

I assume that this is no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, but may as well keep it?

@@ -6,6 +6,14 @@ pub trait CheckedAdd<Rhs = Self> {
Self: Sized;
}

pub trait SaturatingAdd<Rhs = Self> {
Copy link
Member

Choose a reason for hiding this comment

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

I assume that this is no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, but may as well keep it?

@@ -72,6 +72,10 @@ impl NodeId {
matches!(self.entity_type(), Some(t) if t.is_global_component())
}

pub const fn is_global_account(&self) -> bool {
matches!(self.entity_type(), Some(t) if t.is_global_account())
Copy link
Member

Choose a reason for hiding this comment

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

Consistency aside, a nicer way to implement this is:

self.entity_type().is_some_and(|entity_type| entity_type.is_global_account())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't work because these methods are const, and is_some_and is not const :D

method_name: &str,
args: &::radix_common::prelude::ManifestValue
) -> Result<Option<Self>, StaticResourceMovementsError> {
Ok(match *package_address {
Copy link
Member

Choose a reason for hiding this comment

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

To reduce nesting in the generated code and improve the readability of the macro code I think we should match over a tuple of package address and blueprint name.

let maybe_self = match (*package_address, blueprint_name) {
    $(
        $(
            ($package_address, stringify!($blueprint_name)) => {
                let invocation = [< $blueprint_name Method >]::from_invocation(method_name, args)?;
                Some(Self::[< $package_name Package >](
                    [< $package_name Invocations >]::[< $blueprint_name Blueprint >](
                        [< $blueprint_name BlueprintInvocations >]::Method(invocation)
                    )
                ))
            }
        )*
    )*
    _ => Err(StaticResourceMovementsError::UnknownNativeBlueprint {
        package: *package_address,
        blueprint: blueprint_name.to_string(),
    }),
};
Ok(maybe_self)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't work.

We need a different error message for "package address not matched" (the package address is not native - or not supported yet - so we return None for not-matched)" to "package address is native and method is not detected" (this is an error, because our method mapping for each package should be complete)

pub fn from_method_invocation(
address: &NodeId,
module_id: ::radix_engine_interface::prelude::ModuleId,
pub fn from_main_module_method_invocation(
Copy link
Member

Choose a reason for hiding this comment

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

Something about the new constructors isn't quite sitting right with me... Maybe we should combine the CallMethod invocation constructors into a single constructor that takes in a DynamicGlobalAddress?

This new DynamicGlobalAddress would be defined as:

pub enum DynamicGlobalAddress {
    Static(GlobalAddress),
    Named {
        package_address: PackageAddress,
        blueprint_name: String
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly - although we shouldn't call it a DynamicGlobalAddress because that clashes with the existing enum.
And internally, we will still need to delegate to two different match statements.

But yes, I'd support adding back a module_id in this API and letting a high level API then delegate to either a blueprint or entity type match. (e.g. a module id of metadata would go to a blueprint match on the metadata module blueprint)

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TrackedResources {
/// Captures the bounds of explicitly tracked resources.
/// Some of these may be
Copy link
Member

Choose a reason for hiding this comment

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

Comment is incomplete :)

}

pub fn new_with_possible_balance_of_unspecified_resources(
change_sources: impl IntoIterator<Item = ChangeSource>,
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps it doesn't need to be an iterator and can just be a ChangeSource.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In principle, you could have multiple sources here - e.g. in a test assertion, if we have a subintent start, then a random call, and then a deposit worktop, then the deposit would have two uncertainty sources.

@0xOmarA 0xOmarA merged commit 658b9ff into feature/resource-movements-static-analysis Oct 1, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants