trie: replace custom logic with bytes.HasPrefix (#30771)

in `trie`
- Replace custom logic with `bytes.HasPrefix`
- Remove unnecessary code in `GetNode`
This commit is contained in:
j2gg0s 2024-11-22 16:16:42 +08:00 committed by GitHub
parent 16f2f7155f
commit 6eeff3ee7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 9 deletions

View File

@ -48,7 +48,7 @@ func (t *Trie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
for len(key) > 0 && tn != nil { for len(key) > 0 && tn != nil {
switch n := tn.(type) { switch n := tn.(type) {
case *shortNode: case *shortNode:
if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) { if !bytes.HasPrefix(key, n.Key) {
// The trie doesn't contain the key. // The trie doesn't contain the key.
tn = nil tn = nil
} else { } else {
@ -371,7 +371,7 @@ func unset(parent node, child node, key []byte, pos int, removeLeft bool) error
} }
return unset(cld, cld.Children[key[pos]], key, pos+1, removeLeft) return unset(cld, cld.Children[key[pos]], key, pos+1, removeLeft)
case *shortNode: case *shortNode:
if len(key[pos:]) < len(cld.Key) || !bytes.Equal(cld.Key, key[pos:pos+len(cld.Key)]) { if !bytes.HasPrefix(key[pos:], cld.Key) {
// Find the fork point, it's a non-existent branch. // Find the fork point, it's a non-existent branch.
if removeLeft { if removeLeft {
if bytes.Compare(cld.Key, key[pos:]) < 0 { if bytes.Compare(cld.Key, key[pos:]) < 0 {
@ -434,7 +434,7 @@ func hasRightElement(node node, key []byte) bool {
} }
node, pos = rn.Children[key[pos]], pos+1 node, pos = rn.Children[key[pos]], pos+1
case *shortNode: case *shortNode:
if len(key)-pos < len(rn.Key) || !bytes.Equal(rn.Key, key[pos:pos+len(rn.Key)]) { if !bytes.HasPrefix(key[pos:], rn.Key) {
return bytes.Compare(rn.Key, key[pos:]) > 0 return bytes.Compare(rn.Key, key[pos:]) > 0
} }
node, pos = rn.Val, pos+len(rn.Key) node, pos = rn.Val, pos+len(rn.Key)
@ -589,7 +589,7 @@ func get(tn node, key []byte, skipResolved bool) ([]byte, node) {
for { for {
switch n := tn.(type) { switch n := tn.(type) {
case *shortNode: case *shortNode:
if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) { if !bytes.HasPrefix(key, n.Key) {
return nil, nil return nil, nil
} }
tn = n.Val tn = n.Val

View File

@ -163,7 +163,7 @@ func (t *Trie) get(origNode node, key []byte, pos int) (value []byte, newnode no
case valueNode: case valueNode:
return n, n, false, nil return n, n, false, nil
case *shortNode: case *shortNode:
if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) { if !bytes.HasPrefix(key[pos:], n.Key) {
// key not found in trie // key not found in trie
return nil, n, false, nil return nil, n, false, nil
} }
@ -219,9 +219,6 @@ func (t *Trie) GetNode(path []byte) ([]byte, int, error) {
if resolved > 0 { if resolved > 0 {
t.root = newroot t.root = newroot
} }
if item == nil {
return nil, resolved, nil
}
return item, resolved, nil return item, resolved, nil
} }
@ -254,7 +251,7 @@ func (t *Trie) getNode(origNode node, path []byte, pos int) (item []byte, newnod
return nil, nil, 0, nil return nil, nil, 0, nil
case *shortNode: case *shortNode:
if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) { if !bytes.HasPrefix(path[pos:], n.Key) {
// Path branches off from short node // Path branches off from short node
return nil, n, 0, nil return nil, n, 0, nil
} }