Skip to content

Commit

Permalink
cleaner structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Oct 21, 2023
1 parent 8504d81 commit 6e938cd
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 182 deletions.
13 changes: 8 additions & 5 deletions client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func NewTxCmd(ac address.Codec) *cobra.Command {

txCmd.AddCommand(
NewCreateValidatorCmd(ac),
NewSetPowerCmd(),
NewSetPowerCmd(ac),
NewRemoveValidatorCmd(),
)
return txCmd
}

func NewSetPowerCmd() *cobra.Command {
func NewSetPowerCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "set-power [validator] [power]",
Args: cobra.ExactArgs(2),
Expand All @@ -70,6 +70,10 @@ func NewSetPowerCmd() *cobra.Command {
Power: power,
}

if err := msg.Validate(ac); err != nil {
return fmt.Errorf("msg.Validate failed: %w", err)
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand Down Expand Up @@ -125,7 +129,7 @@ Where validator.json contains:
{
"pubkey": {"@type":"/cosmos.crypto.ed25519.PubKey","key":"oWg2ISpLF405Jcm2vXV+2v4fnjodh6aafuIdeoW+rUw="},
"amount": "1stake",
"amount": "1stake", # ignored
"moniker": "myvalidator",
"identity": "optional identity signature (ex. UPort or Keybase)",
"website": "validator's (optional) website",
Expand All @@ -134,7 +138,7 @@ Where validator.json contains:
"commission-rate": "0.1",
"commission-max-rate": "0.2",
"commission-max-change-rate": "0.01",
"min-self-delegation": "1"
"min-self-delegation": "1" # ignored
}
where we can get the pubkey using "%s tendermint show-validator"
Expand Down Expand Up @@ -194,7 +198,6 @@ func newBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl
return txf, nil, err
}
msg, err := poa.NewMsgCreateValidator(
// TODO: force set min delegation at the params level?
valStr, val.PubKey, description, commissionRates, val.MinSelfDelegation,
)
if err != nil {
Expand Down
83 changes: 83 additions & 0 deletions conversions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package poa

import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)

func NewDescription(moniker, identity, website, securityContact, details string) Description {
return Description{
Moniker: moniker,
Identity: identity,
Website: website,
SecurityContact: securityContact,
Details: details,
}
}

func NewCommissionRates(rate, maxRate, maxChangeRate math.LegacyDec) CommissionRates {
return CommissionRates{
Rate: rate,
MaxRate: maxRate,
MaxChangeRate: maxChangeRate,
}
}

// TODO: ideally we should remove this need?
func ConvertPOAToStaking(poa Validator) types.Validator {
return types.Validator{
OperatorAddress: poa.OperatorAddress,
ConsensusPubkey: poa.ConsensusPubkey,
Jailed: poa.Jailed,
Status: types.BondStatus(poa.Status),
Tokens: poa.Tokens,
DelegatorShares: poa.DelegatorShares,
Description: types.NewDescription(
poa.Description.Moniker,
poa.Description.Identity,
poa.Description.Website,
poa.Description.SecurityContact,
poa.Description.Details,
),
UnbondingHeight: poa.UnbondingHeight,
UnbondingTime: poa.UnbondingTime,
Commission: types.NewCommission(
poa.Commission.CommissionRates.Rate,
poa.Commission.CommissionRates.MaxRate,
poa.Commission.CommissionRates.MaxChangeRate,
),
MinSelfDelegation: poa.MinSelfDelegation,
UnbondingOnHoldRefCount: poa.UnbondingOnHoldRefCount,
UnbondingIds: poa.UnbondingIds,
}
}

func ConvertStakingToPOA(val types.Validator) *Validator {
return &Validator{
OperatorAddress: val.OperatorAddress,
ConsensusPubkey: val.ConsensusPubkey,
Jailed: val.Jailed,
Status: BondStatus(val.Status),
Tokens: val.Tokens,
DelegatorShares: val.DelegatorShares,
Description: Description{
Moniker: val.Description.Moniker,
Identity: val.Description.Identity,
Website: val.Description.Website,
SecurityContact: val.Description.SecurityContact,
Details: val.Description.Details,
},
UnbondingHeight: val.UnbondingHeight,
UnbondingTime: val.UnbondingTime,
Commission: Commission{
CommissionRates: CommissionRates{
Rate: val.Commission.Rate,
MaxRate: val.Commission.MaxRate,
MaxChangeRate: val.Commission.MaxChangeRate,
},
},
MinSelfDelegation: val.MinSelfDelegation,
UnbondingOnHoldRefCount: val.UnbondingOnHoldRefCount,
UnbondingIds: val.UnbondingIds,
}
}
3 changes: 2 additions & 1 deletion genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package poa
// NewGenesisState creates a new genesis state with default values.
func NewGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
Params: DefaultParams(),
PendingValidators: []Validator{},
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/DataDog/zstd v1.5.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/bits-and-blooms/bitset v1.8.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
addresscodec "cosmossdk.io/core/address"
storetypes "cosmossdk.io/core/store"
"github.com/cosmos/cosmos-sdk/codec"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/strangelove-ventures/poa"
)
Expand All @@ -16,20 +17,23 @@ type Keeper struct {
validatorAddressCodec addresscodec.Codec

stakingKeeper *stakingkeeper.Keeper
slashKeeper slashingkeeper.Keeper
}

// NewKeeper creates a new Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
storeService storetypes.KVStoreService,
sk *stakingkeeper.Keeper,
slk slashingkeeper.Keeper,
validatorAddressCodec addresscodec.Codec,
) Keeper {
k := Keeper{
cdc: cdc,
storeService: storeService,
stakingKeeper: sk,
validatorAddressCodec: validatorAddressCodec,
slashKeeper: slk,
}

return k
Expand Down
8 changes: 4 additions & 4 deletions module/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"cosmossdk.io/depinject"

"github.com/cosmos/cosmos-sdk/codec"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
modulev1 "github.com/strangelove-ventures/poa/api/module/v1"
"github.com/strangelove-ventures/poa/keeper"
Expand Down Expand Up @@ -36,8 +36,8 @@ type ModuleInputs struct {
StoreService store.KVStoreService
AddressCodec address.Codec

StakingKeeper stakingkeeper.Keeper
BankKeeper bankkeeper.Keeper
StakingKeeper stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
}

type ModuleOutputs struct {
Expand All @@ -48,7 +48,7 @@ type ModuleOutputs struct {
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
k := keeper.NewKeeper(in.Cdc, in.StoreService, &in.StakingKeeper, in.AddressCodec)
k := keeper.NewKeeper(in.Cdc, in.StoreService, &in.StakingKeeper, in.SlashingKeeper, in.AddressCodec)
m := NewAppModule(in.Cdc, k)

return ModuleOutputs{Module: m, Keeper: k, Out: depinject.Out{}}
Expand Down
Loading

0 comments on commit 6e938cd

Please sign in to comment.