fix existing gaia tests

This commit is contained in:
rigelrozanski 2018-04-07 01:50:46 -04:00
parent 179caa5768
commit 8a34b91fac
9 changed files with 127 additions and 111 deletions

View File

@ -1,32 +0,0 @@
package app
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)
// State to Unmarshal
type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"`
}
// GenesisAccount doesn't need pubkey or sequence
type GenesisAccount struct {
Address sdk.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
}
func NewGenesisAccount(acc auth.BaseAccount) GenesisAccount {
return GenesisAccount{
Address: acc.Address,
Coins: acc.Coins,
}
}
// convert GenesisAccount to GaiaAccount
func (ga *GenesisAccount) ToAccount() (acc auth.BaseAccount) {
return auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins.Sort(),
}
}

View File

@ -35,6 +35,9 @@ type GaiaApp struct {
// Manage getting and setting accounts
accountMapper sdk.AccountMapper
coinKeeper bank.CoinKeeper
ibcMapper ibc.IBCMapper
stakeKeeper stake.Keeper
}
func NewGaiaApp(logger log.Logger, dbs map[string]dbm.DB) *GaiaApp {
@ -55,18 +58,18 @@ func NewGaiaApp(logger log.Logger, dbs map[string]dbm.DB) *GaiaApp {
)
// add handlers
coinKeeper := bank.NewCoinKeeper(app.accountMapper)
ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
stakeKeeper := stake.NewKeeper(app.cdc, app.capKeyStakeStore, coinKeeper)
app.coinKeeper = bank.NewCoinKeeper(app.accountMapper)
app.ibcMapper = ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.capKeyStakeStore, app.coinKeeper)
app.Router().
AddRoute("bank", bank.NewHandler(coinKeeper)).
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper)).
AddRoute("stake", stake.NewHandler(stakeKeeper))
AddRoute("bank", bank.NewHandler(app.coinKeeper)).
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
AddRoute("stake", stake.NewHandler(app.stakeKeeper))
// initialize BaseApp
app.SetTxDecoder(app.txDecoder)
app.SetInitChainer(app.initChainer)
app.SetEndBlocker(stake.NewEndBlocker(stakeKeeper))
app.SetEndBlocker(stake.NewEndBlocker(app.stakeKeeper))
app.MountStoreWithDB(app.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"])
app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"])
app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"])
@ -150,9 +153,43 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
// load the accounts
for _, gacc := range genesisState.Accounts {
acc := gacc.ToAccount()
app.accountMapper.SetAccount(ctx, &acc)
app.accountMapper.SetAccount(ctx, acc)
}
// load the initial stake information
stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
return abci.ResponseInitChain{}
}
//__________________________________________________________
// State to Unmarshal
type GenesisState struct {
Accounts []GenesisAccount `json:"accounts"`
StakeData json.RawMessage `json:"stake"`
}
// GenesisAccount doesn't need pubkey or sequence
type GenesisAccount struct {
Address sdk.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
}
func NewGenesisAccount(acc *auth.BaseAccount) GenesisAccount {
return GenesisAccount{
Address: acc.Address,
Coins: acc.Coins,
}
}
// convert GenesisAccount to GaiaAccount
func (ga *GenesisAccount) ToAccount() (acc *auth.BaseAccount) {
return &auth.BaseAccount{
Address: ga.Address,
Coins: ga.Coins.Sort(),
}
}

View File

@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/stake"
abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
@ -100,14 +101,15 @@ func newGaiaApp() *GaiaApp {
return NewGaiaApp(logger, dbs)
}
func setGenesisAccounts(gapp *GaiaApp, accs ...auth.BaseAccount) error {
func setGenesis(gapp *GaiaApp, accs ...*auth.BaseAccount) error {
genaccs := make([]GenesisAccount, len(accs))
for i, acc := range accs {
genaccs[i] = NewGenesisAccount(acc)
}
genesisState := GenesisState{
Accounts: genaccs,
Accounts: genaccs,
StakeData: stake.GetGenesisJSON(),
}
stateBytes, err := json.MarshalIndent(genesisState, "", "\t")
@ -127,6 +129,7 @@ func setGenesisAccounts(gapp *GaiaApp, accs ...auth.BaseAccount) error {
func TestMsgs(t *testing.T) {
gapp := newGaiaApp()
assert.Nil(t, setGenesis(gapp))
msgs := []struct {
msg sdk.Msg
@ -180,6 +183,7 @@ func TestSortGenesis(t *testing.T) {
require.Nil(t, err)
// Ensure we can send
assert.Nil(t, setGenesis(gapp)) // initialize the pool
SignCheckDeliver(t, gapp, sendMsg5, []int64{0}, true, priv1)
}
@ -192,12 +196,12 @@ func TestGenesis(t *testing.T) {
addr := pk.Address()
coins, err := sdk.ParseCoins("77foocoin,99barcoin")
require.Nil(t, err)
baseAcc := auth.BaseAccount{
baseAcc := &auth.BaseAccount{
Address: addr,
Coins: coins,
}
err = setGenesisAccounts(gapp, baseAcc)
err = setGenesis(gapp, baseAcc)
assert.Nil(t, err)
// A checkTx context
@ -219,13 +223,13 @@ func TestSendMsgWithAccounts(t *testing.T) {
// Give 77 foocoin to the first key
coins, err := sdk.ParseCoins("77foocoin")
require.Nil(t, err)
baseAcc := auth.BaseAccount{
baseAcc := &auth.BaseAccount{
Address: addr1,
Coins: coins,
}
// Construct genesis state
err = setGenesisAccounts(gapp, baseAcc)
err = setGenesis(gapp, baseAcc)
assert.Nil(t, err)
// A checkTx context (true)
ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{})
@ -259,17 +263,17 @@ func TestSendMsgMultipleOut(t *testing.T) {
genCoins, err := sdk.ParseCoins("42foocoin")
require.Nil(t, err)
acc1 := auth.BaseAccount{
acc1 := &auth.BaseAccount{
Address: addr1,
Coins: genCoins,
}
acc2 := auth.BaseAccount{
acc2 := &auth.BaseAccount{
Address: addr2,
Coins: genCoins,
}
err = setGenesisAccounts(gapp, acc1, acc2)
err = setGenesis(gapp, acc1, acc2)
assert.Nil(t, err)
// Simulate a Block
@ -287,22 +291,22 @@ func TestSengMsgMultipleInOut(t *testing.T) {
genCoins, err := sdk.ParseCoins("42foocoin")
require.Nil(t, err)
acc1 := auth.BaseAccount{
acc1 := &auth.BaseAccount{
Address: addr1,
Coins: genCoins,
}
acc2 := auth.BaseAccount{
acc2 := &auth.BaseAccount{
Address: addr2,
Coins: genCoins,
}
acc4 := auth.BaseAccount{
acc4 := &auth.BaseAccount{
Address: addr4,
Coins: genCoins,
}
err = setGenesisAccounts(gapp, acc1, acc2, acc4)
err = setGenesis(gapp, acc1, acc2, acc4)
assert.Nil(t, err)
// CheckDeliver
@ -321,12 +325,12 @@ func TestSendMsgDependent(t *testing.T) {
genCoins, err := sdk.ParseCoins("42foocoin")
require.Nil(t, err)
acc1 := auth.BaseAccount{
acc1 := &auth.BaseAccount{
Address: addr1,
Coins: genCoins,
}
err = setGenesisAccounts(gapp, acc1)
err = setGenesis(gapp, acc1)
assert.Nil(t, err)
// CheckDeliver
@ -349,12 +353,12 @@ func TestIBCMsgs(t *testing.T) {
sourceChain := "source-chain"
destChain := "dest-chain"
baseAcc := auth.BaseAccount{
baseAcc := &auth.BaseAccount{
Address: addr1,
Coins: coins,
}
err := setGenesisAccounts(gapp, baseAcc)
err := setGenesis(gapp, baseAcc)
assert.Nil(t, err)
// A checkTx context (true)
ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{})

View File

@ -2,6 +2,7 @@ package stake
import (
"bytes"
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types"
@ -35,7 +36,7 @@ func NewHandler(k Keeper) sdk.Handler {
}
}
//_______________________________________________
//_____________________________________________________________________
// NewEndBlocker generates sdk.EndBlocker
// Performs tick functionality
@ -48,6 +49,19 @@ func NewEndBlocker(k Keeper) sdk.EndBlocker {
//_____________________________________________________________________
// InitGenesis - store genesis parameters
func InitGenesis(ctx sdk.Context, k Keeper, data json.RawMessage) error {
var state GenesisState
if err := json.Unmarshal(data, &state); err != nil {
return err
}
k.setPool(ctx, state.Pool)
k.setParams(ctx, state.Params)
return nil
}
//_____________________________________________________________________
// These functions assume everything has been authenticated,
// now we just perform action and save

View File

@ -12,6 +12,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
func TestInitGenesis(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
encoded := GetGenesisJSON()
err := InitGenesis(ctx, keeper, encoded)
require.Nil(t, err)
require.Equal(t, keeper.GetPool(ctx), initialPool())
require.Equal(t, keeper.GetParams(ctx), defaultParams())
}
//______________________________________________________________________
func newTestMsgDeclareCandidacy(address sdk.Address, pubKey crypto.PubKey, amt int64) MsgDeclareCandidacy {

View File

@ -2,7 +2,6 @@ package stake
import (
"bytes"
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
@ -34,17 +33,6 @@ func NewKeeper(ctx sdk.Context, cdc *wire.Codec, key sdk.StoreKey, ck bank.CoinK
return keeper
}
// InitGenesis - store genesis parameters
func (k Keeper) InitGenesis(ctx sdk.Context, data json.RawMessage) error {
var state GenesisState
if err := json.Unmarshal(data, &state); err != nil {
return err
}
k.setPool(ctx, state.Pool)
k.setParams(ctx, state.Params)
return nil
}
//_________________________________________________________________________
// get a single candidate

View File

@ -2,7 +2,6 @@ package stake
import (
"bytes"
"encoding/json"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -584,31 +583,3 @@ func TestPool(t *testing.T) {
resPool = keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool)
}
func TestInitGenesis(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
jsonStr := `{
"params": {
"inflation_rate_change": {"num": 13, "denom": 100},
"inflation_max": {"num": 20, "denom": 100},
"inflation_min": {"num": 7, "denom": 100},
"goal_bonded": {"num": 67, "denom": 100},
"max_validators": 100,
"bond_denom": "fermion"
},
"pool": {
"total_supply": 0,
"bonded_shares": {"num": 0, "denom": 1},
"unbonded_shares": {"num": 0, "denom": 1},
"bonded_pool": 0,
"unbonded_pool": 0,
"inflation_last_time": 0,
"inflation": {"num": 7, "denom": 100}
}
}`
encoded := json.RawMessage(jsonStr)
err := keeper.InitGenesis(ctx, encoded)
require.Nil(t, err)
require.Equal(t, keeper.GetPool(ctx), initialPool())
require.Equal(t, keeper.GetParams(ctx), defaultParams())
}

View File

@ -2,6 +2,7 @@ package stake
import (
"encoding/hex"
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
@ -75,6 +76,30 @@ func initialPool() Pool {
}
}
// get raw genesis raw message for testing
func GetGenesisJSON() json.RawMessage {
jsonStr := `{
"params": {
"inflation_rate_change": {"num": 13, "denom": 100},
"inflation_max": {"num": 20, "denom": 100},
"inflation_min": {"num": 7, "denom": 100},
"goal_bonded": {"num": 67, "denom": 100},
"max_validators": 100,
"bond_denom": "fermion"
},
"pool": {
"total_supply": 0,
"bonded_shares": {"num": 0, "denom": 1},
"unbonded_shares": {"num": 0, "denom": 1},
"bonded_pool": 0,
"unbonded_pool": 0,
"inflation_last_time": 0,
"inflation": {"num": 7, "denom": 100}
}
}`
return json.RawMessage(jsonStr)
}
// XXX reference the common declaration of this function
func subspace(prefix []byte) (start, end []byte) {
end = make([]byte, len(prefix))

View File

@ -7,6 +7,14 @@ import (
crypto "github.com/tendermint/go-crypto"
)
// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
Pool Pool `json:"pool"`
Params Params `json:"params"`
}
//_________________________________________________________________________
// Params defines the high level settings for staking
type Params struct {
InflationRateChange sdk.Rat `json:"inflation_rate_change"` // maximum annual change in inflation rate
@ -31,13 +39,7 @@ type Pool struct {
Inflation sdk.Rat `json:"inflation"` // current annual inflation rate
}
// GenesisState - all staking state that must be provided at genesis
type GenesisState struct {
Pool Pool `json:"pool"`
Params Params `json:"params"`
}
//_______________________________________________________________________________________________________
//_________________________________________________________________________
// CandidateStatus - status of a validator-candidate
type CandidateStatus byte
@ -65,6 +67,9 @@ type Candidate struct {
Description Description `json:"description"` // Description terms for the candidate
}
// Candidates - list of Candidates
type Candidates []Candidate
// NewCandidate - initialize a new candidate
func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate {
return Candidate{
@ -151,11 +156,6 @@ func (v Validator) abciValidatorZero(cdc *wire.Codec) abci.Validator {
//_________________________________________________________________________
// Candidates - list of Candidates
type Candidates []Candidate
//_________________________________________________________________________
// DelegatorBond represents the bond with tokens held by an account. It is
// owned by one delegator, and is associated with the voting power of one
// pubKey.