Skip to content

Commit

Permalink
feat: Add TryFrom impls for SubstateId and Addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
humb1t committed Jan 15, 2025
1 parent 953b976 commit 9328770
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 23 deletions.
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
7 changes: 4 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,9 @@ 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.0 }
})?
},
None => state.id_provider()?.new_component_address(template_address, None)?,
};
Expand Down
123 changes: 105 additions & 18 deletions dan_layer/engine_types/src/substate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,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 +162,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 +341,105 @@ impl From<PublishedTemplateAddress> for SubstateId {
}
}

#[derive(Debug, thiserror::Error)]
#[error("Invalid substate id variant'{0}'")]
pub struct InvalidSubstateIdVariant(pub SubstateId);

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}

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(value)),
}
}
}
Expand Down

0 comments on commit 9328770

Please sign in to comment.