From 7b8d972ec7b8a045462877cf30e79d641bf2a628 Mon Sep 17 00:00:00 2001 From: green Date: Fri, 25 Nov 2022 00:50:14 +0000 Subject: [PATCH] Return only unspent messages and coins --- fuel-chain-config/src/config/state.rs | 4 ++++ fuel-core/src/database.rs | 8 ++++---- fuel-core/src/database/coin.rs | 13 +++++++++++++ fuel-core/src/database/message.rs | 12 ++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/fuel-chain-config/src/config/state.rs b/fuel-chain-config/src/config/state.rs index 097c4d04c76..25c26eb252b 100644 --- a/fuel-chain-config/src/config/state.rs +++ b/fuel-chain-config/src/config/state.rs @@ -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>>; + /// Returns *alive* contract configs available in the database. fn get_contract_config(&self) -> Result>, anyhow::Error>; + /// Returns *all* unspent message configs available in the database. fn get_message_config(&self) -> Result>, Error>; + /// Returns the last available block height. fn get_block_height(&self) -> Result, Error>; } diff --git a/fuel-core/src/database.rs b/fuel-core/src/database.rs index 1901b4bf979..63d28692540 100644 --- a/fuel-core/src/database.rs +++ b/fuel-core/src/database.rs @@ -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, Error> { - Self::get_block_height(self) - } - fn get_coin_config(&self) -> anyhow::Result>> { Self::get_coin_config(self) } @@ -372,6 +368,10 @@ impl ChainConfigDb for Database { ) -> Result>, Error> { Self::get_message_config(self) } + + fn get_block_height(&self) -> Result, Error> { + Self::get_block_height(self) + } } // TODO: Move to a separate file `database/relayer.rs` diff --git a/fuel-core/src/database/coin.rs b/fuel-core/src/database/coin.rs index d156865d9b3..b9806944169 100644 --- a/fuel-core/src/database/coin.rs +++ b/fuel-core/src/database/coin.rs @@ -24,6 +24,7 @@ use fuel_core_interfaces::{ }, }, db::Coins, + model::CoinStatus, }; use std::borrow::Cow; @@ -115,6 +116,18 @@ impl Database { pub fn get_coin_config(&self) -> anyhow::Result>> { let configs = self .iter_all::, 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 { let coin = raw_coin?; diff --git a/fuel-core/src/database/message.rs b/fuel-core/src/database/message.rs index 42dba178a35..98a776f0cb5 100644 --- a/fuel-core/src/database/message.rs +++ b/fuel-core/src/database/message.rs @@ -113,6 +113,18 @@ impl Database { pub fn get_message_config(&self) -> Result>, 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 { let msg = msg?;