Skip to content

Commit

Permalink
re-expose static-analysis method
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Nov 1, 2024
1 parent d7ecce4 commit 2b797ad
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 121 deletions.
6 changes: 4 additions & 2 deletions crates/radix-engine-toolkit-uniffi/src/internal_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ mod core {
to_payload_bytes as core_transaction_v1_manifest_to_payload_bytes,
from_payload_bytes as core_transaction_v1_manifest_from_payload_bytes,
statically_validate as core_transaction_v1_manifest_statically_validate,
classify as core_transaction_v1_manifest_classify,
statically_analyze as core_transaction_v1_manifest_statically_analyze,
statically_analyze_and_validate as core_transaction_v1_manifest_statically_analyze_and_validate,
dynamically_analyze as core_transaction_v1_manifest_dynamically_analyze,
};
pub use radix_engine_toolkit::functions::transaction_v1::intent::{
Expand All @@ -90,15 +90,16 @@ mod core {
pub use radix_engine_toolkit::functions::transaction_v2::transaction_manifest::{
to_payload_bytes as core_transaction_v2_transaction_manifest_to_payload_bytes,
from_payload_bytes as core_transaction_v2_transaction_manifest_from_payload_bytes,
classify as core_transaction_v2_transaction_manifest_classify,
statically_analyze as core_transaction_v2_transaction_manifest_statically_analyze,
statically_analyze_and_validate as core_transaction_v2_transaction_manifest_statically_analyze_and_validate,
dynamically_analyze as core_transaction_v2_transaction_manifest_dynamically_analyze,
statically_validate as core_transaction_v2_transaction_manifest_statically_validate,
};
pub use radix_engine_toolkit::functions::transaction_v2::subintent_manifest::{
to_payload_bytes as core_transaction_v2_subintent_manifest_to_payload_bytes,
from_payload_bytes as core_transaction_v2_subintent_manifest_from_payload_bytes,
statically_analyze as core_transaction_v2_subintent_manifest_statically_analyze,
statically_analyze_and_validate as core_transaction_v2_subintent_manifest_statically_analyze_and_validate,
statically_validate as core_transaction_v2_subintent_manifest_statically_validate,
as_enclosed as core_transaction_v2_subintent_manifest_as_enclosed,
};
Expand Down Expand Up @@ -154,6 +155,7 @@ mod core {
DynamicAnalysisCallback as CoreDynamicAnalysisCallback,
TransactionTypesError as CoreTransactionTypesError,
StaticAnalysis as CoreStaticAnalysis,
StaticAnalysisWithResourceMovements as CoreStaticAnalysisWithResourceMovements,
DynamicAnalysis as CoreDynamicAnalysis,
TransactionTypesReceipt as CoreTransactionTypesReceipt,
NewEntities as CoreNewEntities,
Expand Down
135 changes: 120 additions & 15 deletions crates/radix-engine-toolkit-uniffi/src/transaction_v1/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,30 @@ impl TransactionManifestV1 {
map
}

pub fn static_analysis_and_validate(
pub fn statically_analyze(&self, network_id: u8) -> StaticAnalysis {
let native = self.clone().to_native();
StaticAnalysis::from_native(
core_transaction_v1_manifest_statically_analyze(&native),
network_id,
)
}

pub fn statically_analyze_and_validate(
&self,
network_id: u8,
) -> Result<StaticAnalysis> {
) -> Result<StaticAnalysisWithResourceMovements> {
let native = self.clone().to_native();
core_transaction_v1_manifest_statically_analyze(&native)
core_transaction_v1_manifest_statically_analyze_and_validate(&native)
.map_err(RadixEngineToolkitError::from)
.map(|static_analysis| {
StaticAnalysis::from_native(static_analysis, network_id)
StaticAnalysisWithResourceMovements::from_native(
static_analysis,
network_id,
)
})
}

pub fn classify(&self) -> Vec<ManifestClass> {
core_transaction_v1_manifest_classify(&self.to_native())
.into_iter()
.map(ManifestClass::from)
.collect()
}

pub fn dynamic_analysis(
pub fn dynamically_analyze(
&self,
network_id: u8,
toolkit_receipt: String,
Expand Down Expand Up @@ -1048,7 +1052,7 @@ impl From<UpperBound> for NativeUpperBound {
}

#[derive(Clone, Debug, Record)]
pub struct StaticAnalysis {
pub struct StaticAnalysisWithResourceMovements {
pub account_withdraws: HashMap<String, Vec<AccountWithdraw>>,
pub account_deposits: HashMap<String, Vec<AccountDeposit>>,
pub presented_proofs: HashMap<String, Vec<ResourceSpecifier>>,
Expand All @@ -1061,8 +1065,11 @@ pub struct StaticAnalysis {
pub classification: Vec<ManifestClass>,
}

impl StaticAnalysis {
pub fn from_native(native: CoreStaticAnalysis, network_id: u8) -> Self {
impl StaticAnalysisWithResourceMovements {
pub fn from_native(
native: CoreStaticAnalysisWithResourceMovements,
network_id: u8,
) -> Self {
Self {
account_withdraws: native
.account_withdraws
Expand Down Expand Up @@ -1179,6 +1186,104 @@ impl StaticAnalysis {
}
}

#[derive(Clone, Debug, Record)]
pub struct StaticAnalysis {
pub presented_proofs: HashMap<String, Vec<ResourceSpecifier>>,
pub accounts_withdrawn_from: Vec<Arc<Address>>,
pub accounts_deposited_into: Vec<Arc<Address>>,
pub encountered_entities: Vec<Arc<Address>>,
pub accounts_requiring_auth: Vec<Arc<Address>>,
pub identities_requiring_auth: Vec<Arc<Address>>,
pub reserved_instructions: Vec<ReservedInstruction>,
pub classification: Vec<ManifestClass>,
}

impl StaticAnalysis {
pub fn from_native(native: CoreStaticAnalysis, network_id: u8) -> Self {
Self {
presented_proofs: native
.presented_proofs
.into_iter()
.map(|item| {
(
Address::unsafe_from_raw(
item.0.into_node_id(),
network_id,
)
.address_string(),
item.1
.iter()
.map(|i| {
ResourceSpecifier::from_native(i, network_id)
})
.collect(),
)
})
.collect(),
accounts_withdrawn_from: native
.accounts_withdrawn_from
.into_iter()
.map(|item| {
Arc::new(Address::unsafe_from_raw(
item.into_node_id(),
network_id,
))
})
.collect(),
accounts_deposited_into: native
.accounts_deposited_into
.into_iter()
.map(|item| {
Arc::new(Address::unsafe_from_raw(
item.into_node_id(),
network_id,
))
})
.collect(),
encountered_entities: native
.encountered_entities
.into_iter()
.map(|item| {
Arc::new(Address::unsafe_from_raw(
item.into_node_id(),
network_id,
))
})
.collect(),
accounts_requiring_auth: native
.accounts_requiring_auth
.into_iter()
.map(|item| {
Arc::new(Address::unsafe_from_raw(
item.into_node_id(),
network_id,
))
})
.collect(),
identities_requiring_auth: native
.identities_requiring_auth
.into_iter()
.map(|item| {
Arc::new(Address::unsafe_from_raw(
item.into_node_id(),
network_id,
))
})
.collect(),
reserved_instructions: native
.reserved_instructions
.into_iter()
.map(ReservedInstruction::from)
.collect(),
classification: native
.classification
.into_iter()
.map(ManifestClass::from)
.collect(),
}
}
}

#[derive(Clone, Debug, Record)]
pub struct TrackedPoolContribution {
pub pool_address: Arc<Address>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ impl SubintentManifestV2 {
map
}

pub fn static_analysis(&self, network_id: u8) -> Result<StaticAnalysis> {
pub fn static_analysis(&self, network_id: u8) -> Result<StaticAnalysisWithResourceMovements> {
let native = self.clone().to_native();
core_transaction_v2_subintent_manifest_statically_analyze(&native)
.map_err(RadixEngineToolkitError::from)
.map(|static_analysis| {
StaticAnalysis::from_native(static_analysis, network_id)
})
core_transaction_v2_subintent_manifest_statically_analyze_and_validate(
&native,
)
.map_err(RadixEngineToolkitError::from)
.map(|static_analysis| {
StaticAnalysisWithResourceMovements::from_native(static_analysis, network_id)
})
}

pub fn as_enclosed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,29 @@ impl TransactionManifestV2 {
map
}

pub fn static_analysis_and_validate(
pub fn statically_analyze(&self, network_id: u8) -> StaticAnalysis {
let native = self.clone().to_native();
StaticAnalysis::from_native(
core_transaction_v2_transaction_manifest_statically_analyze(
&native,
),
network_id,
)
}

pub fn statically_analyze_and_validate(
&self,
network_id: u8,
) -> Result<StaticAnalysis> {
) -> Result<StaticAnalysisWithResourceMovements> {
let native = self.clone().to_native();
core_transaction_v2_transaction_manifest_statically_analyze(&native)
core_transaction_v2_transaction_manifest_statically_analyze_and_validate(&native)
.map_err(RadixEngineToolkitError::from)
.map(|static_analysis| {
StaticAnalysis::from_native(static_analysis, network_id)
StaticAnalysisWithResourceMovements::from_native(static_analysis, network_id)
})
}

pub fn dynamic_analysis(
pub fn dynamically_analyze(
&self,
network_id: u8,
toolkit_receipt: String,
Expand Down Expand Up @@ -135,13 +145,6 @@ impl TransactionManifestV2 {
)
.map_err(Into::into)
}

pub fn classify(&self) -> Vec<ManifestClass> {
core_transaction_v2_transaction_manifest_classify(&self.to_native())
.into_iter()
.map(ManifestClass::from)
.collect()
}
}

impl TransactionManifestV2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// specific language governing permissions and limitations
// under the License.

use indexmap::IndexSet;
use radix_common::prelude::*;
use radix_engine_toolkit_common::receipt::RuntimeToolkitTransactionReceipt;
use radix_transactions::errors::*;
Expand Down Expand Up @@ -55,14 +54,14 @@ pub fn statically_validate(
})
}

pub fn statically_analyze(
manifest: &TransactionManifestV1,
) -> Result<StaticAnalysis, StaticResourceMovementsError> {
pub fn statically_analyze(manifest: &TransactionManifestV1) -> StaticAnalysis {
crate::transaction_types::statically_analyze(manifest)
}

pub fn classify(manifest: &TransactionManifestV1) -> IndexSet<ManifestClass> {
crate::transaction_types::classify_manifest(manifest)
pub fn statically_analyze_and_validate(
manifest: &TransactionManifestV1,
) -> Result<StaticAnalysisWithResourceMovements, StaticResourceMovementsError> {
crate::transaction_types::statically_analyze_and_validate(manifest)
}

pub fn dynamically_analyze(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ where
SubintentManifestV2::from_raw(&payload_bytes.as_ref().to_vec().into())
}

pub fn statically_analyze(
manifest: &SubintentManifestV2,
) -> Result<StaticAnalysis, StaticResourceMovementsError> {
pub fn statically_analyze(manifest: &SubintentManifestV2) -> StaticAnalysis {
crate::transaction_types::statically_analyze(manifest)
}

pub fn statically_analyze_and_validate(
manifest: &SubintentManifestV2,
) -> Result<StaticAnalysisWithResourceMovements, StaticResourceMovementsError> {
crate::transaction_types::statically_analyze_and_validate(manifest)
}

pub fn as_enclosed(
SubintentManifestV2 {
instructions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ where
TransactionManifestV2::from_raw(&payload_bytes.as_ref().to_vec().into())
}

pub fn statically_analyze(
manifest: &TransactionManifestV2,
) -> Result<StaticAnalysis, StaticResourceMovementsError> {
pub fn statically_analyze(manifest: &TransactionManifestV2) -> StaticAnalysis {
crate::transaction_types::statically_analyze(manifest)
}

pub fn classify(manifest: &TransactionManifestV2) -> IndexSet<ManifestClass> {
crate::transaction_types::classify_manifest(manifest)
pub fn statically_analyze_and_validate(
manifest: &TransactionManifestV2,
) -> Result<StaticAnalysisWithResourceMovements, StaticResourceMovementsError> {
crate::transaction_types::statically_analyze_and_validate(manifest)
}

pub fn dynamically_analyze(
Expand Down
Loading

0 comments on commit 2b797ad

Please sign in to comment.