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

Use Dynamic*Address types in the ManifestInput models #1992

Merged
merged 7 commits into from
Nov 1, 2024
Merged
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
2 changes: 1 addition & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ cd "$(dirname "$0")"

echo "[PRE-COMMIT] Formatting assertion"

../../check.sh --quiet || { echo "[PRE-COMMIT] Formatting check FAIL: Committing aborted"; exit 1; }
../../format-check.sh --quiet || { echo "[PRE-COMMIT] Formatting check FAIL: Committing aborted"; exit 1; }

echo "[PRE-COMMIT] Formatting check PASS"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- name: Setup environment
uses: ./.github/actions/setup-env
- name: Check format
run: bash ./check.sh
run: bash ./format-check.sh

sbor-unit-tests:
name: Run SBOR unit tests
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

cd "$(dirname "$0")"

# This should align with format.sh, check.sh, test.sh, clean.sh, update-cargo-locks-minimally.sh
# This should align with format.sh, format-check.sh, check.sh, test.sh, clean.sh, update-cargo-locks-minimally.sh
echo "Building the workspace packages and tests (with all extended features)..."

(set -x; cargo build; cargo test --no-run; cargo bench --no-run)
Expand Down
4 changes: 2 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ packages+="$(find examples -mindepth 2 -maxdepth 2 -type f \( -name Cargo.toml \

for package in $packages; do
folder=$(dirname $package)
(cd $folder; cargo fmt --check $quiet_flag) || { echo "$lf>> Code format check FAILED for $package$lf"; failed=1; }
(cd $folder; cargo check $quiet_flag) || { echo "$lf>> Code compile check FAILED for $package$lf"; failed=1; }
done

[ $failed -eq 0 ] && echo "Code format check passed!"
[ $failed -eq 0 ] && echo "Code compile check passed!"
exit $failed
43 changes: 43 additions & 0 deletions format-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

set -eE

err_report() {
echo "Something went wrong on line $1"
}

quiet_flag=""

if [ "$1" = "--quiet" ]
then quiet_flag="--quiet"
fi

trap 'err_report $LINENO' ERR

failed=0
lf=$'\n'

cd "$(dirname "$0")"

# We use the cd trick to avoid issues like this: https://github.com/rust-lang/rustfmt/issues/4432

# This should align with format.sh, build.sh, test.sh, clean.sh, update-cargo-locks-minimally.sh
# ... and at some point, we should replace all these with a shared
# workspace lister function which returns this list stripped of Cargo.tomls
# and can be used by all these scripts to save duplication.
packages="Cargo.toml$lf"
packages+="radix-engine-tests/assets/blueprints/Cargo.toml$lf"
packages+="radix-clis/tests/blueprints/Cargo.toml$lf"
packages+="scrypto-test/tests/blueprints/Cargo.toml$lf"
packages+="scrypto-test/assets/blueprints/Cargo.toml$lf"
packages+="scrypto-compiler/tests/assets/scenario_1/Cargo.toml$lf"
packages+="scrypto-compiler/tests/assets/scenario_2/Cargo.toml$lf"
packages+="$(find examples -mindepth 2 -maxdepth 2 -type f \( -name Cargo.toml \))$lf"

for package in $packages; do
folder=$(dirname $package)
(cd $folder; cargo fmt --check $quiet_flag) || { echo "$lf>> Code format check FAILED for $package$lf"; failed=1; }
done

[ $failed -eq 0 ] && echo "Code format check passed!"
exit $failed
2 changes: 1 addition & 1 deletion format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e

cd "$(dirname "$0")"

# This should align with check.sh, build.sh, test.sh, clean.sh, update-cargo-locks-minimally.sh
# This should align with format-check.sh, check.sh, build.sh, test.sh, clean.sh, update-cargo-locks-minimally.sh

(set -x; cd .; cargo fmt)
(set -x; cd radix-engine-tests/assets/blueprints; cargo fmt)
Expand Down
7 changes: 7 additions & 0 deletions radix-common/src/data/manifest/model/manifest_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub struct ManifestNamedAddress(pub u32);
pub const MANIFEST_ADDRESS_DISCRIMINATOR_STATIC: u8 = 0u8;
pub const MANIFEST_ADDRESS_DISCRIMINATOR_NAMED: u8 = 1u8;

/// This is for use with the `ResolvableXAddress` traits, and is allowed to panic if the
/// parameters aren't valid.
pub trait NamedAddressResolver {
fn assert_named_address_exists(&self, named_address: ManifestNamedAddress);
fn resolve_named_address(&self, address_name: &str) -> ManifestNamedAddress;
}

//========
// binary
//========
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TAKE_FROM_WORKTOP
```
*/

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DynamicGlobalAddress {
Static(GlobalAddress),
Named(ManifestNamedAddress),
Expand Down Expand Up @@ -214,7 +214,45 @@ impl TryFrom<ManifestAddress> for DynamicGlobalAddress {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub trait ResolvableGlobalAddress: Sized {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicGlobalAddress;
}

impl<A, E> ResolvableGlobalAddress for A
where
A: TryInto<DynamicGlobalAddress, Error = E>,
E: Debug,
{
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicGlobalAddress {
let address = self
.try_into()
.expect("Address was not a valid DynamicGlobalAddress");
if let DynamicGlobalAddress::Named(named_address) = address {
resolver.assert_named_address_exists(named_address);
}
address
}
}

impl<'a> ResolvableGlobalAddress for &'a str {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicGlobalAddress {
resolver.resolve_named_address(self).into()
}
}

impl<'a> ResolvableGlobalAddress for &'a String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicGlobalAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

impl ResolvableGlobalAddress for String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicGlobalAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DynamicPackageAddress {
Static(PackageAddress),
Named(ManifestNamedAddress),
Expand Down Expand Up @@ -339,7 +377,56 @@ impl TryFrom<ManifestAddress> for DynamicPackageAddress {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub trait ResolvablePackageAddress: Sized {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicPackageAddress;

/// Note - this can be removed when all the static package addresses in the
/// manifest instructions are gone
fn resolve_static(self, resolver: &impl NamedAddressResolver) -> PackageAddress {
match self.resolve(resolver) {
DynamicPackageAddress::Static(address) => address,
DynamicPackageAddress::Named(_) => {
panic!("This address needs to be a static/fixed address")
}
}
}
}

impl<A, E> ResolvablePackageAddress for A
where
A: TryInto<DynamicPackageAddress, Error = E>,
E: Debug,
{
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicPackageAddress {
let address = self
.try_into()
.expect("Address was not a valid DynamicPackageAddress");
if let DynamicPackageAddress::Named(named_address) = address {
resolver.assert_named_address_exists(named_address);
}
address
}
}

impl<'a> ResolvablePackageAddress for &'a str {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicPackageAddress {
resolver.resolve_named_address(self).into()
}
}

impl<'a> ResolvablePackageAddress for &'a String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicPackageAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

impl ResolvablePackageAddress for String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicPackageAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DynamicComponentAddress {
Static(ComponentAddress),
Named(ManifestNamedAddress),
Expand Down Expand Up @@ -441,7 +528,45 @@ impl TryFrom<ManifestAddress> for DynamicComponentAddress {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub trait ResolvableComponentAddress {
fn resolve(self, registrar: &impl NamedAddressResolver) -> DynamicComponentAddress;
}

impl<A, E> ResolvableComponentAddress for A
where
A: TryInto<DynamicComponentAddress, Error = E>,
E: Debug,
{
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicComponentAddress {
let address = self
.try_into()
.expect("Address was not a valid DynamicComponentAddress");
if let DynamicComponentAddress::Named(named_address) = address {
resolver.assert_named_address_exists(named_address);
}
address
}
}

impl<'a> ResolvableComponentAddress for &'a str {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicComponentAddress {
resolver.resolve_named_address(self).into()
}
}

impl<'a> ResolvableComponentAddress for &'a String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicComponentAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

impl ResolvableComponentAddress for String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicComponentAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DynamicResourceAddress {
Static(ResourceAddress),
Named(ManifestNamedAddress),
Expand Down Expand Up @@ -542,3 +667,52 @@ impl TryFrom<ManifestAddress> for DynamicResourceAddress {
})
}
}

pub trait ResolvableResourceAddress: Sized {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicResourceAddress;

/// Note - this can be removed when all the static resource addresses in the
/// manifest instructions are gone
fn resolve_static(self, resolver: &impl NamedAddressResolver) -> ResourceAddress {
match self.resolve(resolver) {
DynamicResourceAddress::Static(address) => address,
DynamicResourceAddress::Named(_) => {
panic!("This address needs to be a static/fixed address")
}
}
}
}

impl<A, E> ResolvableResourceAddress for A
where
A: TryInto<DynamicResourceAddress, Error = E>,
E: Debug,
{
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicResourceAddress {
let address = self
.try_into()
.expect("Address was not a valid DynamicResourceAddress");
if let DynamicResourceAddress::Named(named_address) = address {
resolver.assert_named_address_exists(named_address);
}
address
}
}

impl<'a> ResolvableResourceAddress for &'a str {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicResourceAddress {
resolver.resolve_named_address(self).into()
}
}

impl<'a> ResolvableResourceAddress for &'a String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicResourceAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}

impl ResolvableResourceAddress for String {
fn resolve(self, resolver: &impl NamedAddressResolver) -> DynamicResourceAddress {
resolver.resolve_named_address(self.as_str()).into()
}
}
2 changes: 2 additions & 0 deletions radix-common/src/data/manifest/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod manifest_address_reservation;
mod manifest_blob;
mod manifest_bucket;
mod manifest_decimal;
mod manifest_dynamic_addresses;
mod manifest_expression;
mod manifest_non_fungible_local_id;
mod manifest_precise_decimal;
Expand All @@ -14,6 +15,7 @@ pub use manifest_address_reservation::*;
pub use manifest_blob::*;
pub use manifest_bucket::*;
pub use manifest_decimal::*;
pub use manifest_dynamic_addresses::*;
pub use manifest_expression::*;
pub use manifest_non_fungible_local_id::*;
pub use manifest_precise_decimal::*;
Expand Down
Loading
Loading