Skip to content

Commit

Permalink
Return only unspent messages and coins (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx authored Nov 29, 2022
1 parent 22116cf commit 398b36d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
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 @@ -356,10 +356,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 @@ -373,6 +369,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

0 comments on commit 398b36d

Please sign in to comment.