From a82414183cc8f1cde78b0a22c86cf7e82f2f8727 Mon Sep 17 00:00:00 2001 From: Bobbin Threadbare Date: Thu, 4 Apr 2024 17:48:31 -0700 Subject: [PATCH] fix: no_std build --- objects/src/accounts/mod.rs | 145 +++++++++++++++++++----------------- objects/src/notes/mod.rs | 3 +- 2 files changed, 78 insertions(+), 70 deletions(-) diff --git a/objects/src/accounts/mod.rs b/objects/src/accounts/mod.rs index 4056d672b..5e17c5225 100644 --- a/objects/src/accounts/mod.rs +++ b/objects/src/accounts/mod.rs @@ -289,77 +289,86 @@ pub fn hash_account( // ================================================================================================ #[cfg(any(feature = "testing", test))] -const fn account_id(account_type: AccountType, storage: AccountStorageType, rest: u64) -> u64 { - let mut id = 0; +pub use testing::*; - id ^= (storage as u64) << ACCOUNT_STORAGE_MASK_SHIFT; - id ^= (account_type as u64) << ACCOUNT_TYPE_MASK_SHIFT; - id ^= rest; +#[cfg(any(feature = "testing", test))] +mod testing { + use super::{ + AccountStorageType, AccountType, ACCOUNT_STORAGE_MASK_SHIFT, ACCOUNT_TYPE_MASK_SHIFT, + }; - id -} + const fn account_id(account_type: AccountType, storage: AccountStorageType, rest: u64) -> u64 { + let mut id = 0; -// REGULAR ACCOUNTS - OFF-CHAIN -pub const ACCOUNT_ID_SENDER: u64 = account_id( - AccountType::RegularAccountImmutableCode, - AccountStorageType::OffChain, - 0b0001_1111, -); -pub const ACCOUNT_ID_OFF_CHAIN_SENDER: u64 = account_id( - AccountType::RegularAccountImmutableCode, - AccountStorageType::OffChain, - 0b0010_1111, -); -pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN: u64 = account_id( - AccountType::RegularAccountUpdatableCode, - AccountStorageType::OffChain, - 0b0011_1111, -); -// REGULAR ACCOUNTS - ON-CHAIN -pub const ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN: u64 = account_id( - AccountType::RegularAccountImmutableCode, - AccountStorageType::OnChain, - 0b0001_1111, -); -pub const ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN_2: u64 = account_id( - AccountType::RegularAccountImmutableCode, - AccountStorageType::OnChain, - 0b0010_1111, -); -pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN: u64 = account_id( - AccountType::RegularAccountUpdatableCode, - AccountStorageType::OnChain, - 0b0011_1111, -); -pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN_2: u64 = account_id( - AccountType::RegularAccountUpdatableCode, - AccountStorageType::OnChain, - 0b0100_1111, -); - -// FUNGIBLE TOKENS - OFF-CHAIN -pub const ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN: u64 = - account_id(AccountType::FungibleFaucet, AccountStorageType::OffChain, 0b0001_1111); -// FUNGIBLE TOKENS - ON-CHAIN -pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u64 = - account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0001_1111); -pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 = - account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0010_1111); -pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2: u64 = - account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0011_1111); -pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3: u64 = - account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0100_1111); - -// NON-FUNGIBLE TOKENS - OFF-CHAIN -pub const ACCOUNT_ID_INSUFFICIENT_ONES: u64 = - account_id(AccountType::NonFungibleFaucet, AccountStorageType::OffChain, 0b0000_0000); // invalid -pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN: u64 = - account_id(AccountType::NonFungibleFaucet, AccountStorageType::OffChain, 0b0001_1111); -// NON-FUNGIBLE TOKENS - ON-CHAIN -pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN: u64 = - account_id(AccountType::NonFungibleFaucet, AccountStorageType::OnChain, 0b0010_1111); -pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 = - account_id(AccountType::NonFungibleFaucet, AccountStorageType::OnChain, 0b0011_1111); + id ^= (storage as u64) << ACCOUNT_STORAGE_MASK_SHIFT; + id ^= (account_type as u64) << ACCOUNT_TYPE_MASK_SHIFT; + id ^= rest; + + id + } + + // REGULAR ACCOUNTS - OFF-CHAIN + pub const ACCOUNT_ID_SENDER: u64 = account_id( + AccountType::RegularAccountImmutableCode, + AccountStorageType::OffChain, + 0b0001_1111, + ); + pub const ACCOUNT_ID_OFF_CHAIN_SENDER: u64 = account_id( + AccountType::RegularAccountImmutableCode, + AccountStorageType::OffChain, + 0b0010_1111, + ); + pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN: u64 = account_id( + AccountType::RegularAccountUpdatableCode, + AccountStorageType::OffChain, + 0b0011_1111, + ); + // REGULAR ACCOUNTS - ON-CHAIN + pub const ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN: u64 = account_id( + AccountType::RegularAccountImmutableCode, + AccountStorageType::OnChain, + 0b0001_1111, + ); + pub const ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN_2: u64 = account_id( + AccountType::RegularAccountImmutableCode, + AccountStorageType::OnChain, + 0b0010_1111, + ); + pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN: u64 = account_id( + AccountType::RegularAccountUpdatableCode, + AccountStorageType::OnChain, + 0b0011_1111, + ); + pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_ON_CHAIN_2: u64 = account_id( + AccountType::RegularAccountUpdatableCode, + AccountStorageType::OnChain, + 0b0100_1111, + ); + + // FUNGIBLE TOKENS - OFF-CHAIN + pub const ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN: u64 = + account_id(AccountType::FungibleFaucet, AccountStorageType::OffChain, 0b0001_1111); + // FUNGIBLE TOKENS - ON-CHAIN + pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u64 = + account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0001_1111); + pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 = + account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0010_1111); + pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2: u64 = + account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0011_1111); + pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3: u64 = + account_id(AccountType::FungibleFaucet, AccountStorageType::OnChain, 0b0100_1111); + + // NON-FUNGIBLE TOKENS - OFF-CHAIN + pub const ACCOUNT_ID_INSUFFICIENT_ONES: u64 = + account_id(AccountType::NonFungibleFaucet, AccountStorageType::OffChain, 0b0000_0000); // invalid + pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN: u64 = + account_id(AccountType::NonFungibleFaucet, AccountStorageType::OffChain, 0b0001_1111); + // NON-FUNGIBLE TOKENS - ON-CHAIN + pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN: u64 = + account_id(AccountType::NonFungibleFaucet, AccountStorageType::OnChain, 0b0010_1111); + pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1: u64 = + account_id(AccountType::NonFungibleFaucet, AccountStorageType::OnChain, 0b0011_1111); +} // TESTS // ================================================================================================ diff --git a/objects/src/notes/mod.rs b/objects/src/notes/mod.rs index 43145796c..f626e1195 100644 --- a/objects/src/notes/mod.rs +++ b/objects/src/notes/mod.rs @@ -1,5 +1,3 @@ -use alloc::vec::Vec; - use crate::{ accounts::AccountId, assembly::{Assembler, AssemblyContext, ProgramAst}, @@ -234,6 +232,7 @@ impl serde::Serialize for Note { #[cfg(feature = "serde")] impl<'de> serde::Deserialize<'de> for Note { fn deserialize>(deserializer: D) -> Result { + use alloc::vec::Vec; let bytes: Vec = as serde::Deserialize>::deserialize(deserializer)?; Self::read_from_bytes(&bytes).map_err(serde::de::Error::custom) }