2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-07 14:24:04 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-06-21 17:19:14 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-07-03 21:21:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mock"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// initialize the mock application for this module
|
2019-02-05 21:30:48 -08:00
|
|
|
func getMockApp(t *testing.T, numGenAccs int, genState GenesisState, genAccs []auth.Account) (
|
|
|
|
mapp *mock.App, keeper Keeper, sk staking.Keeper, addrs []sdk.AccAddress,
|
|
|
|
pubKeys []crypto.PubKey, privKeys []crypto.PrivKey) {
|
|
|
|
|
2019-01-07 14:24:04 -08:00
|
|
|
mapp = mock.NewApp()
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
staking.RegisterCodec(mapp.Cdc)
|
2018-09-13 11:17:32 -07:00
|
|
|
RegisterCodec(mapp.Cdc)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
keyStaking := sdk.NewKVStoreKey(staking.StoreKey)
|
|
|
|
tkeyStaking := sdk.NewTransientStoreKey(staking.TStoreKey)
|
2018-12-19 11:58:30 -08:00
|
|
|
keyGov := sdk.NewKVStoreKey(StoreKey)
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2018-12-20 11:09:43 -08:00
|
|
|
pk := mapp.ParamsKeeper
|
2019-01-28 19:06:48 -08:00
|
|
|
ck := bank.NewBaseKeeper(mapp.AccountKeeper, mapp.ParamsKeeper.Subspace(bank.DefaultParamspace), bank.DefaultCodespace)
|
2019-01-11 12:08:01 -08:00
|
|
|
sk = staking.NewKeeper(mapp.Cdc, keyStaking, tkeyStaking, ck, pk.Subspace(staking.DefaultParamspace), staking.DefaultCodespace)
|
2019-01-07 14:24:04 -08:00
|
|
|
keeper = NewKeeper(mapp.Cdc, keyGov, pk, pk.Subspace("testgov"), ck, sk, DefaultCodespace)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-12-19 11:58:30 -08:00
|
|
|
mapp.Router().AddRoute(RouterKey, NewHandler(keeper))
|
|
|
|
mapp.QueryRouter().AddRoute(QuerierRoute, NewQuerier(keeper))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
mapp.SetEndBlocker(getEndBlocker(keeper))
|
2019-01-07 14:24:04 -08:00
|
|
|
mapp.SetInitChainer(getInitChainer(mapp, keeper, sk, genState))
|
2018-06-21 17:19:14 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
require.NoError(t, mapp.CompleteSetup(keyStaking, tkeyStaking, keyGov))
|
2018-07-31 21:23:25 -07:00
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
valTokens := sdk.TokensFromTendermintPower(42)
|
2019-01-07 14:24:04 -08:00
|
|
|
if genAccs == nil || len(genAccs) == 0 {
|
2019-02-05 21:30:48 -08:00
|
|
|
genAccs, addrs, pubKeys, privKeys = mock.CreateGenAccounts(numGenAccs,
|
2019-02-13 15:01:50 -08:00
|
|
|
sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, valTokens)})
|
2019-01-07 14:24:04 -08:00
|
|
|
}
|
2018-07-31 21:23:25 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
mock.SetGenesis(mapp, genAccs)
|
|
|
|
|
|
|
|
return mapp, keeper, sk, addrs, pubKeys, privKeys
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
// gov and staking endblocker
|
2018-06-21 17:19:14 -07:00
|
|
|
func getEndBlocker(keeper Keeper) sdk.EndBlocker {
|
2019-04-05 13:20:58 -07:00
|
|
|
return func(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
|
|
|
tags := EndBlocker(ctx, keeper)
|
2018-06-21 17:19:14 -07:00
|
|
|
return abci.ResponseEndBlock{
|
|
|
|
Tags: tags,
|
2019-04-05 13:20:58 -07:00
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
// gov and staking initchainer
|
|
|
|
func getInitChainer(mapp *mock.App, keeper Keeper, stakingKeeper staking.Keeper, genState GenesisState) sdk.InitChainer {
|
2018-06-21 17:19:14 -07:00
|
|
|
return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
|
|
|
mapp.InitChainer(ctx, req)
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingGenesis := staking.DefaultGenesisState()
|
2019-02-13 15:01:50 -08:00
|
|
|
tokens := sdk.TokensFromTendermintPower(100000)
|
2019-02-05 21:30:48 -08:00
|
|
|
stakingGenesis.Pool.NotBondedTokens = tokens
|
2018-06-26 19:00:12 -07:00
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
validators, err := staking.InitGenesis(ctx, stakingKeeper, stakingGenesis)
|
2018-07-09 19:51:13 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-07 14:24:04 -08:00
|
|
|
if genState.IsEmpty() {
|
|
|
|
InitGenesis(ctx, keeper, DefaultGenesisState())
|
|
|
|
} else {
|
|
|
|
InitGenesis(ctx, keeper, genState)
|
|
|
|
}
|
2018-07-18 23:39:40 -07:00
|
|
|
return abci.ResponseInitChain{
|
|
|
|
Validators: validators,
|
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:44 -07:00
|
|
|
// TODO: Remove once address interface has been implemented (ref: #2186)
|
|
|
|
func SortValAddresses(addrs []sdk.ValAddress) {
|
|
|
|
var byteAddrs [][]byte
|
|
|
|
for _, addr := range addrs {
|
|
|
|
byteAddrs = append(byteAddrs, addr.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
SortByteArrays(byteAddrs)
|
|
|
|
|
|
|
|
for i, byteAddr := range byteAddrs {
|
|
|
|
addrs[i] = byteAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
// Sorts Addresses
|
2018-07-06 00:06:53 -07:00
|
|
|
func SortAddresses(addrs []sdk.AccAddress) {
|
2018-06-21 17:19:14 -07:00
|
|
|
var byteAddrs [][]byte
|
|
|
|
for _, addr := range addrs {
|
|
|
|
byteAddrs = append(byteAddrs, addr.Bytes())
|
|
|
|
}
|
|
|
|
SortByteArrays(byteAddrs)
|
|
|
|
for i, byteAddr := range byteAddrs {
|
|
|
|
addrs[i] = byteAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// implement `Interface` in sort package.
|
|
|
|
type sortByteArrays [][]byte
|
|
|
|
|
|
|
|
func (b sortByteArrays) Len() int {
|
|
|
|
return len(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b sortByteArrays) Less(i, j int) bool {
|
|
|
|
// bytes package already implements Comparable for []byte.
|
|
|
|
switch bytes.Compare(b[i], b[j]) {
|
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 0, 1:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b sortByteArrays) Swap(i, j int) {
|
|
|
|
b[j], b[i] = b[i], b[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public
|
|
|
|
func SortByteArrays(src [][]byte) [][]byte {
|
|
|
|
sorted := sortByteArrays(src)
|
|
|
|
sort.Sort(sorted)
|
|
|
|
return sorted
|
|
|
|
}
|
2019-03-15 09:47:47 -07:00
|
|
|
|
|
|
|
func testProposal() TextProposal {
|
|
|
|
return NewTextProposal("Test", "description")
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks if two proposals are equal (note: slow, for tests only)
|
|
|
|
func ProposalEqual(proposalA Proposal, proposalB Proposal) bool {
|
|
|
|
return bytes.Equal(msgCdc.MustMarshalBinaryBare(proposalA), msgCdc.MustMarshalBinaryBare(proposalB))
|
|
|
|
}
|