Skip to content

Commit

Permalink
fix to store by cons addr
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 10, 2024
1 parent 308cc3e commit a055501
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions x/opchild/keeper/executor_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (k Keeper) ChangeExecutor(ctx context.Context, plan types.ExecutorChangePla
if err := k.SetValidator(ctx, plan.NextValidator); err != nil {
return err
}
if err = k.SetValidatorByConsAddr(ctx, plan.NextValidator); err != nil {
return err
}

params, err := k.GetParams(ctx)
if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions x/opchild/keeper/executor_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
testutilsims "github.com/cosmos/cosmos-sdk/testutil/sims"

"github.com/initia-labs/OPinit/x/opchild/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -53,3 +55,65 @@ func Test_RegisterExecutorChangePlan(t *testing.T) {
Info: info,
}, input.OPChildKeeper.ExecutorChangePlans[height.Uint64()])
}

func Test_ExecuteChangePlan(t *testing.T) {
ctx, input := createDefaultTestInput(t)

valPubKeys := testutilsims.CreateTestPubKeys(2)
val1, err := types.NewValidator(valAddrs[1], valPubKeys[0], "validator1")
require.NoError(t, err)

val2, err := types.NewValidator(valAddrs[2], valPubKeys[1], "validator2")
require.NoError(t, err)

// set validators
require.NoError(t, input.OPChildKeeper.SetValidator(ctx, val1))
require.NoError(t, input.OPChildKeeper.SetValidatorByConsAddr(ctx, val1))
require.NoError(t, input.OPChildKeeper.SetValidator(ctx, val2))
require.NoError(t, input.OPChildKeeper.SetValidatorByConsAddr(ctx, val2))

// Arguments
l1ProposalID, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
require.NoError(t, err)
height, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
require.NoError(t, err)
nextValAddr := valAddrsStr[0]
nextExecutorAddr := []string{addrsStr[0], addrsStr[1]}
consensusPubKey := "l7aqGv+Zjbm0rallfqfqz+3iN31iOmgJCafWV5pGs6o="
moniker := "moniker"
info := "info"

err = input.OPChildKeeper.RegisterExecutorChangePlan(
l1ProposalID.Uint64(), height.Uint64(), nextValAddr,
moniker,
fmt.Sprintf(`{"@type":"/cosmos.crypto.ed25519.PubKey","key":"%s"}`, consensusPubKey),
info,
nextExecutorAddr,
)
require.NoError(t, err)
require.Len(t, input.OPChildKeeper.ExecutorChangePlans, 1)

err = input.OPChildKeeper.ChangeExecutor(ctx, input.OPChildKeeper.ExecutorChangePlans[height.Uint64()])
require.NoError(t, err)

// Check if the validator has been updated
validator, found := input.OPChildKeeper.GetValidator(ctx, valAddrs[0])
require.True(t, found)
require.Equal(t, moniker, validator.GetMoniker())
require.Equal(t, int64(1), validator.ConsPower)

consAddr, err := validator.GetConsAddr()
require.NoError(t, err)
v := input.OPChildKeeper.ValidatorByConsAddr(ctx, consAddr)
require.Equal(t, moniker, v.GetMoniker())
require.Equal(t, int64(1), v.GetConsensusPower())

// Check if the old validators have been updated
validator, found = input.OPChildKeeper.GetValidator(ctx, valAddrs[1])
require.True(t, found)
require.Equal(t, int64(0), validator.ConsPower)

validator, found = input.OPChildKeeper.GetValidator(ctx, valAddrs[2])
require.True(t, found)
require.Equal(t, int64(0), validator.ConsPower)
}

0 comments on commit a055501

Please sign in to comment.