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

Extract HistoricalView trait from the AtomicView #1989

Merged
merged 6 commits into from
Jun 26, 2024
Merged
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: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- [1972](https://github.com/FuelLabs/fuel-core/pull/1972): Implement `AlgorithmUpdater` for `GasPriceService`
- [#1972](https://github.com/FuelLabs/fuel-core/pull/1972): Implement `AlgorithmUpdater` for `GasPriceService`
- [#1948](https://github.com/FuelLabs/fuel-core/pull/1948): Add new `AlgorithmV1` and `AlgorithmUpdaterV1` for the gas price. Include tools for analysis

### Changed

#### Breaking
- [#1989](https://github.com/FuelLabs/fuel-core/pull/1989): Extract `HistoricalView` trait from the `AtomicView`.

## [Version 0.30.0]

### Added
Expand Down
Binary file not shown.
13 changes: 7 additions & 6 deletions crates/fuel-core/src/coins_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ mod tests {
asset,
base_asset_id,
None,
&db.view(),
&db.test_view(),
))
.map(|coins| {
coins
Expand Down Expand Up @@ -503,7 +503,7 @@ mod tests {
db: &ServiceDatabase,
) -> Result<Vec<(AssetId, u64)>, CoinsQueryError> {
let coins = random_improve(
&db.view(),
&db.test_view(),
&SpendQuery::new(owner, &query_per_asset, None, base_asset_id)?,
);

Expand Down Expand Up @@ -701,7 +701,8 @@ mod tests {
Some(excluded_ids),
base_asset_id,
)?;
let coins = random_improve(&db.service_database().view(), &spend_query);
let coins =
random_improve(&db.service_database().test_view(), &spend_query);

// Transform result for convenience
coins.map(|coins| {
Expand Down Expand Up @@ -859,7 +860,7 @@ mod tests {
}

let coins = random_improve(
&db.service_database().view(),
&db.service_database().test_view(),
&SpendQuery::new(
owner,
&[AssetSpendTarget {
Expand Down Expand Up @@ -1008,7 +1009,7 @@ mod tests {
pub fn owned_coins(&self, owner: &Address) -> Vec<Coin> {
use crate::query::CoinQueryData;
let query = self.service_database();
let query = query.view();
let query = query.test_view();
query
.owned_coins_ids(owner, None, IterDirection::Forward)
.map(|res| res.map(|id| query.coin(id).unwrap()))
Expand All @@ -1019,7 +1020,7 @@ mod tests {
pub fn owned_messages(&self, owner: &Address) -> Vec<Message> {
use crate::query::MessageQueryData;
let query = self.service_database();
let query = query.view();
let query = query.test_view();
query
.owned_message_ids(owner, None, IterDirection::Forward)
.map(|res| res.map(|id| query.message(&id).unwrap()))
Expand Down
102 changes: 22 additions & 80 deletions crates/fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ use fuel_core_storage::{
StorageInspect,
StorageMutate,
};
use fuel_core_types::{
blockchain::{
block::CompressedBlock,
primitives::DaBlockHeight,
},
fuel_types::BlockHeight,
};
use fuel_core_types::blockchain::block::CompressedBlock;
use itertools::Itertools;
use std::{
fmt::Debug,
Expand All @@ -66,8 +60,10 @@ pub use fuel_core_database::Error;
pub type Result<T> = core::result::Result<T, Error>;

// TODO: Extract `Database` and all belongs into `fuel-core-database`.
use crate::database::database_description::DatabaseHeight;
#[cfg(feature = "rocksdb")]
use crate::state::rocks_db::RocksDb;
use fuel_core_storage::transactional::HistoricalView;
#[cfg(feature = "rocksdb")]
use std::path::Path;

Expand Down Expand Up @@ -294,60 +290,32 @@ where
}
}

impl AtomicView for Database<OnChain> {
type View = Self;

type Height = BlockHeight;

fn latest_height(&self) -> Option<Self::Height> {
*self.stage.height.lock()
}

fn view_at(&self, _: &BlockHeight) -> StorageResult<Self::View> {
// TODO: Unimplemented until of the https://github.com/FuelLabs/fuel-core/issues/451
Ok(self.latest_view())
}
impl<Description> AtomicView for Database<Description>
where
Description: DatabaseDescription,
{
type LatestView = Self;

fn latest_view(&self) -> Self::View {
fn latest_view(&self) -> StorageResult<Self::LatestView> {
// TODO: https://github.com/FuelLabs/fuel-core/issues/1581
self.clone()
Ok(self.clone())
}
}

impl AtomicView for Database<OffChain> {
type View = Self;

type Height = BlockHeight;
impl<Description> HistoricalView for Database<Description>
where
Description: DatabaseDescription,
{
type Height = Description::Height;
type ViewAtHeight = Self;

fn latest_height(&self) -> Option<Self::Height> {
*self.stage.height.lock()
}

fn view_at(&self, _: &BlockHeight) -> StorageResult<Self::View> {
fn view_at(&self, _: &Self::Height) -> StorageResult<Self::ViewAtHeight> {
// TODO: Unimplemented until of the https://github.com/FuelLabs/fuel-core/issues/451
Ok(self.latest_view())
}

fn latest_view(&self) -> Self::View {
// TODO: https://github.com/FuelLabs/fuel-core/issues/1581
self.clone()
}
}

impl AtomicView for Database<Relayer> {
type View = Self;
type Height = DaBlockHeight;

fn latest_height(&self) -> Option<Self::Height> {
*self.stage.height.lock()
}

fn view_at(&self, _: &Self::Height) -> StorageResult<Self::View> {
Ok(self.latest_view())
}

fn latest_view(&self) -> Self::View {
self.clone()
Ok(self.clone())
}
}

Expand Down Expand Up @@ -409,33 +377,6 @@ impl Modifiable for GenesisDatabase<Relayer> {
}
}

trait DatabaseHeight: Sized {
fn as_u64(&self) -> u64;

fn advance_height(&self) -> Option<Self>;
}

impl DatabaseHeight for BlockHeight {
fn as_u64(&self) -> u64 {
let height: u32 = (*self).into();
height as u64
}

fn advance_height(&self) -> Option<Self> {
self.succ()
}
}

impl DatabaseHeight for DaBlockHeight {
fn as_u64(&self) -> u64 {
self.0
}

fn advance_height(&self) -> Option<Self> {
self.0.checked_add(1).map(Into::into)
}
}

fn commit_changes_with_height_update<Description>(
database: &mut Database<Description>,
changes: Changes,
Expand Down Expand Up @@ -620,7 +561,7 @@ mod tests {
.unwrap();

// Then
assert_eq!(AtomicView::latest_height(&database), None);
assert_eq!(HistoricalView::latest_height(&database), None);
}

#[test]
Expand Down Expand Up @@ -780,7 +721,7 @@ mod tests {
.unwrap();

// Then
assert_eq!(AtomicView::latest_height(&database), None);
assert_eq!(HistoricalView::latest_height(&database), None);
}

#[test]
Expand Down Expand Up @@ -903,6 +844,7 @@ mod tests {
};
use fuel_core_relayer::storage::EventsHistory;
use fuel_core_storage::transactional::WriteTransaction;
use fuel_core_types::blockchain::primitives::DaBlockHeight;

#[test]
fn column_keys_not_exceed_count_test() {
Expand Down Expand Up @@ -945,7 +887,7 @@ mod tests {
.unwrap();

// Then
assert_eq!(AtomicView::latest_height(&database), None);
assert_eq!(HistoricalView::latest_height(&database), None);
}

#[test]
Expand Down
33 changes: 32 additions & 1 deletion crates/fuel-core/src/database/database_description.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
use core::fmt::Debug;
use fuel_core_storage::kv_store::StorageColumn;
use fuel_core_types::{
blockchain::primitives::DaBlockHeight,
fuel_types::BlockHeight,
};

pub mod off_chain;
pub mod on_chain;
pub mod relayer;

pub trait DatabaseHeight: PartialEq + Default + Copy + Send + Sync {
fn as_u64(&self) -> u64;

fn advance_height(&self) -> Option<Self>;
}

impl DatabaseHeight for BlockHeight {
fn as_u64(&self) -> u64 {
let height: u32 = (*self).into();
height as u64
}

fn advance_height(&self) -> Option<Self> {
self.succ()
}
}

impl DatabaseHeight for DaBlockHeight {
fn as_u64(&self) -> u64 {
self.0
}

fn advance_height(&self) -> Option<Self> {
self.0.checked_add(1).map(Into::into)
}
}

/// The description of the database that makes it unique.
pub trait DatabaseDescription: 'static + Clone + Debug + Send + Sync {
/// The type of the column used by the database.
type Column: StorageColumn + strum::EnumCount + enum_iterator::Sequence;
/// The type of the height of the database used to track commits.
type Height: Default + Copy;
type Height: DatabaseHeight;

/// Returns the expected version of the database.
fn version() -> u32;
Expand Down
26 changes: 10 additions & 16 deletions crates/fuel-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,10 @@ mod tests {
}

impl AtomicView for DisabledRelayer {
type View = Self;
type Height = DaBlockHeight;
type LatestView = Self;

fn latest_height(&self) -> Option<Self::Height> {
Some(0u64.into())
}

fn view_at(&self, _: &Self::Height) -> StorageResult<Self::View> {
Ok(self.latest_view())
}

fn latest_view(&self) -> Self::View {
self.clone()
fn latest_view(&self) -> StorageResult<Self::LatestView> {
Ok(self.clone())
}
}

Expand Down Expand Up @@ -508,6 +499,7 @@ mod tests {
} = producer
.storage_view_provider
.latest_view()
.unwrap()
.contract_balances(recipient, None, IterDirection::Forward)
.next()
.unwrap()
Expand Down Expand Up @@ -598,6 +590,7 @@ mod tests {
} = producer
.storage_view_provider
.latest_view()
.unwrap()
.contract_balances(recipient, None, IterDirection::Forward)
.next()
.unwrap()
Expand Down Expand Up @@ -707,6 +700,7 @@ mod tests {
} = validator
.storage_view_provider
.latest_view()
.unwrap()
.contract_balances(recipient, None, IterDirection::Forward)
.next()
.unwrap()
Expand Down Expand Up @@ -2303,7 +2297,7 @@ mod tests {
};

let mut exec = make_executor(&messages);
let view = exec.storage_view_provider.latest_view();
let view = exec.storage_view_provider.latest_view().unwrap();
assert!(view.message_exists(message_coin.nonce()).unwrap());
assert!(view.message_exists(message_data.nonce()).unwrap());

Expand All @@ -2314,7 +2308,7 @@ mod tests {
assert_eq!(skipped_transactions.len(), 0);

// Successful execution consumes `message_coin` and `message_data`.
let view = exec.storage_view_provider.latest_view();
let view = exec.storage_view_provider.latest_view().unwrap();
assert!(!view.message_exists(message_coin.nonce()).unwrap());
assert!(!view.message_exists(message_data.nonce()).unwrap());
assert_eq!(
Expand Down Expand Up @@ -2350,7 +2344,7 @@ mod tests {
};

let mut exec = make_executor(&messages);
let view = exec.storage_view_provider.latest_view();
let view = exec.storage_view_provider.latest_view().unwrap();
assert!(view.message_exists(message_coin.nonce()).unwrap());
assert!(view.message_exists(message_data.nonce()).unwrap());

Expand All @@ -2361,7 +2355,7 @@ mod tests {
assert_eq!(skipped_transactions.len(), 0);

// We should spend only `message_coin`. The `message_data` should be unspent.
let view = exec.storage_view_provider.latest_view();
let view = exec.storage_view_provider.latest_view().unwrap();
assert!(!view.message_exists(message_coin.nonce()).unwrap());
assert!(view.message_exists(message_data.nonce()).unwrap());
assert_eq!(*view.coin(&UtxoId::new(tx_id, 0)).unwrap().amount(), amount);
Expand Down
8 changes: 4 additions & 4 deletions crates/fuel-core/src/graphql_api/api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ pub fn new_service<OnChain, OffChain>(
request_timeout: Duration,
) -> anyhow::Result<Service>
where
OnChain: AtomicView<Height = BlockHeight> + 'static,
OffChain: AtomicView<Height = BlockHeight> + 'static,
OnChain::View: OnChainDatabase,
OffChain::View: OffChainDatabase,
OnChain: AtomicView + 'static,
OffChain: AtomicView + 'static,
OnChain::LatestView: OnChainDatabase,
OffChain::LatestView: OffChainDatabase,
{
let network_addr = config.addr;
let combined_read_database =
Expand Down
Loading
Loading