Skip to content

Commit

Permalink
Return back balances.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx committed Nov 30, 2022
1 parent 23a9d7c commit d2a9dd0
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 161 deletions.
1 change: 1 addition & 0 deletions fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mod receipts;
mod sealed_block;
mod state;

pub mod balances;
pub mod metadata;
pub mod resource;
pub mod transaction;
Expand Down
194 changes: 194 additions & 0 deletions fuel-core/src/database/balances.rs
Original file line number Diff line number Diff line change
@@ -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<ContractsAssets<'_>> for Database {
type Error = Error;

fn get(&self, key: &(&ContractId, &AssetId)) -> Result<Option<Cow<Word>>, Error> {
let key = MultiKey::new(key);
self.get(key.as_ref(), Column::ContractsAssets)
}

fn contains_key(&self, key: &(&ContractId, &AssetId)) -> Result<bool, Error> {
let key = MultiKey::new(key);
self.exists(key.as_ref(), Column::ContractsAssets)
}
}

impl StorageMutate<ContractsAssets<'_>> for Database {
fn insert(
&mut self,
key: &(&ContractId, &AssetId),
value: &Word,
) -> Result<Option<Word>, Error> {
let key = MultiKey::new(key);
Database::insert(self, key.as_ref(), Column::ContractsAssets, *value)
}

fn remove(&mut self, key: &(&ContractId, &AssetId)) -> Result<Option<Word>, Error> {
let key = MultiKey::new(key);
Database::remove(self, key.as_ref(), Column::ContractsAssets)
}
}

impl MerkleRootStorage<ContractId, ContractsAssets<'_>> for Database {
fn root(&mut self, parent: &ContractId) -> Result<MerkleRoot, Error> {
let items: Vec<_> = Database::iter_all::<Vec<u8>, 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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

assert_eq!(
database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

let returned = database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

database.storage::<ContractsAssets>().remove(key).unwrap();

assert!(!database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

assert!(database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

let root = database.storage::<ContractsAssets>().root(&balance_id.0);
assert!(root.is_ok())
}
}
161 changes: 0 additions & 161 deletions fuel-core/src/database/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use crate::{
use fuel_chain_config::ContractConfig;
use fuel_core_interfaces::{
common::{
crypto,
fuel_storage::{
MerkleRoot,
MerkleRootStorage,
StorageAsRef,
StorageInspect,
StorageMutate,
Expand All @@ -32,13 +29,11 @@ use fuel_core_interfaces::{
},
},
db::{
ContractsAssets,
ContractsInfo,
ContractsLatestUtxo,
ContractsRawCode,
},
};
use itertools::Itertools;
use std::borrow::Cow;

impl StorageInspect<ContractsRawCode> for Database {
Expand Down Expand Up @@ -93,59 +88,6 @@ impl StorageMutate<ContractsLatestUtxo> for Database {
}
}

impl StorageInspect<ContractsAssets<'_>> for Database {
type Error = Error;

fn get(&self, key: &(&ContractId, &AssetId)) -> Result<Option<Cow<Word>>, Error> {
let key = MultiKey::new(key);
self.get(key.as_ref(), Column::ContractsAssets)
}

fn contains_key(&self, key: &(&ContractId, &AssetId)) -> Result<bool, Error> {
let key = MultiKey::new(key);
self.exists(key.as_ref(), Column::ContractsAssets)
}
}

impl StorageMutate<ContractsAssets<'_>> for Database {
fn insert(
&mut self,
key: &(&ContractId, &AssetId),
value: &Word,
) -> Result<Option<Word>, Error> {
let key = MultiKey::new(key);
Database::insert(self, key.as_ref(), Column::ContractsAssets, *value)
}

fn remove(&mut self, key: &(&ContractId, &AssetId)) -> Result<Option<Word>, Error> {
let key = MultiKey::new(key);
Database::remove(self, key.as_ref(), Column::ContractsAssets)
}
}

impl MerkleRootStorage<ContractId, ContractsAssets<'_>> for Database {
fn root(&mut self, parent: &ContractId) -> Result<MerkleRoot, Error> {
let items: Vec<_> = Database::iter_all::<Vec<u8>, 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,
Expand Down Expand Up @@ -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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

assert_eq!(
database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

let returned = database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

database.storage::<ContractsAssets>().remove(key).unwrap();

assert!(!database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

assert!(database
.storage::<ContractsAssets>()
.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::<ContractsAssets>()
.insert(key, &balance)
.unwrap();

let root = database.storage::<ContractsAssets>().root(&balance_id.0);
assert!(root.is_ok())
}

#[test]
fn raw_code_get() {
let contract_id: ContractId = ContractId::from([1u8; 32]);
Expand Down

0 comments on commit d2a9dd0

Please sign in to comment.