2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03: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 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03: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 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2014-10-31 15:45:03 +02:00
|
|
|
package trie
|
2014-02-15 00:56:09 +02:00
|
|
|
|
2017-04-18 14:25:07 +03: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-06 02:19:48 +03:00
|
|
|
terminator := byte(0)
|
2017-04-18 14:25:07 +03:00
|
|
|
if hasTerm(hex) {
|
2014-02-15 00:56:09 +02:00
|
|
|
terminator = 1
|
2017-04-18 14:25:07 +03:00
|
|
|
hex = hex[:len(hex)-1]
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
2017-04-18 14:25:07 +03: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-15 00:56:09 +02:00
|
|
|
}
|
2017-04-18 14:25:07 +03:00
|
|
|
decodeNibbles(hex, buf[1:])
|
2015-08-06 19:39:07 +03:00
|
|
|
return buf
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 18:38:13 +03:00
|
|
|
// hexToCompactInPlace places the compact key in input buffer, returning the length
|
|
|
|
// needed for the representation
|
|
|
|
func hexToCompactInPlace(hex []byte) int {
|
|
|
|
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
|
|
|
|
return binLen
|
|
|
|
}
|
|
|
|
|
2017-04-18 14:25:07 +03:00
|
|
|
func compactToHex(compact []byte) []byte {
|
2019-02-16 18:16:12 +03:00
|
|
|
if len(compact) == 0 {
|
|
|
|
return compact
|
|
|
|
}
|
2017-04-18 14:25:07 +03:00
|
|
|
base := keybytesToHex(compact)
|
2018-06-07 11:48:36 +03:00
|
|
|
// delete terminator flag
|
|
|
|
if base[0] < 2 {
|
|
|
|
base = base[:len(base)-1]
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
2017-04-18 14:25:07 +03:00
|
|
|
// apply odd flag
|
|
|
|
chop := 2 - base[0]&1
|
|
|
|
return base[chop:]
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 14:25:07 +03:00
|
|
|
func keybytesToHex(str []byte) []byte {
|
2015-08-06 19:39:07 +03: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-15 00:56:09 +02:00
|
|
|
}
|
2015-08-06 19:39:07 +03:00
|
|
|
nibbles[l-1] = 16
|
2015-08-06 10:11:10 +03:00
|
|
|
return nibbles
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
2014-05-27 02:08:51 +03:00
|
|
|
|
2017-04-18 14:25:07 +03: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 14:34:19 +02:00
|
|
|
}
|
2017-04-18 14:25:07 +03:00
|
|
|
if len(hex)&1 != 0 {
|
|
|
|
panic("can't convert hex key of odd length")
|
2015-11-30 14:34:19 +02:00
|
|
|
}
|
2018-05-29 18:48:43 +03:00
|
|
|
key := make([]byte, len(hex)/2)
|
2017-04-18 14:25:07 +03:00
|
|
|
decodeNibbles(hex, key)
|
|
|
|
return key
|
2015-11-30 14:34:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 14:25:07 +03: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-27 02:08:51 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-06 02:19:48 +03: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 14:25:07 +03:00
|
|
|
// hasTerm returns whether a hex key has the terminator flag.
|
2015-07-06 02:19:48 +03:00
|
|
|
func hasTerm(s []byte) bool {
|
2017-04-18 14:25:07 +03:00
|
|
|
return len(s) > 0 && s[len(s)-1] == 16
|
2015-07-06 02:19:48 +03:00
|
|
|
}
|