Skip to content

Commit

Permalink
Update scrypto dependency and add static validation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Oct 30, 2024
1 parent fc1da69 commit 5a03cec
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 163 deletions.
229 changes: 113 additions & 116 deletions Cargo.lock

Large diffs are not rendered by default.

47 changes: 18 additions & 29 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,24 @@ members = [
# Shared dependencies that all of the crates need - for now this is just the
# `radixdlt-scrypto` dependencies
[workspace.dependencies]

# ================================================================
# DEPENDENCIES ON RADIXDLT-SCRYPTO
# ================================================================
#
# To make builds reproducible, we must specify a fixed tag below, not a branch.
#
# Go to your checkout of `radixdlt-scrypto`, then run the following:
# $ git checkout <relevant-branch>
# $ git pull
# $ git rev-parse HEAD | cut -c1-8
# $ git tag "release_name-<8 char hash id from previous command>"
# $ git push origin "release_name-BLAH"
#
# Then use tag="release_name-BLAH" in the below dependencies.
# =================================================================

sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582", features = ["serde"] }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582", features = ["serde"] }
scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582", features = ["serde"] }
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-substate-store-queries = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-substate-store-impls = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-substate-store-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-transactions = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-transaction-scenarios = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
radix-engine-toolkit-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "cuttlefish-2ea8b582" }
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3", features = [
"serde",
] }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3", features = [
"serde",
] }
scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3", features = [
"serde",
] }
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-substate-store-queries = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-substate-store-impls = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-substate-store-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-transactions = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-transaction-scenarios = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }
radix-engine-toolkit-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "9cdcfd4292044ee1def2cb37aa9807cef95f74a3" }

[profile.release]
incremental = false
Expand Down
16 changes: 8 additions & 8 deletions crates/radix-engine-toolkit-uniffi/src/common/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
use crate::prelude::*;

macro_rules! define_uniffi_decimal {
($type: ty) => {
($type: ty, $to_inner: ident, $from_inner: ident) => {
paste::paste!{
define_uniffi_decimal!{[<$type>],$crate::prelude::[<Native $type>],$crate::prelude::[<NativeInner $type>]}
define_uniffi_decimal!{[<$type>],$crate::prelude::[<Native $type>],$crate::prelude::[<NativeInner $type>], $to_inner, $from_inner}
}
};
($ident: ident, $native_type: ty, $native_inner_type: ty) => {
($ident: ident, $native_type: ty, $native_inner_type: ty, $to_inner: ident, $from_inner: ident) => {
paste::paste! {
#[derive(Clone, Debug, $crate::prelude::Object, Default)]
pub struct $ident(pub(crate) $native_type);
Expand Down Expand Up @@ -174,16 +174,16 @@ macro_rules! define_uniffi_decimal {
}

pub fn mantissa(&self) -> String {
self.0.0.to_string()
self.0.$to_inner().to_string()
}

pub fn to_le_bytes(&self) -> Vec<u8> {
self.0.0.to_le_bytes().to_vec()
self.0.$to_inner().to_le_bytes().to_vec()
}

#[uniffi::constructor]
pub fn from_le_bytes(value: &Vec<u8>) -> $crate::prelude::Arc<Self> {
$crate::prelude::Arc::new(Self($native_type($native_inner_type::from_le_bytes(
$crate::prelude::Arc::new(Self($native_type::$from_inner($native_inner_type::from_le_bytes(
&value
))))
}
Expand All @@ -192,8 +192,8 @@ macro_rules! define_uniffi_decimal {
}
}
}
define_uniffi_decimal!(Decimal);
define_uniffi_decimal!(PreciseDecimal);
define_uniffi_decimal!(Decimal, attos, from_attos);
define_uniffi_decimal!(PreciseDecimal, precise_subunits, from_precise_subunits);

#[derive(Clone, Debug, Enum)]
pub enum WithdrawStrategy {
Expand Down
4 changes: 4 additions & 0 deletions crates/radix-engine-toolkit-uniffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub enum RadixEngineToolkitError {

#[error("Not all of the builder items were specified, progressing failed.")]
NotAllBuilderItemsWereSpecified,

#[error("Validation of manifest failed: {error}")]
ManifestValidationError { error: String },
}

macro_rules! dbg_str {
Expand Down Expand Up @@ -186,6 +189,7 @@ impl_dbg_str_from! { CoreManifestSborError, ManifestSborError }
impl_dbg_str_from! { CoreScryptoSborError, ScryptoSborError }
impl_dbg_str_from! { NativeTypedNativeEventError, TypedNativeEventError }
impl_dbg_str_from! { CoreInvalidEntityTypeIdError, InvalidEntityTypeIdError }
impl_dbg_str_from! { NativeManifestValidationError, ManifestValidationError }
impl_dbg_str_from! { NativeStaticResourceMovementsError, StaticAnalysisFailed }

impl From<NameRecordError> for RadixEngineToolkitError {
Expand Down
10 changes: 8 additions & 2 deletions crates/radix-engine-toolkit-uniffi/src/internal_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ mod core {
from_payload_bytes as core_transaction_v2_transaction_manifest_from_payload_bytes,
statically_analyze as core_transaction_v2_transaction_manifest_statically_analyze,
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_validate as core_transaction_v2_subintent_manifest_statically_validate,
as_enclosed as core_transaction_v2_subintent_manifest_as_enclosed,
};
pub use radix_engine_toolkit::functions::transaction_v2::subintent::{
Expand Down Expand Up @@ -128,6 +130,7 @@ mod core {
hash as core_transaction_v2_signed_partial_transaction_hash,
to_payload_bytes as core_transaction_v2_signed_partial_transaction_to_payload_bytes,
from_payload_bytes as core_transaction_v2_signed_partial_transaction_from_payload_bytes,
statically_validate as core_transaction_v2_signed_partial_transaction_statically_validate,
};
pub use radix_engine_toolkit::functions::manifest_sbor::{
ManifestSborError as CoreManifestSborError,
Expand Down Expand Up @@ -225,6 +228,9 @@ mod native {
SimpleFungibleResourceBounds as NativeSimpleFungibleResourceBounds,
SimpleNonFungibleResourceBounds as NativeSimpleNonFungibleResourceBounds,
};
pub use radix_transactions::manifest::{
ManifestValidationError as NativeManifestValidationError
};
pub use radix_common::prelude::{
NetworkDefinition as NativeNetworkDefinition,

Expand Down Expand Up @@ -514,8 +520,8 @@ mod native {
NamedManifestAddress as NativeNamedManifestAddress,

SubintentHash as NativeSubintentHash,
ChildSubintent as NativeChildSubintent,
ChildIntentsV2 as NativeChildIntentsV2
ChildSubintentSpecifier as NativeChildSubintentSpecifier,
ChildSubintentSpecifiersV2 as NativeChildSubintentSpecifiersV2
};
pub use radix_transactions::validation::{
MessageValidationConfig as NativeMessageValidationConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ impl TryFrom<IntentCoreV2> for NativeIntentCoreV2 {
blobs: blobs.into_iter().map(NativeBlobV1).collect(),
},
message: NativeMessageV2::try_from(message)?,
children: NativeChildIntentsV2 {
children: NativeChildSubintentSpecifiersV2 {
children: children
.into_iter()
.map(|value| NativeSubintentHash(value.0))
.map(|hash| NativeChildSubintent { hash })
.map(|hash| NativeChildSubintentSpecifier { hash })
.collect(),
},
instructions: NativeInstructionsV2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl NotarizedTransactionV2 {
},
)
}

pub fn statically_validate(&self, network_id: u8) -> Result<()> {
core_transaction_v2_notarized_transaction_statically_validate(
&self.clone().try_into()?,
&core_network_definition_from_network_id(network_id),
)
.map_err(Into::into)
}
}

impl TryFrom<NativeNotarizedTransactionV2> for NotarizedTransactionV2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ impl SignedPartialTransactionV2 {
},
)
}

pub fn statically_validate(&self, network_id: u8) -> Result<()> {
core_transaction_v2_signed_partial_transaction_statically_validate(
&self.clone().try_into()?,
&core_network_definition_from_network_id(network_id),
)
.map_err(Into::into)
}
}

impl TryFrom<NativeSignedPartialTransactionV2> for SignedPartialTransactionV2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ impl SubintentManifestV2 {
Arc::new(TransactionManifestV2::from_native(&manifest, network_id))
})
}

pub fn statically_validate(&self) -> Result<()> {
core_transaction_v2_subintent_manifest_statically_validate(
&self.clone().to_native(),
)
.map_err(Into::into)
}
}

impl SubintentManifestV2 {
Expand Down Expand Up @@ -147,7 +154,7 @@ impl SubintentManifestV2 {
.children
.iter()
.map(|value| NativeSubintentHash(value.as_ref().0))
.map(|value| NativeChildSubintent { hash: value })
.map(|value| NativeChildSubintentSpecifier { hash: value })
.collect(),
object_names: Default::default(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ impl TransactionManifestV2 {
.map_err(|_| RadixEngineToolkitError::InvalidReceipt)
.map(|summary| DynamicAnalysis::from_native(summary, network_id))?
}

pub fn statically_validate(&self) -> Result<()> {
core_transaction_v2_transaction_manifest_statically_validate(
&self.clone().to_native(),
)
.map_err(Into::into)
}
}

impl TransactionManifestV2 {
Expand Down Expand Up @@ -164,7 +171,7 @@ impl TransactionManifestV2 {
.children
.iter()
.map(|value| NativeSubintentHash(value.as_ref().0))
.map(|value| NativeChildSubintent { hash: value })
.map(|value| NativeChildSubintentSpecifier { hash: value })
.collect(),
object_names: Default::default(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn statically_validate(
)?;
TransactionValidator::new_with_latest_config(network_definition)
.validate_instructions_v1(instructions, blobs)
.map_err(InstructionValidationError::TransactionValidationError)?;
.map_err(InstructionValidationError::IntentValidationError)?;
Ok(())
}

Expand All @@ -64,6 +64,7 @@ pub fn extract_addresses(

#[derive(Clone, Debug)]
pub enum InstructionValidationError {
IntentValidationError(IntentValidationError),
TransactionValidationError(TransactionValidationError),
LocatedInstructionSchemaValidationError(
LocatedInstructionSchemaValidationError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,13 @@ pub fn statically_validate(
intent
.prepare(&PreparationSettings::latest())
.map_err(TransactionValidationError::PrepareError)
.and_then(|prepared| validator.validate_intent_v1(&prepared))
.and_then(|prepared| {
validator.validate_intent_v1(&prepared).map_err(|error| {
TransactionValidationError::IntentValidationError(
TransactionValidationErrorLocation::Unlocatable,
error,
)
})
})
.map(|_| ())
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub fn statically_validate(
) -> Result<(), TransactionValidationError> {
TransactionValidator::new_with_latest_config(network_definition)
.validate_instructions_v1(&manifest.instructions, &manifest.blobs)
.map_err(|error| {
TransactionValidationError::IntentValidationError(
TransactionValidationErrorLocation::Unlocatable,
error,
)
})
}

pub fn statically_analyze(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,15 @@ pub fn statically_validate(
signed_intent
.prepare(&PreparationSettings::latest())
.map_err(TransactionValidationError::PrepareError)
.and_then(|prepared| validator.validate_intent_v1(&prepared.intent))
.and_then(|prepared| {
validator
.validate_intent_v1(&prepared.intent)
.map_err(|error| {
TransactionValidationError::IntentValidationError(
TransactionValidationErrorLocation::Unlocatable,
error,
)
})
})
.map(|_| ())
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// specific language governing permissions and limitations
// under the License.

use radix_transactions::errors::*;
use radix_transactions::model::*;
use radix_transactions::validation::*;
use sbor::*;
use scrypto::prelude::*;

Expand Down Expand Up @@ -56,3 +58,18 @@ where
&payload_bytes.as_ref().to_vec().into(),
)
}

pub fn statically_validate(
signed_partial_transaction: &SignedPartialTransactionV2,
network_definition: &NetworkDefinition,
) -> Result<(), TransactionValidationError> {
let validator =
TransactionValidator::new_with_latest_config(network_definition);
signed_partial_transaction
.prepare(&PreparationSettings::latest())
.map_err(TransactionValidationError::PrepareError)
.and_then(|prepared| {
validator.validate_signed_partial_transaction_v2(prepared)
})
.map(|_| ())
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,30 @@ pub fn as_enclosed(
None
}
}

pub fn statically_validate(
manifest: &SubintentManifestV2,
) -> Result<(), ManifestValidationError> {
pub struct Error(ManifestValidationError);
impl From<ManifestValidationError> for Error {
fn from(value: ManifestValidationError) -> Self {
Self(value)
}
}
impl From<Error> for ManifestValidationError {
fn from(value: Error) -> Self {
value.0
}
}

pub struct Visitor;
impl ManifestInterpretationVisitor for Visitor {
type Output = Error;
}

let interpreter =
StaticManifestInterpreter::new(ValidationRuleset::all(), manifest);
interpreter.validate_and_apply_visitor(&mut Visitor)?;

Ok(())
}
Loading

0 comments on commit 5a03cec

Please sign in to comment.