From 130f3560a74a1756898198111ffd67ccf8b419c7 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Fri, 19 Jul 2019 16:05:37 +0200 Subject: [PATCH] Merge PR #4729: Extend DiffKVStores to return a list of KVPairs --- sim_test.go | 6 +++--- utils.go | 51 +++++++++++++++++++++++++++++---------------------- utils_test.go | 48 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 68 insertions(+), 37 deletions(-) diff --git a/sim_test.go b/sim_test.go index 59a9b6d4ad41..9a45bbd4c0d5 100644 --- a/sim_test.go +++ b/sim_test.go @@ -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)) } } diff --git a/utils.go b/utils.go index 0624b4e9fd13..a19de40f3424 100644 --- a/utils.go +++ b/utils.go @@ -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 { + // skip if the value doesn't have any bytes + continue + } - if len(kvA.Value) == 0 && len(kvB.Value) == 0 { - return + 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 diff --git a/utils_test.go b/utils_test.go index b76189728ec5..2fb0fbab82db 100644 --- a/utils_test.go +++ b/utils_test.go @@ -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) }) } }