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
  • Loading branch information
mossid committed Jul 4, 2018
1 parent 0862563 commit a148093
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,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) // no error

// NOTE: we don't commit, but BeginBlock for block 1
// starts from this deliverState
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,11 +164,13 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
}

// load the initial stake information
stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
validators := stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)

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,7 +238,9 @@ func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci
}

// load the initial stake information
stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
return abci.ResponseInitChain{}
validators := stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
return abci.ResponseInitChain{
Validators: validators,
}

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

stake.InitGenesis(ctx, stakeKeeper, stakeGenesis)
validators := stake.InitGenesis(ctx, stakeKeeper, stakeGenesis)
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,8 +55,11 @@ func getInitChainer(mapp *mock.App, keeper stake.Keeper) sdk.InitChainer {
mapp.InitChainer(ctx, req)
stakeGenesis := stake.DefaultGenesisState()
stakeGenesis.Pool.LooseTokens = 100000
stake.InitGenesis(ctx, keeper, stakeGenesis)
return abci.ResponseInitChain{}
validators := stake.InitGenesis(ctx, keeper, stakeGenesis)

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

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 @@ -62,9 +62,11 @@ func getInitChainer(mapp *mock.App, keeper Keeper) sdk.InitChainer {
stakeGenesis := DefaultGenesisState()
stakeGenesis.Pool.LooseTokens = 100000

InitGenesis(ctx, keeper, stakeGenesis)
validators := InitGenesis(ctx, keeper, stakeGenesis)

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

Expand Down
13 changes: 11 additions & 2 deletions x/stake/genesis.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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"
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) {
func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) (res []abci.Validator) {
keeper.SetPool(ctx, data.Pool)
keeper.SetNewParams(ctx, data.Params)
keeper.InitIntraTxCounter(ctx)
Expand All @@ -33,6 +35,13 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
}

keeper.UpdateBondedValidatorsFull(ctx)

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
42 changes: 42 additions & 0 deletions x/stake/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package stake

import (
"testing"

"github.com/stretchr/testify/assert"

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

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
)

func TestInitGenesis(t *testing.T) {
vals := make([]Validator, 10)
for i, _ := range vals {
vals[i] = NewValidator(
keeper.Addrs[i],
keeper.PKs[i],
Description{},
)
vals[i].PoolShares = PoolShares{
Status: sdk.Bonded,
Amount: sdk.NewRat(int64(i)),
}
}

state := GenesisState{
Validators: vals,
}

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

ctx, _, k := keeper.CreateTestInput(t, false, 100000)

res := InitGenesis(ctx, k, state)

assert.Equal(t, abcivals, res)
}

0 comments on commit a148093

Please sign in to comment.