From 1e6f419dd50342143a70a8784683e8ea8a3441ec Mon Sep 17 00:00:00 2001 From: beer-1 Date: Thu, 5 Sep 2024 18:25:17 +0900 Subject: [PATCH] bump deps --- Dockerfile | 2 +- app/modules.go | 2 +- cmd/move/move.go | 4 +- go.mod | 6 +- go.sum | 12 ++-- x/ibc/nft-transfer/keeper/integration_test.go | 2 +- x/move/client/cli/query.go | 4 +- x/move/client/cli/tx.go | 8 +-- x/move/client/cli/utils.go | 27 +++++--- x/move/client/cli/utils_test.go | 2 + x/move/common_test.go | 17 ++--- x/move/keeper/api.go | 17 +++-- x/move/keeper/api_test.go | 6 +- x/move/keeper/dex_test.go | 17 ++--- x/move/keeper/nft.go | 2 +- x/move/keeper/staking.go | 33 +++++----- x/move/keeper/staking_test.go | 24 +++++-- x/move/types/connector.go | 65 ++++++++++++------- x/mstaking/keeper/delegation_test.go | 17 ++--- 19 files changed, 149 insertions(+), 118 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0f5c69f6..05ff6659 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ ARG TARGETARCH ARG GOARCH # See https://github.com/initia-labs/movevm/releases -ENV LIBMOVEVM_VERSION=v0.4.4 +ENV LIBMOVEVM_VERSION=v0.4.5 # Install necessary packages RUN set -eux; apk add --no-cache ca-certificates build-base git cmake diff --git a/app/modules.go b/app/modules.go index 517270cc..5e39d40e 100644 --- a/app/modules.go +++ b/app/modules.go @@ -236,7 +236,7 @@ func orderInitBlockers() []string { authz.ModuleName, group.ModuleName, upgradetypes.ModuleName, feegrant.ModuleName, consensusparamtypes.ModuleName, ibcexported.ModuleName, ibctransfertypes.ModuleName, ibcnfttransfertypes.ModuleName, icatypes.ModuleName, icaauthtypes.ModuleName, ibcfeetypes.ModuleName, - ibcpermtypes.ModuleName, consensusparamtypes.ModuleName, auctiontypes.ModuleName, ophosttypes.ModuleName, + ibcpermtypes.ModuleName, auctiontypes.ModuleName, ophosttypes.ModuleName, oracletypes.ModuleName, marketmaptypes.ModuleName, packetforwardtypes.ModuleName, ibchookstypes.ModuleName, forwardingtypes.ModuleName, ratelimittypes.ModuleName, } diff --git a/cmd/move/move.go b/cmd/move/move.go index 6b6e142a..5f46a0a8 100644 --- a/cmd/move/move.go +++ b/cmd/move/move.go @@ -190,11 +190,11 @@ func moveEncodeCmd(ac address.Codec) *cobra.Command { Provide BCS encoding for move arguments. Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: - $ %s move encode --args '["address:0x1", "bool:true", "u8:0x01", "u128:1234", "vector:a,b,c,d", "string:hello world"]' + $ %s move encode --args '["address:0x1", "bool:true", "u8:0x01", "u128:1234", "vector:a,b,c,d", "string:hello world", "bigdecimal:123.456"]' `, version.AppName, ), Args: cobra.ExactArgs(0), diff --git a/go.mod b/go.mod index 27484ce3..59f1a272 100644 --- a/go.mod +++ b/go.mod @@ -38,9 +38,9 @@ require ( github.com/gorilla/mux v1.8.1 github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/hashicorp/go-metrics v0.5.3 - github.com/initia-labs/OPinit v0.4.2 + github.com/initia-labs/OPinit v0.4.3 // we also need to update `LIBMOVEVM_VERSION` of Dockerfile#9 - github.com/initia-labs/movevm v0.4.4 + github.com/initia-labs/movevm v0.4.5 github.com/noble-assets/forwarding/v2 v2.0.0-20240521090705-86712c4c9e43 github.com/pelletier/go-toml v1.9.5 github.com/pkg/errors v0.9.1 @@ -148,7 +148,7 @@ require ( github.com/iancoleman/strcase v0.3.0 // indirect github.com/improbable-eng/grpc-web v0.15.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/initia-labs/OPinit/api v0.4.1 // indirect + github.com/initia-labs/OPinit/api v0.4.3 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/jmhodges/levigo v1.0.0 // indirect github.com/klauspost/compress v1.17.9 // indirect diff --git a/go.sum b/go.sum index 9a2f494e..642e91ef 100644 --- a/go.sum +++ b/go.sum @@ -727,14 +727,14 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= -github.com/initia-labs/OPinit v0.4.2 h1:cfE6LXb3EquDK1/UTDT62o9c2HOQL0E9pWTr5BWAf8g= -github.com/initia-labs/OPinit v0.4.2/go.mod h1:bM+tav+ER4uC6U84PB5vgnRUNyjc/phNgHGYQX9ALhg= -github.com/initia-labs/OPinit/api v0.4.1 h1:Q8etW92LiwekKZxzDYVFdiHF3uOpEA4nyajy8zpcxB0= -github.com/initia-labs/OPinit/api v0.4.1/go.mod h1:Xy/Nt3ubXLQ4zKn0m7RuQOM1sj8TVdlNNyek21TGYR0= +github.com/initia-labs/OPinit v0.4.3 h1:tFpcT9qeOLS49tFdEeK9ACEibeCEYd+V4Oz69gQPvp8= +github.com/initia-labs/OPinit v0.4.3/go.mod h1:1bf2//8NDHa2geXj81wm+2tLOW7zD6PQiGR6eakim00= +github.com/initia-labs/OPinit/api v0.4.3 h1:qljFODGw3F2ClGgJD4uiw1QXb3Px9tJX3jqWolPco/Q= +github.com/initia-labs/OPinit/api v0.4.3/go.mod h1:NorLLEBESDeLPQIzTFIT2XjvD/tkS1VRE6YL59TXYT0= github.com/initia-labs/cometbft v0.0.0-20240802022359-e0a5ce0336b6 h1:ObKvj9nGiKE+MWZ/LnH3rPcXsnBVVn4c30/iw+fMsd4= github.com/initia-labs/cometbft v0.0.0-20240802022359-e0a5ce0336b6/go.mod h1:jHPx9vQpWzPHEAiYI/7EDKaB1NXhK6o3SArrrY8ExKc= -github.com/initia-labs/movevm v0.4.4 h1:JUUzxi+SOdfhpazWPVVMHj88om0zGDe+PWEGV2Yi8aQ= -github.com/initia-labs/movevm v0.4.4/go.mod h1:6MxR4GP5zH3JUc1IMgfqAe1e483mZVS7fshPknZPJ30= +github.com/initia-labs/movevm v0.4.5 h1:C1KfVQJiIqH3fcSlQ/KELRmEu2y3VAsVMSSyKTBfE0k= +github.com/initia-labs/movevm v0.4.5/go.mod h1:aUWdvFZPdULjJ2McQTE+mLnfnG3CLAz0TWJRFzFFUwg= github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls= github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= diff --git a/x/ibc/nft-transfer/keeper/integration_test.go b/x/ibc/nft-transfer/keeper/integration_test.go index 54e6780d..8bcf4444 100644 --- a/x/ibc/nft-transfer/keeper/integration_test.go +++ b/x/ibc/nft-transfer/keeper/integration_test.go @@ -38,7 +38,7 @@ func (suite *KeeperTestSuite) CreateNftClass( movetypes.MoveModuleNameInitiaNft, movetypes.FunctionNameInitiaNftCreateCollection, []vmtypes.TypeTag{}, - [][]byte{descBz, {0}, nameBz, uriBz, {0}, {0}, {0}, {0}, {0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + [][]byte{descBz, {0}, nameBz, uriBz, {0}, {0}, {0}, {0}, {0}, {0}}, ) suite.Require().NoError(err, "MakeCollection error on chain") diff --git a/x/move/client/cli/query.go b/x/move/client/cli/query.go index 16b1623d..9d695f4f 100644 --- a/x/move/client/cli/query.go +++ b/x/move/client/cli/query.go @@ -283,7 +283,7 @@ func GetCmdQueryViewFunction(ac address.Codec) *cobra.Command { Get an view function execution result Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: @@ -360,7 +360,7 @@ func GetCmdQueryViewJSONFunction(ac address.Codec) *cobra.Command { Get an view function execution result Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: "0x1" "true" "0" "hello vector" ["a","b","c","d"] Example: diff --git a/x/move/client/cli/tx.go b/x/move/client/cli/tx.go index ccc8efb2..4fcec045 100644 --- a/x/move/client/cli/tx.go +++ b/x/move/client/cli/tx.go @@ -119,7 +119,7 @@ func ExecuteCmd(ac address.Codec) *cobra.Command { Execute an entry function of a published module Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: @@ -198,7 +198,7 @@ func ExecuteJSONCmd(ac address.Codec) *cobra.Command { Execute an entry function of a published module Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: "0x1" "true" "0" "hello vector" ["a","b","c","d"] Example: @@ -273,7 +273,7 @@ func ScriptCmd(ac address.Codec) *cobra.Command { Execute a given script Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: @@ -347,7 +347,7 @@ func ScriptJSONCmd(ac address.Codec) *cobra.Command { Execute a given script Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, - vector, option, decimal128, decimal256, fixed_point32, fixed_point64 + vector, option, biguint, bigdecimal, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: diff --git a/x/move/client/cli/utils.go b/x/move/client/cli/utils.go index 3737c065..f0c4bf4c 100644 --- a/x/move/client/cli/utils.go +++ b/x/move/client/cli/utils.go @@ -8,6 +8,7 @@ import ( "fmt" "math/big" "regexp" + "slices" "strconv" "strings" @@ -197,20 +198,30 @@ func bcsSerializeArg(argType string, arg string, s serde.Serializer, ac address. High: highHigh, }) return s.GetBytes(), nil - case "decimal128": - dec, err := sdkmath.LegacyNewDecFromStr(arg) + case "biguint": + n, err := sdkmath.ParseUint(arg) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to parse %q as biguint", err) } - decstr := dec.MulInt64(1000000000000000000).TruncateInt().String() - return bcsSerializeArg("u128", decstr, s, ac) - case "decimal256": + + // convert to little endian + bz := n.BigInt().Bytes() + slices.Reverse(bz) + + err = s.SerializeBytes(bz) + return s.GetBytes(), err + case "bigdecimal": dec, err := sdkmath.LegacyNewDecFromStr(arg) if err != nil { return nil, err } - decstr := dec.MulInt64(1000000000000000000).TruncateInt().String() - return bcsSerializeArg("u256", decstr, s, ac) + + // convert to little endian + bz := dec.BigInt().Bytes() + slices.Reverse(bz) + + err = s.SerializeBytes(bz) + return s.GetBytes(), err case "fixed_point32": dec, err := sdkmath.LegacyNewDecFromStr(arg) if err != nil { diff --git a/x/move/client/cli/utils_test.go b/x/move/client/cli/utils_test.go index 69990032..74e44131 100644 --- a/x/move/client/cli/utils_test.go +++ b/x/move/client/cli/utils_test.go @@ -110,6 +110,8 @@ func Test_BCSEncode(t *testing.T) { {"vector:0", []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, "vector 0"}, {"vector:1", []byte{0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, false, "vector 1"}, {"badtype:1", nil, true, "badtype 1"}, + {"biguint:1077952576", []byte{4, 64, 64, 64, 64}, false, "biguint 1077952576"}, + {"bigdecimal:0.00000027595585952", []byte{5, 64, 64, 64, 64, 64}, false, "bigdecimal 0.00000027595585952"}, } ac := address.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()) diff --git a/x/move/common_test.go b/x/move/common_test.go index e71c8652..be780e1f 100644 --- a/x/move/common_test.go +++ b/x/move/common_test.go @@ -1,8 +1,7 @@ package move_test import ( - "bytes" - "encoding/binary" + "slices" "testing" initiaapp "github.com/initia-labs/initia/app" @@ -113,18 +112,14 @@ func executeMsgs(t *testing.T, app *initiaapp.InitiaApp, msgs []sdk.Msg, account } func decToVmArgument(t *testing.T, val math.LegacyDec) []byte { + // big-endian bytes bz := val.BigInt().Bytes() - diff := 16 - len(bz) - require.True(t, diff >= 0) - if diff > 0 { - bz = append(bytes.Repeat([]byte{0}, diff), bz...) - } - high := binary.BigEndian.Uint64(bz[:8]) - low := binary.BigEndian.Uint64(bz[8:16]) + // reverse bytes to little-endian + slices.Reverse(bz) - // serialize to uint128 - bz, err := vmtypes.SerializeUint128(high, low) + // serialize bytes + bz, err := vmtypes.SerializeBytes(bz) require.NoError(t, err) return bz diff --git a/x/move/keeper/api.go b/x/move/keeper/api.go index 222bb474..e4629192 100644 --- a/x/move/keeper/api.go +++ b/x/move/keeper/api.go @@ -55,23 +55,23 @@ func (api GoApi) GetAccountInfo(addr vmtypes.AccountAddress) (bool /* found */, } // AmountToShare convert amount to share -func (api GoApi) AmountToShare(valBz []byte, metadata vmtypes.AccountAddress, amount uint64) (uint64, error) { +func (api GoApi) AmountToShare(valBz []byte, metadata vmtypes.AccountAddress, amount uint64) (string, error) { valAddr, err := api.vc.StringToBytes(string(valBz)) if err != nil { - return 0, err + return "0", err } denom, err := types.DenomFromMetadataAddress(api.ctx, NewMoveBankKeeper(&api.Keeper), metadata) if err != nil { - return 0, err + return "0", err } share, err := api.Keeper.AmountToShare(api.ctx, valAddr, sdk.NewCoin(denom, math.NewIntFromUint64(amount))) - return share.Uint64(), err + return share.String(), err } // ShareToAmount convert share to amount -func (api GoApi) ShareToAmount(valBz []byte, metadata vmtypes.AccountAddress, share uint64) (uint64, error) { +func (api GoApi) ShareToAmount(valBz []byte, metadata vmtypes.AccountAddress, share string) (uint64, error) { valAddr, err := api.vc.StringToBytes(string(valBz)) if err != nil { return 0, err @@ -82,7 +82,12 @@ func (api GoApi) ShareToAmount(valBz []byte, metadata vmtypes.AccountAddress, sh return 0, err } - amount, err := api.Keeper.ShareToAmount(api.ctx, valAddr, sdk.NewDecCoin(denom, math.NewIntFromUint64(share))) + dec, err := math.LegacyNewDecFromStr(share) + if err != nil { + return 0, err + } + + amount, err := api.Keeper.ShareToAmount(api.ctx, valAddr, sdk.NewDecCoinFromDec(denom, dec)) return amount.Uint64(), err } diff --git a/x/move/keeper/api_test.go b/x/move/keeper/api_test.go index 35418f43..f3c744e1 100644 --- a/x/move/keeper/api_test.go +++ b/x/move/keeper/api_test.go @@ -109,7 +109,7 @@ func Test_AmountToShareAPI(t *testing.T) { api := keeper.NewApi(input.MoveKeeper, ctx) amount, err := api.AmountToShare([]byte(valAddr.String()), metadata, 150) require.NoError(t, err) - require.Equal(t, uint64(150), amount) + require.Equal(t, "150.000000000000000000", amount) } func Test_AmountToShareAPI_InvalidAddr(t *testing.T) { @@ -154,7 +154,7 @@ func Test_ShareToAmountAPI(t *testing.T) { require.NoError(t, err) api := keeper.NewApi(input.MoveKeeper, ctx) - amount, err := api.ShareToAmount([]byte(valAddr.String()), metadata, 150) + amount, err := api.ShareToAmount([]byte(valAddr.String()), metadata, "150") require.NoError(t, err) require.Equal(t, uint64(150), amount) } @@ -178,7 +178,7 @@ func Test_ShareToAmountAPI_InvalidAddr(t *testing.T) { require.NoError(t, err) api := keeper.NewApi(input.MoveKeeper, ctx) - _, err = api.ShareToAmount(valAddr, metadata, 150) + _, err = api.ShareToAmount(valAddr, metadata, "150") require.Error(t, err) } diff --git a/x/move/keeper/dex_test.go b/x/move/keeper/dex_test.go index 8ee2d77d..e53af428 100644 --- a/x/move/keeper/dex_test.go +++ b/x/move/keeper/dex_test.go @@ -1,8 +1,7 @@ package keeper_test import ( - "bytes" - "encoding/binary" + "slices" "testing" "cosmossdk.io/math" @@ -16,18 +15,14 @@ import ( ) func decToVmArgument(t *testing.T, val math.LegacyDec) []byte { + // big-endian bytes bz := val.BigInt().Bytes() - diff := 16 - len(bz) - require.True(t, diff >= 0) - if diff > 0 { - bz = append(bytes.Repeat([]byte{0}, diff), bz...) - } - high := binary.BigEndian.Uint64(bz[:8]) - low := binary.BigEndian.Uint64(bz[8:16]) + // reverse bytes to little-endian + slices.Reverse(bz) - // serialize to uint128 - bz, err := vmtypes.SerializeUint128(high, low) + // serialize bytes + bz, err := vmtypes.SerializeBytes(bz) require.NoError(t, err) return bz diff --git a/x/move/keeper/nft.go b/x/move/keeper/nft.go index 6277a3cc..d032939a 100644 --- a/x/move/keeper/nft.go +++ b/x/move/keeper/nft.go @@ -161,7 +161,7 @@ func (k NftKeeper) initializeCollection(ctx context.Context, collectionName, cla types.MoveModuleNameInitiaNft, types.FunctionNameInitiaNftCreateCollection, []vmtypes.TypeTag{}, - [][]byte{collectionDescBz, {0}, collectionNameBz, collectionUriBz, {0}, {0}, {0}, {0}, {0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, + [][]byte{collectionDescBz, {0}, collectionNameBz, collectionUriBz, {0}, {0}, {0}, {0}, {0}, {0}}, ) } diff --git a/x/move/keeper/staking.go b/x/move/keeper/staking.go index fadfbffa..7c0a1893 100644 --- a/x/move/keeper/staking.go +++ b/x/move/keeper/staking.go @@ -3,6 +3,7 @@ package keeper import ( "context" "errors" + "fmt" "cosmossdk.io/collections" "cosmossdk.io/math" @@ -17,18 +18,18 @@ import ( ) // AmountToShare convert token to share in the ratio of a validator's share/token -func (k Keeper) AmountToShare(ctx context.Context, valAddr sdk.ValAddress, amount sdk.Coin) (math.Int, error) { +func (k Keeper) AmountToShare(ctx context.Context, valAddr sdk.ValAddress, amount sdk.Coin) (math.LegacyDec, error) { val, err := k.StakingKeeper.Validator(ctx, valAddr) if err != nil { - return math.ZeroInt(), err + return math.LegacyZeroDec(), err } shares, err := val.SharesFromTokens(sdk.NewCoins(amount)) if err != nil { - return math.ZeroInt(), err + return math.LegacyZeroDec(), err } - return shares.AmountOf(amount.Denom).TruncateInt(), err + return shares.AmountOf(amount.Denom), err } // ShareToAmount convert share to token in the ratio of a validator's token/share @@ -238,8 +239,12 @@ func (k Keeper) ApplyStakingDeltas( delegations[valAddrStr] = delegations[valAddrStr].Add(delCoin) } - if delta.Undelegation > 0 { - undelCoin := sdk.NewDecCoin(denom, math.NewIntFromUint64(delta.Undelegation)) + unbondingDecAmount, err := math.LegacyNewDecFromStr(delta.Undelegation) + if err != nil { + return err + } + if unbondingDecAmount.IsPositive() { + undelCoin := sdk.NewDecCoinFromDec(denom, unbondingDecAmount) undelegations[valAddrStr] = undelegations[valAddrStr].Add(undelCoin) } } @@ -440,18 +445,12 @@ func (k Keeper) SlashUnbondingDelegations( } for _, metadata := range metadatas { - fractionArg, err := vmtypes.SerializeString(fraction.String()) - if err != nil { - return err - } - - valArg, err := vmtypes.SerializeString(valAddr.String()) - if err != nil { - return err - } + metadataArg := fmt.Sprintf("\"%s\"", metadata.String()) + fractionArg := fmt.Sprintf("\"%s\"", fraction.String()) + valArg := fmt.Sprintf("\"%s\"", valAddr.String()) - args := [][]byte{metadata[:], valArg, fractionArg} - if err := k.ExecuteEntryFunction( + args := []string{metadataArg, valArg, fractionArg} + if err := k.ExecuteEntryFunctionJSON( ctx, vmtypes.StdAddress, vmtypes.StdAddress, diff --git a/x/move/keeper/staking_test.go b/x/move/keeper/staking_test.go index bb0b041b..7c5e6600 100644 --- a/x/move/keeper/staking_test.go +++ b/x/move/keeper/staking_test.go @@ -129,7 +129,7 @@ func TestAmountToShare(t *testing.T) { amount := sdk.NewCoin(bondDenom, math.NewInt(150)) share, err := input.MoveKeeper.AmountToShare(ctx, valAddr, amount) require.NoError(t, err) - require.Equal(t, math.NewInt(150), share) + require.Equal(t, math.LegacyNewDecFromInt(math.NewInt(150)), share) } func TestShareToAmount(t *testing.T) { @@ -330,7 +330,7 @@ func TestApplyStakingDeltas(t *testing.T) { unbondingShare, unbondingCoinStore, err := types.ReadUnbondingInfosFromStakingState(tableEntry.ValueBytes) require.NoError(t, err) - require.Equal(t, unbondingShare, math.NewInt(25_000_000)) + require.Equal(t, unbondingShare, math.LegacyNewDecFromInt(math.NewInt(25_000_000))) _, unbondingAmount, err := keeper.NewMoveBankKeeper(&input.MoveKeeper).Balance(ctx, unbondingCoinStore) require.NoError(t, err) @@ -416,9 +416,6 @@ func Test_SlashUnbondingDelegations(t *testing.T) { require.NoError(t, err) require.Equal(t, delegation.Shares, sdk.NewDecCoins(sdk.NewDecCoin(secondBondDenom, math.NewInt(25_000_000)))) - // slash 5% - input.MoveKeeper.Hooks().SlashUnbondingDelegations(ctx, valAddr, math.LegacyNewDecWithPrec(5, 2)) - // check staking state tableHandle, err := input.MoveKeeper.GetStakingStatesTableHandle(ctx) require.NoError(t, err) @@ -437,9 +434,24 @@ func Test_SlashUnbondingDelegations(t *testing.T) { unbondingShare, unbondingCoinStore, err := types.ReadUnbondingInfosFromStakingState(tableEntry.ValueBytes) require.NoError(t, err) + require.Equal(t, unbondingShare, math.LegacyNewDecFromInt(math.NewInt(25_000_000))) _, unbondingAmount, err := keeper.NewMoveBankKeeper(&input.MoveKeeper).Balance(ctx, unbondingCoinStore) require.NoError(t, err) + require.Equal(t, unbondingAmount, math.NewInt(25_000_000)) + + // slash 5% + input.MoveKeeper.Hooks().SlashUnbondingDelegations(ctx, valAddr, math.LegacyNewDecWithPrec(5, 2)) + + // read validator entry + tableEntry, err = input.MoveKeeper.GetTableEntry(ctx, metadataTableHandle, keyBz) + require.NoError(t, err) + + unbondingShare, _, err = types.ReadUnbondingInfosFromStakingState(tableEntry.ValueBytes) + require.NoError(t, err) + + _, unbondingAmount, err = keeper.NewMoveBankKeeper(&input.MoveKeeper).Balance(ctx, unbondingCoinStore) + require.NoError(t, err) require.Equal(t, unbondingAmount, math.NewInt(23_750_000)) - require.Equal(t, unbondingShare, math.NewInt(25_000_000)) + require.Equal(t, unbondingShare, math.LegacyNewDecFromInt(math.NewInt(25_000_000))) } diff --git a/x/move/types/connector.go b/x/move/types/connector.go index a00eea5a..63f78dbd 100644 --- a/x/move/types/connector.go +++ b/x/move/types/connector.go @@ -2,6 +2,7 @@ package types import ( "math/big" + "slices" "cosmossdk.io/errors" "cosmossdk.io/math" @@ -192,15 +193,13 @@ func DeserializeUint64(bz []byte) (math.Int, error) { return num, nil } -// DeserializeDecimal deserialize uint128 bytes to math.Int -func DeserializeDecimal(bz []byte) (math.LegacyDec, error) { - num, err := DeserializeUint128(bz) - if err != nil { - return math.LegacyZeroDec(), err - } +// DeserializeBigDecimal deserialize uint128 bytes to math.Int +func DeserializeBigDecimal(bz []byte) (math.LegacyDec, error) { + slices.Reverse(bz) + num := new(big.Int).SetBytes(bz) // fractional part length is 18 - return math.LegacyNewDecFromIntWithPrec(num, 18), nil + return math.LegacyNewDecFromIntWithPrec(math.NewIntFromBigInt(num), 18), nil } // DeserializeUint128 deserialize uint128 bytes to math.Int @@ -324,31 +323,46 @@ func ReadWeightsFromDexConfig(timestamp math.Int, bz []byte) (math.LegacyDec, ma cursor += AddressBytesLength + 8 // before weights - weightCoinABefore, err := DeserializeDecimal(bz[cursor : cursor+16]) + weightLen, len := readULEB128(bz[cursor:]) + cursor += len + weightCoinABefore, err := DeserializeBigDecimal(bz[cursor : cursor+weightLen]) if err != nil { return math.LegacyZeroDec(), math.LegacyZeroDec(), err } - weightCoinBBefore, err := DeserializeDecimal(bz[cursor+16 : cursor+32]) + cursor += weightLen + + weightLen, len = readULEB128(bz[cursor:]) + cursor += len + weightCoinBBefore, err := DeserializeBigDecimal(bz[cursor : cursor+weightLen]) if err != nil { return math.LegacyZeroDec(), math.LegacyZeroDec(), err } - timestampBefore, err := DeserializeUint64(bz[cursor+32 : cursor+40]) + cursor += weightLen + + timestampBefore, err := DeserializeUint64(bz[cursor : cursor+8]) if err != nil { return math.LegacyZeroDec(), math.LegacyZeroDec(), err } - - cursor += 40 + cursor += 8 // after weights - weightCoinAAfter, err := DeserializeDecimal(bz[cursor : cursor+16]) + weightLen, len = readULEB128(bz[cursor:]) + cursor += len + weightCoinAAfter, err := DeserializeBigDecimal(bz[cursor : cursor+weightLen]) if err != nil { return math.LegacyZeroDec(), math.LegacyZeroDec(), err } - weightCoinBAfter, err := DeserializeDecimal(bz[cursor+16 : cursor+32]) + cursor += weightLen + + weightLen, len = readULEB128(bz[cursor:]) + cursor += len + weightCoinBAfter, err := DeserializeBigDecimal(bz[cursor : cursor+weightLen]) if err != nil { return math.LegacyZeroDec(), math.LegacyZeroDec(), err } - timestampAfter, err := DeserializeUint64(bz[cursor+32 : cursor+40]) + cursor += weightLen + + timestampAfter, err := DeserializeUint64(bz[cursor : cursor+8]) if err != nil { return math.LegacyZeroDec(), math.LegacyZeroDec(), err } @@ -421,7 +435,7 @@ func GetBaseSpotPrice( } // ReadUnbondingInfosFromStakingState util function to read unbonding coin amount from the StakingState -func ReadUnbondingInfosFromStakingState(bz []byte) (unbondingShare math.Int, unbondingCoinStore vmtypes.AccountAddress, err error) { +func ReadUnbondingInfosFromStakingState(bz []byte) (unbondingShare math.LegacyDec, unbondingCoinStore vmtypes.AccountAddress, err error) { cursor := int(0) // read metadata @@ -431,19 +445,22 @@ func ReadUnbondingInfosFromStakingState(bz []byte) (unbondingShare math.Int, unb valLen, len := readULEB128(bz[cursor:]) cursor += (valLen + len) - // read total_share - cursor += 16 + // read total_share(BigDecimal) + decLen, len := readULEB128(bz[cursor:]) + cursor += (decLen + len) - // read unbonding_share - unbondingShare, err = DeserializeUint128(bz[cursor : cursor+16]) + // read unbonding_share(BigDecimal) + decLen, len = readULEB128(bz[cursor:]) + cursor += len + unbondingShare, err = DeserializeBigDecimal(bz[cursor : cursor+decLen]) if err != nil { return } + cursor += decLen - cursor += 16 - - // read reward_index(Decimal128) - cursor += 16 + // read reward_index(BigDecimal) + decLen, len = readULEB128(bz[cursor:]) + cursor += (decLen + len) // read reward_coin_store_ref(ExtendRef) cursor += AddressBytesLength + 8 diff --git a/x/mstaking/keeper/delegation_test.go b/x/mstaking/keeper/delegation_test.go index 309162a8..a7a791ae 100644 --- a/x/mstaking/keeper/delegation_test.go +++ b/x/mstaking/keeper/delegation_test.go @@ -1,8 +1,7 @@ package keeper_test import ( - "bytes" - "encoding/binary" + "slices" "testing" "time" @@ -19,18 +18,14 @@ import ( ) func decToVmArgument(t *testing.T, val math.LegacyDec) []byte { + // big-endian bytes bz := val.BigInt().Bytes() - diff := 16 - len(bz) - require.True(t, diff >= 0) - if diff > 0 { - bz = append(bytes.Repeat([]byte{0}, diff), bz...) - } - high := binary.BigEndian.Uint64(bz[:8]) - low := binary.BigEndian.Uint64(bz[8:16]) + // reverse bytes to little-endian + slices.Reverse(bz) - // serialize to uint128 - bz, err := vmtypes.SerializeUint128(high, low) + // serialize bytes + bz, err := vmtypes.SerializeBytes(bz) require.NoError(t, err) return bz