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

[Application] Enforce minimum stake when staking #847

Draft
wants to merge 4 commits into
base: issues/612/application/min-stake-param
Choose a base branch
from
Draft
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
39 changes: 19 additions & 20 deletions x/application/keeper/msg_server_delegate_to_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"fmt"
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

keepertest "github.com/pokt-network/poktroll/testutil/keeper"
"github.com/pokt-network/poktroll/testutil/sample"
"github.com/pokt-network/poktroll/x/application/keeper"
"github.com/pokt-network/poktroll/x/application/types"
apptypes "github.com/pokt-network/poktroll/x/application/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

Expand All @@ -28,9 +27,9 @@ func TestMsgServer_DelegateToGateway_SuccessfullyDelegate(t *testing.T) {
keepertest.AddGatewayToStakedGatewayMap(t, gatewayAddr2)

// Prepare the application
stakeMsg := &types.MsgStakeApplication{
stakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: &apptypes.DefaultMinStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{
ServiceId: "svc1",
Expand All @@ -45,7 +44,7 @@ func TestMsgServer_DelegateToGateway_SuccessfullyDelegate(t *testing.T) {
require.True(t, isAppFound)

// Prepare the delegation message
delegateMsg := &types.MsgDelegateToGateway{
delegateMsg := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr1,
}
Expand All @@ -72,7 +71,7 @@ func TestMsgServer_DelegateToGateway_SuccessfullyDelegate(t *testing.T) {
require.Equal(t, gatewayAddr1, foundApp.DelegateeGatewayAddresses[0])

// Prepare a second delegation message
delegateMsg2 := &types.MsgDelegateToGateway{
delegateMsg2 := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr2,
}
Expand Down Expand Up @@ -108,9 +107,9 @@ func TestMsgServer_DelegateToGateway_FailDuplicate(t *testing.T) {
keepertest.AddGatewayToStakedGatewayMap(t, gatewayAddr)

// Prepare the application
stakeMsg := &types.MsgStakeApplication{
stakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: &apptypes.DefaultMinStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{
ServiceId: "svc1",
Expand All @@ -125,7 +124,7 @@ func TestMsgServer_DelegateToGateway_FailDuplicate(t *testing.T) {
require.True(t, isAppFound)

// Prepare the delegation message
delegateMsg := &types.MsgDelegateToGateway{
delegateMsg := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr,
}
Expand All @@ -152,14 +151,14 @@ func TestMsgServer_DelegateToGateway_FailDuplicate(t *testing.T) {
require.Equal(t, gatewayAddr, foundApp.DelegateeGatewayAddresses[0])

// Prepare a second delegation message
delegateMsg2 := &types.MsgDelegateToGateway{
delegateMsg2 := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr,
}

// Attempt to delegate the application to the gateway again
_, err = srv.DelegateToGateway(ctx, delegateMsg2)
require.ErrorIs(t, err, types.ErrAppAlreadyDelegated)
require.ErrorIs(t, err, apptypes.ErrAppAlreadyDelegated)
events = sdkCtx.EventManager().Events()
require.Equal(t, 1, len(events))
foundApp, isAppFound = k.GetApplication(ctx, appAddr)
Expand All @@ -177,9 +176,9 @@ func TestMsgServer_DelegateToGateway_FailGatewayNotStaked(t *testing.T) {
gatewayAddr := sample.AccAddress()

// Prepare the application
stakeMsg := &types.MsgStakeApplication{
stakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: &apptypes.DefaultMinStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{
ServiceId: "svc1",
Expand All @@ -194,14 +193,14 @@ func TestMsgServer_DelegateToGateway_FailGatewayNotStaked(t *testing.T) {
require.True(t, isAppFound)

// Prepare the delegation message
delegateMsg := &types.MsgDelegateToGateway{
delegateMsg := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr,
}

// Attempt to delegate the application to the unstaked gateway
_, err = srv.DelegateToGateway(ctx, delegateMsg)
require.ErrorIs(t, err, types.ErrAppGatewayNotFound)
require.ErrorIs(t, err, apptypes.ErrAppGatewayNotFound)
foundApp, isAppFound := k.GetApplication(ctx, appAddr)
require.True(t, isAppFound)
require.Equal(t, 0, len(foundApp.DelegateeGatewayAddresses))
Expand All @@ -215,9 +214,9 @@ func TestMsgServer_DelegateToGateway_FailMaxReached(t *testing.T) {
appAddr := sample.AccAddress()

// Prepare the application
stakeMsg := &types.MsgStakeApplication{
stakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: &apptypes.DefaultMinStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{
ServiceId: "svc1",
Expand All @@ -240,7 +239,7 @@ func TestMsgServer_DelegateToGateway_FailMaxReached(t *testing.T) {
gatewayAddresses[i] = gatewayAddr
// Mock the gateway being staked via the staked gateway map
keepertest.AddGatewayToStakedGatewayMap(t, gatewayAddr)
delegateMsg := &types.MsgDelegateToGateway{
delegateMsg := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr,
}
Expand Down Expand Up @@ -270,14 +269,14 @@ func TestMsgServer_DelegateToGateway_FailMaxReached(t *testing.T) {
keepertest.AddGatewayToStakedGatewayMap(t, gatewayAddr)

// Prepare the delegation message
delegateMsg := &types.MsgDelegateToGateway{
delegateMsg := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
GatewayAddress: gatewayAddr,
}

// Attempt to delegate the application when the max is already reached
_, err = srv.DelegateToGateway(ctx, delegateMsg)
require.ErrorIs(t, err, types.ErrAppMaxDelegatedGateways)
require.ErrorIs(t, err, apptypes.ErrAppMaxDelegatedGateways)
events = sdkCtx.EventManager().Events()
require.Equal(t, int(maxDelegatedParam), len(events))
foundApp, isStakedAppFound := k.GetApplication(ctx, appAddr)
Expand Down
38 changes: 28 additions & 10 deletions x/application/keeper/msg_server_stake_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/pokt-network/poktroll/telemetry"
"github.com/pokt-network/poktroll/x/application/types"
Expand All @@ -23,7 +25,7 @@ func (k msgServer) StakeApplication(ctx context.Context, msg *types.MsgStakeAppl

if err := msg.ValidateBasic(); err != nil {
logger.Error(fmt.Sprintf("invalid MsgStakeApplication: %v", err))
return nil, err
return nil, status.Error(codes.InvalidArgument, err.Error())
}

// Check if the application already exists or not
Expand All @@ -38,38 +40,54 @@ func (k msgServer) StakeApplication(ctx context.Context, msg *types.MsgStakeAppl
logger.Info(fmt.Sprintf("Application found. About to try and update application for address %q", msg.Address))
currAppStake := *foundApp.Stake
if err = k.updateApplication(ctx, &foundApp, msg); err != nil {
logger.Error(fmt.Sprintf("could not update application for address %q due to error %v", msg.Address, err))
return nil, err
logger.Info(fmt.Sprintf("could not update application for address %q due to error %v", msg.Address, err))
return nil, status.Error(codes.InvalidArgument, err.Error())
}
coinsToEscrow, err = (*msg.Stake).SafeSub(currAppStake)
if err != nil {
logger.Error(fmt.Sprintf("could not calculate coins to escrow due to error %v", err))
return nil, err
logger.Info(fmt.Sprintf("could not calculate coins to escrow due to error %v", err))
return nil, status.Error(codes.InvalidArgument, err.Error())
}
logger.Info(fmt.Sprintf("Application is going to escrow an additional %+v coins", coinsToEscrow))

// If the application has initiated an unstake action, cancel it since it is staking again.
foundApp.UnstakeSessionEndHeight = types.ApplicationNotUnstaking
}

// Must always stake or upstake (> 0 delta)
// MUST ALWAYS stake or upstake (> 0 delta)
if coinsToEscrow.IsZero() {
logger.Warn(fmt.Sprintf("Application %q must escrow more than 0 additional coins", msg.Address))
return nil, types.ErrAppInvalidStake.Wrapf("application %q must escrow more than 0 additional coins", msg.Address)
return nil, status.Error(
codes.InvalidArgument,
types.ErrAppInvalidStake.Wrapf(
"application %q must escrow more than 0 additional coins",
msg.Address,
).Error())
}

// MUST ALWAYS have at least minimum stake.
minStake := k.GetParams(ctx).MinStake
if msg.Stake.Amount.LT(minStake.Amount) {
errFmt := "application %q must stake at least %s"
logger.Info(fmt.Sprintf(errFmt, msg.Address, minStake))
return nil, status.Error(
codes.InvalidArgument,
types.ErrAppInvalidStake.Wrapf(errFmt, msg.Address, minStake).Error(),
)
}

// Retrieve the address of the application
appAddress, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
logger.Error(fmt.Sprintf("could not parse address %q", msg.Address))
return nil, err
logger.Info(fmt.Sprintf("could not parse address %q", msg.Address))
return nil, status.Error(codes.InvalidArgument, err.Error())
}

// Send the coins from the application to the staked application pool
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, appAddress, types.ModuleName, []sdk.Coin{coinsToEscrow})
if err != nil {
logger.Error(fmt.Sprintf("could not send %v coins from %q to %q module account due to %v", coinsToEscrow, appAddress, types.ModuleName, err))
return nil, err
return nil, status.Error(codes.Internal, err.Error())
}
logger.Info(fmt.Sprintf("Successfully escrowed %v coins from %q to %q module account", coinsToEscrow, appAddress, types.ModuleName))

Expand Down
Loading
Loading