|
| 1 | +use crate::db_lookup_times_utils::{ |
| 2 | + matrix::matrix, |
| 3 | + utils::open_raw_rocksdb, |
| 4 | +}; |
| 5 | +use fuel_core::{ |
| 6 | + database::database_description::on_chain::OnChain, |
| 7 | + state::rocks_db::RocksDb, |
| 8 | +}; |
| 9 | +use fuel_core_storage::{ |
| 10 | + column::Column, |
| 11 | + kv_store::{ |
| 12 | + KeyValueMutate, |
| 13 | + Value, |
| 14 | + }, |
| 15 | +}; |
| 16 | +use fuel_core_types::{ |
| 17 | + blockchain::{ |
| 18 | + block::{ |
| 19 | + Block, |
| 20 | + PartialFuelBlock, |
| 21 | + }, |
| 22 | + header::{ |
| 23 | + ConsensusHeader, |
| 24 | + PartialBlockHeader, |
| 25 | + }, |
| 26 | + primitives::Empty, |
| 27 | + }, |
| 28 | + fuel_tx::{ |
| 29 | + Transaction, |
| 30 | + UniqueIdentifier, |
| 31 | + }, |
| 32 | + fuel_types::{ |
| 33 | + BlockHeight, |
| 34 | + ChainId, |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +fn generate_bench_block(height: u32, tx_count: u32) -> Block { |
| 39 | + let header = PartialBlockHeader { |
| 40 | + application: Default::default(), |
| 41 | + consensus: ConsensusHeader::<Empty> { |
| 42 | + height: BlockHeight::from(height), |
| 43 | + ..Default::default() |
| 44 | + }, |
| 45 | + }; |
| 46 | + let txes = generate_bench_transactions(tx_count); |
| 47 | + let block = PartialFuelBlock::new(header, txes); |
| 48 | + block.generate(&[], Default::default()).unwrap() |
| 49 | +} |
| 50 | + |
| 51 | +fn generate_bench_transactions(tx_count: u32) -> Vec<Transaction> { |
| 52 | + let mut txes = vec![]; |
| 53 | + for _ in 0..tx_count { |
| 54 | + txes.push(Transaction::default_test_tx()); |
| 55 | + } |
| 56 | + txes |
| 57 | +} |
| 58 | + |
| 59 | +fn height_key(block_height: u32) -> Vec<u8> { |
| 60 | + BlockHeight::from(block_height).to_bytes().to_vec() |
| 61 | +} |
| 62 | + |
| 63 | +fn insert_compressed_block( |
| 64 | + database: &mut RocksDb<OnChain>, |
| 65 | + height: u32, |
| 66 | + tx_count: u32, |
| 67 | +) -> Block { |
| 68 | + let block = generate_bench_block(height, tx_count); |
| 69 | + |
| 70 | + let compressed_block = block.compress(&ChainId::default()); |
| 71 | + let height_key = height_key(height); |
| 72 | + |
| 73 | + let raw_compressed_block = postcard::to_allocvec(&compressed_block).unwrap().to_vec(); |
| 74 | + let raw_transactions = block |
| 75 | + .transactions() |
| 76 | + .into_iter() |
| 77 | + .map(|tx| { |
| 78 | + ( |
| 79 | + tx.id(&ChainId::default()), |
| 80 | + postcard::to_allocvec(tx).unwrap().to_vec(), |
| 81 | + ) |
| 82 | + }) |
| 83 | + .collect::<Vec<_>>(); |
| 84 | + |
| 85 | + // 1. insert into CompressedBlocks table |
| 86 | + database |
| 87 | + .put( |
| 88 | + height_key.as_slice(), |
| 89 | + Column::FuelBlocks, |
| 90 | + Value::new(raw_compressed_block), |
| 91 | + ) |
| 92 | + .unwrap(); |
| 93 | + // 2. insert into Transactions table |
| 94 | + for (tx_id, tx) in raw_transactions { |
| 95 | + database |
| 96 | + .put(tx_id.as_slice(), Column::Transactions, Value::new(tx)) |
| 97 | + .unwrap(); |
| 98 | + } |
| 99 | + |
| 100 | + block |
| 101 | +} |
| 102 | + |
| 103 | +fn insert_full_block(database: &mut RocksDb<OnChain>, height: u32, tx_count: u32) { |
| 104 | + let block = match insert_compressed_block(database, height, tx_count) { |
| 105 | + Block::V1(b) => b, |
| 106 | + }; |
| 107 | + |
| 108 | + let height_key = height_key(height); |
| 109 | + let raw_full_block = postcard::to_allocvec(&block).unwrap().to_vec(); |
| 110 | + |
| 111 | + // 3. insert into FullFuelBlocks table |
| 112 | + database |
| 113 | + .put( |
| 114 | + height_key.as_slice(), |
| 115 | + Column::FullFuelBlocks, |
| 116 | + Value::new(raw_full_block), |
| 117 | + ) |
| 118 | + .unwrap(); |
| 119 | +} |
| 120 | + |
| 121 | +fn seed_compressed_blocks_and_transactions( |
| 122 | + database: &mut RocksDb<OnChain>, |
| 123 | + block_count: u32, |
| 124 | + tx_count: u32, |
| 125 | +) -> Vec<Block> { |
| 126 | + let mut blocks = vec![]; |
| 127 | + for block_number in 0..block_count { |
| 128 | + blocks.push(insert_compressed_block(database, block_number, tx_count)); |
| 129 | + } |
| 130 | + blocks |
| 131 | +} |
| 132 | + |
| 133 | +pub fn seed_compressed_blocks_and_transactions_matrix(method: &str) { |
| 134 | + for (block_count, tx_count) in matrix() { |
| 135 | + let mut database = open_raw_rocksdb(block_count, tx_count, method); |
| 136 | + let _ = |
| 137 | + seed_compressed_blocks_and_transactions(&mut database, block_count, tx_count); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn seed_full_blocks(database: &mut RocksDb<OnChain>, block_count: u32, tx_count: u32) { |
| 142 | + // we seed compressed blocks and transactions to not affect individual |
| 143 | + // lookup times |
| 144 | + |
| 145 | + for block_number in 0..block_count { |
| 146 | + insert_full_block(database, block_number, tx_count); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +pub fn seed_full_block_matrix() { |
| 151 | + for (block_count, tx_count) in matrix() { |
| 152 | + let mut database = open_raw_rocksdb(block_count, tx_count, "full_block"); |
| 153 | + seed_full_blocks(&mut database, block_count, tx_count); |
| 154 | + } |
| 155 | +} |
0 commit comments