Skip to content

Commit

Permalink
Merge pull request #1373: Initialization of POS chain
Browse files Browse the repository at this point in the history
pass lint

apply requests

fix test

fix abci dep

fix TestValidatorsQuery

rm mistake file

apply requests

fix lint
  • Loading branch information
mossid committed Jul 13, 2018
1 parent 98bc419 commit 9f12e67
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 46 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
if app.initChainer == nil {
return
}
app.initChainer(app.deliverState.ctx, req) // no error
res = app.initChainer(app.deliverState.ctx, req)

// NOTE: we don't commit, but BeginBlock for block 1
// starts from this deliverState
Expand Down
21 changes: 8 additions & 13 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,25 +359,20 @@ func TestTxs(t *testing.T) {
}

func TestValidatorsQuery(t *testing.T) {
cleanup, pks, port := InitializeTestLCD(t, 2, []sdk.AccAddress{})
cleanup, pks, port := InitializeTestLCD(t, 1, []sdk.AccAddress{})
defer cleanup()
require.Equal(t, 2, len(pks))
require.Equal(t, 1, len(pks))

validators := getValidators(t, port)
require.Equal(t, len(validators), 2)
require.Equal(t, len(validators), 1)

// make sure all the validators were found (order unknown because sorted by owner addr)
foundVal1, foundVal2 := false, false
pk1Bech := sdk.MustBech32ifyValPub(pks[0])
pk2Bech := sdk.MustBech32ifyValPub(pks[1])
if validators[0].PubKey == pk1Bech || validators[1].PubKey == pk1Bech {
foundVal1 = true
foundVal := false
pkBech := sdk.MustBech32ifyValPub(pks[0])
if validators[0].PubKey == pkBech {
foundVal = true
}
if validators[0].PubKey == pk2Bech || validators[1].PubKey == pk2Bech {
foundVal2 = true
}
require.True(t, foundVal1, "pk1Bech %v, owner1 %v, owner2 %v", pk1Bech, validators[0].Owner, validators[1].Owner)
require.True(t, foundVal2, "pk2Bech %v, owner1 %v, owner2 %v", pk2Bech, validators[0].Owner, validators[1].Owner)
require.True(t, foundVal, "pkBech %v, owner %v", pkBech, validators[0].Owner)
}

func TestBonding(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,17 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
}

// load the initial stake information
err = stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
validators, err := stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}

gov.InitGenesis(ctx, app.govKeeper, gov.DefaultGenesisState())

return abci.ResponseInitChain{}
return abci.ResponseInitChain{
Validators: validators,
}
}

// export the state of gaia for a genesis file
Expand Down
6 changes: 4 additions & 2 deletions cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
}

// load the initial stake information
err = stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
validators, err := stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 // return sdk.ErrGenesisParse("").TraceCause(err, "")
}

return abci.ResponseInitChain{}
return abci.ResponseInitChain{
Validators: validators,
}
}
17 changes: 7 additions & 10 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ var (

// genesis piece structure for creating combined genesis
type GenesisTx struct {
NodeID string `json:"node_id"`
IP string `json:"ip"`
Validator tmtypes.GenesisValidator `json:"validator"`
AppGenTx json.RawMessage `json:"app_gen_tx"`
NodeID string `json:"node_id"`
IP string `json:"ip"`
AppGenTx json.RawMessage `json:"app_gen_tx"`
}

// Storage for init command input parameters
Expand Down Expand Up @@ -121,16 +120,15 @@ func gentxWithConfig(cdc *wire.Codec, appInit AppInit, config *cfg.Config, genTx
nodeID := string(nodeKey.ID())
pubKey := readOrCreatePrivValidator(config)

appGenTx, cliPrint, validator, err := appInit.AppGenTx(cdc, pubKey, genTxConfig)
appGenTx, cliPrint, _, err := appInit.AppGenTx(cdc, pubKey, genTxConfig)
if err != nil {
return
}

tx := GenesisTx{
NodeID: nodeID,
IP: genTxConfig.IP,
Validator: validator,
AppGenTx: appGenTx,
NodeID: nodeID,
IP: genTxConfig.IP,
AppGenTx: appGenTx,
}
bz, err := wire.MarshalJSONIndent(cdc, tx)
if err != nil {
Expand Down Expand Up @@ -302,7 +300,6 @@ func processGenTxs(genTxsDir string, cdc *wire.Codec) (
genTx := genTxs[nodeID]

// combine some stuff
validators = append(validators, genTx.Validator)
appGenTxs = append(appGenTxs, genTx.AppGenTx)

// Add a persistent peer
Expand Down
6 changes: 4 additions & 2 deletions x/gov/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ func getInitChainer(mapp *mock.App, keeper Keeper, stakeKeeper stake.Keeper) sdk
stakeGenesis := stake.DefaultGenesisState()
stakeGenesis.Pool.LooseTokens = 100000

err := stake.InitGenesis(ctx, stakeKeeper, stakeGenesis)
validators, err := stake.InitGenesis(ctx, stakeKeeper, stakeGenesis)
if err != nil {
panic(err)
}
InitGenesis(ctx, keeper, DefaultGenesisState())
return abci.ResponseInitChain{}
return abci.ResponseInitChain{
Validators: validators,
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions x/slashing/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ func getInitChainer(mapp *mock.App, keeper stake.Keeper) sdk.InitChainer {
mapp.InitChainer(ctx, req)
stakeGenesis := stake.DefaultGenesisState()
stakeGenesis.Pool.LooseTokens = 100000
err := stake.InitGenesis(ctx, keeper, stakeGenesis)
validators, err := stake.InitGenesis(ctx, keeper, stakeGenesis)
if err != nil {
panic(err)
}
return abci.ResponseInitChain{}

return abci.ResponseInitChain{
Validators: validators,
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/slashing/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func createTestInput(t *testing.T) (sdk.Context, bank.Keeper, stake.Keeper, Keep
sk := stake.NewKeeper(cdc, keyStake, ck, stake.DefaultCodespace)
genesis := stake.DefaultGenesisState()
genesis.Pool.LooseTokens = initCoins.MulRaw(int64(len(addrs))).Int64()
err = stake.InitGenesis(ctx, sk, genesis)
_, err = stake.InitGenesis(ctx, sk, genesis)
require.Nil(t, err)

for _, addr := range addrs {
Expand Down
6 changes: 4 additions & 2 deletions x/stake/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ func getInitChainer(mapp *mock.App, keeper Keeper) sdk.InitChainer {
stakeGenesis := DefaultGenesisState()
stakeGenesis.Pool.LooseTokens = 100000

err := InitGenesis(ctx, keeper, stakeGenesis)
validators, err := InitGenesis(ctx, keeper, stakeGenesis)
if err != nil {
panic(err)
}

return abci.ResponseInitChain{}
return abci.ResponseInitChain{
Validators: validators,
}
}
}

Expand Down
19 changes: 14 additions & 5 deletions x/stake/genesis.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package stake

import (
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/pkg/errors"
tmtypes "github.com/tendermint/tendermint/types"
)

// InitGenesis sets the pool and parameters for the provided keeper and
// initializes the IntraTxCounter. For each validator in data, it sets that
// validator in the keeper along with manually setting the indexes. In
// addition, it also sets any delegations found in data. Finally, it updates
// the bonded validators.
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) error {
// Returns final validator set after applying all declaration and delegations
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.Validator, err error) {
keeper.SetPool(ctx, data.Pool)
keeper.SetNewParams(ctx, data.Params)
keeper.InitIntraTxCounter(ctx)
Expand All @@ -21,10 +24,10 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) error
keeper.SetValidator(ctx, validator)

if validator.PoolShares.Amount.IsZero() {
return errors.Errorf("genesis validator cannot have zero pool shares, validator: %v", validator)
return res, errors.Errorf("genesis validator cannot have zero pool shares, validator: %v", validator)
}
if validator.DelegatorShares.IsZero() {
return errors.Errorf("genesis validator cannot have zero delegator shares, validator: %v", validator)
return res, errors.Errorf("genesis validator cannot have zero delegator shares, validator: %v", validator)
}

// Manually set indexes for the first time
Expand All @@ -41,7 +44,13 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) error
}

keeper.UpdateBondedValidatorsFull(ctx)
return nil

vals := keeper.GetAllValidators(ctx)
res = make([]abci.Validator, len(vals))
for i, val := range vals {
res[i] = sdk.ABCIValidator(val)
}
return
}

// WriteGenesis returns a GenesisState for a given context and keeper. The
Expand Down
20 changes: 14 additions & 6 deletions x/stake/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
keep "github.com/cosmos/cosmos-sdk/x/stake/keeper"
"github.com/cosmos/cosmos-sdk/x/stake/types"
Expand All @@ -24,14 +26,20 @@ func TestInitGenesis(t *testing.T) {
NewValidator(keep.Addrs[0], keep.PKs[0], Description{Moniker: "hoop"}),
}

genesisState := types.NewGenesisState(pool, params, validators, delegations)
err := InitGenesis(ctx, keeper, genesisState)
require.Error(t, err)

// Setting status to bonded since for sdk.ABCIValidator
// dosen't affect on actual execution
validators[0].PoolShares.Status = sdk.Bonded
validators[0].PoolShares.Amount = sdk.OneRat()
validators[0].DelegatorShares = sdk.OneRat()

genesisState = types.NewGenesisState(pool, params, validators, delegations)
err = InitGenesis(ctx, keeper, genesisState)
genesisState := types.NewGenesisState(pool, params, validators, delegations)
vals, err := InitGenesis(ctx, keeper, genesisState)
require.NoError(t, err)

abcivals := make([]abci.Validator, len(vals))
for i, val := range validators {
abcivals[i] = sdk.ABCIValidator(val)
}

require.Equal(t, abcivals, vals)
}

0 comments on commit 9f12e67

Please sign in to comment.