fuel-core-importer 0.48.2

Fuel Block Importer
Documentation
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use crate::{
    Config,
    ImporterResult,
    error::Error,
    local_runner::LocalRunner,
    ports::{
        BlockReconciliationWritePort,
        BlockVerifier,
        DatabaseTransaction,
        ImporterDatabase,
        Transactional,
        Validator,
    },
};
use fuel_core_metrics::importer::importer_metrics;
use fuel_core_storage::{
    not_found,
    transactional::{
        Changes,
        StorageChanges,
    },
};
use fuel_core_types::{
    blockchain::{
        SealedBlock,
        consensus::Consensus,
    },
    fuel_tx::{
        Transaction,
        field::MintGasPrice,
    },
    fuel_types::{
        BlockHeight,
        ChainId,
    },
    services::{
        block_importer::{
            ImportResult,
            Source,
            UncommittedResult,
        },
        executor::{
            Event,
            TransactionExecutionStatus,
            ValidationResult,
        },
    },
};
use std::{
    ops::Deref,
    sync::Arc,
    time::{
        Duration,
        Instant,
        SystemTime,
        UNIX_EPOCH,
    },
};
use tokio::sync::{
    OwnedSemaphorePermit,
    Semaphore,
    broadcast,
    mpsc,
    oneshot,
};
use tracing::warn;

#[cfg(test)]
pub mod test;

enum CommitInput {
    Uncommitted(UncommittedResult<Changes>),
    PrepareImportResult(PrepareImportResult),
}

enum Commands {
    Stop,
    CommitResult {
        result: CommitInput,
        permit: OwnedSemaphorePermit,
        callback: oneshot::Sender<Result<(), Error>>,
    },
    #[cfg(test)]
    VerifyAndExecuteBlock {
        sealed_block: SealedBlock,
        callback: oneshot::Sender<Result<UncommittedResult<Changes>, Error>>,
    },
    PrepareImportResult {
        sealed_block: SealedBlock,
        callback: oneshot::Sender<Result<PrepareImportResult, Error>>,
    },
}

struct ImporterInner<D, E, V, W> {
    database: D,
    executor: E,
    verifier: V,
    chain_id: ChainId,
    broadcast: broadcast::Sender<ImporterResult>,
    commands: mpsc::UnboundedReceiver<Commands>,
    /// Enables prometheus metrics for this fuel-service
    metrics: bool,
    block_reconciliation_write_port: W,
}

#[cfg(test)]
#[derive(Default)]
struct NoopBlockReconciliationWritePort;

#[cfg(test)]
impl BlockReconciliationWritePort for NoopBlockReconciliationWritePort {
    fn publish_produced_block(&self, _block: &SealedBlock) -> anyhow::Result<()> {
        Ok(())
    }
}

pub struct Importer {
    broadcast: broadcast::Sender<ImporterResult>,
    guard: Semaphore,
    commands: mpsc::UnboundedSender<Commands>,
    /// The semaphore tracks the number of unprocessed `SharedImportResult`.
    /// If the number of unprocessed results is more than the threshold,
    /// the block importer stops committing new blocks and waits for
    /// the resolution of the previous one.
    active_import_results: Arc<Semaphore>,
    inner: Option<std::thread::JoinHandle<()>>,
}

impl Drop for Importer {
    fn drop(&mut self) {
        let _ = self.commands.send(Commands::Stop);

        let inner = self.inner.take();

        if let Some(inner) = inner {
            let result = inner.join();

            if let Err(err) = result {
                tracing::error!("The inner importer thread panicked: {:?}", err);
            }
        }
    }
}

impl Importer {
    pub fn new<D, E, V, W>(
        chain_id: ChainId,
        config: Config,
        database: D,
        executor: E,
        verifier: V,
        block_reconciliation_write_port: W,
    ) -> Self
    where
        D: ImporterDatabase + Transactional + 'static,
        E: Validator + 'static,
        V: BlockVerifier + 'static,
        W: BlockReconciliationWritePort + Send + Sync + 'static,
    {
        // We use semaphore as a back pressure mechanism instead of a `broadcast`
        // channel because we want to prevent committing to the database results
        // that will not be processed.
        let max_block_notify_buffer = config.max_block_notify_buffer;
        let (broadcast, _) = broadcast::channel(max_block_notify_buffer);
        let (sender, receiver) = mpsc::unbounded_channel();

        let mut inner = ImporterInner {
            database,
            executor,
            verifier,
            chain_id,
            commands: receiver,
            broadcast: broadcast.clone(),
            metrics: config.metrics,
            block_reconciliation_write_port,
        };

        if config.metrics {
            inner.init_metrics();
        }

        let inner = std::thread::spawn(move || {
            let runtime = tokio::runtime::Builder::new_current_thread()
                .build()
                .expect("Failed to create tokio runtime for importer inner task");
            runtime.block_on(inner.run());
        });

        Self {
            broadcast,
            commands: sender,
            active_import_results: Arc::new(Semaphore::new(max_block_notify_buffer)),
            guard: Semaphore::new(1),
            inner: Some(inner),
        }
    }

    #[cfg(test)]
    pub fn default_config<D, E, V>(database: D, executor: E, verifier: V) -> Self
    where
        D: ImporterDatabase + Transactional + 'static,
        E: Validator + 'static,
        V: BlockVerifier + 'static,
    {
        Self::new(
            Default::default(),
            Default::default(),
            database,
            executor,
            verifier,
            NoopBlockReconciliationWritePort,
        )
    }

    pub fn subscribe(&self) -> broadcast::Receiver<ImporterResult> {
        self.broadcast.subscribe()
    }

    pub(crate) fn lock(&self) -> Result<tokio::sync::SemaphorePermit<'_>, Error> {
        let guard = self.guard.try_acquire();
        match guard {
            Ok(permit) => Ok(permit),
            Err(err) => {
                tracing::error!(
                    "The semaphore was acquired before. It is a problem \
                    because the current architecture doesn't expect that."
                );
                Err(Error::Semaphore(err))
            }
        }
    }
}

impl Importer {
    /// The method commits the result of the block execution attaching the consensus data.
    /// It expects that the `UncommittedResult` contains the result of the block
    /// execution(It includes the block itself), but not more.
    ///
    /// It doesn't do any checks regarding block validity(execution, fields, signing, etc.).
    /// It only checks the validity of the database.
    ///
    /// After the commit into the database notifies about a new imported block.
    ///
    /// # Concurrency
    ///
    /// Only one commit may be in progress at the time. All other calls will fail.
    /// Returns an error if called while another call is in progress.
    pub async fn commit_result(
        &self,
        result: UncommittedResult<Changes>,
    ) -> Result<(), Error> {
        let _guard = self.lock()?;

        // Await until all receivers of the notification process the result.
        const TIMEOUT: u64 = 20;
        let await_result = tokio::time::timeout(
            Duration::from_secs(TIMEOUT),
            self.active_import_results.clone().acquire_owned(),
        )
        .await;

        let Ok(permit) = await_result else {
            tracing::error!(
                "The previous block processing \
                    was not finished for {TIMEOUT} seconds."
            );
            return Err(Error::PreviousBlockProcessingNotFinished)
        };
        let permit = permit.map_err(Error::ActiveBlockResultsSemaphoreClosed)?;

        self.run_commit_result(permit, CommitInput::Uncommitted(result))
            .await
    }

    async fn run_commit_result(
        &self,
        permit: OwnedSemaphorePermit,
        result: CommitInput,
    ) -> Result<(), Error> {
        let (sender, receiver) = oneshot::channel();

        let command = Commands::CommitResult {
            result,
            permit,
            callback: sender,
        };
        self.commands.send(command)?;
        receiver.await?
    }

    #[cfg(test)]
    async fn run_verify_and_execute_block(
        &self,
        sealed_block: SealedBlock,
    ) -> Result<UncommittedResult<Changes>, Error> {
        let (sender, receiver) = oneshot::channel();
        let command = Commands::VerifyAndExecuteBlock {
            sealed_block,
            callback: sender,
        };
        self.commands.send(command)?;
        receiver.await?
    }

    async fn run_prepare_import_result(
        &self,
        sealed_block: SealedBlock,
    ) -> Result<PrepareImportResult, Error> {
        let (sender, receiver) = oneshot::channel();
        let command = Commands::PrepareImportResult {
            sealed_block,
            callback: sender,
        };
        self.commands.send(command)?;
        receiver.await?
    }
}

impl<D, E, V, W> ImporterInner<D, E, V, W>
where
    D: ImporterDatabase + Transactional,
    E: Send + Sync,
    V: Send + Sync,
    W: BlockReconciliationWritePort,
{
    /// The method commits the result of the block execution attaching the consensus data.
    /// It expects that the `UncommittedResult` contains the result of the block
    /// execution(It includes the block itself), but not more.
    ///
    /// It doesn't do any checks regarding block validity(execution, fields, signing, etc.).
    /// It only checks the validity of the database.
    ///
    /// After the commit into the database notifies about a new imported block.
    pub fn commit_result(
        &mut self,
        runner: &LocalRunner,
        permit: OwnedSemaphorePermit,
        result: CommitInput,
    ) -> Result<(), Error> {
        runner.run(move || {
            let result = match result {
                CommitInput::Uncommitted(result) => {
                    let block_changes = create_block_changes(
                        &self.chain_id,
                        &result.result().sealed_block,
                        &self.database,
                    )?;

                    PrepareImportResult {
                        result,
                        block_changes,
                    }
                }
                CommitInput::PrepareImportResult(result) => result,
            };

            self._commit_result(result, permit)
        })
    }

    /// The method commits the result of the block execution and notifies about a new imported block.
    #[tracing::instrument(
        skip_all,
        fields(
            block_id = % prepare.result.result().sealed_block.entity.id(),
            height = * * prepare.result.result().sealed_block.entity.header().height(),
            tx_count = prepare.result.result().tx_status.len(),
        ),
        err
    )]
    fn _commit_result(
        &mut self,
        prepare: PrepareImportResult,
        permit: OwnedSemaphorePermit,
    ) -> Result<(), Error> {
        let PrepareImportResult {
            result,
            block_changes,
        } = prepare;

        let (result, changes) = result.into();
        tracing::trace!(tx_status = ?result.tx_status, "Full transaction execution status");
        let block = &result.sealed_block.entity;
        let actual_next_height = *block.header().height();

        // Importer expects that `UncommittedResult` contains the result of block
        // execution without block itself.
        let expected_block_root = self.database.latest_block_root()?;

        let db_after_execution = self.database.storage_transaction(changes);
        let actual_block_root = db_after_execution.latest_block_root()?;

        if actual_block_root != expected_block_root {
            return Err(Error::InvalidDatabaseStateAfterExecution(
                expected_block_root,
                actual_block_root,
            ))
        }

        if result.source == Source::Local {
            self.block_reconciliation_write_port
                .publish_produced_block(&result.sealed_block)
                .map_err(Error::FailedBlockReconciliationWrite)?;
        }

        let changes = db_after_execution.into_changes();

        #[cfg(feature = "test-helpers")]
        let changes_clone = changes.clone();

        self.database
            .commit_changes(StorageChanges::ChangesList(vec![block_changes, changes]))?;

        if self.metrics {
            Self::update_metrics(&result, &actual_next_height);
        }
        tracing::info!("Committed block {:#x}", result.sealed_block.entity.id());

        let result = ImporterResult {
            shared_result: Arc::new(Awaiter::new(result, permit)),
            #[cfg(feature = "test-helpers")]
            changes: Arc::new(changes_clone),
        };
        let _ = self.broadcast.send(result);

        Ok(())
    }

    /// Should only be called once after startup to set importer metrics to their initial values
    pub fn init_metrics(&self) {
        // load starting values from database

        // Errors are optimistically handled via fallback to default values since the metrics
        // should get updated regularly anyways and these errors will be discovered and handled
        // correctly in more mission critical areas (such as _commit_result)
        let current_block_height = self
            .database
            .latest_block_height()
            .unwrap_or_default()
            .unwrap_or_default();
        importer_metrics()
            .block_height
            .set(*current_block_height.deref() as i64);
        // on init just set to current time since it's not worth tracking in the db
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs_f64();
        importer_metrics()
            .latest_block_import_timestamp
            .set(current_time);
    }

    fn update_metrics(result: &ImportResult, actual_next_height: &BlockHeight) {
        let (total_gas_used, total_fee): (u64, u64) = result
            .tx_status
            .iter()
            .map(|tx_result| {
                (*tx_result.result.total_gas(), *tx_result.result.total_fee())
            })
            .fold((0_u64, 0_u64), |(acc_gas, acc_fee), (used_gas, fee)| {
                (
                    acc_gas.saturating_add(used_gas),
                    acc_fee.saturating_add(fee),
                )
            });
        let maybe_last_tx = result.sealed_block.entity.transactions().last();
        if let Some(last_tx) = maybe_last_tx {
            if let Transaction::Mint(mint) = last_tx {
                importer_metrics()
                    .gas_price
                    .set((*mint.gas_price()).try_into().unwrap_or(i64::MAX));
            } else {
                warn!("Last transaction is not a mint transaction");
            }
        }

        let total_transactions = result.tx_status.len();
        importer_metrics()
            .block_height
            .set(*actual_next_height.deref() as i64);
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs_f64();
        importer_metrics()
            .latest_block_import_timestamp
            .set(current_time);
        importer_metrics()
            .gas_per_block
            .set(total_gas_used.try_into().unwrap_or(i64::MAX));
        importer_metrics()
            .fee_per_block
            .set(total_fee.try_into().unwrap_or(i64::MAX));
        importer_metrics()
            .transactions_per_block
            .set(total_transactions.try_into().unwrap_or(i64::MAX));
    }
}

struct VerifyAndExecutionResult {
    tx_status: Vec<TransactionExecutionStatus>,
    events: Vec<Event>,
    changes: Changes,
}

struct PrepareImportResult {
    result: UncommittedResult<Changes>,
    block_changes: Changes,
}

impl<IDatabase, E, V, W> ImporterInner<IDatabase, E, V, W>
where
    IDatabase: ImporterDatabase + Transactional,
    E: Validator,
    V: BlockVerifier,
    W: BlockReconciliationWritePort,
{
    async fn run(&mut self) {
        let local_runner = LocalRunner::new().expect("Failed to create the local runner");
        while let Some(command) = self.commands.recv().await {
            match command {
                Commands::Stop => break,
                Commands::CommitResult {
                    result,
                    permit,
                    callback,
                } => {
                    let result = self.commit_result(&local_runner, permit, result);
                    let _ = callback.send(result);
                }
                #[cfg(test)]
                Commands::VerifyAndExecuteBlock {
                    sealed_block,
                    callback,
                } => {
                    let result =
                        self.verify_and_execute_block(&local_runner, sealed_block);
                    let _ = callback.send(result);
                }
                Commands::PrepareImportResult {
                    sealed_block,
                    callback,
                } => {
                    let result = self.prepare_import_result(&local_runner, sealed_block);
                    let _ = callback.send(result);
                }
            }
        }
    }

    /// Prepares the block for committing. It includes the execution of the block,
    /// the validation of the block fields, and preparing the changes
    /// to the database with the block and transactions.
    pub fn prepare_import_result(
        &self,
        runner: &LocalRunner,
        sealed_block: SealedBlock,
    ) -> Result<PrepareImportResult, Error> {
        let sealed_block_ref = &sealed_block;

        let block_changes =
            || create_block_changes(&self.chain_id, sealed_block_ref, &self.database);

        let execution = || {
            Self::verify_and_execute_block_inner(
                &self.executor,
                &self.verifier,
                sealed_block_ref,
            )
        };

        let (block_changes_result, execution_result) =
            runner.run_in_parallel(block_changes, execution);

        let result = execution_result?;
        let block_changes = block_changes_result?;

        let VerifyAndExecutionResult {
            tx_status,
            events,
            changes,
        } = result;
        let import_result =
            ImportResult::new_from_network(sealed_block, tx_status, events);

        let result = UncommittedResult::new(import_result, changes);

        Ok(PrepareImportResult {
            result,
            block_changes,
        })
    }

    /// Performs all checks required to commit the block, it includes the execution of
    /// the block(As a result returns the uncommitted state).
    ///
    /// It validates only the `Block` execution rules and the `Block` fields' validity.
    /// The validity of the `SealedBlock` and seal information is not the concern of this function.
    ///
    /// The method doesn't require synchronous access, so it could be called in a
    /// concurrent environment.
    ///
    /// Returns `Err` if the block is invalid for committing. Otherwise, it returns the
    /// `Ok` with the uncommitted state.
    #[cfg(test)]
    pub fn verify_and_execute_block(
        &self,
        runner: &LocalRunner,
        sealed_block: SealedBlock,
    ) -> Result<UncommittedResult<Changes>, Error> {
        runner.run(move || {
            let result = Self::verify_and_execute_block_inner(
                &self.executor,
                &self.verifier,
                &sealed_block,
            );

            let VerifyAndExecutionResult {
                tx_status,
                events,
                changes,
            } = result?;

            let import_result =
                ImportResult::new_from_network(sealed_block, tx_status, events);

            Ok(UncommittedResult::new(import_result, changes))
        })
    }

    fn verify_and_execute_block_inner(
        executor: &E,
        verifier: &V,
        sealed_block: &SealedBlock,
    ) -> Result<VerifyAndExecutionResult, Error> {
        let consensus = &sealed_block.consensus;
        let block = &sealed_block.entity;
        let sealed_block_id = block.id();

        let result_of_verification = verifier.verify_block_fields(consensus, block);
        if let Err(err) = result_of_verification {
            return Err(Error::FailedVerification(err))
        }

        // The current code has a separate function X to process `StateConfig`.
        // It is not possible to execute it via `Executor`.
        // Maybe we need consider to move the function X here, if that possible.
        if let Consensus::Genesis(_) = consensus {
            return Err(Error::ExecuteGenesis)
        }

        let (ValidationResult { tx_status, events }, changes) = executor
            .validate(block)
            .map_err(Error::FailedExecution)?
            .into();

        let actual_block_id = block.id();
        if actual_block_id != sealed_block_id {
            // It should not be possible because, during validation, we don't touch the block.
            // But while we pass it by value, let's check it.
            return Err(Error::BlockIdMismatch(sealed_block_id, actual_block_id))
        }

        let result = VerifyAndExecutionResult {
            tx_status,
            events,
            changes,
        };

        Ok(result)
    }
}

impl Importer {
    /// The method validates the `Block` fields and commits the `SealedBlock`.
    pub async fn execute_and_commit(
        &self,
        sealed_block: SealedBlock,
    ) -> Result<(), Error> {
        let _guard = self.lock()?;

        let start = Instant::now();
        let result = self.run_prepare_import_result(sealed_block).await?;
        let execute_time = start.elapsed().as_secs_f64();

        // Await until all receivers of the notification process the result.
        const TIMEOUT: u64 = 20;
        let await_result = tokio::time::timeout(
            Duration::from_secs(TIMEOUT),
            self.active_import_results.clone().acquire_owned(),
        )
        .await;

        let Ok(permit) = await_result else {
            tracing::error!(
                "The previous block processing \
                     was not finished for {TIMEOUT} seconds."
            );
            return Err(Error::PreviousBlockProcessingNotFinished)
        };
        let permit = permit.map_err(Error::ActiveBlockResultsSemaphoreClosed)?;

        let start = Instant::now();
        let commit_result = self
            .run_commit_result(permit, CommitInput::PrepareImportResult(result))
            .await;

        let commit_time = start.elapsed().as_secs_f64();
        let time = execute_time + commit_time;

        importer_metrics().execute_and_commit_duration.observe(time);

        commit_result
    }
}

/// The wrapper around `ImportResult` to notify about the end of the processing of a new block.
struct Awaiter {
    result: ImportResult,
    _permit: OwnedSemaphorePermit,
}

impl Deref for Awaiter {
    type Target = ImportResult;

    fn deref(&self) -> &Self::Target {
        &self.result
    }
}

impl Awaiter {
    fn new(result: ImportResult, permit: OwnedSemaphorePermit) -> Self {
        Self {
            result,
            _permit: permit,
        }
    }
}

fn create_block_changes<D: ImporterDatabase + Transactional>(
    chain_id: &ChainId,
    sealed_block: &SealedBlock,
    database: &D,
) -> Result<Changes, Error> {
    let consensus = &sealed_block.consensus;
    let actual_next_height = *sealed_block.entity.header().height();

    // During importing of the genesis block, the database should not be initialized
    // and the genesis block defines the next height.
    // During the production of the non-genesis block, the next height should be underlying
    // database height + 1.
    let expected_next_height = match consensus {
        Consensus::Genesis(_) => {
            let result = database.latest_block_height()?;
            let found = result.is_some();
            // Because the genesis block is not committed, it should return `None`.
            // If we find the latest height, something is wrong with the state of the database.
            if found {
                return Err(Error::InvalidUnderlyingDatabaseGenesisState)
            }
            actual_next_height
        }
        Consensus::PoA(_) => {
            if actual_next_height == BlockHeight::from(0u32) {
                return Err(Error::ZeroNonGenericHeight)
            }

            let last_db_height = database
                .latest_block_height()?
                .ok_or(not_found!("Latest block height"))?;
            last_db_height
                .checked_add(1u32)
                .ok_or(Error::Overflow)?
                .into()
        }
        _ => {
            return Err(Error::UnsupportedConsensusVariant(format!(
                "{:?}",
                consensus
            )))
        }
    };

    if expected_next_height != actual_next_height {
        return Err(Error::IncorrectBlockHeight(
            expected_next_height,
            actual_next_height,
        ))
    }

    let mut transaction = database.storage_transaction(Changes::new());

    if !transaction.store_new_block(chain_id, sealed_block)? {
        return Err(Error::NotUnique(actual_next_height))
    }

    Ok(transaction.into_changes())
}