Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Keeper staking tests to use simapp #5683

Merged
merged 95 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
f912618
use simapp into keeper
jgimeno Feb 18, 2020
918949c
refactor TestHistoricalInfo to use simapp
jgimeno Feb 18, 2020
6025132
refactor historical info test
jgimeno Feb 18, 2020
c84c592
temporal commit on querier test, it passes
jgimeno Feb 18, 2020
7f8d0e6
commit end of day refactoring
jgimeno Feb 18, 2020
cdcbecb
rename and temp commit
jgimeno Feb 19, 2020
0d24d2b
fix test query validators
jgimeno Feb 20, 2020
29987a0
make TestQueryDelegations pass with simapp :D
jgimeno Feb 20, 2020
06c1448
rename function
jgimeno Feb 20, 2020
32a27af
make TestQueryRedelegations pass with simapp
jgimeno Feb 20, 2020
fdba5b5
finish keeper refactor for querier in staking using simapp
jgimeno Feb 20, 2020
9eaa5b0
refactor delegation test TestUnbondDelegation to use simapp
jgimeno Feb 21, 2020
27ff208
make all test pass temporary
jgimeno Feb 21, 2020
594e0a6
avoid usage of historicals
jgimeno Feb 21, 2020
e155b10
refactor creation of the simapp
jgimeno Feb 21, 2020
01aaf84
refactor creation of simapp
jgimeno Feb 21, 2020
8978576
comment before creating new way to generate accounts
jgimeno Feb 21, 2020
ee7ccc3
make TestDelegation pass with generated accounts
jgimeno Feb 21, 2020
e5b6780
refactor to use accounts
jgimeno Feb 21, 2020
8cf9219
refactor test unbondingdelegationsmax entries
jgimeno Feb 21, 2020
8378be2
refactor TestUndelegateSelfDelegationBelowMinSelfDelegation to use si…
jgimeno Feb 21, 2020
bbc2b33
update TestUndelegate from unbonding validator and fix bug
jgimeno Feb 24, 2020
6ce1218
refactor TestUndelegateFromUnbondedValidator to use simapp
jgimeno Feb 24, 2020
62dbd8b
TestUnbondingAllDelegationFromValidator to use simapp
jgimeno Feb 24, 2020
9a5aae9
refactor TestGetRedelegationsFromSrcValidator to use simapp
jgimeno Feb 24, 2020
e33844f
refactor TestRedelegation to use simapp
jgimeno Feb 24, 2020
27e2023
refactor TestRedelegateToSameValidator to use simapp
jgimeno Feb 24, 2020
ae44324
refacotr TestRedelegationMaxEntries to use simapp
jgimeno Feb 24, 2020
5bf1fa0
refactor delegation test
jgimeno Feb 24, 2020
a1fcb07
refactor TestRedelegateFromUnbondingValidator to use simapp
jgimeno Feb 24, 2020
17ee1f5
finish refactor delegation test
jgimeno Feb 24, 2020
3910ec2
refactor and remove unused code
jgimeno Feb 24, 2020
fe1012d
migrate revocation of old slash test
jgimeno Feb 24, 2020
a34575a
fix TestSlashUnbondingDelegation test to use simapp
jgimeno Feb 24, 2020
3ea53f5
refactor TestSlashRedelegation to use simapp
jgimeno Feb 24, 2020
7ee50ea
refactor TestSlashAtFutureHeight test
jgimeno Feb 24, 2020
b36cdac
test TestSlashAtNegativeHeight migrated to simapp
jgimeno Feb 24, 2020
a6767d3
migrated two tests from slash_test to use simapp
jgimeno Feb 25, 2020
15eaa12
refactor TestSlashWithRedelegation
jgimeno Feb 25, 2020
0cddd4f
end refactoring slash_test
jgimeno Feb 25, 2020
66abd1f
refactor first test validation to simapp
jgimeno Feb 25, 2020
25681ad
refacor TestUpdateValidatorByPowerIndex to use simapp
jgimeno Feb 25, 2020
385c5c5
refactor TestUpdateBondedValidatorsDecreaseCliff to simapp
jgimeno Feb 25, 2020
ed40111
refactor TestSlashToZeroPowerRemoved
jgimeno Feb 25, 2020
9fa658c
TestValidatorBasics
jgimeno Feb 25, 2020
460619f
refactro TestGetValidatorSortingUnmixed to simapp
jgimeno Feb 25, 2020
c7d68c5
refactor TestGetValidatorSortingMixed test to simap
jgimeno Feb 25, 2020
9955492
refctor TestGetValidatorsEdgeCases to use simapp
jgimeno Feb 25, 2020
96216c3
make test TestValidatorBondHeight pass
jgimeno Feb 25, 2020
b4ab8e5
refactor TestFullValidatorSetPowerChange test
jgimeno Feb 25, 2020
9ae5326
end refactoring validator_test
jgimeno Feb 25, 2020
8f779b1
clean code
jgimeno Feb 25, 2020
632e65f
move methods
jgimeno Feb 25, 2020
b931c49
rename
jgimeno Feb 25, 2020
a44bebc
rename commont test
jgimeno Feb 25, 2020
13f2138
Merge branch 'master' into jonathan/4875-staking-test-use-simapp
jgimeno Feb 25, 2020
194b97b
git remove unused vars
jgimeno Feb 25, 2020
660ffc8
refactor ordering functions
jgimeno Feb 25, 2020
399ea76
refactor old genesis_test to use simapp
jgimeno Feb 26, 2020
315572b
refactor TestValidatorByPowerIndex
jgimeno Feb 26, 2020
59ecf11
refactor TestDuplicatesMsgCreateValidator
jgimeno Feb 26, 2020
030f612
refactor TestInvalidPubKeyTypeMsgCreateValidator
jgimeno Feb 26, 2020
c314b93
temporary commit
jgimeno Feb 26, 2020
ee21805
refactor TestLegacyValidatorDelegations to use simapp
jgimeno Feb 26, 2020
3f319c2
refactor TestIncrementsMsgDelegate
jgimeno Feb 26, 2020
8ecd5b8
refactor next
jgimeno Feb 26, 2020
a07ed83
refactor TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond
jgimeno Feb 26, 2020
46d62eb
refactor TestIncrementsMsgUnbond
jgimeno Feb 26, 2020
d251f64
refator TestMultipleMsgCreateValidator
jgimeno Feb 26, 2020
0e3b958
refactor TestMultipleMsgDelegate
jgimeno Feb 26, 2020
0fcf1e2
refactor TestJailValidator
jgimeno Feb 26, 2020
eb26b47
refactor TestUnbondingPeriod and TestValidatorQueue
jgimeno Feb 26, 2020
c58ec7f
refactor TestUnbondingFromUnbondingValidator and TestRedelegationPeriod
jgimeno Feb 26, 2020
4e16a08
refactor TestTransitiveRedelegation and TestMultipleRedelegationAtSam…
jgimeno Feb 26, 2020
9381500
refactor TestMultipleRedelegationAtUniqueTimes and TestMultipleUnbond…
jgimeno Feb 26, 2020
015fd43
refactor TestMultipleUnbondingDelegationAtUniqueTimes and TestUnbondi…
jgimeno Feb 26, 2020
bbbbc61
end refactor handler_test
jgimeno Feb 26, 2020
64c2971
remove test_common
jgimeno Feb 26, 2020
1d18f88
remove create test public keys
jgimeno Feb 26, 2020
c94a98f
fix based on PR comments
jgimeno Feb 26, 2020
1648edf
use prealloc array for ConvertAddrsToValAddrs
jgimeno Feb 26, 2020
14469cc
fix lint errors
jgimeno Feb 26, 2020
4de26fd
fix lint errors 2
jgimeno Feb 26, 2020
d279066
remove duplicated func
jgimeno Feb 27, 2020
ce46939
rename function names
jgimeno Feb 27, 2020
60293dd
Merge branch 'master' into jonathan/4875-staking-test-use-simapp
jgimeno Feb 27, 2020
2c32ff3
Merge branch 'master' into jonathan/4875-staking-test-use-simapp
jgimeno Feb 28, 2020
256e5fc
Update simapp/test_helpers.go
alexanderbez Feb 28, 2020
9aa2088
Update x/staking/keeper/keeper.go
alexanderbez Feb 28, 2020
03af49b
Update simapp/test_helpers.go
alexanderbez Feb 28, 2020
19546f2
Update simapp/test_helpers.go
alexanderbez Feb 28, 2020
2b688a0
Merge branch 'master' into jonathan/4875-staking-test-use-simapp
alexanderbez Feb 28, 2020
f8442bb
add last touches to the PR
jgimeno Feb 28, 2020
f090da2
edit text
jgimeno Feb 28, 2020
52c6e5e
Merge branch 'master' into jonathan/4875-staking-test-use-simapp
Feb 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package simapp

import (
"bytes"
"encoding/hex"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -187,3 +190,30 @@ func incrementAllSequenceNumbers(initSeqNums []uint64) {
initSeqNums[i]++
}
}

func CreateTestPubKeys(numPubKeys int) []crypto.PubKey {
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
var publicKeys []crypto.PubKey
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
var buffer bytes.Buffer

//start at 10 to avoid changing 1 to 01, 2 to 02, etc
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
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
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
publicKeys = append(publicKeys, NewPubKey(buffer.String()))
buffer.Reset()
}

return publicKeys
}

func NewPubKey(pk string) (res crypto.PubKey) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing godoc.

jgimeno marked this conversation as resolved.
Show resolved Hide resolved
pkBytes, err := hex.DecodeString(pk)
if err != nil {
panic(err)
}
//res, err = crypto.PubKeyFromBytes(pkBytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump

var pkEd ed25519.PubKeyEd25519
copy(pkEd[:], pkBytes)
return pkEd
}
46 changes: 26 additions & 20 deletions x/staking/keeper/historical_info_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package keeper
package keeper_test

import (
"sort"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/cosmos-sdk/simapp"

"github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
)

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))

for i, valAddr := range addrVals {
Expand All @@ -21,27 +26,28 @@ func TestHistoricalInfo(t *testing.T) {

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.Equal(t, hi, recv, "HistoricalInfo not equal")
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.Equal(t, types.HistoricalInfo{}, recv, "HistoricalInfo is not empty")
}

func TestTrackHistoricalInfo(t *testing.T) {
ctx, _, _, k, _ := CreateTestInput(t, false, 10)
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})

// set historical entries in params to 5
params := types.DefaultParams()
params.HistoricalEntries = 5
k.SetParams(ctx, params)
app.StakingKeeper.SetParams(ctx, params)

// set historical info at 5, 4 which should be pruned
// and check that it has been stored
Expand All @@ -59,24 +65,24 @@ func TestTrackHistoricalInfo(t *testing.T) {
}
hi4 := types.NewHistoricalInfo(h4, valSet)
hi5 := types.NewHistoricalInfo(h5, valSet)
k.SetHistoricalInfo(ctx, 4, hi4)
k.SetHistoricalInfo(ctx, 5, hi5)
recv, found := k.GetHistoricalInfo(ctx, 4)
app.StakingKeeper.SetHistoricalInfo(ctx, 4, hi4)
app.StakingKeeper.SetHistoricalInfo(ctx, 5, hi5)
recv, found := app.StakingKeeper.GetHistoricalInfo(ctx, 4)
require.True(t, found)
require.Equal(t, hi4, recv)
recv, found = k.GetHistoricalInfo(ctx, 5)
recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 5)
require.True(t, found)
require.Equal(t, hi5, recv)

// Set last validators in keeper
val1 := types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{})
k.SetValidator(ctx, val1)
k.SetLastValidatorPower(ctx, val1.OperatorAddress, 10)
app.StakingKeeper.SetValidator(ctx, val1)
app.StakingKeeper.SetLastValidatorPower(ctx, val1.OperatorAddress, 10)
val2 := types.NewValidator(sdk.ValAddress(Addrs[3]), PKs[3], types.Description{})
vals := []types.Validator{val1, val2}
sort.Sort(types.Validators(vals))
k.SetValidator(ctx, val2)
k.SetLastValidatorPower(ctx, val2.OperatorAddress, 8)
app.StakingKeeper.SetValidator(ctx, val2)
app.StakingKeeper.SetLastValidatorPower(ctx, val2.OperatorAddress, 8)

// Set Header for BeginBlock context
header := abci.Header{
Expand All @@ -85,22 +91,22 @@ func TestTrackHistoricalInfo(t *testing.T) {
}
ctx = ctx.WithBlockHeader(header)

k.TrackHistoricalInfo(ctx)
app.StakingKeeper.TrackHistoricalInfo(ctx)

// Check HistoricalInfo at height 10 is persisted
expected := types.HistoricalInfo{
Header: header,
Valset: vals,
}
recv, found = k.GetHistoricalInfo(ctx, 10)
recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 10)
require.True(t, found, "GetHistoricalInfo failed after BeginBlock")
require.Equal(t, expected, recv, "GetHistoricalInfo returned eunexpected result")

// Check HistoricalInfo at height 5, 4 is pruned
recv, found = k.GetHistoricalInfo(ctx, 4)
recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 4)
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")
recv, found = k.GetHistoricalInfo(ctx, 5)
recv, found = app.StakingKeeper.GetHistoricalInfo(ctx, 5)
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")
}
14 changes: 9 additions & 5 deletions x/staking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package keeper
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/tendermint/tendermint/abci/types"
)

func TestParams(t *testing.T) {
ctx, _, _, keeper, _ := CreateTestInput(t, false, 0)
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})

expParams := types.DefaultParams()

//check that the empty keeper loads the default
resParams := keeper.GetParams(ctx)
resParams := app.StakingKeeper.GetParams(ctx)
require.True(t, expParams.Equal(resParams))

//modify a params, save, and retrieve
expParams.MaxValidators = 777
keeper.SetParams(ctx, expParams)
resParams = keeper.GetParams(ctx)
app.StakingKeeper.SetParams(ctx, expParams)
resParams = app.StakingKeeper.GetParams(ctx)
require.True(t, expParams.Equal(resParams))
}
Loading