Skip to content

Commit 14535c4

Browse files
committed
chore: remove needless TriePath type and use TrieHash everywhere. Also, add MARF::get_by_path() to look up MARFValues by TrieHash instead of by &str keys, and add the relevant ClarityBackingStore implementation to the stackslib's read-only and writeable MARF stores
1 parent 6260c16 commit 14535c4

File tree

28 files changed

+241
-234
lines changed

28 files changed

+241
-234
lines changed

stacks-common/src/types/chainstate.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,68 @@ impl_byte_array_serde!(TrieHash);
3030

3131
pub const TRIEHASH_ENCODED_SIZE: usize = 32;
3232

33+
impl TrieHash {
34+
pub fn from_key(k: &str) -> Self {
35+
Self::from_data(k.as_bytes())
36+
}
37+
38+
/// TrieHash of zero bytes
39+
pub fn from_empty_data() -> TrieHash {
40+
// sha2-512/256 hash of empty string.
41+
// this is used so frequently it helps performance if we just have a constant for it.
42+
TrieHash([
43+
0xc6, 0x72, 0xb8, 0xd1, 0xef, 0x56, 0xed, 0x28, 0xab, 0x87, 0xc3, 0x62, 0x2c, 0x51,
44+
0x14, 0x06, 0x9b, 0xdd, 0x3a, 0xd7, 0xb8, 0xf9, 0x73, 0x74, 0x98, 0xd0, 0xc0, 0x1e,
45+
0xce, 0xf0, 0x96, 0x7a,
46+
])
47+
}
48+
49+
/// TrieHash from bytes
50+
pub fn from_data(data: &[u8]) -> TrieHash {
51+
if data.len() == 0 {
52+
return TrieHash::from_empty_data();
53+
}
54+
55+
let mut tmp = [0u8; 32];
56+
57+
let mut hasher = Sha512_256::new();
58+
hasher.update(data);
59+
tmp.copy_from_slice(hasher.finalize().as_slice());
60+
61+
TrieHash(tmp)
62+
}
63+
64+
pub fn from_data_array<B: AsRef<[u8]>>(data: &[B]) -> TrieHash {
65+
if data.len() == 0 {
66+
return TrieHash::from_empty_data();
67+
}
68+
69+
let mut tmp = [0u8; 32];
70+
71+
let mut hasher = Sha512_256::new();
72+
73+
for item in data.iter() {
74+
hasher.update(item);
75+
}
76+
tmp.copy_from_slice(hasher.finalize().as_slice());
77+
TrieHash(tmp)
78+
}
79+
80+
/// Convert to a String that can be used in e.g. sqlite
81+
pub fn to_string(&self) -> String {
82+
let s = format!("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
83+
self.0[0], self.0[1], self.0[2], self.0[3],
84+
self.0[4], self.0[5], self.0[6], self.0[7],
85+
self.0[8], self.0[9], self.0[10], self.0[11],
86+
self.0[12], self.0[13], self.0[14], self.0[15],
87+
self.0[16], self.0[17], self.0[18], self.0[19],
88+
self.0[20], self.0[21], self.0[22], self.0[23],
89+
self.0[24], self.0[25], self.0[26], self.0[27],
90+
self.0[28], self.0[29], self.0[30], self.0[31]);
91+
s
92+
}
93+
}
94+
3395
#[derive(Serialize, Deserialize)]
3496
pub struct BurnchainHeaderHash(pub [u8; 32]);
3597
impl_array_newtype!(BurnchainHeaderHash, u8, 32);

stackslib/src/burnchains/tests/burnchain.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use crate::chainstate::burn::{
4444
BlockSnapshot, ConsensusHash, ConsensusHashExtensions, OpsHash, SortitionHash,
4545
};
4646
use crate::chainstate::stacks::address::StacksAddressExtensions;
47-
use crate::chainstate::stacks::index::TrieHashExtension;
4847
use crate::chainstate::stacks::StacksPublicKey;
4948
use crate::util_lib::db::Error as db_error;
5049

stackslib/src/chainstate/burn/db/processing.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ mod tests {
353353
use crate::chainstate::burn::operations::{LeaderBlockCommitOp, LeaderKeyRegisterOp};
354354
use crate::chainstate::burn::*;
355355
use crate::chainstate::stacks::address::StacksAddressExtensions;
356-
use crate::chainstate::stacks::index::TrieHashExtension;
357356
use crate::chainstate::stacks::StacksPublicKey;
358357
use crate::core::MICROSTACKS_PER_STACKS;
359358

stackslib/src/chainstate/burn/db/sortdb.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6594,7 +6594,6 @@ pub mod tests {
65946594
BlockstackOperationType, LeaderBlockCommitOp, LeaderKeyRegisterOp,
65956595
};
65966596
use crate::chainstate::burn::ConsensusHash;
6597-
use crate::chainstate::stacks::index::TrieHashExtension;
65986597
use crate::chainstate::stacks::StacksPublicKey;
65996598
use crate::core::{StacksEpochExtension, *};
66006599
use crate::util_lib::db::Error as db_error;

stackslib/src/chainstate/burn/distribution.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ mod tests {
450450
};
451451
use crate::chainstate::burn::ConsensusHash;
452452
use crate::chainstate::stacks::address::StacksAddressExtensions;
453-
use crate::chainstate::stacks::index::TrieHashExtension;
454453
use crate::chainstate::stacks::StacksPublicKey;
455454
use crate::core::MINING_COMMITMENT_WINDOW;
456455

stackslib/src/chainstate/burn/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ mod tests {
432432
use crate::burnchains::bitcoin::address::BitcoinAddress;
433433
use crate::burnchains::bitcoin::keys::BitcoinPublicKey;
434434
use crate::chainstate::burn::db::sortdb::*;
435-
use crate::chainstate::stacks::index::TrieHashExtension;
436435
use crate::util_lib::db::Error as db_error;
437436

438437
#[test]

stackslib/src/chainstate/burn/operations/leader_block_commit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,6 @@ mod tests {
11721172
use crate::chainstate::burn::operations::*;
11731173
use crate::chainstate::burn::{ConsensusHash, *};
11741174
use crate::chainstate::stacks::address::StacksAddressExtensions;
1175-
use crate::chainstate::stacks::index::TrieHashExtension;
11761175
use crate::chainstate::stacks::StacksPublicKey;
11771176
use crate::core::{
11781177
StacksEpoch, StacksEpochExtension, StacksEpochId, PEER_VERSION_EPOCH_1_0,

stackslib/src/chainstate/burn/operations/leader_key_register.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ pub mod tests {
253253
};
254254
use crate::chainstate::burn::{BlockSnapshot, ConsensusHash, OpsHash, SortitionHash};
255255
use crate::chainstate::stacks::address::StacksAddressExtensions;
256-
use crate::chainstate::stacks::index::TrieHashExtension;
257256
use crate::core::StacksEpochId;
258257

259258
pub struct OpFixture {

stackslib/src/chainstate/burn/sortition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::chainstate::burn::{
4040
SortitionHash,
4141
};
4242
use crate::chainstate::stacks::db::StacksChainState;
43-
use crate::chainstate::stacks::index::{ClarityMarfTrieId, MarfTrieId, TrieHashExtension};
43+
use crate::chainstate::stacks::index::{ClarityMarfTrieId, MarfTrieId};
4444
use crate::core::*;
4545
use crate::util_lib::db::Error as db_error;
4646

stackslib/src/chainstate/stacks/index/bits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use stacks_common::util::macros::is_trace;
2929

3030
use crate::chainstate::stacks::index::node::{
3131
clear_backptr, ConsensusSerializable, TrieNode, TrieNode16, TrieNode256, TrieNode4, TrieNode48,
32-
TrieNodeID, TrieNodeType, TriePtr, TRIEPATH_MAX_LEN, TRIEPTR_SIZE,
32+
TrieNodeID, TrieNodeType, TriePtr, TRIEPTR_SIZE,
3333
};
3434
use crate::chainstate::stacks::index::storage::{TrieFileStorage, TrieStorageConnection};
3535
use crate::chainstate::stacks::index::{BlockMap, Error, MarfTrieId, TrieLeaf};
@@ -55,15 +55,15 @@ pub fn path_from_bytes<R: Read>(r: &mut R) -> Result<Vec<u8>, Error> {
5555
}
5656
})?;
5757

58-
if lenbuf[0] as usize > TRIEPATH_MAX_LEN {
58+
if lenbuf[0] as usize > TRIEHASH_ENCODED_SIZE {
5959
trace!(
6060
"Path length is {} (expected <= {})",
6161
lenbuf[0],
62-
TRIEPATH_MAX_LEN
62+
TRIEHASH_ENCODED_SIZE
6363
);
6464
return Err(Error::CorruptionError(format!(
6565
"Node path is longer than {} bytes (got {})",
66-
TRIEPATH_MAX_LEN, lenbuf[0]
66+
TRIEHASH_ENCODED_SIZE, lenbuf[0]
6767
)));
6868
}
6969

@@ -326,7 +326,7 @@ pub fn read_nodetype_at_head_nohash<F: Read + Seek>(
326326
/// node hash id ptrs & ptr data path
327327
///
328328
/// X is fixed and determined by the TrieNodeType variant.
329-
/// Y is variable, but no more than TriePath::len().
329+
/// Y is variable, but no more than TrieHash::len().
330330
///
331331
/// If `read_hash` is false, then the contents of the node hash are undefined.
332332
fn inner_read_nodetype_at_head<F: Read + Seek>(

0 commit comments

Comments
 (0)