1mod branch;
2mod extension;
3mod leaf;
4
5use alloc::sync::Arc;
6#[cfg(not(feature = "std"))]
7use alloc::{boxed::Box, vec::Vec};
8#[cfg(feature = "std")]
9pub use std::sync::OnceLock;
10
11#[cfg(not(feature = "std"))]
25pub struct OnceLock<T>(core::cell::UnsafeCell<Option<T>>);
26
27#[cfg(not(feature = "std"))]
28impl<T> OnceLock<T> {
29 #[inline]
30 fn new() -> Self {
31 Self(core::cell::UnsafeCell::new(None))
32 }
33
34 #[inline]
35 fn get(&self) -> Option<&T> {
36 unsafe { &*self.0.get() }.as_ref()
37 }
38
39 #[inline]
40 fn get_or_init(&self, f: impl FnOnce() -> T) -> &T {
41 match self.get_or_try_init(|| Ok::<T, core::convert::Infallible>(f())) {
42 Ok(val) => val,
43 Err(e) => match e {},
44 }
45 }
46
47 #[inline]
48 fn get_or_try_init<E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
49 if let Some(val) = self.get() {
50 return Ok(val);
51 }
52 self.try_init(f)
53 }
54
55 #[inline]
56 fn set(&self, value: T) -> Result<(), T> {
57 match self.try_insert(value) {
58 Ok(_) => Ok(()),
59 Err((_, value)) => Err(value),
60 }
61 }
62
63 #[inline]
64 fn try_insert(&self, value: T) -> Result<&T, (&T, T)> {
65 if let Some(old) = self.get() {
66 return Err((old, value));
67 }
68 let slot = unsafe { &mut *self.0.get() };
69 Ok(slot.insert(value))
70 }
71
72 #[inline]
73 fn try_init<E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
74 let val = f()?;
75 let slot = unsafe { &mut *self.0.get() };
76 debug_assert!(slot.is_none());
77 Ok(slot.insert(val))
78 }
79
80 #[inline]
81 fn take(&mut self) -> Option<T> {
82 self.0.get_mut().take()
83 }
84}
85
86#[cfg(not(feature = "std"))]
87impl<T: PartialEq> PartialEq for OnceLock<T> {
88 #[inline]
89 fn eq(&self, other: &Self) -> bool {
90 self.get() == other.get()
91 }
92}
93
94#[cfg(not(feature = "std"))]
95impl<T> Default for OnceLock<T> {
96 #[inline]
97 fn default() -> Self {
98 Self::new()
99 }
100}
101
102#[cfg(not(feature = "std"))]
103impl<T: Eq> Eq for OnceLock<T> {}
104
105#[cfg(not(feature = "std"))]
106impl<T: Clone> Clone for OnceLock<T> {
107 #[inline]
108 fn clone(&self) -> OnceLock<T> {
109 match self.get() {
110 Some(value) => OnceLock::from(value.clone()),
111 None => OnceLock::new(),
112 }
113 }
114}
115
116#[cfg(not(feature = "std"))]
117impl<T: core::fmt::Debug> core::fmt::Debug for OnceLock<T> {
118 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
119 let mut d = f.debug_tuple("OnceLock");
120 match self.get() {
121 Some(v) => d.field(v),
122 None => d.field(&format_args!("<uninit>")),
123 };
124 d.finish()
125 }
126}
127
128#[cfg(not(feature = "std"))]
129impl<T> From<T> for OnceLock<T> {
130 #[inline]
131 fn from(value: T) -> Self {
132 OnceLock(core::cell::UnsafeCell::new(Some(value)))
133 }
134}
135
136pub use branch::BranchNode;
137use ethrex_rlp::{decode::RLPDecode, encode::RLPEncode};
138pub use extension::ExtensionNode;
139pub use leaf::LeafNode;
140use rkyv::{
141 de::Pooling,
142 rancor::Source,
143 ser::{Allocator, Sharing, Writer},
144 validation::{ArchiveContext, SharedContext},
145 with::Skip,
146};
147
148use ethrex_crypto::{Crypto, NativeCrypto};
149
150use crate::{NodeRLP, TrieDB, error::TrieError, nibbles::Nibbles};
151
152use super::{ValueRLP, node_hash::NodeHash};
153
154#[derive(
159 Clone,
160 Debug,
161 serde::Serialize,
162 serde::Deserialize,
163 rkyv::Serialize,
164 rkyv::Deserialize,
165 rkyv::Archive,
166)]
167#[rkyv(serialize_bounds(__S: Writer + Allocator + Sharing, __S::Error: Source))]
168#[rkyv(deserialize_bounds(__D: Pooling, __D::Error: Source))]
169#[rkyv(bytecheck(bounds(__C: ArchiveContext + SharedContext)))]
170pub enum NodeRef {
171 Node(
173 #[rkyv(omit_bounds)] Arc<Node>,
174 #[rkyv(with = Skip)]
175 #[serde(skip)]
176 OnceLock<NodeHash>,
177 ),
178 Hash(NodeHash),
180}
181
182impl NodeRef {
183 pub fn get_node(&self, db: &dyn TrieDB, path: Nibbles) -> Result<Option<Arc<Node>>, TrieError> {
187 match self {
188 NodeRef::Node(node, _) => Ok(Some(node.clone())),
189 NodeRef::Hash(hash @ NodeHash::Inline(_)) => {
190 Ok(Some(Arc::new(Node::decode(hash.as_ref())?)))
191 }
192 NodeRef::Hash(_) => db
193 .get(path)?
194 .filter(|rlp| !rlp.is_empty())
195 .map(|rlp| Ok(Arc::new(Node::decode(&rlp)?)))
196 .transpose(),
197 }
198 }
199
200 pub fn get_node_checked(
208 &self,
209 db: &dyn TrieDB,
210 path: Nibbles,
211 ) -> Result<Option<Arc<Node>>, TrieError> {
212 match self {
213 NodeRef::Node(node, _) => Ok(Some(node.clone())),
214 NodeRef::Hash(hash @ NodeHash::Inline(_)) => {
215 Ok(Some(Arc::new(Node::decode(hash.as_ref())?)))
216 }
217 NodeRef::Hash(hash @ NodeHash::Hashed(_)) => {
218 db.get(path)?
219 .filter(|rlp| !rlp.is_empty())
220 .and_then(|rlp| match Node::decode(&rlp) {
221 Ok(node) => (node.compute_hash(&NativeCrypto) == *hash)
222 .then_some(Ok(Arc::new(node))),
223 Err(err) => Some(Err(TrieError::RLPDecode(err))),
224 })
225 .transpose()
226 }
227 }
228 }
229
230 pub(crate) fn get_node_mut(
237 &mut self,
238 db: &dyn TrieDB,
239 path: Nibbles,
240 ) -> Result<Option<&mut Node>, TrieError> {
241 match self {
242 NodeRef::Node(node, _) => Ok(Some(Arc::make_mut(node))),
243 NodeRef::Hash(hash @ NodeHash::Inline(_)) => {
244 let node = Node::decode(hash.as_ref())?;
245 *self = NodeRef::Node(Arc::new(node), OnceLock::from(*hash));
246 self.get_node_mut(db, path)
247 }
248 NodeRef::Hash(hash @ NodeHash::Hashed(_)) => {
249 let Some(node) = db
250 .get(path.clone())?
251 .filter(|rlp| !rlp.is_empty())
252 .map(|rlp| Node::decode(&rlp).map_err(TrieError::RLPDecode))
253 .transpose()?
254 else {
255 return Ok(None);
256 };
257 *self = NodeRef::Node(Arc::new(node), OnceLock::from(*hash));
258 self.get_node_mut(db, path)
259 }
260 }
261 }
262
263 pub fn is_valid(&self) -> bool {
264 match self {
265 NodeRef::Node(_, _) => true,
266 NodeRef::Hash(hash) => hash.is_valid(),
267 }
268 }
269
270 pub fn commit(
271 &mut self,
272 path: Nibbles,
273 acc: &mut Vec<(Nibbles, Vec<u8>)>,
274 crypto: &(impl Crypto + ?Sized),
275 ) -> NodeHash {
276 match *self {
277 NodeRef::Node(ref mut node, ref mut hash) => {
278 if let Some(hash) = hash.get() {
279 return *hash;
280 }
281 match Arc::make_mut(node) {
282 Node::Branch(node) => {
283 for (choice, node) in &mut node.choices.iter_mut().enumerate() {
284 node.commit(path.append_new(choice as u8), acc, crypto);
285 }
286 }
287 Node::Extension(node) => {
288 node.child.commit(path.concat(&node.prefix), acc, crypto);
289 }
290 Node::Leaf(_) => {}
291 }
292 let mut buf = Vec::new();
293 node.encode(&mut buf);
294 let hash = *hash.get_or_init(|| NodeHash::from_encoded(&buf, crypto));
295 if let Node::Leaf(leaf) = node.as_ref() {
296 acc.push((path.concat(&leaf.partial), leaf.value.clone()));
297 }
298 acc.push((path, buf));
299
300 hash
301 }
302 NodeRef::Hash(hash) => hash,
303 }
304 }
305
306 pub fn compute_hash(&self, crypto: &(impl Crypto + ?Sized)) -> NodeHash {
307 *self.compute_hash_ref(crypto)
308 }
309
310 pub fn compute_hash_ref(&self, crypto: &(impl Crypto + ?Sized)) -> &NodeHash {
311 match self {
312 NodeRef::Node(node, hash) => hash.get_or_init(|| node.compute_hash(crypto)),
313 NodeRef::Hash(hash) => hash,
314 }
315 }
316
317 pub fn compute_hash_no_alloc(
318 &self,
319 buf: &mut Vec<u8>,
320 crypto: &(impl Crypto + ?Sized),
321 ) -> &NodeHash {
322 match self {
323 NodeRef::Node(node, hash) => {
324 hash.get_or_init(|| node.compute_hash_no_alloc(buf, crypto))
325 }
326 NodeRef::Hash(hash) => hash,
327 }
328 }
329
330 pub fn memoize_hashes(&self, buf: &mut Vec<u8>, crypto: &(impl Crypto + ?Sized)) {
331 if let NodeRef::Node(node, hash) = &self
332 && hash.get().is_none()
333 {
334 node.memoize_hashes(buf, crypto);
335 let _ = hash.set(node.compute_hash_no_alloc(buf, crypto));
336 }
337 }
338
339 pub fn clear_hash(&mut self) {
344 if let NodeRef::Node(_, hash) = self {
345 hash.take();
346 }
347 }
348}
349
350impl Default for NodeRef {
351 fn default() -> Self {
352 Self::Hash(NodeHash::default())
353 }
354}
355
356impl From<Node> for NodeRef {
357 fn from(value: Node) -> Self {
358 Self::Node(Arc::new(value), OnceLock::new())
359 }
360}
361
362impl From<NodeHash> for NodeRef {
363 fn from(value: NodeHash) -> Self {
364 Self::Hash(value)
365 }
366}
367
368impl From<Arc<Node>> for NodeRef {
369 fn from(value: Arc<Node>) -> Self {
370 Self::Node(value, OnceLock::new())
371 }
372}
373
374impl PartialEq for NodeRef {
375 fn eq(&self, other: &Self) -> bool {
376 let mut buf = Vec::new();
377 self.compute_hash_no_alloc(&mut buf, &NativeCrypto)
378 == other.compute_hash_no_alloc(&mut buf, &NativeCrypto)
379 }
380}
381
382pub enum ValueOrHash {
383 Value(ValueRLP),
384 Hash(NodeHash),
385}
386
387impl From<ValueRLP> for ValueOrHash {
388 fn from(value: ValueRLP) -> Self {
389 Self::Value(value)
390 }
391}
392
393impl From<NodeHash> for ValueOrHash {
394 fn from(value: NodeHash) -> Self {
395 Self::Hash(value)
396 }
397}
398
399#[derive(
400 Debug,
401 Clone,
402 PartialEq,
403 serde::Serialize,
404 serde::Deserialize,
405 rkyv::Deserialize,
406 rkyv::Serialize,
407 rkyv::Archive,
408)]
409pub enum Node {
411 Branch(Box<BranchNode>),
412 Extension(ExtensionNode),
413 Leaf(LeafNode),
414}
415
416impl Default for Node {
417 fn default() -> Self {
418 Self::Leaf(LeafNode {
420 partial: Nibbles::from_bytes(&[]),
421 value: Vec::new(),
422 })
423 }
424}
425
426impl From<Box<BranchNode>> for Node {
427 fn from(val: Box<BranchNode>) -> Self {
428 Node::Branch(val)
429 }
430}
431
432impl From<BranchNode> for Node {
433 fn from(val: BranchNode) -> Self {
434 Node::Branch(Box::new(val))
435 }
436}
437
438impl From<ExtensionNode> for Node {
439 fn from(val: ExtensionNode) -> Self {
440 Node::Extension(val)
441 }
442}
443
444impl From<LeafNode> for Node {
445 fn from(val: LeafNode) -> Self {
446 Node::Leaf(val)
447 }
448}
449
450impl Node {
451 pub fn get(&self, db: &dyn TrieDB, path: Nibbles) -> Result<Option<ValueRLP>, TrieError> {
453 match self {
454 Node::Branch(n) => n.get(db, path),
455 Node::Extension(n) => n.get(db, path),
456 Node::Leaf(n) => n.get(path),
457 }
458 }
459
460 pub fn insert(
462 &mut self,
463 db: &dyn TrieDB,
464 path: Nibbles,
465 value: impl Into<ValueOrHash>,
466 ) -> Result<(), TrieError> {
467 let new_node = match self {
468 Node::Branch(n) => {
469 n.insert(db, path, value.into())?;
470 Ok(None)
471 }
472 Node::Extension(n) => n.insert(db, path, value.into()),
473 Node::Leaf(n) => n.insert(path, value.into()),
474 };
475 if let Some(new_node) = new_node? {
476 *self = new_node;
477 }
478 Ok(())
479 }
480
481 pub fn remove(
484 &mut self,
485 db: &dyn TrieDB,
486 path: Nibbles,
487 ) -> Result<(bool, Option<ValueRLP>), TrieError> {
488 let (new_root, value) = match self {
489 Node::Branch(n) => n.remove(db, path),
490 Node::Extension(n) => n.remove(db, path),
491 Node::Leaf(n) => n.remove(path),
492 }?;
493
494 let is_trie_empty = new_root.is_none();
495 if let Some(NodeRemoveResult::New(new_root)) = new_root {
496 *self = new_root;
497 }
498 Ok((is_trie_empty, value))
499 }
500
501 pub fn get_path(
505 &self,
506 db: &dyn TrieDB,
507 path: Nibbles,
508 node_path: &mut Vec<Vec<u8>>,
509 ) -> Result<(), TrieError> {
510 match self {
511 Node::Branch(n) => n.get_path(db, path, node_path),
512 Node::Extension(n) => n.get_path(db, path, node_path),
513 Node::Leaf(n) => n.get_path(node_path),
514 }
515 }
516
517 pub fn compute_hash(&self, crypto: &(impl Crypto + ?Sized)) -> NodeHash {
519 let mut buf = Vec::new();
520 self.memoize_hashes(&mut buf, crypto);
521 match self {
522 Node::Branch(n) => n.compute_hash_no_alloc(&mut buf, crypto),
523 Node::Extension(n) => n.compute_hash_no_alloc(&mut buf, crypto),
524 Node::Leaf(n) => n.compute_hash_no_alloc(&mut buf, crypto),
525 }
526 }
527
528 pub fn compute_hash_no_alloc(
530 &self,
531 buf: &mut Vec<u8>,
532 crypto: &(impl Crypto + ?Sized),
533 ) -> NodeHash {
534 self.memoize_hashes(buf, crypto);
535 match self {
536 Node::Branch(n) => n.compute_hash_no_alloc(buf, crypto),
537 Node::Extension(n) => n.compute_hash_no_alloc(buf, crypto),
538 Node::Leaf(n) => n.compute_hash_no_alloc(buf, crypto),
539 }
540 }
541
542 pub fn memoize_hashes(&self, buf: &mut Vec<u8>, crypto: &(impl Crypto + ?Sized)) {
545 match self {
546 Node::Branch(n) => {
547 for child in &n.choices {
548 child.memoize_hashes(buf, crypto);
549 }
550 }
551 Node::Extension(n) => n.child.memoize_hashes(buf, crypto),
552 _ => {}
553 }
554 }
555
556 pub fn encode_subtrie(&self, encoded: &mut Vec<NodeRLP>) -> Result<(), TrieError> {
561 match self {
562 Node::Branch(node) => {
563 for choice in &node.choices {
564 if let NodeRef::Node(choice, _) = choice {
565 choice.encode_subtrie(encoded)?;
566 }
567 }
568 }
569 Node::Extension(node) => {
570 if let NodeRef::Node(child, _) = &node.child {
571 child.encode_subtrie(encoded)?;
572 }
573 }
574 Node::Leaf(_) => {}
575 };
576
577 encoded.push(self.encode_to_vec());
578 Ok(())
579 }
580}
581
582pub enum NodeRemoveResult {
586 Mutated,
587 New(Node),
588}