MCPcopy Create free account
hub / github.com/base/triedb / encode

Method encode

src/node.rs:557–602  ·  view source on GitHub ↗
(&self, out: &mut dyn BufMut)

Source from the content-addressed store, hash-verified

555
556impl Encodable for Node {
557 fn encode(&self, out: &mut dyn BufMut) {
558 let prefix = self.prefix();
559 match self.kind() {
560 NodeKind::StorageLeaf { ref value_rlp } => {
561 LeafNodeRef { key: prefix, value: value_rlp }.encode(out);
562 }
563 NodeKind::AccountLeaf {
564 ref balance_rlp,
565 ref nonce_rlp,
566 ref code_hash,
567 ref storage_root,
568 } => {
569 let mut buf = [0u8; 110]; // max RLP length for an account: 2 bytes for list length, 9 for nonce, 33 for
570 // balance, 33 for storage root, 33 for code hash
571 let mut value_rlp = buf.as_mut();
572 let storage_root_hash = storage_root
573 .as_ref()
574 .map_or(EMPTY_ROOT_HASH, |p| p.rlp().as_hash().unwrap_or(EMPTY_ROOT_HASH));
575 let account_rlp_length = encode_account_leaf(
576 nonce_rlp,
577 balance_rlp,
578 code_hash,
579 &storage_root_hash,
580 &mut value_rlp,
581 );
582 LeafNodeRef { key: prefix, value: &buf[..account_rlp_length] }.encode(out);
583 }
584 NodeKind::Branch { ref children } => {
585 if prefix.is_empty() {
586 encode_branch(children, out);
587 } else {
588 let mut buf = [0u8; 3 + 33 * 16 + 1]; // max RLP length for a branch: 3 bytes for the list length, 33 bytes for each
589 // of the 16 children, 1 byte for the empty 17th slot
590 let mut branch_rlp = buf.as_mut();
591
592 let branch_rlp_length = encode_branch(children, &mut branch_rlp);
593
594 ExtensionNodeRef {
595 key: prefix,
596 child: &RlpNode::from_rlp(&buf[..branch_rlp_length]),
597 }
598 .encode(out);
599 }
600 }
601 }
602 }
603
604 fn length(&self) -> usize {
605 let prefix = self.prefix();

Callers 3

encode_branchFunction · 0.80
test_leaf_node_encodeFunction · 0.80
test_branch_node_encodeFunction · 0.80

Calls 8

encode_account_leafFunction · 0.85
encode_branchFunction · 0.85
kindMethod · 0.80
as_mutMethod · 0.80
as_refMethod · 0.80
rlpMethod · 0.80
prefixMethod · 0.45
is_emptyMethod · 0.45

Tested by 2

test_leaf_node_encodeFunction · 0.64
test_branch_node_encodeFunction · 0.64