encoding.go 3.51 KB
Newer Older
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// 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.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

obscuren's avatar
obscuren committed
17
package trie
obscuren's avatar
obscuren committed
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// 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 {
Felix Lange's avatar
Felix Lange committed
38
	terminator := byte(0)
39
	if hasTerm(hex) {
obscuren's avatar
obscuren committed
40
		terminator = 1
41
		hex = hex[:len(hex)-1]
obscuren's avatar
obscuren committed
42
	}
43 44 45 46 47 48
	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:]
obscuren's avatar
obscuren committed
49
	}
50
	decodeNibbles(hex, buf[1:])
51
	return buf
obscuren's avatar
obscuren committed
52 53
}

54 55
func compactToHex(compact []byte) []byte {
	base := keybytesToHex(compact)
obscuren's avatar
obscuren committed
56
	base = base[:len(base)-1]
57
	// apply terminator flag
obscuren's avatar
obscuren committed
58
	if base[0] >= 2 {
obscuren's avatar
obscuren committed
59 60
		base = append(base, 16)
	}
61 62 63
	// apply odd flag
	chop := 2 - base[0]&1
	return base[chop:]
obscuren's avatar
obscuren committed
64 65
}

66
func keybytesToHex(str []byte) []byte {
67 68 69 70 71
	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
obscuren's avatar
obscuren committed
72
	}
73
	nibbles[l-1] = 16
74
	return nibbles
obscuren's avatar
obscuren committed
75
}
obscuren's avatar
obscuren committed
76

77 78 79 80 81
// 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]
82
	}
83 84
	if len(hex)&1 != 0 {
		panic("can't convert hex key of odd length")
85
	}
86 87 88
	key := make([]byte, (len(hex)+1)/2)
	decodeNibbles(hex, key)
	return key
89 90
}

91 92 93
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]
obscuren's avatar
obscuren committed
94 95
	}
}
Felix Lange's avatar
Felix Lange committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

// 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
}

111
// hasTerm returns whether a hex key has the terminator flag.
Felix Lange's avatar
Felix Lange committed
112
func hasTerm(s []byte) bool {
113
	return len(s) > 0 && s[len(s)-1] == 16
Felix Lange's avatar
Felix Lange committed
114
}