Skip to content

Commit

Permalink
fix: no_std build
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Apr 5, 2024
1 parent 060d3ea commit a824141
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 70 deletions.
145 changes: 77 additions & 68 deletions objects/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ================================================================================================
Expand Down
3 changes: 1 addition & 2 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use alloc::vec::Vec;

use crate::{
accounts::AccountId,
assembly::{Assembler, AssemblyContext, ProgramAst},
Expand Down Expand Up @@ -234,6 +232,7 @@ impl serde::Serialize for Note {
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Note {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use alloc::vec::Vec;
let bytes: Vec<u8> = <Vec<u8> as serde::Deserialize>::deserialize(deserializer)?;
Self::read_from_bytes(&bytes).map_err(serde::de::Error::custom)
}
Expand Down

0 comments on commit a824141

Please sign in to comment.