encoding.go 4.26 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// 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
}

83
func compactToHex(compact []byte) []byte {
84 85 86
	if len(compact) == 0 {
		return compact
	}
87
	base := keybytesToHex(compact)
88 89 90
	// delete terminator flag
	if base[0] < 2 {
		base = base[:len(base)-1]
obscuren's avatar
obscuren committed
91
	}
92 93 94
	// apply odd flag
	chop := 2 - base[0]&1
	return base[chop:]
obscuren's avatar
obscuren committed
95 96
}

97
func keybytesToHex(str []byte) []byte {
98 99 100 101 102
	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
103
	}
104
	nibbles[l-1] = 16
105
	return nibbles
obscuren's avatar
obscuren committed
106
}
obscuren's avatar
obscuren committed
107

108 109 110 111 112
// 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]
113
	}
114 115
	if len(hex)&1 != 0 {
		panic("can't convert hex key of odd length")
116
	}
117
	key := make([]byte, len(hex)/2)
118 119
	decodeNibbles(hex, key)
	return key
120 121
}

122 123 124
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
125 126
	}
}
Felix Lange's avatar
Felix Lange committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

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

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