Skip to content

Commit

Permalink
chore(deps): collections to v0.4.0 for math.Int value encoder (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Dec 12, 2023
1 parent b400eed commit 8d4ee29
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 124 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Bump `github.com/grpc-ecosystem/grpc-gateway/v2` from 2.18.0 to 2.18.1 ([#1675](https://github.com/NibiruChain/nibiru/pull/1675))
* Bump `actions/setup-go` from 4 to 5 ([#1696](https://github.com/NibiruChain/nibiru/pull/1696))
* Bump `golang` from 1.19 to 1.21 ([#1698](https://github.com/NibiruChain/nibiru/pull/1698))
* [#1678](https://github.com/NibiruChain/nibiru/pull/1678) - chore(deps): collections to v0.4.0 for math.Int value encoder


## [v1.1.0] - 2023-11-20
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/CosmWasm/wasmd v0.44.0
github.com/CosmWasm/wasmvm v1.5.0
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/NibiruChain/collections v0.3.0
github.com/NibiruChain/collections v0.4.0
github.com/armon/go-metrics v0.4.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZ
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/NibiruChain/collections v0.3.0 h1:DB2RPzzgcHk35lRXuJ1cPOezweAZ6/c/Guq3bAo4W6w=
github.com/NibiruChain/collections v0.3.0/go.mod h1:tKTlBL+Cs1oJnS4tT9MIaFWr7BWsUXrc7KPzP1LxRBo=
github.com/NibiruChain/collections v0.4.0 h1:KNJj+CJyqOT/Q33kcVzT2uLYIiwhiFAeZMhGLPge5Og=
github.com/NibiruChain/collections v0.4.0/go.mod h1:tKTlBL+Cs1oJnS4tT9MIaFWr7BWsUXrc7KPzP1LxRBo=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
Expand Down
58 changes: 0 additions & 58 deletions x/perp/v2/keeper/dnr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"math/big"

"cosmossdk.io/math"
"github.com/NibiruChain/collections"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -12,62 +10,6 @@ import (
"github.com/NibiruChain/nibiru/x/common/asset"
)

// IntValueEncoder instructs collections on how to encode a math.Int as a value.
// TODO: move to collections.
var IntValueEncoder collections.ValueEncoder[math.Int] = intValueEncoder{}

// IntKeyEncoder instructs collections on how to encode a math.Int as a key.
// NOTE: unsafe to use as the first part of a composite key.
var IntKeyEncoder collections.KeyEncoder[math.Int] = intKeyEncoder{}

type intValueEncoder struct{}

func (intValueEncoder) Encode(value math.Int) []byte {
return IntKeyEncoder.Encode(value)
}

func (intValueEncoder) Decode(b []byte) math.Int {
_, got := IntKeyEncoder.Decode(b)
return got
}

func (intValueEncoder) Stringify(value math.Int) string {
return IntKeyEncoder.Stringify(value)
}

func (intValueEncoder) Name() string {
return "math.Int"
}

type intKeyEncoder struct{}

const maxIntKeyLen = math.MaxBitLen / 8

func (intKeyEncoder) Encode(key math.Int) []byte {
if key.IsNil() {
panic("cannot encode invalid math.Int")
}
if key.IsNegative() {
panic("cannot encode negative math.Int")
}
i := key.BigInt()

be := i.Bytes()
padded := make([]byte, maxIntKeyLen)
copy(padded[maxIntKeyLen-len(be):], be)
return padded
}

func (intKeyEncoder) Decode(b []byte) (int, math.Int) {
if len(b) != maxIntKeyLen {
panic("invalid key length")
}
i := new(big.Int).SetBytes(b)
return maxIntKeyLen, math.NewIntFromBigInt(i)
}

func (intKeyEncoder) Stringify(key math.Int) string { return key.String() }

// maybeUpdateDnREpoch checks if the current epoch hook call matches the
// epoch name that targets discounts and rebates, if it does then we simply
// invoke the StartNewEpoch function to kickstart a new epoch.
Expand Down
59 changes: 0 additions & 59 deletions x/perp/v2/keeper/dnr_int_encoder_test.go

This file was deleted.

8 changes: 4 additions & 4 deletions x/perp/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ func NewKeeper(
GlobalVolumes: collections.NewMap(
storeKey, NamespaceGlobalVolumes,
collections.Uint64KeyEncoder,
IntValueEncoder,
collections.IntValueEncoder,
),
TraderVolumes: collections.NewMap(
storeKey, NamespaceUserVolumes,
collections.PairKeyEncoder(collections.AccAddressKeyEncoder, collections.Uint64KeyEncoder),
IntValueEncoder,
collections.IntValueEncoder,
),
GlobalDiscounts: collections.NewMap(
storeKey, NamespaceGlobalDiscounts,
IntKeyEncoder,
collections.IntKeyEncoder,
collections.DecValueEncoder,
),
TraderDiscounts: collections.NewMap(
storeKey, NamespaceUserDiscounts,
collections.PairKeyEncoder(collections.AccAddressKeyEncoder, IntKeyEncoder),
collections.PairKeyEncoder(collections.AccAddressKeyEncoder, collections.IntKeyEncoder),
collections.DecValueEncoder,
),
EpochRebateAllocations: collections.NewMap(
Expand Down

0 comments on commit 8d4ee29

Please sign in to comment.