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

Prep v32 #517

Merged
merged 11 commits into from
May 27, 2024
2 changes: 1 addition & 1 deletion .github/workflows/software-upgrade-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ jobs:

- name: Submit new proposal
run: |
GOMEMLIMIT=32GiB \
GOMEMLIMIT=8GiB \
${{ needs.build-new-binary.outputs.NEW_UPGRADE_ASSURE_BINARY_PATH }} \
${{ needs.retrieve-snapshot.outputs.SNAPSHOT_FILE_PATH }} \
${{ needs.retrieve-old-binary.outputs.OLD_BINARY_PATH }} \
Expand Down
44 changes: 44 additions & 0 deletions app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Dependencies map

## Keepers dependencies

`commitment` -> `staking`
`tokenomics` -> `commitment`
`estaking` -> `staking`, `commitment`, `distr`, `tokenomics`
`slashing` -> `estaking`
`amm` -> `commitment`
`stablestake` -> `commitment`
`distr` -> `commitment`, `estaking`
`masterchef` -> `tokenomics`
`leveragelp` -> `stablestake`, `commitment`

## Hooks dependencies

`staking` -> `slashing`, `distr`, `estaking`
`amm` -> `perpetual`, `leveragelp`, `masterchef`
`epochs` -> `oracle`, `commitment`, `perpetual`
`perpetual` -> `accountedpool`
`commitment` -> `estaking`
`stablestake` -> `masterchef`

## The deepest stack actions

### Eden uncommit

`commitment` hooks `estaking` with `EdenUncommitted`
`estaking` calls `commitment` with `BurnEdenBoost`
`commitment` hooks `BeforeEdenBCommitChange`, `BeforeEdenCommitChange`, `CommitmentChanged`
`estaking` hooks `staking` with `BeforeDelegationCreated`, `BeforeDelegationSharesModified`, `BeforeDelegationRemoved`, `AfterDelegationModified`
`staking` hooks `estaking` with `AfterDelegationModified`
`staking` hooks `distr` with `BeforeDelegationCreated`, `BeforeDelegationSharesModified`, `BeforeDelegationRemoved`, `AfterDelegationModified`
`distr` executes rewards claim on `BeforeDelegationSharesModified`

### Elys unstake

`staking` hooks `AfterDelegationModified`
`estaking` calls `commitment` with `BurnEdenBoost`
`commitment` hooks `BeforeEdenBCommitChange`, `BeforeEdenCommitChange`, `CommitmentChanged`
`estaking` hooks `staking` with `BeforeDelegationCreated`, `BeforeDelegationSharesModified`, `BeforeDelegationRemoved`, `AfterDelegationModified`
`staking` hooks `estaking` with `AfterDelegationModified`
`staking` hooks `distr` with `BeforeDelegationCreated`, `BeforeDelegationSharesModified`, `BeforeDelegationRemoved`, `AfterDelegationModified`
`distr` executes rewards claim on `BeforeDelegationSharesModified`
10 changes: 5 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import (
upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibc_hooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7"
ibchooks "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7"
ibchookskeeper "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/keeper"
ibchookstypes "github.com/cosmos/ibc-apps/modules/ibc-hooks/v7/types"
ica "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts"
Expand Down Expand Up @@ -412,8 +412,8 @@ type ElysApp struct {
configurator module.Configurator

// Middleware wrapper
Ics20WasmHooks *ibc_hooks.WasmHooks
HooksICS4Wrapper ibc_hooks.ICS4Middleware
Ics20WasmHooks *ibchooks.WasmHooks
HooksICS4Wrapper ibchooks.ICS4Middleware
}

// New returns a reference to an initialized blockchain app
Expand Down Expand Up @@ -940,9 +940,9 @@ func NewElysApp(
)
app.IBCHooksKeeper = &hooksKeeper

wasmHooks := ibc_hooks.NewWasmHooks(app.IBCHooksKeeper, &app.WasmKeeper, AccountAddressPrefix) // The contract keeper needs to be set later
wasmHooks := ibchooks.NewWasmHooks(app.IBCHooksKeeper, &app.WasmKeeper, AccountAddressPrefix) // The contract keeper needs to be set later
app.Ics20WasmHooks = &wasmHooks
app.HooksICS4Wrapper = ibc_hooks.NewICS4Middleware(
app.HooksICS4Wrapper = ibchooks.NewICS4Middleware(
app.IBCKeeper.ChannelKeeper,
app.Ics20WasmHooks,
)
Expand Down
36 changes: 30 additions & 6 deletions app/setup_handlers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package app

import (
"cosmossdk.io/math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
m "github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

Expand All @@ -21,13 +24,34 @@ func setUpgradeHandler(app *ElysApp) {
func(ctx sdk.Context, plan upgradetypes.Plan, vm m.VersionMap) (m.VersionMap, error) {
app.Logger().Info("Running upgrade handler for " + version.Version)

// if version.Version == "v0.32.0" {
app.Logger().Info("Deleting proposals with ID <= 193")
store := ctx.KVStore(app.keys[govtypes.StoreKey])
for i := uint64(1); i <= 193; i++ {
store.Delete(govtypes.ProposalKey(i))
if version.Version == "v0.32.0" || version.Version == "v999.999.999" {
// Since invariant is broken sending missing amount to bonded pool
sumOfValTokens := math.ZeroInt()
app.EstakingKeeper.IterateBondedValidatorsByPower(ctx, func(_ int64, validator stakingtypes.ValidatorI) bool {
sumOfValTokens = sumOfValTokens.Add(validator.GetTokens())
return false
})

totalBondedTokens := app.EstakingKeeper.TotalBondedTokens(ctx)
if totalBondedTokens.LT(sumOfValTokens) {
missingAmount := sumOfValTokens.Sub(totalBondedTokens)
missingCoins := sdk.Coins{sdk.NewCoin("uelys", missingAmount)}
err := app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, missingCoins)
if err != nil {
panic(err)
}
err = app.BankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, stakingtypes.BondedPoolName, missingCoins)
if err != nil {
panic(err)
}
}

app.Logger().Info("Deleting proposals with ID <= 193")
store := ctx.KVStore(app.keys[govtypes.StoreKey])
for i := uint64(1); i <= 193; i++ {
store.Delete(govtypes.ProposalKey(i))
}
}
// }

return app.mm.RunMigrations(ctx, app.configurator, vm)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/upgrade-assure/update-genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func updateGenesis(validatorBalance, homePath, genesisFilePath string) {
genesis.AppState.Bank.Balances = append(genesis.AppState.Bank.Balances, genesisInit.AppState.Bank.Balances...)

// Add distribution account balance
distributionCoins := sdk.NewCoins(sdk.NewInt64Coin("ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", 128141787))
distributionCoins := sdk.NewCoins(sdk.NewInt64Coin("ibc/2180E84E20F5679FCC760D8C165B60F42065DEF7F46A72B447CFF1B7DC6C0A65", 127502189))
genesis.AppState.Bank.Balances = append(
genesis.AppState.Bank.Balances,
banktypes.Balance{
Expand Down
41 changes: 41 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40363,6 +40363,40 @@ paths:
type: boolean
tags:
- Query
/elys-network/elys/estaking/invariant:
get:
summary: Invariant queries the invariant values
operationId: ElysEstakingInvariant
responses:
'200':
description: A successful response.
schema:
type: object
properties:
total_bonded:
type: string
bonded_validator_tokens_sum:
type: string
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
tags:
- Query
/elys-network/elys/estaking/params:
get:
summary: Parameters queries the parameters of the module.
Expand Down Expand Up @@ -86071,6 +86105,13 @@ definitions:
Accumulated amount at distribution epoch - recalculated at every
distribution epoch
description: Params defines the parameters for the module.
elys.estaking.QueryInvariantResponse:
type: object
properties:
total_bonded:
type: string
bonded_validator_tokens_sum:
type: string
elys.estaking.QueryParamsResponse:
type: object
properties:
Expand Down
18 changes: 17 additions & 1 deletion proto/elys/estaking/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ service Query {
rpc Rewards(QueryRewardsRequest) returns (QueryRewardsResponse) {
option (google.api.http).get = "/elys-network/elys/estaking/rewards/{address}";
}
// Invariant queries the invariant values
rpc Invariant(QueryInvariantRequest) returns (QueryInvariantResponse) {
option (google.api.http).get = "/elys-network/elys/estaking/invariant";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -50,4 +54,16 @@ message QueryRewardsResponse {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}
}

message QueryInvariantRequest {}
message QueryInvariantResponse {
string total_bonded = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string bonded_validator_tokens_sum = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
26 changes: 26 additions & 0 deletions x/estaking/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(
CmdQueryParams(),
CmdQueryRewards(),
CmdQueryInvariant(),
)
// this line is used by starport scaffolding # 1

Expand Down Expand Up @@ -87,3 +88,28 @@ func CmdQueryRewards() *cobra.Command {

return cmd
}

func CmdQueryInvariant() *cobra.Command {
cmd := &cobra.Command{
Use: "invariant",
Short: "Query invariant values",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Invariant(cmd.Context(), &types.QueryInvariantRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
6 changes: 3 additions & 3 deletions x/estaking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func (k Keeper) EndBlocker(ctx sdk.Context) {
k.BurnEdenBIfElysStakingReduced(ctx)
}

func (k Keeper) TakeDelegationSnapshot(ctx sdk.Context, addr string) {
func (k Keeper) TakeDelegationSnapshot(ctx sdk.Context, addr sdk.AccAddress) {
// Calculate delegated amount per delegator
delAmount := k.CalcDelegationAmount(ctx, addr)

elysStaked := types.ElysStaked{
Address: addr,
Address: addr.String(),
Amount: delAmount,
}

Expand All @@ -38,7 +38,7 @@ func (k Keeper) BurnEdenBIfElysStakingReduced(ctx sdk.Context) {
// This hook is exposed for genesis delegations as well
for _, delAddr := range addrs {
k.BurnEdenBFromElysUnstaking(ctx, delAddr)
k.TakeDelegationSnapshot(ctx, delAddr.String())
k.TakeDelegationSnapshot(ctx, delAddr)
k.RemoveElysStakeChange(ctx, delAddr)
}
}
Expand Down
3 changes: 1 addition & 2 deletions x/estaking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (k Keeper) CommitmentChanged(ctx sdk.Context, creator string, amount sdk.Co

// Process eden uncommitted hook
func (k Keeper) EdenUncommitted(ctx sdk.Context, creator string, amount sdk.Coin) error {
k.BurnEdenBFromEdenUncommitted(ctx, creator, amount.Amount)
return nil
return k.BurnEdenBFromEdenUncommitted(ctx, creator, amount.Amount)
}

func (k Keeper) BeforeEdenInitialCommit(ctx sdk.Context, addr sdk.AccAddress) error {
Expand Down
7 changes: 5 additions & 2 deletions x/estaking/keeper/keeper_burn_edenB.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k Keeper) BurnEdenBFromElysUnstaking(ctx sdk.Context, delegator sdk.AccAdd
}

// Calculate current delegated amount of delegator
delAmount := k.CalcDelegationAmount(ctx, delAddr)
delAmount := k.CalcDelegationAmount(ctx, delegator)

// If not unstaked,
// should return nil otherwise it will break staking module
Expand Down Expand Up @@ -50,9 +50,12 @@ func (k Keeper) BurnEdenBFromElysUnstaking(ctx sdk.Context, delegator sdk.AccAdd
}

// Burn EdenB in commitment module
err := k.commKeeper.BurnEdenBoost(ctx, delAddr, ptypes.EdenB, edenBToBurn.TruncateInt())
cacheCtx, write := ctx.CacheContext()
err := k.commKeeper.BurnEdenBoost(cacheCtx, delAddr, ptypes.EdenB, edenBToBurn.TruncateInt())
if err != nil {
k.Logger(ctx).Error("EdenB burn failure", err)
} else {
write()
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/estaking/keeper/keeper_burn_edenB_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestBurnEdenBFromElysUnstaked(t *testing.T) {
simapp.AddTestCommitment(app, ctx, genAccount, committed)

// Take elys staked snapshot
ek.TakeDelegationSnapshot(ctx, genAccount.String())
ek.TakeDelegationSnapshot(ctx, genAccount)

// burn amount = 100000 (unbonded amt) / (1000000 (elys staked) + 10000 (Eden committed)) * (20000 EdenB + 5000 EdenB committed)
unbondAmt, err := sk.Unbond(ctx, genAccount, valAddr, sdk.NewDecWithPrec(10, 2))
Expand Down
20 changes: 3 additions & 17 deletions x/estaking/keeper/keeper_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ import (
)

// Calculate the delegated amount
func (k Keeper) CalcDelegationAmount(ctx sdk.Context, delegator string) math.Int {
// Derivate bech32 based delegator address
delAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
// This could be validator address
return sdk.ZeroInt()
}

func (k Keeper) CalcDelegationAmount(ctx sdk.Context, delegator sdk.AccAddress) math.Int {
// Get elys delegation
delAmount := sdk.ZeroDec()
delegations := k.Keeper.GetDelegatorDelegations(ctx, delAddr, gomath.MaxUint16)
delegations := k.Keeper.GetDelegatorDelegations(ctx, delegator, gomath.MaxUint16)
for _, del := range delegations {
valAddr := del.GetValidatorAddr()
val := k.Keeper.Validator(ctx, valAddr)
Expand All @@ -32,14 +25,7 @@ func (k Keeper) CalcDelegationAmount(ctx sdk.Context, delegator string) math.Int
}

// Calculate delegation to bonded validators
func (k Keeper) CalcBondedDelegationAmount(ctx sdk.Context, delegator string) math.Int {
// Derivate bech32 based delegator address
delAddr, err := sdk.AccAddressFromBech32(delegator)
if err != nil {
// This could be validator address
return sdk.ZeroInt()
}

func (k Keeper) CalcBondedDelegationAmount(ctx sdk.Context, delAddr sdk.AccAddress) math.Int {
// Get elys delegation for creator address
delAmount := sdk.ZeroDec()
delegations := k.Keeper.GetDelegatorDelegations(ctx, delAddr, gomath.MaxUint16)
Expand Down
4 changes: 2 additions & 2 deletions x/estaking/keeper/keeper_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestCalcDelegationAmount(t *testing.T) {
addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000))

// Check with non-delegator
delegatedAmount := ek.CalcDelegationAmount(ctx, addr[0].String())
delegatedAmount := ek.CalcDelegationAmount(ctx, addr[0])
require.Equal(t, delegatedAmount, sdk.ZeroInt())

// Check with genesis account (delegator)
delegatedAmount = ek.CalcDelegationAmount(ctx, genAccount.String())
delegatedAmount = ek.CalcDelegationAmount(ctx, genAccount)
require.Equal(t, delegatedAmount, sdk.DefaultPowerReduction)
}
Loading