github.com/dominant-strategies/go-quai@v0.28.2/trie/encoding.go (about)

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