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

feat: Clawback Continuous Vesting Account #195

Merged
merged 15 commits into from
Feb 15, 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
23 changes: 13 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ import (
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"

"github.com/cosmos/cosmos-sdk/x/authz"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
Expand Down Expand Up @@ -97,8 +96,7 @@ import (
"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"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
sdkstakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/ibc-go/modules/capability"
Expand Down Expand Up @@ -134,6 +132,10 @@ import (
"github.com/sedaprotocol/seda-chain/app/keepers"
appparams "github.com/sedaprotocol/seda-chain/app/params"
"github.com/sedaprotocol/seda-chain/docs"
"github.com/sedaprotocol/seda-chain/x/staking"
stakingkeeper "github.com/sedaprotocol/seda-chain/x/staking/keeper"
"github.com/sedaprotocol/seda-chain/x/vesting"
vestingtypes "github.com/sedaprotocol/seda-chain/x/vesting/types"
)

const (
Expand Down Expand Up @@ -375,7 +377,7 @@ func NewApp(
}
app.txConfig = txConfig

app.StakingKeeper = stakingkeeper.NewKeeper(
sdkStakingKeeper := sdkstakingkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[stakingtypes.StoreKey]),
app.AccountKeeper,
Expand All @@ -384,6 +386,7 @@ func NewApp(
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)
app.StakingKeeper = stakingkeeper.NewKeeper(sdkStakingKeeper)

app.FeeGrantKeeper = feegrantkeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -688,15 +691,15 @@ func NewApp(
genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app, txConfig),
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, nil),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, nil),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
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),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, nil),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, nil), // customstaking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.RandomnessKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, nil),
upgrade.NewAppModule(app.UpgradeKeeper, app.AccountKeeper.AddressCodec()),
evidence.NewAppModule(app.EvidenceKeeper),
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
Expand Down Expand Up @@ -733,7 +736,7 @@ func NewApp(
// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
// CanWithdrawInvariant invariant.
// NOTE: staking module is required if HistoricalEntries param > 0
// NOTE: sdkstaking module is required if HistoricalEntries param > 0
app.mm.SetOrderPreBlockers(
upgradetypes.ModuleName,
)
Expand Down Expand Up @@ -791,7 +794,7 @@ func NewApp(
packetforwardtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
// NOTE: The genutils module must occur after sdkstaking so that pools are
// properly initialized with tokens from genesis accounts.
// NOTE: Capability module must occur first so that it can initialize any capabilities
// so that other modules that want to create or claim capabilities afterwards in InitChain
Expand Down Expand Up @@ -846,7 +849,7 @@ func NewApp(

// create the simulation manager and define the order of the modules for deterministic simulations
overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, randomGenesisAccounts, nil),
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, RandomGenesisAccounts, nil),
}
app.sm = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules)
app.sm.RegisterStoreDecoders()
Expand Down
2 changes: 1 addition & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (app *App) ExportAppStateAndValidators(
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
validators, err := staking.WriteValidators(ctx, app.StakingKeeper.Keeper)
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Expand Down
30 changes: 16 additions & 14 deletions app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"crypto/rand"
"encoding/json"

sdkmath "cosmossdk.io/math"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
sdkvestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"

vestingtypes "github.com/sedaprotocol/seda-chain/x/vesting/types"
)

// The genesis state of the blockchain is represented here as a map of raw json
Expand All @@ -27,27 +29,31 @@ func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
}

// randomGenesisAccounts defines the default RandomGenesisAccountsFn used on the SDK.
// It creates a slice of BaseAccount, ContinuousVestingAccount and DelayedVestingAccount.
// RandomGenesisAccounts defines the default RandomGenesisAccountsFn used on the SDK.
// NOTE: This function is a modified version of
// https://github.com/cosmos/cosmos-sdk/blob/7e6948f50cd4838a0161838a099f74e0b5b0213c/x/auth/simulation/genesis.go#L26
func randomGenesisAccounts(simState *module.SimulationState) types.GenesisAccounts {
func RandomGenesisAccounts(simState *module.SimulationState) types.GenesisAccounts {
genesisAccs := make(types.GenesisAccounts, len(simState.Accounts))
var funderAddress string
if int(simState.NumBonded) < len(simState.Accounts) {
funderAddress = simState.Accounts[simState.NumBonded].Address.String() // acc at index NumBonded is designated funder
}
for i, acc := range simState.Accounts {
bacc := types.NewBaseAccountWithAddress(acc.Address)

// Only consider making a vesting account once the initial bonded validator
// set is exhausted due to needing to track DelegatedVesting.
if !(int64(i) > simState.NumBonded && simState.Rand.Intn(100) < 50) {
if int64(i) <= simState.NumBonded || simState.Rand.Intn(100) < 50 {
genesisAccs[i] = bacc
continue
}

initialVestingAmount, err := rand.Int(rand.Reader, simState.InitialStake.BigInt())
maxInitialVesting := simState.InitialStake.Quo(math.NewInt(int64(len(simState.Accounts))))
initialVestingAmount, err := rand.Int(rand.Reader, maxInitialVesting.BigInt())
if err != nil {
panic(err)
}
initialVesting := sdk.NewCoins(sdk.NewCoin(simState.BondDenom, sdkmath.NewIntFromBigInt(initialVestingAmount)))
initialVesting := sdk.NewCoins(sdk.NewCoin(simState.BondDenom, math.NewIntFromBigInt(initialVestingAmount)))

var endTime int64
startTime := simState.GenTimestamp.Unix()
Expand All @@ -58,16 +64,12 @@ func randomGenesisAccounts(simState *module.SimulationState) types.GenesisAccoun
endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*12))))
}

bva, err := vestingtypes.NewBaseVestingAccount(bacc, initialVesting, endTime)
bva, err := sdkvestingtypes.NewBaseVestingAccount(bacc, initialVesting, endTime)
if err != nil {
panic(err)
}

if simState.Rand.Intn(100) < 50 {
genesisAccs[i] = vestingtypes.NewContinuousVestingAccountRaw(bva, startTime)
} else {
genesisAccs[i] = vestingtypes.NewDelayedVestingAccountRaw(bva)
}
genesisAccs[i] = vestingtypes.NewClawbackContinuousVestingAccountRaw(bva, startTime, funderAddress)
}

return genesisAccs
Expand Down
3 changes: 2 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

Expand All @@ -28,6 +27,8 @@ import (
// ica
icacontrollerkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/keeper"
icahostkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/keeper"

stakingkeeper "github.com/sedaprotocol/seda-chain/x/staking/keeper"
)

type AppKeepers struct {
Expand Down
2 changes: 1 addition & 1 deletion app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +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"
"github.com/cosmos/cosmos-sdk/x/staking"
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
Expand Down
23 changes: 15 additions & 8 deletions cmd/seda-chaind/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"

vestingtypes "github.com/sedaprotocol/seda-chain/x/vesting/types"
)

const (
flagVestingStart = "vesting-start-time"
flagVestingEnd = "vesting-end-time"
flagVestingAmt = "vesting-amount"
flagFunder = "funder"
)

// addGenesisAccountCmd returns add-genesis-account cobra Command.
Expand Down Expand Up @@ -111,14 +114,17 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
return errors.New("vesting amount cannot be greater than total amount")
}

switch {
case vestingStart != 0 && vestingEnd != 0:
genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart)

case vestingEnd != 0:
genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount)

default:
if vestingEnd != 0 && vestingStart != 0 {
funder, err := cmd.Flags().GetString(flagFunder)
if err != nil {
return err
}
funderAddr, err := sdk.AccAddressFromBech32(funder)
if err != nil {
return errors.New("funder of vesting account must be provided using from flag")
}
Comment on lines +123 to +125
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message is a bit ambiguous to me. "invalid funder address" would make more sense imo. funder on line 118 would be "" if no funder address was provided in the flag.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to add a check for vestingStart > vestingEnd.

genAccount = vestingtypes.NewClawbackContinuousVestingAccountRaw(baseVestingAccount, vestingStart, funderAddr.String())
} else {
return errors.New("invalid vesting parameters; must supply start and end time or end time")
}
} else {
Expand Down Expand Up @@ -190,6 +196,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
cmd.Flags().String(flagFunder, "", "funder address for creating clawback vesting account")
flags.AddQueryFlagsToCmd(cmd)

return cmd
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
mvdan.cc/gofumpt v0.5.0 // indirect
nhooyr.io/websocket v1.8.6 // indirect
pgregory.net/rapid v1.1.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1787,8 +1787,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
mvdan.cc/gofumpt v0.5.0 h1:0EQ+Z56k8tXjj/6TQD25BFNKQXpCvT0rnansIc7Ug5E=
mvdan.cc/gofumpt v0.5.0/go.mod h1:HBeVDtMKRZpXyxFciAirzdKklDlGu8aAy1wEbH5Y9js=
nhooyr.io/websocket v1.8.6 h1:s+C3xAMLwGmlI31Nyn/eAehUlZPwfYZu2JXM621Q5/k=
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw=
Expand Down
Loading
Loading