-
Notifications
You must be signed in to change notification settings - Fork 620
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
chore(perf): protorev tracker coin array #7240
Changes from 1 commit
2e919c6
3785022
25b911d
3f217b9
ccf65fb
2d88b49
91d647e
867a1d0
b98972c
bc9d9b0
12c0f74
ef5176b
2e78405
69c2df1
e3c0e33
ee425f0
759831d
7a09aef
11013db
286e914
68bf4ec
a16bea6
dfca598
6d6cd16
ba0c68e
166f039
f19b819
0a054b0
a59dd33
a27e3c8
673a360
ba52471
fd1a39d
a582fa1
46bd310
c3f306d
c665113
709b70d
b83740e
ac2f006
17cb7c0
1216b17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,118 +7,40 @@ import ( | |
"github.com/osmosis-labs/osmosis/osmomath" | ||
"github.com/osmosis-labs/osmosis/osmoutils" | ||
"github.com/osmosis-labs/osmosis/v21/x/poolmanager/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
) | ||
|
||
// GetTakerFeeTrackerForStakers returns the taker fee for stakers tracker for all denoms that has been | ||
// collected since the accounting height. | ||
func (k Keeper) GetTakerFeeTrackerForStakers(ctx sdk.Context) []sdk.Coin { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Getters, Getters by Denom, and Update logic follows the same pattern that protorev uses for tracking profits by denom. |
||
takerFeesForStakers := make([]sdk.Coin, 0) | ||
|
||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, types.KeyTakerFeeStakersProtoRevArray) | ||
|
||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
bz := iterator.Value() | ||
takerFeeForStakers := sdk.Coin{} | ||
if err := takerFeeForStakers.Unmarshal(bz); err == nil { | ||
takerFeesForStakers = append(takerFeesForStakers, takerFeeForStakers) | ||
} | ||
} | ||
|
||
return takerFeesForStakers | ||
return osmoutils.GetCoinArrayFromPrefix(ctx, k.storeKey, types.KeyTakerFeeStakersProtoRevArray) | ||
} | ||
|
||
// GetTakerFeeTrackerForStakersByDenom returns the taker fee for stakers tracker for the specified denom that has been | ||
// collected since the accounting height. | ||
// collected since the accounting height. If the denom is not found, a zero coin is returned. | ||
func (k Keeper) GetTakerFeeTrackerForStakersByDenom(ctx sdk.Context, denom string) (sdk.Coin, error) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyTakerFeeStakersProtoRevArray) | ||
key := types.GetKeyPrefixTakerFeeStakersProtoRevByDenom(denom) | ||
|
||
bz := store.Get(key) | ||
if len(bz) == 0 { | ||
return sdk.NewCoin(denom, osmomath.ZeroInt()), nil | ||
} | ||
|
||
takerFeeForStakers := sdk.Coin{} | ||
if err := takerFeeForStakers.Unmarshal(bz); err != nil { | ||
return sdk.NewCoin(denom, osmomath.ZeroInt()), err | ||
} | ||
|
||
return takerFeeForStakers, nil | ||
return osmoutils.GetCoinByDenomFromPrefix(ctx, k.storeKey, types.KeyTakerFeeStakersProtoRevArray, denom) | ||
} | ||
|
||
// UpdateTakerFeeTrackerForStakersByDenom increases the take fee for stakers tracker for the specified denom by the specified amount. | ||
func (k Keeper) UpdateTakerFeeTrackerForStakersByDenom(ctx sdk.Context, denom string, increasedAmt osmomath.Int) error { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyTakerFeeStakersProtoRevArray) | ||
key := types.GetKeyPrefixTakerFeeStakersProtoRevByDenom(denom) | ||
|
||
takerFeeForStakers, _ := k.GetTakerFeeTrackerForStakersByDenom(ctx, denom) | ||
takerFeeForStakers.Amount = takerFeeForStakers.Amount.Add(increasedAmt) | ||
bz, err := takerFeeForStakers.Marshal() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store.Set(key, bz) | ||
return nil | ||
return osmoutils.IncreaseCoinByDenomFromPrefix(ctx, k.storeKey, types.KeyTakerFeeStakersProtoRevArray, denom, increasedAmt) | ||
} | ||
|
||
// GetTakerFeeTrackerForCommunityPool returns the taker fee for community pool tracker for all denoms that has been | ||
// collected since the accounting height. | ||
func (k Keeper) GetTakerFeeTrackerForCommunityPool(ctx sdk.Context) []sdk.Coin { | ||
takerFeesForCommunityPool := make([]sdk.Coin, 0) | ||
|
||
store := ctx.KVStore(k.storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, types.KeyTakerFeeCommunityPoolProtoRevArray) | ||
|
||
defer iterator.Close() | ||
for ; iterator.Valid(); iterator.Next() { | ||
bz := iterator.Value() | ||
takerFeeForCommunityPool := sdk.Coin{} | ||
if err := takerFeeForCommunityPool.Unmarshal(bz); err == nil { | ||
takerFeesForCommunityPool = append(takerFeesForCommunityPool, takerFeeForCommunityPool) | ||
} | ||
} | ||
|
||
return takerFeesForCommunityPool | ||
return osmoutils.GetCoinArrayFromPrefix(ctx, k.storeKey, types.KeyTakerFeeCommunityPoolProtoRevArray) | ||
} | ||
|
||
// GetTakerFeeTrackerForCommunityPoolByDenom returns the taker fee for community pool tracker for the specified denom that has been | ||
// collected since the accounting height. | ||
// collected since the accounting height. If the denom is not found, a zero coin is returned. | ||
func (k Keeper) GetTakerFeeTrackerForCommunityPoolByDenom(ctx sdk.Context, denom string) (sdk.Coin, error) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyTakerFeeCommunityPoolProtoRevArray) | ||
key := types.GetKeyPrefixTakerFeeCommunityPoolProtoRevByDenom(denom) | ||
|
||
bz := store.Get(key) | ||
if len(bz) == 0 { | ||
return sdk.NewCoin(denom, osmomath.ZeroInt()), nil | ||
} | ||
|
||
takerFeeForCommunityPool := sdk.Coin{} | ||
if err := takerFeeForCommunityPool.Unmarshal(bz); err != nil { | ||
return sdk.NewCoin(denom, osmomath.ZeroInt()), err | ||
} | ||
|
||
return takerFeeForCommunityPool, nil | ||
return osmoutils.GetCoinByDenomFromPrefix(ctx, k.storeKey, types.KeyTakerFeeCommunityPoolProtoRevArray, denom) | ||
} | ||
|
||
// UpdateTakerFeeTrackerForCommunityPoolByDenom increases the take fee for community pool tracker for the specified denom by the specified amount. | ||
func (k Keeper) UpdateTakerFeeTrackerForCommunityPoolByDenom(ctx sdk.Context, denom string, increasedAmt osmomath.Int) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need so many similar methods? What if we only had one that takes store prefix as an input and made short wrappers that would call this shared function? This would reduce code duplication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, added the logic to osmoutils here ee425f0 Lmk if you think I should just keep in poolmanger instead. I thought the logic could be extendable to any other time we need to track coins in the future, which I imagine will occur again. |
||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyTakerFeeCommunityPoolProtoRevArray) | ||
key := types.GetKeyPrefixTakerFeeCommunityPoolProtoRevByDenom(denom) | ||
|
||
takerFeeForCommunityPool, _ := k.GetTakerFeeTrackerForCommunityPoolByDenom(ctx, denom) | ||
takerFeeForCommunityPool.Amount = takerFeeForCommunityPool.Amount.Add(increasedAmt) | ||
bz, err := takerFeeForCommunityPool.Marshal() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store.Set(key, bz) | ||
return nil | ||
return osmoutils.IncreaseCoinByDenomFromPrefix(ctx, k.storeKey, types.KeyTakerFeeCommunityPoolProtoRevArray, denom, increasedAmt) | ||
} | ||
|
||
// GetTakerFeeTrackerStartHeight gets the height from which we started accounting for taker fees. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we only marshal the amount, since the key contains the denom?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ValarDragon Is there performance improvement if we just unmarshal the amount and then from that create a coin to return?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things:
a27e3c8
a582fa1
c3f306d
c665113