Commit 68ace11b authored by Litian's avatar Litian

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

parent 6c094edc
...@@ -437,7 +437,7 @@ func getAbi(cmd *cobra.Command, args []string) { ...@@ -437,7 +437,7 @@ func getAbi(cmd *cobra.Command, args []string) {
var req = evmtypes.EvmQueryAbiReq{Address: addr} var req = evmtypes.EvmQueryAbiReq{Address: addr}
var resp evmtypes.EvmQueryAbiResp var resp evmtypes.EvmQueryAbiResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "QueryABI", req, &resp) query := sendQuery(rpcLaddr, "QueryABI", &req, &resp)
if query { if query {
fmt.Fprintln(os.Stdout, resp.Abi) fmt.Fprintln(os.Stdout, resp.Abi)
...@@ -470,7 +470,7 @@ func callAbi(cmd *cobra.Command, args []string) { ...@@ -470,7 +470,7 @@ func callAbi(cmd *cobra.Command, args []string) {
var req = evmtypes.EvmQueryReq{Address: addr, Input: input, Caller: caller} var req = evmtypes.EvmQueryReq{Address: addr, Input: input, Caller: caller}
var resp evmtypes.EvmQueryResp var resp evmtypes.EvmQueryResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "Query", req, &resp) query := sendQuery(rpcLaddr, "Query", &req, &resp)
if query { if query {
data, err := json.MarshalIndent(&resp, "", " ") data, err := json.MarshalIndent(&resp, "", " ")
...@@ -503,7 +503,7 @@ func estimateContract(cmd *cobra.Command, args []string) { ...@@ -503,7 +503,7 @@ func estimateContract(cmd *cobra.Command, args []string) {
var estGasReq = evmtypes.EstimateEVMGasReq{To: toAddr, Code: bCode, Caller: caller, Amount: amountInt64} var estGasReq = evmtypes.EstimateEVMGasReq{To: toAddr, Code: bCode, Caller: caller, Amount: amountInt64}
var estGasResp evmtypes.EstimateEVMGasResp var estGasResp evmtypes.EstimateEVMGasResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "EstimateGas", estGasReq, &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)
...@@ -568,7 +568,7 @@ func checkContractAddr(cmd *cobra.Command, args []string) { ...@@ -568,7 +568,7 @@ func checkContractAddr(cmd *cobra.Command, args []string) {
var checkAddrReq = evmtypes.CheckEVMAddrReq{Addr: toAddr} var checkAddrReq = evmtypes.CheckEVMAddrReq{Addr: toAddr}
var checkAddrResp evmtypes.CheckEVMAddrResp var checkAddrResp evmtypes.CheckEVMAddrResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "CheckAddrExists", checkAddrReq, &checkAddrResp) query := sendQuery(rpcLaddr, "CheckAddrExists", &checkAddrReq, &checkAddrResp)
if query && checkAddrResp.Contract { if query && checkAddrResp.Contract {
proto.MarshalText(os.Stdout, &checkAddrResp) proto.MarshalText(os.Stdout, &checkAddrResp)
...@@ -628,7 +628,7 @@ func evmDebugRPC(cmd *cobra.Command, flag int32) { ...@@ -628,7 +628,7 @@ func evmDebugRPC(cmd *cobra.Command, flag int32) {
var debugReq = evmtypes.EvmDebugReq{Optype: flag} var debugReq = evmtypes.EvmDebugReq{Optype: flag}
var debugResp evmtypes.EvmDebugResp var debugResp evmtypes.EvmDebugResp
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr") rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := sendQuery(rpcLaddr, "EvmDebug", debugReq, &debugResp) query := sendQuery(rpcLaddr, "EvmDebug", &debugReq, &debugResp)
if query { if query {
proto.MarshalText(os.Stdout, &debugResp) proto.MarshalText(os.Stdout, &debugResp)
...@@ -733,11 +733,16 @@ func evmWithdraw(cmd *cobra.Command, args []string) { ...@@ -733,11 +733,16 @@ func evmWithdraw(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal() ctx.RunWithoutMarshal()
} }
func sendQuery(rpcAddr, funcName string, request interface{}, result proto.Message) bool { func sendQuery(rpcAddr, funcName string, request, result proto.Message) bool {
params := types.Query4Cli{ js, err := types.PBToJSON(request)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return false
}
params := rpctypes.Query4Jrpc{
Execer: "evm", Execer: "evm",
FuncName: funcName, FuncName: funcName,
Payload: request, Payload: js,
} }
jsonrpc, err := jsonclient.NewJSONClient(rpcAddr) 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 ...@@ -67,7 +67,11 @@ func (evm *EVMExecutor) Query_EstimateGas(in *evmtypes.EstimateEVMGasReq) (types
caller = common.ExecAddress(types.ExecName(evmtypes.ExecutorName)) 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() txHash := common.BigToHash(big.NewInt(evmtypes.MaxGasLimit)).Bytes()
receipt, err := evm.innerExec(msg, txHash, 1, evmtypes.MaxGasLimit, false) receipt, err := evm.innerExec(msg, txHash, 1, evmtypes.MaxGasLimit, false)
......
...@@ -137,12 +137,12 @@ func createEvmTx(param *CreateCallTx) (*types.Transaction, error) { ...@@ -137,12 +137,12 @@ func createEvmTx(param *CreateCallTx) (*types.Transaction, error) {
} }
if param.IsCreate { 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{} tx := &types.Transaction{}
if len(name) == 0 { if len(name) == 0 {
tx = &types.Transaction{ tx = &types.Transaction{
...@@ -161,5 +161,10 @@ func createRawTx(action *EVMContractAction, name string) (*types.Transaction, er ...@@ -161,5 +161,10 @@ func createRawTx(action *EVMContractAction, name string) (*types.Transaction, er
if err != nil { if err != nil {
return nil, err return nil, err
} }
if tx.Fee < fee {
tx.Fee = fee
}
return tx, nil return tx, nil
} }
...@@ -5,8 +5,9 @@ package types ...@@ -5,8 +5,9 @@ package types
import ( import (
fmt "fmt" fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math" math "math"
proto "github.com/golang/protobuf/proto"
) )
// Reference imports to suppress errors if they are not otherwise used. // 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