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 // Manage getting and setting accounts
accountMapper sdk.AccountMapper accountMapper sdk.AccountMapper
coinKeeper bank.CoinKeeper
ibcMapper ibc.IBCMapper
stakeKeeper stake.Keeper
} }
func NewGaiaApp(logger log.Logger, dbs map[string]dbm.DB) *GaiaApp { 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 // add handlers
coinKeeper := bank.NewCoinKeeper(app.accountMapper) app.coinKeeper = bank.NewCoinKeeper(app.accountMapper)
ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore) app.ibcMapper = ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
stakeKeeper := stake.NewKeeper(app.cdc, app.capKeyStakeStore, coinKeeper) app.stakeKeeper = stake.NewKeeper(app.cdc, app.capKeyStakeStore, app.coinKeeper)
app.Router(). app.Router().
AddRoute("bank", bank.NewHandler(coinKeeper)). AddRoute("bank", bank.NewHandler(app.coinKeeper)).
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper)). AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
AddRoute("stake", stake.NewHandler(stakeKeeper)) AddRoute("stake", stake.NewHandler(app.stakeKeeper))
// initialize BaseApp // initialize BaseApp
app.SetTxDecoder(app.txDecoder) app.SetTxDecoder(app.txDecoder)
app.SetInitChainer(app.initChainer) 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.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"])
app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"]) app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"])
app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"]) 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, "") // return sdk.ErrGenesisParse("").TraceCause(err, "")
} }
// load the accounts
for _, gacc := range genesisState.Accounts { for _, gacc := range genesisState.Accounts {
acc := gacc.ToAccount() 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{} 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/auth"
"github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/ibc" "github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/stake"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
@ -100,14 +101,15 @@ func newGaiaApp() *GaiaApp {
return NewGaiaApp(logger, dbs) 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)) genaccs := make([]GenesisAccount, len(accs))
for i, acc := range accs { for i, acc := range accs {
genaccs[i] = NewGenesisAccount(acc) genaccs[i] = NewGenesisAccount(acc)
} }
genesisState := GenesisState{ genesisState := GenesisState{
Accounts: genaccs, Accounts: genaccs,
StakeData: stake.GetGenesisJSON(),
} }
stateBytes, err := json.MarshalIndent(genesisState, "", "\t") stateBytes, err := json.MarshalIndent(genesisState, "", "\t")
@ -127,6 +129,7 @@ func setGenesisAccounts(gapp *GaiaApp, accs ...auth.BaseAccount) error {
func TestMsgs(t *testing.T) { func TestMsgs(t *testing.T) {
gapp := newGaiaApp() gapp := newGaiaApp()
assert.Nil(t, setGenesis(gapp))
msgs := []struct { msgs := []struct {
msg sdk.Msg msg sdk.Msg
@ -180,6 +183,7 @@ func TestSortGenesis(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
// Ensure we can send // Ensure we can send
assert.Nil(t, setGenesis(gapp)) // initialize the pool
SignCheckDeliver(t, gapp, sendMsg5, []int64{0}, true, priv1) SignCheckDeliver(t, gapp, sendMsg5, []int64{0}, true, priv1)
} }
@ -192,12 +196,12 @@ func TestGenesis(t *testing.T) {
addr := pk.Address() addr := pk.Address()
coins, err := sdk.ParseCoins("77foocoin,99barcoin") coins, err := sdk.ParseCoins("77foocoin,99barcoin")
require.Nil(t, err) require.Nil(t, err)
baseAcc := auth.BaseAccount{ baseAcc := &auth.BaseAccount{
Address: addr, Address: addr,
Coins: coins, Coins: coins,
} }
err = setGenesisAccounts(gapp, baseAcc) err = setGenesis(gapp, baseAcc)
assert.Nil(t, err) assert.Nil(t, err)
// A checkTx context // A checkTx context
@ -219,13 +223,13 @@ func TestSendMsgWithAccounts(t *testing.T) {
// Give 77 foocoin to the first key // Give 77 foocoin to the first key
coins, err := sdk.ParseCoins("77foocoin") coins, err := sdk.ParseCoins("77foocoin")
require.Nil(t, err) require.Nil(t, err)
baseAcc := auth.BaseAccount{ baseAcc := &auth.BaseAccount{
Address: addr1, Address: addr1,
Coins: coins, Coins: coins,
} }
// Construct genesis state // Construct genesis state
err = setGenesisAccounts(gapp, baseAcc) err = setGenesis(gapp, baseAcc)
assert.Nil(t, err) assert.Nil(t, err)
// A checkTx context (true) // A checkTx context (true)
ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{}) ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{})
@ -259,17 +263,17 @@ func TestSendMsgMultipleOut(t *testing.T) {
genCoins, err := sdk.ParseCoins("42foocoin") genCoins, err := sdk.ParseCoins("42foocoin")
require.Nil(t, err) require.Nil(t, err)
acc1 := auth.BaseAccount{ acc1 := &auth.BaseAccount{
Address: addr1, Address: addr1,
Coins: genCoins, Coins: genCoins,
} }
acc2 := auth.BaseAccount{ acc2 := &auth.BaseAccount{
Address: addr2, Address: addr2,
Coins: genCoins, Coins: genCoins,
} }
err = setGenesisAccounts(gapp, acc1, acc2) err = setGenesis(gapp, acc1, acc2)
assert.Nil(t, err) assert.Nil(t, err)
// Simulate a Block // Simulate a Block
@ -287,22 +291,22 @@ func TestSengMsgMultipleInOut(t *testing.T) {
genCoins, err := sdk.ParseCoins("42foocoin") genCoins, err := sdk.ParseCoins("42foocoin")
require.Nil(t, err) require.Nil(t, err)
acc1 := auth.BaseAccount{ acc1 := &auth.BaseAccount{
Address: addr1, Address: addr1,
Coins: genCoins, Coins: genCoins,
} }
acc2 := auth.BaseAccount{ acc2 := &auth.BaseAccount{
Address: addr2, Address: addr2,
Coins: genCoins, Coins: genCoins,
} }
acc4 := auth.BaseAccount{ acc4 := &auth.BaseAccount{
Address: addr4, Address: addr4,
Coins: genCoins, Coins: genCoins,
} }
err = setGenesisAccounts(gapp, acc1, acc2, acc4) err = setGenesis(gapp, acc1, acc2, acc4)
assert.Nil(t, err) assert.Nil(t, err)
// CheckDeliver // CheckDeliver
@ -321,12 +325,12 @@ func TestSendMsgDependent(t *testing.T) {
genCoins, err := sdk.ParseCoins("42foocoin") genCoins, err := sdk.ParseCoins("42foocoin")
require.Nil(t, err) require.Nil(t, err)
acc1 := auth.BaseAccount{ acc1 := &auth.BaseAccount{
Address: addr1, Address: addr1,
Coins: genCoins, Coins: genCoins,
} }
err = setGenesisAccounts(gapp, acc1) err = setGenesis(gapp, acc1)
assert.Nil(t, err) assert.Nil(t, err)
// CheckDeliver // CheckDeliver
@ -349,12 +353,12 @@ func TestIBCMsgs(t *testing.T) {
sourceChain := "source-chain" sourceChain := "source-chain"
destChain := "dest-chain" destChain := "dest-chain"
baseAcc := auth.BaseAccount{ baseAcc := &auth.BaseAccount{
Address: addr1, Address: addr1,
Coins: coins, Coins: coins,
} }
err := setGenesisAccounts(gapp, baseAcc) err := setGenesis(gapp, baseAcc)
assert.Nil(t, err) assert.Nil(t, err)
// A checkTx context (true) // A checkTx context (true)
ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{}) ctxCheck := gapp.BaseApp.NewContext(true, abci.Header{})

View File

@ -2,6 +2,7 @@ package stake
import ( import (
"bytes" "bytes"
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
@ -35,7 +36,7 @@ func NewHandler(k Keeper) sdk.Handler {
} }
} }
//_______________________________________________ //_____________________________________________________________________
// NewEndBlocker generates sdk.EndBlocker // NewEndBlocker generates sdk.EndBlocker
// Performs tick functionality // 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, // These functions assume everything has been authenticated,
// now we just perform action and save // now we just perform action and save

View File

@ -12,6 +12,15 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" 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 { func newTestMsgDeclareCandidacy(address sdk.Address, pubKey crypto.PubKey, amt int64) MsgDeclareCandidacy {

View File

@ -2,7 +2,6 @@ package stake
import ( import (
"bytes" "bytes"
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "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 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 // get a single candidate

View File

@ -2,7 +2,6 @@ package stake
import ( import (
"bytes" "bytes"
"encoding/json"
"testing" "testing"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -584,31 +583,3 @@ func TestPool(t *testing.T) {
resPool = keeper.GetPool(ctx) resPool = keeper.GetPool(ctx)
assert.Equal(t, expPool, resPool) 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 ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"testing" "testing"
"github.com/stretchr/testify/require" "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 // XXX reference the common declaration of this function
func subspace(prefix []byte) (start, end []byte) { func subspace(prefix []byte) (start, end []byte) {
end = make([]byte, len(prefix)) end = make([]byte, len(prefix))

View File

@ -7,6 +7,14 @@ import (
crypto "github.com/tendermint/go-crypto" 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 // Params defines the high level settings for staking
type Params struct { type Params struct {
InflationRateChange sdk.Rat `json:"inflation_rate_change"` // maximum annual change in inflation rate 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 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 // CandidateStatus - status of a validator-candidate
type CandidateStatus byte type CandidateStatus byte
@ -65,6 +67,9 @@ type Candidate struct {
Description Description `json:"description"` // Description terms for the candidate Description Description `json:"description"` // Description terms for the candidate
} }
// Candidates - list of Candidates
type Candidates []Candidate
// NewCandidate - initialize a new candidate // NewCandidate - initialize a new candidate
func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate { func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Description) Candidate {
return 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 // 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 // owned by one delegator, and is associated with the voting power of one
// pubKey. // pubKey.