Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: service add profiling mode, fix msg type and replace Minus to SafeMinus #806

Merged
merged 3 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/service/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func GetCmdSvcWithdrawTax(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-tax",
Short: "withdraw service fee tax to a account",
Example: "iriscli service withdraw-tax --chain-id=<chain-id> --from=<key name> --fee=0.004iris --dest-address=<account address> --amount=1iris",
Example: "iriscli service withdraw-tax --chain-id=<chain-id> --from=<key name> --fee=0.004iris --dest-address=<account address> --withdraw-amount=1iris",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc).WithLogger(os.Stdout).
WithAccountDecoder(utils.GetAccountDecoder(cdc))
Expand Down
4 changes: 4 additions & 0 deletions modules/service/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,7 @@ func ErrNotMatchingReqChainID(codespace sdk.CodespaceType, reqChainID string) sd
func ErrNotTrustee(codespace sdk.CodespaceType, trustee sdk.AccAddress) sdk.Error {
return sdk.NewError(codespace, CodeInvalidInput, fmt.Sprintf("[%s] is not a trustee address", trustee))
}

func ErrNotProfiler(codespace sdk.CodespaceType, profiler sdk.AccAddress) sdk.Error {
return sdk.NewError(codespace, CodeInvalidInput, fmt.Sprintf("[%s] is not a profiler address", profiler))
}
20 changes: 16 additions & 4 deletions modules/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,21 @@ func handleMsgSvcRequest(ctx sdk.Context, k Keeper, msg MsgSvcRequest) sdk.Resul
return ErrMethodNotExists(k.Codespace(), msg.MethodID).Result()
}

if msg.Profiling {
if _, found := k.gk.GetProfiler(ctx, msg.Consumer); !found {
return ErrNotProfiler(k.Codespace(), msg.Consumer).Result()
}
}

//Method id start at 1
if len(bind.Prices) >= int(msg.MethodID) && !msg.ServiceFee.IsAllGTE(sdk.Coins{bind.Prices[msg.MethodID-1]}) {
return ErrLtServiceFee(k.Codespace(), sdk.Coins{bind.Prices[msg.MethodID-1]}).Result()
}

request := NewSvcRequest(msg.DefChainID, msg.DefName, msg.BindChainID, msg.ReqChainID, msg.Consumer, msg.Provider, msg.MethodID, msg.Input, msg.ServiceFee, msg.Profiling)

// request service fee is equal to service binding service fee
if len(bind.Prices) >= int(msg.MethodID) {
// request service fee is equal to service binding service fee if not profiling
if len(bind.Prices) >= int(msg.MethodID) && !msg.Profiling {
request.ServiceFee = sdk.Coins{bind.Prices[msg.MethodID-1]}
} else {
request.ServiceFee = nil
Expand Down Expand Up @@ -199,6 +205,9 @@ func handleMsgSvcResponse(ctx sdk.Context, k Keeper, msg MsgSvcResponse) sdk.Res

resTags := sdk.NewTags(
tags.Action, tags.ActionSvcRespond,
tags.RequestID, []byte(request.RequestID()),
tags.Consumer, []byte(response.Consumer.String()),
tags.Provider, []byte(response.Provider.String()),
)
return sdk.Result{
Tags: resTags,
Expand Down Expand Up @@ -237,7 +246,11 @@ func handleMsgSvcWithdrawTax(ctx sdk.Context, k Keeper, msg MsgSvcWithdrawTax) s
return ErrNotTrustee(k.Codespace(), msg.Trustee).Result()
}
oldTaxPool := k.GetServiceFeeTaxPool(ctx)
newTaxPool := oldTaxPool.Minus(msg.Amount)
newTaxPool, hasNeg := oldTaxPool.SafeMinus(msg.Amount)
if hasNeg {
errMsg := fmt.Sprintf("%s < %s", oldTaxPool, msg.Amount)
return sdk.ErrInsufficientFunds(errMsg).Result()
}
if !newTaxPool.IsNotNegative() {
return sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", oldTaxPool, msg.Amount)).Result()
}
Expand All @@ -249,7 +262,6 @@ func handleMsgSvcWithdrawTax(ctx sdk.Context, k Keeper, msg MsgSvcWithdrawTax) s
k.SetServiceFeeTaxPool(ctx, newTaxPool)
resTags := sdk.NewTags(
tags.Action, tags.ActionSvcWithdrawTax,
tags.Provider, []byte(msg.DestAddress.String()),
)
return sdk.Result{
Tags: resTags,
Expand Down
6 changes: 5 additions & 1 deletion modules/service/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,11 @@ func (k Keeper) AddIncomingFee(ctx sdk.Context, address sdk.AccAddress, coins sd
taxPool = taxPool.Plus(taxFee)
k.SetServiceFeeTaxPool(ctx, taxPool)

incomingFee := coins.Minus(taxFee)
incomingFee, hasNeg := coins.SafeMinus(taxFee)
if hasNeg {
errMsg := fmt.Sprintf("%s < %s", coins, taxFee)
return sdk.ErrInsufficientFunds(errMsg)
}
if !incomingFee.IsNotNegative() {
return sdk.ErrInsufficientCoins(fmt.Sprintf("%s < %s", incomingFee, taxFee))
}
Expand Down
22 changes: 11 additions & 11 deletions modules/service/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewMsgSvcDef(name, chainId, description string, tags []string, author sdk.A
}

func (msg MsgSvcDef) Route() string { return MsgType }
func (msg MsgSvcDef) Type() string { return "service definition" }
func (msg MsgSvcDef) Type() string { return "service_define" }

func (msg MsgSvcDef) GetSignBytes() []byte {
if len(msg.Tags) == 0 {
Expand Down Expand Up @@ -156,7 +156,7 @@ func NewMsgSvcBind(defChainID, defName, bindChainID string, provider sdk.AccAddr
}

func (msg MsgSvcBind) Route() string { return MsgType }
func (msg MsgSvcBind) Type() string { return "service binding" }
func (msg MsgSvcBind) Type() string { return "service_bind" }

func (msg MsgSvcBind) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
Expand Down Expand Up @@ -227,7 +227,7 @@ func NewMsgSvcBindingUpdate(defChainID, defName, bindChainID string, provider sd
}
}
func (msg MsgSvcBindingUpdate) Route() string { return MsgType }
func (msg MsgSvcBindingUpdate) Type() string { return "service binding update" }
func (msg MsgSvcBindingUpdate) Type() string { return "service_binding_update" }

func (msg MsgSvcBindingUpdate) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
Expand Down Expand Up @@ -291,7 +291,7 @@ func NewMsgSvcDisable(defChainID, defName, bindChainID string, provider sdk.AccA
}

func (msg MsgSvcDisable) Route() string { return MsgType }
func (msg MsgSvcDisable) Type() string { return "service disable" }
func (msg MsgSvcDisable) Type() string { return "service_disable" }

func (msg MsgSvcDisable) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
Expand Down Expand Up @@ -343,7 +343,7 @@ func NewMsgSvcEnable(defChainID, defName, bindChainID string, provider sdk.AccAd
}

func (msg MsgSvcEnable) Route() string { return MsgType }
func (msg MsgSvcEnable) Type() string { return "service enable" }
func (msg MsgSvcEnable) Type() string { return "service_enable" }

func (msg MsgSvcEnable) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
Expand Down Expand Up @@ -396,7 +396,7 @@ func NewMsgSvcRefundDeposit(defChainID, defName, bindChainID string, provider sd
}

func (msg MsgSvcRefundDeposit) Route() string { return MsgType }
func (msg MsgSvcRefundDeposit) Type() string { return "service refund deposit" }
func (msg MsgSvcRefundDeposit) Type() string { return "service_refund_deposit" }

func (msg MsgSvcRefundDeposit) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
Expand Down Expand Up @@ -458,7 +458,7 @@ func NewMsgSvcRequest(defChainID, defName, bindChainID, reqChainID string, consu
}

func (msg MsgSvcRequest) Route() string { return MsgType }
func (msg MsgSvcRequest) Type() string { return "service request" }
func (msg MsgSvcRequest) Type() string { return "service_call" }

func (msg MsgSvcRequest) GetSignBytes() []byte {
if len(msg.Input) == 0 {
Expand Down Expand Up @@ -519,7 +519,7 @@ func NewMsgSvcResponse(reqChainID string, requestId string, provider sdk.AccAddr
}

func (msg MsgSvcResponse) Route() string { return MsgType }
func (msg MsgSvcResponse) Type() string { return "service response" }
func (msg MsgSvcResponse) Type() string { return "service_respond" }

func (msg MsgSvcResponse) GetSignBytes() []byte {
if len(msg.Output) == 0 {
Expand Down Expand Up @@ -568,7 +568,7 @@ func NewMsgSvcRefundFees(consumer sdk.AccAddress) MsgSvcRefundFees {
}

func (msg MsgSvcRefundFees) Route() string { return MsgType }
func (msg MsgSvcRefundFees) Type() string { return "service refund fees" }
func (msg MsgSvcRefundFees) Type() string { return "service_refund_fees" }

func (msg MsgSvcRefundFees) GetSignBytes() []byte {
b := msgCdc.MustMarshalJSON(msg)
Expand Down Expand Up @@ -600,7 +600,7 @@ func NewMsgSvcWithdrawFees(provider sdk.AccAddress) MsgSvcWithdrawFees {
}

func (msg MsgSvcWithdrawFees) Route() string { return MsgType }
func (msg MsgSvcWithdrawFees) Type() string { return "service withdraw fees" }
func (msg MsgSvcWithdrawFees) Type() string { return "service_withdraw_fees" }

func (msg MsgSvcWithdrawFees) GetSignBytes() []byte {
b := msgCdc.MustMarshalJSON(msg)
Expand Down Expand Up @@ -636,7 +636,7 @@ func NewMsgSvcWithdrawTax(trustee, destAddress sdk.AccAddress, amount sdk.Coins)
}

func (msg MsgSvcWithdrawTax) Route() string { return MsgType }
func (msg MsgSvcWithdrawTax) Type() string { return "service withdraw fees" }
func (msg MsgSvcWithdrawTax) Type() string { return "service_withdraw_fee_tax" }

func (msg MsgSvcWithdrawTax) GetSignBytes() []byte {
b := msgCdc.MustMarshalJSON(msg)
Expand Down
1 change: 0 additions & 1 deletion modules/service/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ var (
Provider = "provider"
Consumer = "consumer"
RequestID = "request-id"

)
2 changes: 1 addition & 1 deletion modules/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestJailedValidatorDelegations(t *testing.T) {
JailedUntil: time.Unix(0, 0),
MissedBlocksCounter: int64(0),
}
slashingKeeper.setValidatorSigningInfo(ctx, consAddr, newInfo)
slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, newInfo)

// delegate tokens to the validator
delAddr := sdk.AccAddress(addrs[2])
Expand Down
2 changes: 1 addition & 1 deletion modules/slashing/signing_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestGetSetValidatorSigningInfo(t *testing.T) {
JailedUntil: time.Unix(2, 0),
MissedBlocksCounter: int64(10),
}
keeper.setValidatorSigningInfo(ctx, sdk.ConsAddress(addrs[0]), newInfo)
keeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrs[0]), newInfo)
info, found = keeper.getValidatorSigningInfo(ctx, sdk.ConsAddress(addrs[0]))
require.True(t, found)
require.Equal(t, info.StartHeight, int64(4))
Expand Down
22 changes: 16 additions & 6 deletions store/iavlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,14 @@ func testPruning(t *testing.T, numRecent int64, storeEvery int64, states []prune
ver, step, numRecent, storeEvery)
}
for _, ver := range state.deleted {
require.False(t, iavlStore.VersionExists(ver),
"Unpruned version %d with latest version %d. Should prune all but last %d and every %d",
ver, step, numRecent, storeEvery)
if ver == 1 {
require.True(t, iavlStore.VersionExists(ver),
"Version 1 should be kept anyway")
} else {
require.False(t, iavlStore.VersionExists(ver),
"Unpruned version %d with latest version %d. Should prune all but last %d and every %d",
ver, step, numRecent, storeEvery)
}
}
nextVersion(iavlStore)
}
Expand Down Expand Up @@ -364,9 +369,14 @@ func TestIAVLPruneEverything(t *testing.T) {
nextVersion(iavlStore)
for i := 1; i < 100; i++ {
for j := 1; j < i; j++ {
require.False(t, iavlStore.VersionExists(int64(j)),
"Unpruned version %d with latest version %d. Should prune all old versions",
j, i)
if j == 1 {
require.True(t, iavlStore.VersionExists(int64(j)),
"Version 1 should be kept anyway")
} else {
require.False(t, iavlStore.VersionExists(int64(j)),
"Unpruned version %d with latest version %d. Should prune all old versions",
j, i)
}
}
require.True(t, iavlStore.VersionExists(int64(i)),
"Missing current version on step %d, should not prune current state tree",
Expand Down