Skip to content

Commit

Permalink
[[FIX]] init evm exec address with format
Browse files Browse the repository at this point in the history
  • Loading branch information
bysomeone authored and vipwzw committed Mar 16, 2023
1 parent a2f1d80 commit 36e8875
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
13 changes: 7 additions & 6 deletions plugin/dapp/evm/executor/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"fmt"
"math/big"
"os"
"sort"

"reflect"
"sort"

log "github.com/33cn/chain33/common/log/log15"

Expand All @@ -26,9 +25,10 @@ import (

var (
evmDebugInited = false
// EvmAddress 本合约地址
EvmAddress = ""
driverName = evmtypes.ExecutorName
// 执行器地址, 格式由evm配置的地址驱动指定
evmExecAddress = ""
evmExecFormatAddress = ""
driverName = evmtypes.ExecutorName
)

type subConfig struct {
Expand Down Expand Up @@ -67,7 +67,8 @@ func Init(name string, cfg *types.Chain33Config, sub []byte) {
driverName = name
drivers.Register(cfg, driverName, newEVMDriver, enableHeight)
// 格式化为配置地址格式
EvmAddress = common.StringToAddress(address.ExecAddress(cfg.ExecName(name))).String()
evmExecAddress = common.StringToAddress(address.ExecAddress(cfg.ExecName(name))).String()
evmExecFormatAddress = address.ToLower(evmExecAddress)
// 初始化硬分叉数据
state.InitForkData()
InitExecType()
Expand Down
22 changes: 18 additions & 4 deletions plugin/dapp/evm/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"sync/atomic"

"github.com/33cn/chain33/common/address"

"github.com/33cn/chain33/account"
"github.com/ethereum/go-ethereum/params"

Expand Down Expand Up @@ -48,19 +50,20 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, sigType in
cfg := evm.GetAPI().GetConfig()
// 获取当前区块的上下文信息构造EVM上下文
context := evm.NewEVMContext(msg, txHash)
execAddr := evm.getEvmExecAddress()
// 创建EVM运行时对象
env := runtime.NewEVM(context, evm.mStateDB, *evm.vmCfg, cfg)
isCreate := strings.Compare(msg.To().String(), EvmAddress) == 0 && len(msg.Data()) > 0
isTransferOnly := strings.Compare(msg.To().String(), EvmAddress) == 0 && 0 == len(msg.Data())
isCreate := strings.Compare(msg.To().String(), execAddr) == 0 && len(msg.Data()) > 0
isTransferOnly := strings.Compare(msg.To().String(), execAddr) == 0 && 0 == len(msg.Data())
//coins转账,para数据作为备注交易
isTransferNote := strings.Compare(msg.To().String(), EvmAddress) != 0 && !env.StateDB.Exist(msg.To().String()) && len(msg.Para()) > 0 && msg.Value() != 0
isTransferNote := strings.Compare(msg.To().String(), execAddr) != 0 && !env.StateDB.Exist(msg.To().String()) && len(msg.Para()) > 0 && msg.Value() != 0

//加上固有消费的gas
gas, err := intrinsicGas(msg, isCreate, true)
if err != nil {
return nil, err
}
log.Info("innerExec", "isCreate", isCreate, "isTransferOnly", isTransferOnly, "isTransferNote:", isTransferNote, "evmaddr", EvmAddress, "msg.From:", msg.From(), "msg.To", msg.To().String(),
log.Info("innerExec", "isCreate", isCreate, "isTransferOnly", isTransferOnly, "isTransferNote:", isTransferNote, "evmaddr", execAddr, "msg.From:", msg.From(), "msg.To", msg.To().String(),
"data size:", len(msg.Data()), "para size:", len(msg.Para()), "readOnly:", readOnly, "intrinsicGas:", gas, "value:", msg.Value())
if msg.GasLimit() < gas {
return nil, fmt.Errorf("%w: have %d, want %d", model.ErrIntrinsicGas, msg.GasLimit(), gas)
Expand Down Expand Up @@ -356,6 +359,17 @@ func (evm *EVMExecutor) GetTxFee(tx *types.Transaction, index int) int64 {
return fee
}

// 获取evm 执行器地址
func (evm *EVMExecutor) getEvmExecAddress() string {

isFork := evm.GetAPI().GetConfig().IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMAddressInit)
if isFork && address.IsEthAddress(evmExecAddress) {
return evmExecFormatAddress
}

return evmExecAddress
}

func getDataHashKey(addr common.Address) []byte {
return []byte(fmt.Sprintf("mavl-%v-data-hash:%v", evmtypes.ExecutorName, addr))
}
Expand Down
5 changes: 3 additions & 2 deletions plugin/dapp/evm/executor/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ func (evm *EVMExecutor) Query_EstimateGas(req *evmtypes.EstimateEVMGasReq) (type
var lo uint64 = 21000
var hi uint64 = evmtypes.MaxGasLimit
var cap = hi
execAddr := evm.getEvmExecAddress()
// 创建EVM运行时对象
env := runtime.NewEVM(evm.NewEVMContext(msg, tx.Hash()), evm.mStateDB, *evm.vmCfg, evm.GetAPI().GetConfig())
isTransferOnly := strings.Compare(msg.To().String(), EvmAddress) == 0 && 0 == len(msg.Data())
isTransferOnly := strings.Compare(msg.To().String(), execAddr) == 0 && 0 == len(msg.Data())
//coins转账,para数据作为备注交易
isTransferNote := strings.Compare(msg.To().String(), EvmAddress) != 0 && !env.StateDB.Exist(msg.To().String()) && len(msg.Para()) > 0 && msg.Value() != 0
isTransferNote := strings.Compare(msg.To().String(), execAddr) != 0 && !env.StateDB.Exist(msg.To().String()) && len(msg.Para()) > 0 && msg.Value() != 0
//如果是普通转账或者带有备注的Coins 转账 则直接返回
if isTransferOnly || isTransferNote {
result := &evmtypes.EstimateEVMGasResp{}
Expand Down
2 changes: 2 additions & 0 deletions plugin/dapp/evm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
ForkEVMTxGroup = "ForkEVMTxGroup"
// ForkEVMMixAddress evm同时兼容base58及16进制地址混合调用处理
ForkEVMMixAddress = "ForkEVMMixAddress"
// ForkEVMAddressInit 初始化以太坊格式地址
ForkEVMAddressInit = "ForkEVMAddressInit"
)

var (
Expand Down

0 comments on commit 36e8875

Please sign in to comment.