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

Merged
merged 112 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
112 commits
Select commit Hold shift + click to select a range
d1cd5e6
scaffold: message update-param --module gateway --signer authority na…
bryanchriswhite Sep 9, 2024
91eecfc
chore: update gateway MsgUpdateParam fields
bryanchriswhite Sep 9, 2024
ef62280
chore: update MsgUpdateParamResponse fields
bryanchriswhite Sep 23, 2024
f7a6e5b
chore: comment out autocli for gateway module param updates
bryanchriswhite Sep 10, 2024
8957f1c
fix: linter error
bryanchriswhite Sep 26, 2024
3016ca3
revert: premature changes
bryanchriswhite Sep 26, 2024
899effb
feat: add min_stake gateway module param
bryanchriswhite Sep 26, 2024
83c1d74
chore: cleanup makefiles and param update JSON files
bryanchriswhite Sep 23, 2024
55e6a31
chore: add gateway module param make targets
bryanchriswhite Sep 23, 2024
ddd68bb
refctor: consolidate param config types
bryanchriswhite Sep 26, 2024
fa8caf6
tests: improve error messaging
bryanchriswhite Sep 26, 2024
ae006e2
tests: add coverage over gateway min stake param updates
bryanchriswhite Sep 26, 2024
e92e3b1
chore: add as_coin as_type type 😉
bryanchriswhite Sep 26, 2024
b2e1267
chore: add app MsgUpdateParam to genesis authorizations
bryanchriswhite Sep 26, 2024
0373fa2
fix: typo
bryanchriswhite Sep 26, 2024
da7894a
chore: cleanup makefiles
bryanchriswhite Sep 26, 2024
015c537
wip: fixing tests
bryanchriswhite Sep 26, 2024
1ac6c48
tests: add tests
bryanchriswhite Sep 26, 2024
db366f6
tests: remove erroneous case
bryanchriswhite Sep 26, 2024
60bef17
test: gateway staking below minimum fails
bryanchriswhite Sep 27, 2024
a21687f
feat: gateway min stake validation & grpc status error returns
bryanchriswhite Sep 27, 2024
7e9f857
chore: cleanup comments
bryanchriswhite Sep 27, 2024
4140710
scaffold: message update-param --module examplemod --signer authority…
bryanchriswhite Sep 27, 2024
0a8d922
chore: update MsgUpdateParam fields
bryanchriswhite Sep 27, 2024
c88edd6
chore: comment out application MsgUpdateParam autocli
bryanchriswhite Sep 27, 2024
da51239
chore: add application MsgUpdateParam to dao genesis authorizations
bryanchriswhite Sep 27, 2024
45c5a99
chore: update NewMsgUpdateParam constructor
bryanchriswhite Sep 27, 2024
d987e09
fix: simulations generated code
bryanchriswhite Sep 27, 2024
0da255d
chore: add min_stake param to application module params
bryanchriswhite Sep 27, 2024
4542628
chore: update params config for param updates test suite
bryanchriswhite Sep 27, 2024
b81ca9e
chore: add parameter validation
bryanchriswhite Sep 27, 2024
cdff4b3
chore: add param update make targets and JSON msgs
bryanchriswhite Sep 27, 2024
be24753
chore: add default application min_stake param of 1 POKT to genesis c…
bryanchriswhite Sep 27, 2024
45b6e5c
chore: update MsgUpdateParamResponse & add AsUint64 AsType type
bryanchriswhite Sep 27, 2024
b3e139a
feat: implement MsgUpdateParam handler
bryanchriswhite Sep 27, 2024
872202b
test: MsgUpdateParam handler
bryanchriswhite Sep 27, 2024
a49d69b
feat: implement NewMsgUpdateParam constructor
bryanchriswhite Sep 27, 2024
d63a429
chore: add min_stake validation
bryanchriswhite Sep 27, 2024
9040bf0
chore: tidy up
bryanchriswhite Sep 27, 2024
4340590
chore: review feedback improvements
bryanchriswhite Sep 30, 2024
4b8e95b
Merge branch 'main' into issues/612/chore/gateway-msg-update-param
bryanchriswhite Sep 30, 2024
352afa1
chore: review feedback improvements
bryanchriswhite Sep 30, 2024
6afdaef
fix: linter errors
bryanchriswhite Sep 30, 2024
0839ae4
chore: review feedback improvements
bryanchriswhite Sep 30, 2024
34b0d6d
Merge remote-tracking branch 'pokt/issues/612/chore/gateway-msg-updat…
bryanchriswhite Sep 30, 2024
f48fa66
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Sep 30, 2024
6c5e13f
chore: review feedback improvements
bryanchriswhite Sep 30, 2024
a77877b
fix: test error message
bryanchriswhite Sep 30, 2024
9f65d25
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Sep 30, 2024
28962ba
Merge remote-tracking branch 'pokt/main' into issues/612/param/min-st…
bryanchriswhite Sep 30, 2024
497ecd9
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Sep 30, 2024
d92eb59
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Sep 30, 2024
941b801
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Sep 30, 2024
36e5f2e
chore: update message_update_param_test.go
bryanchriswhite Sep 30, 2024
2baa173
fix: genesis tests
bryanchriswhite Sep 30, 2024
7018149
chore: use AsInt64 to update MaxDelegatedGateways for consistency
bryanchriswhite Sep 30, 2024
c36fe79
chore: ensure application stake handler returns grpc status errors
bryanchriswhite Sep 30, 2024
eab9417
tests: update app stake msg handler unit tests
bryanchriswhite Sep 30, 2024
da9b322
feat: enforce application min stake when staking
bryanchriswhite Sep 30, 2024
5e47c98
fix: failing unit tests
bryanchriswhite Sep 30, 2024
891a858
chore: review feedback improvements
bryanchriswhite Oct 2, 2024
08e99ba
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Oct 2, 2024
da9a240
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 2, 2024
d85d969
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Oct 2, 2024
47da555
Merge branch 'issues/612/application/min-stake-param' into issues/612…
bryanchriswhite Oct 2, 2024
ccc3f53
fix: e2e test
bryanchriswhite Oct 2, 2024
47e0993
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Oct 2, 2024
c08abfd
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 2, 2024
8336d76
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Oct 2, 2024
9386791
Merge branch 'issues/612/application/min-stake-param' into issues/612…
bryanchriswhite Oct 2, 2024
b29843e
Empty commit
bryanchriswhite Oct 2, 2024
8c25061
Empty commit
bryanchriswhite Oct 2, 2024
3f59041
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Oct 2, 2024
4e259c9
chore: apply improvements
bryanchriswhite Oct 2, 2024
2808925
chore: add godoc style comment to min_stake params field
bryanchriswhite Oct 2, 2024
197af04
chore: add godoc style comment to min_stake params field
bryanchriswhite Oct 2, 2024
2a33127
chore: improvements:
bryanchriswhite Oct 4, 2024
3280142
chore: improve logging & ensure gRPC status error returns
bryanchriswhite Oct 4, 2024
98d1c96
chore: improve logging & ensure gRPC status error returns
bryanchriswhite Oct 4, 2024
c256eae
chore: review feedback improvements
bryanchriswhite Oct 4, 2024
0d36e0f
Merge remote-tracking branch 'pokt/main' into issues/612/param/min-st…
bryanchriswhite Oct 4, 2024
00914b0
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Oct 4, 2024
f3fa183
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 4, 2024
f4b8284
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Oct 4, 2024
12d1b43
Merge branch 'issues/612/application/min-stake-param' into issues/612…
bryanchriswhite Oct 4, 2024
30b48f4
test: simplify coin equality assertions
bryanchriswhite Oct 4, 2024
1c309b8
Empty commit
bryanchriswhite Oct 4, 2024
e7ff959
Merge branch 'issues/612/param/min-stake-gateway' into issues/612/gat…
bryanchriswhite Oct 4, 2024
1257f6e
Merge branch 'main' into issues/612/gateway/logic
bryanchriswhite Oct 4, 2024
b972e3b
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 4, 2024
44257f3
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Oct 4, 2024
561a1aa
Merge branch 'issues/612/application/min-stake-param' into issues/612…
bryanchriswhite Oct 4, 2024
4d2c2d9
chore: review feedback improvements
bryanchriswhite Oct 4, 2024
5ba2a48
fix: typo
bryanchriswhite Oct 4, 2024
3e4ef9e
fix: linter errors
bryanchriswhite Oct 4, 2024
8c7eadf
Empty commit
bryanchriswhite Oct 4, 2024
cd5e420
chore: review feedback improvements
bryanchriswhite Oct 4, 2024
cc95ebf
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 4, 2024
65158ad
Merge branch 'main' into issues/612/gateway/logic
bryanchriswhite Oct 4, 2024
70f8985
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 4, 2024
698991a
Empty commit
bryanchriswhite Oct 4, 2024
6c689e8
Merge branch 'issues/612/gateway/logic' into issues/612/app/msg-updat…
bryanchriswhite Oct 4, 2024
c2bf664
Merge branch 'main' into issues/612/app/msg-update-param
bryanchriswhite Oct 4, 2024
0d63e07
fix: linter error
bryanchriswhite Oct 4, 2024
7753572
Merge branch 'issues/612/app/msg-update-param' into issues/612/applic…
bryanchriswhite Oct 4, 2024
5556f4a
Merge remote-tracking branch 'pokt/main' into issues/612/application/…
bryanchriswhite Oct 4, 2024
1faf4ab
chore: regenerate protobufs
bryanchriswhite Oct 4, 2024
48ba5dd
Merge branch 'issues/612/application/min-stake-param' into issues/612…
bryanchriswhite Oct 4, 2024
d91dde7
Merge branch 'main' into issues/612/application/staking
bryanchriswhite Oct 4, 2024
7e9bd33
Empty commit
bryanchriswhite Oct 7, 2024
69c2755
Empty commit
bryanchriswhite Oct 9, 2024
1f7e201
Merge branch 'main' into issues/612/application/staking
bryanchriswhite Oct 9, 2024
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
76 changes: 57 additions & 19 deletions x/application/keeper/msg_server_stake_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"testing"

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

"github.com/pokt-network/poktroll/app/volatile"
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 @@ -26,9 +27,10 @@ func TestMsgServer_StakeApplication_SuccessfulCreateAndUpdate(t *testing.T) {
require.False(t, isAppFound)

// Prepare the application
stakeMsg := &types.MsgStakeApplication{
initialStake := &apptypes.DefaultMinStake
stakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: initialStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{ServiceId: "svc1"},
},
Expand All @@ -42,14 +44,15 @@ func TestMsgServer_StakeApplication_SuccessfulCreateAndUpdate(t *testing.T) {
foundApp, isAppFound := k.GetApplication(ctx, appAddr)
require.True(t, isAppFound)
require.Equal(t, appAddr, foundApp.Address)
require.Equal(t, int64(100), foundApp.Stake.Amount.Int64())
require.Equal(t, initialStake, foundApp.Stake)
require.Len(t, foundApp.ServiceConfigs, 1)
require.Equal(t, "svc1", foundApp.ServiceConfigs[0].ServiceId)

// Prepare an updated application with a higher stake and another service
updateStakeMsg := &types.MsgStakeApplication{
upStake := initialStake.AddAmount(math.NewInt(100))
updateStakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(200)},
Stake: &upStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{ServiceId: "svc1"},
{ServiceId: "svc2"},
Expand All @@ -61,7 +64,7 @@ func TestMsgServer_StakeApplication_SuccessfulCreateAndUpdate(t *testing.T) {
require.NoError(t, err)
foundApp, isAppFound = k.GetApplication(ctx, appAddr)
require.True(t, isAppFound)
require.Equal(t, int64(200), foundApp.Stake.Amount.Int64())
require.Equal(t, &upStake, foundApp.Stake)
require.Len(t, foundApp.ServiceConfigs, 2)
require.Equal(t, "svc1", foundApp.ServiceConfigs[0].ServiceId)
require.Equal(t, "svc2", foundApp.ServiceConfigs[1].ServiceId)
Expand All @@ -74,9 +77,10 @@ func TestMsgServer_StakeApplication_FailRestakingDueToInvalidServices(t *testing
appAddr := sample.AccAddress()

// Prepare the application stake message
stakeMsg := &types.MsgStakeApplication{
initialStake := &apptypes.DefaultMinStake
stakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: initialStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{ServiceId: "svc1"},
},
Expand All @@ -87,9 +91,10 @@ func TestMsgServer_StakeApplication_FailRestakingDueToInvalidServices(t *testing
require.NoError(t, err)

// Prepare the application stake message without any services
updateStakeMsg := &types.MsgStakeApplication{
upStake := initialStake.AddAmount(math.NewInt(100))
updateStakeMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: &upStake,
Services: []*sharedtypes.ApplicationServiceConfig{},
}

Expand All @@ -105,9 +110,9 @@ func TestMsgServer_StakeApplication_FailRestakingDueToInvalidServices(t *testing
require.Equal(t, "svc1", foundApp.ServiceConfigs[0].ServiceId)

// Prepare the application stake message with an invalid service ID
updateStakeMsg = &types.MsgStakeApplication{
updateStakeMsg = &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(100)},
Stake: &upStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{ServiceId: "svc1 INVALID ! & *"},
},
Expand All @@ -130,10 +135,11 @@ func TestMsgServer_StakeApplication_FailLoweringStake(t *testing.T) {
srv := keeper.NewMsgServerImpl(k)

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

// Prepare an updated application with a lower stake
updateMsg := &types.MsgStakeApplication{
downStake := initialStake.SubAmount(math.NewInt(1000))
updateMsg := &apptypes.MsgStakeApplication{
Address: appAddr,
Stake: &sdk.Coin{Denom: "upokt", Amount: math.NewInt(50)},
Stake: &downStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{ServiceId: "svc1"},
},
Expand All @@ -161,5 +168,36 @@ func TestMsgServer_StakeApplication_FailLoweringStake(t *testing.T) {
// Verify that the application stake is unchanged
foundApp, isAppFound := k.GetApplication(ctx, appAddr)
require.True(t, isAppFound)
require.Equal(t, int64(100), foundApp.Stake.Amount.Int64())
require.Equal(t, initialStake, foundApp.Stake)
}

func TestMsgServer_StakeApplication_FailBelowMinStake(t *testing.T) {
k, ctx := keepertest.ApplicationKeeper(t)
srv := keeper.NewMsgServerImpl(k)

addr := sample.AccAddress()
appStake := cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 100)
minStake := appStake.AddAmount(math.NewInt(1))
expectedErr := apptypes.ErrAppInvalidStake.Wrapf("application %q must stake at least %s", addr, minStake)

// Set the minimum stake to be greater than the application stake.
params := k.GetParams(ctx)
params.MinStake = &minStake
err := k.SetParams(ctx, params)
require.NoError(t, err)

// Prepare the application.
stakeMsg := &apptypes.MsgStakeApplication{
Address: addr,
Stake: &appStake,
Services: []*sharedtypes.ApplicationServiceConfig{
{ServiceId: "svc1"},
},
}

// Attempt to stake the application & verify that the application does NOT exist.
_, err = srv.StakeApplication(ctx, stakeMsg)
require.ErrorContains(t, err, expectedErr.Error())
_, isGatewayFound := k.GetApplication(ctx, addr)
require.False(t, isGatewayFound)
}
Loading
Loading