cosmos-sdk/db/version_manager_test.go
Roy Crihfield 2c31451a55
feat: ADR 040: Implement in-memory DB backend (#9952)
## 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)
2021-08-31 08:09:37 +00:00

60 lines
1.4 KiB
Go

package db_test
import (
"sort"
"testing"
"github.com/stretchr/testify/require"
dbm "github.com/cosmos/cosmos-sdk/db"
)
// Test that VersionManager satisfies the behavior of VersionSet
func TestVersionManager(t *testing.T) {
vm := dbm.NewVersionManager(nil)
require.Equal(t, uint64(0), vm.Last())
require.Equal(t, 0, vm.Count())
require.True(t, vm.Equal(vm))
require.False(t, vm.Exists(0))
id, err := vm.Save(0)
require.NoError(t, err)
require.Equal(t, uint64(1), id)
require.True(t, vm.Exists(id))
id2, err := vm.Save(0)
require.NoError(t, err)
require.True(t, vm.Exists(id2))
id3, err := vm.Save(0)
require.NoError(t, err)
require.True(t, vm.Exists(id3))
id, err = vm.Save(id) // can't save existing id
require.Error(t, err)
id, err = vm.Save(0)
require.NoError(t, err)
require.True(t, vm.Exists(id))
vm.Delete(id)
require.False(t, vm.Exists(id))
vm.Delete(1)
require.False(t, vm.Exists(1))
require.Equal(t, id2, vm.Initial())
require.Equal(t, id3, vm.Last())
var all []uint64
for it := vm.Iterator(); it.Next(); {
all = append(all, it.Value())
}
sort.Slice(all, func(i, j int) bool { return all[i] < all[j] })
require.Equal(t, []uint64{id2, id3}, all)
vmc := vm.Copy()
id, err = vmc.Save(0)
require.NoError(t, err)
require.False(t, vm.Exists(id)) // true copy is made
vm2 := dbm.NewVersionManager([]uint64{id2, id3})
require.True(t, vm.Equal(vm2))
}