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

Make SubAssetId its own type instead of using Bytes32 #778

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 4 additions & 3 deletions fuel-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use fuel_asm::{
PanicInstruction,
PanicReason,
};
use fuel_types::SubAssetId;
pub use fuel_types::{
Address,
AssetId,
Expand Down Expand Up @@ -131,14 +132,14 @@ pub use contract::Contract;
/// Trait extends the functionality of the `ContractId` type.
pub trait ContractIdExt {
/// Creates an `AssetId` from the `ContractId` and `sub_id`.
fn asset_id(&self, sub_id: &Bytes32) -> AssetId;
fn asset_id(&self, sub_id: &SubAssetId) -> AssetId;

/// Creates an `AssetId` from the `ContractId` and the default 0x00..000 `sub_id`.
fn default_asset(&self) -> AssetId;
}

impl ContractIdExt for ContractId {
fn asset_id(&self, sub_id: &Bytes32) -> AssetId {
fn asset_id(&self, sub_id: &SubAssetId) -> AssetId {
let hasher = fuel_crypto::Hasher::default();
AssetId::new(
*hasher
Expand All @@ -149,6 +150,6 @@ impl ContractIdExt for ContractId {
}

fn default_asset(&self) -> AssetId {
self.asset_id(&Bytes32::zeroed())
self.asset_id(&SubAssetId::zeroed())
}
}
11 changes: 6 additions & 5 deletions fuel-tx/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use fuel_types::{
ContractId,
MessageId,
Nonce,
SubAssetId,
Word,
};

Expand Down Expand Up @@ -139,14 +140,14 @@ pub enum Receipt {
data: Option<Vec<u8>>,
},
Mint {
sub_id: Bytes32,
sub_id: SubAssetId,
contract_id: ContractId,
val: Word,
pc: Word,
is: Word,
},
Burn {
sub_id: Bytes32,
sub_id: SubAssetId,
contract_id: ContractId,
val: Word,
pc: Word,
Expand Down Expand Up @@ -403,7 +404,7 @@ impl Receipt {
}

pub fn mint(
sub_id: Bytes32,
sub_id: SubAssetId,
contract_id: ContractId,
val: Word,
pc: Word,
Expand All @@ -419,7 +420,7 @@ impl Receipt {
}

pub fn burn(
sub_id: Bytes32,
sub_id: SubAssetId,
contract_id: ContractId,
val: Word,
pc: Word,
Expand Down Expand Up @@ -453,7 +454,7 @@ impl Receipt {
})
}

pub const fn sub_id(&self) -> Option<&Bytes32> {
pub const fn sub_id(&self) -> Option<&SubAssetId> {
match self {
Self::Mint { sub_id, .. } => Some(sub_id),
Self::Burn { sub_id, .. } => Some(sub_id),
Expand Down
1 change: 1 addition & 0 deletions fuel-types/src/array_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ macro_rules! key_methods {

key!(Address, 32);
key!(AssetId, 32);
key!(SubAssetId, 32);
key!(ContractId, 32);
key!(Bytes4, 4);
key!(Bytes8, 8);
Expand Down
11 changes: 7 additions & 4 deletions fuel-vm/src/interpreter/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ use fuel_tx::{
Receipt,
};
use fuel_types::{
bytes,
bytes::padded_len_word,
bytes::{
self,
padded_len_word,
},
Address,
AssetId,
BlockHeight,
Bytes32,
ContractId,
RegisterId,
SubAssetId,
Word,
};

Expand Down Expand Up @@ -686,7 +689,7 @@ where
{
pub(crate) fn burn(self, a: Word, b: Word) -> IoResult<(), S::Error> {
let contract_id = internal_contract(self.context, self.fp, self.memory)?;
let sub_id = Bytes32::new(self.memory.read_bytes(b)?);
let sub_id = SubAssetId::new(self.memory.read_bytes(b)?);
let asset_id = contract_id.asset_id(&sub_id);

let balance = balance(self.storage, &contract_id, &asset_id)?;
Expand Down Expand Up @@ -727,7 +730,7 @@ where
{
pub(crate) fn mint(self, a: Word, b: Word) -> Result<(), RuntimeError<S::Error>> {
let contract_id = internal_contract(self.context, self.fp, self.memory)?;
let sub_id = Bytes32::new(self.memory.read_bytes(b)?);
let sub_id = SubAssetId::new(self.memory.read_bytes(b)?);
let asset_id = contract_id.asset_id(&sub_id);

let balance = balance(self.storage, &contract_id, &asset_id)?;
Expand Down
4 changes: 2 additions & 2 deletions fuel-vm/src/interpreter/blockchain/other_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn test_burn(
memory[0..ContractId::LEN].copy_from_slice(contract_id.as_slice());
memory[ContractId::LEN..ContractId::LEN + Bytes32::LEN]
.copy_from_slice(sub_id.as_slice());
let sub_id = Bytes32::from(sub_id);
let sub_id = SubAssetId::from(sub_id);
let asset_id = contract_id.asset_id(&sub_id);
let initialize = initialize.into();
if let Some(initialize) = initialize {
Expand Down Expand Up @@ -110,7 +110,7 @@ fn test_mint(
memory[0..ContractId::LEN].copy_from_slice(contract_id.as_slice());
memory[ContractId::LEN..ContractId::LEN + Bytes32::LEN]
.copy_from_slice(sub_id.as_slice());
let sub_id = Bytes32::from(sub_id);
let sub_id = SubAssetId::from(sub_id);
let asset_id = contract_id.asset_id(&sub_id);
let initialize = initialize.into();
if let Some(initialize) = initialize {
Expand Down
7 changes: 5 additions & 2 deletions fuel-vm/src/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use fuel_tx::{
ConsensusParameters,
Witness,
};
use fuel_types::canonical::Serialize;
use fuel_types::{
canonical::Serialize,
SubAssetId,
};
use rand::{
rngs::StdRng,
Rng,
Expand Down Expand Up @@ -115,7 +118,7 @@ fn mint_burn() {

let contract_id = test_context.setup_contract(program, None, None).contract_id;

let asset_id = contract_id.asset_id(&Bytes32::zeroed());
let asset_id = contract_id.asset_id(&SubAssetId::zeroed());

let (script_call, _) = script_with_data_offset!(
data_offset,
Expand Down
Loading