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

docker fixes + reward distribution transfer function #418

Merged
merged 4 commits into from
Jan 18, 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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION="1.19"
ARG GO_VERSION="1.20"
ARG RUNNER_IMAGE="gcr.io/distroless/static-debian11"

# --------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION="1.19"
ARG GO_VERSION="1.20"
ARG RUNNER_IMAGE="alpine:3.16"

# --------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ lint:
### Proto ###
###############################################################################

protoVer=0.11.6
protoVer=0.12.1
protoImageName=ghcr.io/cosmos/proto-builder:$(protoVer)
containerProtoGen=proto-gen-$(protoVer)
containerProtoFmt=proto-fmt-$(protoVer)
Expand Down
3 changes: 2 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.AccountKeeper,
)

appKeepers.StakingMiddlewareKeeper = stakingmiddleware.NewKeeper(appCodec, appKeepers.keys[stakingmiddlewaretypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
appKeepers.StakingMiddlewareKeeper = stakingmiddleware.NewKeeper(appCodec, appKeepers.keys[stakingmiddlewaretypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String())

appKeepers.StakingKeeper = customstaking.NewKeeper(
appCodec, appKeepers.keys[stakingtypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), &appKeepers.StakingMiddlewareKeeper,
Expand Down Expand Up @@ -238,6 +238,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
)

appKeepers.BankKeeper.RegisterKeepers(appKeepers.AllianceKeeper, appKeepers.StakingKeeper)
appKeepers.StakingMiddlewareKeeper.RegisterKeepers(appKeepers.StakingKeeper)
// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
appKeepers.StakingKeeper.SetHooks(
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v6_4/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func CreateUpgradeHandler(
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
// Add params for custom middleware
custommiddlewareparams := customstmiddleware.Params{BlocksPerEpoch: 100, AllowUnbondAfterEpochProgressBlockNumber: 0}
custommiddlewareparams := customstmiddleware.Params{BlocksPerEpoch: 360, AllowUnbondAfterEpochProgressBlockNumber: 0}
keepers.StakingMiddlewareKeeper.SetParams(ctx, custommiddlewareparams)

return mm.RunMigrations(ctx, configurator, vm)
Expand Down
15 changes: 15 additions & 0 deletions proto/composable/stakingmiddleware/v1beta1/tx.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
syntax = "proto3";
package composable.stakingmiddleware.v1beta1;

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/msg/v1/msg.proto";
import "amino/amino.proto";
import "gogoproto/gogo.proto";
Expand All @@ -14,6 +15,7 @@ service Msg {
option (cosmos.msg.v1.service) = true;

rpc UpdateEpochParams(MsgUpdateEpochParams) returns (MsgUpdateParamsEpochResponse);
rpc AddRevenueFundsToStaking(MsgAddRevenueFundsToStakingParams) returns (MsgAddRevenueFundsToStakingResponse);
}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand All @@ -39,3 +41,16 @@ message MsgUpdateEpochParams {
//
// Since: cosmos-sdk 0.47
message MsgUpdateParamsEpochResponse {}

message MsgAddRevenueFundsToStakingParams {
option (cosmos.msg.v1.signer) = "from_address";

string from_address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
repeated cosmos.base.v1beta1.Coin amount = 3 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

message MsgAddRevenueFundsToStakingResponse {}
4 changes: 2 additions & 2 deletions scripts/testnode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LOGLEVEL="info"
TRACE=""

# remove existing daemon
rm -rf ~/.centauri*
rm -rf ~/.banksy*

centaurid config keyring-backend $KEYRING
centaurid config chain-id $CHAINID
Expand Down Expand Up @@ -39,7 +39,7 @@ fi

# update request max size so that we can upload the light client
# '' -e is a must have params on mac, if use linux please delete before run
sed -i'' -e 's/max_body_bytes = /max_body_bytes = 1/g' ~/.centauri/config/config.toml
sed -i'' -e 's/max_body_bytes = /max_body_bytes = 1/g' ~/.banksy/config/config.toml

# Start the node (remove the --pruning=nothing flag if historical queries are not needed)
centaurid start --pruning=nothing --minimum-gas-prices=0.0001stake --rpc.laddr tcp://0.0.0.0:26657
38 changes: 35 additions & 3 deletions x/stakingmiddleware/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/notional-labs/composable/v6/x/stakingmiddleware/types"
"github.com/spf13/cobra"
)

// GetTxCmd returns the tx commands for staking middleware module.
Expand All @@ -17,7 +19,37 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

txCmd.AddCommand()
txCmd.AddCommand(
GetCmdAddRevenueFundsToStaking(),
)

return txCmd
}

func GetCmdAddRevenueFundsToStaking() *cobra.Command {
cmd := &cobra.Command{
Use: "add-revenue [amount]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

coins, err := sdk.ParseCoinsNormalized(args[0])
if err != nil {
return err
}

msg := &types.MsgAddRevenueFundsToStakingParams{
FromAddress: clientCtx.GetFromAddress().String(),
Amount: coins,
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)

return cmd
}
29 changes: 24 additions & 5 deletions x/stakingmiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
banktypes "github.com/notional-labs/composable/v6/custom/bank/types"
)

// Keeper of the staking middleware store
type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
stakingKeeper banktypes.StakingKeeper
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
Expand All @@ -24,15 +29,24 @@ type Keeper struct {
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
ak types.AccountKeeper,
bk types.BankKeeper,
authority string,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: key,
authority: authority,
cdc: cdc,
storeKey: key,
accountKeeper: ak,
bankKeeper: bk,
stakingKeeper: stakingkeeper.Keeper{},
authority: authority,
}
}

func (k *Keeper) RegisterKeepers(sk banktypes.StakingKeeper) {
k.stakingKeeper = sk
}

// GetAuthority returns the x/stakingmiddleware module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
Expand Down Expand Up @@ -74,3 +88,8 @@ func (k Keeper) GetParams(ctx sdk.Context) (p types.Params) {
k.cdc.MustUnmarshal(bz, &p)
return p
}

func (k Keeper) GetModuleAccountAccAddress(ctx sdk.Context) sdk.AccAddress {
moduleAccount := k.accountKeeper.GetModuleAccount(ctx, types.RewardModuleName)
return moduleAccount.GetAddress()
}
28 changes: 28 additions & 0 deletions x/stakingmiddleware/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ func (ms msgServer) UpdateEpochParams(goCtx context.Context, req *types.MsgUpdat

return &types.MsgUpdateParamsEpochResponse{}, nil
}

// UpdateParams updates the params.
func (ms msgServer) AddRevenueFundsToStaking(goCtx context.Context, req *types.MsgAddRevenueFundsToStakingParams) (*types.MsgAddRevenueFundsToStakingResponse, error) {
// Unwrap context
ctx := sdk.UnwrapSDKContext(goCtx)

// Check sender address
sender, err := sdk.AccAddressFromBech32(req.FromAddress)
if err != nil {
return nil, err
}

rewardDenom := ms.Keeper.stakingKeeper.BondDenom(ctx)

// Check that reward is 1 coin rewardDenom
if len(req.Amount.Denoms()) != 1 || req.Amount[0].Denom != rewardDenom {
return nil, errorsmod.Wrapf(types.ErrInvalidCoin, "Invalid coin")
}

// Send Fund to account module
moduleAccountAccAddress := ms.GetModuleAccountAccAddress(ctx)
err = ms.bankKeeper.SendCoins(ctx, sender, moduleAccountAccAddress, req.Amount)
if err != nil {
return nil, err
}

return &types.MsgAddRevenueFundsToStakingResponse{}, nil
}
7 changes: 7 additions & 0 deletions x/stakingmiddleware/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

import (
errorsmod "cosmossdk.io/errors"
)

var ErrInvalidCoin = errorsmod.Register(ModuleName, 1, "invalid coin")
17 changes: 17 additions & 0 deletions x/stakingmiddleware/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types // noalias

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

// AccountKeeper defines the contract required for account APIs.
type AccountKeeper interface {
GetModuleAccount(ctx sdk.Context, moduleName string) types.ModuleAccountI
}

// BankKeeper defines the contract needed to be fulfilled for banking and supply
// dependencies.
type BankKeeper interface {
SendCoins(ctx sdk.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error
}
2 changes: 1 addition & 1 deletion x/stakingmiddleware/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func NewGenesisState(params Params) *GenesisState {
// DefaultGenesisState creates a default GenesisState object
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: Params{BlocksPerEpoch: 10, AllowUnbondAfterEpochProgressBlockNumber: 0},
Params: Params{BlocksPerEpoch: 360, AllowUnbondAfterEpochProgressBlockNumber: 0},
}
}

Expand Down
3 changes: 2 additions & 1 deletion x/stakingmiddleware/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var (

const (
// module name
ModuleName = "stakingmiddleware"
ModuleName = "stakingmiddleware"
RewardModuleName = "fee_collector"

// StoreKey is the default store key for stakingmiddleware module that store params when apply validator set changes and when allow to unbond/redelegate

Expand Down
20 changes: 20 additions & 0 deletions x/stakingmiddleware/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ func (m *MsgUpdateEpochParams) ValidateBasic() error {

return nil
}

// GetSignBytes implements the LegacyMsg interface.
func (m MsgAddRevenueFundsToStakingParams) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgAddRevenueFundsToStakingParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.FromAddress)
return []sdk.AccAddress{addr}
}

// ValidateBasic does a sanity check on the provided data.
func (m *MsgAddRevenueFundsToStakingParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.FromAddress); err != nil {
return errorsmod.Wrapf(err, "invalid address")
}

return nil
}
Loading
Loading