Skip to content

Commit

Permalink
lint + fix remove val test (check bonded)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Apr 14, 2024
1 parent 90e3847 commit 2c8c7ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
18 changes: 17 additions & 1 deletion e2e/poa_remove_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package e2e

import (
"context"
"fmt"
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
Expand Down Expand Up @@ -75,7 +78,7 @@ func TestPOARemoval(t *testing.T) {

assertSignatures(t, ctx, chain, initialValidators-1)
require.Equal(t, initialValidators-1, len(consensus.Validators), "BFT consensus should have one less validator")
require.Equal(t, initialValidators-1, len(vals), "Validators should have one less validator")
require.Equal(t, initialValidators-1, getBondedValidators(t, ctx, chain), "Bonded validators should have one less active validator")
}

func getValToRemove(t *testing.T, vals helpers.Validators, delegationAmt int64) string {
Expand All @@ -90,3 +93,16 @@ func getValToRemove(t *testing.T, vals helpers.Validators, delegationAmt int64)

return valToRemove
}

func getBondedValidators(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain) int {
vals := helpers.GetValidators(t, ctx, chain).Validators

bonded := 0
for _, v := range vals {
if v.Status == int(stakingtypes.Bonded) {
bonded++
}
}

return bonded
}
4 changes: 1 addition & 3 deletions keeper/poa.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (k Keeper) SetPOAPower(ctx context.Context, valOpBech32 string, newShares i
if err := k.slashKeeper.DeleteMissedBlockBitmap(ctx, consAddr); err != nil {
return stakingtypes.Validator{}, err
}

} else {
// Sets the new consensus power for the validator (this is executed in the x/staking ApplyAndReturnValidatorUpdates method)
if err := k.GetStakingKeeper().SetLastValidatorPower(ctx, valAddr, newBFTConsensusPower); err != nil {
Expand All @@ -119,11 +118,10 @@ func (k Keeper) SetPOAPower(ctx context.Context, valOpBech32 string, newShares i

// A cache to handle updated validators power. Once set, the next begin block will remove it from the cache.
// This allows us to Delete the validator index on the staking side, and ensures power updates do not persist over many blocks.
// This multi block persistance would break ABCI updates via x/staking if the validator's power is updated again, or removed.
// This multi block persistence would break ABCI updates via x/staking if the validator's power is updated again, or removed.
if err := k.UpdatedValidatorsCache.Set(ctx, val.OperatorAddress); err != nil {
return stakingtypes.Validator{}, err
}

}

absPowerDiff := uint64(math.Abs(float64(newBFTConsensusPower - currentPower)))
Expand Down
8 changes: 4 additions & 4 deletions keeper/staking_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package keeper

import (
"context"
"fmt"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"cosmossdk.io/math"
)

// Before a validator is jailed, we must delete it from the power index. else:
Expand All @@ -26,13 +26,13 @@ func (k Keeper) Hooks() Hooks {

// BeforeValidatorModified implements types.StakingHooks.
func (h Hooks) BeforeValidatorModified(ctx context.Context, valAddr types.ValAddress) error {
fmt.Println("BeforeValidatorModified", valAddr.String())
h.k.logger.Info("BeforeValidatorModified", "valAddr", valAddr.String())
return h.k.BeforeJailedValidators.Set(ctx, valAddr.String())
}

// BeforeValidatorSlashed implements types.StakingHooks.
func (h Hooks) BeforeValidatorSlashed(ctx context.Context, valAddr types.ValAddress, fraction math.LegacyDec) error {
fmt.Println("BeforeValidatorSlashed", valAddr.String(), fraction.String())
h.k.logger.Info("BeforeValidatorSlashed", valAddr.String(), fraction.String())
return nil
}

Expand Down
11 changes: 6 additions & 5 deletions module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (am AppModule) BeginBlocker(ctx context.Context) error {
if err != nil {
return err
}
fmt.Printf("UpdatedValidatorsCache: %s\n", valOperAddr)
am.keeper.Logger().Info("UpdatedValidatorsCache: %s\n", valOperAddr)

valAddr, err := sk.ValidatorAddressCodec().StringToBytes(valOperAddr)
if err != nil {
Expand Down Expand Up @@ -87,11 +87,12 @@ func (am AppModule) BeginBlocker(ctx context.Context) error {
return nil
}

fmt.Println("\nBeginBlocker events:")
// TODO: Error here so it's easy to see (remove later)
am.keeper.Logger().Error("BeginBlocker events:\n")
for _, e := range events {
fmt.Printf(" %s: %d\n", &e.PubKey, e.Power)
am.keeper.Logger().Error(fmt.Sprintf("PubKey: %s, Power: %d", &e.PubKey, e.Power))

Check failure on line 93 in module/abci.go

View workflow job for this annotation

GitHub Actions / golangci-lint

G601: Implicit memory aliasing in for loop. (gosec)
}
fmt.Println()
am.keeper.Logger().Info("\n")

return nil
}
Expand All @@ -111,7 +112,7 @@ func (am AppModule) handleBeforeJailedValidators(ctx context.Context) error {
if err != nil {
return err
}
fmt.Printf("EndBlocker BeforeJailedValidators: %s\n", valOperAddr)
am.keeper.Logger().Info("EndBlocker BeforeJailedValidators", valOperAddr, "\n")

valAddr, err := sk.ValidatorAddressCodec().StringToBytes(valOperAddr)
if err != nil {
Expand Down

0 comments on commit 2c8c7ed

Please sign in to comment.