Skip to content

Commit

Permalink
working local: val-add and jail
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Apr 13, 2024
1 parent bc3fb40 commit 86b1246
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
6 changes: 3 additions & 3 deletions e2e/poa_jail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPOAJailing(t *testing.T) {
updatedSlashingCfg.ModifyGenesis = cosmos.ModifyGenesis(append(defaultGenesis, []cosmos.GenesisKV{
{
Key: "app_state.slashing.params.signed_blocks_window",
Value: "2",
Value: "3",
},
{
Key: "app_state.slashing.params.min_signed_per_window",
Expand Down Expand Up @@ -66,8 +66,8 @@ func TestPOAJailing(t *testing.T) {
t.Fatal(err)
}

// Wait for the stopped node to be jailed.
require.NoError(t, testutil.WaitForBlocks(ctx, 4, chain.Validators[0]))
// Wait for the stopped node to be jailed & persist
require.NoError(t, testutil.WaitForBlocks(ctx, 5, chain.Validators[0]))

// Validate 1 validator is jailed (status 1)
vals := helpers.GetValidators(t, ctx, chain)
Expand Down
41 changes: 19 additions & 22 deletions module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package module

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -34,12 +33,12 @@ func (am AppModule) BeginBlocker(ctx context.Context) error {
}

for _, val := range vals {
if val.Status == stakingtypes.Bonded {
valBz, err := sdk.ValAddressFromBech32(val.OperatorAddress)
if err != nil {
return err
}
valBz, err := sdk.ValAddressFromBech32(val.OperatorAddress)
if err != nil {
return err
}

if val.Status == stakingtypes.Bonded {
lastPower, err := am.keeper.GetStakingKeeper().GetLastValidatorPower(ctx, valBz)
if err != nil {
return err
Expand All @@ -54,6 +53,10 @@ func (am AppModule) BeginBlocker(ctx context.Context) error {
if err := am.keeper.GetStakingKeeper().SetLastValidatorPower(ctx, valBz, lastPower); err != nil {
return err
}

if err := am.keeper.GetStakingKeeper().SetValidatorByPowerIndex(ctx, val); err != nil {
return err
}
}
}
}
Expand All @@ -76,9 +79,9 @@ func (am AppModule) EndBlocker(ctx context.Context) error {
if err != nil {
return err
}
fmt.Printf("\nENDBLOCK POA valUpdates before:\n")

for _, valUpdate := range valUpdates {
fmt.Printf(" - %v: %d\n", valUpdate.PubKey.String(), valUpdate.Power)
am.keeper.Logger().Info("ValUpdate Before", "pubkey", valUpdate.PubKey.String(), "power", valUpdate.Power)
}

for _, v := range vals {
Expand All @@ -89,12 +92,7 @@ func (am AppModule) EndBlocker(ctx context.Context) error {

switch v.GetStatus() {
case stakingtypes.Unbonding:
// if the validator is unbonding, force it to be unbonded. (H+1)
v.Status = stakingtypes.Unbonded
if err := am.keeper.GetStakingKeeper().SetValidator(ctx, v); err != nil {
return err
}

continue
case stakingtypes.Unbonded:
// if the validator is unbonded (above case), delete the last validator power. (H+2)
if err := am.keeper.GetStakingKeeper().DeleteLastValidatorPower(ctx, valAddr); err != nil {
Expand All @@ -103,20 +101,20 @@ func (am AppModule) EndBlocker(ctx context.Context) error {

case stakingtypes.Unspecified, stakingtypes.Bonded:
if !v.DelegatorShares.IsZero() {
// TODO: new val cache still needed?
// if the validator is freshly created, then perform the validator update.

// TODO: get last validator power from x/staking here instead? (then we can remove the cache)
isNewVal, err := am.keeper.NewValidatorsCache.Has(ctx, v.GetOperator())
if err != nil {
return err
}

power, err := am.keeper.GetStakingKeeper().GetLastValidatorPower(ctx, valAddr)
fmt.Println("\nisNewVal", isNewVal)
fmt.Println("power", power)
fmt.Println("err", err)
// power, err := am.keeper.GetStakingKeeper().GetLastValidatorPower(ctx, valAddr)
// if err != nil {
// return err
// }

if isNewVal {
am.keeper.Logger().Info("New Validator", "operator", v.GetOperator(), "bonded_tokens", v.GetBondedTokens())
if err := am.keeper.NewValidatorsCache.Remove(ctx, v.GetOperator()); err != nil {
return err
}
Expand All @@ -137,9 +135,8 @@ func (am AppModule) EndBlocker(ctx context.Context) error {
}
}

fmt.Printf("POA valUpdates after:\n")
for _, valUpdate := range valUpdates {
fmt.Printf(" - %v: %d\n", valUpdate.PubKey.String(), valUpdate.Power)
am.keeper.Logger().Info("ValUpdate After", "pubkey", valUpdate.PubKey.String(), "power", valUpdate.Power)
}

return err
Expand Down

0 comments on commit 86b1246

Please sign in to comment.