1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::net::SocketAddr;

#[cfg(feature = "fuel-core-lib")]
use fuel_core::service::{Config as ServiceConfig, FuelService as CoreFuelService};
use fuel_core_chain_config::{ChainConfig, StateConfig};
#[cfg(feature = "fuel-core-lib")]
use fuel_core_services::Service;
use fuel_core_services::State;
use fuels_core::types::errors::{error, Result};

#[cfg(not(feature = "fuel-core-lib"))]
use crate::fuel_bin_service::FuelService as BinFuelService;
use crate::NodeConfig;

pub struct FuelService {
    #[cfg(feature = "fuel-core-lib")]
    service: CoreFuelService,
    #[cfg(not(feature = "fuel-core-lib"))]
    service: BinFuelService,
    bound_address: SocketAddr,
}

impl FuelService {
    pub async fn start(
        node_config: NodeConfig,
        chain_config: ChainConfig,
        state_config: StateConfig,
    ) -> Result<Self> {
        #[cfg(feature = "fuel-core-lib")]
        let service = {
            let config = Self::service_config(node_config, chain_config, state_config);
            CoreFuelService::new_node(config)
                .await
                .map_err(|err| error!(Other, "{err}"))?
        };

        #[cfg(not(feature = "fuel-core-lib"))]
        let service = BinFuelService::new_node(node_config, chain_config, state_config).await?;

        let bound_address = service.bound_address;

        Ok(FuelService {
            service,
            bound_address,
        })
    }

    pub async fn stop(&self) -> Result<State> {
        #[cfg(feature = "fuel-core-lib")]
        let result = self.service.stop_and_await().await;

        #[cfg(not(feature = "fuel-core-lib"))]
        let result = self.service.stop();

        result.map_err(|err| error!(Other, "{err}"))
    }

    pub fn bound_address(&self) -> SocketAddr {
        self.bound_address
    }

    #[cfg(feature = "fuel-core-lib")]
    fn service_config(
        node_config: NodeConfig,
        chain_config: ChainConfig,
        state_config: StateConfig,
    ) -> ServiceConfig {
        use fuel_core::combined_database::CombinedDatabaseConfig;
        use fuel_core_chain_config::SnapshotReader;

        use crate::{DbType, MAX_DATABASE_CACHE_SIZE};

        let snapshot_reader = SnapshotReader::new_in_memory(chain_config, state_config);

        let combined_db_config = CombinedDatabaseConfig {
            max_database_cache_size: node_config
                .max_database_cache_size
                .unwrap_or(MAX_DATABASE_CACHE_SIZE),
            database_path: match &node_config.database_type {
                DbType::InMemory => Default::default(),
                DbType::RocksDb(path) => path.clone().unwrap_or_default(),
            },
            database_type: node_config.database_type.into(),
        };
        ServiceConfig {
            addr: node_config.addr,
            combined_db_config,
            snapshot_reader,
            utxo_validation: node_config.utxo_validation,
            debug: node_config.debug,
            block_production: node_config.block_production.into(),
            ..ServiceConfig::local_node()
        }
    }
}