From 0c1bc13ed02008213db5293b4de491f26b859303 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 19 Mar 2023 19:07:09 -0500 Subject: [PATCH] utilize bytes.Buffer to build keys --- tests/e2e/e2e_test.go | 6 +- x/concentrated-liquidity/store.go | 22 ++++--- x/concentrated-liquidity/types/keys.go | 85 +++++++++++++++++--------- 3 files changed, 73 insertions(+), 40 deletions(-) diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 98f6040542f..6bd29648c34 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -246,9 +246,9 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() { positionsAddress3 := node.QueryConcentratedPositions(address3) // assert number of positions per address - s.Require().Equal(len(positionsAddress1), 2) - s.Require().Equal(len(positionsAddress2), 1) - s.Require().Equal(len(positionsAddress3), 2) + s.Require().Equal(2, len(positionsAddress1)) + s.Require().Equal(1, len(positionsAddress2)) + s.Require().Equal(2, len(positionsAddress3)) // Assert returned positions: validateCLPosition := func(position model.Position, poolId uint64, lowerTick, upperTick int64) { diff --git a/x/concentrated-liquidity/store.go b/x/concentrated-liquidity/store.go index 527316b1028..3666c2969ed 100644 --- a/x/concentrated-liquidity/store.go +++ b/x/concentrated-liquidity/store.go @@ -2,6 +2,7 @@ package concentrated_liquidity import ( "bytes" + "encoding/binary" "errors" "fmt" "strconv" @@ -163,37 +164,44 @@ func ParseFullPositionFromBytes(key, value []byte) (model.Position, error) { // - upper tick // - join time // - freeze duration - address, err := sdk.AccAddressFromHex(fullPositionKeyComponents[1]) + address, err := sdk.AccAddressFromHex(fmt.Sprintf("%x", []byte(fullPositionKeyComponents[1]))) if err != nil { return model.Position{}, err } - poolId, err := strconv.ParseUint(fullPositionKeyComponents[2], 10, 64) + var poolId uint64 + err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[2])), binary.BigEndian, &poolId) if err != nil { return model.Position{}, err } - lowerTick, err := strconv.ParseInt(fullPositionKeyComponents[3], 10, 64) + var lowerTick int64 + err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[3])), binary.BigEndian, &lowerTick) if err != nil { return model.Position{}, err } - upperTick, err := strconv.ParseInt(fullPositionKeyComponents[4], 10, 64) + var upperTick int64 + err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[4])), binary.BigEndian, &upperTick) if err != nil { return model.Position{}, err } - joinTime, err := osmoutils.ParseTimeString(fullPositionKeyComponents[5]) + var joinTimeUnix int64 + err = binary.Read(bytes.NewReader([]byte(fullPositionKeyComponents[5])), binary.BigEndian, &joinTimeUnix) if err != nil { return model.Position{}, err } + joinTime := time.Unix(0, joinTimeUnix).UTC() - freezeDuration, err := strconv.ParseUint(fullPositionKeyComponents[6], 10, 64) + var freezeDuration time.Duration + err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[6])), binary.BigEndian, &freezeDuration) if err != nil { return model.Position{}, err } - positionId, err := strconv.ParseUint(fullPositionKeyComponents[7], 10, 64) + var positionId uint64 + err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[7])), binary.BigEndian, &positionId) if err != nil { return model.Position{}, err } diff --git a/x/concentrated-liquidity/types/keys.go b/x/concentrated-liquidity/types/keys.go index 5f8617b0f3e..0275f4cdcdd 100644 --- a/x/concentrated-liquidity/types/keys.go +++ b/x/concentrated-liquidity/types/keys.go @@ -1,16 +1,13 @@ package types import ( - "encoding/hex" + "bytes" + "encoding/binary" "fmt" - "strconv" - "strings" "time" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" - - "github.com/osmosis-labs/osmosis/osmoutils" ) const ( @@ -118,41 +115,69 @@ func keyTickPrefixByPoolIdPrealloc(poolId uint64, preAllocBytes int) []byte { // KeyFullPosition uses pool Id, owner, lower tick, upper tick, joinTime, freezeDuration, and positionId for keys func KeyFullPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64, joinTime time.Time, freezeDuration time.Duration, positionId uint64) []byte { - joinTimeKey := osmoutils.FormatTimeString(joinTime) - - var builder strings.Builder - builder.Grow(len(PositionPrefix) + 9*len(KeySeparator) + len(addr.Bytes())*2 + 5*20) // pre-allocating the buffer - - builder.Write(PositionPrefix) - builder.WriteString(KeySeparator) - builder.WriteString(hex.EncodeToString(addr.Bytes())) - builder.WriteString(KeySeparator) - builder.WriteString(strconv.FormatUint(poolId, 10)) - builder.WriteString(KeySeparator) - builder.WriteString(strconv.FormatInt(lowerTick, 10)) - builder.WriteString(KeySeparator) - builder.WriteString(strconv.FormatInt(upperTick, 10)) - builder.WriteString(KeySeparator) - builder.WriteString(joinTimeKey) - builder.WriteString(KeySeparator) - builder.WriteString(strconv.FormatUint(uint64(freezeDuration), 10)) - builder.WriteString(KeySeparator) - builder.WriteString(strconv.FormatUint(positionId, 10)) - - return []byte(builder.String()) + var buf bytes.Buffer + + buf.Grow(len(PositionPrefix) + 7*len(KeySeparator) + len(addr.Bytes()) + 7*8) // pre-allocating the buffer + + buf.Write(PositionPrefix) + buf.WriteString(KeySeparator) + buf.Write(addr.Bytes()) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, poolId) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, lowerTick) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, upperTick) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, joinTime.UTC().UnixNano()) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, uint64(freezeDuration)) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, positionId) + + return buf.Bytes() } // KeyPosition uses pool Id, owner, lower tick and upper tick for keys func KeyPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64) []byte { - return []byte(fmt.Sprintf("%s%s%x%s%d%s%d%s%d", PositionPrefix, KeySeparator, addr.Bytes(), KeySeparator, poolId, KeySeparator, lowerTick, KeySeparator, upperTick)) + var buf bytes.Buffer + buf.Grow(len(PositionPrefix) + 4*len(KeySeparator) + len(addr.Bytes()) + 4*8) // pre-allocating the buffer + + buf.Write(PositionPrefix) + buf.WriteString(KeySeparator) + buf.Write(addr.Bytes()) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, poolId) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, lowerTick) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, upperTick) + + return buf.Bytes() } func KeyAddressAndPoolId(addr sdk.AccAddress, poolId uint64) []byte { - return []byte(fmt.Sprintf("%s%s%x%s%d", PositionPrefix, KeySeparator, addr.Bytes(), KeySeparator, poolId)) + var buf bytes.Buffer + buf.Grow(len(PositionPrefix) + 2*len(KeySeparator) + len(addr.Bytes()) + 2*8) // pre-allocating the buffer + + buf.Write(PositionPrefix) + buf.WriteString(KeySeparator) + buf.Write(addr.Bytes()) + buf.WriteString(KeySeparator) + binary.Write(&buf, binary.BigEndian, poolId) + + return buf.Bytes() } func KeyUserPositions(addr sdk.AccAddress) []byte { - return []byte(fmt.Sprintf("%s%s%x", PositionPrefix, KeySeparator, addr.Bytes())) + var buf bytes.Buffer + buf.Grow(len(PositionPrefix) + len(KeySeparator) + len(addr.Bytes()) + 8) // pre-allocating the buffer + + buf.Write(PositionPrefix) + buf.WriteString(KeySeparator) + buf.Write(addr.Bytes()) + + return buf.Bytes() } func KeyPool(poolId uint64) []byte {