fuels_test_helpers/
service.rs

1use std::net::SocketAddr;
2
3#[cfg(feature = "fuel-core-lib")]
4use fuel_core::service::{Config as ServiceConfig, FuelService as CoreFuelService};
5use fuel_core_chain_config::{ChainConfig, StateConfig};
6use fuel_core_services::State;
7use fuels_core::types::errors::{error, Result};
8
9#[cfg(not(feature = "fuel-core-lib"))]
10use crate::fuel_bin_service::FuelService as BinFuelService;
11use crate::NodeConfig;
12
13pub struct FuelService {
14    #[cfg(feature = "fuel-core-lib")]
15    service: CoreFuelService,
16    #[cfg(not(feature = "fuel-core-lib"))]
17    service: BinFuelService,
18    bound_address: SocketAddr,
19}
20
21impl FuelService {
22    pub async fn start(
23        node_config: NodeConfig,
24        chain_config: ChainConfig,
25        state_config: StateConfig,
26    ) -> Result<Self> {
27        #[cfg(feature = "fuel-core-lib")]
28        let service = {
29            let config = Self::service_config(node_config, chain_config, state_config);
30            CoreFuelService::new_node(config)
31                .await
32                .map_err(|err| error!(Other, "{err}"))?
33        };
34
35        #[cfg(not(feature = "fuel-core-lib"))]
36        let service = BinFuelService::new_node(node_config, chain_config, state_config).await?;
37
38        let bound_address = service.bound_address;
39
40        Ok(FuelService {
41            service,
42            bound_address,
43        })
44    }
45
46    pub async fn stop(&self) -> Result<State> {
47        #[cfg(feature = "fuel-core-lib")]
48        let result = self.service.send_stop_signal_and_await_shutdown().await;
49
50        #[cfg(not(feature = "fuel-core-lib"))]
51        let result = self.service.stop();
52
53        result.map_err(|err| error!(Other, "{err}"))
54    }
55
56    pub fn bound_address(&self) -> SocketAddr {
57        self.bound_address
58    }
59
60    #[cfg(feature = "fuel-core-lib")]
61    fn service_config(
62        node_config: NodeConfig,
63        chain_config: ChainConfig,
64        state_config: StateConfig,
65    ) -> ServiceConfig {
66        use std::time::Duration;
67
68        use fuel_core::{
69            combined_database::CombinedDatabaseConfig,
70            fuel_core_graphql_api::ServiceConfig as GraphQLConfig,
71        };
72        use fuel_core_chain_config::SnapshotReader;
73
74        #[cfg(feature = "rocksdb")]
75        use fuel_core::state::rocks_db::{ColumnsPolicy, DatabaseConfig};
76
77        use crate::DbType;
78
79        let snapshot_reader = SnapshotReader::new_in_memory(chain_config, state_config);
80
81        let combined_db_config = CombinedDatabaseConfig {
82            database_path: match &node_config.database_type {
83                DbType::InMemory => Default::default(),
84                DbType::RocksDb(path) => path.clone().unwrap_or_default(),
85            },
86            database_type: node_config.database_type.into(),
87            #[cfg(feature = "rocksdb")]
88            database_config: DatabaseConfig {
89                cache_capacity: node_config.max_database_cache_size,
90                max_fds: 512,
91                columns_policy: ColumnsPolicy::Lazy,
92            },
93            #[cfg(feature = "rocksdb")]
94            state_rewind_policy: Default::default(),
95        };
96        ServiceConfig {
97            graphql_config: GraphQLConfig {
98                addr: node_config.addr,
99                max_queries_depth: 16,
100                max_queries_complexity: 80000,
101                max_queries_recursive_depth: 16,
102                max_queries_resolver_recursive_depth: 1,
103                max_queries_directives: 10,
104                max_concurrent_queries: 1024,
105                request_body_bytes_limit: 16 * 1024 * 1024,
106                query_log_threshold_time: Duration::from_secs(2),
107                api_request_timeout: Duration::from_secs(60),
108                database_batch_size: 100,
109                costs: Default::default(),
110                number_of_threads: 2,
111            },
112            combined_db_config,
113            snapshot_reader,
114            utxo_validation: node_config.utxo_validation,
115            debug: node_config.debug,
116            block_production: node_config.block_production.into(),
117            starting_exec_gas_price: node_config.starting_gas_price,
118            ..ServiceConfig::local_node()
119        }
120    }
121}