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 jellyfish_merkle_node;
27pub(crate) mod ledger_counters;
28pub(crate) mod ledger_info;
29pub(crate) mod ledger_info_by_block;
30pub(crate) mod ledger_info_by_voted_block;
31pub(crate) mod pos_state;
32pub(crate) mod reward_event;
33pub(crate) mod stale_node_index;
34pub(crate) mod transaction;
35pub(crate) mod transaction_accumulator;
36pub(crate) mod transaction_by_account;
37pub(crate) mod transaction_info;
38
39pub const EPOCH_BY_VERSION_CF_NAME: ColumnFamilyName = "epoch_by_version";
40pub const EVENT_ACCUMULATOR_CF_NAME: ColumnFamilyName = "event_accumulator";
41pub const EVENT_BY_KEY_CF_NAME: ColumnFamilyName = "event_by_key";
42pub const EVENT_BY_VERSION_CF_NAME: ColumnFamilyName = "event_by_version";
43pub const EVENT_CF_NAME: ColumnFamilyName = "event";
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
76fn ensure_slice_len_gt(data: &[u8], len: usize) -> Result<()> {
77    ensure!(
78        data.len() > len,
79        "Unexpected data len {}, expected to be greater than {}.",
80        data.len(),
81        len,
82    );
83    Ok(())
84}
85
86#[cfg(feature = "fuzzing")]
87pub mod fuzzing {
88    use schemadb::schema::{KeyCodec, Schema, ValueCodec};
89
90    macro_rules! decode_key_value {
91        ($schema_type: ty, $data: ident) => {
92            <<$schema_type as Schema>::Key as KeyCodec<$schema_type>>::decode_key($data);
93            <<$schema_type as Schema>::Value as ValueCodec<$schema_type>>::decode_value($data);
94        };
95    }
96
97    pub fn fuzz_decode(data: &[u8]) {
98        #[allow(unused_must_use)]
99        {
100            decode_key_value!(
101                super::epoch_by_version::EpochByVersionSchema,
102                data
103            );
104            decode_key_value!(super::event::EventSchema, data);
105            decode_key_value!(
106                super::event_accumulator::EventAccumulatorSchema,
107                data
108            );
109            decode_key_value!(super::event_by_key::EventByKeySchema, data);
110            decode_key_value!(
111                super::event_by_version::EventByVersionSchema,
112                data
113            );
114            decode_key_value!(
115                super::jellyfish_merkle_node::JellyfishMerkleNodeSchema,
116                data
117            );
118            decode_key_value!(
119                super::ledger_counters::LedgerCountersSchema,
120                data
121            );
122            decode_key_value!(super::ledger_info::LedgerInfoSchema, data);
123            decode_key_value!(
124                super::stale_node_index::StaleNodeIndexSchema,
125                data
126            );
127            decode_key_value!(super::transaction::TransactionSchema, data);
128            decode_key_value!(
129                super::transaction_accumulator::TransactionAccumulatorSchema,
130                data
131            );
132            decode_key_value!(
133                super::transaction_by_account::TransactionByAccountSchema,
134                data
135            );
136            decode_key_value!(
137                super::transaction_info::TransactionInfoSchema,
138                data
139            );
140        }
141    }
142}