Commit 5d367b92 authored by hezhengjun's avatar hezhengjun Committed by 33cn

correct gas estimate

parent 335ef569
......@@ -54,43 +54,32 @@ func (evm *EVMExecutor) Query_CheckAddrExists(in *evmtypes.CheckEVMAddrReq) (typ
}
// Query_EstimateGas 此方法用来估算合约消耗的Gas,不能修改原有执行器的状态数据
func (evm *EVMExecutor) Query_EstimateGas(in *evmtypes.EstimateEVMGasReq) (types.Message, error) {
func (evm *EVMExecutor) Query_EstimateGas(tx *types.Transaction) (types.Message, error) {
evm.CheckInit()
var (
caller evmCommon.Address
)
cfg := evm.GetAPI().GetConfig()
// 如果未指定调用地址,则直接使用一个虚拟的地址发起调用
if len(in.Caller) > 0 {
callAddr := evmCommon.StringToAddress(in.Caller)
if callAddr != nil {
caller = *callAddr
}
} else {
caller = evmCommon.ExecAddress(cfg.ExecName(evmtypes.ExecutorName))
}
to := evmCommon.StringToAddress(in.To)
if to == nil {
to = evmCommon.StringToAddress(EvmAddress)
index := 0
msg, err := evm.GetMessage(tx, index)
if err != nil {
return nil, err
}
msg := evmCommon.NewMessage(caller, to, 0, in.Amount, evmtypes.MaxGasLimit, 1, nil, in.Para, "estimateGas")
txHash := evmCommon.BigToHash(big.NewInt(evmtypes.MaxGasLimit)).Bytes()
receipt, err := evm.innerExec(msg, txHash, 1, evmtypes.MaxGasLimit, false)
msg.SetGasLimit(evmtypes.MaxGasLimit)
receipt, err := evm.innerExec(msg, tx.Hash(), index, evmtypes.MaxGasLimit, true)
if err != nil {
return nil, err
}
if receipt.Ty == types.ExecOk {
callData := getCallReceipt(receipt.GetLogs())
if callData != nil {
result := &evmtypes.EstimateEVMGasResp{}
result.Gas = callData.UsedGas
return result, nil
}
if receipt.Ty != types.ExecOk {
return nil, errors.New("contract call error")
}
callData := getCallReceipt(receipt.GetLogs())
if callData == nil {
return nil, errors.New("nil receipt")
}
return nil, errors.New("contract call error")
result := &evmtypes.EstimateEVMGasResp{}
result.Gas = callData.UsedGas
return result, nil
}
// 从日志中查找调用结果
......
......@@ -54,6 +54,11 @@ func (m Message) Data() []byte { return m.data }
// GasLimit Gas限制
func (m Message) GasLimit() uint64 { return m.gasLimit }
// GasLimit Gas限制
func (m Message) SetGasLimit(gasLimit uint64) {
m.gasLimit = gasLimit
}
// Alias 合约别名
func (m Message) Alias() string { return m.alias }
......
......@@ -27,8 +27,8 @@ const (
// TyLogEVMEventData 合约生成新的event日志数据
TyLogEVMEventData = 605
// MaxGasLimit 最大Gas消耗上限
MaxGasLimit = 10000000
// MaxGasLimit 最大Gas消耗上限 100
MaxGasLimit = (100000000 * 100)
)
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