From d2a9dd04aca9861861fb93a4922044de085a3a76 Mon Sep 17 00:00:00 2001 From: green Date: Fri, 25 Nov 2022 01:46:36 +0000 Subject: [PATCH] Return back `balances.rs` --- fuel-core/src/database.rs | 1 + fuel-core/src/database/balances.rs | 194 ++++++++++++++++++++++++++++ fuel-core/src/database/contracts.rs | 161 ----------------------- 3 files changed, 195 insertions(+), 161 deletions(-) create mode 100644 fuel-core/src/database/balances.rs diff --git a/fuel-core/src/database.rs b/fuel-core/src/database.rs index 3e70a91ab4e..c98489799cb 100644 --- a/fuel-core/src/database.rs +++ b/fuel-core/src/database.rs @@ -70,6 +70,7 @@ mod receipts; mod sealed_block; mod state; +pub mod balances; pub mod metadata; pub mod resource; pub mod transaction; diff --git a/fuel-core/src/database/balances.rs b/fuel-core/src/database/balances.rs new file mode 100644 index 00000000000..8c42c486211 --- /dev/null +++ b/fuel-core/src/database/balances.rs @@ -0,0 +1,194 @@ +use crate::{ + database::{ + Column, + Database, + }, + state::{ + Error, + IterDirection, + MultiKey, + }, +}; +use fuel_core_interfaces::{ + common::{ + fuel_storage::{ + MerkleRoot, + StorageInspect, + StorageMutate, + }, + fuel_vm::{ + crypto, + prelude::{ + AssetId, + ContractId, + MerkleRootStorage, + Word, + }, + }, + }, + db::ContractsAssets, +}; +use itertools::Itertools; +use std::borrow::Cow; + +impl StorageInspect> for Database { + type Error = Error; + + fn get(&self, key: &(&ContractId, &AssetId)) -> Result>, Error> { + let key = MultiKey::new(key); + self.get(key.as_ref(), Column::ContractsAssets) + } + + fn contains_key(&self, key: &(&ContractId, &AssetId)) -> Result { + let key = MultiKey::new(key); + self.exists(key.as_ref(), Column::ContractsAssets) + } +} + +impl StorageMutate> for Database { + fn insert( + &mut self, + key: &(&ContractId, &AssetId), + value: &Word, + ) -> Result, Error> { + let key = MultiKey::new(key); + Database::insert(self, key.as_ref(), Column::ContractsAssets, *value) + } + + fn remove(&mut self, key: &(&ContractId, &AssetId)) -> Result, Error> { + let key = MultiKey::new(key); + Database::remove(self, key.as_ref(), Column::ContractsAssets) + } +} + +impl MerkleRootStorage> for Database { + fn root(&mut self, parent: &ContractId) -> Result { + let items: Vec<_> = Database::iter_all::, Word>( + self, + Column::ContractsAssets, + Some(parent.as_ref().to_vec()), + None, + Some(IterDirection::Forward), + ) + .try_collect()?; + + let root = items + .iter() + .filter_map(|(key, value)| { + (&key[..parent.len()] == parent.as_ref()).then_some((key, value)) + }) + .sorted_by_key(|t| t.0) + .map(|(_, value)| value.to_be_bytes()); + + Ok(crypto::ephemeral_merkle_root(root).into()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use fuel_core_interfaces::common::fuel_storage::StorageAsMut; + + #[test] + fn get() { + let balance_id: (ContractId, AssetId) = + (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); + let balance: Word = 100; + let key = &(&balance_id.0, &balance_id.1); + + let database = &mut Database::default(); + database + .storage::() + .insert(key, &balance) + .unwrap(); + + assert_eq!( + database + .storage::() + .get(key) + .unwrap() + .unwrap() + .into_owned(), + balance + ); + } + + #[test] + fn put() { + let balance_id: (ContractId, AssetId) = + (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); + let balance: Word = 100; + let key = &(&balance_id.0, &balance_id.1); + + let database = &mut Database::default(); + database + .storage::() + .insert(key, &balance) + .unwrap(); + + let returned = database + .storage::() + .get(key) + .unwrap() + .unwrap(); + assert_eq!(*returned, balance); + } + + #[test] + fn remove() { + let balance_id: (ContractId, AssetId) = + (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); + let balance: Word = 100; + let key = &(&balance_id.0, &balance_id.1); + + let database = &mut Database::default(); + database + .storage::() + .insert(key, &balance) + .unwrap(); + + database.storage::().remove(key).unwrap(); + + assert!(!database + .storage::() + .contains_key(key) + .unwrap()); + } + + #[test] + fn exists() { + let balance_id: (ContractId, AssetId) = + (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); + let balance: Word = 100; + let key = &(&balance_id.0, &balance_id.1); + + let database = &mut Database::default(); + database + .storage::() + .insert(key, &balance) + .unwrap(); + + assert!(database + .storage::() + .contains_key(key) + .unwrap()); + } + + #[test] + fn root() { + let balance_id: (ContractId, AssetId) = + (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); + let balance: Word = 100; + let key = &(&balance_id.0, &balance_id.1); + + let database = &mut Database::default(); + + database + .storage::() + .insert(key, &balance) + .unwrap(); + + let root = database.storage::().root(&balance_id.0); + assert!(root.is_ok()) + } +} diff --git a/fuel-core/src/database/contracts.rs b/fuel-core/src/database/contracts.rs index f7b0ae05a4b..85700c78ed6 100644 --- a/fuel-core/src/database/contracts.rs +++ b/fuel-core/src/database/contracts.rs @@ -12,10 +12,7 @@ use crate::{ use fuel_chain_config::ContractConfig; use fuel_core_interfaces::{ common::{ - crypto, fuel_storage::{ - MerkleRoot, - MerkleRootStorage, StorageAsRef, StorageInspect, StorageMutate, @@ -32,13 +29,11 @@ use fuel_core_interfaces::{ }, }, db::{ - ContractsAssets, ContractsInfo, ContractsLatestUtxo, ContractsRawCode, }, }; -use itertools::Itertools; use std::borrow::Cow; impl StorageInspect for Database { @@ -93,59 +88,6 @@ impl StorageMutate for Database { } } -impl StorageInspect> for Database { - type Error = Error; - - fn get(&self, key: &(&ContractId, &AssetId)) -> Result>, Error> { - let key = MultiKey::new(key); - self.get(key.as_ref(), Column::ContractsAssets) - } - - fn contains_key(&self, key: &(&ContractId, &AssetId)) -> Result { - let key = MultiKey::new(key); - self.exists(key.as_ref(), Column::ContractsAssets) - } -} - -impl StorageMutate> for Database { - fn insert( - &mut self, - key: &(&ContractId, &AssetId), - value: &Word, - ) -> Result, Error> { - let key = MultiKey::new(key); - Database::insert(self, key.as_ref(), Column::ContractsAssets, *value) - } - - fn remove(&mut self, key: &(&ContractId, &AssetId)) -> Result, Error> { - let key = MultiKey::new(key); - Database::remove(self, key.as_ref(), Column::ContractsAssets) - } -} - -impl MerkleRootStorage> for Database { - fn root(&mut self, parent: &ContractId) -> Result { - let items: Vec<_> = Database::iter_all::, Word>( - self, - Column::ContractsAssets, - Some(parent.as_ref().to_vec()), - None, - Some(IterDirection::Forward), - ) - .try_collect()?; - - let root = items - .iter() - .filter_map(|(key, value)| { - (&key[..parent.len()] == parent.as_ref()).then_some((key, value)) - }) - .sorted_by_key(|t| t.0) - .map(|(_, value)| value.to_be_bytes()); - - Ok(crypto::ephemeral_merkle_root(root).into()) - } -} - impl Database { pub fn contract_balances( &self, @@ -249,109 +191,6 @@ mod tests { fuel_tx::TxId, }; - #[test] - fn assets_get() { - let balance_id: (ContractId, AssetId) = - (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); - let balance: Word = 100; - let key = &(&balance_id.0, &balance_id.1); - - let database = &mut Database::default(); - database - .storage::() - .insert(key, &balance) - .unwrap(); - - assert_eq!( - database - .storage::() - .get(key) - .unwrap() - .unwrap() - .into_owned(), - balance - ); - } - - #[test] - fn assets_put() { - let balance_id: (ContractId, AssetId) = - (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); - let balance: Word = 100; - let key = &(&balance_id.0, &balance_id.1); - - let database = &mut Database::default(); - database - .storage::() - .insert(key, &balance) - .unwrap(); - - let returned = database - .storage::() - .get(key) - .unwrap() - .unwrap(); - assert_eq!(*returned, balance); - } - - #[test] - fn assets_remove() { - let balance_id: (ContractId, AssetId) = - (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); - let balance: Word = 100; - let key = &(&balance_id.0, &balance_id.1); - - let database = &mut Database::default(); - database - .storage::() - .insert(key, &balance) - .unwrap(); - - database.storage::().remove(key).unwrap(); - - assert!(!database - .storage::() - .contains_key(key) - .unwrap()); - } - - #[test] - fn assets_exists() { - let balance_id: (ContractId, AssetId) = - (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); - let balance: Word = 100; - let key = &(&balance_id.0, &balance_id.1); - - let database = &mut Database::default(); - database - .storage::() - .insert(key, &balance) - .unwrap(); - - assert!(database - .storage::() - .contains_key(key) - .unwrap()); - } - - #[test] - fn assets_root() { - let balance_id: (ContractId, AssetId) = - (ContractId::from([1u8; 32]), AssetId::new([1u8; 32])); - let balance: Word = 100; - let key = &(&balance_id.0, &balance_id.1); - - let database = &mut Database::default(); - - database - .storage::() - .insert(key, &balance) - .unwrap(); - - let root = database.storage::().root(&balance_id.0); - assert!(root.is_ok()) - } - #[test] fn raw_code_get() { let contract_id: ContractId = ContractId::from([1u8; 32]);