pos_ledger_db/schema/
mod.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8//! This module defines representation of Diem core data structures at physical
9//! level via schemas that implement [`schemadb::schema::Schema`].
10//!
11//! All schemas are `pub(crate)` so not shown in rustdoc, refer to the source
12//! code to see details.
13
14use anyhow::{ensure, Result};
15
16use schemadb::ColumnFamilyName;
17
18pub(crate) mod block_by_epoch_and_round;
19pub(crate) mod committed_block;
20pub(crate) mod committed_block_by_view;
21pub(crate) mod epoch_by_version;
22pub(crate) mod event;
23pub(crate) mod event_accumulator;
24pub(crate) mod event_by_key;
25pub(crate) mod event_by_version;
26pub(crate) mod ledger_info;
27pub(crate) mod ledger_info_by_block;
28pub(crate) mod ledger_info_by_voted_block;
29pub(crate) mod pos_state;
30pub(crate) mod reward_event;
31pub(crate) mod transaction;
32pub(crate) mod transaction_accumulator;
33pub(crate) mod transaction_by_account;
34pub(crate) mod transaction_info;
35
36pub const EPOCH_BY_VERSION_CF_NAME: ColumnFamilyName = "epoch_by_version";
37pub const EVENT_ACCUMULATOR_CF_NAME: ColumnFamilyName = "event_accumulator";
38pub const EVENT_BY_KEY_CF_NAME: ColumnFamilyName = "event_by_key";
39pub const EVENT_BY_VERSION_CF_NAME: ColumnFamilyName = "event_by_version";
40pub const EVENT_CF_NAME: ColumnFamilyName = "event";
41// These CF names are kept for backward compatibility with existing RocksDB
42// instances. The column families still need to be listed to open the DB,
43// even though no code reads/writes them anymore.
44pub const JELLYFISH_MERKLE_NODE_CF_NAME: ColumnFamilyName =
45    "jellyfish_merkle_node";
46pub const LEDGER_COUNTERS_CF_NAME: ColumnFamilyName = "ledger_counters";
47pub const STALE_NODE_INDEX_CF_NAME: ColumnFamilyName = "stale_node_index";
48pub const TRANSACTION_CF_NAME: ColumnFamilyName = "transaction";
49pub const TRANSACTION_ACCUMULATOR_CF_NAME: ColumnFamilyName =
50    "transaction_accumulator";
51pub const TRANSACTION_BY_ACCOUNT_CF_NAME: ColumnFamilyName =
52    "transaction_by_account";
53pub const TRANSACTION_INFO_CF_NAME: ColumnFamilyName = "transaction_info";
54pub const LEDGER_INFO_BY_BLOCK_CF_NAME: ColumnFamilyName =
55    "ledger_info_by_block";
56pub const POS_STATE_CF_NAME: ColumnFamilyName = "pos_state";
57pub const REWARD_EVENT_CF_NAME: ColumnFamilyName = "reward_event";
58pub const COMMITTED_BLOCK_CF_NAME: ColumnFamilyName = "committed_block";
59pub const COMMITTED_BLOCK_BY_VIEW_CF_NAME: ColumnFamilyName =
60    "committed_block_by_view";
61pub const LEDGER_INFO_BY_VOTED_BLOCK_CF_NAME: ColumnFamilyName =
62    "ledger_info_by_voted_block";
63pub const BLOCK_BY_EPOCH_AND_ROUND_CF_NAME: ColumnFamilyName =
64    "block_by_epoch_and_round";
65
66fn ensure_slice_len_eq(data: &[u8], len: usize) -> Result<()> {
67    ensure!(
68        data.len() == len,
69        "Unexpected data len {}, expected {}.",
70        data.len(),
71        len,
72    );
73    Ok(())
74}
75
76#[cfg(feature = "fuzzing")]
77pub mod fuzzing {
78    use schemadb::schema::{KeyCodec, Schema, ValueCodec};
79
80    macro_rules! decode_key_value {
81        ($schema_type: ty, $data: ident) => {
82            <<$schema_type as Schema>::Key as KeyCodec<$schema_type>>::decode_key($data);
83            <<$schema_type as Schema>::Value as ValueCodec<$schema_type>>::decode_value($data);
84        };
85    }
86
87    pub fn fuzz_decode(data: &[u8]) {
88        #[allow(unused_must_use)]
89        {
90            decode_key_value!(
91                super::epoch_by_version::EpochByVersionSchema,
92                data
93            );
94            decode_key_value!(super::event::EventSchema, data);
95            decode_key_value!(
96                super::event_accumulator::EventAccumulatorSchema,
97                data
98            );
99            decode_key_value!(super::event_by_key::EventByKeySchema, data);
100            decode_key_value!(
101                super::event_by_version::EventByVersionSchema,
102                data
103            );
104            decode_key_value!(super::ledger_info::LedgerInfoSchema, data);
105            decode_key_value!(super::transaction::TransactionSchema, data);
106            decode_key_value!(
107                super::transaction_accumulator::TransactionAccumulatorSchema,
108                data
109            );
110            decode_key_value!(
111                super::transaction_by_account::TransactionByAccountSchema,
112                data
113            );
114            decode_key_value!(
115                super::transaction_info::TransactionInfoSchema,
116                data
117            );
118        }
119    }
120}