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

feat: add TryFrom impls for SubstateId and Addresses #1243

Merged
merged 1 commit into from
Jan 16, 2025
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
7 changes: 5 additions & 2 deletions dan_layer/engine/src/runtime/address_allocation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use tari_engine_types::{substate::SubstateId, TemplateAddress};
use tari_engine_types::{
substate::{InvalidSubstateIdVariant, SubstateId},
TemplateAddress,
};
use tari_template_lib::models::ComponentAddress;

#[derive(Debug, Clone)]
Expand All @@ -28,7 +31,7 @@ impl AllocatedAddress {
}

impl TryFrom<AllocatedAddress> for ComponentAddress {
type Error = SubstateId;
type Error = InvalidSubstateIdVariant;

fn try_from(value: AllocatedAddress) -> Result<Self, Self::Error> {
value.address.try_into()
Expand Down
9 changes: 6 additions & 3 deletions dan_layer/engine/src/runtime/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use tari_engine_types::{
indexed_value::{IndexedValue, IndexedWellKnownTypes},
lock::LockFlag,
logs::LogEntry,
substate::{SubstateId, SubstateValue},
substate::{InvalidSubstateIdVariant, SubstateId, SubstateValue},
virtual_substate::VirtualSubstates,
TemplateAddress,
};
Expand Down Expand Up @@ -163,8 +163,11 @@ impl StateTracker {
let component_address = match address_allocation {
Some(address_allocation) => {
let addr = state.take_allocated_address(address_allocation.id())?;
addr.try_into()
.map_err(|address| RuntimeError::AddressAllocationTypeMismatch { address })?
addr.try_into().map_err(|error: InvalidSubstateIdVariant| {
RuntimeError::AddressAllocationTypeMismatch {
address: error.substate_id,
}
})?
},
None => state.id_provider()?.new_component_address(template_address, None)?,
};
Expand Down
154 changes: 136 additions & 18 deletions dan_layer/engine_types/src/substate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
any,
fmt::{Display, Formatter},
str::FromStr,
};
Expand All @@ -31,13 +32,8 @@ use tari_bor::{decode, decode_exact, encode, BorError};
use tari_common_types::types::FixedHash;
use tari_template_lib::{
models::{
ComponentAddress,
NonFungibleAddress,
NonFungibleIndexAddress,
ObjectKey,
ResourceAddress,
UnclaimedConfidentialOutputAddress,
VaultId,
ComponentAddress, NonFungibleAddress, NonFungibleIndexAddress, ObjectKey, ResourceAddress,
UnclaimedConfidentialOutputAddress, VaultId,
},
prelude::PUBLIC_IDENTITY_RESOURCE_ADDRESS,
Hash,
Expand Down Expand Up @@ -167,15 +163,15 @@ impl SubstateId {
/// Returns true for any substate that has is "versionable" i.e. can have a version > 0, otherwise false.
pub fn is_versioned(&self) -> bool {
match self {
SubstateId::Component(_) |
SubstateId::Resource(_) |
SubstateId::Vault(_) |
SubstateId::NonFungibleIndex(_) |
SubstateId::Template(_) |
SubstateId::NonFungible(_) => true,
SubstateId::UnclaimedConfidentialOutput(_) |
SubstateId::TransactionReceipt(_) |
SubstateId::FeeClaim(_) => false,
SubstateId::Component(_)
| SubstateId::Resource(_)
| SubstateId::Vault(_)
| SubstateId::NonFungibleIndex(_)
| SubstateId::Template(_)
| SubstateId::NonFungible(_) => true,
SubstateId::UnclaimedConfidentialOutput(_)
| SubstateId::TransactionReceipt(_)
| SubstateId::FeeClaim(_) => false,
}
}

Expand Down Expand Up @@ -346,13 +342,135 @@ impl From<PublishedTemplateAddress> for SubstateId {
}
}

#[derive(Debug, thiserror::Error)]
#[error("Could not convert substate ID variant '{substate_id}' to {expected}")]
pub struct InvalidSubstateIdVariant {
pub substate_id: SubstateId,
pub expected: &'static str,
}

impl TryFrom<SubstateId> for ComponentAddress {
type Error = SubstateId;
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::Component(addr) => Ok(addr),
_ => Err(value),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for ResourceAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::Resource(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for VaultId {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::Vault(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for NonFungibleAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::NonFungible(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for NonFungibleIndexAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::NonFungibleIndex(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for UnclaimedConfidentialOutputAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::UnclaimedConfidentialOutput(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for FeeClaimAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::FeeClaim(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for TransactionReceiptAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::TransactionReceipt(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}

impl TryFrom<SubstateId> for PublishedTemplateAddress {
type Error = InvalidSubstateIdVariant;

fn try_from(value: SubstateId) -> Result<Self, Self::Error> {
match value {
SubstateId::Template(addr) => Ok(addr),
_ => Err(InvalidSubstateIdVariant {
substate_id: value,
expected: any::type_name::<Self>(),
}),
}
}
}
Expand Down
Loading