From 6f9122cf4afa80fe1f295825e5593652b2a2dd80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Tue, 26 Jan 2021 18:35:38 +0800 Subject: [PATCH 1/3] Optimized token accuracy conversion --- modules/token/types/token.go | 30 +++---- modules/token/types/token_test.go | 135 ++++++++++++++++++++++++++---- 2 files changed, 129 insertions(+), 36 deletions(-) diff --git a/modules/token/types/token.go b/modules/token/types/token.go index 41f1243c..720b2e45 100644 --- a/modules/token/types/token.go +++ b/modules/token/types/token.go @@ -2,7 +2,7 @@ package types import ( "encoding/json" - "math" + "math/big" "strconv" "github.com/gogo/protobuf/proto" @@ -12,6 +12,12 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) +var ( + _ proto.Message = &Token{} + tenInt = big.NewInt(10) +) + +// TokenI define a interface for Token type TokenI interface { GetSymbol() string GetName() string @@ -26,8 +32,6 @@ type TokenI interface { ToMinCoin(coin sdk.DecCoin) (sdk.Coin, error) } -var _ proto.Message = &Token{} - // NewToken constructs a new Token instance func NewToken( symbol string, @@ -115,15 +119,9 @@ func (t Token) ToMainCoin(coin sdk.Coin) (sdk.DecCoin, error) { return sdk.NewDecCoin(coin.Denom, coin.Amount), nil } - precision := math.Pow10(int(t.Scale)) - precisionStr := strconv.FormatFloat(precision, 'f', 0, 64) - precisionDec, err := sdk.NewDecFromStr(precisionStr) - if err != nil { - return sdk.DecCoin{}, err - } - + precision := new(big.Int).Exp(tenInt, big.NewInt(int64(t.Scale)), nil) // dest amount = src amount / 10^(scale) - amount := sdk.NewDecFromInt(coin.Amount).Quo(precisionDec) + amount := sdk.NewDecFromInt(coin.Amount).Quo(sdk.NewDecFromBigInt(precision)) return sdk.NewDecCoinFromDec(t.Symbol, amount), nil } @@ -137,15 +135,9 @@ func (t Token) ToMinCoin(coin sdk.DecCoin) (newCoin sdk.Coin, err error) { return sdk.NewCoin(coin.Denom, coin.Amount.TruncateInt()), nil } - precision := math.Pow10(int(t.Scale)) - precisionStr := strconv.FormatFloat(precision, 'f', 0, 64) - precisionDec, err := sdk.NewDecFromStr(precisionStr) - if err != nil { - return sdk.Coin{}, err - } - + precision := new(big.Int).Exp(tenInt, big.NewInt(int64(t.Scale)), nil) // dest amount = src amount * 10^(dest scale) - amount := coin.Amount.Mul(precisionDec) + amount := coin.Amount.Mul(sdk.NewDecFromBigInt(precision)) return sdk.NewCoin(t.MinUnit, amount.TruncateInt()), nil } diff --git a/modules/token/types/token_test.go b/modules/token/types/token_test.go index cb46c7ed..67c40176 100644 --- a/modules/token/types/token_test.go +++ b/modules/token/types/token_test.go @@ -1,15 +1,15 @@ package types import ( + "fmt" + "reflect" "testing" - "github.com/stretchr/testify/require" - sdk "github.com/cosmos/cosmos-sdk/types" ) -func TestToken_ToMinCoin(t *testing.T) { - token := Token{ +var ( + token = Token{ Symbol: "iris", Name: "irisnet", Scale: 18, @@ -19,19 +19,7 @@ func TestToken_ToMinCoin(t *testing.T) { Mintable: true, Owner: "", } - - amt, err := sdk.NewDecFromStr("1.500000000000000001") - require.NoError(t, err) - coin := sdk.NewDecCoinFromDec(token.Symbol, amt) - - c, err := token.ToMinCoin(coin) - require.NoError(t, err) - require.Equal(t, "1500000000000000001atto", c.String()) - - coin1, err := token.ToMainCoin(c) - require.NoError(t, err) - require.Equal(t, coin, coin1) -} +) func TestCheckKeywords(t *testing.T) { type args struct { @@ -61,3 +49,116 @@ func TestCheckKeywords(t *testing.T) { }) } } + +func TestToken_ToMinCoin(t *testing.T) { + type args struct { + coin sdk.DecCoin + } + + for i := uint32(0); i <= MaximumScale; i++ { + token.Scale = i + tests := []struct { + name string + args args + want sdk.Coin + wantErr bool + success bool + }{ + { + name: fmt.Sprintf("Main Coin to Min Coin,scale=%d", i), + wantErr: false, + args: args{coin: sdk.NewDecCoin(token.Symbol, sdk.NewInt(10))}, + want: sdk.NewCoin(token.MinUnit, sdk.NewIntWithDecimal(10, int(token.Scale))), + success: true, + }, + { + name: fmt.Sprintf("Main Coin to Min Coin Failed,scale=%d", i), + wantErr: false, + args: args{coin: sdk.NewDecCoin(token.Symbol, sdk.NewInt(10))}, + want: sdk.NewCoin(token.MinUnit, sdk.NewInt(10)), + success: (i == 0), + }, + { + name: fmt.Sprintf("Min Coin to Min Coin Success,scale=%d", i), + wantErr: false, + args: args{coin: sdk.NewDecCoin(token.MinUnit, sdk.NewInt(10))}, + want: sdk.NewCoin(token.MinUnit, sdk.NewInt(10)), + success: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := Token{ + Symbol: token.Symbol, + Scale: token.Scale, + MinUnit: token.MinUnit, + } + got, err := tr.ToMinCoin(tt.args.coin) + if (err != nil) != tt.wantErr { + t.Errorf("Token.ToMainCoin() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.success != reflect.DeepEqual(got, tt.want) { + t.Errorf("Token.ToMainCoin() = %v, want %v", got, tt.want) + } + }) + } + } +} + +func TestToken_ToMainCoin(t *testing.T) { + type args struct { + coin sdk.Coin + } + + for i := uint32(0); i <= MaximumScale; i++ { + token.Scale = i + tests := []struct { + name string + args args + want sdk.DecCoin + wantErr bool + success bool + }{ + { + name: "Main Coin to Main Coin", + wantErr: false, + args: args{coin: sdk.NewCoin(token.Symbol, sdk.NewInt(10))}, + want: sdk.NewInt64DecCoin(token.Symbol, 10), + success: true, + }, + { + name: "Min Coin to Main Coin Failed", + wantErr: false, + args: args{coin: sdk.NewCoin(token.MinUnit, sdk.NewInt(10))}, + want: sdk.NewInt64DecCoin(token.Symbol, 10), + success: (i == 0), + }, + { + name: "Min Coin to Main Coin Success", + wantErr: false, + args: args{coin: sdk.NewCoin(token.MinUnit, sdk.NewInt(10))}, + want: sdk.NewDecCoinFromDec(token.Symbol, + sdk.NewDecWithPrec(1, int64(token.Scale)).MulInt64(10)), + success: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := Token{ + Symbol: token.Symbol, + Scale: token.Scale, + MinUnit: token.MinUnit, + } + got, err := tr.ToMainCoin(tt.args.coin) + if (err != nil) != tt.wantErr { + t.Errorf("Token.ToMainCoin() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.success != reflect.DeepEqual(got, tt.want) { + t.Errorf("Token.ToMainCoin() = %v, want %v", got, tt.want) + } + }) + } + } +} From eb89fa9d28cdbe6b5fd9dd5cbe88f6a38ad21449 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Thu, 28 Jan 2021 09:32:54 +0800 Subject: [PATCH 2/3] remove TotalBurn unit convert --- modules/token/keeper/grpc_query.go | 20 +----- modules/token/types/query.pb.go | 106 ++++++++++++++--------------- proto/token/query.proto | 94 ++++++++++++------------- 3 files changed, 101 insertions(+), 119 deletions(-) diff --git a/modules/token/keeper/grpc_query.go b/modules/token/keeper/grpc_query.go index 19ed44e2..3c853800 100644 --- a/modules/token/keeper/grpc_query.go +++ b/modules/token/keeper/grpc_query.go @@ -135,21 +135,7 @@ func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types // TotalBurn return the all burn coin func (k Keeper) TotalBurn(c context.Context, req *types.QueryTotalBurnRequest) (*types.QueryTotalBurnResponse, error) { ctx := sdk.UnwrapSDKContext(c) - burnCoins := k.GetAllBurnCoin(ctx) - - coins := make([]sdk.DecCoin, len(burnCoins)) - for i, coin := range burnCoins { - token, err := k.GetToken(ctx, coin.Denom) - if err != nil { - return nil, status.Errorf(codes.Internal, err.Error()) - } - - mainCoin, err := token.ToMainCoin(coin) - if err != nil { - return nil, status.Errorf(codes.Internal, err.Error()) - } - - coins[i] = mainCoin - } - return &types.QueryTotalBurnResponse{BurnedCoins: coins}, nil + return &types.QueryTotalBurnResponse{ + BurnedCoins: k.GetAllBurnCoin(ctx), + }, nil } diff --git a/modules/token/types/query.pb.go b/modules/token/types/query.pb.go index 7221072a..4b06983d 100644 --- a/modules/token/types/query.pb.go +++ b/modules/token/types/query.pb.go @@ -466,7 +466,7 @@ var xxx_messageInfo_QueryTotalBurnRequest proto.InternalMessageInfo // QueryTotalBurnResponse is response type for the Query/TotalBurn RPC method type QueryTotalBurnResponse struct { - BurnedCoins []types1.DecCoin `protobuf:"bytes,1,rep,name=burned_coins,json=burnedCoins,proto3" json:"burned_coins"` + BurnedCoins []types1.Coin `protobuf:"bytes,1,rep,name=burned_coins,json=burnedCoins,proto3" json:"burned_coins"` } func (m *QueryTotalBurnResponse) Reset() { *m = QueryTotalBurnResponse{} } @@ -502,7 +502,7 @@ func (m *QueryTotalBurnResponse) XXX_DiscardUnknown() { var xxx_messageInfo_QueryTotalBurnResponse proto.InternalMessageInfo -func (m *QueryTotalBurnResponse) GetBurnedCoins() []types1.DecCoin { +func (m *QueryTotalBurnResponse) GetBurnedCoins() []types1.Coin { if m != nil { return m.BurnedCoins } @@ -525,56 +525,56 @@ func init() { func init() { proto.RegisterFile("token/query.proto", fileDescriptor_ec043bcd18c4056e) } var fileDescriptor_ec043bcd18c4056e = []byte{ - // 776 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4f, 0x53, 0x13, 0x4b, - 0x10, 0x4f, 0x80, 0xe4, 0x85, 0x81, 0x57, 0x0f, 0xe6, 0x85, 0x3f, 0xc9, 0x83, 0x24, 0xec, 0x13, - 0x51, 0x2c, 0x76, 0x0b, 0xb8, 0x28, 0x37, 0x83, 0x46, 0xbd, 0x58, 0x98, 0xf2, 0xe4, 0x25, 0xb5, - 0x49, 0x9a, 0xb8, 0x45, 0x76, 0x26, 0xec, 0xcc, 0x22, 0x29, 0x8a, 0x8b, 0x55, 0xde, 0xad, 0xf2, - 0xe6, 0xc9, 0x0f, 0xe1, 0x87, 0xa0, 0x3c, 0x61, 0x79, 0xf1, 0x44, 0x59, 0xe0, 0x27, 0xf0, 0xe8, - 0xc9, 0xda, 0x99, 0xde, 0xb8, 0x81, 0x0d, 0x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, 0x15, 0x8b, 0x4b, 0xf7, 0xc8, 0x48, 0xb0, 0x23, - 0xb4, 0x18, 0x57, 0x2a, 0xb2, 0x6a, 0xf9, 0xd2, 0xe0, 0x00, 0x44, 0xba, 0xa3, 0x90, 0x16, 0xe9, - 0xff, 0x03, 0x3a, 0xd4, 0xbb, 0x79, 0x68, 0x6d, 0x07, 0x78, 0x8c, 0xa4, 0xb5, 0x66, 0xe2, 0xfb, - 0xec, 0x53, 0x65, 0x7c, 0x9f, 0xfd, 0x0a, 0x1d, 0xd8, 0x27, 0x6a, 0xf1, 0x80, 0x8c, 0xf6, 0x24, - 0x43, 0x6f, 0xc4, 0xcf, 0xad, 0x5f, 0x6a, 0xf9, 0xc5, 0x2b, 0xa2, 0x10, 0x78, 0x41, 0x01, 0xff, - 0x47, 0x73, 0x17, 0xda, 0x96, 0x76, 0xbb, 0x16, 0x48, 0xab, 0xfc, 0xf8, 0xe8, 0xb4, 0x90, 0x3c, - 0x3e, 0x2d, 0x24, 0xbf, 0x9d, 0x16, 0x92, 0x6f, 0xcf, 0x0a, 0x89, 0xe3, 0xb3, 0x42, 0xe2, 0xeb, - 0x59, 0x21, 0xf1, 0xc2, 0x8c, 0xec, 0x6d, 0x90, 0xce, 0x40, 0xf6, 0xca, 0xb8, 0xbc, 0xe9, 0xb7, - 0x41, 0x84, 0xe5, 0x82, 0x1d, 0xae, 0xa7, 0xd5, 0x8b, 0xbb, 0xfe, 0x2b, 0x00, 0x00, 0xff, 0xff, - 0xb9, 0x1b, 0xa3, 0x9e, 0x84, 0x07, 0x00, 0x00, + // 772 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x54, 0x4f, 0x4f, 0x53, 0x4b, + 0x14, 0x6f, 0x81, 0xf6, 0x95, 0x81, 0x97, 0x07, 0xf3, 0xca, 0x9f, 0xf6, 0x3d, 0xda, 0x72, 0x15, + 0x51, 0x0c, 0xf7,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, 0x00, 0xff, 0xff, 0xc4, 0x43, 0x2f, 0x13, + 0x81, 0x07, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -2235,7 +2235,7 @@ func (m *QueryTotalBurnResponse) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.BurnedCoins = append(m.BurnedCoins, types1.DecCoin{}) + m.BurnedCoins = append(m.BurnedCoins, types1.Coin{}) if err := m.BurnedCoins[len(m.BurnedCoins)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } diff --git a/proto/token/query.proto b/proto/token/query.proto index 32c8b047..577c11a1 100644 --- a/proto/token/query.proto +++ b/proto/token/query.proto @@ -13,72 +13,68 @@ option go_package = "github.com/irisnet/irismod/modules/token/types"; // Query creates service with token as rpc service Query { - // Token returns token with token name - rpc Token(QueryTokenRequest) returns (QueryTokenResponse) { - option (google.api.http).get = "/irismod/token/tokens/{denom}"; - } - // Tokens returns the token list - rpc Tokens(QueryTokensRequest) returns (QueryTokensResponse) { - option (google.api.http).get = "/irismod/token/tokens"; - } - // Fees returns the fees to issue or mint a token - rpc Fees(QueryFeesRequest) returns (QueryFeesResponse) { - option (google.api.http).get = "/irismod/token/tokens/{symbol}/fees"; - } - // Params queries the token parameters - rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { - option (google.api.http).get = "/irismod/token/params"; - } - // Params queries the token parameters - rpc TotalBurn(QueryTotalBurnRequest) returns (QueryTotalBurnResponse) { - option (google.api.http).get = "/irismod/token/total_burn"; - } + // Token returns token with token name + rpc Token(QueryTokenRequest) returns (QueryTokenResponse) { + option (google.api.http).get = "/irismod/token/tokens/{denom}"; + } + // Tokens returns the token list + rpc Tokens(QueryTokensRequest) returns (QueryTokensResponse) { + option (google.api.http).get = "/irismod/token/tokens"; + } + // Fees returns the fees to issue or mint a token + rpc Fees(QueryFeesRequest) returns (QueryFeesResponse) { + option (google.api.http).get = "/irismod/token/tokens/{symbol}/fees"; + } + // Params queries the token parameters + rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { + option (google.api.http).get = "/irismod/token/params"; + } + // Params queries the token parameters + rpc TotalBurn(QueryTotalBurnRequest) returns (QueryTotalBurnResponse) { + option (google.api.http).get = "/irismod/token/total_burn"; + } } // QueryTokenRequest is request type for the Query/Token RPC method -message QueryTokenRequest { - string denom = 1; -} +message QueryTokenRequest { string denom = 1; } // QueryTokenResponse is response type for the Query/Token RPC method message QueryTokenResponse { - google.protobuf.Any Token = 1 - [ (cosmos_proto.accepts_interface) = "ContentI" ]; + google.protobuf.Any Token = 1 + [ (cosmos_proto.accepts_interface) = "ContentI" ]; } // QueryTokensRequest is request type for the Query/Tokens RPC method message QueryTokensRequest { - string owner = 1; - // pagination defines an optional pagination for the request. - cosmos.base.query.v1beta1.PageRequest pagination = 2; + string owner = 1; + // pagination defines an optional pagination for the request. + cosmos.base.query.v1beta1.PageRequest pagination = 2; } // QueryTokensResponse is response type for the Query/Tokens RPC method message QueryTokensResponse { - repeated google.protobuf.Any Tokens = 1 - [ (cosmos_proto.accepts_interface) = "ContentI" ]; + repeated google.protobuf.Any Tokens = 1 + [ (cosmos_proto.accepts_interface) = "ContentI" ]; - cosmos.base.query.v1beta1.PageResponse pagination = 2; + cosmos.base.query.v1beta1.PageResponse pagination = 2; } // QueryFeesRequest is request type for the Query/Fees RPC method -message QueryFeesRequest { - string symbol = 1; -} +message QueryFeesRequest { string symbol = 1; } // QueryFeesResponse is response type for the Query/Fees RPC method message QueryFeesResponse { - bool exist = 1; - cosmos.base.v1beta1.Coin issue_fee = 2 [ - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"issue_fee\"", - (gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.Coin" - ]; - cosmos.base.v1beta1.Coin mint_fee = 3 [ - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"mint_fee\"", - (gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.Coin" - ]; + bool exist = 1; + cosmos.base.v1beta1.Coin issue_fee = 2 [ + (gogoproto.nullable) = false, + (gogoproto.moretags) = "yaml:\"issue_fee\"", + (gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.Coin" + ]; + cosmos.base.v1beta1.Coin mint_fee = 3 [ + (gogoproto.nullable) = false, + (gogoproto.moretags) = "yaml:\"mint_fee\"", + (gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.Coin" + ]; } // QueryParametersRequest is request type for the Query/Parameters RPC method @@ -86,9 +82,9 @@ message QueryParamsRequest {} // QueryParametersResponse is response type for the Query/Parameters RPC method message QueryParamsResponse { - token.Params params = 1 [ (gogoproto.nullable) = false ]; + token.Params params = 1 [ (gogoproto.nullable) = false ]; - cosmos.base.query.v1beta1.PageResponse res = 2; + cosmos.base.query.v1beta1.PageResponse res = 2; } // QueryTokenRequest is request type for the Query/TotalBurn RPC method @@ -96,6 +92,6 @@ message QueryTotalBurnRequest {} // QueryTotalBurnResponse is response type for the Query/TotalBurn RPC method message QueryTotalBurnResponse { - repeated cosmos.base.v1beta1.DecCoin burned_coins = 1 - [ (gogoproto.nullable) = false ]; + repeated cosmos.base.v1beta1.Coin burned_coins = 1 + [ (gogoproto.nullable) = false ]; } From e2f0e8ca5b37e7542ee860b1865aaca5ab21174d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BF=97=E5=BC=BA?= Date: Mon, 1 Feb 2021 14:32:15 +0800 Subject: [PATCH 3/3] fix test error --- modules/token/keeper/grpc_query_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/token/keeper/grpc_query_test.go b/modules/token/keeper/grpc_query_test.go index becf7cc0..0456a1ef 100644 --- a/modules/token/keeper/grpc_query_test.go +++ b/modules/token/keeper/grpc_query_test.go @@ -76,11 +76,8 @@ func (suite *KeeperTestSuite) TestGRPCQueryTotalBurn() { buinCoin := sdk.NewInt64Coin("satoshi", 1000000000000000000) app.TokenKeeper.AddBurnCoin(ctx, buinCoin) - expCoin, err := token.ToMainCoin(buinCoin) - suite.Require().NoError(err) - resp, err := queryClient.TotalBurn(gocontext.Background(), &types.QueryTotalBurnRequest{}) suite.Require().NoError(err) suite.Len(resp.BurnedCoins, 1) - suite.EqualValues(expCoin, resp.BurnedCoins[0]) + suite.EqualValues(buinCoin, resp.BurnedCoins[0]) }