Commit 942e1c65 authored by Litian's avatar Litian

evm支持abi功能合入

parent 341c8d26
...@@ -38,7 +38,7 @@ func init() { ...@@ -38,7 +38,7 @@ func init() {
// Init 初始化本合约对象 // Init 初始化本合约对象
func Init(name string, sub []byte) { func Init(name string, sub []byte) {
driverName = name driverName = name
drivers.Register(driverName, newEVMDriver, types.GetDappFork(driverName, "Enable")) drivers.Register(driverName, newEVMDriver, types.GetDappFork(driverName, evmtypes.EVMEnable))
EvmAddress = address.ExecAddress(types.ExecName(name)) EvmAddress = address.ExecAddress(types.ExecName(name))
// 初始化硬分叉数据 // 初始化硬分叉数据
state.InitForkData() state.InitForkData()
......
...@@ -67,7 +67,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int, ...@@ -67,7 +67,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
if isCreate { if isCreate {
// 如果携带ABI数据,则对数据合法性进行检查 // 如果携带ABI数据,则对数据合法性进行检查
if len(msg.ABI()) > 0 && types.IsDappFork(evm.GetHeight(), "evm", "ForkEVMABI") { if len(msg.ABI()) > 0 && types.IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMABI) {
_, err = abi.JSON(strings.NewReader(msg.ABI())) _, err = abi.JSON(strings.NewReader(msg.ABI()))
if err != nil { if err != nil {
return receipt, err return receipt, err
...@@ -77,7 +77,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int, ...@@ -77,7 +77,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
} else { } else {
inData := msg.Data() inData := msg.Data()
// 在这里进行ABI和十六进制的调用参数转换 // 在这里进行ABI和十六进制的调用参数转换
if len(msg.ABI()) > 0 && types.IsDappFork(evm.GetHeight(), "evm", "ForkEVMABI") { if len(msg.ABI()) > 0 && types.IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMABI) {
funcName, packData, err := abi.Pack(msg.ABI(), evm.mStateDB.GetAbi(msg.To().String()), readOnly) funcName, packData, err := abi.Pack(msg.ABI(), evm.mStateDB.GetAbi(msg.To().String()), readOnly)
if err != nil { if err != nil {
return receipt, err return receipt, err
...@@ -126,7 +126,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int, ...@@ -126,7 +126,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
kvSet, logs := evm.mStateDB.GetChangedData(curVer.GetID()) kvSet, logs := evm.mStateDB.GetChangedData(curVer.GetID())
contractReceipt := &evmtypes.ReceiptEVMContract{Caller: msg.From().String(), ContractName: execName, ContractAddr: contractAddr.String(), UsedGas: usedGas, Ret: ret} contractReceipt := &evmtypes.ReceiptEVMContract{Caller: msg.From().String(), ContractName: execName, ContractAddr: contractAddr.String(), UsedGas: usedGas, Ret: ret}
// 这里进行ABI调用结果格式化 // 这里进行ABI调用结果格式化
if len(methodName) > 0 && len(msg.ABI()) > 0 && types.IsDappFork(evm.GetHeight(), "evm", "ForkEVMABI") { if len(methodName) > 0 && len(msg.ABI()) > 0 && types.IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMABI) {
jsonRet, err := abi.Unpack(ret, methodName, evm.mStateDB.GetAbi(msg.To().String())) jsonRet, err := abi.Unpack(ret, methodName, evm.mStateDB.GetAbi(msg.To().String()))
if err != nil { if err != nil {
// 这里出错不影响整体执行,只打印错误信息 // 这里出错不影响整体执行,只打印错误信息
...@@ -137,7 +137,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int, ...@@ -137,7 +137,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
logs = append(logs, &types.ReceiptLog{Ty: evmtypes.TyLogCallContract, Log: types.Encode(contractReceipt)}) logs = append(logs, &types.ReceiptLog{Ty: evmtypes.TyLogCallContract, Log: types.Encode(contractReceipt)})
logs = append(logs, evm.mStateDB.GetReceiptLogs(contractAddr.String())...) logs = append(logs, evm.mStateDB.GetReceiptLogs(contractAddr.String())...)
if types.IsDappFork(evm.GetHeight(), "evm", "ForkEVMKVHash") { if types.IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMKVHash) {
// 将执行时生成的合约状态数据变更信息也计算哈希并保存 // 将执行时生成的合约状态数据变更信息也计算哈希并保存
hashKV := evm.calcKVHash(contractAddr, logs) hashKV := evm.calcKVHash(contractAddr, logs)
if hashKV != nil { if hashKV != nil {
......
...@@ -19,7 +19,7 @@ func (evm *EVMExecutor) ExecDelLocal(tx *types.Transaction, receipt *types.Recei ...@@ -19,7 +19,7 @@ func (evm *EVMExecutor) ExecDelLocal(tx *types.Transaction, receipt *types.Recei
return set, nil return set, nil
} }
if types.IsDappFork(evm.GetHeight(), "evm", "ForkEVMState") { if types.IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMState) {
// 需要将Exec中生成的合约状态变更信息从localdb中恢复 // 需要将Exec中生成的合约状态变更信息从localdb中恢复
for _, logItem := range receipt.Logs { for _, logItem := range receipt.Logs {
if evmtypes.TyLogEVMStateChangeItem == logItem.Ty { if evmtypes.TyLogEVMStateChangeItem == logItem.Ty {
......
...@@ -20,7 +20,7 @@ func (evm *EVMExecutor) ExecLocal(tx *types.Transaction, receipt *types.ReceiptD ...@@ -20,7 +20,7 @@ func (evm *EVMExecutor) ExecLocal(tx *types.Transaction, receipt *types.ReceiptD
if receipt.GetTy() != types.ExecOk { if receipt.GetTy() != types.ExecOk {
return set, nil return set, nil
} }
if types.IsDappFork(evm.GetHeight(), "evm", "ForkEVMState") { if types.IsDappFork(evm.GetHeight(), "evm", evmtypes.ForkEVMState) {
// 需要将Exec中生成的合约状态变更信息写入localdb // 需要将Exec中生成的合约状态变更信息写入localdb
for _, logItem := range receipt.Logs { for _, logItem := range receipt.Logs {
if evmtypes.TyLogEVMStateChangeItem == logItem.Ty { if evmtypes.TyLogEVMStateChangeItem == logItem.Ty {
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/model" "github.com/33cn/plugin/plugin/dapp/evm/executor/vm/model"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/params" "github.com/33cn/plugin/plugin/dapp/evm/executor/vm/params"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/state" "github.com/33cn/plugin/plugin/dapp/evm/executor/vm/state"
evmtypes "github.com/33cn/plugin/plugin/dapp/evm/types"
) )
type ( type (
...@@ -222,7 +223,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas ...@@ -222,7 +223,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
} }
// 从ForkV20EVMState开始,状态数据存储发生变更,需要做数据迁移 // 从ForkV20EVMState开始,状态数据存储发生变更,需要做数据迁移
if types.IsDappFork(evm.BlockNumber.Int64(), "evm", "ForkEVMState") { if types.IsDappFork(evm.BlockNumber.Int64(), "evm", evmtypes.ForkEVMState) {
evm.StateDB.TransferStateData(addr.String()) evm.StateDB.TransferStateData(addr.String())
} }
...@@ -387,7 +388,7 @@ func (evm *EVM) Create(caller ContractRef, contractAddr common.Address, code []b ...@@ -387,7 +388,7 @@ func (evm *EVM) Create(caller ContractRef, contractAddr common.Address, code []b
if contract.UseGas(createDataGas) { if contract.UseGas(createDataGas) {
evm.StateDB.SetCode(contractAddr.String(), ret) evm.StateDB.SetCode(contractAddr.String(), ret)
// 设置 ABI (如果有的话),这个动作不单独计费 // 设置 ABI (如果有的话),这个动作不单独计费
if len(abi) > 0 && types.IsDappFork(evm.StateDB.GetBlockHeight(), "evm", "ForkEVMKVHash") { if len(abi) > 0 && types.IsDappFork(evm.StateDB.GetBlockHeight(), "evm", evmtypes.ForkEVMABI) {
evm.StateDB.SetAbi(contractAddr.String(), abi) evm.StateDB.SetAbi(contractAddr.String(), abi)
} }
} else { } else {
......
...@@ -50,7 +50,7 @@ func NewContractAccount(addr string, db *MemoryStateDB) *ContractAccount { ...@@ -50,7 +50,7 @@ func NewContractAccount(addr string, db *MemoryStateDB) *ContractAccount {
// 获取数据分为两层,一层是从当前的缓存中获取,如果获取不到,再从localdb中获取 // 获取数据分为两层,一层是从当前的缓存中获取,如果获取不到,再从localdb中获取
func (ca *ContractAccount) GetState(key common.Hash) common.Hash { func (ca *ContractAccount) GetState(key common.Hash) common.Hash {
// 从ForkV19开始,状态数据使用单独的KEY存储 // 从ForkV19开始,状态数据使用单独的KEY存储
if types.IsDappFork(ca.mdb.blockHeight, "evm", "ForkEVMState") { if types.IsDappFork(ca.mdb.blockHeight, "evm", evmtypes.ForkEVMState) {
if val, ok := ca.stateCache[key.Hex()]; ok { if val, ok := ca.stateCache[key.Hex()]; ok {
return val return val
} }
...@@ -76,7 +76,7 @@ func (ca *ContractAccount) SetState(key, value common.Hash) { ...@@ -76,7 +76,7 @@ func (ca *ContractAccount) SetState(key, value common.Hash) {
key: key, key: key,
prevalue: ca.GetState(key), prevalue: ca.GetState(key),
}) })
if types.IsDappFork(ca.mdb.blockHeight, "evm", "ForkEVMState") { if types.IsDappFork(ca.mdb.blockHeight, "evm", evmtypes.ForkEVMState) {
ca.stateCache[key.Hex()] = value ca.stateCache[key.Hex()] = value
//需要设置到localdb中,以免同一个区块中同一个合约多次调用时,状态数据丢失 //需要设置到localdb中,以免同一个区块中同一个合约多次调用时,状态数据丢失
keyStr := getStateItemKey(ca.Addr, key.Hex()) keyStr := getStateItemKey(ca.Addr, key.Hex())
...@@ -107,7 +107,7 @@ func (ca *ContractAccount) TransferState() { ...@@ -107,7 +107,7 @@ func (ca *ContractAccount) TransferState() {
func (ca *ContractAccount) updateStorageHash() { func (ca *ContractAccount) updateStorageHash() {
// 从ForkV20开始,状态数据使用单独KEY存储 // 从ForkV20开始,状态数据使用单独KEY存储
if types.IsDappFork(ca.mdb.blockHeight, "evm", "ForkEVMState") { if types.IsDappFork(ca.mdb.blockHeight, "evm", evmtypes.ForkEVMState) {
return return
} }
var state = &evmtypes.EVMContractState{Suicided: ca.State.Suicided, Nonce: ca.State.Nonce} var state = &evmtypes.EVMContractState{Suicided: ca.State.Suicided, Nonce: ca.State.Nonce}
...@@ -165,14 +165,6 @@ func (ca *ContractAccount) LoadContract(db db.KV) { ...@@ -165,14 +165,6 @@ func (ca *ContractAccount) LoadContract(db db.KV) {
return return
} }
ca.resotreState(data) ca.resotreState(data)
// 加载 ABI (如果有的话)
if types.IsDappFork(ca.mdb.GetBlockHeight(), "evm", "ForkEVMABI") {
data, err := db.Get(ca.GetAbiKey())
if err == nil {
ca.Data.Abi = string(data)
}
}
} }
// SetCode 设置合约二进制代码 // SetCode 设置合约二进制代码
...@@ -191,7 +183,7 @@ func (ca *ContractAccount) SetCode(code []byte) { ...@@ -191,7 +183,7 @@ func (ca *ContractAccount) SetCode(code []byte) {
// SetAbi 设置合约绑定的ABI数据 // SetAbi 设置合约绑定的ABI数据
func (ca *ContractAccount) SetAbi(abi string) { func (ca *ContractAccount) SetAbi(abi string) {
if types.IsDappFork(ca.mdb.GetBlockHeight(), "evm", "ForkEVMABI") { if types.IsDappFork(ca.mdb.GetBlockHeight(), "evm", evmtypes.ForkEVMABI) {
ca.mdb.addChange(abiChange{ ca.mdb.addChange(abiChange{
baseChange: baseChange{}, baseChange: baseChange{},
account: ca.Addr, account: ca.Addr,
...@@ -292,11 +284,6 @@ func (ca *ContractAccount) GetDataKey() []byte { ...@@ -292,11 +284,6 @@ func (ca *ContractAccount) GetDataKey() []byte {
return []byte("mavl-" + evmtypes.ExecutorName + "-data: " + ca.Addr) return []byte("mavl-" + evmtypes.ExecutorName + "-data: " + ca.Addr)
} }
// GetAbiKey 获取Abi数据KEY,ABI数据使用单独的KEY存储
func (ca *ContractAccount) GetAbiKey() []byte {
return []byte("mavl-" + evmtypes.ExecutorName + "-abi: " + ca.Addr)
}
// GetStateKey 获取状态key // GetStateKey 获取状态key
func (ca *ContractAccount) GetStateKey() []byte { func (ca *ContractAccount) GetStateKey() []byte {
return []byte("mavl-" + evmtypes.ExecutorName + "-state: " + ca.Addr) return []byte("mavl-" + evmtypes.ExecutorName + "-state: " + ca.Addr)
......
...@@ -275,7 +275,7 @@ func (ch storageChange) getData(mdb *MemoryStateDB) []*types.KeyValue { ...@@ -275,7 +275,7 @@ func (ch storageChange) getData(mdb *MemoryStateDB) []*types.KeyValue {
} }
func (ch storageChange) getLog(mdb *MemoryStateDB) []*types.ReceiptLog { func (ch storageChange) getLog(mdb *MemoryStateDB) []*types.ReceiptLog {
if types.IsDappFork(mdb.blockHeight, "evm", "ForkEVMState") { if types.IsDappFork(mdb.blockHeight, "evm", evmtypes.ForkEVMState) {
acc := mdb.accounts[ch.account] acc := mdb.accounts[ch.account]
if acc != nil { if acc != nil {
currentVal := acc.GetState(ch.key) currentVal := acc.GetState(ch.key)
......
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common" "github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/model" "github.com/33cn/plugin/plugin/dapp/evm/executor/vm/model"
evmtypes "github.com/33cn/plugin/plugin/dapp/evm/types"
) )
// MemoryStateDB 内存状态数据库,保存在区块操作时内部的数据变更操作 // MemoryStateDB 内存状态数据库,保存在区块操作时内部的数据变更操作
...@@ -263,7 +264,7 @@ func (mdb *MemoryStateDB) SetState(addr string, key common.Hash, value common.Ha ...@@ -263,7 +264,7 @@ func (mdb *MemoryStateDB) SetState(addr string, key common.Hash, value common.Ha
if acc != nil { if acc != nil {
acc.SetState(key, value) acc.SetState(key, value)
// 新的分叉中状态数据变更不需要单独进行标识 // 新的分叉中状态数据变更不需要单独进行标识
if !types.IsDappFork(mdb.blockHeight, "evm", "ForkEVMState") { if !types.IsDappFork(mdb.blockHeight, "evm", evmtypes.ForkEVMState) {
mdb.stateDirty[addr] = true mdb.stateDirty[addr] = true
} }
} }
......
...@@ -28,13 +28,13 @@ func init() { ...@@ -28,13 +28,13 @@ func init() {
// init executor type // init executor type
types.RegistorExecutor(ExecutorName, NewType()) types.RegistorExecutor(ExecutorName, NewType())
types.RegisterDappFork(ExecutorName, "Enable", 500000) types.RegisterDappFork(ExecutorName, EVMEnable, 500000)
// EVM合约中的数据分散存储,支持大数据量 // EVM合约中的数据分散存储,支持大数据量
types.RegisterDappFork(ExecutorName, "ForkEVMState", 650000) types.RegisterDappFork(ExecutorName, ForkEVMState, 650000)
// EVM合约状态数据生成哈希,保存在主链的StateDB中 // EVM合约状态数据生成哈希,保存在主链的StateDB中
types.RegisterDappFork(ExecutorName, "ForkEVMKVHash", 1000000) types.RegisterDappFork(ExecutorName, ForkEVMKVHash, 1000000)
// EVM合约支持ABI绑定和调用 // EVM合约支持ABI绑定和调用
types.RegisterDappFork(ExecutorName, "ForkEVMABI", 1500000) types.RegisterDappFork(ExecutorName, ForkEVMABI, 1250000)
} }
// EvmType EVM类型定义 // EvmType EVM类型定义
......
...@@ -29,6 +29,17 @@ const ( ...@@ -29,6 +29,17 @@ const (
MaxGasLimit = 10000000 MaxGasLimit = 10000000
) )
const (
// EVMEnable 启用EVM
EVMEnable = "Enable"
// ForkEVMState EVM合约中的数据分散存储,支持大数据量
ForkEVMState = "ForkEVMState"
// ForkEVMKVHash EVM合约状态数据生成哈希,保存在主链的StateDB中
ForkEVMKVHash = "ForkEVMKVHash"
// ForkEVMABI EVM合约支持ABI绑定和调用
ForkEVMABI = "ForkEVMABI"
)
var ( var (
// EvmPrefix 本执行器前缀 // EvmPrefix 本执行器前缀
EvmPrefix = "user.evm." EvmPrefix = "user.evm."
......
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