Skip to content

Commit

Permalink
chore(perf): protorev tracker coin array (#7240)
Browse files Browse the repository at this point in the history
* remove tx fee tracker

* add change log

* track array of coins for performance

* changelog

* lints

* lints

* fixes

* Auto: update go.mod after push to adam/protorev-tracker-perf that modified dependencies locally

* fix tests

* event

* nil error

* expect accounting height to always be exported

* range backwards and add test

* reduce code duplication

* Auto: update go.mod after push to adam/protorev-tracker-perf that modified dependencies locally

* init image should not check seq number

* revert change to proto rev bc might expect error case

* revert init tag for now

* config tag

* lint spelling

* init tag

* revert init image

* remove osmoutils method for coins to coin array

* continue tracking from same accounting height pre upgrade

* Auto: update go.mod after push to adam/protorev-tracker-perf that modified dependencies locally

* update store helper

* Auto: update go.mod after push to adam/protorev-tracker-perf that modified dependencies locally

* go mod updates

* just use int

* go mod updates

* revert statistics.go

* fix store key

* update go mods

* dont double append prefix

* update go mod

* go mod changes

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
czarcas7ic and github-actions authored Jan 9, 2024
1 parent 370b381 commit 227c44c
Show file tree
Hide file tree
Showing 33 changed files with 658 additions and 428 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#6991](https://github.com/osmosis-labs/osmosis/pull/6991) Fix: total liquidity poolmanager grpc gateway query
* [#7149](https://github.com/osmosis-labs/osmosis/pull/7149) Fix double emitting CacheCtx events (e.g. Epoch, Superfluid, CL)
* [#7237](https://github.com/osmosis-labs/osmosis/pull/7237) Removes tx_fee_tracker from the proto rev tracker, no longer tracks in state.
* [#7240](https://github.com/osmosis-labs/osmosis/pull/7240) Protorev tracker now tracks a coin array to improve gas efficiency.

### Features

Expand Down
6 changes: 3 additions & 3 deletions app/upgrades/v21/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func (s *UpgradeTestSuite) TestUpgrade() {
s.Require().Equal(v21UpgradeHeight, allProtocolRevenue.TakerFeesTracker.HeightAccountingStartsFrom)
// s.Require().Equal(v21UpgradeHeight, allProtocolRevenue.TxFeesTracker.HeightAccountingStartsFrom)
// All values should be nill except for the cyclic arb profits, which should start at the value it was at time of upgrade
s.Require().Equal(sdk.Coins(nil), allProtocolRevenue.TakerFeesTracker.TakerFeesToCommunityPool)
s.Require().Equal(sdk.Coins(nil), allProtocolRevenue.TakerFeesTracker.TakerFeesToStakers)
s.Require().Equal([]sdk.Coin{}, allProtocolRevenue.TakerFeesTracker.TakerFeesToCommunityPool)
s.Require().Equal([]sdk.Coin{}, allProtocolRevenue.TakerFeesTracker.TakerFeesToStakers)
// s.Require().Equal(sdk.Coins(nil), allProtocolRevenue.TxFeesTracker.TxFees)
s.Require().Equal(cyclicArbProfits, allProtocolRevenue.CyclicArbTracker.CyclicArb)
s.Require().Equal([]sdk.Coin(cyclicArbProfits), allProtocolRevenue.CyclicArbTracker.CyclicArb)

}

Expand Down
17 changes: 17 additions & 0 deletions app/upgrades/v22/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ func CreateUpgradeHandler(
return nil, err
}

// Migrate legacy taker fee tracker to new taker fee tracker (for performance reasons)
oldTakerFeeTrackerForStakers := keepers.PoolManagerKeeper.GetLegacyTakerFeeTrackerForStakers(ctx)
for _, coin := range oldTakerFeeTrackerForStakers {
err := keepers.PoolManagerKeeper.UpdateTakerFeeTrackerForStakersByDenom(ctx, coin.Denom, coin.Amount)
if err != nil {
return nil, err
}
}

oldTakerFeeTrackerForCommunityPool := keepers.PoolManagerKeeper.GetLegacyTakerFeeTrackerForCommunityPool(ctx)
for _, coin := range oldTakerFeeTrackerForCommunityPool {
err := keepers.PoolManagerKeeper.UpdateTakerFeeTrackerForCommunityPoolByDenom(ctx, coin.Denom, coin.Amount)
if err != nil {
return nil, err
}
}

// Properly register consensus params. In the process, change params as per:
// https://www.mintscan.io/osmosis/proposals/705
defaultConsensusParams := tmtypes.DefaultConsensusParams().ToProto()
Expand Down
84 changes: 84 additions & 0 deletions app/upgrades/v22/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package v22_test

import (
"testing"

"github.com/stretchr/testify/suite"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/osmoutils"
"github.com/osmosis-labs/osmosis/v21/app/apptesting"
"github.com/osmosis-labs/osmosis/v21/x/protorev/types"

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

poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
)

const (
v22UpgradeHeight = int64(10)
)

type UpgradeTestSuite struct {
apptesting.KeeperTestHelper
}

func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

func (s *UpgradeTestSuite) TestUpgrade() {
s.Setup()

expectedTakerFeeForStakers := []sdk.Coin{sdk.NewCoin("uakt", osmomath.NewInt(3000)), sdk.NewCoin("uatom", osmomath.NewInt(1000)), sdk.NewCoin("uosmo", osmomath.NewInt(2000))}
expectedTakerFeeForCommunityPool := []sdk.Coin{sdk.NewCoin("uakt", osmomath.NewInt(2000)), sdk.NewCoin("uatom", osmomath.NewInt(3000)), sdk.NewCoin("uosmo", osmomath.NewInt(1000))}
expectedTrackerStartHeight := int64(3)

// Set up old protorev tracker prior to upgrade
s.App.PoolManagerKeeper.SetTakerFeeTrackerStartHeight(s.Ctx, expectedTrackerStartHeight)
newTakerFeeForStakers := poolmanagertypes.TrackedVolume{
Amount: expectedTakerFeeForStakers,
}
osmoutils.MustSet(s.Ctx.KVStore(s.App.GetKey(poolmanagertypes.StoreKey)), poolmanagertypes.KeyTakerFeeStakersProtoRev, &newTakerFeeForStakers)

newTakerFeeForCommunityPool := poolmanagertypes.TrackedVolume{
Amount: expectedTakerFeeForCommunityPool,
}
osmoutils.MustSet(s.Ctx.KVStore(s.App.GetKey(poolmanagertypes.StoreKey)), poolmanagertypes.KeyTakerFeeCommunityPoolProtoRev, &newTakerFeeForCommunityPool)

// Set up cyclic arb tracker just to double check that it is not affected by the upgrade
s.App.ProtoRevKeeper.SetCyclicArbProfitTrackerStartHeight(s.Ctx, expectedTrackerStartHeight)
cyclicArbProfits := sdk.NewCoins(sdk.NewCoin(types.OsmosisDenomination, osmomath.NewInt(9000)), sdk.NewCoin("Atom", osmomath.NewInt(3000)))
err := s.App.AppKeepers.ProtoRevKeeper.UpdateStatistics(s.Ctx, poolmanagertypes.SwapAmountInRoutes{}, cyclicArbProfits[0].Denom, cyclicArbProfits[0].Amount)
s.Require().NoError(err)
err = s.App.AppKeepers.ProtoRevKeeper.UpdateStatistics(s.Ctx, poolmanagertypes.SwapAmountInRoutes{}, cyclicArbProfits[1].Denom, cyclicArbProfits[1].Amount)
s.Require().NoError(err)

dummyUpgrade(s)
s.Require().NotPanics(func() {
s.App.BeginBlocker(s.Ctx, abci.RequestBeginBlock{})
})

allProtocolRevenue := s.App.ProtoRevKeeper.GetAllProtocolRevenue(s.Ctx)

// Check that the taker fee tracker for stakers has been migrated correctly
s.Require().Equal(expectedTakerFeeForStakers, allProtocolRevenue.TakerFeesTracker.TakerFeesToStakers)
s.Require().Equal(expectedTakerFeeForCommunityPool, allProtocolRevenue.TakerFeesTracker.TakerFeesToCommunityPool)
s.Require().Equal(expectedTrackerStartHeight, allProtocolRevenue.TakerFeesTracker.HeightAccountingStartsFrom)
s.Require().Equal(expectedTrackerStartHeight, allProtocolRevenue.CyclicArbTracker.HeightAccountingStartsFrom)
}

func dummyUpgrade(s *UpgradeTestSuite) {
s.Ctx = s.Ctx.WithBlockHeight(v22UpgradeHeight - 1)
plan := upgradetypes.Plan{Name: "v22", Height: v22UpgradeHeight}
err := s.App.UpgradeKeeper.ScheduleUpgrade(s.Ctx, plan)
s.Require().NoError(err)
_, exists := s.App.UpgradeKeeper.GetUpgradePlan(s.Ctx)
s.Require().True(exists)

s.Ctx = s.Ctx.WithBlockHeight(v22UpgradeHeight)
}
61 changes: 33 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ require (
github.com/cosmos/ibc-go/v7 v7.3.1
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/golangci/golangci-lint v1.54.2
github.com/golangci/golangci-lint v1.55.2
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/iancoleman/orderedmap v0.3.0
github.com/mattn/go-sqlite3 v1.14.17
github.com/ory/dockertest/v3 v3.10.0
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109065741-1aea8952d330
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109034818-d2a4cb704d18
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109211933-17cb7c095403
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109211933-17cb7c095403
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109211933-17cb7c095403
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109211933-17cb7c095403
github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d
github.com/osmosis-labs/sqs/sqsdomain v0.0.0-20240108192026-6ccc0a29f77d
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -63,19 +63,22 @@ require (
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/log v1.2.1 // indirect
github.com/4meepo/tagalign v1.3.2 // indirect
github.com/Abirdcfly/dupword v0.0.12 // indirect
github.com/4meepo/tagalign v1.3.3 // indirect
github.com/Abirdcfly/dupword v0.0.13 // indirect
github.com/Antonboom/testifylint v0.2.3 // indirect
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect
github.com/Djarvur/go-err113 v0.1.0 // indirect
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.1.0 // indirect
github.com/OpenPeeDeeP/depguard/v2 v2.1.0 // indirect
github.com/alecthomas/go-check-sumtype v0.1.3 // indirect
github.com/alexkohler/nakedret/v2 v2.0.2 // indirect
github.com/alingse/asasalint v0.0.11 // indirect
github.com/aws/aws-sdk-go v1.44.224 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/butuzov/mirror v1.1.0 // indirect
github.com/catenacyber/perfsprint v0.2.0 // indirect
github.com/ccojocar/zxcvbn-go v1.0.1 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
Expand All @@ -96,6 +99,7 @@ require (
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/getsentry/sentry-go v0.23.0 // indirect
github.com/ghostiam/protogetter v0.2.3 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/google/btree v1.1.2 // indirect
Expand All @@ -116,15 +120,15 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/linxGnu/grocksdb v1.7.16 // indirect
github.com/macabu/inamedparam v0.1.2 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/maratori/testableexamples v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nunnatsa/ginkgolinter v0.13.5 // indirect
github.com/otiai10/copy v1.11.0 // indirect
github.com/nunnatsa/ginkgolinter v0.14.1 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -137,10 +141,11 @@ require (
github.com/tidwall/pretty v1.2.0 // indirect
github.com/timonwong/loggercheck v0.9.4 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xen0n/gosmopolitan v1.2.1 // indirect
github.com/xen0n/gosmopolitan v1.2.2 // indirect
github.com/ykadowak/zerologlint v0.1.3 // indirect
github.com/zimmski/go-mutesting v0.0.0-20210610104036-6d9217011a00 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go-simpler.org/sloglint v0.1.2 // indirect
go.tmz.dev/musttag v0.7.2 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
Expand Down Expand Up @@ -176,19 +181,19 @@ require (
github.com/bkielbasa/cyclop v1.2.1 // indirect
github.com/blizzy78/varnamelen v0.8.0 // indirect
github.com/bombsimon/wsl/v3 v3.4.0 // indirect
github.com/breml/bidichk v0.2.4 // indirect
github.com/breml/errchkjson v0.3.1 // indirect
github.com/butuzov/ireturn v0.2.0 // indirect
github.com/breml/bidichk v0.2.7 // indirect
github.com/breml/errchkjson v0.3.6 // indirect
github.com/butuzov/ireturn v0.2.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charithe/durationcheck v0.0.10 // indirect
github.com/chavacava/garif v0.0.0-20230227094218-b8c73b2037b8 // indirect
github.com/chavacava/garif v0.1.0 // indirect
github.com/confio/ics23/go v0.9.1 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/cosmos/btcutil v1.0.5
github.com/cosmos/ledger-cosmos-go v0.12.4 // indirect
github.com/daixiang0/gci v0.11.0 // indirect
github.com/daixiang0/gci v0.11.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/denis-tingaikin/go-header v0.4.3 // indirect
Expand Down Expand Up @@ -229,11 +234,11 @@ require (
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe // indirect
github.com/golangci/gofmt v0.0.0-20220901101216-f2edd75033f2 // indirect
github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e // indirect
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0 // indirect
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca // indirect
github.com/golangci/misspell v0.4.1 // indirect
github.com/golangci/revgrep v0.0.0-20220804021717-745bb2f7c2e6 // indirect
github.com/golangci/revgrep v0.5.2 // indirect
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -260,7 +265,7 @@ require (
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jessevdk/go-flags v1.5.0 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
github.com/jgautheron/goconst v1.6.0 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
Expand All @@ -285,7 +290,7 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mbilski/exhaustivestruct v1.2.0 // indirect
github.com/mgechev/revive v1.3.2 // indirect
github.com/mgechev/revive v1.3.4 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -302,7 +307,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/polyfloyd/go-errorlint v1.4.4 // indirect
github.com/polyfloyd/go-errorlint v1.4.5 // indirect
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
Expand All @@ -316,10 +321,10 @@ require (
github.com/rs/cors v1.9.0 // indirect
github.com/rs/zerolog v1.30.0 // indirect
github.com/ryancurrah/gomodguard v1.3.0 // indirect
github.com/ryanrolds/sqlclosecheck v0.4.0 // indirect
github.com/ryanrolds/sqlclosecheck v0.5.1 // indirect
github.com/sanposhiho/wastedassign/v2 v2.0.7 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/securego/gosec/v2 v2.17.0 // indirect
github.com/securego/gosec/v2 v2.18.2 // indirect
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sivchari/containedctx v1.0.3 // indirect
Expand All @@ -334,13 +339,13 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
github.com/tdakkota/asciicheck v0.2.0 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tetafro/godot v1.4.14 // indirect
github.com/tetafro/godot v1.4.15 // indirect
github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966 // indirect
github.com/tomarrell/wrapcheck/v2 v2.8.1 // indirect
github.com/tommy-muehle/go-mnd/v2 v2.5.1 // indirect
github.com/ultraware/funlen v0.1.0 // indirect
github.com/ultraware/whitespace v0.0.5 // indirect
github.com/uudashr/gocognit v1.0.7 // indirect
github.com/uudashr/gocognit v1.1.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand All @@ -349,20 +354,20 @@ require (
github.com/zimmski/go-tool v0.0.0-20150119110811-2dfdc9ac8439 // indirect
github.com/zimmski/osutil v0.0.0-20190128123334-0d0b3ca231ac // indirect
github.com/zondax/hid v0.9.2 // indirect
gitlab.com/bosi/decorder v0.4.0 // indirect
gitlab.com/bosi/decorder v0.4.1 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20230307190834-24139beb5833 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
honnef.co/go/tools v0.4.5 // indirect
honnef.co/go/tools v0.4.6 // indirect
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
mvdan.cc/unparam v0.0.0-20230312165513-e84e2d14e3b8 // indirect
Expand Down
Loading

0 comments on commit 227c44c

Please sign in to comment.