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

Require secp256k1 pubkey registration for unjail #434

Merged
merged 4 commits into from
Dec 10, 2024
Merged
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
11 changes: 6 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/mint"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
sdkslashing "github.com/cosmos/cosmos-sdk/x/slashing"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
sdkstakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
Expand All @@ -147,6 +147,7 @@ import (
"github.com/sedaprotocol/seda-chain/x/pubkey"
pubkeykeeper "github.com/sedaprotocol/seda-chain/x/pubkey/keeper"
pubkeytypes "github.com/sedaprotocol/seda-chain/x/pubkey/types"
"github.com/sedaprotocol/seda-chain/x/slashing"
"github.com/sedaprotocol/seda-chain/x/staking"
stakingkeeper "github.com/sedaprotocol/seda-chain/x/staking/keeper"
"github.com/sedaprotocol/seda-chain/x/tally"
Expand Down Expand Up @@ -180,9 +181,9 @@ var (
groupmodule.AppModuleBasic{},
gov.NewAppModuleBasic([]govclient.ProposalHandler{}),
mintModule{}, // custom modifications
slashing.AppModuleBasic{},
distrModule{}, // custom modifications
stakingModule{}, // custom modifications
sdkslashing.AppModuleBasic{},
distrModule{}, // custom modifications
staking.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
consensus.AppModuleBasic{},
Expand Down Expand Up @@ -784,7 +785,7 @@ func NewApp(
groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, nil),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, nil),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, nil, app.interfaceRegistry),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, nil, app.interfaceRegistry, app.PubKeyKeeper, authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix())),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, nil),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.PubKeyKeeper),
upgrade.NewAppModule(app.UpgradeKeeper, app.AccountKeeper.AddressCodec()),
Expand Down
16 changes: 0 additions & 16 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,10 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/mint"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/sedaprotocol/seda-chain/app/params"
"github.com/sedaprotocol/seda-chain/x/staking"
)

// stakingModule wraps the x/staking module to overwrite some genesis
// parameters.
type stakingModule struct {
staking.AppModuleBasic
}

// DefaultGenesis returns custom x/staking default genesis state.
func (stakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
genesis := stakingtypes.DefaultGenesisState()
genesis.Params.BondDenom = params.DefaultBondDenom

return cdc.MustMarshalJSON(genesis)
}

// distrModule wraps the x/distribution module to overwrite some genesis
// parameters.
type distrModule struct {
Expand Down
47 changes: 15 additions & 32 deletions scripts/local_multi_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ $BIN keys add validator3 --keyring-backend=test --home=$HOME/.sedad/validator3
$BIN keys add validator4 --keyring-backend=test --home=$HOME/.sedad/validator4

# create validator node with tokens to transfer to the three other nodes
$BIN add-genesis-account $($BIN keys show validator1 -a --keyring-backend=test --home=$HOME/.sedad/validator1) 10000000000000000000aseda --home=$HOME/.sedad/validator1
$BIN gentx validator1 1000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing
$BIN add-genesis-account $($BIN keys show validator1 -a --keyring-backend=test --home=$HOME/.sedad/validator1) 100000000000000000000aseda --home=$HOME/.sedad/validator1
$BIN gentx validator1 10000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing
$BIN collect-gentxs --home=$HOME/.sedad/validator1

# change app.toml values
Expand Down Expand Up @@ -116,34 +116,34 @@ sed -i '' -E "s|persistent_peers = \"\"|persistent_peers = \"${NODE1_ID}@localho
# start all four validators
tmux new-session -s validator1 -d
tmux send -t validator1 'BIN=./build/sedad' ENTER
tmux send -t validator1 '$BIN start --home=$HOME/.sedad/validator1' ENTER
tmux send -t validator1 '$BIN start --home=$HOME/.sedad/validator1 --log_level debug > val1_multi_local.log 2>&1 &' ENTER

tmux new-session -s validator2 -d
tmux send -t validator2 'BIN=./build/sedad' ENTER
tmux send -t validator2 '$BIN start --home=$HOME/.sedad/validator2' ENTER
tmux send -t validator2 '$BIN start --home=$HOME/.sedad/validator2 --log_level debug > val2_multi_local.log 2>&1 &' ENTER

tmux new-session -s validator3 -d
tmux send -t validator3 'BIN=./build/sedad' ENTER
tmux send -t validator3 '$BIN start --home=$HOME/.sedad/validator3' ENTER
tmux send -t validator3 '$BIN start --home=$HOME/.sedad/validator3 --log_level debug > val3_multi_local.log 2>&1 &' ENTER

tmux new-session -s validator4 -d
tmux send -t validator4 'BIN=./build/sedad' ENTER
tmux send -t validator4 '$BIN start --home=$HOME/.sedad/validator4' ENTER
tmux send -t validator4 '$BIN start --home=$HOME/.sedad/validator4 --log_level debug > val4_multi_local.log 2>&1 &' ENTER

echo "begin sending funds to validators 2, 3, & 4"
sleep 10
$BIN tx bank send validator1 $($BIN keys show validator2 -a --keyring-backend=test --home=$HOME/.sedad/validator2) 2000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing --node http://localhost:26657 --broadcast-mode sync --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
$BIN tx bank send validator1 $($BIN keys show validator2 -a --keyring-backend=test --home=$HOME/.sedad/validator2) 20000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing --node http://localhost:26657 --broadcast-mode sync --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
sleep 10
$BIN tx bank send validator1 $($BIN keys show validator3 -a --keyring-backend=test --home=$HOME/.sedad/validator3) 2000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing --node http://localhost:26657 --broadcast-mode sync --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
$BIN tx bank send validator1 $($BIN keys show validator3 -a --keyring-backend=test --home=$HOME/.sedad/validator3) 20000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing --node http://localhost:26657 --broadcast-mode sync --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
sleep 10
$BIN tx bank send validator1 $($BIN keys show validator4 -a --keyring-backend=test --home=$HOME/.sedad/validator4) 2000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing --node http://localhost:26657 --broadcast-mode sync --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
$BIN tx bank send validator1 $($BIN keys show validator4 -a --keyring-backend=test --home=$HOME/.sedad/validator4) 20000000000000000000aseda --keyring-backend=test --home=$HOME/.sedad/validator1 --chain-id=testing --node http://localhost:26657 --broadcast-mode sync --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
sleep 10

echo "begin sending create validator txs"
cat << EOF > validator2.json
{
"pubkey": $($BIN tendermint show-validator --home=$HOME/.sedad/validator2),
"amount": "1000000000000000000aseda",
"amount": "10000000000000000000aseda",
"moniker": "validator2",
"identity": "val2",
"website": "val2.com",
Expand All @@ -161,7 +161,7 @@ rm validator2.json
cat << EOF > validator3.json
{
"pubkey": $($BIN tendermint show-validator --home=$HOME/.sedad/validator3),
"amount": "1000000000000000000aseda",
"amount": "10000000000000000000aseda",
"moniker": "validator3",
"identity": "val3",
"website": "val3.com",
Expand All @@ -179,7 +179,7 @@ rm validator3.json
cat << EOF > validator4.json
{
"pubkey": $($BIN tendermint show-validator --home=$HOME/.sedad/validator4),
"amount": "1000000000000000000aseda",
"amount": "5000000000000000000aseda",
"moniker": "validator4",
"identity": "val4",
"website": "val4.com",
Expand All @@ -191,28 +191,11 @@ cat << EOF > validator4.json
"min-self-delegation": "1"
}
EOF
$BIN tx staking create-validator validator4.json --from=validator4 --keyring-backend=test --home=$HOME/.sedad/validator4 --broadcast-mode sync --chain-id=testing --node http://localhost:26657 --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
$BIN tx staking create-validator validator4.json --without-seda-keys --from=validator4 --keyring-backend=test --home=$HOME/.sedad/validator4 --broadcast-mode sync --chain-id=testing --node http://localhost:26657 --yes --gas-prices 10000000000aseda --gas auto --gas-adjustment 1.7
rm validator4.json

sleep 10
echo "4 validators are up and running!"

# generate and upload SEDA keys
$BIN tx pubkey add-seda-keys --from validator1 --keyring-backend test --home $HOME/.sedad/validator1 --gas-prices 10000000000aseda --gas auto --gas-adjustment 2.0 --keyring-backend test --chain-id=testing --node http://localhost:26657 --yes
$BIN tx pubkey add-seda-keys --from validator2 --keyring-backend test --home $HOME/.sedad/validator2 --gas-prices 10000000000aseda --gas auto --gas-adjustment 2.0 --keyring-backend test --chain-id=testing --node http://localhost:26657 --yes
$BIN tx pubkey add-seda-keys --from validator3 --keyring-backend test --home $HOME/.sedad/validator3 --gas-prices 10000000000aseda --gas auto --gas-adjustment 2.0 --keyring-backend test --chain-id=testing --node http://localhost:26657 --yes
$BIN tx pubkey add-seda-keys --from validator4 --keyring-backend test --home $HOME/.sedad/validator4 --gas-prices 10000000000aseda --gas auto --gas-adjustment 2.0 --keyring-backend test --chain-id=testing --node http://localhost:26657 --yes

# restart to use SEDA keys
sleep 10
tmux send -t validator1 'C-c'
tmux send -t validator1 '$BIN start --home=$HOME/.sedad/validator1 --log_level debug > val1_multi_local.log 2>&1 &' ENTER

tmux send -t validator2 'C-c'
tmux send -t validator2 '$BIN start --home=$HOME/.sedad/validator2 --log_level debug > val2_multi_local.log 2>&1 &' ENTER

tmux send -t validator3 'C-c'
tmux send -t validator3 '$BIN start --home=$HOME/.sedad/validator3 --log_level debug > val3_multi_local.log 2>&1 &' ENTER

tmux send -t validator4 'C-c'
tmux send -t validator4 '$BIN start --home=$HOME/.sedad/validator4 --log_level debug > val4_multi_local.log 2>&1 &' ENTER
# val4 generates and uploads SEDA keys
# $BIN tx pubkey add-seda-keys --from validator4 --keyring-backend test --home $HOME/.sedad/validator4 --gas-prices 10000000000aseda --gas auto --gas-adjustment 2.0 --keyring-backend test --chain-id=testing --node http://localhost:26657 --yes
Thomasvdam marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 1 addition & 2 deletions x/batching/keeper/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ func initFixture(tb testing.TB) *fixture {
log.NewNopLogger(),
)

var pubKeyKeeper *pubkeykeeper.Keeper
sdkStakingKeeper := sdkstakingkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[sdkstakingtypes.StoreKey]),
Expand Down Expand Up @@ -200,7 +199,7 @@ func initFixture(tb testing.TB) *fixture {
viewKeeper,
)

pubKeyKeeper = pubkeykeeper.NewKeeper(
pubKeyKeeper := pubkeykeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[pubkeytypes.StoreKey]),
stakingKeeper,
Expand Down
46 changes: 21 additions & 25 deletions x/pubkey/keeper/endblock.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package keeper

import (
"errors"

"cosmossdk.io/collections"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sedaprotocol/seda-chain/app/utils"
Expand Down Expand Up @@ -89,13 +85,13 @@ func (k Keeper) CheckKeyRegistrationRate(ctx sdk.Context, keyIndex utils.SEDAKey

var powerSum uint64
err = k.stakingKeeper.IterateLastValidatorPowers(ctx, func(valAddr sdk.ValAddress, power int64) (stop bool) {
_, err := k.GetValidatorKeyAtIndex(ctx, valAddr, keyIndex)
registered, err := k.HasRegisteredKey(ctx, valAddr, keyIndex)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return false
}
panic(err)
}
if !registered {
return false
}
//nolint:gosec // G115: We shouldn't get negative power anyway.
powerSum += uint64(power)
return false
Expand Down Expand Up @@ -133,27 +129,27 @@ func (k Keeper) JailValidators(ctx sdk.Context, keyIndex utils.SEDAKeyIndex) err
if err != nil {
return err
}
_, err = k.GetValidatorKeyAtIndex(ctx, valAddr, keyIndex)
registered, err := k.HasRegisteredKey(ctx, valAddr, keyIndex)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
consAddr, err := val.GetConsAddr()
if err != nil {
return err
}
err = k.slashingKeeper.Jail(ctx, consAddr)
if err != nil {
return err
}
k.Logger(ctx).Info(
"jailed validator for not having required public key",
"consensus_address", consAddr,
"operator_address", val.OperatorAddress,
"key_index", keyIndex,
)
} else {
return err
}
if !registered {
consAddr, err := val.GetConsAddr()
if err != nil {
return err
}
err = k.slashingKeeper.Jail(ctx, consAddr)
if err != nil {
return err
}
k.Logger(ctx).Info(
"jailed validator for missing required public key",
"consensus_address", consAddr,
"operator_address", val.OperatorAddress,
"key_index", keyIndex,
)
}
}

return nil
}
4 changes: 1 addition & 3 deletions x/pubkey/keeper/endblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"github.com/sedaprotocol/seda-chain/integration"
"github.com/sedaprotocol/seda-chain/x/pubkey"
"github.com/sedaprotocol/seda-chain/x/pubkey/keeper"
pubkeykeeper "github.com/sedaprotocol/seda-chain/x/pubkey/keeper"
"github.com/sedaprotocol/seda-chain/x/pubkey/types"
"github.com/sedaprotocol/seda-chain/x/staking"
stakingkeeper "github.com/sedaprotocol/seda-chain/x/staking/keeper"
Expand Down Expand Up @@ -127,7 +126,6 @@ func initFixture(tb testing.TB) *fixture {
log.NewNopLogger(),
)

var pubKeyKeeper *pubkeykeeper.Keeper
sdkStakingKeeper := sdkstakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[sdkstakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(params.Bech32PrefixValAddr), addresscodec.NewBech32Codec(params.Bech32PrefixConsAddr))
stakingKeeper := stakingkeeper.NewKeeper(sdkStakingKeeper, addresscodec.NewBech32Codec(params.Bech32PrefixValAddr))

Expand All @@ -144,7 +142,7 @@ func initFixture(tb testing.TB) *fixture {
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

pubKeyKeeper = keeper.NewKeeper(
pubKeyKeeper := keeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[types.StoreKey]),
stakingKeeper,
Expand Down
14 changes: 14 additions & 0 deletions x/pubkey/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"

"cosmossdk.io/collections"
Expand Down Expand Up @@ -97,6 +98,19 @@ func (k Keeper) GetValidatorKeyAtIndex(ctx context.Context, validatorAddr sdk.Va
return pubKey, nil
}

// HasRegisteredKey returns true if the validator has registered a key
// at the index.
func (k Keeper) HasRegisteredKey(ctx context.Context, validatorAddr sdk.ValAddress, index utils.SEDAKeyIndex) (bool, error) {
_, err := k.pubKeys.Get(ctx, collections.Join(validatorAddr.Bytes(), uint32(index)))
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return false, nil
}
return false, err
}
return true, nil
}

// GetValidatorKeys returns all public keys of a given validator.
func (k Keeper) GetValidatorKeys(ctx context.Context, validatorAddr string) (result types.ValidatorPubKeys, err error) {
valAddr, err := k.validatorAddressCodec.StringToBytes(validatorAddr)
Expand Down
57 changes: 57 additions & 0 deletions x/slashing/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package slashing

import (
addresscodec "cosmossdk.io/core/address"

"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/slashing/exported"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)

var _ module.AppModule = AppModule{}

// ----------------------------------------------------------------------------
// AppModule
// ----------------------------------------------------------------------------

// AppModule implements an application module for the slashing module.
type AppModule struct {
slashing.AppModule

keeper keeper.Keeper
pubKeyKeeper PubKeyKeeper
validatorAddressCodec addresscodec.Codec
}

// NewAppModule creates a new AppModule object.
func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
sk types.StakingKeeper,
ss exported.Subspace,
registry cdctypes.InterfaceRegistry,
pk PubKeyKeeper,
valAddrCdc addresscodec.Codec,
) AppModule {
baseAppModule := slashing.NewAppModule(cdc, keeper, ak, bk, sk, ss, registry)
return AppModule{
AppModule: baseAppModule,
keeper: keeper,
pubKeyKeeper: pk,
validatorAddressCodec: valAddrCdc,
}
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
sdkMsgServer := keeper.NewMsgServerImpl(am.keeper)
msgServer := NewMsgServerImpl(sdkMsgServer, am.pubKeyKeeper, am.validatorAddressCodec)
types.RegisterMsgServer(cfg.MsgServer(), msgServer)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}
Loading
Loading