From: oysheng Date: Sat, 21 Apr 2018 07:07:44 +0000 (+0800) Subject: fix build-transaction take exception when amount is nil wrong X-Git-Tag: v1.0.5~62^2~1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=46df9d125902f919d58af4c3d52393dd0b3fda10;p=bytom%2Fbytom.git fix build-transaction take exception when amount is nil wrong fix build-transaction export erorr when amount is 0 --- diff --git a/api/request.go b/api/request.go index 967cf931..171f7d3f 100644 --- a/api/request.go +++ b/api/request.go @@ -13,6 +13,8 @@ import ( var ( errBadActionType = errors.New("bad action type") errBadAction = errors.New("bad action object") + errEmptyAmount = errors.New("nil amount in the request actions") + errBadAmount = errors.New("bad amount in the request actions") ) // BuildRequest is main struct when building transactions diff --git a/api/transact.go b/api/transact.go index 4541761b..d1cfe605 100644 --- a/api/transact.go +++ b/api/transact.go @@ -44,7 +44,7 @@ func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, erro return decoder, true } -func mergeActions(req *BuildRequest) []map[string]interface{} { +func mergeActions(req *BuildRequest) ([]map[string]interface{}, error) { var actions []map[string]interface{} actionMap := make(map[string]map[string]interface{}) @@ -54,13 +54,28 @@ func mergeActions(req *BuildRequest) []map[string]interface{} { continue } - actionKey := m["asset_id"].(string) + m["account_id"].(string) + if m["amount"] == nil { + return nil, errEmptyAmount + } + amountNumber := m["amount"].(json.Number) - amount, _ := amountNumber.Int64() + amount, err := amountNumber.Int64() + if err != nil || amount == 0 { + return nil, errBadAmount + } + actionKey := m["asset_id"].(string) + m["account_id"].(string) if tmpM, ok := actionMap[actionKey]; ok { - tmpNumber, _ := tmpM["amount"].(json.Number) - tmpAmount, _ := tmpNumber.Int64() + if tmpM["amount"] == nil { + return nil, errEmptyAmount + } + + tmpNumber := tmpM["amount"].(json.Number) + tmpAmount, err := tmpNumber.Int64() + if err != nil || tmpAmount == 0 { + return nil, errBadAmount + } + tmpM["amount"] = json.Number(fmt.Sprintf("%v", tmpAmount+amount)) } else { actionMap[actionKey] = m @@ -68,7 +83,7 @@ func mergeActions(req *BuildRequest) []map[string]interface{} { } } - return actions + return actions, nil } func onlyHaveSpendActions(req *BuildRequest) bool { @@ -92,7 +107,11 @@ func (a *API) buildSingle(ctx context.Context, req *BuildRequest) (*txbuilder.Te return nil, errors.New("transaction only contain spend actions, didn't have output actions") } - reqActions := mergeActions(req) + reqActions, err := mergeActions(req) + if err != nil { + return nil, errors.WithDetail(err, "unmarshal json amount error in mergeActions") + } + actions := make([]txbuilder.Action, 0, len(reqActions)) for i, act := range reqActions { typ, ok := act["type"].(string)