Skip to content

Commit

Permalink
[Code Health] Ensure application module msg responses are non-empty (#…
Browse files Browse the repository at this point in the history
…974)

## Summary

Ensure all application module msg responses are non-empty. This adds a
`Application` field to the following protobuf type(s), and updates the
unit tests to assert for presence and correctness.

- `MsgUnstakeApplicationResponse`
- `MsgUndelegateFromGatewayResponse`

## Issue

- #663

## Type of change

Select one or more from the following:

- [ ] New feature, functionality or library
- [ ] Consensus breaking; add the `consensus-breaking` label if so. See
#791 for details
- [ ] Bug fix
- [x] Code health or cleanup
- [ ] Documentation
- [ ] Other (specify)

## Testing

- [ ] **Documentation**: `make docusaurus_start`; only needed if you
make doc changes
- [ ] **Unit Tests**: `make go_develop_and_test`
- [ ] **LocalNet E2E Tests**: `make test_e2e`
- [ ] **DevNet E2E Tests**: Add the `devnet-test-e2e` label to the PR.

## Sanity Checklist

- [x] I have tested my changes using the available tooling
- [ ] I have commented my code
- [x] I have performed a self-review of my own code; both comments &
source code
- [ ] I create and reference any new tickets, if applicable
- [ ] I have left TODOs throughout the codebase, if applicable
  • Loading branch information
bryanchriswhite authored Dec 9, 2024
1 parent 0376986 commit 23b4c88
Show file tree
Hide file tree
Showing 9 changed files with 576 additions and 243 deletions.
497 changes: 342 additions & 155 deletions api/poktroll/application/tx.pulsar.go

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions proto/poktroll/application/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ message MsgUnstakeApplication {
string address = 1;
}

message MsgUnstakeApplicationResponse {}
message MsgUnstakeApplicationResponse {
poktroll.application.Application application = 1;
}

message MsgDelegateToGateway {
option (cosmos.msg.v1.signer) = "app_address"; // https://docs.cosmos.network/main/build/building-modules/messages-and-queries
Expand All @@ -87,7 +89,9 @@ message MsgUndelegateFromGateway {
string gateway_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the gateway the application wants to undelegate from.
}

message MsgUndelegateFromGatewayResponse {}
message MsgUndelegateFromGatewayResponse {
poktroll.application.Application application = 1;
}

message MsgTransferApplication {
option (cosmos.msg.v1.signer) = "source_address";
Expand Down
28 changes: 14 additions & 14 deletions x/application/keeper/msg_server_delegate_to_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ func TestMsgServer_DelegateToGateway_SuccessfullyDelegate(t *testing.T) {
},
}

expectedApp := &apptypes.Application{
Address: stakeMsg.GetAddress(),
Stake: stakeMsg.GetStake(),
ServiceConfigs: stakeMsg.GetServices(),
DelegateeGatewayAddresses: make([]string, 0),
PendingUndelegations: make(map[uint64]apptypes.UndelegatingGatewayList),
}

// Stake the application & verify that the application exists
_, err := srv.StakeApplication(ctx, stakeMsg)
require.NoError(t, err)
_, isAppFound := k.GetApplication(ctx, appAddr)
foundApp, isAppFound := k.GetApplication(ctx, appAddr)
require.Equal(t, expectedApp, &foundApp)
require.True(t, isAppFound)

// Prepare the delegation message
Expand All @@ -49,21 +58,17 @@ func TestMsgServer_DelegateToGateway_SuccessfullyDelegate(t *testing.T) {
GatewayAddress: gatewayAddr1,
}

expectedApp.DelegateeGatewayAddresses = append(expectedApp.DelegateeGatewayAddresses, gatewayAddr1)

// Delegate the application to the gateway
_, err = srv.DelegateToGateway(ctx, delegateMsg)
delegateRes, err := srv.DelegateToGateway(ctx, delegateMsg)
require.NoError(t, err)
require.Equal(t, delegateRes.GetApplication(), expectedApp)

sdkCtx := sdk.UnwrapSDKContext(ctx)
currentHeight := sdkCtx.BlockHeight()
sharedParams := sharedtypes.DefaultParams()
sessionEndHeight := sharedtypes.GetSessionEndHeight(&sharedParams, currentHeight)
expectedApp := &apptypes.Application{
Address: stakeMsg.GetAddress(),
Stake: stakeMsg.GetStake(),
ServiceConfigs: stakeMsg.GetServices(),
DelegateeGatewayAddresses: []string{gatewayAddr1},
PendingUndelegations: make(map[uint64]apptypes.UndelegatingGatewayList),
}
expectedEvent := &apptypes.EventRedelegation{
Application: expectedApp,
SessionEndHeight: sessionEndHeight,
Expand All @@ -77,11 +82,6 @@ func TestMsgServer_DelegateToGateway_SuccessfullyDelegate(t *testing.T) {
// Reset the events, as if a new block were created.
ctx, sdkCtx = testevents.ResetEventManager(ctx)

// Verify that the application exists
foundApp, isAppFound := k.GetApplication(ctx, appAddr)
require.True(t, isAppFound)
require.EqualValues(t, expectedApp, &foundApp)

// Prepare a second delegation message
delegateMsg2 := &apptypes.MsgDelegateToGateway{
AppAddress: appAddr,
Expand Down
27 changes: 21 additions & 6 deletions x/application/keeper/msg_server_stake_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ func TestMsgServer_StakeApplication_SuccessfulCreateAndUpdate(t *testing.T) {
require.NoError(t, err)

// Assert that the response contains the staked application.
app := stakeAppRes.GetApplication()
require.Equal(t, stakeMsg.GetAddress(), app.GetAddress())
require.Equal(t, stakeMsg.GetStake(), app.GetStake())
require.Equal(t, stakeMsg.GetServices(), app.GetServiceConfigs())
expectedApp := &apptypes.Application{
Address: stakeMsg.GetAddress(),
Stake: stakeMsg.GetStake(),
ServiceConfigs: stakeMsg.GetServices(),
PendingUndelegations: make(map[uint64]apptypes.UndelegatingGatewayList),
DelegateeGatewayAddresses: make([]string, 0),
}
require.Equal(t, expectedApp, stakeAppRes.GetApplication())

// Assert that the EventApplicationStaked event is emitted.
sharedParams := sharedtypes.DefaultParams()
currentHeight := cosmostypes.UnwrapSDKContext(ctx).BlockHeight()
sessionEndHeight := sharedtypes.GetSessionEndHeight(&sharedParams, currentHeight)
expectedEvent, err := cosmostypes.TypedEventToEvent(
&apptypes.EventApplicationStaked{
Application: app,
Application: stakeAppRes.GetApplication(),
SessionEndHeight: sessionEndHeight,
},
)
Expand Down Expand Up @@ -85,8 +89,19 @@ func TestMsgServer_StakeApplication_SuccessfulCreateAndUpdate(t *testing.T) {
}

// Update the staked application
_, err = srv.StakeApplication(ctx, updateStakeMsg)
stakeAppRes, err = srv.StakeApplication(ctx, updateStakeMsg)
require.NoError(t, err)

// Assert that the response contains the staked application.
expectedApp = &apptypes.Application{
Address: updateStakeMsg.GetAddress(),
Stake: updateStakeMsg.GetStake(),
ServiceConfigs: updateStakeMsg.GetServices(),
PendingUndelegations: make(map[uint64]apptypes.UndelegatingGatewayList),
DelegateeGatewayAddresses: make([]string, 0),
}
require.Equal(t, expectedApp, stakeAppRes.GetApplication())

foundApp, isAppFound = k.GetApplication(ctx, appAddr)
require.True(t, isAppFound)
require.Equal(t, &upStake, foundApp.Stake)
Expand Down
4 changes: 3 additions & 1 deletion x/application/keeper/msg_server_undelegate_from_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func (k msgServer) UndelegateFromGateway(ctx context.Context, msg *apptypes.MsgU
logger.Info(fmt.Sprintf("Emitted application redelegation event %v", event))

isSuccessful = true
return &apptypes.MsgUndelegateFromGatewayResponse{}, nil
return &apptypes.MsgUndelegateFromGatewayResponse{
Application: &foundApp,
}, nil
}

// recordPendingUndelegation adds the given gateway address to the application's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ func TestMsgServer_UndelegateFromGateway_SuccessfullyUndelegate(t *testing.T) {
GatewayAddress: expectedGatewayAddresses[3],
}

// Undelegate the application from the gateway
_, err = srv.UndelegateFromGateway(ctx, undelegateMsg)
require.NoError(t, err)

// Assert that the EventRedelegation event is emitted.
expectedGatewayAddresses = append(expectedGatewayAddresses[:3], expectedGatewayAddresses[4:]...)
expectedApp := &apptypes.Application{
Expand All @@ -118,6 +114,11 @@ func TestMsgServer_UndelegateFromGateway_SuccessfullyUndelegate(t *testing.T) {
},
}

// Undelegate the application from the gateway
undelegateRes, err := srv.UndelegateFromGateway(ctx, undelegateMsg)
require.NoError(t, err)
require.Equal(t, undelegateRes.GetApplication(), expectedApp)

events = sdkCtx.EventManager().Events()
redelgationEvents = testevents.FilterEvents[*apptypes.EventRedelegation](t, events)
lastEventIdx := len(redelgationEvents) - 1
Expand Down
4 changes: 3 additions & 1 deletion x/application/keeper/msg_server_unstake_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,7 @@ func (k msgServer) UnstakeApplication(
}

isSuccessful = true
return &apptypes.MsgUnstakeApplicationResponse{}, nil
return &apptypes.MsgUnstakeApplicationResponse{
Application: &foundApp,
}, nil
}
6 changes: 4 additions & 2 deletions x/application/keeper/msg_server_unstake_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ func TestMsgServer_UnstakeApplication_CancelUnbondingIfRestaked(t *testing.T) {

// Initiate the application unstaking
unstakeMsg := &apptypes.MsgUnstakeApplication{Address: appAddr}
_, err = srv.UnstakeApplication(ctx, unstakeMsg)
unstakeRes, err := srv.UnstakeApplication(ctx, unstakeMsg)
require.NoError(t, err)

// Assert that the EventApplicationUnbondingBegin event is emitted.
foundApp.UnstakeSessionEndHeight = uint64(sessionEndHeight)
foundApp.DelegateeGatewayAddresses = make([]string, 0)
require.Equal(t, &foundApp, unstakeRes.GetApplication())

// Assert that the EventApplicationUnbondingBegin event is emitted.
unbondingEndHeight := apptypes.GetApplicationUnbondingHeight(&sharedParams, &foundApp)
expectedAppUnbondingBeginEvent := &apptypes.EventApplicationUnbondingBegin{
Application: &foundApp,
Expand Down
Loading

0 comments on commit 23b4c88

Please sign in to comment.