Commit 68ace11b authored by Litian's avatar Litian

修改命令行调用bug,以及evm合约bug

parent 6c094edc
......@@ -437,7 +437,7 @@ func getAbi(cmd *cobra.Command, args []string) {
var req = evmtypes.EvmQueryAbiReq{Address: addr}
var resp evmtypes.EvmQueryAbiResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "QueryABI", req, &resp)
query := sendQuery(rpcLaddr, "QueryABI", &req, &resp)
if query {
fmt.Fprintln(os.Stdout, resp.Abi)
......@@ -470,7 +470,7 @@ func callAbi(cmd *cobra.Command, args []string) {
var req = evmtypes.EvmQueryReq{Address: addr, Input: input, Caller: caller}
var resp evmtypes.EvmQueryResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "Query", req, &resp)
query := sendQuery(rpcLaddr, "Query", &req, &resp)
if query {
data, err := json.MarshalIndent(&resp, "", " ")
......@@ -503,7 +503,7 @@ func estimateContract(cmd *cobra.Command, args []string) {
var estGasReq = evmtypes.EstimateEVMGasReq{To: toAddr, Code: bCode, Caller: caller, Amount: amountInt64}
var estGasResp evmtypes.EstimateEVMGasResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "EstimateGas", estGasReq, &estGasResp)
query := sendQuery(rpcLaddr, "EstimateGas", &estGasReq, &estGasResp)
if query {
fmt.Fprintf(os.Stdout, "gas cost estimate %v\n", estGasResp.Gas)
......@@ -568,7 +568,7 @@ func checkContractAddr(cmd *cobra.Command, args []string) {
var checkAddrReq = evmtypes.CheckEVMAddrReq{Addr: toAddr}
var checkAddrResp evmtypes.CheckEVMAddrResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "CheckAddrExists", checkAddrReq, &checkAddrResp)
query := sendQuery(rpcLaddr, "CheckAddrExists", &checkAddrReq, &checkAddrResp)
if query && checkAddrResp.Contract {
proto.MarshalText(os.Stdout, &checkAddrResp)
......@@ -628,7 +628,7 @@ func evmDebugRPC(cmd *cobra.Command, flag int32) {
var debugReq = evmtypes.EvmDebugReq{Optype: flag}
var debugResp evmtypes.EvmDebugResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "EvmDebug", debugReq, &debugResp)
query := sendQuery(rpcLaddr, "EvmDebug", &debugReq, &debugResp)
if query {
proto.MarshalText(os.Stdout, &debugResp)
......@@ -733,11 +733,16 @@ func evmWithdraw(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
func sendQuery(rpcAddr, funcName string, request interface{}, result proto.Message) bool {
params := types.Query4Cli{
func sendQuery(rpcAddr, funcName string, request, result proto.Message) bool {
js, err := types.PBToJSON(request)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return false
}
params := rpctypes.Query4Jrpc{
Execer: "evm",
FuncName: funcName,
Payload: request,
Payload: js,
}
jsonrpc, err := jsonclient.NewJSONClient(rpcAddr)
......
package commands
import (
"testing"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util/testnode"
"github.com/stretchr/testify/assert"
// 因为测试程序在外层,而合约类型的初始化在里面,所以需要显示引用,否则不会加载合约插件
_ "github.com/33cn/plugin/plugin/dapp/evm/executor"
evmtypes "github.com/33cn/plugin/plugin/dapp/evm/types"
// 需要显示引用系统插件,以加载系统内置合约
"github.com/33cn/chain33/client/mocks"
_ "github.com/33cn/chain33/system"
)
// TestQueryDebug 测试命令行调用rpc接口
func TestQueryDebug(t *testing.T) {
var debugReq = evmtypes.EvmDebugReq{Optype: 1}
js, err := types.PBToJSON(&debugReq)
assert.Nil(t, err)
in := &rpctypes.Query4Jrpc{
Execer: "evm",
FuncName: "EvmDebug",
Payload: js,
}
var mockResp = evmtypes.EvmDebugResp{DebugStatus: "on"}
mockapi := &mocks.QueueProtocolAPI{}
// 这里对需要mock的方法打桩,Close是必须的,其它方法根据需要
mockapi.On("Close").Return()
mockapi.On("Query", "evm", "EvmDebug", &debugReq).Return(&mockResp, nil)
mock33 := testnode.New("", mockapi)
defer mock33.Close()
rpcCfg := mock33.GetCfg().RPC
// 这里必须设置监听端口,默认的是无效值
rpcCfg.JrpcBindAddr = "127.0.0.1:8899"
mock33.GetRPC().Listen()
jsonClient, err := jsonclient.NewJSONClient("http://" + rpcCfg.JrpcBindAddr + "/")
assert.Nil(t, err)
assert.NotNil(t, jsonClient)
var debugResp evmtypes.EvmDebugResp
err = jsonClient.Call("Chain33.Query", in, &debugResp)
assert.Nil(t, err)
assert.Equal(t, "on", debugResp.DebugStatus)
}
......@@ -67,7 +67,11 @@ func (evm *EVMExecutor) Query_EstimateGas(in *evmtypes.EstimateEVMGasReq) (types
caller = common.ExecAddress(types.ExecName(evmtypes.ExecutorName))
}
msg := common.NewMessage(caller, nil, 0, in.Amount, evmtypes.MaxGasLimit, 1, in.Code, "estimateGas", in.Abi)
to := common.StringToAddress(in.To)
if to == nil {
to = common.StringToAddress(EvmAddress)
}
msg := common.NewMessage(caller, to, 0, in.Amount, evmtypes.MaxGasLimit, 1, in.Code, "estimateGas", in.Abi)
txHash := common.BigToHash(big.NewInt(evmtypes.MaxGasLimit)).Bytes()
receipt, err := evm.innerExec(msg, txHash, 1, evmtypes.MaxGasLimit, false)
......
......@@ -137,12 +137,12 @@ func createEvmTx(param *CreateCallTx) (*types.Transaction, error) {
}
if param.IsCreate {
return createRawTx(action, "")
return createRawTx(action, "", param.Fee)
}
return createRawTx(action, param.Name)
return createRawTx(action, param.Name, param.Fee)
}
func createRawTx(action *EVMContractAction, name string) (*types.Transaction, error) {
func createRawTx(action *EVMContractAction, name string, fee int64) (*types.Transaction, error) {
tx := &types.Transaction{}
if len(name) == 0 {
tx = &types.Transaction{
......@@ -161,5 +161,10 @@ func createRawTx(action *EVMContractAction, name string) (*types.Transaction, er
if err != nil {
return nil, err
}
if tx.Fee < fee {
tx.Fee = fee
}
return tx, nil
}
......@@ -5,8 +5,9 @@ package types
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
proto "github.com/golang/protobuf/proto"
)
// Reference imports to suppress errors if they are not otherwise used.
......
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