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

Function test_branch_node_serialize

src/node.rs:922–998  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

920
921 #[test]
922 fn test_branch_node_serialize() {
923 let mut node: Node = Node::new_branch(&RawPath::new()).expect("can create node");
924 let hash1 = b256!("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
925 let hash2 = b256!("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
926 node.set_child(0, Pointer::new(42.into(), RlpNode::word_rlp(&hash1)))
927 .expect("should set child");
928 node.set_child(11, Pointer::new(43.into(), RlpNode::word_rlp(&hash2)))
929 .expect("should set child");
930 let bytes = node.serialize().unwrap();
931 assert_eq!(bytes.len(), 2 + 2 + 37 * 2);
932 // branch, no prefix
933 let mut expected = Vec::from([2, 0]);
934 // children bitmask (10000000 00010000)
935 expected.extend([128, 16]);
936 // child 0
937 expected.extend([0, 0, 0, 42, 1]);
938 expected.extend(hash1.as_slice());
939 // child 11
940 expected.extend([0, 0, 0, 43, 1]);
941 expected.extend(hash2.as_slice());
942 // children 12-15
943 assert_eq!(bytes, expected);
944
945 let mut node: Node = Node::new_branch(&RawPath::from_nibbles(&[0xa, 0xb, 0xc, 0xd, 0xe]))
946 .expect("can create node");
947 let hash1 = b256!("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef");
948 let hash2 = b256!("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
949 let hash3 = b256!("0x1111111111111111111111111111111111111111111111111111111111111111");
950 node.set_child(2, Pointer::new(100.into(), RlpNode::word_rlp(&hash1)))
951 .expect("should set child");
952 node.set_child(3, Pointer::new(200.into(), RlpNode::word_rlp(&hash2)))
953 .expect("should set child");
954 node.set_child(15, Pointer::new(210.into(), RlpNode::word_rlp(&hash3)))
955 .expect("should set child");
956 let bytes = node.serialize().unwrap();
957 assert_eq!(bytes.len(), 2 + 3 + 2 + 37 * 4);
958 // branch, length, prefix
959 let mut expected = Vec::from([2, 5, 171, 205, 224]);
960 // children bitmask (00110000 00000001)
961 expected.extend([48, 1]);
962 // child 2
963 expected.extend([0, 0, 0, 100, 1]);
964 expected.extend(hash1.as_slice());
965 // child 3
966 expected.extend([0, 0, 0, 200, 1]);
967 expected.extend(hash2.as_slice());
968 // child 15
969 expected.extend([0, 0, 0, 210, 1]);
970 expected.extend(hash3.as_slice());
971 // remaining empty slot
972 expected.extend([0; 37]);
973 assert_eq!(bytes, expected);
974
975 let mut node: Node =
976 Node::new_branch(&RawPath::from_nibbles(&[0x0, 0x0])).expect("can create node");
977 let v1 = encode(1u8);
978 let v2 = encode("hello world");
979 node.set_child(1, Pointer::new(99999.into(), RlpNode::from_rlp(&v1)))

Callers

nothing calls this directly

Calls 3

set_childMethod · 0.80
serializeMethod · 0.80
extendMethod · 0.80

Tested by

no test coverage detected