eth-iterator-utils/util.go
Roy Crihfield e0c758f9f6 Add tracker package, update and simplify (#1)
This revives the standalone repo for these tools now that we're liberated from the Geth fork.
* pulls changes from geth fork
* remove unused: StartPath, SubtrieIteratorFactory
* changes module name to reflect bundled `tracker`
* refactors fixture data into separate module

Reviewed-on: #1
2023-09-19 16:58:23 +00:00

51 lines
1.1 KiB
Go

package iterator
import (
"bytes"
"github.com/ethereum/go-ethereum/trie"
)
func CompareNodes(a, b trie.NodeIterator) int {
if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 {
return cmp
}
if a.Leaf() && !b.Leaf() {
return -1
} else if b.Leaf() && !a.Leaf() {
return 1
}
if cmp := bytes.Compare(a.Hash().Bytes(), b.Hash().Bytes()); cmp != 0 {
return cmp
}
if a.Leaf() && b.Leaf() {
return bytes.Compare(a.LeafBlob(), b.LeafBlob())
}
return 0
}
// 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]
}
if len(hex)&1 != 0 {
panic("can't convert hex key of odd length")
}
key := make([]byte, len(hex)/2)
decodeNibbles(hex, key)
return key
}
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]
}
}
// hasTerm returns whether a hex key has the terminator flag.
func hasTerm(s []byte) bool {
return len(s) > 0 && s[len(s)-1] == 16
}