Commit 465913bc authored by hezhengjun's avatar hezhengjun Committed by 33cn

correct gas estimate for command

parent 5d367b92
...@@ -42,7 +42,7 @@ func EvmCmd() *cobra.Command { ...@@ -42,7 +42,7 @@ func EvmCmd() *cobra.Command {
createContractCmd(), createContractCmd(),
callContractCmd(), callContractCmd(),
abiCmd(), abiCmd(),
estimateContractCmd(), estimateGasCmd(),
checkContractAddrCmd(), checkContractAddrCmd(),
evmDebugCmd(), evmDebugCmd(),
evmTransferCmd(), evmTransferCmd(),
...@@ -283,7 +283,6 @@ func createContract(cmd *cobra.Command, args []string) { ...@@ -283,7 +283,6 @@ func createContract(cmd *cobra.Command, args []string) {
tx := &types.Transaction{Execer: []byte(exector), Payload: types.Encode(&action), Fee: 0, To: action.ContractAddr, ChainID: chainID} tx := &types.Transaction{Execer: []byte(exector), Payload: types.Encode(&action), Fee: 0, To: action.ContractAddr, ChainID: chainID}
tx.Fee, _ = tx.GetRealFee(cfg.GetMinTxFeeRate()) tx.Fee, _ = tx.GetRealFee(cfg.GetMinTxFeeRate())
fmt.Println("feeInt64 is", feeInt64)
if tx.Fee < int64(feeInt64) { if tx.Fee < int64(feeInt64) {
tx.Fee += int64(feeInt64) tx.Fee += int64(feeInt64)
} }
...@@ -499,38 +498,15 @@ func callAbi(cmd *cobra.Command, args []string) { ...@@ -499,38 +498,15 @@ func callAbi(cmd *cobra.Command, args []string) {
} }
} }
func estimateContract(cmd *cobra.Command, args []string) { func estimateGas(cmd *cobra.Command, args []string) {
input, _ := cmd.Flags().GetString("input") txStr, _ := cmd.Flags().GetString("tx")
name, _ := cmd.Flags().GetString("exec") rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
caller, _ := cmd.Flags().GetString("caller") txInfo := &types.ReqString{
amount, _ := cmd.Flags().GetFloat64("amount") Data: txStr,
path, _ := cmd.Flags().GetString("path")
toAddr := address.ExecAddress("evm")
if len(name) > 0 {
toAddr = address.ExecAddress(name)
}
amountInt64 := uint64(amount*1e4) * 1e4
abiFileName := path + name + ".abi"
abiStr, err := readFile(abiFileName)
if nil != err {
_, _ = fmt.Fprintln(os.Stderr, "Can't read abi info, Pls set correct abi path and provide abi file as", abiFileName)
return
}
_, packedParameter, err := evmAbi.Pack(input, abiStr, false)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "Failed to do para pack", err.Error())
return
} }
var estGasReq = evmtypes.EstimateEVMGasReq{To: toAddr, Para: packedParameter, Caller: caller, Amount: amountInt64}
var estGasResp evmtypes.EstimateEVMGasResp var estGasResp evmtypes.EstimateEVMGasResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") query := sendQuery(rpcLaddr, "EstimateGas", txInfo, &estGasResp)
query := sendQuery(rpcLaddr, "EstimateGas", &estGasReq, &estGasResp)
if query { if query {
fmt.Fprintf(os.Stdout, "gas cost estimate %v\n", estGasResp.Gas) fmt.Fprintf(os.Stdout, "gas cost estimate %v\n", estGasResp.Gas)
} else { } else {
...@@ -538,27 +514,19 @@ func estimateContract(cmd *cobra.Command, args []string) { ...@@ -538,27 +514,19 @@ func estimateContract(cmd *cobra.Command, args []string) {
} }
} }
func addEstimateFlags(cmd *cobra.Command) { func addEstimateGasFlags(cmd *cobra.Command) {
cmd.Flags().StringP("input", "i", "", "input contract binary code") cmd.Flags().StringP("tx", "x", "", "tx string(should be signatured)")
cmd.MarkFlagRequired("input") _ = cmd.MarkFlagRequired("tx")
cmd.Flags().StringP("exec", "e", "", "evm contract name (like user.evm.xxxxx)")
cmd.Flags().StringP("caller", "c", "", "the caller address")
cmd.Flags().StringP("path", "t", "./", "abi path(optional), default to .(current directory)")
cmd.Flags().Float64P("amount", "a", 0, "the amount transfer to the contract (optional)")
} }
// 估算合约消耗 // 估算合约消耗
func estimateContractCmd() *cobra.Command { func estimateGasCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "estimate", Use: "estimate",
Short: "Estimate the gas cost of calling or creating a contract", Short: "Estimate the gas cost of calling or creating a contract",
Run: estimateContract, Run: estimateGas,
} }
addEstimateFlags(cmd) addEstimateGasFlags(cmd)
return cmd return cmd
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package executor package executor
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
...@@ -54,11 +55,25 @@ func (evm *EVMExecutor) Query_CheckAddrExists(in *evmtypes.CheckEVMAddrReq) (typ ...@@ -54,11 +55,25 @@ func (evm *EVMExecutor) Query_CheckAddrExists(in *evmtypes.CheckEVMAddrReq) (typ
} }
// Query_EstimateGas 此方法用来估算合约消耗的Gas,不能修改原有执行器的状态数据 // Query_EstimateGas 此方法用来估算合约消耗的Gas,不能修改原有执行器的状态数据
func (evm *EVMExecutor) Query_EstimateGas(tx *types.Transaction) (types.Message, error) { func (evm *EVMExecutor) Query_EstimateGas(txInfo *types.ReqString) (types.Message, error) {
evm.CheckInit() evm.CheckInit()
txBytes, err := hex.DecodeString(txInfo.Data)
if nil != err {
return nil, err
}
var tx types.Transaction
err = types.Decode(txBytes, &tx)
if nil != err {
return nil, err
}
if nil == tx.Signature {
return nil, errors.New("Tx should be signatured")
}
index := 0 index := 0
msg, err := evm.GetMessage(tx, index) msg, err := evm.GetMessage(&tx, index)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -27,8 +27,8 @@ const ( ...@@ -27,8 +27,8 @@ const (
// TyLogEVMEventData 合约生成新的event日志数据 // TyLogEVMEventData 合约生成新的event日志数据
TyLogEVMEventData = 605 TyLogEVMEventData = 605
// MaxGasLimit 最大Gas消耗上限 100 // MaxGasLimit 最大Gas消耗上限 5
MaxGasLimit = (100000000 * 100) MaxGasLimit = (100000000 * 5)
) )
const ( const (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment