OSDN Git Service

Remove /sign-submit-transaction api (#692)
[bytom/bytom.git] / api / transact.go
index 8fc11b1..1cab954 100644 (file)
@@ -10,7 +10,9 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/bytom/blockchain/txbuilder"
+       "github.com/bytom/consensus"
        "github.com/bytom/errors"
+       "github.com/bytom/math/checked"
        "github.com/bytom/net/http/reqid"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/bc/types"
@@ -42,7 +44,7 @@ func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, erro
 }
 
 func mergeActions(req *BuildRequest) []map[string]interface{} {
-       actions := make([]map[string]interface{}, 0)
+       var actions []map[string]interface{}
        actionMap := make(map[string]map[string]interface{})
 
        for _, m := range req.Actions {
@@ -152,18 +154,6 @@ func (a *API) build(ctx context.Context, buildReqs *BuildRequest) Response {
        return NewSuccessResponse(tmpl)
 }
 
-func (a *API) submitSingle(ctx context.Context, tpl *txbuilder.Template) (map[string]string, error) {
-       if tpl.Transaction == nil {
-               return nil, errors.Wrap(txbuilder.ErrMissingRawTx)
-       }
-
-       if err := txbuilder.FinalizeTx(ctx, a.chain, tpl.Transaction); err != nil {
-               return nil, errors.Wrapf(err, "tx %s", tpl.Transaction.ID.String())
-       }
-
-       return map[string]string{"tx_id": tpl.Transaction.ID.String()}, nil
-}
-
 type submitTxResp struct {
        TxID *bc.Hash `json:"tx_id"`
 }
@@ -180,23 +170,37 @@ func (a *API) submit(ctx context.Context, ins struct {
        return NewSuccessResponse(&submitTxResp{TxID: &ins.Tx.ID})
 }
 
-// POST /sign-submit-transaction
-func (a *API) signSubmit(ctx context.Context, x struct {
-       Password []string           `json:"password"`
-       Txs      txbuilder.Template `json:"transaction"`
+type EstimateTxGasResp struct {
+       LeftBTM     int64 `json:"left_btm"`
+       ConsumedBTM int64 `json:"consumed_btm"`
+       LeftGas     int64 `json:"left_gas"`
+       ConsumedGas int64 `json:"consumed_gas"`
+       StorageGas  int64 `json:"storage_gas"`
+       VMGas       int64 `json:"vm_gas"`
+}
+
+// POST /estimate-transaction-gas
+func (a *API) estimateTxGas(ctx context.Context, ins struct {
+       Tx types.Tx `json:"raw_transaction"`
 }) Response {
-       if err := txbuilder.Sign(ctx, &x.Txs, nil, x.Password[0], a.pseudohsmSignTemplate); err != nil {
-               log.WithField("build err", err).Error("fail on sign transaction.")
+       gasState, err := txbuilder.EstimateTxGas(a.chain, &ins.Tx)
+       if err != nil {
                return NewErrorResponse(err)
        }
-       log.Info("Sign Transaction complete.")
 
-       txID, err := a.submitSingle(nil, &x.Txs)
-       if err != nil {
-               log.WithField("err", err).Error("submit single tx")
-               return NewErrorResponse(err)
+       btmLeft, ok := checked.MulInt64(gasState.GasLeft, consensus.VMGasRate)
+       if !ok {
+               return NewErrorResponse(errors.New("calculate btmleft got a math error"))
+       }
+
+       txGasResp := &EstimateTxGasResp{
+               LeftBTM:     btmLeft,
+               ConsumedBTM: int64(gasState.BTMValue) - btmLeft,
+               LeftGas:     gasState.GasLeft,
+               ConsumedGas: gasState.GasUsed,
+               StorageGas:  gasState.StorageGas,
+               VMGas:       gasState.GasUsed - gasState.StorageGas,
        }
 
-       log.WithField("tx_id", txID["tx_id"]).Info("submit single tx")
-       return NewSuccessResponse(txID)
+       return NewSuccessResponse(txGasResp)
 }