encoding.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  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. // GNU Lesser General Public License for more details.
  13. //
  14. // 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. package trie
  17. // Trie keys are dealt with in three distinct encodings:
  18. //
  19. // KEYBYTES encoding contains the actual key and nothing else. This encoding is the
  20. // input to most API functions.
  21. //
  22. // HEX encoding contains one byte for each nibble of the key and an optional trailing
  23. // 'terminator' byte of value 0x10 which indicates whether or not the node at the key
  24. // contains a value. Hex key encoding is used for nodes loaded in memory because it's
  25. // convenient to access.
  26. //
  27. // COMPACT encoding is defined by the Ethereum Yellow Paper (it's called "hex prefix
  28. // encoding" there) and contains the bytes of the key and a flag. The high nibble of the
  29. // first byte contains the flag; the lowest bit encoding the oddness of the length and
  30. // the second-lowest encoding whether the node at the key is a value node. The low nibble
  31. // of the first byte is zero in the case of an even number of nibbles and the first nibble
  32. // in the case of an odd number. All remaining nibbles (now an even number) fit properly
  33. // into the remaining bytes. Compact encoding is used for nodes stored on disk.
  34. func hexToCompact(hex []byte) []byte {
  35. terminator := byte(0)
  36. if hasTerm(hex) {
  37. terminator = 1
  38. hex = hex[:len(hex)-1]
  39. }
  40. buf := make([]byte, len(hex)/2+1)
  41. buf[0] = terminator << 5 // the flag byte
  42. if len(hex)&1 == 1 {
  43. buf[0] |= 1 << 4 // odd flag
  44. buf[0] |= hex[0] // first nibble is contained in the first byte
  45. hex = hex[1:]
  46. }
  47. decodeNibbles(hex, buf[1:])
  48. return buf
  49. }
  50. // hexToCompactInPlace places the compact key in input buffer, returning the length
  51. // needed for the representation
  52. func hexToCompactInPlace(hex []byte) int {
  53. var (
  54. hexLen = len(hex) // length of the hex input
  55. firstByte = byte(0)
  56. )
  57. // Check if we have a terminator there
  58. if hexLen > 0 && hex[hexLen-1] == 16 {
  59. firstByte = 1 << 5
  60. hexLen-- // last part was the terminator, ignore that
  61. }
  62. var (
  63. binLen = hexLen/2 + 1
  64. ni = 0 // index in hex
  65. bi = 1 // index in bin (compact)
  66. )
  67. if hexLen&1 == 1 {
  68. firstByte |= 1 << 4 // odd flag
  69. firstByte |= hex[0] // first nibble is contained in the first byte
  70. ni++
  71. }
  72. for ; ni < hexLen; bi, ni = bi+1, ni+2 {
  73. hex[bi] = hex[ni]<<4 | hex[ni+1]
  74. }
  75. hex[0] = firstByte
  76. return binLen
  77. }
  78. func compactToHex(compact []byte) []byte {
  79. if len(compact) == 0 {
  80. return compact
  81. }
  82. base := keybytesToHex(compact)
  83. // delete terminator flag
  84. if base[0] < 2 {
  85. base = base[:len(base)-1]
  86. }
  87. // apply odd flag
  88. chop := 2 - base[0]&1
  89. return base[chop:]
  90. }
  91. func keybytesToHex(str []byte) []byte {
  92. l := len(str)*2 + 1
  93. var nibbles = make([]byte, l)
  94. for i, b := range str {
  95. nibbles[i*2] = b / 16
  96. nibbles[i*2+1] = b % 16
  97. }
  98. nibbles[l-1] = 16
  99. return nibbles
  100. }
  101. // hexToKeybytes turns hex nibbles into key bytes.
  102. // This can only be used for keys of even length.
  103. func hexToKeybytes(hex []byte) []byte {
  104. if hasTerm(hex) {
  105. hex = hex[:len(hex)-1]
  106. }
  107. if len(hex)&1 != 0 {
  108. panic("can't convert hex key of odd length")
  109. }
  110. key := make([]byte, len(hex)/2)
  111. decodeNibbles(hex, key)
  112. return key
  113. }
  114. func decodeNibbles(nibbles []byte, bytes []byte) {
  115. for bi, ni := 0, 0; ni < len(nibbles); bi, ni = bi+1, ni+2 {
  116. bytes[bi] = nibbles[ni]<<4 | nibbles[ni+1]
  117. }
  118. }
  119. // prefixLen returns the length of the common prefix of a and b.
  120. func prefixLen(a, b []byte) int {
  121. var i, length = 0, len(a)
  122. if len(b) < length {
  123. length = len(b)
  124. }
  125. for ; i < length; i++ {
  126. if a[i] != b[i] {
  127. break
  128. }
  129. }
  130. return i
  131. }
  132. // hasTerm returns whether a hex key has the terminator flag.
  133. func hasTerm(s []byte) bool {
  134. return len(s) > 0 && s[len(s)-1] == 16
  135. }