use crate::Mappable;
use fuel_core_types::{
blockchain::{
block::CompressedBlock,
consensus::Consensus,
header::{
ConsensusParametersVersion,
StateTransitionBytecodeVersion,
},
},
entities::{
coins::coin::CompressedCoin,
contract::ContractUtxoInfo,
relayer::message::Message,
},
fuel_tx::{
Bytes32,
ConsensusParameters,
Transaction,
TxId,
UtxoId,
},
fuel_types::{
BlockHeight,
ContractId,
Nonce,
},
};
pub use fuel_vm_private::storage::{
ContractsAssets,
ContractsRawCode,
ContractsState,
UploadedBytecodes,
};
pub struct FuelBlocks;
impl Mappable for FuelBlocks {
type Key = Self::OwnedKey;
type OwnedKey = BlockHeight;
type Value = Self::OwnedValue;
type OwnedValue = CompressedBlock;
}
pub struct ContractsLatestUtxo;
impl Mappable for ContractsLatestUtxo {
type Key = Self::OwnedKey;
type OwnedKey = ContractId;
type Value = Self::OwnedValue;
type OwnedValue = ContractUtxoInfo;
}
pub struct SealedBlockConsensus;
impl Mappable for SealedBlockConsensus {
type Key = Self::OwnedKey;
type OwnedKey = BlockHeight;
type Value = Self::OwnedValue;
type OwnedValue = Consensus;
}
pub struct Coins;
impl Mappable for Coins {
type Key = Self::OwnedKey;
type OwnedKey = UtxoId;
type Value = Self::OwnedValue;
type OwnedValue = CompressedCoin;
}
pub struct Messages;
impl Mappable for Messages {
type Key = Self::OwnedKey;
type OwnedKey = Nonce;
type Value = Self::OwnedValue;
type OwnedValue = Message;
}
pub struct Transactions;
impl Mappable for Transactions {
type Key = Self::OwnedKey;
type OwnedKey = TxId;
type Value = Self::OwnedValue;
type OwnedValue = Transaction;
}
pub struct ProcessedTransactions;
impl Mappable for ProcessedTransactions {
type Key = Self::OwnedKey;
type OwnedKey = TxId;
type Value = Self::OwnedValue;
type OwnedValue = ();
}
pub struct ConsensusParametersVersions;
impl Mappable for ConsensusParametersVersions {
type Key = Self::OwnedKey;
type OwnedKey = ConsensusParametersVersion;
type Value = Self::OwnedValue;
type OwnedValue = ConsensusParameters;
}
pub struct StateTransitionBytecodeVersions;
impl Mappable for StateTransitionBytecodeVersions {
type Key = Self::OwnedKey;
type OwnedKey = StateTransitionBytecodeVersion;
type Value = Self::OwnedValue;
type OwnedValue = Bytes32;
}
pub mod merkle {
use crate::{
Mappable,
MerkleRoot,
};
use fuel_core_types::{
fuel_merkle::{
binary,
sparse,
},
fuel_tx::ContractId,
fuel_types::BlockHeight,
};
#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum DenseMetadataKey<PrimaryKey> {
Primary(PrimaryKey),
#[default]
Latest,
}
#[cfg(feature = "test-helpers")]
impl<PrimaryKey> rand::distributions::Distribution<DenseMetadataKey<PrimaryKey>>
for rand::distributions::Standard
where
rand::distributions::Standard: rand::distributions::Distribution<PrimaryKey>,
{
fn sample<R: rand::Rng + ?Sized>(
&self,
rng: &mut R,
) -> DenseMetadataKey<PrimaryKey> {
DenseMetadataKey::Primary(rng.gen())
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub enum DenseMerkleMetadata {
V1(DenseMerkleMetadataV1),
}
impl Default for DenseMerkleMetadata {
fn default() -> Self {
Self::V1(Default::default())
}
}
impl DenseMerkleMetadata {
pub fn new(root: MerkleRoot, version: u64) -> Self {
let metadata = DenseMerkleMetadataV1 { root, version };
Self::V1(metadata)
}
pub fn root(&self) -> &MerkleRoot {
match self {
DenseMerkleMetadata::V1(metadata) => &metadata.root,
}
}
pub fn version(&self) -> u64 {
match self {
DenseMerkleMetadata::V1(metadata) => metadata.version,
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct DenseMerkleMetadataV1 {
pub root: MerkleRoot,
pub version: u64,
}
impl Default for DenseMerkleMetadataV1 {
fn default() -> Self {
let empty_merkle_tree = binary::root_calculator::MerkleRootCalculator::new();
Self {
root: empty_merkle_tree.root(),
version: 0,
}
}
}
impl From<DenseMerkleMetadataV1> for DenseMerkleMetadata {
fn from(value: DenseMerkleMetadataV1) -> Self {
Self::V1(value)
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub enum SparseMerkleMetadata {
V1(SparseMerkleMetadataV1),
}
impl Default for SparseMerkleMetadata {
fn default() -> Self {
Self::V1(Default::default())
}
}
impl SparseMerkleMetadata {
pub fn new(root: MerkleRoot) -> Self {
let metadata = SparseMerkleMetadataV1 { root };
Self::V1(metadata)
}
pub fn root(&self) -> &MerkleRoot {
match self {
SparseMerkleMetadata::V1(metadata) => &metadata.root,
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct SparseMerkleMetadataV1 {
pub root: MerkleRoot,
}
impl Default for SparseMerkleMetadataV1 {
fn default() -> Self {
let empty_merkle_tree = sparse::in_memory::MerkleTree::new();
Self {
root: empty_merkle_tree.root(),
}
}
}
impl From<SparseMerkleMetadataV1> for SparseMerkleMetadata {
fn from(value: SparseMerkleMetadataV1) -> Self {
Self::V1(value)
}
}
pub struct FuelBlockMerkleData;
impl Mappable for FuelBlockMerkleData {
type Key = u64;
type OwnedKey = Self::Key;
type Value = binary::Primitive;
type OwnedValue = Self::Value;
}
pub struct FuelBlockMerkleMetadata;
impl Mappable for FuelBlockMerkleMetadata {
type Key = DenseMetadataKey<BlockHeight>;
type OwnedKey = Self::Key;
type Value = DenseMerkleMetadata;
type OwnedValue = Self::Value;
}
pub struct ContractsAssetsMerkleData;
impl Mappable for ContractsAssetsMerkleData {
type Key = [u8; 32];
type OwnedKey = Self::Key;
type Value = sparse::Primitive;
type OwnedValue = Self::Value;
}
pub struct ContractsAssetsMerkleMetadata;
impl Mappable for ContractsAssetsMerkleMetadata {
type Key = ContractId;
type OwnedKey = Self::Key;
type Value = SparseMerkleMetadata;
type OwnedValue = Self::Value;
}
pub struct ContractsStateMerkleData;
impl Mappable for ContractsStateMerkleData {
type Key = [u8; 32];
type OwnedKey = Self::Key;
type Value = sparse::Primitive;
type OwnedValue = Self::Value;
}
pub struct ContractsStateMerkleMetadata;
impl Mappable for ContractsStateMerkleMetadata {
type Key = ContractId;
type OwnedKey = Self::Key;
type Value = SparseMerkleMetadata;
type OwnedValue = Self::Value;
}
}