Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal-ch committed Oct 25, 2024
1 parent 8ce4550 commit 19fd311
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 42 deletions.
1 change: 1 addition & 0 deletions crates/fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub mod coin;
pub mod contracts;
pub mod database_description;
pub mod genesis_progress;
pub mod indexation;
pub mod message;
pub mod metadata;
pub mod sealed_block;
Expand Down
32 changes: 30 additions & 2 deletions crates/fuel-core/src/database/database_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use fuel_core_types::{
blockchain::primitives::DaBlockHeight,
fuel_types::BlockHeight,
};
use off_chain::OffChain;
use std::collections::HashMap;

use super::indexation;

pub mod gas_price;
pub mod off_chain;
Expand Down Expand Up @@ -68,23 +72,47 @@ pub trait DatabaseDescription: 'static + Copy + Debug + Send + Sync {
}

/// The metadata of the database contains information about the version and its height.
#[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum DatabaseMetadata<Height> {
V1 { version: u32, height: Height },
V1 {
version: u32,
height: Height,
},
V2 {
version: u32,
height: Height,
indexation_progress: HashMap<indexation::Kind, indexation::Status>,
},
}

impl<Height> DatabaseMetadata<Height> {
/// Returns the version of the database.
pub fn version(&self) -> u32 {
match self {
Self::V1 { version, .. } => *version,
Self::V2 { version, .. } => *version,
}
}

/// Returns the height of the database.
pub fn height(&self) -> &Height {
match self {
Self::V1 { height, .. } => height,
Self::V2 { height, .. } => height,
}
}

/// Returns the indexation progress of a database
pub fn indexation_progress(
&self,
indexation_type: indexation::Kind,
) -> Option<&indexation::Status> {
match self {
Self::V1 { .. } => None,
Self::V2 {
indexation_progress,
..
} => indexation_progress.get(&indexation_type),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl DatabaseDescription for OffChain {
type Column = fuel_core_graphql_api::storage::Column;
type Height = BlockHeight;

// TODO[RC]: Do we bump this due to extended metadata?
fn version() -> u32 {
0
}
Expand Down
26 changes: 26 additions & 0 deletions crates/fuel-core/src/database/indexation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use fuel_core_types::fuel_types::BlockHeight;

#[derive(
Copy, Clone, Debug, serde::Serialize, serde::Deserialize, Hash, Eq, PartialEq,
)]
pub enum Kind {
Balances,
CoinsToSpend,
}

#[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum Status {
Pending,
CompletedUntil(BlockHeight),
Finished,
}

impl Status {
pub fn new() -> Self {
Status::Pending
}

pub fn is_finished(&self) -> bool {
matches!(self, Status::Finished)
}
}
9 changes: 9 additions & 0 deletions crates/fuel-core/src/database/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::database::{
database_description::{
DatabaseDescription,
Expand All @@ -15,6 +17,7 @@ use fuel_core_storage::{
Result as StorageResult,
StorageAsRef,
StorageInspect,
StorageMutate,
};

/// The table that stores all metadata about the database.
Expand Down Expand Up @@ -74,4 +77,10 @@ where

Ok(metadata)
}

pub fn metadata(
&self,
) -> StorageResult<Option<Cow<DatabaseMetadata<Description::Height>>>> {
self.storage::<MetadataTable<Description>>().get(&())
}
}
2 changes: 2 additions & 0 deletions crates/fuel-core/src/graphql_api/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub struct ReadDatabase {
on_chain: Box<dyn AtomicView<LatestView = OnChainView>>,
/// The off-chain database view provider.
off_chain: Box<dyn AtomicView<LatestView = OffChainView>>,
// TODO
// balances_enabled: bool
}

impl ReadDatabase {
Expand Down
5 changes: 5 additions & 0 deletions crates/fuel-core/src/graphql_api/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ pub trait MemoryPool {
pub mod worker {
use super::super::storage::blocks::FuelBlockIdsToHeights;
use crate::{
database::{
database_description::off_chain::OffChain,
metadata::MetadataTable,
},
fuel_core_graphql_api::storage::{
coins::OwnedCoins,
contracts::ContractsInfo,
Expand All @@ -306,6 +310,7 @@ pub mod worker {
use fuel_core_storage::{
Error as StorageError,
Result as StorageResult,
StorageInspect,
StorageMutate,
};
use fuel_core_types::{
Expand Down
103 changes: 66 additions & 37 deletions crates/fuel-core/src/graphql_api/worker_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ use super::{
},
};
use crate::{
database::{
database_description::off_chain::OffChain,
indexation,
metadata::MetadataTable,
},
fuel_core_graphql_api::{
ports::{
self,
Expand Down Expand Up @@ -106,7 +111,10 @@ use std::{
borrow::Cow,
ops::Deref,
};
use tracing::debug;
use tracing::{
debug,
error,
};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -138,6 +146,7 @@ pub struct Task<TxPool, D> {
chain_id: ChainId,
da_compression_config: DaCompressionConfig,
continue_on_error: bool,
balances_enabled: bool,
}

impl<TxPool, D> Task<TxPool, D>
Expand Down Expand Up @@ -170,6 +179,7 @@ where
process_executor_events(
result.events.iter().map(Cow::Borrowed),
&mut transaction,
// balances_enabled
)?;

match self.da_compression_config {
Expand Down Expand Up @@ -207,7 +217,16 @@ where
{
let key = BalancesKey::new(owner, asset_id);
let current_balance = tx.storage::<CoinBalances>().get(&key)?.unwrap_or_default();
let prev_balance = current_balance.clone();
let new_balance = updater(current_balance, amount);
debug!(
%owner,
%asset_id,
amount,
%prev_balance,
new_balance,
"changing coin balance"
);
tx.storage_as_mut::<CoinBalances>()
.insert(&key, &new_balance)
}
Expand All @@ -227,7 +246,14 @@ where
.storage::<MessageBalances>()
.get(key)?
.unwrap_or_default();
let prev_balance = current_balance.clone();
let new_balance = updater(current_balance, amount);
debug!(
%owner,
%amount,
%prev_balance,
new_balance,
"changing message balance");
tx.storage_as_mut::<MessageBalances>()
.insert(key, &new_balance)
}
Expand All @@ -236,6 +262,7 @@ where
pub fn process_executor_events<'a, Iter, T>(
events: Iter,
block_st_transaction: &mut T,
balances_enabled: bool,
) -> anyhow::Result<()>
where
Iter: Iterator<Item = Cow<'a, Event>>,
Expand All @@ -251,13 +278,15 @@ where
&(),
)?;

debug!(recipient=%message.recipient(), amount=%message.amount(), "increasing message balance");
update_message_balance(
message.recipient(),
message.amount(),
block_st_transaction,
|balance, amount| balance.saturating_add(amount),
)?;
// TODO[RC]: Refactor to have this if called only once
if balances_enabled {
update_message_balance(
message.recipient(),
message.amount(),
block_st_transaction,
|balance, amount| balance.saturating_add(amount),
)?;
}
}
Event::MessageConsumed(message) => {
block_st_transaction
Expand All @@ -270,49 +299,46 @@ where
.storage::<SpentMessages>()
.insert(message.nonce(), &())?;

debug!(recipient=%message.recipient(), amount=%message.amount(), "decreasing message balance");
update_message_balance(
message.recipient(),
message.amount(),
block_st_transaction,
|balance, amount| balance.saturating_sub(amount),
)?;
if balances_enabled {
update_message_balance(
message.recipient(),
message.amount(),
block_st_transaction,
|balance, amount| balance.saturating_sub(amount),
)?;
}
}
Event::CoinCreated(coin) => {
let coin_by_owner = owner_coin_id_key(&coin.owner, &coin.utxo_id);
block_st_transaction
.storage_as_mut::<OwnedCoins>()
.insert(&coin_by_owner, &())?;

debug!(
owner=%coin.owner,
asset_id=%coin.asset_id,
amount=%coin.amount, "increasing coin balance");
update_coin_balance(
&coin.owner,
&coin.asset_id,
coin.amount,
block_st_transaction,
|balance, amount| balance.saturating_add(amount),
)?;
if balances_enabled {
update_coin_balance(
&coin.owner,
&coin.asset_id,
coin.amount,
block_st_transaction,
|balance, amount| balance.saturating_add(amount),
)?;
}
}
Event::CoinConsumed(coin) => {
let key = owner_coin_id_key(&coin.owner, &coin.utxo_id);
block_st_transaction
.storage_as_mut::<OwnedCoins>()
.remove(&key)?;

debug!(
owner=%coin.owner,
asset_id=%coin.asset_id,
amount=%coin.amount, "decreasing coin balance");
update_coin_balance(
&coin.owner,
&coin.asset_id,
coin.amount,
block_st_transaction,
|balance, amount| balance.saturating_sub(amount),
)?;
if balances_enabled {
update_coin_balance(
&coin.owner,
&coin.asset_id,
coin.amount,
block_st_transaction,
|balance, amount| balance.saturating_sub(amount),
)?;
}
}
Event::ForcedTransactionFailed {
id,
Expand Down Expand Up @@ -564,6 +590,9 @@ where
graphql_metrics().total_txs_count.set(total_tx_count as i64);
}

// TODO
self.off_chain_database.latest_height();

let InitializeTask {
chain_id,
da_compression_config,
Expand Down
2 changes: 1 addition & 1 deletion crates/fuel-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(clippy::arithmetic_side_effects)]
#![deny(clippy::cast_possible_truncation)]
#![deny(unused_crate_dependencies)]
#![deny(warnings)]
#![allow(warnings)]

use crate::service::genesis::NotifyCancel;
use tokio_util::sync::CancellationToken;
Expand Down
Loading

0 comments on commit 19fd311

Please sign in to comment.