Skip to main content

ab_core_primitives/
trees.rs

1//! Merkle Trees used by the protocol.
2//!
3//! Every tree hashes its inner nodes with its own key, so a node of one tree can't be interpreted
4//! as a node of another tree. Trees with a number of leaves that isn't known at compile time also
5//! commit to it.
6
7mod hashers;
8#[cfg(test)]
9mod tests;
10
11use crate::address::Address;
12use ab_io_type::trivial_type::TrivialType;
13use ab_merkle_tree::balanced::BalancedMerkleTree;
14use ab_merkle_tree::blake3::Blake3MerkleHasher;
15use ab_merkle_tree::mmr::MerkleMountainRange;
16use ab_merkle_tree::sparse::SparseMerkleTree;
17use ab_merkle_tree::unbalanced::UnbalancedMerkleTree;
18use hashers::{Blake3SizedMerkleHasher, Blake3StaticMerkleHasher};
19
20type SegmentRootsMerkleHasher =
21    Blake3SizedMerkleHasher<{ ab_blake3::const_hash(b"segment-roots-merkle-tree") }>;
22type OwnSegmentsMerkleHasher =
23    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"own-segments-merkle-tree") }>;
24type ShardOwnSegmentsMerkleHasher =
25    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"shard-own-segments-merkle-tree") }>;
26type LeafShardBodyMerkleHasher =
27    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"leaf-shard-body-merkle-tree") }>;
28type TransactionsMerkleHasher =
29    Blake3SizedMerkleHasher<{ ab_blake3::const_hash(b"transactions-merkle-tree") }>;
30type IntermediateShardBodyMerkleHasher =
31    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"intermediate-shard-body-merkle-tree") }>;
32type LeafShardsSegmentsMerkleHasher =
33    Blake3SizedMerkleHasher<{ ab_blake3::const_hash(b"leaf-shards-segments-merkle-tree") }>;
34type ChildLeafShardBlocksMerkleHasher =
35    Blake3SizedMerkleHasher<{ ab_blake3::const_hash(b"child-leaf-shard-blocks-merkle-tree") }>;
36type ChildIntermediateShardBlocksMerkleHasher = Blake3SizedMerkleHasher<
37    { ab_blake3::const_hash(b"child-intermediate-shard-blocks-merkle-tree") },
38>;
39type BeaconChainBodyMerkleHasher =
40    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"beacon-chain-body-merkle-tree") }>;
41type IntermediateShardsSegmentsMerkleHasher =
42    Blake3SizedMerkleHasher<{ ab_blake3::const_hash(b"intermediate-shards-segments-merkle-tree") }>;
43type BeaconChainHeaderMerkleHasher =
44    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"beacon-chain-header-merkle-tree") }>;
45type IntermediateShardHeaderMerkleHasher =
46    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"intermediate-shard-header-merkle-tree") }>;
47type LeafShardHeaderMerkleHasher =
48    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"leaf-shard-header-merkle-tree") }>;
49type SuperSegmentMerkleHasher =
50    Blake3SizedMerkleHasher<{ ab_blake3::const_hash(b"super-segment-merkle-tree") }>;
51type StateOwnersSparseMerkleHasher =
52    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"state-owners-sparse-merkle-tree") }>;
53type StateOwnerContractsSparseMerkleHasher = Blake3StaticMerkleHasher<
54    { ab_blake3::const_hash(b"state-owner-contracts-sparse-merkle-tree") },
55>;
56type BlockMerkleMountainRangeHasher =
57    Blake3StaticMerkleHasher<{ ab_blake3::const_hash(b"block-merkle-mountain-range") }>;
58
59const {
60    let keys = [
61        Blake3MerkleHasher::KEY,
62        SegmentRootsMerkleHasher::BASE_KEY,
63        OwnSegmentsMerkleHasher::KEY,
64        ShardOwnSegmentsMerkleHasher::KEY,
65        LeafShardBodyMerkleHasher::KEY,
66        TransactionsMerkleHasher::BASE_KEY,
67        IntermediateShardBodyMerkleHasher::KEY,
68        LeafShardsSegmentsMerkleHasher::BASE_KEY,
69        ChildLeafShardBlocksMerkleHasher::BASE_KEY,
70        ChildIntermediateShardBlocksMerkleHasher::BASE_KEY,
71        BeaconChainBodyMerkleHasher::KEY,
72        IntermediateShardsSegmentsMerkleHasher::BASE_KEY,
73        BeaconChainHeaderMerkleHasher::KEY,
74        IntermediateShardHeaderMerkleHasher::KEY,
75        LeafShardHeaderMerkleHasher::KEY,
76        SuperSegmentMerkleHasher::BASE_KEY,
77        StateOwnersSparseMerkleHasher::KEY,
78        StateOwnerContractsSparseMerkleHasher::KEY,
79        BlockMerkleMountainRangeHasher::KEY,
80    ];
81
82    let mut i = 0;
83    while i < keys.len() {
84        let mut j = i + 1;
85        while j < keys.len() {
86            assert!(keys[i] != keys[j], "Keys of Merkle Trees must be unique");
87            j += 1;
88        }
89        i += 1;
90    }
91}
92
93/// Number of bits in [`Address`], which is the number of levels of state sparse Merkle Trees
94const ADDRESS_BITS: u8 = Address::SIZE as u8 * u8::BITS as u8;
95
96/// Segment roots of own segments of a shard
97pub type SegmentRootsMerkleTree = UnbalancedMerkleTree<SegmentRootsMerkleHasher>;
98/// Own segments of a shard
99pub type OwnSegmentsMerkleTree<'a> = BalancedMerkleTree<'a, 2, OwnSegmentsMerkleHasher>;
100/// Own segments of a leaf shard
101pub type ShardOwnSegmentsMerkleTree<'a> = BalancedMerkleTree<'a, 2, ShardOwnSegmentsMerkleHasher>;
102/// Leaf shard block body
103pub type LeafShardBodyMerkleTree<'a> = BalancedMerkleTree<'a, 2, LeafShardBodyMerkleHasher>;
104/// Transactions of a leaf shard block
105pub type TransactionsMerkleTree = UnbalancedMerkleTree<TransactionsMerkleHasher>;
106/// Intermediate shard block body
107pub type IntermediateShardBodyMerkleTree = UnbalancedMerkleTree<IntermediateShardBodyMerkleHasher>;
108/// Own segments of child leaf shard blocks of an intermediate shard block
109pub type LeafShardsSegmentsMerkleTree = UnbalancedMerkleTree<LeafShardsSegmentsMerkleHasher>;
110/// Child leaf shard block roots of an intermediate shard block
111pub type ChildLeafShardBlocksMerkleTree = UnbalancedMerkleTree<ChildLeafShardBlocksMerkleHasher>;
112/// Child intermediate shard block roots of a beacon chain block
113pub type ChildIntermediateShardBlocksMerkleTree =
114    UnbalancedMerkleTree<ChildIntermediateShardBlocksMerkleHasher>;
115/// Beacon chain block body
116pub type BeaconChainBodyMerkleTree<'a> = BalancedMerkleTree<'a, 4, BeaconChainBodyMerkleHasher>;
117/// Segments of child intermediate shard blocks of a beacon chain block
118pub type IntermediateShardsSegmentsMerkleTree =
119    UnbalancedMerkleTree<IntermediateShardsSegmentsMerkleHasher>;
120/// Beacon chain block header fields, its root is the block root
121pub type BeaconChainHeaderMerkleTree = UnbalancedMerkleTree<BeaconChainHeaderMerkleHasher>;
122/// Intermediate shard block header fields, its root is the block root
123pub type IntermediateShardHeaderMerkleTree =
124    UnbalancedMerkleTree<IntermediateShardHeaderMerkleHasher>;
125/// Leaf shard block header fields, its root is the block root
126pub type LeafShardHeaderMerkleTree = UnbalancedMerkleTree<LeafShardHeaderMerkleHasher>;
127/// Segment roots with their positions in a super segment
128pub type SuperSegmentMerkleTree = UnbalancedMerkleTree<SuperSegmentMerkleHasher>;
129/// Global state: state roots of owners by owner address
130pub type StateOwnersSparseMerkleTree =
131    SparseMerkleTree<ADDRESS_BITS, StateOwnersSparseMerkleHasher>;
132/// State of one owner: hashes of contents of slots by the address of the managing contract
133pub type StateOwnerContractsSparseMerkleTree =
134    SparseMerkleTree<ADDRESS_BITS, StateOwnerContractsSparseMerkleHasher>;
135/// Beacon chain block roots.
136///
137/// NOTE: `u32` is smaller than `BlockNumber`'s internal `u64` but will be sufficient for a long
138/// time and substantially decrease the size of the in-memory data structure.
139pub type BlockMerkleMountainRange =
140    MerkleMountainRange<{ u64::from(u32::MAX) }, BlockMerkleMountainRangeHasher>;