fuel_core_gas_price_service/v0/
service.rs

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::{
    common::{
        l2_block_source::L2BlockSource,
        updater_metadata::UpdaterMetadata,
        utils::BlockInfo,
    },
    ports::MetadataStorage,
    v0::uninitialized_task::SharedV0Algorithm,
};
use anyhow::anyhow;
use async_trait::async_trait;
use fuel_core_services::{
    RunnableTask,
    StateWatcher,
};
use fuel_gas_price_algorithm::v0::{
    AlgorithmUpdaterV0,
    AlgorithmV0,
};
use futures::FutureExt;
use std::num::NonZeroU64;

/// The service that updates the gas price algorithm.
pub struct GasPriceServiceV0<L2, Metadata> {
    /// The algorithm that can be used in the next block
    shared_algo: SharedV0Algorithm,
    /// The L2 block source
    l2_block_source: L2,
    /// The metadata storage
    metadata_storage: Metadata,
    /// The algorithm updater
    algorithm_updater: AlgorithmUpdaterV0,
}

impl<L2, Metadata> GasPriceServiceV0<L2, Metadata>
where
    Metadata: MetadataStorage,
{
    pub fn new(
        l2_block_source: L2,
        metadata_storage: Metadata,
        shared_algo: SharedV0Algorithm,
        algorithm_updater: AlgorithmUpdaterV0,
    ) -> Self {
        Self {
            shared_algo,
            l2_block_source,
            metadata_storage,
            algorithm_updater,
        }
    }

    pub fn algorithm_updater(&self) -> &AlgorithmUpdaterV0 {
        &self.algorithm_updater
    }

    pub fn next_block_algorithm(&self) -> SharedV0Algorithm {
        self.shared_algo.clone()
    }

    async fn update(&mut self, new_algorithm: AlgorithmV0) {
        self.shared_algo.update(new_algorithm).await;
    }

    fn validate_block_gas_capacity(
        &self,
        block_gas_capacity: u64,
    ) -> anyhow::Result<NonZeroU64> {
        NonZeroU64::new(block_gas_capacity)
            .ok_or_else(|| anyhow!("Block gas capacity must be non-zero"))
    }

    async fn set_metadata(&mut self) -> anyhow::Result<()> {
        let metadata: UpdaterMetadata = self.algorithm_updater.clone().into();
        self.metadata_storage
            .set_metadata(&metadata)
            .map_err(|err| anyhow!(err))
    }

    async fn handle_normal_block(
        &mut self,
        height: u32,
        gas_used: u64,
        block_gas_capacity: u64,
    ) -> anyhow::Result<()> {
        let capacity = self.validate_block_gas_capacity(block_gas_capacity)?;

        self.algorithm_updater
            .update_l2_block_data(height, gas_used, capacity)?;

        self.set_metadata().await?;
        Ok(())
    }

    async fn apply_block_info_to_gas_algorithm(
        &mut self,
        l2_block: BlockInfo,
    ) -> anyhow::Result<()> {
        match l2_block {
            BlockInfo::GenesisBlock => {
                self.set_metadata().await?;
            }
            BlockInfo::Block {
                height,
                gas_used,
                block_gas_capacity,
            } => {
                self.handle_normal_block(height, gas_used, block_gas_capacity)
                    .await?;
            }
        }

        self.update(self.algorithm_updater.algorithm()).await;
        Ok(())
    }
}

#[async_trait]
impl<L2, Metadata> RunnableTask for GasPriceServiceV0<L2, Metadata>
where
    L2: L2BlockSource,
    Metadata: MetadataStorage,
{
    async fn run(&mut self, watcher: &mut StateWatcher) -> anyhow::Result<bool> {
        let should_continue;
        tokio::select! {
            biased;
            _ = watcher.while_started() => {
                tracing::debug!("Stopping gas price service");
                should_continue = false;
            }
            l2_block_res = self.l2_block_source.get_l2_block() => {
                tracing::info!("Received L2 block result: {:?}", l2_block_res);
                let block = l2_block_res?;

                tracing::debug!("Updating gas price algorithm");
                self.apply_block_info_to_gas_algorithm(block).await?;
                should_continue = true;
            }
        }
        Ok(should_continue)
    }

    async fn shutdown(mut self) -> anyhow::Result<()> {
        while let Some(Ok(block)) = self.l2_block_source.get_l2_block().now_or_never() {
            tracing::debug!("Updating gas price algorithm");
            self.apply_block_info_to_gas_algorithm(block).await?;
        }
        Ok(())
    }
}

#[allow(clippy::arithmetic_side_effects)]
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use crate::{
        common::{
            l2_block_source::L2BlockSource,
            updater_metadata::UpdaterMetadata,
            utils::{
                BlockInfo,
                Result as GasPriceResult,
            },
        },
        ports::MetadataStorage,
        v0::{
            metadata::V0Metadata,
            service::GasPriceServiceV0,
            uninitialized_task::{
                initialize_algorithm,
                SharedV0Algorithm,
            },
        },
    };
    use fuel_core_services::{
        RunnableService,
        Service,
        ServiceRunner,
        StateWatcher,
    };
    use fuel_core_types::fuel_types::BlockHeight;
    use std::sync::Arc;
    use tokio::sync::mpsc;

    #[async_trait::async_trait]
    impl<L2, Metadata> RunnableService for GasPriceServiceV0<L2, Metadata>
    where
        L2: L2BlockSource,
        Metadata: MetadataStorage,
    {
        const NAME: &'static str = "GasPriceServiceV0";
        type SharedData = SharedV0Algorithm;
        type Task = Self;
        type TaskParams = ();

        fn shared_data(&self) -> Self::SharedData {
            self.shared_algo.clone()
        }

        async fn into_task(
            mut self,
            _state_watcher: &StateWatcher,
            _params: Self::TaskParams,
        ) -> anyhow::Result<Self::Task> {
            let algorithm = self.algorithm_updater.algorithm();
            self.shared_algo.update(algorithm).await;
            Ok(self)
        }
    }

    struct FakeL2BlockSource {
        l2_block: mpsc::Receiver<BlockInfo>,
    }

    #[async_trait::async_trait]
    impl L2BlockSource for FakeL2BlockSource {
        async fn get_l2_block(&mut self) -> GasPriceResult<BlockInfo> {
            let block = self.l2_block.recv().await.unwrap();
            Ok(block)
        }
    }

    struct FakeMetadata {
        inner: Arc<std::sync::Mutex<Option<UpdaterMetadata>>>,
    }

    impl FakeMetadata {
        fn empty() -> Self {
            Self {
                inner: Arc::new(std::sync::Mutex::new(None)),
            }
        }
    }

    impl MetadataStorage for FakeMetadata {
        fn get_metadata(
            &self,
            _: &BlockHeight,
        ) -> GasPriceResult<Option<UpdaterMetadata>> {
            let metadata = self.inner.lock().unwrap().clone();
            Ok(metadata)
        }

        fn set_metadata(&mut self, metadata: &UpdaterMetadata) -> GasPriceResult<()> {
            *self.inner.lock().unwrap() = Some(metadata.clone());
            Ok(())
        }
    }

    #[tokio::test]
    async fn run__updates_gas_price() {
        // given
        let block_height = 1;
        let l2_block = BlockInfo::Block {
            height: block_height,
            gas_used: 60,
            block_gas_capacity: 100,
        };
        let (l2_block_sender, l2_block_receiver) = mpsc::channel(1);
        let l2_block_source = FakeL2BlockSource {
            l2_block: l2_block_receiver,
        };
        let metadata_storage = FakeMetadata::empty();
        let starting_metadata = V0Metadata {
            min_exec_gas_price: 10,
            exec_gas_price_change_percent: 10,
            new_exec_price: 100,
            l2_block_fullness_threshold_percent: 0,
            l2_block_height: 0,
        };
        let (algo_updater, shared_algo) =
            initialize_algorithm(starting_metadata.clone(), &metadata_storage).unwrap();

        let service = GasPriceServiceV0::new(
            l2_block_source,
            metadata_storage,
            shared_algo,
            algo_updater,
        );
        let read_algo = service.next_block_algorithm();
        let service = ServiceRunner::new(service);
        let prev = read_algo.next_gas_price();

        // when
        service.start_and_await().await.unwrap();
        l2_block_sender.send(l2_block).await.unwrap();
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        // then
        let actual_price = read_algo.next_gas_price();
        assert_ne!(prev, actual_price);
        service.stop_and_await().await.unwrap();
    }
}