Merge PR #4729: Extend DiffKVStores to return a list of KVPairs
This commit is contained in:
committed by
Alexander Bezobchuk
parent
b45a94a54e
commit
b17f8f8df7
+3
-3
@@ -499,9 +499,9 @@ func TestAppImportExport(t *testing.T) {
|
||||
prefixes := storeKeysPrefix.Prefixes
|
||||
storeA := ctxA.KVStore(storeKeyA)
|
||||
storeB := ctxB.KVStore(storeKeyB)
|
||||
kvA, kvB, count, equal := sdk.DiffKVStores(storeA, storeB, prefixes)
|
||||
fmt.Printf("Compared %d key/value pairs between %s and %s\n", count, storeKeyA, storeKeyB)
|
||||
require.True(t, equal, GetSimulationLog(storeKeyA.Name(), app.cdc, newApp.cdc, kvA, kvB))
|
||||
failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes)
|
||||
fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB)
|
||||
require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.cdc, newApp.cdc, failedKVs))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+29
-22
@@ -496,31 +496,38 @@ func GenStakingGenesisState(
|
||||
|
||||
// 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, cdcA, cdcB *codec.Codec, kvA, kvB cmn.KVPair) (log string) {
|
||||
log = fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value)
|
||||
func GetSimulationLog(storeName string, cdcA, cdcB *codec.Codec, kvs []cmn.KVPair) (log string) {
|
||||
var kvA, kvB cmn.KVPair
|
||||
for i := 0; i < len(kvs); i += 2 {
|
||||
kvA = kvs[i]
|
||||
kvB = kvs[i+1]
|
||||
|
||||
if len(kvA.Value) == 0 && len(kvB.Value) == 0 {
|
||||
return
|
||||
if len(kvA.Value) == 0 && len(kvB.Value) == 0 {
|
||||
// skip if the value doesn't have any bytes
|
||||
continue
|
||||
}
|
||||
|
||||
switch storeName {
|
||||
case auth.StoreKey:
|
||||
log += DecodeAccountStore(cdcA, cdcB, kvA, kvB)
|
||||
case mint.StoreKey:
|
||||
log += DecodeMintStore(cdcA, cdcB, kvA, kvB)
|
||||
case staking.StoreKey:
|
||||
log += DecodeStakingStore(cdcA, cdcB, kvA, kvB)
|
||||
case slashing.StoreKey:
|
||||
log += DecodeSlashingStore(cdcA, cdcB, kvA, kvB)
|
||||
case gov.StoreKey:
|
||||
log += DecodeGovStore(cdcA, cdcB, kvA, kvB)
|
||||
case distribution.StoreKey:
|
||||
log += DecodeDistributionStore(cdcA, cdcB, kvA, kvB)
|
||||
case supply.StoreKey:
|
||||
log += DecodeSupplyStore(cdcA, cdcB, kvA, kvB)
|
||||
default:
|
||||
log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvA.Key, kvA.Value, kvB.Key, kvB.Value)
|
||||
}
|
||||
}
|
||||
|
||||
switch storeName {
|
||||
case auth.StoreKey:
|
||||
return DecodeAccountStore(cdcA, cdcB, kvA, kvB)
|
||||
case mint.StoreKey:
|
||||
return DecodeMintStore(cdcA, cdcB, kvA, kvB)
|
||||
case staking.StoreKey:
|
||||
return DecodeStakingStore(cdcA, cdcB, kvA, kvB)
|
||||
case slashing.StoreKey:
|
||||
return DecodeSlashingStore(cdcA, cdcB, kvA, kvB)
|
||||
case gov.StoreKey:
|
||||
return DecodeGovStore(cdcA, cdcB, kvA, kvB)
|
||||
case distribution.StoreKey:
|
||||
return DecodeDistributionStore(cdcA, cdcB, kvA, kvB)
|
||||
case supply.StoreKey:
|
||||
return DecodeSupplyStore(cdcA, cdcB, kvA, kvB)
|
||||
default:
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DecodeAccountStore unmarshals the KVPair's Value to the corresponding auth type
|
||||
|
||||
+36
-12
@@ -48,23 +48,47 @@ func TestGetSimulationLog(t *testing.T) {
|
||||
cdc := makeTestCodec()
|
||||
|
||||
tests := []struct {
|
||||
store string
|
||||
kvPair cmn.KVPair
|
||||
store string
|
||||
kvPairs []cmn.KVPair
|
||||
}{
|
||||
{auth.StoreKey, cmn.KVPair{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(auth.BaseAccount{})}},
|
||||
{mint.StoreKey, cmn.KVPair{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(mint.Minter{})}},
|
||||
{staking.StoreKey, cmn.KVPair{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()}},
|
||||
{gov.StoreKey, cmn.KVPair{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(gov.Vote{})}},
|
||||
{distribution.StoreKey, cmn.KVPair{Key: distr.ProposerKey, Value: consAddr1.Bytes()}},
|
||||
{slashing.StoreKey, cmn.KVPair{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(true)}},
|
||||
{supply.StoreKey, cmn.KVPair{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(supply.NewSupply(sdk.Coins{}))}},
|
||||
{"Empty", cmn.KVPair{}},
|
||||
{"OtherStore", cmn.KVPair{Key: []byte("key"), Value: []byte("value")}},
|
||||
{auth.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(auth.BaseAccount{})},
|
||||
cmn.KVPair{Key: auth.AddressStoreKey(delAddr1), Value: cdc.MustMarshalBinaryBare(auth.BaseAccount{})},
|
||||
}},
|
||||
{mint.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(mint.Minter{})},
|
||||
cmn.KVPair{Key: mint.MinterKey, Value: cdc.MustMarshalBinaryLengthPrefixed(mint.Minter{})},
|
||||
}},
|
||||
{staking.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()},
|
||||
cmn.KVPair{Key: staking.LastValidatorPowerKey, Value: valAddr1.Bytes()},
|
||||
}},
|
||||
{gov.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(gov.Vote{})},
|
||||
cmn.KVPair{Key: gov.VoteKey(1, delAddr1), Value: cdc.MustMarshalBinaryLengthPrefixed(gov.Vote{})},
|
||||
}},
|
||||
{distribution.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: distr.ProposerKey, Value: consAddr1.Bytes()},
|
||||
cmn.KVPair{Key: distr.ProposerKey, Value: consAddr1.Bytes()},
|
||||
}},
|
||||
{slashing.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(true)},
|
||||
cmn.KVPair{Key: slashing.GetValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryLengthPrefixed(true)},
|
||||
}},
|
||||
{supply.StoreKey, []cmn.KVPair{
|
||||
cmn.KVPair{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(supply.NewSupply(sdk.Coins{}))},
|
||||
cmn.KVPair{Key: supply.SupplyKey, Value: cdc.MustMarshalBinaryLengthPrefixed(supply.NewSupply(sdk.Coins{}))},
|
||||
}},
|
||||
{"Empty", []cmn.KVPair{cmn.KVPair{}, cmn.KVPair{}}},
|
||||
{"OtherStore", []cmn.KVPair{
|
||||
cmn.KVPair{Key: []byte("key"), Value: []byte("value")},
|
||||
cmn.KVPair{Key: []byte("key"), Value: []byte("other_value")},
|
||||
}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.store, func(t *testing.T) {
|
||||
require.NotPanics(t, func() { GetSimulationLog(tt.store, cdc, cdc, tt.kvPair, tt.kvPair) }, tt.store)
|
||||
require.NotPanics(t, func() { GetSimulationLog(tt.store, cdc, cdc, tt.kvPairs) }, tt.store)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user