Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
942e1c65
Commit
942e1c65
authored
Nov 27, 2018
by
Litian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
evm支持abi功能合入
parent
341c8d26
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
32 additions
and
32 deletions
+32
-32
evm.go
plugin/dapp/evm/executor/evm.go
+1
-1
exec.go
plugin/dapp/evm/executor/exec.go
+4
-4
exec_del_local.go
plugin/dapp/evm/executor/exec_del_local.go
+1
-1
exec_local.go
plugin/dapp/evm/executor/exec_local.go
+1
-1
evm.go
plugin/dapp/evm/executor/vm/runtime/evm.go
+3
-2
account.go
plugin/dapp/evm/executor/vm/state/account.go
+4
-17
snapshot.go
plugin/dapp/evm/executor/vm/state/snapshot.go
+1
-1
statedb.go
plugin/dapp/evm/executor/vm/state/statedb.go
+2
-1
evm.go
plugin/dapp/evm/types/evm.go
+4
-4
types.go
plugin/dapp/evm/types/types.go
+11
-0
No files found.
plugin/dapp/evm/executor/evm.go
View file @
942e1c65
...
...
@@ -38,7 +38,7 @@ func init() {
// Init 初始化本合约对象
func
Init
(
name
string
,
sub
[]
byte
)
{
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
))
// 初始化硬分叉数据
state
.
InitForkData
()
...
...
plugin/dapp/evm/executor/exec.go
View file @
942e1c65
...
...
@@ -67,7 +67,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
if
isCreate
{
// 如果携带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
()))
if
err
!=
nil
{
return
receipt
,
err
...
...
@@ -77,7 +77,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
}
else
{
inData
:=
msg
.
Data
()
// 在这里进行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
)
if
err
!=
nil
{
return
receipt
,
err
...
...
@@ -126,7 +126,7 @@ func (evm *EVMExecutor) innerExec(msg *common.Message, txHash []byte, index int,
kvSet
,
logs
:=
evm
.
mStateDB
.
GetChangedData
(
curVer
.
GetID
())
contractReceipt
:=
&
evmtypes
.
ReceiptEVMContract
{
Caller
:
msg
.
From
()
.
String
(),
ContractName
:
execName
,
ContractAddr
:
contractAddr
.
String
(),
UsedGas
:
usedGas
,
Ret
:
ret
}
// 这里进行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
()))
if
err
!=
nil
{
// 这里出错不影响整体执行,只打印错误信息
...
...
@@ -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
,
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
)
if
hashKV
!=
nil
{
...
...
plugin/dapp/evm/executor/exec_del_local.go
View file @
942e1c65
...
...
@@ -19,7 +19,7 @@ func (evm *EVMExecutor) ExecDelLocal(tx *types.Transaction, receipt *types.Recei
return
set
,
nil
}
if
types
.
IsDappFork
(
evm
.
GetHeight
(),
"evm"
,
"ForkEVMState"
)
{
if
types
.
IsDappFork
(
evm
.
GetHeight
(),
"evm"
,
evmtypes
.
ForkEVMState
)
{
// 需要将Exec中生成的合约状态变更信息从localdb中恢复
for
_
,
logItem
:=
range
receipt
.
Logs
{
if
evmtypes
.
TyLogEVMStateChangeItem
==
logItem
.
Ty
{
...
...
plugin/dapp/evm/executor/exec_local.go
View file @
942e1c65
...
...
@@ -20,7 +20,7 @@ func (evm *EVMExecutor) ExecLocal(tx *types.Transaction, receipt *types.ReceiptD
if
receipt
.
GetTy
()
!=
types
.
ExecOk
{
return
set
,
nil
}
if
types
.
IsDappFork
(
evm
.
GetHeight
(),
"evm"
,
"ForkEVMState"
)
{
if
types
.
IsDappFork
(
evm
.
GetHeight
(),
"evm"
,
evmtypes
.
ForkEVMState
)
{
// 需要将Exec中生成的合约状态变更信息写入localdb
for
_
,
logItem
:=
range
receipt
.
Logs
{
if
evmtypes
.
TyLogEVMStateChangeItem
==
logItem
.
Ty
{
...
...
plugin/dapp/evm/executor/vm/runtime/evm.go
View file @
942e1c65
...
...
@@ -16,6 +16,7 @@ import (
"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/state"
evmtypes
"github.com/33cn/plugin/plugin/dapp/evm/types"
)
type
(
...
...
@@ -222,7 +223,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
}
// 从ForkV20EVMState开始,状态数据存储发生变更,需要做数据迁移
if
types
.
IsDappFork
(
evm
.
BlockNumber
.
Int64
(),
"evm"
,
"ForkEVMState"
)
{
if
types
.
IsDappFork
(
evm
.
BlockNumber
.
Int64
(),
"evm"
,
evmtypes
.
ForkEVMState
)
{
evm
.
StateDB
.
TransferStateData
(
addr
.
String
())
}
...
...
@@ -387,7 +388,7 @@ func (evm *EVM) Create(caller ContractRef, contractAddr common.Address, code []b
if
contract
.
UseGas
(
createDataGas
)
{
evm
.
StateDB
.
SetCode
(
contractAddr
.
String
(),
ret
)
// 设置 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
)
}
}
else
{
...
...
plugin/dapp/evm/executor/vm/state/account.go
View file @
942e1c65
...
...
@@ -50,7 +50,7 @@ func NewContractAccount(addr string, db *MemoryStateDB) *ContractAccount {
// 获取数据分为两层,一层是从当前的缓存中获取,如果获取不到,再从localdb中获取
func
(
ca
*
ContractAccount
)
GetState
(
key
common
.
Hash
)
common
.
Hash
{
// 从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
{
return
val
}
...
...
@@ -76,7 +76,7 @@ func (ca *ContractAccount) SetState(key, value common.Hash) {
key
:
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
//需要设置到localdb中,以免同一个区块中同一个合约多次调用时,状态数据丢失
keyStr
:=
getStateItemKey
(
ca
.
Addr
,
key
.
Hex
())
...
...
@@ -107,7 +107,7 @@ func (ca *ContractAccount) TransferState() {
func
(
ca
*
ContractAccount
)
updateStorageHash
()
{
// 从ForkV20开始,状态数据使用单独KEY存储
if
types
.
IsDappFork
(
ca
.
mdb
.
blockHeight
,
"evm"
,
"ForkEVMState"
)
{
if
types
.
IsDappFork
(
ca
.
mdb
.
blockHeight
,
"evm"
,
evmtypes
.
ForkEVMState
)
{
return
}
var
state
=
&
evmtypes
.
EVMContractState
{
Suicided
:
ca
.
State
.
Suicided
,
Nonce
:
ca
.
State
.
Nonce
}
...
...
@@ -165,14 +165,6 @@ func (ca *ContractAccount) LoadContract(db db.KV) {
return
}
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 设置合约二进制代码
...
...
@@ -191,7 +183,7 @@ func (ca *ContractAccount) SetCode(code []byte) {
// SetAbi 设置合约绑定的ABI数据
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
{
baseChange
:
baseChange
{},
account
:
ca
.
Addr
,
...
...
@@ -292,11 +284,6 @@ func (ca *ContractAccount) GetDataKey() []byte {
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
func
(
ca
*
ContractAccount
)
GetStateKey
()
[]
byte
{
return
[]
byte
(
"mavl-"
+
evmtypes
.
ExecutorName
+
"-state: "
+
ca
.
Addr
)
...
...
plugin/dapp/evm/executor/vm/state/snapshot.go
View file @
942e1c65
...
...
@@ -275,7 +275,7 @@ func (ch storageChange) getData(mdb *MemoryStateDB) []*types.KeyValue {
}
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
]
if
acc
!=
nil
{
currentVal
:=
acc
.
GetState
(
ch
.
key
)
...
...
plugin/dapp/evm/executor/vm/state/statedb.go
View file @
942e1c65
...
...
@@ -14,6 +14,7 @@ import (
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/model"
evmtypes
"github.com/33cn/plugin/plugin/dapp/evm/types"
)
// MemoryStateDB 内存状态数据库,保存在区块操作时内部的数据变更操作
...
...
@@ -263,7 +264,7 @@ func (mdb *MemoryStateDB) SetState(addr string, key common.Hash, value common.Ha
if
acc
!=
nil
{
acc
.
SetState
(
key
,
value
)
// 新的分叉中状态数据变更不需要单独进行标识
if
!
types
.
IsDappFork
(
mdb
.
blockHeight
,
"evm"
,
"ForkEVMState"
)
{
if
!
types
.
IsDappFork
(
mdb
.
blockHeight
,
"evm"
,
evmtypes
.
ForkEVMState
)
{
mdb
.
stateDirty
[
addr
]
=
true
}
}
...
...
plugin/dapp/evm/types/evm.go
View file @
942e1c65
...
...
@@ -28,13 +28,13 @@ func init() {
// init executor type
types
.
RegistorExecutor
(
ExecutorName
,
NewType
())
types
.
RegisterDappFork
(
ExecutorName
,
"Enable"
,
500000
)
types
.
RegisterDappFork
(
ExecutorName
,
EVMEnable
,
500000
)
// EVM合约中的数据分散存储,支持大数据量
types
.
RegisterDappFork
(
ExecutorName
,
"ForkEVMState"
,
650000
)
types
.
RegisterDappFork
(
ExecutorName
,
ForkEVMState
,
650000
)
// EVM合约状态数据生成哈希,保存在主链的StateDB中
types
.
RegisterDappFork
(
ExecutorName
,
"ForkEVMKVHash"
,
1000000
)
types
.
RegisterDappFork
(
ExecutorName
,
ForkEVMKVHash
,
1000000
)
// EVM合约支持ABI绑定和调用
types
.
RegisterDappFork
(
ExecutorName
,
"ForkEVMABI"
,
150
0000
)
types
.
RegisterDappFork
(
ExecutorName
,
ForkEVMABI
,
125
0000
)
}
// EvmType EVM类型定义
...
...
plugin/dapp/evm/types/types.go
View file @
942e1c65
...
...
@@ -29,6 +29,17 @@ const (
MaxGasLimit
=
10000000
)
const
(
// EVMEnable 启用EVM
EVMEnable
=
"Enable"
// ForkEVMState EVM合约中的数据分散存储,支持大数据量
ForkEVMState
=
"ForkEVMState"
// ForkEVMKVHash EVM合约状态数据生成哈希,保存在主链的StateDB中
ForkEVMKVHash
=
"ForkEVMKVHash"
// ForkEVMABI EVM合约支持ABI绑定和调用
ForkEVMABI
=
"ForkEVMABI"
)
var
(
// EvmPrefix 本执行器前缀
EvmPrefix
=
"user.evm."
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment