Skip to content

Commit

Permalink
perf: remove unnecessary null byte (#2110)
Browse files Browse the repository at this point in the history
* perf: remove unnecessary null byte

* Apply suggestions from code review

Co-authored-by: Adam Moser <63419657+toteki@users.noreply.github.com>

* feat: add borrow factor (#2114)

* cl++

* cl++

* rephrase test - sign changes based on cpu architecture so strict equal avoided

* implement borrow factor restriction

* fix new uToken calc

* Update x/leverage/keeper/oracle.go

* Update x/leverage/types/token.go

* revert

* revert

* implement borrow factor in userMaxBorrow

* implement userMaxBorrow + cause failing wasm test

* lint

* add denom to test suite registry

* max withdaw test

* withdraw and max withdraw tests

* borrow and maxborrow tests

* renaming suggestion

* halfDec

* rename

* README

* Update x/leverage/README.md

Co-authored-by: Robert Zaremba <robert@zaremba.ch>

* readme suggestions

---------

Co-authored-by: Robert Zaremba <robert@zaremba.ch>

* tests: remove gb from e2e tests (#2119)

* tests: remove gb from e2e tests

* go.mod tidy

---------

Co-authored-by: Adam Moser <63419657+toteki@users.noreply.github.com>
  • Loading branch information
robert-zaremba and toteki authored Jun 28, 2023
1 parent 532a243 commit bc66b64
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
11 changes: 11 additions & 0 deletions util/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ func ExtractAddressAndString(startIndex int, key []byte) (addr sdk.AccAddress, s
s, nextIndex, err = ExtractString(nextIndex, key)
return addr, s, nextIndex, err
}

// ToStr takes the full key and converts it to a string
func ToStr(key []byte) string {
return string(key)
}

// NoLastByte returns sub-slice of the key without the last byte.
// Panics if length of key is zero.
func NoLastByte(key []byte) string {
return string(key[:len(key)-1])
}
6 changes: 0 additions & 6 deletions util/store/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,3 @@ func SumCoins(s storetypes.KVStore, f StrExtractor) sdk.Coins {
// StrExtractor is a function type which will take a bytes string value and extracts
// string out of it.
type StrExtractor func([]byte) string

// NoLastByte returns sub-slice of the key without the last byte.
// Panics if length of key is zero.
func NoLastByte(key []byte) string {
return string(key[:len(key)-1])
}
3 changes: 2 additions & 1 deletion util/store/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gotest.tools/v3/assert"

"github.com/umee-network/umee/v5/tests/tsdk"
"github.com/umee-network/umee/v5/util/keys"
)

func TestIterate(t *testing.T) {
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestSumCoins(t *testing.T) {
}

pdb := prefixstore.NewStore(db, []byte(prefix))
sum := SumCoins(pdb, NoLastByte)
sum := SumCoins(pdb, keys.NoLastByte)
sum.Sort()
assert.DeepEqual(t, expected, sum)
}
4 changes: 2 additions & 2 deletions x/incentive/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (k Keeper) getAllAccountUnbondings(ctx sdk.Context) ([]incentive.AccountUnb

// getAllTotalUnbonding gets total unbonding for all uTokens (used for a query)
func (k Keeper) getAllTotalUnbonding(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalUnbonding), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalUnbonding), keys.ToStr)
}

// getAllTotalBonded gets total bonded for all uTokens (used for a query)
func (k Keeper) getAllTotalBonded(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalBonded), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalBonded), keys.ToStr)
}
8 changes: 4 additions & 4 deletions x/incentive/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func keyIncentiveProgram(id uint32, status incentive.ProgramStatus) []byte {

// keyTotalBonded returns a KVStore key for total bonded uTokens of a given denom.
func keyTotalBonded(denom string) []byte {
// totalBondedPrefix | denom | 0x00
return util.ConcatBytes(1, keyPrefixTotalBonded, []byte(denom))
// totalBondedPrefix | denom
return util.ConcatBytes(0, keyPrefixTotalBonded, []byte(denom))
}

// keyTotalUnbonding returns a KVStore key for total unbonding uTokens of a given denom.
func keyTotalUnbonding(denom string) []byte {
// totalUnbondingPrefix | denom | 0x00
return util.ConcatBytes(1, keyPrefixTotalUnbonding, []byte(denom))
// totalUnbondingPrefix | denom
return util.ConcatBytes(0, keyPrefixTotalUnbonding, []byte(denom))
}

// keyBondAmount returns a KVStore key for bonded amounts for a uToken denom and account.
Expand Down
5 changes: 3 additions & 2 deletions x/leverage/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/umee-network/umee/v5/util"
"github.com/umee-network/umee/v5/util/keys"
"github.com/umee-network/umee/v5/util/store"
"github.com/umee-network/umee/v5/x/leverage/types"
)
Expand Down Expand Up @@ -177,12 +178,12 @@ func (k Keeper) SweepBadDebts(ctx sdk.Context) error {

// GetAllUTokenSupply returns total supply of all uToken denoms.
func (k Keeper) GetAllUTokenSupply(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixUtokenSupply), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixUtokenSupply), keys.NoLastByte)
}

// GetAllReserves returns all reserves.
func (k Keeper) GetAllReserves(ctx sdk.Context) sdk.Coins {
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixReserveAmount), store.NoLastByte)
return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixReserveAmount), keys.NoLastByte)
}

func (k Keeper) prefixStore(ctx sdk.Context, p []byte) storetypes.KVStore {
Expand Down

0 comments on commit bc66b64

Please sign in to comment.