fuel_core/service/
genesis.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
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
use self::importer::SnapshotImporter;
use crate::{
    combined_database::{
        CombinedDatabase,
        CombinedGenesisDatabase,
    },
    database::{
        database_description::{
            off_chain::OffChain,
            on_chain::OnChain,
        },
        genesis_progress::GenesisMetadata,
        Database,
    },
    service::config::Config,
};
use fuel_core_chain_config::GenesisCommitment;
use fuel_core_services::StateWatcher;
use fuel_core_storage::{
    iter::IteratorOverTable,
    not_found,
    tables::{
        ConsensusParametersVersions,
        StateTransitionBytecodeVersions,
        UploadedBytecodes,
    },
    transactional::{
        AtomicView,
        Changes,
        IntoTransaction,
        ReadTransaction,
    },
    StorageAsMut,
};
use fuel_core_types::{
    self,
    blockchain::{
        block::Block,
        consensus::{
            Consensus,
            Genesis,
        },
        header::{
            ApplicationHeader,
            ConsensusHeader,
            ConsensusParametersVersion,
            PartialBlockHeader,
            StateTransitionBytecodeVersion,
        },
        primitives::Empty,
        SealedBlock,
    },
    fuel_crypto::Hasher,
    fuel_types::Bytes32,
    fuel_vm::UploadedBytecode,
    services::block_importer::{
        ImportResult,
        UncommittedResult as UncommittedImportResult,
    },
};
use itertools::Itertools;

pub use exporter::Exporter;
pub use task_manager::NotifyCancel;

mod exporter;
mod importer;
mod progress;
mod task_manager;

/// Performs the importing of the genesis block from the snapshot.
pub async fn execute_genesis_block(
    watcher: StateWatcher,
    config: &Config,
    db: &CombinedDatabase,
) -> anyhow::Result<UncommittedImportResult<Changes>> {
    let genesis_block = create_genesis_block(config);
    tracing::info!("Genesis block created: {:?}", genesis_block.header());
    let on_chain = db
        .on_chain()
        .clone()
        .into_genesis()
        .map_err(|_| anyhow::anyhow!("On chain database is already initialized"))?;
    let off_chain = db
        .off_chain()
        .clone()
        .into_genesis()
        .map_err(|_| anyhow::anyhow!("Off chain database is already initialized"))?;

    let genesis_db = CombinedGenesisDatabase {
        on_chain,
        off_chain,
    };

    SnapshotImporter::import(
        genesis_db.clone(),
        genesis_block.clone(),
        config.snapshot_reader.clone(),
        watcher,
    )
    .await?;

    let genesis_progress_on_chain: Vec<String> = db
        .on_chain()
        .iter_all_keys::<GenesisMetadata<OnChain>>(None)
        .try_collect()?;
    let genesis_progress_off_chain: Vec<String> = db
        .off_chain()
        .iter_all_keys::<GenesisMetadata<OffChain>>(None)
        .try_collect()?;

    let chain_config = config.snapshot_reader.chain_config();
    let genesis = Genesis {
        chain_config_hash: chain_config.root()?.into(),
        coins_root: genesis_db.on_chain().genesis_coins_root()?.into(),
        messages_root: genesis_db.on_chain().genesis_messages_root()?.into(),
        contracts_root: genesis_db.on_chain().genesis_contracts_root()?.into(),
        transactions_root: genesis_db.on_chain().processed_transactions_root()?.into(),
    };

    let consensus = Consensus::Genesis(genesis);
    let block = SealedBlock {
        entity: genesis_block.clone(),
        consensus,
    };

    let mut database_transaction_off_chain = db.off_chain().clone().into_transaction();
    for key in genesis_progress_off_chain {
        database_transaction_off_chain
            .storage_as_mut::<GenesisMetadata<OffChain>>()
            .remove(&key)?;
    }
    database_transaction_off_chain.commit()?;

    let mut database_transaction_on_chain = db.on_chain().read_transaction();
    database_transaction_on_chain
        .storage_as_mut::<ConsensusParametersVersions>()
        .insert(
            &genesis_block.header().consensus_parameters_version,
            &chain_config.consensus_parameters,
        )?;

    let bytecode_root = Hasher::hash(chain_config.state_transition_bytecode.as_slice());
    database_transaction_on_chain
        .storage_as_mut::<StateTransitionBytecodeVersions>()
        .insert(
            &genesis_block.header().state_transition_bytecode_version,
            &bytecode_root,
        )?;
    database_transaction_on_chain
        .storage_as_mut::<UploadedBytecodes>()
        .insert(
            &bytecode_root,
            &UploadedBytecode::Completed(chain_config.state_transition_bytecode.clone()),
        )?;

    // Needs to be given the progress because `iter_all` is not implemented on db transactions.
    for key in genesis_progress_on_chain {
        database_transaction_on_chain
            .storage_as_mut::<GenesisMetadata<OnChain>>()
            .remove(&key)?;
    }

    let result = UncommittedImportResult::new(
        ImportResult::new_from_local(block, vec![], vec![]),
        database_transaction_on_chain.into_changes(),
    );

    Ok(result)
}

pub async fn recover_missing_tables_from_genesis_state_config(
    watcher: StateWatcher,
    config: &Config,
    db: &CombinedDatabase,
) -> anyhow::Result<()> {
    // TODO: The code below only modifies the contract of the genesis block of the off chain database.
    //  It is safe to initialize some missing data, for now, but it can change in the future.
    //  We plan to remove this code later, see: https://github.com/FuelLabs/fuel-core/issues/2326
    let Err(off_chain) = db.off_chain().clone().into_genesis() else {
        return Ok(())
    };

    let genesis_db = CombinedGenesisDatabase {
        on_chain: Database::default(),
        off_chain,
    };
    let genesis_block = db
        .on_chain()
        .latest_view()?
        .genesis_block()?
        .ok_or(not_found!("Genesis block"))?;
    let genesis_block = genesis_block.uncompress(vec![]);

    SnapshotImporter::repopulate_maybe_missing_tables(
        genesis_db,
        genesis_block,
        config.snapshot_reader.clone(),
        watcher,
    )
    .await
}

#[cfg(feature = "test-helpers")]
pub async fn execute_and_commit_genesis_block(
    config: &Config,
    db: &CombinedDatabase,
) -> anyhow::Result<()> {
    let result = execute_genesis_block(StateWatcher::default(), config, db).await?;
    let importer = fuel_core_importer::Importer::new(
        config
            .snapshot_reader
            .chain_config()
            .consensus_parameters
            .chain_id(),
        config.block_importer.clone(),
        db.on_chain().clone(),
        (),
        (),
    );
    importer.commit_result(result).await?;
    Ok(())
}

pub fn create_genesis_block(config: &Config) -> Block {
    let height;
    let da_height;
    let consensus_parameters_version;
    let state_transition_bytecode_version;
    let prev_root;

    // If the rollup continues the old rollup, the height of the new block should
    // be higher than that of the old chain by one to make it continuous.
    // The same applies to the state transition functions and consensus
    // parameters since it is a new chain.
    if let Some(latest_block) = config.snapshot_reader.last_block_config() {
        height = latest_block
            .block_height
            .succ()
            .expect("Block height overflow");
        consensus_parameters_version = latest_block
            .consensus_parameters_version
            .checked_add(1)
            .expect("Consensus parameters version overflow");
        state_transition_bytecode_version = latest_block
            .state_transition_version
            .checked_add(1)
            .expect("State transition bytecode version overflow");
        da_height = latest_block.da_block_height;
        prev_root = latest_block.blocks_root;
    } else {
        height = 0u32.into();
        #[cfg(feature = "relayer")]
        {
            da_height = config
                .relayer
                .as_ref()
                .map(|r| r.da_deploy_height)
                .unwrap_or_default();
        }
        #[cfg(not(feature = "relayer"))]
        {
            da_height = 0u64.into();
        }
        consensus_parameters_version = ConsensusParametersVersion::MIN;
        state_transition_bytecode_version = config
            .snapshot_reader
            .chain_config()
            .genesis_state_transition_version
            .unwrap_or(StateTransitionBytecodeVersion::MIN);
        prev_root = Bytes32::zeroed();
    }

    let transactions_ids = vec![];
    let message_ids = &[];
    let events = Default::default();
    Block::new(
        PartialBlockHeader {
            application: ApplicationHeader::<Empty> {
                da_height,
                consensus_parameters_version,
                state_transition_bytecode_version,
                generated: Empty,
            },
            consensus: ConsensusHeader::<Empty> {
                prev_root,
                height,
                time: fuel_core_types::tai64::Tai64::UNIX_EPOCH,
                generated: Empty,
            },
        },
        transactions_ids,
        message_ids,
        events,
    )
    .expect("The block is valid; qed")
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::{
        combined_database::CombinedDatabase,
        database::Database,
        service::{
            config::Config,
            FuelService,
        },
        ShutdownListener,
    };
    use fuel_core_chain_config::{
        BlobConfig,
        CoinConfig,
        ContractConfig,
        LastBlockConfig,
        MessageConfig,
        Randomize,
        StateConfig,
    };
    use fuel_core_producer::ports::BlockProducerDatabase;
    use fuel_core_storage::{
        tables::{
            Coins,
            ContractsAssets,
            ContractsState,
        },
        transactional::{
            AtomicView,
            HistoricalView,
        },
        StorageAsRef,
    };
    use fuel_core_types::{
        blockchain::primitives::DaBlockHeight,
        entities::coins::coin::Coin,
        fuel_tx::UtxoId,
        fuel_types::{
            Address,
            AssetId,
            BlockHeight,
        },
    };
    use itertools::Itertools;
    use rand::{
        rngs::StdRng,
        Rng,
        RngCore,
        SeedableRng,
    };
    use std::vec;

    #[tokio::test]
    async fn config_initializes_block_height_of_genesis_block() {
        let block_height = BlockHeight::from(99u32);
        let service_config = Config::local_node_with_state_config(StateConfig {
            last_block: Some(LastBlockConfig {
                block_height,
                state_transition_version: 0,
                ..Default::default()
            }),
            ..Default::default()
        });

        let db = Database::default();
        FuelService::from_database(db.clone(), service_config)
            .await
            .unwrap();

        // The genesis block has next block height after the latest block of the previous network.
        let genesis_block_height = block_height.succ().unwrap();
        assert_eq!(
            genesis_block_height,
            db.latest_height()
                .expect("Expected a block height to be set")
        )
    }

    #[tokio::test]
    async fn genesis_columns_are_cleared_after_import() {
        let mut rng = StdRng::seed_from_u64(10);

        let coins = std::iter::repeat_with(|| CoinConfig {
            tx_pointer_block_height: 0.into(),
            ..Randomize::randomize(&mut rng)
        })
        .take(1000)
        .collect_vec();

        let messages = std::iter::repeat_with(|| MessageConfig {
            da_height: DaBlockHeight(0),
            ..MessageConfig::randomize(&mut rng)
        })
        .take(1000)
        .collect_vec();

        let blobs = std::iter::repeat_with(|| BlobConfig::randomize(&mut rng))
            .take(1000)
            .collect_vec();

        let contracts = std::iter::repeat_with(|| given_contract_config(&mut rng))
            .take(1000)
            .collect_vec();

        let state = StateConfig {
            coins,
            messages,
            blobs,
            contracts,
            last_block: Some(LastBlockConfig {
                block_height: BlockHeight::from(0u32),
                state_transition_version: 0,
                ..Default::default()
            }),
        };

        let service_config = Config::local_node_with_state_config(state);

        let db = Database::default();
        FuelService::from_database(db.clone(), service_config)
            .await
            .unwrap();

        let keys = db.iter_all::<GenesisMetadata<OnChain>>(None).count();
        assert_eq!(keys, 0);
    }

    #[tokio::test]
    async fn config_state_initializes_multiple_coins_with_different_owners_and_asset_ids()
    {
        let mut rng = StdRng::seed_from_u64(10);

        // a coin with all options set
        let alice: Address = rng.gen();
        let asset_id_alice: AssetId = rng.gen();
        let alice_value = rng.gen();
        let alice_block_created: BlockHeight = rng.next_u32().into();
        let alice_block_created_tx_idx = rng.gen();
        let alice_tx_id = rng.gen();
        let alice_output_index = rng.gen();
        let alice_utxo_id = UtxoId::new(alice_tx_id, alice_output_index);

        // a coin with minimal options set
        let bob: Address = rng.gen();
        let asset_id_bob: AssetId = rng.gen();
        let bob_value = rng.gen();

        let starting_height = {
            let mut h: u32 = alice_block_created.into();
            h = h.saturating_add(rng.next_u32());
            h.into()
        };
        let state = StateConfig {
            coins: vec![
                CoinConfig {
                    tx_id: alice_tx_id,
                    output_index: alice_output_index,
                    tx_pointer_block_height: alice_block_created,
                    tx_pointer_tx_idx: alice_block_created_tx_idx,
                    owner: alice,
                    amount: alice_value,
                    asset_id: asset_id_alice,
                },
                CoinConfig {
                    owner: bob,
                    amount: bob_value,
                    asset_id: asset_id_bob,
                    ..Default::default()
                },
            ],
            last_block: Some(LastBlockConfig {
                block_height: starting_height,
                state_transition_version: 0,
                ..Default::default()
            }),
            ..Default::default()
        };

        let service_config = Config::local_node_with_state_config(state);

        let db = CombinedDatabase::default();
        FuelService::from_combined_database(db.clone(), service_config)
            .await
            .unwrap();

        let alice_coins = get_coins(&db, &alice);
        let bob_coins = get_coins(&db, &bob);

        assert!(matches!(
            alice_coins.as_slice(),
            &[Coin {
                utxo_id,
                owner,
                amount,
                asset_id,
                tx_pointer,
                ..
            }] if utxo_id == alice_utxo_id
            && owner == alice
            && amount == alice_value
            && asset_id == asset_id_alice
            && tx_pointer.block_height() == alice_block_created
        ));
        assert!(matches!(
            bob_coins.as_slice(),
            &[Coin {
                owner,
                amount,
                asset_id,
                ..
            }] if owner == bob
            && amount == bob_value
            && asset_id == asset_id_bob
        ));
    }

    #[tokio::test]
    async fn config_state_initializes_contract_state() {
        // given
        let mut rng = StdRng::seed_from_u64(10);

        let contract = given_contract_config(&mut rng);
        let contract_id = contract.contract_id;
        let states = contract.states.clone();

        let service_config = Config::local_node_with_state_config(StateConfig {
            contracts: vec![contract],
            ..Default::default()
        });
        let db = Database::default();

        // when
        FuelService::from_database(db.clone(), service_config)
            .await
            .unwrap();

        // then
        for state in states {
            let ret = db
                .storage::<ContractsState>()
                .get(&(&contract_id, &state.key).into())
                .unwrap()
                .expect("Expect a state entry to exist")
                .into_owned();
            assert_eq!(state.value, ret.0)
        }
    }

    #[cfg(feature = "test-helpers")]
    #[tokio::test]
    async fn tests_init_da_msgs() {
        use fuel_core_storage::tables::Messages;

        let mut rng = StdRng::seed_from_u64(32492);

        let msg = MessageConfig {
            sender: rng.gen(),
            recipient: rng.gen(),
            nonce: rng.gen(),
            amount: rng.gen(),
            data: vec![rng.gen()],
            da_height: DaBlockHeight(0),
        };

        let state = StateConfig {
            messages: vec![msg.clone()],
            ..Default::default()
        };
        let config = Config::local_node_with_state_config(state);

        let db = CombinedDatabase::default();

        super::execute_and_commit_genesis_block(&config, &db)
            .await
            .unwrap();

        let expected_msg: fuel_core_types::entities::Message = msg.into();

        let ret_msg = db
            .on_chain()
            .storage::<Messages>()
            .get(expected_msg.id())
            .unwrap()
            .unwrap()
            .into_owned();

        assert_eq!(expected_msg, ret_msg);
    }

    #[tokio::test]
    async fn config_state_initializes_contract_balance() {
        let mut rng = StdRng::seed_from_u64(10);

        let contract = given_contract_config(&mut rng);
        let contract_id = contract.contract_id;
        let balances = contract.balances.clone();
        let service_config = Config::local_node_with_state_config(StateConfig {
            contracts: vec![contract],
            ..Default::default()
        });

        let db = Database::default();
        FuelService::from_database(db.clone(), service_config)
            .await
            .unwrap();

        for balance in balances {
            let ret = db
                .storage::<ContractsAssets>()
                .get(&(&contract_id, &balance.asset_id).into())
                .unwrap()
                .expect("Expected a balance to be present")
                .into_owned();

            assert_eq!(balance.amount, ret)
        }
    }

    #[tokio::test]
    async fn coin_tx_pointer_cant_exceed_genesis_height() {
        let state = StateConfig {
            coins: vec![CoinConfig {
                // set txpointer height > genesis height
                tx_pointer_block_height: BlockHeight::from(11u32),
                amount: 10,
                ..Default::default()
            }],
            last_block: Some(LastBlockConfig {
                block_height: BlockHeight::from(9u32),
                state_transition_version: 0,
                ..Default::default()
            }),
            ..Default::default()
        };
        let service_config = Config::local_node_with_state_config(state);

        let db = CombinedDatabase::default();
        let mut shutdown = ShutdownListener::spawn();
        let task = FuelService::new(db, service_config, &mut shutdown).unwrap();
        let init_result = task.start_and_await().await;

        assert!(init_result.is_err())
    }

    #[tokio::test]
    async fn contract_tx_pointer_cant_exceed_genesis_height() {
        let mut rng = StdRng::seed_from_u64(10);

        let state = StateConfig {
            contracts: vec![ContractConfig {
                // set txpointer height > genesis height
                tx_pointer_block_height: BlockHeight::from(11u32),
                ..given_contract_config(&mut rng)
            }],
            last_block: Some(LastBlockConfig {
                block_height: BlockHeight::from(9u32),
                state_transition_version: 0,
                ..Default::default()
            }),
            ..Default::default()
        };
        let service_config = Config::local_node_with_state_config(state);

        let db = CombinedDatabase::default();
        let mut shutdown = ShutdownListener::spawn();
        let task = FuelService::new(db, service_config, &mut shutdown).unwrap();
        let init_result = task.start_and_await().await;

        assert!(init_result.is_err())
    }

    fn get_coins(db: &CombinedDatabase, owner: &Address) -> Vec<Coin> {
        db.off_chain()
            .latest_view()
            .unwrap()
            .owned_coins_ids(owner, None, None)
            .map(|r| {
                let coin_id = r.unwrap();
                db.on_chain()
                    .storage::<Coins>()
                    .get(&coin_id)
                    .map(|v| v.unwrap().into_owned().uncompress(coin_id))
                    .unwrap()
            })
            .collect()
    }

    fn given_contract_config(rng: &mut StdRng) -> ContractConfig {
        ContractConfig {
            tx_pointer_block_height: 0.into(),
            ..ContractConfig::randomize(rng)
        }
    }

    #[tokio::test]
    async fn config_state_initializes_contract() {
        let mut rng = StdRng::seed_from_u64(10);

        let initial_state = StateConfig {
            contracts: vec![given_contract_config(&mut rng)],
            ..Default::default()
        };
        let service_config = Config::local_node_with_state_config(initial_state.clone());

        let db = CombinedDatabase::default();
        FuelService::from_combined_database(db.clone(), service_config)
            .await
            .unwrap();

        let actual_state = db.read_state_config().unwrap();
        let mut expected_state = initial_state;
        let mut last_block = LastBlockConfig::default();
        let view = db.on_chain().latest_view().unwrap();
        last_block.block_height = view.latest_height().unwrap();
        last_block.state_transition_version = view
            .latest_block()
            .unwrap()
            .header()
            .state_transition_bytecode_version;
        last_block.blocks_root = view
            .block_header_merkle_root(&last_block.block_height)
            .unwrap();
        expected_state.last_block = Some(last_block);
        assert_eq!(expected_state, actual_state);
    }
}