fuel_core/service/adapters/
gas_price_adapters.rs1use crate::{
2 database::OnChainIterableKeyValueView,
3 service::adapters::ChainStateInfoProvider,
4};
5use fuel_core_gas_price_service::{
6 common::{
7 fuel_core_storage_adapter::{
8 GasPriceSettings,
9 GasPriceSettingsProvider,
10 },
11 utils::{
12 Error as GasPriceError,
13 Result as GasPriceResult,
14 },
15 },
16 ports::{
17 GasPriceData,
18 L2Data,
19 },
20 v1::metadata::V1AlgorithmConfig,
21};
22use fuel_core_storage::{
23 Result as StorageResult,
24 transactional::HistoricalView,
25};
26use fuel_core_types::{
27 blockchain::{
28 block::Block,
29 header::ConsensusParametersVersion,
30 },
31 fuel_tx::Transaction,
32 fuel_types::BlockHeight,
33};
34
35use crate::{
36 database::{
37 Database,
38 database_description::gas_price::GasPriceDatabase,
39 },
40 service::{
41 Config,
42 config::GasPriceConfig,
43 },
44};
45
46#[cfg(test)]
47mod tests;
48
49impl L2Data for OnChainIterableKeyValueView {
50 fn latest_height(&self) -> StorageResult<BlockHeight> {
51 self.latest_height()
52 }
53
54 fn get_block(
55 &self,
56 height: &BlockHeight,
57 ) -> StorageResult<Option<Block<Transaction>>> {
58 self.get_full_block(height)
59 }
60}
61
62impl GasPriceData for Database<GasPriceDatabase> {
63 fn latest_height(&self) -> Option<BlockHeight> {
64 HistoricalView::latest_height(self)
65 }
66}
67
68impl From<Config> for V1AlgorithmConfig {
69 fn from(value: Config) -> Self {
70 let GasPriceConfig {
71 starting_exec_gas_price,
72 exec_gas_price_change_percent,
73 min_exec_gas_price,
74 exec_gas_price_threshold_percent,
75 da_committer_url: _,
76 da_poll_interval,
77 da_gas_price_factor,
78 starting_recorded_height,
79 min_da_gas_price,
80 max_da_gas_price,
81 max_da_gas_price_change_percent,
82 da_gas_price_p_component,
83 da_gas_price_d_component,
84 gas_price_metrics,
85 activity_normal_range_size,
86 activity_capped_range_size,
87 activity_decrease_range_size,
88 block_activity_threshold,
89 } = value.gas_price_config;
90 V1AlgorithmConfig {
91 new_exec_gas_price: starting_exec_gas_price.max(min_exec_gas_price),
92 min_exec_gas_price,
93 exec_gas_price_change_percent,
94 l2_block_fullness_threshold_percent: exec_gas_price_threshold_percent,
95 min_da_gas_price,
96 max_da_gas_price,
97 max_da_gas_price_change_percent,
98 da_p_component: da_gas_price_p_component,
99 da_d_component: da_gas_price_d_component,
100 normal_range_size: activity_normal_range_size,
101 capped_range_size: activity_capped_range_size,
102 decrease_range_size: activity_decrease_range_size,
103 block_activity_threshold,
104 da_poll_interval,
105 gas_price_factor: da_gas_price_factor,
106 starting_recorded_height: starting_recorded_height.map(BlockHeight::from),
107 record_metrics: gas_price_metrics,
108 }
109 }
110}
111
112impl GasPriceSettingsProvider for ChainStateInfoProvider {
113 fn settings(
114 &self,
115 param_version: &ConsensusParametersVersion,
116 ) -> GasPriceResult<GasPriceSettings> {
117 self.shared_state
118 .get_consensus_parameters(param_version)
119 .map(|params| GasPriceSettings {
120 gas_price_factor: params.fee_params().gas_price_factor(),
121 block_gas_limit: params.block_gas_limit(),
122 })
123 .map_err(|err| GasPriceError::CouldNotFetchMetadata {
124 source_error: err.into(),
125 })
126 }
127}