fuel_core_producer/
block_producer.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use crate::{
    block_producer::gas_price::{
        ConsensusParametersProvider,
        GasPriceProvider as GasPriceProviderConstraint,
    },
    ports::{
        self,
        BlockProducerDatabase,
        RelayerBlockInfo,
    },
    Config,
};
use anyhow::{
    anyhow,
    Context,
};
use fuel_core_storage::transactional::{
    AtomicView,
    Changes,
};
use fuel_core_types::{
    blockchain::{
        block::Block,
        header::{
            ApplicationHeader,
            ConsensusHeader,
            PartialBlockHeader,
        },
        primitives::DaBlockHeight,
    },
    fuel_tx::{
        field::{
            InputContract,
            MintGasPrice,
        },
        Transaction,
    },
    fuel_types::{
        BlockHeight,
        Bytes32,
    },
    services::{
        block_producer::Components,
        executor::{
            TransactionExecutionStatus,
            UncommittedResult,
        },
    },
    tai64::Tai64,
};
use std::{
    future::Future,
    sync::Arc,
};
use tokio::sync::Mutex;
use tracing::debug;

#[cfg(test)]
mod tests;

pub mod gas_price;

#[derive(Debug, derive_more::Display)]
pub enum Error {
    #[display(fmt = "Genesis block is absent")]
    NoGenesisBlock,
    #[display(
        fmt = "The block height {height} should be higher than the previous block height {previous_block}"
    )]
    BlockHeightShouldBeHigherThanPrevious {
        height: BlockHeight,
        previous_block: BlockHeight,
    },
    #[display(fmt = "Previous block height {_0} doesn't exist")]
    MissingBlock(BlockHeight),
    #[display(
        fmt = "Best finalized da_height {best} is behind previous block da_height {previous_block}"
    )]
    InvalidDaFinalizationState {
        best: DaBlockHeight,
        previous_block: DaBlockHeight,
    },
}

impl From<Error> for anyhow::Error {
    fn from(error: Error) -> Self {
        anyhow::Error::msg(error)
    }
}

pub struct Producer<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider> {
    pub config: Config,
    pub view_provider: ViewProvider,
    pub txpool: TxPool,
    pub executor: Arc<Executor>,
    pub relayer: Box<dyn ports::Relayer>,
    // use a tokio lock since we want callers to yield until the previous block
    // execution has completed (which may take a while).
    pub lock: Mutex<()>,
    pub gas_price_provider: GasPriceProvider,
    pub consensus_parameters_provider: ConsensusProvider,
}

impl<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
    Producer<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
where
    ViewProvider: AtomicView + 'static,
    ViewProvider::LatestView: BlockProducerDatabase,
    ConsensusProvider: ConsensusParametersProvider,
{
    pub async fn produce_and_execute_predefined(
        &self,
        predefined_block: &Block,
    ) -> anyhow::Result<UncommittedResult<Changes>>
    where
        Executor: ports::BlockProducer<Vec<Transaction>> + 'static,
    {
        let _production_guard = self.lock.lock().await;

        let mut transactions_source = predefined_block.transactions().to_vec();

        let height = predefined_block.header().consensus().height;

        let block_time = predefined_block.header().consensus().time;

        let da_height = predefined_block.header().application().da_height;

        let header_to_produce =
            self.new_header_with_da_height(height, block_time, da_height)?;

        let maybe_mint_tx = transactions_source.pop();
        let mint_tx =
            maybe_mint_tx
                .and_then(|tx| tx.as_mint().cloned())
                .ok_or(anyhow!(
                    "The last transaction in the block should be a mint transaction"
                ))?;

        let gas_price = *mint_tx.gas_price();
        let coinbase_recipient = mint_tx.input_contract().contract_id;

        let component = Components {
            header_to_produce,
            transactions_source,
            coinbase_recipient,
            gas_price,
        };

        let result = self
            .executor
            .produce_without_commit(component)
            .map_err(Into::<anyhow::Error>::into)
            .with_context(|| {
                format!("Failed to produce block {height:?} due to execution failure")
            })?;

        debug!("Produced block with result: {:?}", result.result());
        Ok(result)
    }
}
impl<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
    Producer<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
where
    ViewProvider: AtomicView + 'static,
    ViewProvider::LatestView: BlockProducerDatabase,
    GasPriceProvider: GasPriceProviderConstraint,
    ConsensusProvider: ConsensusParametersProvider,
{
    /// Produces and execute block for the specified height.
    async fn produce_and_execute<TxSource, F>(
        &self,
        height: BlockHeight,
        block_time: Tai64,
        tx_source: impl FnOnce(u64, BlockHeight) -> F,
    ) -> anyhow::Result<UncommittedResult<Changes>>
    where
        Executor: ports::BlockProducer<TxSource> + 'static,
        F: Future<Output = anyhow::Result<TxSource>>,
    {
        //  - get previous block info (hash, root, etc)
        //  - select best da_height from relayer
        //  - get available txs from txpool
        //  - select best txs based on factors like:
        //      1. fees
        //      2. parallel throughput
        //  - Execute block with production mode to correctly malleate txs outputs and block headers

        // prevent simultaneous block production calls, the guard will drop at the end of this fn.
        let _production_guard = self.lock.lock().await;

        let gas_price = self.calculate_gas_price().await?;

        let source = tx_source(gas_price, height).await?;

        let header = self
            .new_header_with_new_da_height(height, block_time)
            .await?;

        let component = Components {
            header_to_produce: header,
            transactions_source: source,
            coinbase_recipient: self.config.coinbase_recipient.unwrap_or_default(),
            gas_price,
        };

        // Store the context string in case we error.
        let context_string =
            format!("Failed to produce block {height:?} due to execution failure");
        let result = self
            .executor
            .produce_without_commit(component)
            .map_err(Into::<anyhow::Error>::into)
            .context(context_string)?;

        debug!("Produced block with result: {:?}", result.result());
        Ok(result)
    }

    async fn calculate_gas_price(&self) -> anyhow::Result<u64> {
        self.gas_price_provider
            .next_gas_price()
            .await
            .map_err(|e| anyhow!("No gas price found: {e:?}"))
    }
}

impl<ViewProvider, TxPool, Executor, TxSource, GasPriceProvider, ConsensusProvider>
    Producer<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
where
    ViewProvider: AtomicView + 'static,
    ViewProvider::LatestView: BlockProducerDatabase,
    TxPool: ports::TxPool<TxSource = TxSource> + 'static,
    Executor: ports::BlockProducer<TxSource> + 'static,
    GasPriceProvider: GasPriceProviderConstraint,
    ConsensusProvider: ConsensusParametersProvider,
{
    /// Produces and execute block for the specified height with transactions from the `TxPool`.
    pub async fn produce_and_execute_block_txpool(
        &self,
        height: BlockHeight,
        block_time: Tai64,
    ) -> anyhow::Result<UncommittedResult<Changes>> {
        self.produce_and_execute::<TxSource, _>(
            height,
            block_time,
            |gas_price, height| self.txpool.get_source(gas_price, height),
        )
        .await
    }
}

impl<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
    Producer<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
where
    ViewProvider: AtomicView + 'static,
    ViewProvider::LatestView: BlockProducerDatabase,
    Executor: ports::BlockProducer<Vec<Transaction>> + 'static,
    GasPriceProvider: GasPriceProviderConstraint,
    ConsensusProvider: ConsensusParametersProvider,
{
    /// Produces and execute block for the specified height with `transactions`.
    pub async fn produce_and_execute_block_transactions(
        &self,
        height: BlockHeight,
        block_time: Tai64,
        transactions: Vec<Transaction>,
    ) -> anyhow::Result<UncommittedResult<Changes>> {
        self.produce_and_execute(height, block_time, |_, _| async { Ok(transactions) })
            .await
    }
}

impl<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
    Producer<ViewProvider, TxPool, Executor, GasPriceProvider, ConsensusProvider>
where
    ViewProvider: AtomicView + 'static,
    ViewProvider::LatestView: BlockProducerDatabase,
    Executor: ports::DryRunner + 'static,
    GasPriceProvider: GasPriceProviderConstraint,
    ConsensusProvider: ConsensusParametersProvider,
{
    /// Simulates multiple transactions without altering any state. Does not acquire the production lock.
    /// since it is basically a "read only" operation and shouldn't get in the way of normal
    /// production.
    pub async fn dry_run(
        &self,
        transactions: Vec<Transaction>,
        height: Option<BlockHeight>,
        time: Option<Tai64>,
        utxo_validation: Option<bool>,
        gas_price: Option<u64>,
    ) -> anyhow::Result<Vec<TransactionExecutionStatus>> {
        let view = self.view_provider.latest_view()?;
        let latest_height = view.latest_height().unwrap_or_default();

        let simulated_height = height.unwrap_or_else(|| {
            latest_height
                .succ()
                .expect("It is impossible to overflow the current block height")
        });

        let simulated_time = time.unwrap_or_else(|| {
            view.get_block(&latest_height)
                .map(|block| block.header().time())
                .unwrap_or(Tai64::UNIX_EPOCH)
        });

        let header = self.new_header(simulated_height, simulated_time)?;

        let gas_price = if let Some(inner) = gas_price {
            inner
        } else {
            self.calculate_gas_price().await?
        };

        // The dry run execution should use the state of the blockchain based on the
        // last available block, not on the upcoming one. It means that we need to
        // use the same configuration as the last block -> the same DA height.
        // It is deterministic from the result perspective, plus it is more performant
        // because we don't need to wait for the relayer to sync.
        let component = Components {
            header_to_produce: header,
            transactions_source: transactions.clone(),
            coinbase_recipient: self.config.coinbase_recipient.unwrap_or_default(),
            gas_price,
        };

        let executor = self.executor.clone();

        // use the blocking threadpool for dry_run to avoid clogging up the main async runtime
        let tx_statuses = tokio_rayon::spawn_fifo(
            move || -> anyhow::Result<Vec<TransactionExecutionStatus>> {
                Ok(executor.dry_run(component, utxo_validation)?)
            },
        )
        .await?;

        if transactions
            .iter()
            .zip(tx_statuses.iter())
            .any(|(transaction, tx_status)| {
                transaction.is_script() && tx_status.result.receipts().is_empty()
            })
        {
            Err(anyhow!("Expected at least one set of receipts"))
        } else {
            Ok(tx_statuses)
        }
    }
}

pub const NO_NEW_DA_HEIGHT_FOUND: &str = "No new da_height found";

impl<ViewProvider, TxPool, Executor, GP, ConsensusProvider>
    Producer<ViewProvider, TxPool, Executor, GP, ConsensusProvider>
where
    ViewProvider: AtomicView + 'static,
    ViewProvider::LatestView: BlockProducerDatabase,
    ConsensusProvider: ConsensusParametersProvider,
{
    /// Create the header for a new block at the provided height
    async fn new_header_with_new_da_height(
        &self,
        height: BlockHeight,
        block_time: Tai64,
    ) -> anyhow::Result<PartialBlockHeader> {
        let mut block_header = self.new_header(height, block_time)?;
        let previous_da_height = block_header.da_height;
        let gas_limit = self
            .consensus_parameters_provider
            .consensus_params_at_version(&block_header.consensus_parameters_version)?
            .block_gas_limit();
        // We have a hard limit of u16::MAX transactions per block, including the final mint transactions.
        // Therefore we choose the `new_da_height` to never include more than u16::MAX - 1 transactions in a block.
        let new_da_height = self
            .select_new_da_height(gas_limit, previous_da_height, u16::MAX - 1)
            .await?;

        block_header.application.da_height = new_da_height;

        Ok(block_header)
    }
    /// Create the header for a new block at the provided height
    fn new_header_with_da_height(
        &self,
        height: BlockHeight,
        block_time: Tai64,
        da_height: DaBlockHeight,
    ) -> anyhow::Result<PartialBlockHeader> {
        let mut block_header = self.new_header(height, block_time)?;
        block_header.application.da_height = da_height;
        Ok(block_header)
    }
    async fn select_new_da_height(
        &self,
        gas_limit: u64,
        previous_da_height: DaBlockHeight,
        transactions_limit: u16,
    ) -> anyhow::Result<DaBlockHeight> {
        let mut new_best = previous_da_height;
        let mut total_cost: u64 = 0;
        let transactions_limit: u64 = transactions_limit as u64;
        let mut total_transactions: u64 = 0;
        let highest = self
            .relayer
            .wait_for_at_least_height(&previous_da_height)
            .await?;
        if highest < previous_da_height {
            return Err(Error::InvalidDaFinalizationState {
                best: highest,
                previous_block: previous_da_height,
            }
            .into());
        }

        if highest == previous_da_height {
            return Ok(highest);
        }

        let next_da_height = previous_da_height.saturating_add(1);
        for height in next_da_height..=highest.0 {
            let RelayerBlockInfo { gas_cost, tx_count } = self
                .relayer
                .get_cost_and_transactions_number_for_block(&DaBlockHeight(height))
                .await?;
            total_cost = total_cost.saturating_add(gas_cost);
            total_transactions = total_transactions.saturating_add(tx_count);
            if total_cost > gas_limit || total_transactions > transactions_limit {
                break;
            }

            new_best = DaBlockHeight(height);
        }

        if new_best == previous_da_height {
            Err(anyhow!(NO_NEW_DA_HEIGHT_FOUND))
        } else {
            Ok(new_best)
        }
    }

    fn new_header(
        &self,
        height: BlockHeight,
        block_time: Tai64,
    ) -> anyhow::Result<PartialBlockHeader> {
        let view = self.view_provider.latest_view()?;
        let previous_block_info = self.previous_block_info(height, &view)?;
        let consensus_parameters_version = view.latest_consensus_parameters_version()?;
        let state_transition_bytecode_version =
            view.latest_state_transition_bytecode_version()?;

        Ok(PartialBlockHeader {
            application: ApplicationHeader {
                da_height: previous_block_info.da_height,
                consensus_parameters_version,
                state_transition_bytecode_version,
                generated: Default::default(),
            },
            consensus: ConsensusHeader {
                prev_root: previous_block_info.prev_root,
                height,
                time: block_time,
                generated: Default::default(),
            },
        })
    }

    fn previous_block_info(
        &self,
        height: BlockHeight,
        view: &ViewProvider::LatestView,
    ) -> anyhow::Result<PreviousBlockInfo> {
        let latest_height = self
            .view_provider
            .latest_view()?
            .latest_height()
            .ok_or(Error::NoGenesisBlock)?;
        // block 0 is reserved for genesis
        if height <= latest_height {
            Err(Error::BlockHeightShouldBeHigherThanPrevious {
                height,
                previous_block: latest_height,
            }
            .into())
        } else {
            // get info from previous block height
            let prev_height = height.pred().expect("We checked the height above");
            let previous_block = view.get_block(&prev_height)?;
            let prev_root = view.block_header_merkle_root(&prev_height)?;

            Ok(PreviousBlockInfo {
                prev_root,
                da_height: previous_block.header().da_height,
            })
        }
    }
}

struct PreviousBlockInfo {
    prev_root: Bytes32,
    da_height: DaBlockHeight,
}