2018-02-14 06:49:11 -06:00
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
2017-05-28 16:39:33 -05:00
package vm
import (
2018-10-05 02:32:35 -05:00
"bytes"
2019-04-04 04:19:38 -05:00
"encoding/json"
"fmt"
2022-01-10 01:44:21 -06:00
"math/big"
2022-05-16 04:59:35 -05:00
"os"
2023-07-11 02:55:34 -05:00
"strings"
2017-05-28 16:39:33 -05:00
"testing"
"github.com/ethereum/go-ethereum/common"
2023-07-11 02:55:34 -05:00
"github.com/ethereum/go-ethereum/common/math"
2022-11-16 03:18:52 -06:00
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
2023-05-11 02:19:42 -05:00
"github.com/ethereum/go-ethereum/core/types"
2018-10-05 02:32:35 -05:00
"github.com/ethereum/go-ethereum/crypto"
2017-05-28 16:39:33 -05:00
"github.com/ethereum/go-ethereum/params"
2020-06-08 07:24:40 -05:00
"github.com/holiman/uint256"
2017-05-28 16:39:33 -05:00
)
2019-04-04 04:19:38 -05:00
type TwoOperandTestcase struct {
X string
Y string
Expected string
2018-02-23 04:32:57 -06:00
}
2019-04-04 04:19:38 -05:00
type twoOperandParams struct {
x string
y string
}
2021-04-30 05:49:13 -05:00
var alphabetSoup = "ABCDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff"
2019-04-04 04:19:38 -05:00
var commonParams [ ] * twoOperandParams
var twoOpMethods map [ string ] executionFunc
2022-11-16 03:18:52 -06:00
type contractRef struct {
addr common . Address
}
func ( c contractRef ) Address ( ) common . Address {
return c . addr
}
2019-04-04 04:19:38 -05:00
func init ( ) {
// Params is a list of common edgecases that should be used for some common tests
params := [ ] string {
"0000000000000000000000000000000000000000000000000000000000000000" , // 0
"0000000000000000000000000000000000000000000000000000000000000001" , // +1
"0000000000000000000000000000000000000000000000000000000000000005" , // +5
"7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" , // + max -1
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , // + max
"8000000000000000000000000000000000000000000000000000000000000000" , // - max
"8000000000000000000000000000000000000000000000000000000000000001" , // - max+1
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb" , // - 5
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , // - 1
}
// Params are combined so each param is used on each 'side'
commonParams = make ( [ ] * twoOperandParams , len ( params ) * len ( params ) )
for i , x := range params {
for j , y := range params {
commonParams [ i * len ( params ) + j ] = & twoOperandParams { x , y }
}
}
twoOpMethods = map [ string ] executionFunc {
"add" : opAdd ,
"sub" : opSub ,
"mul" : opMul ,
"div" : opDiv ,
"sdiv" : opSdiv ,
"mod" : opMod ,
"smod" : opSmod ,
"exp" : opExp ,
"signext" : opSignExtend ,
"lt" : opLt ,
"gt" : opGt ,
"slt" : opSlt ,
"sgt" : opSgt ,
"eq" : opEq ,
"and" : opAnd ,
"or" : opOr ,
"xor" : opXor ,
"byte" : opByte ,
"shl" : opSHL ,
"shr" : opSHR ,
"sar" : opSAR ,
}
}
func testTwoOperandOp ( t * testing . T , tests [ ] TwoOperandTestcase , opFn executionFunc , name string ) {
2018-02-23 04:32:57 -06:00
var (
2020-11-13 06:42:19 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
2018-07-25 07:56:39 -05:00
stack = newstack ( )
pc = uint64 ( 0 )
2021-07-06 15:03:09 -05:00
evmInterpreter = env . interpreter
2018-02-23 04:32:57 -06:00
)
2019-04-04 04:19:38 -05:00
2018-02-23 04:32:57 -06:00
for i , test := range tests {
2020-06-08 07:24:40 -05:00
x := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . X ) )
y := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . Y ) )
expected := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . Expected ) )
2018-02-23 04:32:57 -06:00
stack . push ( x )
2019-04-04 04:19:38 -05:00
stack . push ( y )
2021-03-25 04:13:14 -05:00
opFn ( & pc , evmInterpreter , & ScopeContext { nil , stack , nil } )
2020-06-08 07:24:40 -05:00
if len ( stack . data ) != 1 {
t . Errorf ( "Expected one item on stack after %v, got %d: " , name , len ( stack . data ) )
}
2018-02-23 04:32:57 -06:00
actual := stack . pop ( )
2019-04-04 04:19:38 -05:00
2018-02-23 04:32:57 -06:00
if actual . Cmp ( expected ) != 0 {
2019-04-04 04:19:38 -05:00
t . Errorf ( "Testcase %v %d, %v(%x, %x): expected %x, got %x" , name , i , name , x , y , expected , actual )
2018-02-23 04:32:57 -06:00
}
}
}
2017-05-28 16:39:33 -05:00
func TestByteOp ( t * testing . T ) {
2019-04-04 04:19:38 -05:00
tests := [ ] TwoOperandTestcase {
{ "ABCDEF0908070605040302010000000000000000000000000000000000000000" , "00" , "AB" } ,
{ "ABCDEF0908070605040302010000000000000000000000000000000000000000" , "01" , "CD" } ,
{ "00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" , "00" , "00" } ,
{ "00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff" , "01" , "CD" } ,
{ "0000000000000000000000000000000000000000000000000000000000102030" , "1F" , "30" } ,
{ "0000000000000000000000000000000000000000000000000000000000102030" , "1E" , "20" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "20" , "00" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "FFFFFFFFFFFFFFFF" , "00" } ,
2017-05-28 16:39:33 -05:00
}
2019-04-04 04:19:38 -05:00
testTwoOperandOp ( t , tests , opByte , "byte" )
2017-05-28 16:39:33 -05:00
}
2017-06-28 04:45:45 -05:00
2018-02-23 04:32:57 -06:00
func TestSHL ( t * testing . T ) {
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shl-shift-left
2019-04-04 04:19:38 -05:00
tests := [ ] TwoOperandTestcase {
2018-02-23 04:32:57 -06:00
{ "0000000000000000000000000000000000000000000000000000000000000001" , "01" , "0000000000000000000000000000000000000000000000000000000000000002" } ,
{ "0000000000000000000000000000000000000000000000000000000000000001" , "ff" , "8000000000000000000000000000000000000000000000000000000000000000" } ,
{ "0000000000000000000000000000000000000000000000000000000000000001" , "0100" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "0000000000000000000000000000000000000000000000000000000000000001" , "0101" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "00" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "01" , "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "ff" , "8000000000000000000000000000000000000000000000000000000000000000" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "0100" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "0000000000000000000000000000000000000000000000000000000000000000" , "01" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "01" , "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" } ,
}
2019-04-04 04:19:38 -05:00
testTwoOperandOp ( t , tests , opSHL , "shl" )
2018-02-23 04:32:57 -06:00
}
func TestSHR ( t * testing . T ) {
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#shr-logical-shift-right
2019-04-04 04:19:38 -05:00
tests := [ ] TwoOperandTestcase {
2018-02-23 04:32:57 -06:00
{ "0000000000000000000000000000000000000000000000000000000000000001" , "00" , "0000000000000000000000000000000000000000000000000000000000000001" } ,
{ "0000000000000000000000000000000000000000000000000000000000000001" , "01" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "01" , "4000000000000000000000000000000000000000000000000000000000000000" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "ff" , "0000000000000000000000000000000000000000000000000000000000000001" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "0100" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "0101" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "00" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "01" , "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "ff" , "0000000000000000000000000000000000000000000000000000000000000001" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "0100" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "0000000000000000000000000000000000000000000000000000000000000000" , "01" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
}
2019-04-04 04:19:38 -05:00
testTwoOperandOp ( t , tests , opSHR , "shr" )
2018-02-23 04:32:57 -06:00
}
func TestSAR ( t * testing . T ) {
// Testcases from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-145.md#sar-arithmetic-shift-right
2019-04-04 04:19:38 -05:00
tests := [ ] TwoOperandTestcase {
2018-02-23 04:32:57 -06:00
{ "0000000000000000000000000000000000000000000000000000000000000001" , "00" , "0000000000000000000000000000000000000000000000000000000000000001" } ,
{ "0000000000000000000000000000000000000000000000000000000000000001" , "01" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "01" , "c000000000000000000000000000000000000000000000000000000000000000" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "ff" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "0100" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "8000000000000000000000000000000000000000000000000000000000000000" , "0101" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "00" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "01" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "ff" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "0100" , "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" } ,
{ "0000000000000000000000000000000000000000000000000000000000000000" , "01" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "4000000000000000000000000000000000000000000000000000000000000000" , "fe" , "0000000000000000000000000000000000000000000000000000000000000001" } ,
{ "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "f8" , "000000000000000000000000000000000000000000000000000000000000007f" } ,
{ "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "fe" , "0000000000000000000000000000000000000000000000000000000000000001" } ,
{ "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "ff" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
{ "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" , "0100" , "0000000000000000000000000000000000000000000000000000000000000000" } ,
}
2019-04-04 04:19:38 -05:00
testTwoOperandOp ( t , tests , opSAR , "sar" )
}
2020-06-08 07:24:40 -05:00
func TestAddMod ( t * testing . T ) {
var (
2020-11-13 06:42:19 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
2020-06-08 07:24:40 -05:00
stack = newstack ( )
2023-02-05 08:11:25 -06:00
evmInterpreter = NewEVMInterpreter ( env )
2020-06-08 07:24:40 -05:00
pc = uint64 ( 0 )
)
tests := [ ] struct {
x string
y string
z string
expected string
} {
{ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ,
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" ,
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ,
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe" ,
} ,
}
// x + y = 0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
// in 256 bit repr, fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd
for i , test := range tests {
x := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . x ) )
y := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . y ) )
z := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . z ) )
expected := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( test . expected ) )
stack . push ( z )
stack . push ( y )
stack . push ( x )
2021-03-25 04:13:14 -05:00
opAddmod ( & pc , evmInterpreter , & ScopeContext { nil , stack , nil } )
2020-06-08 07:24:40 -05:00
actual := stack . pop ( )
if actual . Cmp ( expected ) != 0 {
t . Errorf ( "Testcase %d, expected %x, got %x" , i , expected , actual )
}
}
}
2019-04-04 04:19:38 -05:00
// utility function to fill the json-file with testcases
// Enable this test to generate the 'testcases_xx.json' files
2019-11-18 02:49:18 -06:00
func TestWriteExpectedValues ( t * testing . T ) {
t . Skip ( "Enable this test to create json test cases." )
2022-06-13 09:24:45 -05:00
// getResult is a convenience function to generate the expected values
getResult := func ( args [ ] * twoOperandParams , opFn executionFunc ) [ ] TwoOperandTestcase {
var (
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
stack = newstack ( )
pc = uint64 ( 0 )
interpreter = env . interpreter
)
result := make ( [ ] TwoOperandTestcase , len ( args ) )
for i , param := range args {
x := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( param . x ) )
y := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( param . y ) )
stack . push ( x )
stack . push ( y )
opFn ( & pc , interpreter , & ScopeContext { nil , stack , nil } )
actual := stack . pop ( )
result [ i ] = TwoOperandTestcase { param . x , param . y , fmt . Sprintf ( "%064x" , actual ) }
}
return result
}
2019-04-04 04:19:38 -05:00
for name , method := range twoOpMethods {
data , err := json . Marshal ( getResult ( commonParams , method ) )
if err != nil {
t . Fatal ( err )
}
2022-05-16 04:59:35 -05:00
_ = os . WriteFile ( fmt . Sprintf ( "testdata/testcases_%v.json" , name ) , data , 0644 )
2019-04-04 04:19:38 -05:00
if err != nil {
t . Fatal ( err )
}
2018-02-23 04:32:57 -06:00
}
}
2019-04-04 04:19:38 -05:00
// TestJsonTestcases runs through all the testcases defined as json-files
func TestJsonTestcases ( t * testing . T ) {
for name := range twoOpMethods {
2022-05-16 04:59:35 -05:00
data , err := os . ReadFile ( fmt . Sprintf ( "testdata/testcases_%v.json" , name ) )
2019-04-04 04:19:38 -05:00
if err != nil {
t . Fatal ( "Failed to read file" , err )
}
var testcases [ ] TwoOperandTestcase
json . Unmarshal ( data , & testcases )
testTwoOperandOp ( t , testcases , twoOpMethods [ name ] , name )
2018-02-23 04:32:57 -06:00
}
}
2020-04-07 04:45:21 -05:00
func opBenchmark ( bench * testing . B , op executionFunc , args ... string ) {
2017-06-28 04:45:45 -05:00
var (
2020-11-13 06:42:19 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
2021-03-02 15:40:57 -06:00
stack = newstack ( )
2022-05-17 02:21:43 -05:00
scope = & ScopeContext { nil , stack , nil }
2023-02-05 08:11:25 -06:00
evmInterpreter = NewEVMInterpreter ( env )
2017-06-28 04:45:45 -05:00
)
2018-07-25 07:56:39 -05:00
env . interpreter = evmInterpreter
2017-06-28 04:45:45 -05:00
// convert args
2022-05-17 02:21:43 -05:00
intArgs := make ( [ ] * uint256 . Int , len ( args ) )
2017-06-28 04:45:45 -05:00
for i , arg := range args {
2022-05-17 02:21:43 -05:00
intArgs [ i ] = new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( arg ) )
2017-06-28 04:45:45 -05:00
}
pc := uint64 ( 0 )
bench . ResetTimer ( )
for i := 0 ; i < bench . N ; i ++ {
2022-05-17 02:21:43 -05:00
for _ , arg := range intArgs {
stack . push ( arg )
2017-06-28 04:45:45 -05:00
}
2022-05-17 02:21:43 -05:00
op ( & pc , evmInterpreter , scope )
2017-06-28 04:45:45 -05:00
stack . pop ( )
}
2022-05-17 02:21:43 -05:00
bench . StopTimer ( )
for i , arg := range args {
want := new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( arg ) )
if have := intArgs [ i ] ; ! want . Eq ( have ) {
bench . Fatalf ( "input #%d mutated, have %x want %x" , i , have , want )
}
}
2017-06-28 04:45:45 -05:00
}
2017-08-14 07:26:47 -05:00
func BenchmarkOpAdd64 ( b * testing . B ) {
x := "ffffffff"
y := "fd37f3e2bba2c4f"
2017-06-28 04:45:45 -05:00
2017-08-14 07:26:47 -05:00
opBenchmark ( b , opAdd , x , y )
2017-06-28 04:45:45 -05:00
}
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
func BenchmarkOpAdd128 ( b * testing . B ) {
x := "ffffffffffffffff"
y := "f5470b43c6549b016288e9a65629687"
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
opBenchmark ( b , opAdd , x , y )
2017-06-28 04:45:45 -05:00
}
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
func BenchmarkOpAdd256 ( b * testing . B ) {
x := "0802431afcbce1fc194c9eaa417b2fb67dc75a95db0bc7ec6b1c8af11df6a1da9"
y := "a1f5aac137876480252e5dcac62c354ec0d42b76b0642b6181ed099849ea1d57"
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
opBenchmark ( b , opAdd , x , y )
2017-08-10 08:39:43 -05:00
}
2017-08-14 07:26:47 -05:00
func BenchmarkOpSub64 ( b * testing . B ) {
x := "51022b6317003a9d"
y := "a20456c62e00753a"
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
opBenchmark ( b , opSub , x , y )
2017-08-10 08:39:43 -05:00
}
2017-08-14 07:26:47 -05:00
func BenchmarkOpSub128 ( b * testing . B ) {
x := "4dde30faaacdc14d00327aac314e915d"
y := "9bbc61f5559b829a0064f558629d22ba"
2017-06-28 04:45:45 -05:00
2017-08-14 07:26:47 -05:00
opBenchmark ( b , opSub , x , y )
2017-06-28 04:45:45 -05:00
}
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
func BenchmarkOpSub256 ( b * testing . B ) {
x := "4bfcd8bb2ac462735b48a17580690283980aa2d679f091c64364594df113ea37"
y := "97f9b1765588c4e6b69142eb00d20507301545acf3e1238c86c8b29be227d46e"
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opSub , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpMul ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opMul , x , y )
}
2017-08-10 08:39:43 -05:00
2017-08-14 07:26:47 -05:00
func BenchmarkOpDiv256 ( b * testing . B ) {
x := "ff3f9014f20db29ae04af2c2d265de17"
y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
opBenchmark ( b , opDiv , x , y )
}
2017-06-28 04:45:45 -05:00
2017-08-14 07:26:47 -05:00
func BenchmarkOpDiv128 ( b * testing . B ) {
x := "fdedc7f10142ff97"
y := "fbdfda0e2ce356173d1993d5f70a2b11"
opBenchmark ( b , opDiv , x , y )
}
func BenchmarkOpDiv64 ( b * testing . B ) {
x := "fcb34eb3"
y := "f97180878e839129"
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opDiv , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpSdiv ( b * testing . B ) {
2017-08-14 07:26:47 -05:00
x := "ff3f9014f20db29ae04af2c2d265de17"
y := "fe7fb0d1f59dfe9492ffbf73683fd1e870eec79504c60144cc7f5fc2bad1e611"
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opSdiv , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpMod ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opMod , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpSmod ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opSmod , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpExp ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opExp , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpSignExtend ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opSignExtend , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpLt ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opLt , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpGt ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opGt , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpSlt ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opSlt , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpSgt ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opSgt , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpEq ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opEq , x , y )
}
2018-03-08 06:48:19 -06:00
func BenchmarkOpEq2 ( b * testing . B ) {
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
y := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201fffffffe"
opBenchmark ( b , opEq , x , y )
}
2017-06-28 04:45:45 -05:00
func BenchmarkOpAnd ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opAnd , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpOr ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opOr , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpXor ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opXor , x , y )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpByte ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opByte , x , y )
}
func BenchmarkOpAddmod ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
z := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opAddmod , x , y , z )
}
2017-08-10 08:39:43 -05:00
2017-06-28 04:45:45 -05:00
func BenchmarkOpMulmod ( b * testing . B ) {
2021-04-30 05:49:13 -05:00
x := alphabetSoup
y := alphabetSoup
z := alphabetSoup
2017-06-28 04:45:45 -05:00
opBenchmark ( b , opMulmod , x , y , z )
}
2018-02-23 04:32:57 -06:00
func BenchmarkOpSHL ( b * testing . B ) {
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
y := "ff"
opBenchmark ( b , opSHL , x , y )
}
func BenchmarkOpSHR ( b * testing . B ) {
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
y := "ff"
opBenchmark ( b , opSHR , x , y )
}
func BenchmarkOpSAR ( b * testing . B ) {
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
y := "ff"
opBenchmark ( b , opSAR , x , y )
}
2018-03-08 06:48:19 -06:00
func BenchmarkOpIsZero ( b * testing . B ) {
x := "FBCDEF090807060504030201ffffffffFBCDEF090807060504030201ffffffff"
opBenchmark ( b , opIszero , x )
}
2018-06-14 04:23:37 -05:00
func TestOpMstore ( t * testing . T ) {
var (
2020-11-13 06:42:19 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
2021-03-02 15:40:57 -06:00
stack = newstack ( )
2018-07-25 07:56:39 -05:00
mem = NewMemory ( )
2023-02-05 08:11:25 -06:00
evmInterpreter = NewEVMInterpreter ( env )
2018-06-14 04:23:37 -05:00
)
2018-07-25 07:56:39 -05:00
env . interpreter = evmInterpreter
2018-06-14 04:23:37 -05:00
mem . Resize ( 64 )
pc := uint64 ( 0 )
v := "abcdef00000000000000abba000000000deaf000000c0de00100000000133700"
2021-12-03 03:16:49 -06:00
stack . push ( new ( uint256 . Int ) . SetBytes ( common . Hex2Bytes ( v ) ) )
stack . push ( new ( uint256 . Int ) )
2021-03-25 04:13:14 -05:00
opMstore ( & pc , evmInterpreter , & ScopeContext { mem , stack , nil } )
2019-11-04 03:31:10 -06:00
if got := common . Bytes2Hex ( mem . GetCopy ( 0 , 32 ) ) ; got != v {
2018-06-14 04:23:37 -05:00
t . Fatalf ( "Mstore fail, got %v, expected %v" , got , v )
}
2021-12-03 03:16:49 -06:00
stack . push ( new ( uint256 . Int ) . SetUint64 ( 0x1 ) )
stack . push ( new ( uint256 . Int ) )
2021-03-25 04:13:14 -05:00
opMstore ( & pc , evmInterpreter , & ScopeContext { mem , stack , nil } )
2019-11-04 03:31:10 -06:00
if common . Bytes2Hex ( mem . GetCopy ( 0 , 32 ) ) != "0000000000000000000000000000000000000000000000000000000000000001" {
2018-06-14 04:23:37 -05:00
t . Fatalf ( "Mstore failed to overwrite previous value" )
}
}
func BenchmarkOpMstore ( bench * testing . B ) {
var (
2020-11-13 06:42:19 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
2021-03-02 15:40:57 -06:00
stack = newstack ( )
2018-07-25 07:56:39 -05:00
mem = NewMemory ( )
2023-02-05 08:11:25 -06:00
evmInterpreter = NewEVMInterpreter ( env )
2018-06-14 04:23:37 -05:00
)
2018-07-25 07:56:39 -05:00
env . interpreter = evmInterpreter
2018-06-14 04:23:37 -05:00
mem . Resize ( 64 )
pc := uint64 ( 0 )
2020-06-08 07:24:40 -05:00
memStart := new ( uint256 . Int )
value := new ( uint256 . Int ) . SetUint64 ( 0x1337 )
2018-06-14 04:23:37 -05:00
bench . ResetTimer ( )
for i := 0 ; i < bench . N ; i ++ {
2021-12-03 03:16:49 -06:00
stack . push ( value )
stack . push ( memStart )
2021-03-25 04:13:14 -05:00
opMstore ( & pc , evmInterpreter , & ScopeContext { mem , stack , nil } )
2018-06-14 04:23:37 -05:00
}
}
2018-10-05 02:32:35 -05:00
2022-11-16 03:18:52 -06:00
func TestOpTstore ( t * testing . T ) {
var (
2023-05-11 02:19:42 -05:00
statedb , _ = state . New ( types . EmptyRootHash , state . NewDatabase ( rawdb . NewMemoryDatabase ( ) ) , nil )
2022-11-16 03:18:52 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , statedb , params . TestChainConfig , Config { } )
stack = newstack ( )
mem = NewMemory ( )
2023-02-05 08:11:25 -06:00
evmInterpreter = NewEVMInterpreter ( env )
2022-11-16 03:18:52 -06:00
caller = common . Address { }
to = common . Address { 1 }
contractRef = contractRef { caller }
contract = NewContract ( contractRef , AccountRef ( to ) , new ( big . Int ) , 0 )
scopeContext = ScopeContext { mem , stack , contract }
value = common . Hex2Bytes ( "abcdef00000000000000abba000000000deaf000000c0de00100000000133700" )
)
// Add a stateObject for the caller and the contract being called
statedb . CreateAccount ( caller )
statedb . CreateAccount ( to )
env . interpreter = evmInterpreter
pc := uint64 ( 0 )
// push the value to the stack
stack . push ( new ( uint256 . Int ) . SetBytes ( value ) )
// push the location to the stack
stack . push ( new ( uint256 . Int ) )
opTstore ( & pc , evmInterpreter , & scopeContext )
// there should be no elements on the stack after TSTORE
if stack . len ( ) != 0 {
t . Fatal ( "stack wrong size" )
}
// push the location to the stack
stack . push ( new ( uint256 . Int ) )
opTload ( & pc , evmInterpreter , & scopeContext )
// there should be one element on the stack after TLOAD
if stack . len ( ) != 1 {
t . Fatal ( "stack wrong size" )
}
val := stack . peek ( )
if ! bytes . Equal ( val . Bytes ( ) , value ) {
t . Fatal ( "incorrect element read from transient storage" )
}
}
2021-11-30 03:34:34 -06:00
func BenchmarkOpKeccak256 ( bench * testing . B ) {
2018-10-08 06:14:29 -05:00
var (
2020-11-13 06:42:19 -06:00
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
2021-03-02 15:40:57 -06:00
stack = newstack ( )
2018-10-08 06:14:29 -05:00
mem = NewMemory ( )
2023-02-05 08:11:25 -06:00
evmInterpreter = NewEVMInterpreter ( env )
2018-10-08 06:14:29 -05:00
)
env . interpreter = evmInterpreter
mem . Resize ( 32 )
pc := uint64 ( 0 )
2021-05-10 05:29:33 -05:00
start := new ( uint256 . Int )
2018-10-08 06:14:29 -05:00
bench . ResetTimer ( )
for i := 0 ; i < bench . N ; i ++ {
2021-12-03 03:16:49 -06:00
stack . push ( uint256 . NewInt ( 32 ) )
stack . push ( start )
2021-11-30 03:34:34 -06:00
opKeccak256 ( & pc , evmInterpreter , & ScopeContext { mem , stack , nil } )
2018-10-08 06:14:29 -05:00
}
}
2018-10-05 02:32:35 -05:00
func TestCreate2Addreses ( t * testing . T ) {
type testcase struct {
origin string
salt string
code string
expected string
}
for i , tt := range [ ] testcase {
{
origin : "0x0000000000000000000000000000000000000000" ,
salt : "0x0000000000000000000000000000000000000000" ,
code : "0x00" ,
expected : "0x4d1a2e2bb4f88f0250f26ffff098b0b30b26bf38" ,
} ,
{
origin : "0xdeadbeef00000000000000000000000000000000" ,
salt : "0x0000000000000000000000000000000000000000" ,
code : "0x00" ,
expected : "0xB928f69Bb1D91Cd65274e3c79d8986362984fDA3" ,
} ,
{
origin : "0xdeadbeef00000000000000000000000000000000" ,
salt : "0xfeed000000000000000000000000000000000000" ,
code : "0x00" ,
expected : "0xD04116cDd17beBE565EB2422F2497E06cC1C9833" ,
} ,
{
origin : "0x0000000000000000000000000000000000000000" ,
salt : "0x0000000000000000000000000000000000000000" ,
code : "0xdeadbeef" ,
expected : "0x70f2b2914A2a4b783FaEFb75f459A580616Fcb5e" ,
} ,
{
origin : "0x00000000000000000000000000000000deadbeef" ,
salt : "0xcafebabe" ,
code : "0xdeadbeef" ,
expected : "0x60f3f640a8508fC6a86d45DF051962668E1e8AC7" ,
} ,
{
origin : "0x00000000000000000000000000000000deadbeef" ,
salt : "0xcafebabe" ,
code : "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ,
expected : "0x1d8bfDC5D46DC4f61D6b6115972536eBE6A8854C" ,
} ,
{
origin : "0x0000000000000000000000000000000000000000" ,
salt : "0x0000000000000000000000000000000000000000" ,
code : "0x" ,
expected : "0xE33C0C7F7df4809055C3ebA6c09CFe4BaF1BD9e0" ,
} ,
} {
origin := common . BytesToAddress ( common . FromHex ( tt . origin ) )
salt := common . BytesToHash ( common . FromHex ( tt . salt ) )
code := common . FromHex ( tt . code )
2018-10-05 12:02:06 -05:00
codeHash := crypto . Keccak256 ( code )
address := crypto . CreateAddress2 ( origin , salt , codeHash )
2018-10-05 02:32:35 -05:00
/ *
stack := newstack ( )
// salt, but we don't need that for this test
stack . push ( big . NewInt ( int64 ( len ( code ) ) ) ) //size
stack . push ( big . NewInt ( 0 ) ) // memstart
stack . push ( big . NewInt ( 0 ) ) // value
gas , _ := gasCreate2 ( params . GasTable { } , nil , nil , stack , nil , 0 )
fmt . Printf ( "Example %d\n* address `0x%x`\n* salt `0x%x`\n* init_code `0x%x`\n* gas (assuming no mem expansion): `%v`\n* result: `%s`\n\n" , i , origin , salt , code , gas , address . String ( ) )
* /
expected := common . BytesToAddress ( common . FromHex ( tt . expected ) )
if ! bytes . Equal ( expected . Bytes ( ) , address . Bytes ( ) ) {
t . Errorf ( "test %d: expected %s, got %s" , i , expected . String ( ) , address . String ( ) )
}
}
}
2022-01-10 01:44:21 -06:00
func TestRandom ( t * testing . T ) {
type testcase struct {
name string
random common . Hash
}
for _ , tt := range [ ] testcase {
{ name : "empty hash" , random : common . Hash { } } ,
{ name : "1" , random : common . Hash { 0 } } ,
2023-07-15 10:45:57 -05:00
{ name : "emptyCodeHash" , random : types . EmptyCodeHash } ,
2022-01-10 01:44:21 -06:00
{ name : "hash(0x010203)" , random : crypto . Keccak256Hash ( [ ] byte { 0x01 , 0x02 , 0x03 } ) } ,
} {
var (
env = NewEVM ( BlockContext { Random : & tt . random } , TxContext { } , nil , params . TestChainConfig , Config { } )
stack = newstack ( )
pc = uint64 ( 0 )
evmInterpreter = env . interpreter
)
opRandom ( & pc , evmInterpreter , & ScopeContext { nil , stack , nil } )
if len ( stack . data ) != 1 {
t . Errorf ( "Expected one item on stack after %v, got %d: " , tt . name , len ( stack . data ) )
}
actual := stack . pop ( )
expected , overflow := uint256 . FromBig ( new ( big . Int ) . SetBytes ( tt . random . Bytes ( ) ) )
if overflow {
t . Errorf ( "Testcase %v: invalid overflow" , tt . name )
}
if actual . Cmp ( expected ) != 0 {
t . Errorf ( "Testcase %v: expected %x, got %x" , tt . name , expected , actual )
}
}
}
2023-06-05 08:43:25 -05:00
func TestBlobHash ( t * testing . T ) {
type testcase struct {
name string
idx uint64
expect common . Hash
hashes [ ] common . Hash
}
var (
zero = common . Hash { 0 }
one = common . Hash { 1 }
two = common . Hash { 2 }
three = common . Hash { 3 }
)
for _ , tt := range [ ] testcase {
{ name : "[{1}]" , idx : 0 , expect : one , hashes : [ ] common . Hash { one } } ,
{ name : "[1,{2},3]" , idx : 2 , expect : three , hashes : [ ] common . Hash { one , two , three } } ,
{ name : "out-of-bounds (empty)" , idx : 10 , expect : zero , hashes : [ ] common . Hash { } } ,
{ name : "out-of-bounds" , idx : 25 , expect : zero , hashes : [ ] common . Hash { one , two , three } } ,
{ name : "out-of-bounds (nil)" , idx : 25 , expect : zero , hashes : nil } ,
} {
var (
env = NewEVM ( BlockContext { } , TxContext { BlobHashes : tt . hashes } , nil , params . TestChainConfig , Config { } )
stack = newstack ( )
pc = uint64 ( 0 )
evmInterpreter = env . interpreter
)
stack . push ( uint256 . NewInt ( tt . idx ) )
opBlobHash ( & pc , evmInterpreter , & ScopeContext { nil , stack , nil } )
if len ( stack . data ) != 1 {
t . Errorf ( "Expected one item on stack after %v, got %d: " , tt . name , len ( stack . data ) )
}
actual := stack . pop ( )
expected , overflow := uint256 . FromBig ( new ( big . Int ) . SetBytes ( tt . expect . Bytes ( ) ) )
if overflow {
t . Errorf ( "Testcase %v: invalid overflow" , tt . name )
}
if actual . Cmp ( expected ) != 0 {
t . Errorf ( "Testcase %v: expected %x, got %x" , tt . name , expected , actual )
}
}
}
2023-07-11 02:55:34 -05:00
func TestOpMCopy ( t * testing . T ) {
// Test cases from https://eips.ethereum.org/EIPS/eip-5656#test-cases
for i , tc := range [ ] struct {
dst , src , len string
pre string
want string
wantGas uint64
} {
{ // MCOPY 0 32 32 - copy 32 bytes from offset 32 to offset 0.
dst : "0x0" , src : "0x20" , len : "0x20" ,
pre : "0000000000000000000000000000000000000000000000000000000000000000 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" ,
want : "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" ,
wantGas : 6 ,
} ,
{ // MCOPY 0 0 32 - copy 32 bytes from offset 0 to offset 0.
dst : "0x0" , src : "0x0" , len : "0x20" ,
pre : "0101010101010101010101010101010101010101010101010101010101010101" ,
want : "0101010101010101010101010101010101010101010101010101010101010101" ,
wantGas : 6 ,
} ,
{ // MCOPY 0 1 8 - copy 8 bytes from offset 1 to offset 0 (overlapping).
dst : "0x0" , src : "0x1" , len : "0x8" ,
pre : "000102030405060708 000000000000000000000000000000000000000000000000" ,
want : "010203040506070808 000000000000000000000000000000000000000000000000" ,
wantGas : 6 ,
} ,
{ // MCOPY 1 0 8 - copy 8 bytes from offset 0 to offset 1 (overlapping).
dst : "0x1" , src : "0x0" , len : "0x8" ,
pre : "000102030405060708 000000000000000000000000000000000000000000000000" ,
want : "000001020304050607 000000000000000000000000000000000000000000000000" ,
wantGas : 6 ,
} ,
// Tests below are not in the EIP, but maybe should be added
{ // MCOPY 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds index(overlapping).
dst : "0xFFFFFFFFFFFF" , src : "0xFFFFFFFFFFFF" , len : "0x0" ,
pre : "11" ,
want : "11" ,
wantGas : 3 ,
} ,
{ // MCOPY 0xFFFFFFFFFFFF 0 0 - copy zero bytes from start of mem to out-of-bounds.
dst : "0xFFFFFFFFFFFF" , src : "0x0" , len : "0x0" ,
pre : "11" ,
want : "11" ,
wantGas : 3 ,
} ,
{ // MCOPY 0 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds to start of mem
dst : "0x0" , src : "0xFFFFFFFFFFFF" , len : "0x0" ,
pre : "11" ,
want : "11" ,
wantGas : 3 ,
} ,
{ // MCOPY - copy 1 from space outside of uint64 space
dst : "0x0" , src : "0x10000000000000000" , len : "0x1" ,
pre : "0" ,
} ,
{ // MCOPY - copy 1 from 0 to space outside of uint64
dst : "0x10000000000000000" , src : "0x0" , len : "0x1" ,
pre : "0" ,
} ,
{ // MCOPY - copy nothing from 0 to space outside of uint64
dst : "0x10000000000000000" , src : "0x0" , len : "0x0" ,
pre : "" ,
want : "" ,
wantGas : 3 ,
} ,
{ // MCOPY - copy 1 from 0x20 to 0x10, with no prior allocated mem
dst : "0x10" , src : "0x20" , len : "0x1" ,
pre : "" ,
// 64 bytes
want : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
wantGas : 12 ,
} ,
{ // MCOPY - copy 1 from 0x19 to 0x10, with no prior allocated mem
dst : "0x10" , src : "0x19" , len : "0x1" ,
pre : "" ,
// 32 bytes
want : "0x0000000000000000000000000000000000000000000000000000000000000000" ,
wantGas : 9 ,
} ,
} {
var (
env = NewEVM ( BlockContext { } , TxContext { } , nil , params . TestChainConfig , Config { } )
stack = newstack ( )
pc = uint64 ( 0 )
evmInterpreter = env . interpreter
)
data := common . FromHex ( strings . ReplaceAll ( tc . pre , " " , "" ) )
// Set pre
mem := NewMemory ( )
mem . Resize ( uint64 ( len ( data ) ) )
mem . Set ( 0 , uint64 ( len ( data ) ) , data )
// Push stack args
len , _ := uint256 . FromHex ( tc . len )
src , _ := uint256 . FromHex ( tc . src )
dst , _ := uint256 . FromHex ( tc . dst )
stack . push ( len )
stack . push ( src )
stack . push ( dst )
wantErr := ( tc . wantGas == 0 )
// Calc mem expansion
var memorySize uint64
if memSize , overflow := memoryMcopy ( stack ) ; overflow {
if wantErr {
continue
}
t . Errorf ( "overflow" )
} else {
var overflow bool
if memorySize , overflow = math . SafeMul ( toWordSize ( memSize ) , 32 ) ; overflow {
t . Error ( ErrGasUintOverflow )
}
}
// and the dynamic cost
var haveGas uint64
if dynamicCost , err := gasMcopy ( env , nil , stack , mem , memorySize ) ; err != nil {
t . Error ( err )
} else {
haveGas = GasFastestStep + dynamicCost
}
// Expand mem
if memorySize > 0 {
mem . Resize ( memorySize )
}
// Do the copy
opMcopy ( & pc , evmInterpreter , & ScopeContext { mem , stack , nil } )
want := common . FromHex ( strings . ReplaceAll ( tc . want , " " , "" ) )
if have := mem . store ; ! bytes . Equal ( want , have ) {
t . Errorf ( "case %d: \nwant: %#x\nhave: %#x\n" , i , want , have )
}
wantGas := tc . wantGas
if haveGas != wantGas {
t . Errorf ( "case %d: gas wrong, want %d have %d\n" , i , wantGas , haveGas )
}
}
}