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

Protorev: Backrun event emission #4878

Merged
merged 14 commits into from
Apr 19, 2023
Merged

Protorev: Backrun event emission #4878

merged 14 commits into from
Apr 19, 2023

Conversation

NotJeremyLiu
Copy link
Contributor

@NotJeremyLiu NotJeremyLiu commented Apr 10, 2023

What is the purpose of the change

  • Adds protorev-specific event emission when the module successfully backruns to make it easier to index for tracking, research, and visualization purposes.

Brief Changelog

  • Emits a protorev_backrun event upon successful backrunning of a transaction. Emits details about 1) user denoms, 2) pool points remaining, 3) profit and denom, 4) amount in and out to execute trade, and 5) tx_hash

Testing and Verifying

(Please pick one of the following options)

  • All tests current tests pass after changing input params necessary for event
  • Added unit test for the added event

Documentation and Release Note

  • Added event section to protorev.md and added the protorev_backrun event type and attrs.

@NotJeremyLiu NotJeremyLiu added the V:state/compatible/backport State machine compatible PR, should be backported label Apr 10, 2023
@NotJeremyLiu NotJeremyLiu changed the title Jl/protorev event emission Protorev: Backrun event emission Apr 10, 2023
@NotJeremyLiu NotJeremyLiu added the protorev All things related to x/protorev label Apr 10, 2023
@github-actions github-actions bot added the C:docs Improvements or additions to documentation label Apr 10, 2023
@NotJeremyLiu NotJeremyLiu marked this pull request as ready for review April 10, 2023 17:07
Copy link
Contributor

@davidterpay davidterpay left a comment

Choose a reason for hiding this comment

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

Update LGTM, but would move the creation and event emission into the same place.

x/protorev/types/events.go Show resolved Hide resolved
x/protorev/keeper/emit.go Outdated Show resolved Hide resolved
x/protorev/keeper/posthandler.go Outdated Show resolved Hide resolved
x/protorev/keeper/emit.go Outdated Show resolved Hide resolved
x/protorev/keeper/emit.go Outdated Show resolved Hide resolved
@ValarDragon ValarDragon added the A:backport/v15.x backport patches to v15.x branch label Apr 11, 2023
@ValarDragon
Copy link
Member

We discussed on call that we are concerned that the keeper call here may change gas. Resolution on call I believe was to:

  • merge this PR
  • on v15 backport be careful to ensure that we delete the getter and usage of that getter in the event

@NotJeremyLiu
Copy link
Contributor Author

We discussed on call that we are concerned that the keeper call here may change gas. Resolution on call I believe was to:

  • merge this PR
  • on v15 backport be careful to ensure that we delete the getter and usage of that getter in the event

I'm going to spend a bit of time today refactoring to not include an extra kvstore read, but to see if we can still output the block pool points. Tbh, this is the main reason why we want this event as it will allow us to know how much room we still have to per block to backrun more or less.

@NotJeremyLiu NotJeremyLiu requested a review from p0mvn as a code owner April 11, 2023 20:35
@NotJeremyLiu
Copy link
Contributor Author

Need to add tests to verify, but this commit intends to allow us to emit blockPoolPointsRemaining without doing an extra kvstore read: 482cbf6

@NotJeremyLiu
Copy link
Contributor Author

Test added, ready for review

Comment on lines 15 to 35
// EmitBackrunEvent updates and emits a backrunEvent
func EmitBackrunEvent(ctx sdk.Context, pool SwapToBackrun, inputCoin sdk.Coin, profit, tokenOutAmount sdk.Int, remainingTxPoolPoints, remainingBlockPoolPoints uint64) error {
// Get tx hash
txHash := strings.ToUpper(hex.EncodeToString(tmhash.Sum(ctx.TxBytes())))
// Update the backrun event and add it to the context
backrunEvent := sdk.NewEvent(
types.TypeEvtBackrun,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeyTxHash, txHash),
sdk.NewAttribute(types.AttributeKeyUserPoolId, strconv.FormatUint(pool.PoolId, 10)),
sdk.NewAttribute(types.AttributeKeyUserDenomIn, pool.TokenInDenom),
sdk.NewAttribute(types.AttributeKeyUserDenomOut, pool.TokenOutDenom),
sdk.NewAttribute(types.AttributeKeyTxPoolPointsRemaining, strconv.FormatUint(remainingTxPoolPoints, 10)),
sdk.NewAttribute(types.AttributeKeyBlockPoolPointsRemaining, strconv.FormatUint(remainingBlockPoolPoints, 10)),
sdk.NewAttribute(types.AttributeKeyProtorevProfit, profit.String()),
sdk.NewAttribute(types.AttributeKeyProtorevAmountIn, inputCoin.Amount.String()),
sdk.NewAttribute(types.AttributeKeyProtorevAmountOut, tokenOutAmount.String()),
sdk.NewAttribute(types.AttributeKeyProtorevArbDenom, inputCoin.Denom),
)
ctx.EventManager().EmitEvent(backrunEvent)

return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

There does not seem to be an error returned, can we remove error return?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved in: f632b42

Comment on lines 227 to 229
if err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

Note that adding error checks can be state-breaking. However, it does not seem that EmitBackrunEvent actually ever returns an error so we can probably remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted, resolved in f632b42 by removing the unused error return

Comment on lines 259 to 263
if numberOfAvailablePoolPoints > maxPoolPointsPerTx {
return maxPoolPointsPerTx, numberOfAvailablePoolPoints, nil
}

return numberOfIterableRoutes, nil
return numberOfAvailablePoolPoints, numberOfAvailablePoolPoints, nil
Copy link
Member

Choose a reason for hiding this comment

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

Could you please update godoc indicating what the return values are and when max vs available are returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in: 03f8e98

@NotJeremyLiu NotJeremyLiu requested a review from p0mvn April 18, 2023 05:27
@NotJeremyLiu
Copy link
Contributor Author

@p0mvn All requested changes have been done, re-review ready

@NotJeremyLiu NotJeremyLiu force-pushed the jl/protorev-event-emission branch from 03f8e98 to 337269e Compare April 18, 2023 14:06
@NotJeremyLiu
Copy link
Contributor Author

Rebased and added change log entry, should be ready to merge

@p0mvn p0mvn requested review from nicolaslara and stackman27 April 19, 2023 17:46
@nicolaslara nicolaslara merged commit 62968cc into main Apr 19, 2023
@nicolaslara nicolaslara deleted the jl/protorev-event-emission branch April 19, 2023 18:13
mergify bot pushed a commit that referenced this pull request Apr 19, 2023
* Add protorev backrun_event emission

* add empty backrun event to pass in as param to make rebalance tests pass

* Add basic validity test for protorev_backrun event

* Add pool id to event, add documentation

* Add tx_hash to backrun event

* change pool point var naming

* Consolidate create and emit backrun event functions into one

* Update logic to not require an additional kvstore read to get data for event

- Instead of reading from store to create the event (previously read to determine remaining block-level pool points remaining), use a previously read variable and passes it through the necessary functions to be able to emit it at the end

* Rename pool points fn to be more general

* Add test to verify proper event emission

* add tx_hash to event documentation

* Remove unused error return

* Update godoc and naming for more clarity

* add changelog entry

(cherry picked from commit 62968cc)
nicolaslara pushed a commit that referenced this pull request Apr 19, 2023
* Add protorev backrun_event emission

* add empty backrun event to pass in as param to make rebalance tests pass

* Add basic validity test for protorev_backrun event

* Add pool id to event, add documentation

* Add tx_hash to backrun event

* change pool point var naming

* Consolidate create and emit backrun event functions into one

* Update logic to not require an additional kvstore read to get data for event

- Instead of reading from store to create the event (previously read to determine remaining block-level pool points remaining), use a previously read variable and passes it through the necessary functions to be able to emit it at the end

* Rename pool points fn to be more general

* Add test to verify proper event emission

* add tx_hash to event documentation

* Remove unused error return

* Update godoc and naming for more clarity

* add changelog entry

(cherry picked from commit 62968cc)

Co-authored-by: Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v15.x backport patches to v15.x branch C:docs Improvements or additions to documentation protorev All things related to x/protorev V:state/compatible/backport State machine compatible PR, should be backported
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants