2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2014-10-31 08:45:03 -05:00
|
|
|
package trie
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2017-04-18 06:25:07 -05:00
|
|
|
// Trie keys are dealt with in three distinct encodings:
|
|
|
|
//
|
|
|
|
// KEYBYTES encoding contains the actual key and nothing else. This encoding is the
|
|
|
|
// input to most API functions.
|
|
|
|
//
|
|
|
|
// HEX encoding contains one byte for each nibble of the key and an optional trailing
|
|
|
|
// 'terminator' byte of value 0x10 which indicates whether or not the node at the key
|
|
|
|
// contains a value. Hex key encoding is used for nodes loaded in memory because it's
|
|
|
|
// convenient to access.
|
|
|
|
//
|
|
|
|
// COMPACT encoding is defined by the Ethereum Yellow Paper (it's called "hex prefix
|
|
|
|
// encoding" there) and contains the bytes of the key and a flag. The high nibble of the
|
|
|
|
// first byte contains the flag; the lowest bit encoding the oddness of the length and
|
|
|
|
// the second-lowest encoding whether the node at the key is a value node. The low nibble
|
|
|
|
// of the first byte is zero in the case of an even number of nibbles and the first nibble
|
|
|
|
// in the case of an odd number. All remaining nibbles (now an even number) fit properly
|
|
|
|
// into the remaining bytes. Compact encoding is used for nodes stored on disk.
|
|
|
|
|
|
|
|
func hexToCompact(hex []byte) []byte {
|
2015-07-05 18:19:48 -05:00
|
|
|
terminator := byte(0)
|
2017-04-18 06:25:07 -05:00
|
|
|
if hasTerm(hex) {
|
2014-02-14 16:56:09 -06:00
|
|
|
terminator = 1
|
2017-04-18 06:25:07 -05:00
|
|
|
hex = hex[:len(hex)-1]
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2017-04-18 06:25:07 -05:00
|
|
|
buf := make([]byte, len(hex)/2+1)
|
|
|
|
buf[0] = terminator << 5 // the flag byte
|
|
|
|
if len(hex)&1 == 1 {
|
|
|
|
buf[0] |= 1 << 4 // odd flag
|
|
|
|
buf[0] |= hex[0] // first nibble is contained in the first byte
|
|
|
|
hex = hex[1:]
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2017-04-18 06:25:07 -05:00
|
|
|
decodeNibbles(hex, buf[1:])
|
2015-08-06 11:39:07 -05:00
|
|
|
return buf
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:41:19 -05:00
|
|
|
// hexToCompactInPlace places the compact key in input buffer, returning the compacted key.
|
|
|
|
func hexToCompactInPlace(hex []byte) []byte {
|
2020-09-29 10:38:13 -05:00
|
|
|
var (
|
|
|
|
hexLen = len(hex) // length of the hex input
|
|
|
|
firstByte = byte(0)
|
|
|
|
)
|
|
|
|
// Check if we have a terminator there
|
|
|
|
if hexLen > 0 && hex[hexLen-1] == 16 {
|
|
|
|
firstByte = 1 << 5
|
|
|
|
hexLen-- // last part was the terminator, ignore that
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
binLen = hexLen/2 + 1
|
|
|
|
ni = 0 // index in hex
|
|
|
|
bi = 1 // index in bin (compact)
|
|
|
|
)
|
|
|
|
if hexLen&1 == 1 {
|
|
|
|
firstByte |= 1 << 4 // odd flag
|
|
|
|
firstByte |= hex[0] // first nibble is contained in the first byte
|
|
|
|
ni++
|
|
|
|
}
|
|
|
|
for ; ni < hexLen; bi, ni = bi+1, ni+2 {
|
|
|
|
hex[bi] = hex[ni]<<4 | hex[ni+1]
|
|
|
|
}
|
|
|
|
hex[0] = firstByte
|
2023-08-18 15:41:19 -05:00
|
|
|
return hex[:binLen]
|
2020-09-29 10:38:13 -05:00
|
|
|
}
|
|
|
|
|
2017-04-18 06:25:07 -05:00
|
|
|
func compactToHex(compact []byte) []byte {
|
2019-02-16 09:16:12 -06:00
|
|
|
if len(compact) == 0 {
|
|
|
|
return compact
|
|
|
|
}
|
2017-04-18 06:25:07 -05:00
|
|
|
base := keybytesToHex(compact)
|
2018-06-07 03:48:36 -05:00
|
|
|
// delete terminator flag
|
|
|
|
if base[0] < 2 {
|
|
|
|
base = base[:len(base)-1]
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2017-04-18 06:25:07 -05:00
|
|
|
// apply odd flag
|
|
|
|
chop := 2 - base[0]&1
|
|
|
|
return base[chop:]
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2017-04-18 06:25:07 -05:00
|
|
|
func keybytesToHex(str []byte) []byte {
|
2015-08-06 11:39:07 -05:00
|
|
|
l := len(str)*2 + 1
|
|
|
|
var nibbles = make([]byte, l)
|
|
|
|
for i, b := range str {
|
|
|
|
nibbles[i*2] = b / 16
|
|
|
|
nibbles[i*2+1] = b % 16
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2015-08-06 11:39:07 -05:00
|
|
|
nibbles[l-1] = 16
|
2015-08-06 02:11:10 -05:00
|
|
|
return nibbles
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2014-05-26 18:08:51 -05:00
|
|
|
|
2017-04-18 06:25:07 -05:00
|
|
|
// hexToKeybytes turns hex nibbles into key bytes.
|
|
|
|
// This can only be used for keys of even length.
|
|
|
|
func hexToKeybytes(hex []byte) []byte {
|
|
|
|
if hasTerm(hex) {
|
|
|
|
hex = hex[:len(hex)-1]
|
2015-11-30 06:34:19 -06:00
|
|
|
}
|
2017-04-18 06:25:07 -05:00
|
|
|
if len(hex)&1 != 0 {
|
|
|
|
panic("can't convert hex key of odd length")
|
2015-11-30 06:34:19 -06:00
|
|
|
}
|
2018-05-29 10:48:43 -05:00
|
|
|
key := make([]byte, len(hex)/2)
|
2017-04-18 06:25:07 -05:00
|
|
|
decodeNibbles(hex, key)
|
|
|
|
return key
|
2015-11-30 06:34:19 -06:00
|
|
|
}
|
|
|
|
|
2017-04-18 06:25:07 -05:00
|
|
|
func decodeNibbles(nibbles []byte, bytes []byte) {
|
|
|
|
for bi, ni := 0, 0; ni < len(nibbles); bi, ni = bi+1, ni+2 {
|
|
|
|
bytes[bi] = nibbles[ni]<<4 | nibbles[ni+1]
|
2014-05-26 18:08:51 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
|
|
|
|
// prefixLen returns the length of the common prefix of a and b.
|
|
|
|
func prefixLen(a, b []byte) int {
|
|
|
|
var i, length = 0, len(a)
|
|
|
|
if len(b) < length {
|
|
|
|
length = len(b)
|
|
|
|
}
|
|
|
|
for ; i < length; i++ {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2017-04-18 06:25:07 -05:00
|
|
|
// hasTerm returns whether a hex key has the terminator flag.
|
2015-07-05 18:19:48 -05:00
|
|
|
func hasTerm(s []byte) bool {
|
2017-04-18 06:25:07 -05:00
|
|
|
return len(s) > 0 && s[len(s)-1] == 16
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|