Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Distr-simulation OnValidatorPowerDidChange panic fix #2632

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ IMPROVEMENTS
- \#1924 [simulation] Use a transition matrix for block size
- #2610 [x/stake] Block redelegation to and from the same validator


* Tendermint


Expand Down
11 changes: 10 additions & 1 deletion x/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
Expand Down Expand Up @@ -42,7 +44,14 @@ func (k Keeper) onValidatorBonded(ctx sdk.Context, valAddr sdk.ValAddress) {
func (k Keeper) onValidatorPowerDidChange(ctx sdk.Context, valAddr sdk.ValAddress) {
vi := k.GetValidatorDistInfo(ctx, valAddr)
if vi.FeePoolWithdrawalHeight != ctx.BlockHeight() {
panic("expected validator dist info FeePoolWithdrawalHeight to be updated, but was not.")
panic(fmt.Sprintf("validator dist info withdraw height not updated:\n"+
"\tOperator Address: \t\t%v\n"+
"\tFeePoolWithdrawalHeight: \t%v\n"+
"\tBlockHeight: \t\t\t%v\n",
vi.OperatorAddr.String(),
vi.FeePoolWithdrawalHeight,
ctx.BlockHeight(),
))
}
}

Expand Down
4 changes: 4 additions & 0 deletions x/mock/simulation/random_simulate_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp,

// Run the BeginBlock handler
logWriter("BeginBlock")
fmt.Printf("BeginBlock: %v\n", i+1)
app.BeginBlock(request)

if testingMode {
Expand Down Expand Up @@ -167,6 +168,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp,
header.Time = header.Time.Add(time.Duration(minTimePerBlock) * time.Second).Add(time.Duration(int64(r.Intn(int(timeDiff)))) * time.Second)
header.ProposerAddress = randomProposer(r, validators)
logWriter("EndBlock")
fmt.Printf("EndBlock: %v\n", i+1)

if testingMode {
// Make sure invariants hold at end of block
Expand Down Expand Up @@ -237,6 +239,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params
lastBlocksizeState, blocksize = getBlockSize(r, params, lastBlocksizeState, avgBlockSize)
for j := 0; j < blocksize; j++ {
logUpdate, futureOps, err := selectOp(r)(r, app, ctx, accounts, event)
//fmt.Printf("\t\t\t%v\n", logUpdate)
logWriter(logUpdate)
if err != nil {
displayLogs()
Expand All @@ -258,6 +261,7 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params
}
}

// TODO annotate of remove this function
func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) {
testingMode = false
if _t, ok := tb.(*testing.T); ok {
Expand Down
3 changes: 1 addition & 2 deletions x/slashing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func (h Hooks) OnValidatorBeginUnbonding(ctx sdk.Context, consAddr sdk.ConsAddre
}

// nolint - unused hooks
func (h Hooks) OnValidatorPowerDidChange(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) {
}
func (h Hooks) OnValidatorPowerDidChange(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}
func (h Hooks) OnValidatorCreated(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) OnValidatorModified(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) OnValidatorRemoved(_ sdk.Context, _ sdk.ValAddress) {}
Expand Down
23 changes: 23 additions & 0 deletions x/stake/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func handleMsgEditValidator(ctx sdk.Context, msg types.MsgEditValidator, k keepe
}
validator.Commission = commission
k.OnValidatorModified(ctx, msg.ValidatorAddr)

// defensive check
k.OnValidatorPowerDidChange(ctx, validator.ConsAddress(), validator.OperatorAddr)
}

k.SetValidator(ctx, validator)
Expand Down Expand Up @@ -184,6 +187,9 @@ func handleMsgDelegate(ctx sdk.Context, msg types.MsgDelegate, k keeper.Keeper)
return err.Result()
}

// defensive check
k.OnValidatorPowerDidChange(ctx, validator.ConsAddress(), validator.OperatorAddr)

tags := sdk.NewTags(
tags.Action, tags.ActionDelegate,
tags.Delegator, []byte(msg.DelegatorAddr.String()),
Expand All @@ -203,6 +209,12 @@ func handleMsgBeginUnbonding(ctx sdk.Context, msg types.MsgBeginUnbonding, k kee

finishTime := types.MsgCdc.MustMarshalBinary(ubd.MinTime)

// defensive check
validator, found := k.GetValidator(ctx, msg.ValidatorAddr)
if found {
k.OnValidatorPowerDidChange(ctx, validator.ConsAddress(), validator.OperatorAddr)
}

tags := sdk.NewTags(
tags.Action, tags.ActionBeginUnbonding,
tags.Delegator, []byte(msg.DelegatorAddr.String()),
Expand All @@ -221,6 +233,17 @@ func handleMsgBeginRedelegate(ctx sdk.Context, msg types.MsgBeginRedelegate, k k

finishTime := types.MsgCdc.MustMarshalBinary(red.MinTime)

// defensive checks
validatorSrc, found := k.GetValidator(ctx, msg.ValidatorSrcAddr)
if found {
k.OnValidatorPowerDidChange(ctx, validatorSrc.ConsAddress(), validatorSrc.OperatorAddr)
}
validatorDst, found := k.GetValidator(ctx, msg.ValidatorDstAddr)
if !found {
panic("dst validator is no longer found")
}
k.OnValidatorPowerDidChange(ctx, validatorDst.ConsAddress(), validatorDst.OperatorAddr)

tags := sdk.NewTags(
tags.Action, tags.ActionBeginRedelegation,
tags.Delegator, []byte(msg.DelegatorAddr.String()),
Expand Down
2 changes: 2 additions & 0 deletions x/stake/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeight int64, power int64, slashFactor sdk.Dec) {
logger := ctx.Logger().With("module", "x/stake")

fmt.Printf("debug slashing consAddr: %v\n", consAddr.String())

if slashFactor.LT(sdk.ZeroDec()) {
panic(fmt.Errorf("attempted to slash with a negative slash factor: %v", slashFactor))
}
Expand Down
33 changes: 19 additions & 14 deletions x/stake/keeper/val_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
break
}

//if validator.OperatorAddr.String() == "cosmosvaloper1xrhemcpkjnyzxjpjzpjysucayy62v4gqur9lqz" {
//fmt.Printf("\nVALSTATE validator: %v\n", validator)
//}

// apply the appropriate state change if necessary
switch validator.Status {
case sdk.Unbonded:
Expand All @@ -78,11 +82,21 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx sdk.Context) (updates []ab
if !found || !bytes.Equal(oldPowerBytes, newPowerBytes) {
updates = append(updates, validator.ABCIValidatorUpdate())

if validator.OperatorAddr.String() == "cosmosvaloper1c0cejfs3ndjnempxckay3quktgsca943qfxrva" {
fmt.Printf("debug found: %v\n", found)
if found {
newPowerInt := sdk.NewInt(newPower)
var oldPowerInt sdk.Int
k.cdc.MustUnmarshalBinary(oldPowerBytes, &oldPowerInt)
fmt.Printf("\n\t height %v\n", ctx.BlockHeight())
fmt.Printf("\t old power: %v\n\t new power: %v\n", oldPowerInt.String(), newPowerInt.String())
fmt.Printf("\t old power bytes: %x\n\t new power bytes: %x\n", oldPowerBytes, newPowerBytes)
}
}

// XXX Assert that the validator had updated its ValidatorDistInfo.FeePoolWithdrawalHeight.
// XXX This hook probably shouldn't exist. Maybe rethink the hook system.
if k.hooks != nil {
k.hooks.OnValidatorPowerDidChange(ctx, validator.ConsAddress(), valAddr)
}
k.OnValidatorPowerDidChange(ctx, validator.ConsAddress(), valAddr)

// set validator power on lookup index.
k.SetLastValidatorPower(ctx, valAddr, sdk.NewInt(newPower))
Expand Down Expand Up @@ -198,14 +212,9 @@ func (k Keeper) bondValidator(ctx sdk.Context, validator types.Validator) types.

// save the now bonded validator record to the three referenced stores
k.SetValidator(ctx, validator)

k.SetValidatorByPowerIndex(ctx, validator, pool)

// call the bond hook if present
if k.hooks != nil {
k.hooks.OnValidatorBonded(ctx, validator.ConsAddress(), validator.OperatorAddr)
}

k.OnValidatorBonded(ctx, validator.ConsAddress(), validator.OperatorAddr)
return validator
}

Expand Down Expand Up @@ -237,11 +246,7 @@ func (k Keeper) beginUnbondingValidator(ctx sdk.Context, validator types.Validat
// Adds to unbonding validator queue
k.InsertValidatorQueue(ctx, validator)

// call the unbond hook if present
if k.hooks != nil {
k.hooks.OnValidatorBeginUnbonding(ctx, validator.ConsAddress(), validator.OperatorAddr)
}

k.OnValidatorBeginUnbonding(ctx, validator.ConsAddress(), validator.OperatorAddr)
return validator
}

Expand Down