Commit 15c55973 authored by hezhengjun's avatar hezhengjun Committed by 33cn

simply evm gas estimate procedure

parent 9a2b1688
......@@ -23,25 +23,13 @@ gas=0
evm_SignTxAndEstimate() {
gas=0
local txHex="$1"
local priKey="$2"
local from=$2
local MAIN_HTTP=$3
local expire="120s"
if [ -n "$4" ]; then
expire=$4
fi
local req='"method":"Chain33.SignRawTx","params":[{"privkey":"'"$priKey"'","txHex":"'"$txHex"'","expire":"'"$expire"'"}]'
signedTx=$(curl -ksd "{$req}" "${MAIN_HTTP}" | jq -r ".result")
if [ "$signedTx" != null ]; then
req='{"method":"Chain33.Query","params":[{"execer":"evm","funcName":"EstimateGas","payload":{"data":"'${signedTx}'"}}]}'
chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "EstimateGas" ".result.gas"
gas=$((RETURN_RESP + 10000))
echo "the estimate gas is = ${gas}"
else
echo "signedTx null error"
fi
req='{"method":"Chain33.Query","params":[{"execer":"evm","funcName":"EstimateGas","payload":{"tx":"'${txHex}'", "from":"'${from}'"}}]}'
chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "EstimateGas" ".result.gas"
gas=$((RETURN_RESP + 10000))
echo "the estimate gas is = ${gas}"
}
#上述未签名交易使用以下指令进行创建
......@@ -49,7 +37,7 @@ evm_SignTxAndEstimate() {
function evm_createContract() {
expire="120s"
tx=$(curl -ksd '{"method":"evm.CreateDeployTx","params":[{"code":"'${erc20_code}'", "abi":"'"${erc20_abi}"'", "note": "deploy erc20", "alias": "zbc", "parameter": "constructor(zbc, zbc, 3300, '${evm_creatorAddr}')", "expire":"'${expire}'", "paraName":"'"${paraName}"'", "amount":0}]}' "${MAIN_HTTP}" | jq -r ".result")
evm_SignTxAndEstimate "${tx}" "${evm_creatorAddr_key}" "$MAIN_HTTP" "${expire}"
evm_SignTxAndEstimate "${tx}" "${evm_creatorAddr}" "$MAIN_HTTP"
echo "evm_createContract :: the estimate gas is = ${gas}"
tx=$(curl -ksd '{"method":"evm.CreateDeployTx","params":[{"code":"'${erc20_code}'", "abi":"'"${erc20_abi}"'", "fee":'${gas}', "note": "deploy erc20", "alias": "zbc", "parameter": "constructor(zbc, zbc, 3300, '${evm_creatorAddr}')", "expire":"'${expire}'", "paraName":"'"${paraName}"'", "amount":0}]}' "${MAIN_HTTP}" | jq -r ".result")
......@@ -96,7 +84,7 @@ function evm_transfer() {
expire="120s"
tx=$(curl -ksd '{"method":"evm.CreateCallTx","params":[{"abi":"'"${erc20_abi}"'", "note": "evm transfer rpc test", "parameter": "transfer('${evm_transferAddr}', 20)", "expire":"'${expire}'", "contractAddr":"'"${evm_contractAddr}"'", "paraName":"'"${paraName}"'"}]}' "${MAIN_HTTP}" | jq -r ".result")
evm_SignTxAndEstimate "${tx}" "${evm_creatorAddr_key}" "$MAIN_HTTP" "${expire}"
evm_SignTxAndEstimate "${tx}" "${evm_creatorAddr}" "$MAIN_HTTP"
# shellcheck disable=SC2090
tx=$(curl -ksd '{"method":"evm.CreateCallTx","params":[{"abi":"'"${erc20_abi}"'", "fee":'${gas}', "note": "evm transfer rpc test", "parameter": "transfer('${evm_transferAddr}', 20)", "expire":"'${expire}'", "contractAddr":"'"${evm_contractAddr}"'", "paraName":"'"${paraName}"'"}]}' "${MAIN_HTTP}" | jq -r ".result")
......
......@@ -25,7 +25,7 @@ import (
func (evm *EVMExecutor) Exec(tx *types.Transaction, index int) (*types.Receipt, error) {
evm.CheckInit()
// 先转换消息
msg, err := evm.GetMessage(tx, index)
msg, err := evm.GetMessage(tx, index, nil)
if err != nil {
return nil, err
}
......@@ -190,14 +190,20 @@ func (evm *EVMExecutor) CheckInit() {
}
// GetMessage 目前的交易中,如果是coins交易,金额是放在payload的,但是合约不行,需要修改Transaction结构
func (evm *EVMExecutor) GetMessage(tx *types.Transaction, index int) (msg *common.Message, err error) {
func (evm *EVMExecutor) GetMessage(tx *types.Transaction, index int, fromPtr *common.Address) (msg *common.Message, err error) {
var action evmtypes.EVMContractAction
err = types.Decode(tx.Payload, &action)
if err != nil {
return msg, err
}
// 此处暂时不考虑消息发送签名的处理,chain33在mempool中对签名做了检查
from := getCaller(tx)
var from common.Address
if fromPtr == nil {
from = getCaller(tx)
} else {
from = *fromPtr
}
to := getReceiver(&action)
if to == nil {
return msg, types.ErrInvalidAddress
......
......@@ -55,10 +55,10 @@ func (evm *EVMExecutor) Query_CheckAddrExists(in *evmtypes.CheckEVMAddrReq) (typ
}
// Query_EstimateGas 此方法用来估算合约消耗的Gas,不能修改原有执行器的状态数据
func (evm *EVMExecutor) Query_EstimateGas(txInfo *types.ReqString) (types.Message, error) {
func (evm *EVMExecutor) Query_EstimateGas(req *evmtypes.EstimateEVMGasReq) (types.Message, error) {
evm.CheckInit()
txBytes, err := hex.DecodeString(txInfo.Data)
txBytes, err := hex.DecodeString(req.Tx)
if nil != err {
return nil, err
}
......@@ -68,12 +68,9 @@ func (evm *EVMExecutor) Query_EstimateGas(txInfo *types.ReqString) (types.Messag
return nil, err
}
if nil == tx.Signature {
return nil, errors.New("Tx should be signatured")
}
index := 0
msg, err := evm.GetMessage(&tx, index)
from := evmCommon.StringToAddress(req.From)
msg, err := evm.GetMessage(&tx, index, from)
if err != nil {
return nil, err
}
......
......@@ -108,10 +108,8 @@ message CheckEVMAddrResp {
}
message EstimateEVMGasReq {
string to = 1;
bytes para = 2;
string caller = 3;
uint64 amount = 4;
string tx = 1;
string from = 2;
}
message EstimateEVMGasResp {
uint64 gas = 1;
......
This diff is collapsed.
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