Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny2022da committed Feb 24, 2024
1 parent 57c79b2 commit 14ea77b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 99 deletions.
4 changes: 4 additions & 0 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
compiler2 "github.com/ethereum/go-ethereum/core/opcodeCompiler/compiler"
"io"
"math/big"
"os"
Expand Down Expand Up @@ -219,6 +220,9 @@ func runCmd(ctx *cli.Context) error {
},
}

if runtimeConfig.EVMConfig.EnableOpcodeOptimizations {
compiler2.GetOpcodeProcessorInstance().EnableOptimization()
}
if cpuProfilePath := ctx.String(CPUProfileFlag.Name); cpuProfilePath != "" {
f, err := os.Create(cpuProfilePath)
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions core/opcodeCompiler/compiler/opcodeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func GetOpcodeProcessorInstance() *OpcodeProcessor {
}

func (p *OpcodeProcessor) EnableOptimization() {
if p.enabled {
return
}
p.enabled = true
p.codeCache = getOpCodeCacheInstance()
}
Expand Down Expand Up @@ -92,6 +95,14 @@ func (p *OpcodeProcessor) FlushCodeCache(address common.Address, hash common.Has
p.taskChannel <- task
}

func (p *OpcodeProcessor) RewriteOptimizedCodeForDB(address common.Address, code []byte, hash common.Hash) {
if p.enabled {
// p.GenOrRewriteOptimizedCode(address, code, hash)
//
p.GenOrLoadOptimizedCode(address, code, hash)
}
}

// Consumer function
func (p *OpcodeProcessor) taskProcessor() {
for {
Expand All @@ -110,15 +121,6 @@ func (p *OpcodeProcessor) handleOptimizationTask(task optimizeTask) {
}
}

// message producer.
func (p *OpcodeProcessor) RewriteOptimizedCodeForDB(address common.Address, code []byte, hash common.Hash) {
if p.enabled {
// TODO. may not need this if memory is enough
p.FlushCodeCache(address, hash)
p.GenOrRewriteOptimizedCode(address, code, hash)
}
}

// GenOrRewriteOptimizedCode generate the optimized code and refresh the codecache.
func (p *OpcodeProcessor) GenOrRewriteOptimizedCode(address common.Address, code []byte, hash common.Hash) (OptCode, error) {
if !p.enabled {
Expand Down
11 changes: 4 additions & 7 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte
}

func tryGetOptimizedCode(evm *EVM, addrCopy common.Address) (bool, []byte) {

var code []byte
// In case the code is not in stateDB. skip the optimization
// TODO - this is a must to avoid the case that code has required to be delete but not yet processed by the codecache,
Expand All @@ -451,14 +450,12 @@ func tryGetOptimizedCode(evm *EVM, addrCopy common.Address) (bool, []byte) {
if evm.Config.EnableOpcodeOptimizations {
codeHash := evm.StateDB.GetCodeHash(addrCopy)
if codeHash != (common.Hash{}) {
code = compiler.GetOpcodeProcessorInstance().LoadOptimizedCode(addrCopy, codeHash)
if len(code) != 0 {
optCode := compiler.GetOpcodeProcessorInstance().LoadOptimizedCode(addrCopy, codeHash)
if len(optCode) != 0 {
code = optCode
optimized = true
} else {
code, _ = compiler.GetOpcodeProcessorInstance().GenOrRewriteOptimizedCode(addrCopy, code, codeHash)
if len(code) > 0 {
optimized = true
}
compiler.GetOpcodeProcessorInstance().GenOrLoadOptimizedCode(addrCopy, code, codeHash)
}
}
}
Expand Down
88 changes: 5 additions & 83 deletions core/vm/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -682,87 +683,6 @@ func TestColdAccountAccessCost(t *testing.T) {
}
}

// TestColdAccountAccessCostWithOpcodeOptimizations test that the cold account access cost is reported
// correctly with opcode optimizations enabled
// see: https://github.com/ethereum/go-ethereum/issues/22649
func TestColdAccountAccessCostWithOpcodeOptimizations(t *testing.T) {
for i, tc := range []struct {
code []byte
step int
want uint64
}{
{ // EXTCODEHASH(0xff)
code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.EXTCODEHASH), byte(vm.POP)},
step: 1,
want: 2600,
},
{ // BALANCE(0xff)
code: []byte{byte(vm.PUSH1), 0xFF, byte(vm.BALANCE), byte(vm.POP)},
step: 1,
want: 2600,
},
{ // CALL(0xff)
code: []byte{
byte(vm.PUSH1), 0x0,
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALL), byte(vm.POP),
},
step: 5,
want: 2855,
},
{ // CALLCODE(0xff)
code: []byte{
byte(vm.PUSH1), 0x0,
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.CALLCODE), byte(vm.POP),
},
step: 5,
want: 2855,
},
{ // DELEGATECALL(0xff)
code: []byte{
byte(vm.PUSH1), 0x0,
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.DELEGATECALL), byte(vm.POP),
},
step: 4,
want: 2855,
},
{ // STATICCALL(0xff)
code: []byte{
byte(vm.PUSH1), 0x0,
byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1),
byte(vm.PUSH1), 0xff, byte(vm.DUP1), byte(vm.STATICCALL), byte(vm.POP),
},
step: 4,
want: 2855,
},
{ // SELFDESTRUCT(0xff)
code: []byte{
byte(vm.PUSH1), 0xff, byte(vm.SELFDESTRUCT),
},
step: 1,
want: 7600,
},
} {
tracer := logger.NewStructLogger(nil)
Execute(tc.code, nil, &Config{
EVMConfig: vm.Config{
Debug: true,
Tracer: tracer,
EnableOpcodeOptimizations: true,
},
})
have := tracer.StructLogs()[tc.step].GasCost

if want := tc.want; have != want {
for ii, op := range tracer.StructLogs() {
t.Logf("%d: %v %d", ii, op.OpName(), op.GasCost)
}
t.Fatalf("tescase %d, gas report wrong, step %d, have %d want %d", i, tc.step, have, want)
}
}
}
func TestRuntimeJSTracer(t *testing.T) {
jsTracers := []string{
`{enters: 0, exits: 0, enterGas: 0, gasUsed: 0, steps:0,
Expand Down Expand Up @@ -1079,6 +999,7 @@ func TestRuntimeJSTracerWithOpcodeOptimizer(t *testing.T) {
byte(vm.SELFDESTRUCT),
}
main := common.HexToAddress("0xaa")
compiler.GetOpcodeProcessorInstance().EnableOptimization()
for i, jsTracer := range jsTracers {
for j, tc := range tests {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
Expand All @@ -1088,7 +1009,8 @@ func TestRuntimeJSTracerWithOpcodeOptimizer(t *testing.T) {
statedb.SetCode(common.HexToAddress("0xdd"), calleeCode)
statedb.SetCode(common.HexToAddress("0xee"), calleeCode)
statedb.SetCode(common.HexToAddress("0xff"), depressedCode)

/* wait for optimized code to be generated */
time.Sleep(time.Second)
tracer, err := tracers.DefaultDirectory.New(jsTracer, new(tracers.Context), nil)
if err != nil {
t.Fatal(err)
Expand All @@ -1101,7 +1023,7 @@ func TestRuntimeJSTracerWithOpcodeOptimizer(t *testing.T) {
Tracer: tracer,
EnableOpcodeOptimizations: true,
}})
compiler.GetOpcodeProcessorInstance().FlushCodeCache(main, common.Hash{})
compiler.GetOpcodeProcessorInstance().DeleteCodeCache(main, common.Hash{})
if err != nil {
t.Fatal("didn't expect error", err)
}
Expand Down

0 comments on commit 14ea77b

Please sign in to comment.