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

x/superfluid: ensure events are emitted and tests added #2255

Merged
merged 25 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
83 changes: 82 additions & 1 deletion x/superfluid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,88 @@ Disable multiple assets from being used for superfluid staking.

## Events
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the motivation for this, but I can see this going out of date and being difficult to maintain.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might still want to have it since these docs get auto-uploaded to the docs website. Currently, there is no info about events but it could be useful for integrators. WDYT?


-----;
There are 7 types of events that exist in Superfluid module:

* `types.TypeEvtSetSuperfluidAsset` - "set_superfluid_asset"
* `types.TypeEvtRemoveSuperfluidAsset` - "remove_superfluid_asset"
* `types.TypeEvtSuperfluidDelegate` - "superfluid_delegate"
* `types.TypeEvtSuperfluidIncreaseDelegation` - "superfluid_increase_delegation"
* `types.TypeEvtSuperfluidUndelegate` - "superfluid_undelegate"
* `types.TypeEvtSuperfluidUnbondLock` - "superfluid_unbond_lock"
* `types.TypeEvtUnpoolId` - "unpool_pool_id"

### `types.TypeEvtSetSuperfluidAsset`

This event is emitted in the proposal which set new superfluid asset

It consists of the following attributes:

* `types.AttributeDenom`
* The value is the asset denom.
* `types.AttributeSuperfluidAssetType`
* The value is the type of asset.

### `types.TypeEvtRemoveSuperfluidAsset`

This event is emitted in the proposal which removes the superfluid asset

It consists of the following attributes:

* `types.AttributeDenom`
* The value is the asset denom.

### `types.TypeEvtSuperfluidDelegate`

This event is emitted in the message server after successfully creating a delegation for the given lock ID and the validator to delegate to.

It consists of the following attributes:

* `types.AttributeLockId`
* The value is the given lock ID.
* `types.AttributeValidator`
* The value is the validator address to delegate to.

### `types.TypeEvtSuperfluidIncreaseDelegation`

This event is emitted in the hook after adding more token to the existing lock

It consists of the following attributes:

* `types.AttributeLockId`
* The value is the given lock ID.
* `types.AttributeAmount`
* The value is the token amount added to the lock.

### `types.TypeEvtSuperfluidUndelegate`

This event is emitted in the message server after undelegating the currently superfluid delegated position given by lock ID.

It consists of the following attributes:

* `types.AttributeLockId`
* The value is the given lock ID.

### `types.TypeEvtSuperfluidUnbondLock`

This event is emitted in the message server after starting unbonding for the currently superfluid undelegating lock.

It consists of the following attributes:

* `types.AttributeLockId`
* The value is the given lock ID.

### `types.TypeEvtUnpoolId`

This event is emitted in the message server `UnPoolWhitelistedPool`

It consists of the following attributes:

* `types.AttributeKeySender`
* The value is the msg sender address.
* `types.AttributeLockId`
* The value is the pool lpShareDenom.
* `types.AttributeNewLockIds`
* The value is the exited lock ids in byte[].

### Messages

Expand Down
14 changes: 3 additions & 11 deletions x/superfluid/keeper/gov/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/osmosis-labs/osmosis/v10/x/superfluid/keeper"
"github.com/osmosis-labs/osmosis/v10/x/superfluid/keeper/internal/events"
"github.com/osmosis-labs/osmosis/v10/x/superfluid/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,12 +13,7 @@ import (
func HandleSetSuperfluidAssetsProposal(ctx sdk.Context, k keeper.Keeper, ek types.EpochKeeper, p *types.SetSuperfluidAssetsProposal) error {
for _, asset := range p.Assets {
k.AddNewSuperfluidAsset(ctx, asset)
event := sdk.NewEvent(
types.TypeEvtSetSuperfluidAsset,
sdk.NewAttribute(types.AttributeDenom, asset.Denom),
sdk.NewAttribute(types.AttributeSuperfluidAssetType, asset.AssetType.String()),
)
ctx.EventManager().EmitEvent(event)
events.EmitSetSuperfluidAssetEvent(ctx, asset.Denom, asset.AssetType)
}
return nil
}
Expand All @@ -30,11 +26,7 @@ func HandleRemoveSuperfluidAssetsProposal(ctx sdk.Context, k keeper.Keeper, p *t
return fmt.Errorf("superfluid asset %s doesn't exist", denom)
}
k.BeginUnwindSuperfluidAsset(ctx, 0, asset)
event := sdk.NewEvent(
types.TypeEvtRemoveSuperfluidAsset,
sdk.NewAttribute(types.AttributeDenom, denom),
)
ctx.EventManager().EmitEvent(event)
events.EmitRemoveSuperfluidAsset(ctx, denom)
}
return nil
}
10 changes: 6 additions & 4 deletions x/superfluid/keeper/gov/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (suite *KeeperTestSuite) TestHandleSetSuperfluidAssetsProposal() {
assets []types.SuperfluidAsset
expectedAssets []types.SuperfluidAsset
expectErr bool
expectedEvent string
}
testCases := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to / should we break down this actions struct by item? Maybe its not a big deal but just seeing a lump of five items together doesn't make the table driven test super easy to figure out what is getting tested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, naming fields is preferable for readability IMO

name string
Expand All @@ -68,21 +69,21 @@ func (suite *KeeperTestSuite) TestHandleSetSuperfluidAssetsProposal() {
"happy path flow",
[]Action{
{
true, []types.SuperfluidAsset{asset1}, []types.SuperfluidAsset{asset1}, false,
true, []types.SuperfluidAsset{asset1}, []types.SuperfluidAsset{asset1}, false, types.TypeEvtSetSuperfluidAsset,
},
{
false, []types.SuperfluidAsset{asset1}, []types.SuperfluidAsset{}, false,
false, []types.SuperfluidAsset{asset1}, []types.SuperfluidAsset{}, false, types.TypeEvtRemoveSuperfluidAsset,
},
},
},
{
"token does not exist",
[]Action{
{
true, []types.SuperfluidAsset{asset1}, []types.SuperfluidAsset{asset1}, false,
true, []types.SuperfluidAsset{asset1}, []types.SuperfluidAsset{asset1}, false, types.TypeEvtSetSuperfluidAsset,
},
{
false, []types.SuperfluidAsset{asset2}, []types.SuperfluidAsset{asset1}, true,
false, []types.SuperfluidAsset{asset2}, []types.SuperfluidAsset{asset1}, true, types.TypeEvtRemoveSuperfluidAsset,
},
},
},
Expand Down Expand Up @@ -132,6 +133,7 @@ func (suite *KeeperTestSuite) TestHandleSetSuperfluidAssetsProposal() {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
assertEventEmitted(suite, suite.ctx, action.expectedEvent, 1)
}

// check assets individually
Expand Down
12 changes: 12 additions & 0 deletions x/superfluid/keeper/gov/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ func (suite *KeeperTestSuite) SetupTest() {
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func assertEventEmitted(suite *KeeperTestSuite, ctx sdk.Context, eventTypeExpected string, numEventsExpected int) {
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
allEvents := ctx.EventManager().Events()
// filter out other events
actualEvents := make([]sdk.Event, 0, 1)
for _, event := range allEvents {
if event.Type == eventTypeExpected {
actualEvents = append(actualEvents, event)
}
}
suite.Require().Equal(numEventsExpected, len(actualEvents))
}
9 changes: 2 additions & 7 deletions x/superfluid/keeper/hooks.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package keeper

import (
"fmt"
"time"

epochstypes "github.com/osmosis-labs/osmosis/v10/x/epochs/types"
"github.com/osmosis-labs/osmosis/v10/x/superfluid/types"
"github.com/osmosis-labs/osmosis/v10/x/superfluid/keeper/internal/events"

sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -43,11 +42,7 @@ func (h Hooks) AfterAddTokensToLock(ctx sdk.Context, address sdk.AccAddress, loc
if err != nil {
h.k.Logger(ctx).Error(err.Error())
} else {
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.TypeEvtSuperfluidIncreaseDelegation,
sdk.NewAttribute(types.AttributeLockId, fmt.Sprintf("%d", lockID)),
sdk.NewAttribute(types.AttributeAmount, amount.String()),
))
events.EmitSuperfluidIncreaseDelegationEvent(ctx, lockID, amount)
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions x/superfluid/keeper/hooks_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package keeper_test

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

lockupkeeper "github.com/osmosis-labs/osmosis/v10/x/lockup/keeper"
lockuptypes "github.com/osmosis-labs/osmosis/v10/x/lockup/types"
"github.com/osmosis-labs/osmosis/v10/x/superfluid/types"
)

func (suite *KeeperTestSuite) TestSuperfluidAfterEpochEnd() {
Expand Down Expand Up @@ -303,3 +309,31 @@ func (suite *KeeperTestSuite) TestBeforeSlashingUnbondingDelegationHook() {
})
}
}

// TestAfterAddTokensToLock_Event tests that events are correctly emitted
// when calling AfterAddTokensToLock.
func (suite *KeeperTestSuite) TestAfterAddTokensToLock_Event() {
suite.SetupTest()

valAddrs := suite.SetupValidators([]stakingtypes.BondStatus{stakingtypes.Bonded})

denoms, _ := suite.SetupGammPoolsAndSuperfluidAssets([]sdk.Dec{sdk.NewDec(20)})

// setup superfluid delegations
_, intermediaryAccs, locks := suite.setupSuperfluidDelegations(valAddrs, []superfluidDelegation{{0, 0, 0, 1000000}}, denoms)
suite.checkIntermediaryAccountDelegations(intermediaryAccs)

for index, lock := range locks {
lockupMsgServer := lockupkeeper.NewMsgServerImpl(suite.App.LockupKeeper)
c := sdk.WrapSDKContext(suite.Ctx)
coinsToLock := sdk.NewCoins(sdk.NewCoin(denoms[index], sdk.NewInt(100)))
sender, _ := sdk.AccAddressFromBech32(lock.Owner)
suite.FundAcc(sender, coinsToLock)

_, err := lockupMsgServer.LockTokens(c, lockuptypes.NewMsgLockTokens(sender, time.Hour*504, coinsToLock))
suite.Require().NoError(err)

// should call AfterAddTokensToLock hook and emit event here
assertEventEmitted(suite, suite.Ctx, types.TypeEvtSuperfluidIncreaseDelegation, 1)
}
}
133 changes: 133 additions & 0 deletions x/superfluid/keeper/internal/events/emit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package events

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/v10/x/superfluid/types"
)

func EmitSetSuperfluidAssetEvent(ctx sdk.Context, denom string, assetType types.SuperfluidAssetType) {
if ctx.EventManager() == nil {
return
}

ctx.EventManager().EmitEvents(sdk.Events{
newSetSuperfluidAssetEvent(denom, assetType),
})
}

func newSetSuperfluidAssetEvent(denom string, assetType types.SuperfluidAssetType) sdk.Event {
return sdk.NewEvent(
types.TypeEvtSetSuperfluidAsset,
sdk.NewAttribute(types.AttributeDenom, denom),
sdk.NewAttribute(types.AttributeSuperfluidAssetType, assetType.String()),
)
Copy link
Contributor

@stackman27 stackman27 Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are going over all the module events, what are the thoughts on using EmitTypedEvent instead of EmitEvents

It'll definitely reduce the code size and gives a better code clarity. For instance the above code could be rewritten with an event.proto file as;

ctx.EventManager().EmitTypedEvent(&superfluid.AssetEvent{
		denom:  denom,
	         assetType: assetType.String(), 
	})

ps: EmitEvents is labelled deprecated too

cc @alexanderbez @ValarDragon @p0mvn

Copy link
Member

@p0mvn p0mvn Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that's a great idea. However, I'm guessing we would make the EmitEvent function reusable across modules instead of defining this on the event manager. What do you think?

Also, I think we would have to make the event constructors exported.

For example, newSetSuperfluidAssetEvent -> NewSetSuperfluidAssetEvent

To have:

EmitEvent(NewSuperfluidAssetEvent(...))

Just to avoid any confusion - we should pursue this separately (not in this PR)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we reusing events? I would only use constructors and separate out the event logic if we use the same ones in multiple places, otherwise it would be overkill.

Copy link
Member

@ValarDragon ValarDragon Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are in 0 rush to switch to typed events.

I also agree that constructors seem overkill unless reuse

}

func EmitRemoveSuperfluidAsset(ctx sdk.Context, denom string) {
if ctx.EventManager() == nil {
return
}

ctx.EventManager().EmitEvents(sdk.Events{
newRemoveSuperfluidAssetEvent(denom),
})
}

func newRemoveSuperfluidAssetEvent(denom string) sdk.Event {
return sdk.NewEvent(
types.TypeEvtRemoveSuperfluidAsset,
sdk.NewAttribute(types.AttributeDenom, denom),
)
}

func EmitSuperfluidDelegateEvent(ctx sdk.Context, lockId uint64, valAddress string) {
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
if ctx.EventManager() == nil {
return
}

ctx.EventManager().EmitEvents(sdk.Events{
newSuperfluidDelegateEvent(lockId, valAddress),
})
}

func newSuperfluidDelegateEvent(lockId uint64, valAddress string) sdk.Event {
return sdk.NewEvent(
types.TypeEvtSuperfluidDelegate,
sdk.NewAttribute(types.AttributeLockId, fmt.Sprintf("%d", lockId)),
hieuvubk marked this conversation as resolved.
Show resolved Hide resolved
sdk.NewAttribute(types.AttributeValidator, valAddress),
)
}

func EmitSuperfluidIncreaseDelegationEvent(ctx sdk.Context, lockId uint64, amount sdk.Coins) {
if ctx.EventManager() == nil {
return
}

ctx.EventManager().EmitEvents(sdk.Events{
newSuperfluidIncreaseDelegationEvent(lockId, amount),
})
}

func newSuperfluidIncreaseDelegationEvent(lockId uint64, amount sdk.Coins) sdk.Event {
return sdk.NewEvent(
types.TypeEvtSuperfluidIncreaseDelegation,
sdk.NewAttribute(types.AttributeLockId, fmt.Sprintf("%d", lockId)),
sdk.NewAttribute(types.AttributeAmount, amount.String()),
)
}

func EmitSuperfluidUndelegateEvent(ctx sdk.Context, lockId uint64) {
if ctx.EventManager() == nil {
return
}

ctx.EventManager().EmitEvents(sdk.Events{
newSuperfluidUndelegateEvent(lockId),
})
}

func newSuperfluidUndelegateEvent(lockId uint64) sdk.Event {
return sdk.NewEvent(
types.TypeEvtSuperfluidUndelegate,
sdk.NewAttribute(types.AttributeLockId, fmt.Sprintf("%d", lockId)),
)
}

func EmitSuperfluidUnbondLockEvent(ctx sdk.Context, lockId uint64) {
if ctx.EventManager() == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventManager should never be nil, but I guess it can't hurt. Just so many LOC :-/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh thank you. If so, I'll remove them.

return
}

ctx.EventManager().EmitEvents(sdk.Events{
newSuperfluidUnbondLockEvent(lockId),
})
}

func newSuperfluidUnbondLockEvent(lockId uint64) sdk.Event {
return sdk.NewEvent(
types.TypeEvtSuperfluidUnbondLock,
sdk.NewAttribute(types.AttributeLockId, fmt.Sprintf("%d", lockId)),
)
}

func EmitUnpoolIdEvent(ctx sdk.Context, sender string, lpShareDenom string, allExitedLockIDsSerialized []byte) {
if ctx.EventManager() == nil {
return
}

ctx.EventManager().EmitEvents(sdk.Events{
newUnpoolIdEvent(sender, lpShareDenom, allExitedLockIDsSerialized),
})
}

func newUnpoolIdEvent(sender string, lpShareDenom string, allExitedLockIDsSerialized []byte) sdk.Event {
return sdk.NewEvent(
types.TypeEvtUnpoolId,
sdk.NewAttribute(sdk.AttributeKeySender, sender),
sdk.NewAttribute(types.AttributeDenom, lpShareDenom),
sdk.NewAttribute(types.AttributeNewLockIds, string(allExitedLockIDsSerialized)),
)
}
Loading