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

Return only unspent messages and coins #787

Merged
merged 4 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions fuel-chain-config/src/config/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ impl StateConfig {
}

pub trait ChainConfigDb {
/// Returns *all* unspent coin configs available in the database.
fn get_coin_config(&self) -> anyhow::Result<Option<Vec<CoinConfig>>>;
/// Returns *alive* contract configs available in the database.
fn get_contract_config(&self) -> Result<Option<Vec<ContractConfig>>, anyhow::Error>;
/// Returns *all* unspent message configs available in the database.
fn get_message_config(&self) -> Result<Option<Vec<MessageConfig>>, Error>;
/// Returns the last available block height.
fn get_block_height(&self) -> Result<Option<BlockHeight>, Error>;
}
8 changes: 4 additions & 4 deletions fuel-core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,6 @@ impl P2pDb for Database {
/// Implement `ChainConfigDb` so that `Database` can be passed to
/// `StateConfig's` `generate_state_config()` method
impl ChainConfigDb for Database {
fn get_block_height(&self) -> Result<Option<BlockHeight>, Error> {
Self::get_block_height(self)
}

fn get_coin_config(&self) -> anyhow::Result<Option<Vec<CoinConfig>>> {
Self::get_coin_config(self)
}
Expand All @@ -372,6 +368,10 @@ impl ChainConfigDb for Database {
) -> Result<Option<Vec<fuel_chain_config::MessageConfig>>, Error> {
Self::get_message_config(self)
}

fn get_block_height(&self) -> Result<Option<BlockHeight>, Error> {
Self::get_block_height(self)
}
}

// TODO: Move to a separate file `database/relayer.rs`
Expand Down
13 changes: 13 additions & 0 deletions fuel-core/src/database/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use fuel_core_interfaces::{
},
},
db::Coins,
model::CoinStatus,
};
use std::borrow::Cow;

Expand Down Expand Up @@ -115,6 +116,18 @@ impl Database {
pub fn get_coin_config(&self) -> anyhow::Result<Option<Vec<CoinConfig>>> {
let configs = self
.iter_all::<Vec<u8>, Coin>(Column::Coins, None, None, None)
.filter_map(|coin| {
// Return only unspent coins
if let Ok(coin) = coin {
if coin.1.status == CoinStatus::Unspent {
Some(Ok(coin))
} else {
None
}
} else {
Some(coin)
}
})
.map(|raw_coin| -> Result<CoinConfig, anyhow::Error> {
let coin = raw_coin?;

Expand Down
12 changes: 12 additions & 0 deletions fuel-core/src/database/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ impl Database {
pub fn get_message_config(&self) -> Result<Option<Vec<MessageConfig>>, Error> {
let configs = self
.all_messages(None, None)
.filter_map(|msg| {
// Return only unspent messages
if let Ok(msg) = msg {
if msg.fuel_block_spend.is_none() {
Some(Ok(msg))
} else {
None
}
} else {
Some(msg)
}
})
.map(|msg| -> Result<MessageConfig, Error> {
let msg = msg?;

Expand Down