Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
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
张蕾
Geth-Modification
Commits
1d3d4a4d
Commit
1d3d4a4d
authored
Oct 08, 2018
by
Péter Szilágyi
Committed by
Felix Lange
Oct 08, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
core/vm: reuse Keccak-256 hashes across opcode executions (#17863)
parent
c5cb214f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
6 deletions
+48
-6
instructions.go
core/vm/instructions.go
+12
-5
instructions_test.go
core/vm/instructions_test.go
+21
-0
interpreter.go
core/vm/interpreter.go
+15
-1
No files found.
core/vm/instructions.go
View file @
1d3d4a4d
...
@@ -24,7 +24,7 @@ import (
...
@@ -24,7 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto
/sha3
"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/params"
)
)
...
@@ -373,13 +373,20 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *
...
@@ -373,13 +373,20 @@ func opSAR(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory *
func
opSha3
(
pc
*
uint64
,
interpreter
*
EVMInterpreter
,
contract
*
Contract
,
memory
*
Memory
,
stack
*
Stack
)
([]
byte
,
error
)
{
func
opSha3
(
pc
*
uint64
,
interpreter
*
EVMInterpreter
,
contract
*
Contract
,
memory
*
Memory
,
stack
*
Stack
)
([]
byte
,
error
)
{
offset
,
size
:=
stack
.
pop
(),
stack
.
pop
()
offset
,
size
:=
stack
.
pop
(),
stack
.
pop
()
data
:=
memory
.
Get
(
offset
.
Int64
(),
size
.
Int64
())
data
:=
memory
.
Get
(
offset
.
Int64
(),
size
.
Int64
())
hash
:=
crypto
.
Keccak256
(
data
)
evm
:=
interpreter
.
evm
if
interpreter
.
hasher
==
nil
{
interpreter
.
hasher
=
sha3
.
NewKeccak256
()
.
(
keccakState
)
}
else
{
interpreter
.
hasher
.
Reset
()
}
interpreter
.
hasher
.
Write
(
data
)
interpreter
.
hasher
.
Read
(
interpreter
.
hasherBuf
[
:
])
evm
:=
interpreter
.
evm
if
evm
.
vmConfig
.
EnablePreimageRecording
{
if
evm
.
vmConfig
.
EnablePreimageRecording
{
evm
.
StateDB
.
AddPreimage
(
common
.
BytesToHash
(
hash
)
,
data
)
evm
.
StateDB
.
AddPreimage
(
interpreter
.
hasherBuf
,
data
)
}
}
stack
.
push
(
interpreter
.
intPool
.
get
()
.
SetBytes
(
hash
))
stack
.
push
(
interpreter
.
intPool
.
get
()
.
SetBytes
(
interpreter
.
hasherBuf
[
:
]
))
interpreter
.
intPool
.
put
(
offset
,
size
)
interpreter
.
intPool
.
put
(
offset
,
size
)
return
nil
,
nil
return
nil
,
nil
...
...
core/vm/instructions_test.go
View file @
1d3d4a4d
...
@@ -492,6 +492,27 @@ func BenchmarkOpMstore(bench *testing.B) {
...
@@ -492,6 +492,27 @@ func BenchmarkOpMstore(bench *testing.B) {
poolOfIntPools
.
put
(
evmInterpreter
.
intPool
)
poolOfIntPools
.
put
(
evmInterpreter
.
intPool
)
}
}
func
BenchmarkOpSHA3
(
bench
*
testing
.
B
)
{
var
(
env
=
NewEVM
(
Context
{},
nil
,
params
.
TestChainConfig
,
Config
{})
stack
=
newstack
()
mem
=
NewMemory
()
evmInterpreter
=
NewEVMInterpreter
(
env
,
env
.
vmConfig
)
)
env
.
interpreter
=
evmInterpreter
evmInterpreter
.
intPool
=
poolOfIntPools
.
get
()
mem
.
Resize
(
32
)
pc
:=
uint64
(
0
)
start
:=
big
.
NewInt
(
0
)
bench
.
ResetTimer
()
for
i
:=
0
;
i
<
bench
.
N
;
i
++
{
stack
.
pushN
(
big
.
NewInt
(
32
),
start
)
opSha3
(
&
pc
,
evmInterpreter
,
nil
,
mem
,
stack
)
}
poolOfIntPools
.
put
(
evmInterpreter
.
intPool
)
}
func
TestCreate2Addreses
(
t
*
testing
.
T
)
{
func
TestCreate2Addreses
(
t
*
testing
.
T
)
{
type
testcase
struct
{
type
testcase
struct
{
origin
string
origin
string
...
...
core/vm/interpreter.go
View file @
1d3d4a4d
...
@@ -18,8 +18,10 @@ package vm
...
@@ -18,8 +18,10 @@ package vm
import
(
import
(
"fmt"
"fmt"
"hash"
"sync/atomic"
"sync/atomic"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/params"
)
)
...
@@ -68,13 +70,25 @@ type Interpreter interface {
...
@@ -68,13 +70,25 @@ type Interpreter interface {
CanRun
([]
byte
)
bool
CanRun
([]
byte
)
bool
}
}
// keccakState wraps sha3.state. In addition to the usual hash methods, it also supports
// Read to get a variable amount of data from the hash state. Read is faster than Sum
// because it doesn't copy the internal state, but also modifies the internal state.
type
keccakState
interface
{
hash
.
Hash
Read
([]
byte
)
(
int
,
error
)
}
// EVMInterpreter represents an EVM interpreter
// EVMInterpreter represents an EVM interpreter
type
EVMInterpreter
struct
{
type
EVMInterpreter
struct
{
evm
*
EVM
evm
*
EVM
cfg
Config
cfg
Config
gasTable
params
.
GasTable
gasTable
params
.
GasTable
intPool
*
intPool
intPool
*
intPool
hasher
keccakState
// Keccak256 hasher instance shared across opcodes
hasherBuf
common
.
Hash
// Keccak256 hasher result array shared aross opcodes
readOnly
bool
// Whether to throw on stateful modifications
readOnly
bool
// Whether to throw on stateful modifications
returnData
[]
byte
// Last CALL's return data for subsequent reuse
returnData
[]
byte
// Last CALL's return data for subsequent reuse
}
}
...
...
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