fuels_test_helpers/
service.rs1use 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::{Result, error};
8
9use crate::NodeConfig;
10#[cfg(not(feature = "fuel-core-lib"))]
11use crate::fuel_bin_service::FuelService as BinFuelService;
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 #[cfg(feature = "rocksdb")]
69 use fuel_core::state::rocks_db::{ColumnsPolicy, DatabaseConfig};
70 use fuel_core::{
71 combined_database::CombinedDatabaseConfig,
72 fuel_core_graphql_api::ServiceConfig as GraphQLConfig, service::config::GasPriceConfig,
73 };
74 use fuel_core_chain_config::SnapshotReader;
75
76 use crate::DbType;
77
78 let snapshot_reader = SnapshotReader::new_in_memory(chain_config, state_config);
79
80 let combined_db_config = CombinedDatabaseConfig {
81 database_path: match &node_config.database_type {
82 DbType::InMemory => Default::default(),
83 DbType::RocksDb(path) => path.clone().unwrap_or_default(),
84 },
85 database_type: node_config.database_type.into(),
86 #[cfg(feature = "rocksdb")]
87 database_config: DatabaseConfig {
88 cache_capacity: node_config.max_database_cache_size,
89 max_fds: 512,
90 columns_policy: ColumnsPolicy::Lazy,
91 },
92 #[cfg(feature = "rocksdb")]
93 state_rewind_policy: Default::default(),
94 };
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 required_fuel_block_height_tolerance: 10,
106 required_fuel_block_height_timeout: Duration::from_secs(30),
107 request_body_bytes_limit: 16 * 1024 * 1024,
108 query_log_threshold_time: Duration::from_secs(2),
109 api_request_timeout: Duration::from_secs(60),
110 database_batch_size: 100,
111 assemble_tx_dry_run_limit: 3,
112 assemble_tx_estimate_predicates_limit: 5,
113 costs: Default::default(),
114 number_of_threads: 2,
115 },
116 combined_db_config,
117 snapshot_reader,
118 historical_execution: node_config.historical_execution,
119 utxo_validation: node_config.utxo_validation,
120 debug: node_config.debug,
121 block_production: node_config.block_production.into(),
122 gas_price_config: GasPriceConfig {
123 starting_exec_gas_price: node_config.starting_gas_price,
124 ..GasPriceConfig::local_node()
125 },
126 ..ServiceConfig::local_node()
127 }
128 }
129}