Skip to content

Commit

Permalink
Merge branch 'main' into cal/versioned-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters authored Apr 16, 2024
2 parents 46beef5 + 541ddf1 commit 513245a
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
version: v1.57.0
args: --timeout 10m
github-token: ${{ secrets.github_token }}
skip-pkg-cache: true
if: env.GIT_DIFF

# hadolint lints the Dockerfile
Expand Down
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo
// Version changes must be increasing. Downgrades are not permitted
if newVersion > currentVersion {
app.SetAppVersion(ctx, newVersion)
app.SignalKeeper.ResetTally(ctx)

Check failure on line 691 in app/app.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

File is not `gofumpt`-ed (gofumpt)
}
}
return res
Expand Down
45 changes: 45 additions & 0 deletions cmd/celestia-appd/cmd/addr_converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
)

// addrConversionCmd returns a command that converts between celestia1xxx and
// celestiavaloper1xxx addresses.
func addrConversionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "addr-conversion [celestia address]",
Short: "Convert between celestia1xxx address and validator operator address celestiavaloper1xxx",
Long: `Reads a celestia1xxx or celestiavaloper1xxx address and converts it to the other type.`,
Example: "celestia-appd addr-conversion celestia1grvklux2yjsln7ztk6slv538396qatckqhs86z\n" +
"celestia-appd addr-conversion celestiavaloper1grvklux2yjsln7ztk6slv538396qatck9gj7vy\n",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
converted, err := convertAccountValidatorAddress(args[0])
if err != nil {
return err
}
_, err = cmd.OutOrStdout().Write([]byte(converted + "\n"))
if err != nil {
return err
}
return nil
},
}
return cmd
}

// convertAccountValidatorAddress converts an account address into a valoper
// address, or a valoper address into an account address.
func convertAccountValidatorAddress(original string) (string, error) {
if accAddr, err := sdk.AccAddressFromBech32(original); err == nil {
return sdk.ValAddress(accAddr.Bytes()).String(), nil
}
if valAddr, err := sdk.ValAddressFromBech32(original); err == nil {
return sdk.AccAddress(valAddr.Bytes()).String(), nil
}
return "", fmt.Errorf("invalid address: %s", original)
}
45 changes: 45 additions & 0 deletions cmd/celestia-appd/cmd/addr_converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"bytes"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestAddrConversionCmd(t *testing.T) {
accAddr := "celestia1grvklux2yjsln7ztk6slv538396qatckqhs86z"
valAddr := "celestiavaloper1grvklux2yjsln7ztk6slv538396qatck9gj7vy"
t.Run("converts an account address", func(t *testing.T) {
output, err := executeCmd(addrConversionCmd(), accAddr)
assert.NoError(t, err)
assert.Equal(t, valAddr+"\n", output)
})
t.Run("converts a valoper address", func(t *testing.T) {
output, err := executeCmd(addrConversionCmd(), valAddr)
assert.NoError(t, err)
assert.Equal(t, accAddr+"\n", output)
})
t.Run("returns an error for an invalid account address", func(t *testing.T) {
invalidAddr := "celestia1xxxxxxxxxxxx"
_, err := executeCmd(addrConversionCmd(), invalidAddr)
assert.Error(t, err)
assert.ErrorContains(t, err, "invalid address")
})
t.Run("returns an error for an invalid valoper address", func(t *testing.T) {
invalidAddr := "celestiavaloper1xxxxxxxxxxxx"
_, err := executeCmd(addrConversionCmd(), invalidAddr)
assert.Error(t, err)
assert.ErrorContains(t, err, "invalid address")
})
}

func executeCmd(cmd *cobra.Command, args ...string) (string, error) {
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetErr(buf)
cmd.SetArgs(args)
err := cmd.Execute()
return buf.String(), err
}
5 changes: 2 additions & 3 deletions cmd/celestia-appd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig encoding.Config) {
cfg := sdk.GetConfig()
cfg.Seal()

debugCmd := debug.Cmd()

rootCmd.AddCommand(
genutilcli.InitCmd(app.ModuleBasics, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
Expand All @@ -135,11 +133,12 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig encoding.Config) {
genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome),
genutilcli.ValidateGenesisCmd(app.ModuleBasics),
tmcli.NewCompletionCmd(rootCmd, true),
debugCmd,
debug.Cmd(),
config.Cmd(),
commands.CompactGoLevelDBCmd,
addrbookCommand(),
downloadGenesisCommand(),
addrConversionCmd(),
)

server.AddCommands(rootCmd, app.DefaultNodeHome, NewAppServer, createAppAndExport, addModuleInitFlags)
Expand Down
12 changes: 4 additions & 8 deletions x/signal/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,14 @@ func (k *Keeper) ShouldUpgrade() (bool, uint64) {
return k.quorumVersion != 0, k.quorumVersion
}

// ResetTally resets the tally after a version change. It iterates over the store,
// and deletes any versions that are less than the provided version. It also
// resets the quorumVersion to 0.
func (k *Keeper) ResetTally(ctx sdk.Context, version uint64) {
// ResetTally resets the tally after a version change. It iterates over the
// store and deletes all versions. It also resets the quorumVersion to 0.
func (k *Keeper) ResetTally(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iterator := store.Iterator(nil, nil)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
v := VersionFromBytes(iterator.Value())
if v <= version {
store.Delete(iterator.Key())
}
store.Delete(iterator.Key())
}
k.quorumVersion = 0
}
Expand Down
30 changes: 29 additions & 1 deletion x/signal/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestTallyingLogic(t *testing.T) {
require.Error(t, err)

// resetting the tally should clear other votes
upgradeKeeper.ResetTally(ctx, 2)
upgradeKeeper.ResetTally(ctx)
res, err = upgradeKeeper.VersionTally(goCtx, &types.QueryVersionTallyRequest{
Version: 2,
})
Expand Down Expand Up @@ -258,6 +258,34 @@ func TestThresholdVotingPower(t *testing.T) {
}
}

// TestResetTally verifies that the VotingPower for all versions is reset to
// zero after calling ResetTally.
func TestResetTally(t *testing.T) {
upgradeKeeper, ctx, _ := setup(t)

_, err := upgradeKeeper.SignalVersion(ctx, &types.MsgSignalVersion{ValidatorAddress: testutil.ValAddrs[0].String(), Version: 1})
require.NoError(t, err)
resp, err := upgradeKeeper.VersionTally(ctx, &types.QueryVersionTallyRequest{Version: 1})
require.NoError(t, err)
assert.Equal(t, uint64(40), resp.VotingPower)

_, err = upgradeKeeper.SignalVersion(ctx, &types.MsgSignalVersion{ValidatorAddress: testutil.ValAddrs[1].String(), Version: 2})
require.NoError(t, err)
resp, err = upgradeKeeper.VersionTally(ctx, &types.QueryVersionTallyRequest{Version: 2})
require.NoError(t, err)
assert.Equal(t, uint64(1), resp.VotingPower)

upgradeKeeper.ResetTally(ctx)

resp, err = upgradeKeeper.VersionTally(ctx, &types.QueryVersionTallyRequest{Version: 1})
require.NoError(t, err)
assert.Equal(t, uint64(0), resp.VotingPower)

resp, err = upgradeKeeper.VersionTally(ctx, &types.QueryVersionTallyRequest{Version: 2})
require.NoError(t, err)
assert.Equal(t, uint64(0), resp.VotingPower)
}

func setup(t *testing.T) (signal.Keeper, sdk.Context, *mockStakingKeeper) {
signalStore := sdk.NewKVStoreKey(types.StoreKey)
db := tmdb.NewMemDB()
Expand Down

0 comments on commit 513245a

Please sign in to comment.