refactor: migrate calls from alias file to appropriate store/types (#14455)
Co-authored-by: Marko <marbar3778@yahoo.com> Closes https://github.com/cosmos/cosmos-sdk/issues/14406
This commit is contained in:
@@ -1,18 +1,21 @@
|
||||
package sims
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
dbm "github.com/cosmos/cosmos-db"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
)
|
||||
|
||||
// SetupSimulation creates the config, db (levelDB), temporary directory and logger for the simulation tests.
|
||||
@@ -106,7 +109,7 @@ func PrintStats(db dbm.DB) {
|
||||
|
||||
// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the
|
||||
// each's module store key and the prefix bytes of the KVPair's key.
|
||||
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs []kv.Pair) (log string) {
|
||||
func GetSimulationLog(storeName string, sdr storetypes.StoreDecoderRegistry, kvAs, kvBs []kv.Pair) (log string) {
|
||||
for i := 0; i < len(kvAs); i++ {
|
||||
if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 {
|
||||
// skip if the value doesn't have any bytes
|
||||
@@ -123,3 +126,59 @@ func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs
|
||||
|
||||
return log
|
||||
}
|
||||
|
||||
// DiffKVStores compares two KVstores and returns all the key/value pairs
|
||||
// that differ from one another. It also skips value comparison for a set of provided prefixes.
|
||||
func DiffKVStores(a storetypes.KVStore, b storetypes.KVStore, prefixesToSkip [][]byte) (kvAs, kvBs []kv.Pair) {
|
||||
iterA := a.Iterator(nil, nil)
|
||||
|
||||
defer iterA.Close()
|
||||
|
||||
iterB := b.Iterator(nil, nil)
|
||||
|
||||
defer iterB.Close()
|
||||
|
||||
for {
|
||||
if !iterA.Valid() && !iterB.Valid() {
|
||||
return kvAs, kvBs
|
||||
}
|
||||
|
||||
var kvA, kvB kv.Pair
|
||||
if iterA.Valid() {
|
||||
kvA = kv.Pair{Key: iterA.Key(), Value: iterA.Value()}
|
||||
|
||||
iterA.Next()
|
||||
}
|
||||
|
||||
if iterB.Valid() {
|
||||
kvB = kv.Pair{Key: iterB.Key(), Value: iterB.Value()}
|
||||
}
|
||||
|
||||
compareValue := true
|
||||
|
||||
for _, prefix := range prefixesToSkip {
|
||||
// Skip value comparison if we matched a prefix
|
||||
if bytes.HasPrefix(kvA.Key, prefix) {
|
||||
compareValue = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !compareValue {
|
||||
// We're skipping this key due to an exclusion prefix. If it's present in B, iterate past it. If it's
|
||||
// absent don't iterate.
|
||||
if bytes.Equal(kvA.Key, kvB.Key) {
|
||||
iterB.Next()
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// always iterate B when comparing
|
||||
iterB.Next()
|
||||
|
||||
if !bytes.Equal(kvA.Key, kvB.Key) || !bytes.Equal(kvA.Value, kvB.Value) {
|
||||
kvAs = append(kvAs, kvA)
|
||||
kvBs = append(kvBs, kvB)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,22 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
dbm "github.com/cosmos/cosmos-db"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/store/metrics"
|
||||
"github.com/cosmos/cosmos-sdk/store/rootmulti"
|
||||
|
||||
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
)
|
||||
|
||||
func TestGetSimulationLog(t *testing.T) {
|
||||
legacyAmino := codec.NewLegacyAmino()
|
||||
decoders := make(sdk.StoreDecoderRegistry)
|
||||
decoders := make(storetypes.StoreDecoderRegistry)
|
||||
decoders[authtypes.StoreKey] = func(kvAs, kvBs kv.Pair) string { return "10" }
|
||||
|
||||
tests := []struct {
|
||||
@@ -46,3 +52,58 @@ func TestGetSimulationLog(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiffKVStores(t *testing.T) {
|
||||
store1, store2 := initTestStores(t)
|
||||
// Two equal stores
|
||||
k1, v1 := []byte("k1"), []byte("v1")
|
||||
store1.Set(k1, v1)
|
||||
store2.Set(k1, v1)
|
||||
|
||||
checkDiffResults(t, store1, store2)
|
||||
|
||||
// delete k1 from store2, which is now empty
|
||||
store2.Delete(k1)
|
||||
checkDiffResults(t, store1, store2)
|
||||
|
||||
// set k1 in store2, different value than what store1 holds for k1
|
||||
v2 := []byte("v2")
|
||||
store2.Set(k1, v2)
|
||||
checkDiffResults(t, store1, store2)
|
||||
|
||||
// add k2 to store2
|
||||
k2 := []byte("k2")
|
||||
store2.Set(k2, v2)
|
||||
checkDiffResults(t, store1, store2)
|
||||
|
||||
// Reset stores
|
||||
store1.Delete(k1)
|
||||
store2.Delete(k1)
|
||||
store2.Delete(k2)
|
||||
|
||||
// Same keys, different value. Comparisons will be nil as prefixes are skipped.
|
||||
prefix := []byte("prefix:")
|
||||
k1Prefixed := append(prefix, k1...) //nolint:gocritic // append is fine here
|
||||
store1.Set(k1Prefixed, v1)
|
||||
store2.Set(k1Prefixed, v2)
|
||||
checkDiffResults(t, store1, store2)
|
||||
}
|
||||
|
||||
func checkDiffResults(t *testing.T, store1, store2 storetypes.KVStore) {
|
||||
kvAs1, kvBs1 := DiffKVStores(store1, store2, nil)
|
||||
kvAs2, kvBs2 := DiffKVStores(store1, store2, nil)
|
||||
assert.DeepEqual(t, kvAs1, kvAs2)
|
||||
assert.DeepEqual(t, kvBs1, kvBs2)
|
||||
}
|
||||
|
||||
func initTestStores(t *testing.T) (storetypes.KVStore, storetypes.KVStore) {
|
||||
db := dbm.NewMemDB()
|
||||
ms := rootmulti.NewStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
|
||||
|
||||
key1 := storetypes.NewKVStoreKey("store1")
|
||||
key2 := storetypes.NewKVStoreKey("store2")
|
||||
require.NotPanics(t, func() { ms.MountStoreWithDB(key1, storetypes.StoreTypeIAVL, db) })
|
||||
require.NotPanics(t, func() { ms.MountStoreWithDB(key2, storetypes.StoreTypeIAVL, db) })
|
||||
require.NotPanics(t, func() { ms.LoadLatestVersion() })
|
||||
return ms.GetKVStore(key1), ms.GetKVStore(key2)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user