trie: do error-checks prior to setting last/first in stacktrie

This commit is contained in:
Martin Holst Swende 2023-10-23 18:34:21 +02:00
parent 566818c629
commit c0ae61fa15
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -98,21 +98,21 @@ func (t *StackTrie) Update(key, value []byte) error {
}
k := keybytesToHex(key)
k = k[:len(k)-1] // chop the termination flag
if bytes.Compare(t.last, k) >= 0 {
return errors.New("non-ascending key order")
}
if err := t.insert(t.root, k, value, nil); err != nil {
return err
}
// track the first and last inserted entries.
if t.first == nil {
t.first = append([]byte{}, k...)
}
if bytes.Compare(t.last, k) >= 0 {
return errors.New("non-ascending key order")
}
if t.last == nil {
t.last = append([]byte{}, k...) // allocate key slice
} else {
t.last = append(t.last[:0], k...) // reuse key slice
}
if err := t.insert(t.root, k, value, nil); err != nil {
return err
}
return nil
}