## Description
Implements an in-memory backend for the DB interface introduced by https://github.com/cosmos/cosmos-sdk/pull/9573 and specified by [ADR-040](eb7d939f86/docs/architecture/adr-040-storage-and-smt-state-commitments.md). This expands on the [btree](https://pkg.go.dev/github.com/google/btree)-based [`MemDB`](https://github.com/tendermint/tm-db/tree/master/memdb) from `tm-db` by using copy-on-write clones to implement versioning.
Resolves: https://github.com/vulcanize/cosmos-sdk/issues/2
Will move out of draft once https://github.com/cosmos/cosmos-sdk/pull/9573 is merged and rebased on.
### 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
- [x] 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)
110 lines
2.3 KiB
Go
110 lines
2.3 KiB
Go
package dbtest
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
dbm "github.com/cosmos/cosmos-sdk/db"
|
|
)
|
|
|
|
func Int64ToBytes(i int64) []byte {
|
|
buf := make([]byte, 8)
|
|
binary.BigEndian.PutUint64(buf, uint64(i))
|
|
return buf
|
|
}
|
|
|
|
func BytesToInt64(buf []byte) int64 {
|
|
return int64(binary.BigEndian.Uint64(buf))
|
|
}
|
|
|
|
func BenchmarkRangeScans(b *testing.B, db dbm.DBReadWriter, dbSize int64) {
|
|
b.StopTimer()
|
|
|
|
rangeSize := int64(10000)
|
|
if dbSize < rangeSize {
|
|
b.Errorf("db size %v cannot be less than range size %v", dbSize, rangeSize)
|
|
}
|
|
|
|
for i := int64(0); i < dbSize; i++ {
|
|
bytes := Int64ToBytes(i)
|
|
err := db.Set(bytes, bytes)
|
|
if err != nil {
|
|
// require.NoError() is very expensive (according to profiler), so check manually
|
|
b.Fatal(b, err)
|
|
}
|
|
}
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
start := rand.Int63n(dbSize - rangeSize) // nolint: gosec
|
|
end := start + rangeSize
|
|
iter, err := db.Iterator(Int64ToBytes(start), Int64ToBytes(end))
|
|
require.NoError(b, err)
|
|
count := 0
|
|
for iter.Next() {
|
|
count++
|
|
}
|
|
iter.Close()
|
|
require.EqualValues(b, rangeSize, count)
|
|
}
|
|
}
|
|
|
|
func BenchmarkRandomReadsWrites(b *testing.B, db dbm.DBReadWriter) {
|
|
b.StopTimer()
|
|
|
|
// create dummy data
|
|
const numItems = int64(1000000)
|
|
internal := map[int64]int64{}
|
|
for i := 0; i < int(numItems); i++ {
|
|
internal[int64(i)] = int64(0)
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
{
|
|
idx := rand.Int63n(numItems) // nolint: gosec
|
|
internal[idx]++
|
|
val := internal[idx]
|
|
idxBytes := Int64ToBytes(idx)
|
|
valBytes := Int64ToBytes(val)
|
|
err := db.Set(idxBytes, valBytes)
|
|
if err != nil {
|
|
// require.NoError() is very expensive (according to profiler), so check manually
|
|
b.Fatal(b, err)
|
|
}
|
|
}
|
|
|
|
{
|
|
idx := rand.Int63n(numItems) // nolint: gosec
|
|
valExp := internal[idx]
|
|
idxBytes := Int64ToBytes(idx)
|
|
valBytes, err := db.Get(idxBytes)
|
|
if err != nil {
|
|
b.Fatal(b, err)
|
|
}
|
|
if valExp == 0 {
|
|
if !bytes.Equal(valBytes, nil) {
|
|
b.Errorf("Expected %v for %v, got %X", nil, idx, valBytes)
|
|
break
|
|
}
|
|
} else {
|
|
if len(valBytes) != 8 {
|
|
b.Errorf("Expected length 8 for %v, got %X", idx, valBytes)
|
|
break
|
|
}
|
|
valGot := BytesToInt64(valBytes)
|
|
if valExp != valGot {
|
|
b.Errorf("Expected %v for %v, got %v", valExp, idx, valGot)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|