## Description
Resolves: https://github.com/cosmos/cosmos-sdk/issues/10117
Implements a `CommitKVStore` which separates the concerns of state storage and state commitment according to [ADR-040](eb7d939f86/docs/architecture/adr-040-storage-and-smt-state-commitments.md).
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - n/a
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
113 lines
2.3 KiB
Go
113 lines
2.3 KiB
Go
package prefix
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
|
|
dbm "github.com/cosmos/cosmos-sdk/db"
|
|
)
|
|
|
|
// IteratePrefix is a convenience function for iterating over a key domain
|
|
// restricted by prefix.
|
|
func IteratePrefix(db dbm.DBReader, prefix []byte) (dbm.Iterator, error) {
|
|
var start, end []byte
|
|
if len(prefix) != 0 {
|
|
start = prefix
|
|
end = cpIncr(prefix)
|
|
}
|
|
itr, err := db.Iterator(start, end)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return itr, nil
|
|
}
|
|
|
|
// Strips prefix while iterating from Iterator.
|
|
type prefixDBIterator struct {
|
|
prefix []byte
|
|
start []byte
|
|
end []byte
|
|
source dbm.Iterator
|
|
err error
|
|
}
|
|
|
|
var _ dbm.Iterator = (*prefixDBIterator)(nil)
|
|
|
|
func newPrefixIterator(prefix, start, end []byte, source dbm.Iterator) *prefixDBIterator {
|
|
return &prefixDBIterator{
|
|
prefix: prefix,
|
|
start: start,
|
|
end: end,
|
|
source: source,
|
|
}
|
|
}
|
|
|
|
// Domain implements Iterator.
|
|
func (itr *prefixDBIterator) Domain() (start, end []byte) {
|
|
return itr.start, itr.end
|
|
}
|
|
|
|
func (itr *prefixDBIterator) valid() bool {
|
|
if itr.err != nil {
|
|
return false
|
|
}
|
|
|
|
key := itr.source.Key()
|
|
if len(key) < len(itr.prefix) || !bytes.Equal(key[:len(itr.prefix)], itr.prefix) {
|
|
itr.err = fmt.Errorf("received invalid key from backend: %x (expected prefix %x)",
|
|
key, itr.prefix)
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// Next implements Iterator.
|
|
func (itr *prefixDBIterator) Next() bool {
|
|
if !itr.source.Next() {
|
|
return false
|
|
}
|
|
key := itr.source.Key()
|
|
if !bytes.HasPrefix(key, itr.prefix) {
|
|
return false
|
|
}
|
|
// Empty keys are not allowed, so if a key exists in the database that exactly matches the
|
|
// prefix we need to skip it.
|
|
if bytes.Equal(key, itr.prefix) {
|
|
return itr.Next()
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Next implements Iterator.
|
|
func (itr *prefixDBIterator) Key() []byte {
|
|
itr.assertIsValid()
|
|
key := itr.source.Key()
|
|
return key[len(itr.prefix):] // we have checked the key in Valid()
|
|
}
|
|
|
|
// Value implements Iterator.
|
|
func (itr *prefixDBIterator) Value() []byte {
|
|
itr.assertIsValid()
|
|
return itr.source.Value()
|
|
}
|
|
|
|
// Error implements Iterator.
|
|
func (itr *prefixDBIterator) Error() error {
|
|
if err := itr.source.Error(); err != nil {
|
|
return err
|
|
}
|
|
return itr.err
|
|
}
|
|
|
|
// Close implements Iterator.
|
|
func (itr *prefixDBIterator) Close() error {
|
|
return itr.source.Close()
|
|
}
|
|
|
|
func (itr *prefixDBIterator) assertIsValid() {
|
|
if !itr.valid() {
|
|
panic("iterator is invalid")
|
|
}
|
|
}
|