refactor TestHistoricalInfo to use simapp

This commit is contained in:
Jonathan Gimeno 2020-02-18 14:00:59 +01:00
parent f912618b8a
commit 918949cf23
2 changed files with 176 additions and 76 deletions

View File

@ -0,0 +1,97 @@
package keeper_test
import (
"bytes"
"encoding/hex"
"strconv"
"github.com/tendermint/tendermint/crypto/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
)
var (
Addrs = createTestAddrs(500)
PKs = createTestPubKeys(500)
addrDels = []sdk.AccAddress{
Addrs[0],
Addrs[1],
}
addrVals = []sdk.ValAddress{
sdk.ValAddress(Addrs[2]),
sdk.ValAddress(Addrs[3]),
sdk.ValAddress(Addrs[4]),
sdk.ValAddress(Addrs[5]),
sdk.ValAddress(Addrs[6]),
}
)
func createTestAddrs(numAddrs int) []sdk.AccAddress {
var addresses []sdk.AccAddress
var buffer bytes.Buffer
// start at 100 so we can make up to 999 test addresses with valid test addresses
for i := 100; i < (numAddrs + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") //base address string
buffer.WriteString(numString) //adding on final two digits to make addresses unique
res, _ := sdk.AccAddressFromHex(buffer.String())
bech := res.String()
addresses = append(addresses, CreateTestAddr(buffer.String(), bech))
buffer.Reset()
}
return addresses
}
// nolint: unparam
func createTestPubKeys(numPubKeys int) []crypto.PubKey {
var publicKeys []crypto.PubKey
var buffer bytes.Buffer
//start at 10 to avoid changing 1 to 01, 2 to 02, etc
for i := 100; i < (numPubKeys + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") //base pubkey string
buffer.WriteString(numString) //adding on final two digits to make pubkeys unique
publicKeys = append(publicKeys, NewPubKey(buffer.String()))
buffer.Reset()
}
return publicKeys
}
func NewPubKey(pk string) (res crypto.PubKey) {
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
//res, err = crypto.PubKeyFromBytes(pkBytes)
var pkEd ed25519.PubKeyEd25519
copy(pkEd[:], pkBytes)
return pkEd
}
// for incode address generation
func CreateTestAddr(addr string, bech string) sdk.AccAddress {
res, err := sdk.AccAddressFromHex(addr)
if err != nil {
panic(err)
}
bechexpected := res.String()
if bech != bechexpected {
panic("Bech encoding doesn't match reference")
}
bechres, err := sdk.AccAddressFromBech32(bech)
if err != nil {
panic(err)
}
if !bytes.Equal(bechres, res) {
panic("Bech decode and hex decode don't match")
}
return res
}

View File

@ -1,10 +1,11 @@
package keeper package keeper_test
import ( import (
"sort" "sort"
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -12,7 +13,9 @@ import (
) )
func TestHistoricalInfo(t *testing.T) { func TestHistoricalInfo(t *testing.T) {
ctx, _, _, keeper, _ := CreateTestInput(t, false, 10) app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})
validators := make([]types.Validator, len(addrVals)) validators := make([]types.Validator, len(addrVals))
for i, valAddr := range addrVals { for i, valAddr := range addrVals {
@ -21,86 +24,86 @@ func TestHistoricalInfo(t *testing.T) {
hi := types.NewHistoricalInfo(ctx.BlockHeader(), validators) hi := types.NewHistoricalInfo(ctx.BlockHeader(), validators)
keeper.SetHistoricalInfo(ctx, 2, hi) app.StakingKeeper.SetHistoricalInfo(ctx, 2, hi)
recv, found := keeper.GetHistoricalInfo(ctx, 2) recv, found := app.StakingKeeper.GetHistoricalInfo(ctx, 2)
require.True(t, found, "HistoricalInfo not found after set") require.True(t, found, "HistoricalInfo not found after set")
require.Equal(t, hi, recv, "HistoricalInfo not equal") require.Equal(t, hi, recv, "HistoricalInfo not equal")
require.True(t, sort.IsSorted(types.Validators(recv.Valset)), "HistoricalInfo validators is not sorted") require.True(t, sort.IsSorted(types.Validators(recv.Valset)), "HistoricalInfo validators is not sorted")
keeper.DeleteHistoricalInfo(ctx, 2) app.StakingKeeper.DeleteHistoricalInfo(ctx, 2)
recv, found = keeper.GetHistoricalInfo(ctx, 2) recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 2)
require.False(t, found, "HistoricalInfo found after delete") require.False(t, found, "HistoricalInfo found after delete")
require.Equal(t, types.HistoricalInfo{}, recv, "HistoricalInfo is not empty") require.Equal(t, types.HistoricalInfo{}, recv, "HistoricalInfo is not empty")
} }
func TestTrackHistoricalInfo(t *testing.T) { //func TestTrackHistoricalInfo(t *testing.T) {
ctx, _, _, k, _ := CreateTestInput(t, false, 10) // ctx, _, _, k, _ := CreateTestInput(t, false, 10)
//
// set historical entries in params to 5 // // set historical entries in params to 5
params := types.DefaultParams() // params := types.DefaultParams()
params.HistoricalEntries = 5 // params.HistoricalEntries = 5
k.SetParams(ctx, params) // k.SetParams(ctx, params)
//
// set historical info at 5, 4 which should be pruned // // set historical info at 5, 4 which should be pruned
// and check that it has been stored // // and check that it has been stored
h4 := abci.Header{ // h4 := abci.Header{
ChainID: "HelloChain", // ChainID: "HelloChain",
Height: 4, // Height: 4,
} // }
h5 := abci.Header{ // h5 := abci.Header{
ChainID: "HelloChain", // ChainID: "HelloChain",
Height: 5, // Height: 5,
} // }
valSet := []types.Validator{ // valSet := []types.Validator{
types.NewValidator(sdk.ValAddress(Addrs[0]), PKs[0], types.Description{}), // types.NewValidator(sdk.ValAddress(Addrs[0]), PKs[0], types.Description{}),
types.NewValidator(sdk.ValAddress(Addrs[1]), PKs[1], types.Description{}), // types.NewValidator(sdk.ValAddress(Addrs[1]), PKs[1], types.Description{}),
} // }
hi4 := types.NewHistoricalInfo(h4, valSet) // hi4 := types.NewHistoricalInfo(h4, valSet)
hi5 := types.NewHistoricalInfo(h5, valSet) // hi5 := types.NewHistoricalInfo(h5, valSet)
k.SetHistoricalInfo(ctx, 4, hi4) // k.SetHistoricalInfo(ctx, 4, hi4)
k.SetHistoricalInfo(ctx, 5, hi5) // k.SetHistoricalInfo(ctx, 5, hi5)
recv, found := k.GetHistoricalInfo(ctx, 4) // recv, found := k.GetHistoricalInfo(ctx, 4)
require.True(t, found) // require.True(t, found)
require.Equal(t, hi4, recv) // require.Equal(t, hi4, recv)
recv, found = k.GetHistoricalInfo(ctx, 5) // recv, found = k.GetHistoricalInfo(ctx, 5)
require.True(t, found) // require.True(t, found)
require.Equal(t, hi5, recv) // require.Equal(t, hi5, recv)
//
// Set last validators in keeper // // Set last validators in keeper
val1 := types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{}) // val1 := types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{})
k.SetValidator(ctx, val1) // k.SetValidator(ctx, val1)
k.SetLastValidatorPower(ctx, val1.OperatorAddress, 10) // k.SetLastValidatorPower(ctx, val1.OperatorAddress, 10)
val2 := types.NewValidator(sdk.ValAddress(Addrs[3]), PKs[3], types.Description{}) // val2 := types.NewValidator(sdk.ValAddress(Addrs[3]), PKs[3], types.Description{})
vals := []types.Validator{val1, val2} // vals := []types.Validator{val1, val2}
sort.Sort(types.Validators(vals)) // sort.Sort(types.Validators(vals))
k.SetValidator(ctx, val2) // k.SetValidator(ctx, val2)
k.SetLastValidatorPower(ctx, val2.OperatorAddress, 8) // k.SetLastValidatorPower(ctx, val2.OperatorAddress, 8)
//
// Set Header for BeginBlock context // // Set Header for BeginBlock context
header := abci.Header{ // header := abci.Header{
ChainID: "HelloChain", // ChainID: "HelloChain",
Height: 10, // Height: 10,
} // }
ctx = ctx.WithBlockHeader(header) // ctx = ctx.WithBlockHeader(header)
//
k.TrackHistoricalInfo(ctx) // k.TrackHistoricalInfo(ctx)
//
// Check HistoricalInfo at height 10 is persisted // // Check HistoricalInfo at height 10 is persisted
expected := types.HistoricalInfo{ // expected := types.HistoricalInfo{
Header: header, // Header: header,
Valset: vals, // Valset: vals,
} // }
recv, found = k.GetHistoricalInfo(ctx, 10) // recv, found = k.GetHistoricalInfo(ctx, 10)
require.True(t, found, "GetHistoricalInfo failed after BeginBlock") // require.True(t, found, "GetHistoricalInfo failed after BeginBlock")
require.Equal(t, expected, recv, "GetHistoricalInfo returned eunexpected result") // require.Equal(t, expected, recv, "GetHistoricalInfo returned eunexpected result")
//
// Check HistoricalInfo at height 5, 4 is pruned // // Check HistoricalInfo at height 5, 4 is pruned
recv, found = k.GetHistoricalInfo(ctx, 4) // recv, found = k.GetHistoricalInfo(ctx, 4)
require.False(t, found, "GetHistoricalInfo did not prune earlier height") // require.False(t, found, "GetHistoricalInfo did not prune earlier height")
require.Equal(t, types.HistoricalInfo{}, recv, "GetHistoricalInfo at height 4 is not empty after prune") // require.Equal(t, types.HistoricalInfo{}, recv, "GetHistoricalInfo at height 4 is not empty after prune")
recv, found = k.GetHistoricalInfo(ctx, 5) // recv, found = k.GetHistoricalInfo(ctx, 5)
require.False(t, found, "GetHistoricalInfo did not prune first prune height") // require.False(t, found, "GetHistoricalInfo did not prune first prune height")
require.Equal(t, types.HistoricalInfo{}, recv, "GetHistoricalInfo at height 5 is not empty after prune") // require.Equal(t, types.HistoricalInfo{}, recv, "GetHistoricalInfo at height 5 is not empty after prune")
} //}