diff --git a/CHANGELOG.md b/CHANGELOG.md index b8bb02992..9afbd93d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/go.mod b/go.mod index 70c21776e..322533e1f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7c421befb..ec9a85ba1 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/x/perp/v2/keeper/dnr.go b/x/perp/v2/keeper/dnr.go index dfffda830..e9d687191 100644 --- a/x/perp/v2/keeper/dnr.go +++ b/x/perp/v2/keeper/dnr.go @@ -1,8 +1,6 @@ package keeper import ( - "math/big" - "cosmossdk.io/math" "github.com/NibiruChain/collections" sdk "github.com/cosmos/cosmos-sdk/types" @@ -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. diff --git a/x/perp/v2/keeper/dnr_int_encoder_test.go b/x/perp/v2/keeper/dnr_int_encoder_test.go deleted file mode 100644 index f59ce1bd3..000000000 --- a/x/perp/v2/keeper/dnr_int_encoder_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package keeper - -import ( - "math/big" - "testing" - - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" -) - -func TestIntEncoder(t *testing.T) { - // we test our assumptions around int are correct. - outOfBounds := new(big.Int).Lsh(big.NewInt(1), 256) // 2^256 - maxBigInt := new(big.Int).Sub(outOfBounds, big.NewInt(1)) // 2^256 - 1 - require.Equal(t, maxBigInt.BitLen(), math.MaxBitLen) - require.Greater(t, outOfBounds.BitLen(), math.MaxBitLen) - - require.NotPanics(t, func() { - sdk.NewIntFromBigInt(maxBigInt) - }) - require.Panics(t, func() { - sdk.NewIntFromBigInt(outOfBounds) - }) - - require.Equal(t, maxIntKeyLen, len(maxBigInt.Bytes())) - - // test encoding ordering - enc1 := IntKeyEncoder.Encode(sdk.NewInt(50_000)) - enc2 := IntKeyEncoder.Encode(sdk.NewInt(100_000)) - require.Less(t, enc1, enc2) - - // test decoding - size, got1 := IntKeyEncoder.Decode(enc1) - require.Equal(t, maxIntKeyLen, size) - _, got2 := IntKeyEncoder.Decode(enc2) - require.Equal(t, sdk.NewInt(50_000), got1) - require.Equal(t, sdk.NewInt(100_000), got2) - - // require panics on negative values - require.Panics(t, func() { - IntKeyEncoder.Encode(sdk.NewInt(-1)) - }) - // require panics on invalid int - require.Panics(t, func() { - IntKeyEncoder.Encode(math.Int{}) - }) - - // test value encoder - value := sdk.NewInt(50_000) - valueBytes := IntValueEncoder.Encode(value) - gotValue := IntValueEncoder.Decode(valueBytes) - require.Equal(t, value, gotValue) - - // panics on invalid math.Int - require.Panics(t, func() { - IntValueEncoder.Encode(math.Int{}) - }) -} diff --git a/x/perp/v2/keeper/keeper.go b/x/perp/v2/keeper/keeper.go index 7f60ccd36..998e36b7f 100644 --- a/x/perp/v2/keeper/keeper.go +++ b/x/perp/v2/keeper/keeper.go @@ -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(