Skip to content

Commit

Permalink
fix: Add back the ability to resolve named addresses from strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dhedey committed Oct 31, 2024
1 parent 1494b75 commit 1aacdf0
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 88 deletions.
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
174 changes: 174 additions & 0 deletions radix-common/src/data/manifest/model/manifest_dynamic_addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,44 @@ impl TryFrom<ManifestAddress> for DynamicGlobalAddress {
}
}

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),
Expand Down Expand Up @@ -339,6 +377,55 @@ impl TryFrom<ManifestAddress> for DynamicPackageAddress {
}
}

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),
Expand Down Expand Up @@ -441,6 +528,44 @@ impl TryFrom<ManifestAddress> for DynamicComponentAddress {
}
}

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),
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()
}
}
10 changes: 2 additions & 8 deletions radix-transaction-scenarios/src/scenarios/basic_subintents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ impl ScenarioCreator for BasicSubintentsScenarioCreator {
)
.get_free_xrd_from_faucet()
.take_all_from_worktop(XRD, "free_xrd")
.then(|builder| {
let parent_account = builder.named_address("parent_account");
builder.deposit(parent_account, "free_xrd")
})
.deposit("parent_account", "free_xrd")
.create_fungible_resource(
OwnerRole::Fixed(rule!(require(
config.child_account_key.public_key().signature_proof()
Expand All @@ -124,10 +121,7 @@ impl ScenarioCreator for BasicSubintentsScenarioCreator {
config.child_account_key.public_key().signature_proof()
))),
)
.then(|builder| {
let child_account = builder.named_address("child_account");
builder.try_deposit_entire_worktop_or_abort(child_account, None)
})
.try_deposit_entire_worktop_or_abort("child_account", None)
})
.sign(&config.parent_account_key)
.complete(core)
Expand Down
92 changes: 12 additions & 80 deletions radix-transactions/src/builder/manifest_namer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,91 +974,23 @@ pub struct NamedManifestAddress {
name: String,
}

pub trait ResolvableComponentAddress {
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicComponentAddress;
}

impl<A, E> ResolvableComponentAddress for A
where
A: TryInto<DynamicComponentAddress, Error = E>,
E: Debug,
{
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicComponentAddress {
let address = self
.try_into()
.expect("Address was not valid ComponentAddress");
registrar.check_address_exists(address);
address
}
}

pub trait ResolvableResourceAddress: Sized {
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicResourceAddress;

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

impl<A: TryInto<DynamicResourceAddress, Error = E>, E: Debug> ResolvableResourceAddress for A {
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicResourceAddress {
let address = self
.try_into()
.expect("Address was not valid ResourceAddress");
registrar.check_address_exists(address);
address
}
}

pub trait ResolvablePackageAddress: Sized {
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicPackageAddress;

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

impl<A: TryInto<DynamicPackageAddress, Error = E>, E: Debug> ResolvablePackageAddress for A {
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicPackageAddress {
let address = self
.try_into()
.expect("Address was not valid PackageAddress");
registrar.check_address_exists(address);
address
fn resolve_named_address(&self, address_name: &str) -> ManifestNamedAddress {
self.name_lookup().named_address_id(address_name)
}
}

pub trait ResolvableGlobalAddress {
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicGlobalAddress;
}

impl<A, E> ResolvableGlobalAddress for A
where
A: TryInto<DynamicGlobalAddress, Error = E>,
E: Debug,
{
fn resolve(self, registrar: &ManifestNameRegistrar) -> DynamicGlobalAddress {
let address = self
.try_into()
.expect("Address was not valid GlobalAddress");
registrar.check_address_exists(address);
address
}
}
// This has been moved to live alongside the dynamic addresses in radix-common to avoid
// foreign trait impl errors.
// But we still re-export it here to avoid breaking any imports.
pub use radix_common::prelude::{
ResolvableComponentAddress, ResolvableGlobalAddress, ResolvablePackageAddress,
ResolvableResourceAddress,
};

//=====================
// DECIMAL
Expand Down

0 comments on commit 1aacdf0

Please sign in to comment.