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

Method from_bytes

src/pointer.rs:69–93  ·  view source on GitHub ↗
(bytes: &[u8])

Source from the content-addressed store, hash-verified

67 }
68
69 fn from_bytes(bytes: &[u8]) -> value::Result<Self> {
70 let arr: [u8; 37] = bytes.try_into().map_err(|_| value::Error::InvalidEncoding)?;
71 let flags = arr[4];
72 let rlp = if flags & HASH_FLAG == HASH_FLAG {
73 RlpNode::word_rlp(&B256::from_slice(&arr[5..37]))
74 } else {
75 // Because the RLP string must be 1-32 bytes, we can safely use the first byte to
76 // determine the length. If the first byte is less than 0x80, then this byte
77 // is the actual encoded value. Otherwise, the length is first_rlp_byte -
78 // 0x80, and the remaining bytes are the encoded U256 value.
79 let first_rlp_byte = arr[5];
80 if first_rlp_byte < 0x80 {
81 RlpNode::from_raw(&[first_rlp_byte]).unwrap()
82 } else if first_rlp_byte <= 0xa0 {
83 let rlp_len = first_rlp_byte - 0x80;
84 RlpNode::from_raw(&arr[5..6 + rlp_len as usize]).unwrap()
85 } else if (0xc0..=0xdf).contains(&first_rlp_byte) {
86 let rlp_len = first_rlp_byte - 0xc0;
87 RlpNode::from_raw(&arr[5..6 + rlp_len as usize]).unwrap()
88 } else {
89 return Err(value::Error::InvalidEncoding);
90 }
91 };
92 Ok(Pointer::new(Location::from(u32::from_be_bytes(arr[..4].try_into().unwrap())), rlp))
93 }
94}
95
96impl From<Pointer> for [u8; 37] {

Callers

nothing calls this directly

Calls 1

try_intoMethod · 0.80

Tested by

no test coverage detected