use crate::serialization::HexNumber;
use fuel_core_storage::Result as StorageResult;
use fuel_core_types::fuel_types::BlockHeight;
use serde::{
Deserialize,
Serialize,
};
use serde_with::{
serde_as,
skip_serializing_none,
};
use super::{
coin::CoinConfig,
contract::ContractConfig,
message::MessageConfig,
};
#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct StateConfig {
pub coins: Option<Vec<CoinConfig>>,
pub contracts: Option<Vec<ContractConfig>>,
pub messages: Option<Vec<MessageConfig>>,
#[serde_as(as = "Option<HexNumber>")]
#[serde(default)]
pub height: Option<BlockHeight>,
}
impl StateConfig {
pub fn generate_state_config<T>(db: T) -> StorageResult<Self>
where
T: ChainConfigDb,
{
Ok(StateConfig {
coins: db.get_coin_config()?,
contracts: db.get_contract_config()?,
messages: db.get_message_config()?,
height: Some(db.get_block_height()?),
})
}
}
pub trait ChainConfigDb {
fn get_coin_config(&self) -> StorageResult<Option<Vec<CoinConfig>>>;
fn get_contract_config(&self) -> StorageResult<Option<Vec<ContractConfig>>>;
fn get_message_config(&self) -> StorageResult<Option<Vec<MessageConfig>>>;
fn get_block_height(&self) -> StorageResult<BlockHeight>;
}