aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitattributes2
-rw-r--r--Makefile6
-rw-r--r--cmd/evm/main.go29
-rw-r--r--cmd/geth/main.go34
-rw-r--r--cmd/utils/customflags.go9
-rw-r--r--cmd/utils/customflags_test.go5
-rw-r--r--cmd/utils/flags.go33
-rw-r--r--common/compiler/solidity_test.go5
-rw-r--r--common/docserver/docserver.go1
-rw-r--r--common/docserver/docserver_test.go19
-rw-r--r--common/path.go11
-rw-r--r--common/path_test.go52
-rw-r--r--common/size_test.go2
-rw-r--r--core/block_processor.go2
-rw-r--r--core/chain_manager.go2
-rw-r--r--core/execution.go27
-rw-r--r--core/vm/context.go1
-rw-r--r--core/vm/contracts.go2
-rw-r--r--core/vm/environment.go1
-rw-r--r--core/vm/gas.go4
-rw-r--r--core/vm/instructions.go537
-rw-r--r--core/vm/jit.go541
-rw-r--r--core/vm/jit_test.go122
-rw-r--r--core/vm/settings.go25
-rw-r--r--core/vm/stack.go32
-rw-r--r--core/vm/vm.go125
-rw-r--r--core/vm_env.go4
-rw-r--r--eth/backend.go9
-rw-r--r--jsre/ethereum_js.go4
-rw-r--r--jsre/jsre_test.go30
-rw-r--r--miner/miner.go20
-rw-r--r--miner/remote_agent.go37
-rw-r--r--p2p/discover/table.go4
-rw-r--r--p2p/discover/table_test.go8
-rw-r--r--p2p/nat/natupnp_test.go5
-rw-r--r--rpc/api/eth.go10
-rw-r--r--rpc/api/eth_args.go31
-rw-r--r--rpc/api/miner.go8
-rw-r--r--tests/state_test.go17
-rw-r--r--tests/state_test_util.go56
-rw-r--r--tests/util.go14
-rw-r--r--tests/vm_test.go14
-rw-r--r--tests/vm_test_util.go74
-rw-r--r--trie/encoding.go57
-rw-r--r--trie/encoding_test.go68
-rw-r--r--trie/iterator.go2
-rw-r--r--trie/shortnode.go4
-rw-r--r--trie/trie.go10
-rw-r--r--xeth/xeth.go11
-rw-r--r--xeth/xeth_test.go26
50 files changed, 1883 insertions, 269 deletions
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 000000000..dfe077042
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+# Auto detect text files and perform LF normalization
+* text=auto
diff --git a/Makefile b/Makefile
index 03e3bf4c6..3478b5433 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
# with Go source code. If you know what GOPATH is then you probably
# don't need to bother with make.
-.PHONY: geth mist all test travis-test-with-coverage clean
+.PHONY: geth evm mist all test travis-test-with-coverage clean
GOBIN = build/bin
geth:
@@ -10,6 +10,10 @@ geth:
@echo "Done building."
@echo "Run \"$(GOBIN)/geth\" to launch geth."
+evm:
+ build/env.sh $(GOROOT)/bin/go install -v $(shell build/ldflags.sh) ./cmd/evm
+ @echo "Done building."
+ @echo "Run \"$(GOBIN)/evm to start the evm."
mist:
build/env.sh go install -v $(shell build/ldflags.sh) ./cmd/mist
@echo "Done building."
diff --git a/cmd/evm/main.go b/cmd/evm/main.go
index 965994382..be6546c95 100644
--- a/cmd/evm/main.go
+++ b/cmd/evm/main.go
@@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
+ "github.com/ethereum/go-ethereum/logger/glog"
)
var (
@@ -40,6 +41,14 @@ var (
Name: "debug",
Usage: "output full trace logs",
}
+ ForceJitFlag = cli.BoolFlag{
+ Name: "forcejit",
+ Usage: "forces jit compilation",
+ }
+ DisableJitFlag = cli.BoolFlag{
+ Name: "nojit",
+ Usage: "disabled jit compilation",
+ }
CodeFlag = cli.StringFlag{
Name: "code",
Usage: "EVM code",
@@ -77,6 +86,8 @@ func init() {
app = utils.NewApp("0.2", "the evm command line interface")
app.Flags = []cli.Flag{
DebugFlag,
+ ForceJitFlag,
+ DisableJitFlag,
SysStatFlag,
CodeFlag,
GasFlag,
@@ -90,6 +101,10 @@ func init() {
func run(ctx *cli.Context) {
vm.Debug = ctx.GlobalBool(DebugFlag.Name)
+ vm.ForceJit = ctx.GlobalBool(ForceJitFlag.Name)
+ vm.DisableJit = ctx.GlobalBool(DisableJitFlag.Name)
+
+ glog.SetToStderr(true)
db, _ := ethdb.NewMemDatabase()
statedb := state.New(common.Hash{}, db)
@@ -110,11 +125,6 @@ func run(ctx *cli.Context) {
)
vmdone := time.Since(tstart)
- if e != nil {
- fmt.Println(e)
- os.Exit(1)
- }
-
if ctx.GlobalBool(DumpFlag.Name) {
fmt.Println(string(statedb.Dump()))
}
@@ -133,7 +143,11 @@ num gc: %d
`, mem.Alloc, mem.TotalAlloc, mem.Mallocs, mem.HeapAlloc, mem.HeapObjects, mem.NumGC)
}
- fmt.Printf("OUT: 0x%x\n", ret)
+ fmt.Printf("OUT: 0x%x", ret)
+ if e != nil {
+ fmt.Printf(" error: %v", e)
+ }
+ fmt.Println()
}
func main() {
@@ -192,6 +206,9 @@ func (self *VMEnv) StructLogs() []vm.StructLog {
func (self *VMEnv) AddLog(log *state.Log) {
self.state.AddLog(log)
}
+func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
+ return from.Balance().Cmp(balance) >= 0
+}
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
return vm.Transfer(from, to, amount)
}
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 74f4e90c3..895e55b44 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -42,6 +42,8 @@ import (
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/metrics"
+ "github.com/ethereum/go-ethereum/params"
+ "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/comms"
"github.com/mattn/go-colorable"
@@ -49,8 +51,11 @@ import (
)
const (
- ClientIdentifier = "Geth"
+ ClientIdentifier = "Geth "
Version = "1.0.1"
+ VersionMajor = 1
+ VersionMinor = 0
+ VersionPatch = 1
)
var (
@@ -307,6 +312,9 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.ExecFlag,
utils.WhisperEnabledFlag,
utils.VMDebugFlag,
+ utils.VMForceJitFlag,
+ utils.VMJitCacheFlag,
+ utils.VMEnableJitFlag,
utils.NetworkIdFlag,
utils.RPCCORSDomainFlag,
utils.VerbosityFlag,
@@ -328,6 +336,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
}
app.Before = func(ctx *cli.Context) error {
utils.SetupLogger(ctx)
+ utils.SetupVM(ctx)
if ctx.GlobalBool(utils.PProfEanbledFlag.Name) {
utils.StartPProf(ctx)
}
@@ -346,6 +355,27 @@ func main() {
}
}
+func makeDefaultExtra() []byte {
+ var clientInfo = struct {
+ Version uint
+ Name string
+ GoVersion string
+ Os string
+ }{uint(VersionMajor<<16 | VersionMinor<<8 | VersionPatch), ClientIdentifier, runtime.Version(), runtime.GOOS}
+ extra, err := rlp.EncodeToBytes(clientInfo)
+ if err != nil {
+ glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
+ }
+
+ if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
+ glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
+ glog.V(logger.Debug).Infof("extra: %x\n", extra)
+ return nil
+ }
+
+ return extra
+}
+
func run(ctx *cli.Context) {
utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name))
if ctx.GlobalBool(utils.OlympicFlag.Name) {
@@ -353,6 +383,8 @@ func run(ctx *cli.Context) {
}
cfg := utils.MakeEthConfig(ClientIdentifier, nodeNameVersion, ctx)
+ cfg.ExtraData = makeDefaultExtra()
+
ethereum, err := eth.New(cfg)
if err != nil {
utils.Fatalf("%v", err)
diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go
index e7efed4e3..4450065c1 100644
--- a/cmd/utils/customflags.go
+++ b/cmd/utils/customflags.go
@@ -21,7 +21,7 @@ import (
"fmt"
"os"
"os/user"
- "path/filepath"
+ "path"
"strings"
"github.com/codegangsta/cli"
@@ -138,11 +138,8 @@ func (self *DirectoryFlag) Set(value string) {
func expandPath(p string) string {
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
if user, err := user.Current(); err == nil {
- if err == nil {
- p = strings.Replace(p, "~", user.HomeDir, 1)
- }
+ p = user.HomeDir + p[1:]
}
}
-
- return filepath.Clean(os.ExpandEnv(p))
+ return path.Clean(os.ExpandEnv(p))
}
diff --git a/cmd/utils/customflags_test.go b/cmd/utils/customflags_test.go
index 0fb0af63b..de39ca36a 100644
--- a/cmd/utils/customflags_test.go
+++ b/cmd/utils/customflags_test.go
@@ -23,18 +23,15 @@ import (
)
func TestPathExpansion(t *testing.T) {
-
user, _ := user.Current()
-
tests := map[string]string{
"/home/someuser/tmp": "/home/someuser/tmp",
"~/tmp": user.HomeDir + "/tmp",
+ "~thisOtherUser/b/": "~thisOtherUser/b",
"$DDDXXX/a/b": "/tmp/a/b",
"/a/b/": "/a/b",
}
-
os.Setenv("DDDXXX", "/tmp")
-
for test, expected := range tests {
got := expandPath(test)
if got != expected {
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 815d48124..e7b30cfa0 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -27,6 +27,7 @@ import (
"runtime"
"strconv"
+ "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/metrics"
"github.com/codegangsta/cli"
@@ -172,6 +173,25 @@ var (
Value: "",
}
+ // vm flags
+ VMDebugFlag = cli.BoolFlag{
+ Name: "vmdebug",
+ Usage: "Virtual Machine debug output",
+ }
+ VMForceJitFlag = cli.BoolFlag{
+ Name: "forcejit",
+ Usage: "Force the JIT VM to take precedence",
+ }
+ VMJitCacheFlag = cli.IntFlag{
+ Name: "jitcache",
+ Usage: "Amount of cached JIT VM programs",
+ Value: 64,
+ }
+ VMEnableJitFlag = cli.BoolFlag{
+ Name: "jitvm",
+ Usage: "Enable the JIT VM",
+ }
+
// logging and debug settings
LogFileFlag = cli.StringFlag{
Name: "logfile",
@@ -196,10 +216,6 @@ var (
Usage: "The syntax of the argument is a comma-separated list of pattern=N, where pattern is a literal file name (minus the \".go\" suffix) or \"glob\" pattern and N is a log verbosity level.",
Value: glog.GetVModule(),
}
- VMDebugFlag = cli.BoolFlag{
- Name: "vmdebug",
- Usage: "Virtual Machine debug output",
- }
BacktraceAtFlag = cli.GenericFlag{
Name: "backtrace_at",
Usage: "If set to a file and line number (e.g., \"block.go:271\") holding a logging statement, a stack trace will be logged",
@@ -434,6 +450,13 @@ func SetupLogger(ctx *cli.Context) {
glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
}
+// SetupVM configured the VM package's global settings
+func SetupVM(ctx *cli.Context) {
+ vm.DisableJit = !ctx.GlobalBool(VMEnableJitFlag.Name)
+ vm.ForceJit = ctx.GlobalBool(VMForceJitFlag.Name)
+ vm.SetJITCacheSize(ctx.GlobalInt(VMJitCacheFlag.Name))
+}
+
// MakeChain creates a chain manager from set command line flags.
func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) {
datadir := ctx.GlobalString(DataDirFlag.Name)
@@ -478,7 +501,7 @@ func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
}
func IpcSocketPath(ctx *cli.Context) (ipcpath string) {
- if common.IsWindows() {
+ if runtime.GOOS == "windows" {
ipcpath = common.DefaultIpcPath()
if ctx.GlobalIsSet(IPCPathFlag.Name) {
ipcpath = ctx.GlobalString(IPCPathFlag.Name)
diff --git a/common/compiler/solidity_test.go b/common/compiler/solidity_test.go
index 8255e8e2d..3303bc15a 100644
--- a/common/compiler/solidity_test.go
+++ b/common/compiler/solidity_test.go
@@ -20,6 +20,7 @@ import (
"encoding/json"
"io/ioutil"
"os"
+ "path"
"testing"
"github.com/ethereum/go-ethereum/common"
@@ -94,7 +95,7 @@ func TestSaveInfo(t *testing.T) {
if err != nil {
t.Errorf("%v", err)
}
- filename := "/tmp/solctest.info.json"
+ filename := path.Join(os.TempDir(), "solctest.info.json")
os.Remove(filename)
cinfohash, err := SaveInfo(&cinfo, filename)
if err != nil {
@@ -110,4 +111,4 @@ func TestSaveInfo(t *testing.T) {
if cinfohash != infohash {
t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex())
}
-}
+} \ No newline at end of file
diff --git a/common/docserver/docserver.go b/common/docserver/docserver.go
index fa120fb38..dac542ba7 100644
--- a/common/docserver/docserver.go
+++ b/common/docserver/docserver.go
@@ -38,7 +38,6 @@ func New(docRoot string) (self *DocServer) {
DocRoot: docRoot,
schemes: []string{"file"},
}
- self.DocRoot = "/tmp/"
self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot)))
return
}
diff --git a/common/docserver/docserver_test.go b/common/docserver/docserver_test.go
index 92e39d167..632603add 100644
--- a/common/docserver/docserver_test.go
+++ b/common/docserver/docserver_test.go
@@ -20,6 +20,7 @@ import (
"io/ioutil"
"net/http"
"os"
+ "path"
"testing"
"github.com/ethereum/go-ethereum/common"
@@ -27,12 +28,18 @@ import (
)
func TestGetAuthContent(t *testing.T) {
- text := "test"
- hash := common.Hash{}
- copy(hash[:], crypto.Sha3([]byte(text)))
- ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm)
+ dir, err := ioutil.TempDir("", "docserver-test")
+ if err != nil {
+ t.Fatal("cannot create temporary directory:", err)
+ }
+ defer os.RemoveAll(dir)
+ ds := New(dir)
- ds := New("/tmp/")
+ text := "test"
+ hash := crypto.Sha3Hash([]byte(text))
+ if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil {
+ t.Fatal("could not write test file", err)
+ }
content, err := ds.GetAuthContent("file:///test.content", hash)
if err != nil {
t.Errorf("no error expected, got %v", err)
@@ -67,4 +74,4 @@ func TestRegisterScheme(t *testing.T) {
if !ds.HasScheme("scheme") {
t.Errorf("expected scheme to be registered")
}
-}
+} \ No newline at end of file
diff --git a/common/path.go b/common/path.go
index 0d7adb961..8b3c7d14b 100644
--- a/common/path.go
+++ b/common/path.go
@@ -116,14 +116,3 @@ func DefaultIpcPath() string {
}
return filepath.Join(DefaultDataDir(), "geth.ipc")
}
-
-func IsWindows() bool {
- return runtime.GOOS == "windows"
-}
-
-func WindonizePath(path string) string {
- if string(path[0]) == "/" && IsWindows() {
- path = path[1:]
- }
- return path
-}
diff --git a/common/path_test.go b/common/path_test.go
deleted file mode 100644
index 71ffd5fe1..000000000
--- a/common/path_test.go
+++ /dev/null
@@ -1,52 +0,0 @@
-// Copyright 2014 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/>.
-
-package common
-
-import (
- "os"
- // "testing"
-
- checker "gopkg.in/check.v1"
-)
-
-type CommonSuite struct{}
-
-var _ = checker.Suite(&CommonSuite{})
-
-func (s *CommonSuite) TestOS(c *checker.C) {
- expwin := (os.PathSeparator == '\\' && os.PathListSeparator == ';')
- res := IsWindows()
-
- if !expwin {
- c.Assert(res, checker.Equals, expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
- } else {
- c.Assert(res, checker.Not(checker.Equals), expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator))
- }
-}
-
-func (s *CommonSuite) TestWindonziePath(c *checker.C) {
- iswindowspath := os.PathSeparator == '\\'
- path := "/opt/eth/test/file.ext"
- res := WindonizePath(path)
- ressep := string(res[0])
-
- if !iswindowspath {
- c.Assert(ressep, checker.Equals, "/")
- } else {
- c.Assert(ressep, checker.Not(checker.Equals), "/")
- }
-}
diff --git a/common/size_test.go b/common/size_test.go
index 8709a0237..ce19cab69 100644
--- a/common/size_test.go
+++ b/common/size_test.go
@@ -40,7 +40,7 @@ func (s *SizeSuite) TestStorageSizeString(c *checker.C) {
c.Assert(StorageSize(data3).String(), checker.Equals, exp3)
}
-func (s *CommonSuite) TestCommon(c *checker.C) {
+func (s *SizeSuite) TestCommon(c *checker.C) {
ether := CurrencyToString(BigPow(10, 19))
finney := CurrencyToString(BigPow(10, 16))
szabo := CurrencyToString(BigPow(10, 13))
diff --git a/core/block_processor.go b/core/block_processor.go
index 5a2ad8377..6ed1bc8ef 100644
--- a/core/block_processor.go
+++ b/core/block_processor.go
@@ -84,8 +84,6 @@ func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block
}
func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *big.Int, transientProcess bool) (*types.Receipt, *big.Int, error) {
- // If we are mining this block and validating we want to set the logs back to 0
-
cb := statedb.GetStateObject(coinbase.Address())
_, gas, err := ApplyMessage(NewEnv(statedb, self.bc, tx, header), tx, cb)
if err != nil {
diff --git a/core/chain_manager.go b/core/chain_manager.go
index fc1d1304f..1b792933c 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -632,7 +632,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
switch status {
case CanonStatTy:
if glog.V(logger.Debug) {
- glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
+ glog.Infof("[%v] inserted block #%d (%d TXs %v G %d UNCs) (%x...). Took %v\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), block.GasUsed(), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))
}
queue[i] = ChainEvent{block, block.Hash(), logs}
queueEvent.canonicalCount++
diff --git a/core/execution.go b/core/execution.go
index 699bad9a3..3a136515d 100644
--- a/core/execution.go
+++ b/core/execution.go
@@ -26,6 +26,7 @@ import (
"github.com/ethereum/go-ethereum/params"
)
+// Execution is the execution environment for the given call or create action.
type Execution struct {
env vm.Environment
address *common.Address
@@ -35,12 +36,15 @@ type Execution struct {
Gas, price, value *big.Int
}
+// NewExecution returns a new execution environment that handles all calling
+// and creation logic defined by the YP.
func NewExecution(env vm.Environment, address *common.Address, input []byte, gas, gasPrice, value *big.Int) *Execution {
exe := &Execution{env: env, address: address, input: input, Gas: gas, price: gasPrice, value: value}
exe.evm = vm.NewVm(env)
return exe
}
+// Call executes within the given context
func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]byte, error) {
// Retrieve the executing code
code := self.env.State().GetCode(codeAddr)
@@ -48,6 +52,9 @@ func (self *Execution) Call(codeAddr common.Address, caller vm.ContextRef) ([]by
return self.exec(&codeAddr, code, caller)
}
+// Create creates a new contract and runs the initialisation procedure of the
+// contract. This returns the returned code for the contract and is stored
+// elsewhere.
func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, account *state.StateObject) {
// Input must be nil for create
code := self.input
@@ -63,16 +70,24 @@ func (self *Execution) Create(caller vm.ContextRef) (ret []byte, err error, acco
return
}
+// exec executes the given code and executes within the contextAddr context.
func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.ContextRef) (ret []byte, err error) {
env := self.env
evm := self.evm
+ // Depth check execution. Fail if we're trying to execute above the
+ // limit.
if env.Depth() > int(params.CallCreateDepth.Int64()) {
caller.ReturnGas(self.Gas, self.price)
return nil, vm.DepthError
}
- vsnapshot := env.State().Copy()
+ if !env.CanTransfer(env.State().GetStateObject(caller.Address()), self.value) {
+ caller.ReturnGas(self.Gas, self.price)
+
+ return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, env.State().GetBalance(caller.Address()))
+ }
+
var createAccount bool
if self.address == nil {
// Generate a new address
@@ -95,15 +110,7 @@ func (self *Execution) exec(contextAddr *common.Address, code []byte, caller vm.
} else {
to = env.State().GetOrNewStateObject(*self.address)
}
-
- err = env.Transfer(from, to, self.value)
- if err != nil {
- env.State().Set(vsnapshot)
-
- caller.ReturnGas(self.Gas, self.price)
-
- return nil, ValueTransferErr("insufficient funds to transfer value. Req %v, has %v", self.value, from.Balance())
- }
+ vm.Transfer(from, to, self.value)
context := vm.NewContext(caller, to, self.value, self.Gas, self.price)
context.SetCallCode(contextAddr, code)
diff --git a/core/vm/context.go b/core/vm/context.go
index 162666ef2..d17934ba5 100644
--- a/core/vm/context.go
+++ b/core/vm/context.go
@@ -35,6 +35,7 @@ type Context struct {
jumpdests destinations // result of JUMPDEST analysis.
Code []byte
+ Input []byte
CodeAddr *common.Address
value, Gas, UsedGas, Price *big.Int
diff --git a/core/vm/contracts.go b/core/vm/contracts.go
index 2d70f173e..b965fa095 100644
--- a/core/vm/contracts.go
+++ b/core/vm/contracts.go
@@ -94,7 +94,7 @@ func ecrecoverFunc(in []byte) []byte {
v := byte(vbig.Uint64())
if !crypto.ValidateSignatureValues(v, r, s) {
- glog.V(logger.Error).Infof("EC RECOVER FAIL: v, r or s value invalid")
+ glog.V(logger.Debug).Infof("EC RECOVER FAIL: v, r or s value invalid")
return nil
}
diff --git a/core/vm/environment.go b/core/vm/environment.go
index 723924b6f..5a1bf3201 100644
--- a/core/vm/environment.go
+++ b/core/vm/environment.go
@@ -36,6 +36,7 @@ type Environment interface {
Time() uint64
Difficulty() *big.Int
GasLimit() *big.Int
+ CanTransfer(from Account, balance *big.Int) bool
Transfer(from, to Account, amount *big.Int) error
AddLog(*state.Log)
AddStructLog(StructLog)
diff --git a/core/vm/gas.go b/core/vm/gas.go
index af2e586a7..b2f068e6e 100644
--- a/core/vm/gas.go
+++ b/core/vm/gas.go
@@ -54,8 +54,8 @@ func baseCheck(op OpCode, stack *stack, gas *big.Int) error {
return err
}
- if r.stackPush > 0 && len(stack.data)-r.stackPop+r.stackPush > int(params.StackLimit.Int64())+1 {
- return fmt.Errorf("stack limit reached %d (%d)", len(stack.data), params.StackLimit.Int64())
+ if r.stackPush > 0 && stack.len()-r.stackPop+r.stackPush > int(params.StackLimit.Int64()) {
+ return fmt.Errorf("stack limit reached %d (%d)", stack.len(), params.StackLimit.Int64())
}
gas.Add(gas, r.gas)
diff --git a/core/vm/instructions.go b/core/vm/instructions.go
new file mode 100644
index 000000000..6b7b41220
--- /dev/null
+++ b/core/vm/instructions.go
@@ -0,0 +1,537 @@
+// Copyright 2014 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/>.
+
+package vm
+
+import (
+ "math/big"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/params"
+)
+
+type instrFn func(instr instruction, env Environment, context *Context, memory *Memory, stack *stack)
+type instrExFn func(instr instruction, ret *big.Int, env Environment, context *Context, memory *Memory, stack *stack)
+
+type instruction struct {
+ op OpCode
+ pc uint64
+ fn instrFn
+ specFn instrExFn
+ data *big.Int
+
+ gas *big.Int
+ spop int
+ spush int
+}
+
+func opStaticJump(instr instruction, ret *big.Int, env Environment, context *Context, memory *Memory, stack *stack) {
+ ret.Set(instr.data)
+}
+
+func opAdd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(U256(x.Add(x, y)))
+}
+
+func opSub(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(U256(x.Sub(x, y)))
+}
+
+func opMul(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(U256(x.Mul(x, y)))
+}
+
+func opDiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ if y.Cmp(common.Big0) != 0 {
+ stack.push(U256(x.Div(x, y)))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opSdiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := S256(stack.pop()), S256(stack.pop())
+ if y.Cmp(common.Big0) == 0 {
+ stack.push(new(big.Int))
+ return
+ } else {
+ n := new(big.Int)
+ if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 {
+ n.SetInt64(-1)
+ } else {
+ n.SetInt64(1)
+ }
+
+ res := x.Div(x.Abs(x), y.Abs(y))
+ res.Mul(res, n)
+
+ stack.push(U256(res))
+ }
+}
+
+func opMod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ if y.Cmp(common.Big0) == 0 {
+ stack.push(new(big.Int))
+ } else {
+ stack.push(U256(x.Mod(x, y)))
+ }
+}
+
+func opSmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := S256(stack.pop()), S256(stack.pop())
+
+ if y.Cmp(common.Big0) == 0 {
+ stack.push(new(big.Int))
+ } else {
+ n := new(big.Int)
+ if x.Cmp(common.Big0) < 0 {
+ n.SetInt64(-1)
+ } else {
+ n.SetInt64(1)
+ }
+
+ res := x.Mod(x.Abs(x), y.Abs(y))
+ res.Mul(res, n)
+
+ stack.push(U256(res))
+ }
+}
+
+func opExp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(U256(x.Exp(x, y, Pow256)))
+}
+
+func opSignExtend(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ back := stack.pop()
+ if back.Cmp(big.NewInt(31)) < 0 {
+ bit := uint(back.Uint64()*8 + 7)
+ num := stack.pop()
+ mask := back.Lsh(common.Big1, bit)
+ mask.Sub(mask, common.Big1)
+ if common.BitTest(num, int(bit)) {
+ num.Or(num, mask.Not(mask))
+ } else {
+ num.And(num, mask)
+ }
+
+ stack.push(U256(num))
+ }
+}
+
+func opNot(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x := stack.pop()
+ stack.push(U256(x.Not(x)))
+}
+
+func opLt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ if x.Cmp(y) < 0 {
+ stack.push(big.NewInt(1))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opGt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ if x.Cmp(y) > 0 {
+ stack.push(big.NewInt(1))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opSlt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := S256(stack.pop()), S256(stack.pop())
+ if x.Cmp(S256(y)) < 0 {
+ stack.push(big.NewInt(1))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opSgt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := S256(stack.pop()), S256(stack.pop())
+ if x.Cmp(y) > 0 {
+ stack.push(big.NewInt(1))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opEq(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ if x.Cmp(y) == 0 {
+ stack.push(big.NewInt(1))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opIszero(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x := stack.pop()
+ if x.Cmp(common.Big0) > 0 {
+ stack.push(new(big.Int))
+ } else {
+ stack.push(big.NewInt(1))
+ }
+}
+
+func opAnd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(x.And(x, y))
+}
+func opOr(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(x.Or(x, y))
+}
+func opXor(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y := stack.pop(), stack.pop()
+ stack.push(x.Xor(x, y))
+}
+func opByte(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ th, val := stack.pop(), stack.pop()
+ if th.Cmp(big.NewInt(32)) < 0 {
+ byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
+ stack.push(byte)
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+func opAddmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y, z := stack.pop(), stack.pop(), stack.pop()
+ if z.Cmp(Zero) > 0 {
+ add := x.Add(x, y)
+ add.Mod(add, z)
+ stack.push(U256(add))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+func opMulmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ x, y, z := stack.pop(), stack.pop(), stack.pop()
+ if z.Cmp(Zero) > 0 {
+ mul := x.Mul(x, y)
+ mul.Mod(mul, z)
+ stack.push(U256(mul))
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opSha3(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ offset, size := stack.pop(), stack.pop()
+ hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64()))
+
+ stack.push(common.BytesToBig(hash))
+}
+
+func opAddress(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(common.Bytes2Big(context.Address().Bytes()))
+}
+
+func opBalance(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ addr := common.BigToAddress(stack.pop())
+ balance := env.State().GetBalance(addr)
+
+ stack.push(new(big.Int).Set(balance))
+}
+
+func opOrigin(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(env.Origin().Big())
+}
+
+func opCaller(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(common.Bytes2Big(context.caller.Address().Bytes()))
+}
+
+func opCallValue(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).Set(context.value))
+}
+
+func opCalldataLoad(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(common.Bytes2Big(getData(context.Input, stack.pop(), common.Big32)))
+}
+
+func opCalldataSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(big.NewInt(int64(len(context.Input))))
+}
+
+func opCalldataCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ var (
+ mOff = stack.pop()
+ cOff = stack.pop()
+ l = stack.pop()
+ )
+ memory.Set(mOff.Uint64(), l.Uint64(), getData(context.Input, cOff, l))
+}
+
+func opExtCodeSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ addr := common.BigToAddress(stack.pop())
+ l := big.NewInt(int64(len(env.State().GetCode(addr))))
+ stack.push(l)
+}
+
+func opCodeSize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ l := big.NewInt(int64(len(context.Code)))
+ stack.push(l)
+}
+
+func opCodeCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ var (
+ mOff = stack.pop()
+ cOff = stack.pop()
+ l = stack.pop()
+ )
+ codeCopy := getData(context.Code, cOff, l)
+
+ memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
+}
+
+func opExtCodeCopy(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ var (
+ addr = common.BigToAddress(stack.pop())
+ mOff = stack.pop()
+ cOff = stack.pop()
+ l = stack.pop()
+ )
+ codeCopy := getData(env.State().GetCode(addr), cOff, l)
+
+ memory.Set(mOff.Uint64(), l.Uint64(), codeCopy)
+}
+
+func opGasprice(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).Set(context.Price))
+}
+
+func opBlockhash(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ num := stack.pop()
+
+ n := new(big.Int).Sub(env.BlockNumber(), common.Big257)
+ if num.Cmp(n) > 0 && num.Cmp(env.BlockNumber()) < 0 {
+ stack.push(env.GetHash(num.Uint64()).Big())
+ } else {
+ stack.push(new(big.Int))
+ }
+}
+
+func opCoinbase(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(env.Coinbase().Big())
+}
+
+func opTimestamp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).SetUint64(env.Time()))
+}
+
+func opNumber(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(U256(env.BlockNumber()))
+}
+
+func opDifficulty(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).Set(env.Difficulty()))
+}
+
+func opGasLimit(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).Set(env.GasLimit()))
+}
+
+func opPop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.pop()
+}
+
+func opPush(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).Set(instr.data))
+}
+
+func opDup(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.dup(int(instr.data.Int64()))
+}
+
+func opSwap(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.swap(int(instr.data.Int64()))
+}
+
+func opLog(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ n := int(instr.data.Int64())
+ topics := make([]common.Hash, n)
+ mStart, mSize := stack.pop(), stack.pop()
+ for i := 0; i < n; i++ {
+ topics[i] = common.BigToHash(stack.pop())
+ }
+
+ d := memory.Get(mStart.Int64(), mSize.Int64())
+ log := state.NewLog(context.Address(), topics, d, env.BlockNumber().Uint64())
+ env.AddLog(log)
+}
+
+func opMload(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ offset := stack.pop()
+ val := common.BigD(memory.Get(offset.Int64(), 32))
+ stack.push(val)
+}
+
+func opMstore(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ // pop value of the stack
+ mStart, val := stack.pop(), stack.pop()
+ memory.Set(mStart.Uint64(), 32, common.BigToBytes(val, 256))
+}
+
+func opMstore8(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ off, val := stack.pop().Int64(), stack.pop().Int64()
+ memory.store[off] = byte(val & 0xff)
+}
+
+func opSload(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ loc := common.BigToHash(stack.pop())
+ val := env.State().GetState(context.Address(), loc).Big()
+ stack.push(val)
+}
+
+func opSstore(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ loc := common.BigToHash(stack.pop())
+ val := stack.pop()
+
+ env.State().SetState(context.Address(), loc, common.BigToHash(val))
+}
+
+func opJump(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+}
+func opJumpi(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+}
+func opJumpdest(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+}
+
+func opPc(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(instr.data)
+}
+
+func opMsize(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(big.NewInt(int64(memory.Len())))
+}
+
+func opGas(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ stack.push(new(big.Int).Set(context.Gas))
+}
+
+func opCreate(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ var (
+ value = stack.pop()
+ offset, size = stack.pop(), stack.pop()
+ input = memory.Get(offset.Int64(), size.Int64())
+ gas = new(big.Int).Set(context.Gas)
+ addr common.Address
+ )
+
+ context.UseGas(context.Gas)
+ ret, suberr, ref := env.Create(context, input, gas, context.Price, value)
+ if suberr != nil {
+ stack.push(new(big.Int))
+
+ } else {
+ // gas < len(ret) * Createinstr.dataGas == NO_CODE
+ dataGas := big.NewInt(int64(len(ret)))
+ dataGas.Mul(dataGas, params.CreateDataGas)
+ if context.UseGas(dataGas) {
+ ref.SetCode(ret)
+ }
+ addr = ref.Address()
+
+ stack.push(addr.Big())
+
+ }
+}
+
+func opCall(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ gas := stack.pop()
+ // pop gas and value of the stack.
+ addr, value := stack.pop(), stack.pop()
+ value = U256(value)
+ // pop input size and offset
+ inOffset, inSize := stack.pop(), stack.pop()
+ // pop return size and offset
+ retOffset, retSize := stack.pop(), stack.pop()
+
+ address := common.BigToAddress(addr)
+
+ // Get the arguments from the memory
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ if len(value.Bytes()) > 0 {
+ gas.Add(gas, params.CallStipend)
+ }
+
+ ret, err := env.Call(context, address, args, gas, context.Price, value)
+
+ if err != nil {
+ stack.push(new(big.Int))
+
+ } else {
+ stack.push(big.NewInt(1))
+
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+}
+
+func opCallCode(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ gas := stack.pop()
+ // pop gas and value of the stack.
+ addr, value := stack.pop(), stack.pop()
+ value = U256(value)
+ // pop input size and offset
+ inOffset, inSize := stack.pop(), stack.pop()
+ // pop return size and offset
+ retOffset, retSize := stack.pop(), stack.pop()
+
+ address := common.BigToAddress(addr)
+
+ // Get the arguments from the memory
+ args := memory.Get(inOffset.Int64(), inSize.Int64())
+
+ if len(value.Bytes()) > 0 {
+ gas.Add(gas, params.CallStipend)
+ }
+
+ ret, err := env.CallCode(context, address, args, gas, context.Price, value)
+
+ if err != nil {
+ stack.push(new(big.Int))
+
+ } else {
+ stack.push(big.NewInt(1))
+
+ memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
+ }
+}
+
+func opReturn(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
+func opStop(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {}
+
+func opSuicide(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
+ receiver := env.State().GetOrNewStateObject(common.BigToAddress(stack.pop()))
+ balance := env.State().GetBalance(context.Address())
+
+ receiver.AddBalance(balance)
+
+ env.State().Delete(context.Address())
+}
diff --git a/core/vm/jit.go b/core/vm/jit.go
new file mode 100644
index 000000000..d5c2d7830
--- /dev/null
+++ b/core/vm/jit.go
@@ -0,0 +1,541 @@
+// Copyright 2014 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/>.
+
+package vm
+
+import (
+ "fmt"
+ "math/big"
+ "sync/atomic"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/params"
+ "github.com/hashicorp/golang-lru"
+)
+
+type progStatus int32
+
+const (
+ progUnknown progStatus = iota
+ progCompile
+ progReady
+ progError
+)
+
+var programs *lru.Cache
+
+func init() {
+ programs, _ = lru.New(defaultJitMaxCache)
+}
+
+// SetJITCacheSize recreates the program cache with the max given size. Setting
+// a new cache is **not** thread safe. Use with caution.
+func SetJITCacheSize(size int) {
+ programs, _ = lru.New(size)
+}
+
+// GetProgram returns the program by id or nil when non-existent
+func GetProgram(id common.Hash) *Program {
+ if p, ok := programs.Get(id); ok {
+ return p.(*Program)
+ }
+
+ return nil
+}
+
+// GenProgramStatus returns the status of the given program id
+func GetProgramStatus(id common.Hash) progStatus {
+ program := GetProgram(id)
+ if program != nil {
+ return progStatus(atomic.LoadInt32(&program.status))
+ }
+
+ return progUnknown
+}
+
+// Program is a compiled program for the JIT VM and holds all required for
+// running a compiled JIT program.
+type Program struct {
+ Id common.Hash // Id of the program
+ status int32 // status should be accessed atomically
+
+ context *Context
+
+ instructions []instruction // instruction set
+ mapping map[uint64]int // real PC mapping to array indices
+ destinations map[uint64]struct{} // cached jump destinations
+
+ code []byte
+}
+
+func NewProgram(code []byte) *Program {
+ program := &Program{
+ Id: crypto.Sha3Hash(code),
+ mapping: make(map[uint64]int),
+ destinations: make(map[uint64]struct{}),
+ code: code,
+ }
+
+ programs.Add(program.Id, program)
+ return program
+}
+
+func (p *Program) addInstr(op OpCode, pc uint64, fn instrFn, data *big.Int) {
+ // PUSH and DUP are a bit special. They all cost the same but we do want to have checking on stack push limit
+ // PUSH is also allowed to calculate the same price for all PUSHes
+ // DUP requirements are handled elsewhere (except for the stack limit check)
+ baseOp := op
+ if op >= PUSH1 && op <= PUSH32 {
+ baseOp = PUSH1
+ }
+ if op >= DUP1 && op <= DUP16 {
+ baseOp = DUP1
+ }
+ base := _baseCheck[baseOp]
+ instr := instruction{op, pc, fn, nil, data, base.gas, base.stackPop, base.stackPush}
+
+ p.instructions = append(p.instructions, instr)
+ p.mapping[pc] = len(p.instructions) - 1
+}
+
+func CompileProgram(program *Program) (err error) {
+ if progStatus(atomic.LoadInt32(&program.status)) == progCompile {
+ return nil
+ }
+ atomic.StoreInt32(&program.status, int32(progCompile))
+ defer func() {
+ if err != nil {
+ atomic.StoreInt32(&program.status, int32(progError))
+ } else {
+ atomic.StoreInt32(&program.status, int32(progReady))
+ }
+ }()
+
+ // loop thru the opcodes and "compile" in to instructions
+ for pc := uint64(0); pc < uint64(len(program.code)); pc++ {
+ switch op := OpCode(program.code[pc]); op {
+ case ADD:
+ program.addInstr(op, pc, opAdd, nil)
+ case SUB:
+ program.addInstr(op, pc, opSub, nil)
+ case MUL:
+ program.addInstr(op, pc, opMul, nil)
+ case DIV:
+ program.addInstr(op, pc, opDiv, nil)
+ case SDIV:
+ program.addInstr(op, pc, opSdiv, nil)
+ case MOD:
+ program.addInstr(op, pc, opMod, nil)
+ case SMOD:
+ program.addInstr(op, pc, opSmod, nil)
+ case EXP:
+ program.addInstr(op, pc, opExp, nil)
+ case SIGNEXTEND:
+ program.addInstr(op, pc, opSignExtend, nil)
+ case NOT:
+ program.addInstr(op, pc, opNot, nil)
+ case LT:
+ program.addInstr(op, pc, opLt, nil)
+ case GT:
+ program.addInstr(op, pc, opGt, nil)
+ case SLT:
+ program.addInstr(op, pc, opSlt, nil)
+ case SGT:
+ program.addInstr(op, pc, opSgt, nil)
+ case EQ:
+ program.addInstr(op, pc, opEq, nil)
+ case ISZERO:
+ program.addInstr(op, pc, opIszero, nil)
+ case AND:
+ program.addInstr(op, pc, opAnd, nil)
+ case OR:
+ program.addInstr(op, pc, opOr, nil)
+ case XOR:
+ program.addInstr(op, pc, opXor, nil)
+ case BYTE:
+ program.addInstr(op, pc, opByte, nil)
+ case ADDMOD:
+ program.addInstr(op, pc, opAddmod, nil)
+ case MULMOD:
+ program.addInstr(op, pc, opMulmod, nil)
+ case SHA3:
+ program.addInstr(op, pc, opSha3, nil)
+ case ADDRESS:
+ program.addInstr(op, pc, opAddress, nil)
+ case BALANCE:
+ program.addInstr(op, pc, opBalance, nil)
+ case ORIGIN:
+ program.addInstr(op, pc, opOrigin, nil)
+ case CALLER:
+ program.addInstr(op, pc, opCaller, nil)
+ case CALLVALUE:
+ program.addInstr(op, pc, opCallValue, nil)
+ case CALLDATALOAD:
+ program.addInstr(op, pc, opCalldataLoad, nil)
+ case CALLDATASIZE:
+ program.addInstr(op, pc, opCalldataSize, nil)
+ case CALLDATACOPY:
+ program.addInstr(op, pc, opCalldataCopy, nil)
+ case CODESIZE:
+ program.addInstr(op, pc, opCodeSize, nil)
+ case EXTCODESIZE:
+ program.addInstr(op, pc, opExtCodeSize, nil)
+ case CODECOPY:
+ program.addInstr(op, pc, opCodeCopy, nil)
+ case EXTCODECOPY:
+ program.addInstr(op, pc, opExtCodeCopy, nil)
+ case GASPRICE:
+ program.addInstr(op, pc, opGasprice, nil)
+ case BLOCKHASH:
+ program.addInstr(op, pc, opBlockhash, nil)
+ case COINBASE:
+ program.addInstr(op, pc, opCoinbase, nil)
+ case TIMESTAMP:
+ program.addInstr(op, pc, opTimestamp, nil)
+ case NUMBER:
+ program.addInstr(op, pc, opNumber, nil)
+ case DIFFICULTY:
+ program.addInstr(op, pc, opDifficulty, nil)
+ case GASLIMIT:
+ program.addInstr(op, pc, opGasLimit, nil)
+ case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
+ size := uint64(op - PUSH1 + 1)
+ bytes := getData([]byte(program.code), new(big.Int).SetUint64(pc+1), new(big.Int).SetUint64(size))
+
+ program.addInstr(op, pc, opPush, common.Bytes2Big(bytes))
+
+ pc += size
+
+ case POP:
+ program.addInstr(op, pc, opPop, nil)
+ case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
+ program.addInstr(op, pc, opDup, big.NewInt(int64(op-DUP1+1)))
+ case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
+ program.addInstr(op, pc, opSwap, big.NewInt(int64(op-SWAP1+2)))
+ case LOG0, LOG1, LOG2, LOG3, LOG4:
+ program.addInstr(op, pc, opLog, big.NewInt(int64(op-LOG0)))
+ case MLOAD:
+ program.addInstr(op, pc, opMload, nil)
+ case MSTORE:
+ program.addInstr(op, pc, opMstore, nil)
+ case MSTORE8:
+ program.addInstr(op, pc, opMstore8, nil)
+ case SLOAD:
+ program.addInstr(op, pc, opSload, nil)
+ case SSTORE:
+ program.addInstr(op, pc, opSstore, nil)
+ case JUMP:
+ program.addInstr(op, pc, opJump, nil)
+ case JUMPI:
+ program.addInstr(op, pc, opJumpi, nil)
+ case JUMPDEST:
+ program.addInstr(op, pc, opJumpdest, nil)
+ program.destinations[pc] = struct{}{}
+ case PC:
+ program.addInstr(op, pc, opPc, big.NewInt(int64(pc)))
+ case MSIZE:
+ program.addInstr(op, pc, opMsize, nil)
+ case GAS:
+ program.addInstr(op, pc, opGas, nil)
+ case CREATE:
+ program.addInstr(op, pc, opCreate, nil)
+ case CALL:
+ program.addInstr(op, pc, opCall, nil)
+ case CALLCODE:
+ program.addInstr(op, pc, opCallCode, nil)
+ case RETURN:
+ program.addInstr(op, pc, opReturn, nil)
+ case SUICIDE:
+ program.addInstr(op, pc, opSuicide, nil)
+ case STOP: // Stop the context
+ program.addInstr(op, pc, opStop, nil)
+ default:
+ program.addInstr(op, pc, nil, nil)
+ }
+ }
+
+ return nil
+}
+
+func RunProgram(program *Program, env Environment, context *Context, input []byte) ([]byte, error) {
+ return runProgram(program, 0, NewMemory(), newstack(), env, context, input)
+}
+
+func runProgram(program *Program, pcstart uint64, mem *Memory, stack *stack, env Environment, context *Context, input []byte) ([]byte, error) {
+ context.Input = input
+
+ var (
+ caller = context.caller
+ statedb = env.State()
+ pc int = program.mapping[pcstart]
+
+ jump = func(to *big.Int) error {
+ if !validDest(program.destinations, to) {
+ nop := context.GetOp(to.Uint64())
+ return fmt.Errorf("invalid jump destination (%v) %v", nop, to)
+ }
+
+ pc = program.mapping[to.Uint64()]
+
+ return nil
+ }
+ )
+
+ for pc < len(program.instructions) {
+ instr := program.instructions[pc]
+
+ // calculate the new memory size and gas price for the current executing opcode
+ newMemSize, cost, err := jitCalculateGasAndSize(env, context, caller, instr, statedb, mem, stack)
+ if err != nil {
+ return nil, err
+ }
+
+ // Use the calculated gas. When insufficient gas is present, use all gas and return an
+ // Out Of Gas error
+ if !context.UseGas(cost) {
+ return nil, OutOfGasError
+ }
+ // Resize the memory calculated previously
+ mem.Resize(newMemSize.Uint64())
+
+ // These opcodes return an argument and are thefor handled
+ // differently from the rest of the opcodes
+ switch instr.op {
+ case JUMP:
+ if err := jump(stack.pop()); err != nil {
+ return nil, err
+ }
+ continue
+ case JUMPI:
+ pos, cond := stack.pop(), stack.pop()
+
+ if cond.Cmp(common.BigTrue) >= 0 {
+ if err := jump(pos); err != nil {
+ return nil, err
+ }
+ continue
+ }
+ case RETURN:
+ offset, size := stack.pop(), stack.pop()
+ ret := mem.GetPtr(offset.Int64(), size.Int64())
+
+ return context.Return(ret), nil
+ case SUICIDE:
+ instr.fn(instr, env, context, mem, stack)
+
+ return context.Return(nil), nil
+ case STOP:
+ return context.Return(nil), nil
+ default:
+ if instr.fn == nil {
+ return nil, fmt.Errorf("Invalid opcode %x", instr.op)
+ }
+
+ instr.fn(instr, env, context, mem, stack)
+ }
+
+ pc++
+ }
+
+ return context.Return(nil), nil
+}
+
+// validDest checks if the given distination is a valid one given the
+// destination table of the program
+func validDest(dests map[uint64]struct{}, dest *big.Int) bool {
+ // PC cannot go beyond len(code) and certainly can't be bigger than 64bits.
+ // Don't bother checking for JUMPDEST in that case.
+ if dest.Cmp(bigMaxUint64) > 0 {
+ return false
+ }
+ _, ok := dests[dest.Uint64()]
+ return ok
+}
+
+// jitCalculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for
+// the operation. This does not reduce gas or resizes the memory.
+func jitCalculateGasAndSize(env Environment, context *Context, caller ContextRef, instr instruction, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
+ var (
+ gas = new(big.Int)
+ newMemSize *big.Int = new(big.Int)
+ )
+ err := jitBaseCheck(instr, stack, gas)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ // stack Check, memory resize & gas phase
+ switch op := instr.op; op {
+ case SWAP1, SWAP2, SWAP3, SWAP4, SWAP5, SWAP6, SWAP7, SWAP8, SWAP9, SWAP10, SWAP11, SWAP12, SWAP13, SWAP14, SWAP15, SWAP16:
+ n := int(op - SWAP1 + 2)
+ err := stack.require(n)
+ if err != nil {
+ return nil, nil, err
+ }
+ gas.Set(GasFastestStep)
+ case DUP1, DUP2, DUP3, DUP4, DUP5, DUP6, DUP7, DUP8, DUP9, DUP10, DUP11, DUP12, DUP13, DUP14, DUP15, DUP16:
+ n := int(op - DUP1 + 1)
+ err := stack.require(n)
+ if err != nil {
+ return nil, nil, err
+ }
+ gas.Set(GasFastestStep)
+ case LOG0, LOG1, LOG2, LOG3, LOG4:
+ n := int(op - LOG0)
+ err := stack.require(n + 2)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ mSize, mStart := stack.data[stack.len()-2], stack.data[stack.len()-1]
+
+ add := new(big.Int)
+ gas.Add(gas, params.LogGas)
+ gas.Add(gas, add.Mul(big.NewInt(int64(n)), params.LogTopicGas))
+ gas.Add(gas, add.Mul(mSize, params.LogDataGas))
+
+ newMemSize = calcMemSize(mStart, mSize)
+ case EXP:
+ gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(len(stack.data[stack.len()-2].Bytes()))), params.ExpByteGas))
+ case SSTORE:
+ err := stack.require(2)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ var g *big.Int
+ y, x := stack.data[stack.len()-2], stack.data[stack.len()-1]
+ val := statedb.GetState(context.Address(), common.BigToHash(x))
+
+ // This checks for 3 scenario's and calculates gas accordingly
+ // 1. From a zero-value address to a non-zero value (NEW VALUE)
+ // 2. From a non-zero value address to a zero-value address (DELETE)
+ // 3. From a nen-zero to a non-zero (CHANGE)
+ if common.EmptyHash(val) && !common.EmptyHash(common.BigToHash(y)) {
+ // 0 => non 0
+ g = params.SstoreSetGas
+ } else if !common.EmptyHash(val) && common.EmptyHash(common.BigToHash(y)) {
+ statedb.Refund(params.SstoreRefundGas)
+
+ g = params.SstoreClearGas
+ } else {
+ // non 0 => non 0 (or 0 => 0)
+ g = params.SstoreClearGas
+ }
+ gas.Set(g)
+ case SUICIDE:
+ if !statedb.IsDeleted(context.Address()) {
+ statedb.Refund(params.SuicideRefundGas)
+ }
+ case MLOAD:
+ newMemSize = calcMemSize(stack.peek(), u256(32))
+ case MSTORE8:
+ newMemSize = calcMemSize(stack.peek(), u256(1))
+ case MSTORE:
+ newMemSize = calcMemSize(stack.peek(), u256(32))
+ case RETURN:
+ newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-2])
+ case SHA3:
+ newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-2])
+
+ words := toWordSize(stack.data[stack.len()-2])
+ gas.Add(gas, words.Mul(words, params.Sha3WordGas))
+ case CALLDATACOPY:
+ newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-3])
+
+ words := toWordSize(stack.data[stack.len()-3])
+ gas.Add(gas, words.Mul(words, params.CopyGas))
+ case CODECOPY:
+ newMemSize = calcMemSize(stack.peek(), stack.data[stack.len()-3])
+
+ words := toWordSize(stack.data[stack.len()-3])
+ gas.Add(gas, words.Mul(words, params.CopyGas))
+ case EXTCODECOPY:
+ newMemSize = calcMemSize(stack.data[stack.len()-2], stack.data[stack.len()-4])
+
+ words := toWordSize(stack.data[stack.len()-4])
+ gas.Add(gas, words.Mul(words, params.CopyGas))
+
+ case CREATE:
+ newMemSize = calcMemSize(stack.data[stack.len()-2], stack.data[stack.len()-3])
+ case CALL, CALLCODE:
+ gas.Add(gas, stack.data[stack.len()-1])
+
+ if op == CALL {
+ if env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil {
+ gas.Add(gas, params.CallNewAccountGas)
+ }
+ }
+
+ if len(stack.data[stack.len()-3].Bytes()) > 0 {
+ gas.Add(gas, params.CallValueTransferGas)
+ }
+
+ x := calcMemSize(stack.data[stack.len()-6], stack.data[stack.len()-7])
+ y := calcMemSize(stack.data[stack.len()-4], stack.data[stack.len()-5])
+
+ newMemSize = common.BigMax(x, y)
+ }
+
+ if newMemSize.Cmp(common.Big0) > 0 {
+ newMemSizeWords := toWordSize(newMemSize)
+ newMemSize.Mul(newMemSizeWords, u256(32))
+
+ if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
+ // be careful reusing variables here when changing.
+ // The order has been optimised to reduce allocation
+ oldSize := toWordSize(big.NewInt(int64(mem.Len())))
+ pow := new(big.Int).Exp(oldSize, common.Big2, Zero)
+ linCoef := oldSize.Mul(oldSize, params.MemoryGas)
+ quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv)
+ oldTotalFee := new(big.Int).Add(linCoef, quadCoef)
+
+ pow.Exp(newMemSizeWords, common.Big2, Zero)
+ linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas)
+ quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv)
+ newTotalFee := linCoef.Add(linCoef, quadCoef)
+
+ fee := newTotalFee.Sub(newTotalFee, oldTotalFee)
+ gas.Add(gas, fee)
+ }
+ }
+
+ return newMemSize, gas, nil
+}
+
+// jitBaseCheck is the same as baseCheck except it doesn't do the look up in the
+// gas table. This is done during compilation instead.
+func jitBaseCheck(instr instruction, stack *stack, gas *big.Int) error {
+ err := stack.require(instr.spop)
+ if err != nil {
+ return err
+ }
+
+ if instr.spush > 0 && stack.len()-instr.spop+instr.spush > int(params.StackLimit.Int64()) {
+ return fmt.Errorf("stack limit reached %d (%d)", stack.len(), params.StackLimit.Int64())
+ }
+
+ // nil on gas means no base calculation
+ if instr.gas == nil {
+ return nil
+ }
+
+ gas.Add(gas, instr.gas)
+
+ return nil
+}
diff --git a/core/vm/jit_test.go b/core/vm/jit_test.go
new file mode 100644
index 000000000..5b3feea99
--- /dev/null
+++ b/core/vm/jit_test.go
@@ -0,0 +1,122 @@
+// Copyright 2014 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/>.
+package vm
+
+import (
+ "math/big"
+ "testing"
+ "time"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/state"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/ethdb"
+)
+
+const maxRun = 1000
+
+type vmBench struct {
+ precompile bool // compile prior to executing
+ nojit bool // ignore jit (sets DisbaleJit = true
+ forcejit bool // forces the jit, precompile is ignored
+
+ code []byte
+ input []byte
+}
+
+func runVmBench(test vmBench, b *testing.B) {
+ db, _ := ethdb.NewMemDatabase()
+ sender := state.NewStateObject(common.Address{}, db)
+
+ if test.precompile && !test.forcejit {
+ NewProgram(test.code)
+ }
+ env := NewEnv()
+
+ DisableJit = test.nojit
+ ForceJit = test.forcejit
+
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ context := NewContext(sender, sender, big.NewInt(100), big.NewInt(10000), big.NewInt(0))
+ context.Code = test.code
+ context.CodeAddr = &common.Address{}
+ _, err := New(env).Run(context, test.input)
+ if err != nil {
+ b.Error(err)
+ b.FailNow()
+ }
+ }
+}
+
+var benchmarks = map[string]vmBench{
+ "pushes": vmBench{
+ false, false, false,
+ common.Hex2Bytes("600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01600a600a01"), nil,
+ },
+}
+
+func BenchmarkPushes(b *testing.B) {
+ runVmBench(benchmarks["pushes"], b)
+}
+
+type Env struct {
+ gasLimit *big.Int
+ depth int
+}
+
+func NewEnv() *Env {
+ return &Env{big.NewInt(10000), 0}
+}
+
+func (self *Env) Origin() common.Address { return common.Address{} }
+func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
+func (self *Env) AddStructLog(log StructLog) {
+}
+func (self *Env) StructLogs() []StructLog {
+ return nil
+}
+
+//func (self *Env) PrevHash() []byte { return self.parent }
+func (self *Env) Coinbase() common.Address { return common.Address{} }
+func (self *Env) Time() uint64 { return uint64(time.Now().Unix()) }
+func (self *Env) Difficulty() *big.Int { return big.NewInt(0) }
+func (self *Env) State() *state.StateDB { return nil }
+func (self *Env) GasLimit() *big.Int { return self.gasLimit }
+func (self *Env) VmType() Type { return StdVmTy }
+func (self *Env) GetHash(n uint64) common.Hash {
+ return common.BytesToHash(crypto.Sha3([]byte(big.NewInt(int64(n)).String())))
+}
+func (self *Env) AddLog(log *state.Log) {
+}
+func (self *Env) Depth() int { return self.depth }
+func (self *Env) SetDepth(i int) { self.depth = i }
+func (self *Env) CanTransfer(from Account, balance *big.Int) bool {
+ return from.Balance().Cmp(balance) >= 0
+}
+func (self *Env) Transfer(from, to Account, amount *big.Int) error {
+ return nil
+}
+func (self *Env) Call(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
+ return nil, nil
+}
+func (self *Env) CallCode(caller ContextRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
+ return nil, nil
+}
+func (self *Env) Create(caller ContextRef, data []byte, gas, price, value *big.Int) ([]byte, error, ContextRef) {
+ return nil, nil, nil
+}
diff --git a/core/vm/settings.go b/core/vm/settings.go
new file mode 100644
index 000000000..0cd931b6a
--- /dev/null
+++ b/core/vm/settings.go
@@ -0,0 +1,25 @@
+// Copyright 2014 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/>.
+
+package vm
+
+var (
+ DisableJit bool = true // Disable the JIT VM
+ ForceJit bool // Force the JIT, skip byte VM
+ MaxProgSize int // Max cache size for JIT Programs
+)
+
+const defaultJitMaxCache int = 64
diff --git a/core/vm/stack.go b/core/vm/stack.go
index 3d669b2f2..009ac9e1b 100644
--- a/core/vm/stack.go
+++ b/core/vm/stack.go
@@ -21,38 +21,36 @@ import (
"math/big"
)
-func newstack() *stack {
- return &stack{}
-}
-
+// stack is an object for basic stack operations. Items popped to the stack are
+// expected to be changed and modified. stack does not take care of adding newly
+// initialised objects.
type stack struct {
data []*big.Int
- ptr int
+}
+
+func newstack() *stack {
+ return &stack{}
}
func (st *stack) Data() []*big.Int {
- return st.data[:st.ptr]
+ return st.data
}
func (st *stack) push(d *big.Int) {
// NOTE push limit (1024) is checked in baseCheck
- stackItem := new(big.Int).Set(d)
- if len(st.data) > st.ptr {
- st.data[st.ptr] = stackItem
- } else {
- st.data = append(st.data, stackItem)
- }
- st.ptr++
+ //stackItem := new(big.Int).Set(d)
+ //st.data = append(st.data, stackItem)
+ st.data = append(st.data, d)
}
func (st *stack) pop() (ret *big.Int) {
- st.ptr--
- ret = st.data[st.ptr]
+ ret = st.data[len(st.data)-1]
+ st.data = st.data[:len(st.data)-1]
return
}
func (st *stack) len() int {
- return st.ptr
+ return len(st.data)
}
func (st *stack) swap(n int) {
@@ -60,7 +58,7 @@ func (st *stack) swap(n int) {
}
func (st *stack) dup(n int) {
- st.push(st.data[st.len()-n])
+ st.push(new(big.Int).Set(st.data[st.len()-n]))
}
func (st *stack) peek() *big.Int {
diff --git a/core/vm/vm.go b/core/vm/vm.go
index 21e0a4665..c292b45d1 100644
--- a/core/vm/vm.go
+++ b/core/vm/vm.go
@@ -24,30 +24,19 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/params"
)
// Vm implements VirtualMachine
type Vm struct {
env Environment
-
- err error
- // For logging
- debug bool
-
- BreakPoints []int64
- Stepping bool
- Fn string
-
- Recoverable bool
-
- // Will be called before the vm returns
- After func(*Context, error)
}
// New returns a new Virtual Machine
func New(env Environment) *Vm {
- return &Vm{env: env, debug: Debug, Recoverable: true}
+ return &Vm{env: env}
}
// Run loops and evaluates the contract's code with the given input data
@@ -55,17 +44,67 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
self.env.SetDepth(self.env.Depth() + 1)
defer self.env.SetDepth(self.env.Depth() - 1)
+ // User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
+ defer func() {
+ if err != nil {
+ // In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
+ context.UseGas(context.Gas)
+
+ ret = context.Return(nil)
+ }
+ }()
+
+ if context.CodeAddr != nil {
+ if p := Precompiled[context.CodeAddr.Str()]; p != nil {
+ return self.RunPrecompiled(p, input, context)
+ }
+ }
+
+ var (
+ codehash = crypto.Sha3Hash(context.Code) // codehash is used when doing jump dest caching
+ program *Program
+ )
+ if !DisableJit {
+ // Fetch program status.
+ // * If ready run using JIT
+ // * If unknown, compile in a seperate goroutine
+ // * If forced wait for compilation and run once done
+ if status := GetProgramStatus(codehash); status == progReady {
+ return RunProgram(GetProgram(codehash), self.env, context, input)
+ } else if status == progUnknown {
+ if ForceJit {
+ // Create and compile program
+ program = NewProgram(context.Code)
+ perr := CompileProgram(program)
+ if perr == nil {
+ return RunProgram(program, self.env, context, input)
+ }
+ glog.V(logger.Info).Infoln("error compiling program", err)
+ } else {
+ // create and compile the program. Compilation
+ // is done in a seperate goroutine
+ program = NewProgram(context.Code)
+ go func() {
+ err := CompileProgram(program)
+ if err != nil {
+ glog.V(logger.Info).Infoln("error compiling program", err)
+ return
+ }
+ }()
+ }
+ }
+ }
+
var (
caller = context.caller
code = context.Code
value = context.value
price = context.Price
- op OpCode // current opcode
- codehash = crypto.Sha3Hash(code) // codehash is used when doing jump dest caching
- mem = NewMemory() // bound memory
- stack = newstack() // local stack
- statedb = self.env.State() // current state
+ op OpCode // current opcode
+ mem = NewMemory() // bound memory
+ stack = newstack() // local stack
+ statedb = self.env.State() // current state
// For optimisation reason we're using uint64 as the program counter.
// It's theoretically possible to go above 2^64. The YP defines the PC to be uint256. Pratically much less so feasible.
pc = uint64(0) // program counter
@@ -89,32 +128,25 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
// User defer pattern to check for an error and, based on the error being nil or not, use all gas and return.
defer func() {
- if self.After != nil {
- self.After(context, err)
- }
-
if err != nil {
self.log(pc, op, context.Gas, cost, mem, stack, context, err)
-
- // In case of a VM exception (known exceptions) all gas consumed (panics NOT included).
- context.UseGas(context.Gas)
-
- ret = context.Return(nil)
}
}()
- if context.CodeAddr != nil {
- if p := Precompiled[context.CodeAddr.Str()]; p != nil {
- return self.RunPrecompiled(p, input, context)
- }
- }
-
// Don't bother with the execution if there's no code.
if len(code) == 0 {
return context.Return(nil), nil
}
for {
+ // Overhead of the atomic read might not be worth it
+ /* TODO this still causes a few issues in the tests
+ if program != nil && progStatus(atomic.LoadInt32(&program.status)) == progReady {
+ // move execution
+ glog.V(logger.Info).Infoln("Moved execution to JIT")
+ return runProgram(program, pc, mem, stack, self.env, context, input)
+ }
+ */
// The base for all big integer arithmetic
base := new(big.Int)
@@ -122,7 +154,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
op = context.GetOp(pc)
// calculate the new memory size and gas price for the current executing opcode
- newMemSize, cost, err = self.calculateGasAndSize(context, caller, op, statedb, mem, stack)
+ newMemSize, cost, err = calculateGasAndSize(self.env, context, caller, op, statedb, mem, stack)
if err != nil {
return nil, err
}
@@ -130,11 +162,9 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
// Use the calculated gas. When insufficient gas is present, use all gas and return an
// Out Of Gas error
if !context.UseGas(cost) {
-
- context.UseGas(context.Gas)
-
- return context.Return(nil), OutOfGasError
+ return nil, OutOfGasError
}
+
// Resize the memory calculated previously
mem.Resize(newMemSize.Uint64())
// Add a log message
@@ -376,7 +406,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
addr := common.BigToAddress(stack.pop())
balance := statedb.GetBalance(addr)
- stack.push(balance)
+ stack.push(new(big.Int).Set(balance))
case ORIGIN:
origin := self.env.Origin()
@@ -388,7 +418,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
stack.push(common.Bytes2Big(caller.Bytes()))
case CALLVALUE:
- stack.push(value)
+ stack.push(new(big.Int).Set(value))
case CALLDATALOAD:
data := getData(input, stack.pop(), common.Big32)
@@ -441,7 +471,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
mem.Set(mOff.Uint64(), l.Uint64(), codeCopy)
case GASPRICE:
- stack.push(context.Price)
+ stack.push(new(big.Int).Set(context.Price))
case BLOCKHASH:
num := stack.pop()
@@ -471,11 +501,11 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
case DIFFICULTY:
difficulty := self.env.Difficulty()
- stack.push(difficulty)
+ stack.push(new(big.Int).Set(difficulty))
case GASLIMIT:
- stack.push(self.env.GasLimit())
+ stack.push(new(big.Int).Set(self.env.GasLimit()))
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
size := uint64(op - PUSH1 + 1)
@@ -555,8 +585,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
case MSIZE:
stack.push(big.NewInt(int64(mem.Len())))
case GAS:
- stack.push(context.Gas)
-
+ stack.push(new(big.Int).Set(context.Gas))
case CREATE:
var (
@@ -652,7 +681,7 @@ func (self *Vm) Run(context *Context, input []byte) (ret []byte, err error) {
// calculateGasAndSize calculates the required given the opcode and stack items calculates the new memorysize for
// the operation. This does not reduce gas or resizes the memory.
-func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
+func calculateGasAndSize(env Environment, context *Context, caller ContextRef, op OpCode, statedb *state.StateDB, mem *Memory, stack *stack) (*big.Int, *big.Int, error) {
var (
gas = new(big.Int)
newMemSize *big.Int = new(big.Int)
@@ -759,7 +788,7 @@ func (self *Vm) calculateGasAndSize(context *Context, caller ContextRef, op OpCo
gas.Add(gas, stack.data[stack.len()-1])
if op == CALL {
- if self.env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil {
+ if env.State().GetStateObject(common.BigToAddress(stack.data[stack.len()-2])) == nil {
gas.Add(gas, params.CallNewAccountGas)
}
}
diff --git a/core/vm_env.go b/core/vm_env.go
index c1a86d63e..719829543 100644
--- a/core/vm_env.go
+++ b/core/vm_env.go
@@ -69,6 +69,10 @@ func (self *VMEnv) GetHash(n uint64) common.Hash {
func (self *VMEnv) AddLog(log *state.Log) {
self.state.AddLog(log)
}
+func (self *VMEnv) CanTransfer(from vm.Account, balance *big.Int) bool {
+ return from.Balance().Cmp(balance) >= 0
+}
+
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
return vm.Transfer(from, to, amount)
}
diff --git a/eth/backend.go b/eth/backend.go
index 4795000e0..ed46a4ab3 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -45,7 +45,6 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/nat"
- "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/whisper"
)
@@ -92,6 +91,7 @@ type Config struct {
NatSpec bool
AutoDAG bool
PowTest bool
+ ExtraData []byte
MaxPeers int
MaxPendingPeers int
@@ -378,12 +378,7 @@ func New(config *Config) (*Ethereum, error) {
eth.miner = miner.New(eth, eth.EventMux(), eth.pow)
eth.miner.SetGasPrice(config.GasPrice)
-
- extra := config.Name
- if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
- extra = extra[:params.MaximumExtraDataSize.Uint64()]
- }
- eth.miner.SetExtra([]byte(extra))
+ eth.miner.SetExtra(config.ExtraData)
if config.Shh {
eth.whisper = whisper.New()
diff --git a/jsre/ethereum_js.go b/jsre/ethereum_js.go
index 27dbed24c..012e5af70 100644
--- a/jsre/ethereum_js.go
+++ b/jsre/ethereum_js.go
@@ -1137,10 +1137,10 @@ var toHex = function (val) {
if (isString(val)) {
if (val.indexOf('-0x') === 0)
return fromDecimal(val);
- else if (!isFinite(val))
- return fromAscii(val);
else if(val.indexOf('0x') === 0)
return val;
+ else if (!isFinite(val))
+ return fromAscii(val);
}
return fromDecimal(val);
diff --git a/jsre/jsre_test.go b/jsre/jsre_test.go
index ad210932a..93dc7d1f9 100644
--- a/jsre/jsre_test.go
+++ b/jsre/jsre_test.go
@@ -19,6 +19,7 @@ package jsre
import (
"io/ioutil"
"os"
+ "path"
"testing"
"time"
@@ -40,10 +41,23 @@ func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value
return v
}
+func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
+ dir, err := ioutil.TempDir("", "jsre-test")
+ if err != nil {
+ t.Fatal("cannot create temporary directory:", err)
+ }
+ if testjs != "" {
+ if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil {
+ t.Fatal("cannot create test.js:", err)
+ }
+ }
+ return New(dir), dir
+}
+
func TestExec(t *testing.T) {
- jsre := New("/tmp")
+ jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
+ defer os.RemoveAll(dir)
- ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
err := jsre.Exec("test.js")
if err != nil {
t.Errorf("expected no error, got %v", err)
@@ -64,9 +78,9 @@ func TestExec(t *testing.T) {
}
func TestNatto(t *testing.T) {
- jsre := New("/tmp")
+ jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`)
+ defer os.RemoveAll(dir)
- ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm)
err := jsre.Exec("test.js")
if err != nil {
t.Errorf("expected no error, got %v", err)
@@ -88,7 +102,7 @@ func TestNatto(t *testing.T) {
}
func TestBind(t *testing.T) {
- jsre := New("/tmp")
+ jsre := New("")
jsre.Bind("no", &testNativeObjectBinding{})
@@ -105,9 +119,9 @@ func TestBind(t *testing.T) {
}
func TestLoadScript(t *testing.T) {
- jsre := New("/tmp")
+ jsre, dir := newWithTestJS(t, `msg = "testMsg"`)
+ defer os.RemoveAll(dir)
- ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm)
_, err := jsre.Run(`loadScript("test.js")`)
if err != nil {
t.Errorf("expected no error, got %v", err)
@@ -125,4 +139,4 @@ func TestLoadScript(t *testing.T) {
t.Errorf("expected '%v', got '%v'", exp, got)
}
jsre.Stop(false)
-}
+} \ No newline at end of file
diff --git a/miner/miner.go b/miner/miner.go
index bf6a48802..b550ed6d6 100644
--- a/miner/miner.go
+++ b/miner/miner.go
@@ -18,6 +18,7 @@
package miner
import (
+ "fmt"
"math/big"
"sync/atomic"
@@ -29,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
+ "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow"
)
@@ -139,12 +141,24 @@ func (self *Miner) Mining() bool {
return atomic.LoadInt32(&self.mining) > 0
}
-func (self *Miner) HashRate() int64 {
- return self.pow.GetHashrate()
+func (self *Miner) HashRate() (tot int64) {
+ tot += self.pow.GetHashrate()
+ // do we care this might race? is it worth we're rewriting some
+ // aspects of the worker/locking up agents so we can get an accurate
+ // hashrate?
+ for _, agent := range self.worker.agents {
+ tot += agent.GetHashRate()
+ }
+ return
}
-func (self *Miner) SetExtra(extra []byte) {
+func (self *Miner) SetExtra(extra []byte) error {
+ if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
+ return fmt.Errorf("Extra exceeds max length. %d > %v", len(extra), params.MaximumExtraDataSize)
+ }
+
self.worker.extra = extra
+ return nil
}
func (self *Miner) PendingState() *state.StateDB {
diff --git a/miner/remote_agent.go b/miner/remote_agent.go
index 674ca40ac..5c672a6e0 100644
--- a/miner/remote_agent.go
+++ b/miner/remote_agent.go
@@ -27,6 +27,11 @@ import (
"github.com/ethereum/go-ethereum/logger/glog"
)
+type hashrate struct {
+ ping time.Time
+ rate uint64
+}
+
type RemoteAgent struct {
mu sync.Mutex
@@ -36,14 +41,24 @@ type RemoteAgent struct {
currentWork *Work
work map[common.Hash]*Work
+
+ hashrateMu sync.RWMutex
+ hashrate map[common.Hash]hashrate
}
func NewRemoteAgent() *RemoteAgent {
- agent := &RemoteAgent{work: make(map[common.Hash]*Work)}
+ agent := &RemoteAgent{work: make(map[common.Hash]*Work), hashrate: make(map[common.Hash]hashrate)}
return agent
}
+func (a *RemoteAgent) SubmitHashrate(id common.Hash, rate uint64) {
+ a.hashrateMu.Lock()
+ defer a.hashrateMu.Unlock()
+
+ a.hashrate[id] = hashrate{time.Now(), rate}
+}
+
func (a *RemoteAgent) Work() chan<- *Work {
return a.workCh
}
@@ -63,7 +78,17 @@ func (a *RemoteAgent) Stop() {
close(a.workCh)
}
-func (a *RemoteAgent) GetHashRate() int64 { return 0 }
+// GetHashRate returns the accumulated hashrate of all identifier combined
+func (a *RemoteAgent) GetHashRate() (tot int64) {
+ a.hashrateMu.RLock()
+ defer a.hashrateMu.RUnlock()
+
+ // this could overflow
+ for _, hashrate := range a.hashrate {
+ tot += int64(hashrate.rate)
+ }
+ return
+}
func (a *RemoteAgent) GetWork() [3]string {
a.mu.Lock()
@@ -131,6 +156,14 @@ out:
}
}
a.mu.Unlock()
+
+ a.hashrateMu.Lock()
+ for id, hashrate := range a.hashrate {
+ if time.Since(hashrate.ping) > 10*time.Second {
+ delete(a.hashrate, id)
+ }
+ }
+ a.hashrateMu.Unlock()
}
}
}
diff --git a/p2p/discover/table.go b/p2p/discover/table.go
index 48c473475..67f7ec46f 100644
--- a/p2p/discover/table.go
+++ b/p2p/discover/table.go
@@ -164,7 +164,9 @@ func randUint(max uint32) uint32 {
// Close terminates the network listener and flushes the node database.
func (tab *Table) Close() {
- tab.net.close()
+ if tab.net != nil {
+ tab.net.close()
+ }
tab.db.close()
}
diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go
index 310fe2b7b..d259177bf 100644
--- a/p2p/discover/table_test.go
+++ b/p2p/discover/table_test.go
@@ -35,6 +35,7 @@ func TestTable_pingReplace(t *testing.T) {
doit := func(newNodeIsResponding, lastInBucketIsResponding bool) {
transport := newPingRecorder()
tab := newTable(transport, NodeID{}, &net.UDPAddr{}, "")
+ defer tab.Close()
pingSender := newNode(MustHexID("a502af0f59b2aab7746995408c79e9ca312d2793cc997e44fc55eda62f0150bbb8c59a6f9269ba3a081518b62699ee807c7c19c20125ddfccca872608af9e370"), net.IP{}, 99, 99)
// fill up the sender's bucket.
@@ -158,9 +159,7 @@ func newPingRecorder() *pingRecorder {
func (t *pingRecorder) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) {
panic("findnode called on pingRecorder")
}
-func (t *pingRecorder) close() {
- panic("close called on pingRecorder")
-}
+func (t *pingRecorder) close() {}
func (t *pingRecorder) waitping(from NodeID) error {
return nil // remote always pings
}
@@ -180,6 +179,7 @@ func TestTable_closest(t *testing.T) {
// for any node table, Target and N
tab := newTable(nil, test.Self, &net.UDPAddr{}, "")
tab.add(test.All)
+ defer tab.Close()
// check that doClosest(Target, N) returns nodes
result := tab.closest(test.Target, test.N).entries
@@ -237,6 +237,7 @@ func TestTable_ReadRandomNodesGetAll(t *testing.T) {
}
test := func(buf []*Node) bool {
tab := newTable(nil, NodeID{}, &net.UDPAddr{}, "")
+ defer tab.Close()
for i := 0; i < len(buf); i++ {
ld := cfg.Rand.Intn(len(tab.buckets))
tab.add([]*Node{nodeAtDistance(tab.self.sha, ld)})
@@ -279,6 +280,7 @@ func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value {
func TestTable_Lookup(t *testing.T) {
self := nodeAtDistance(common.Hash{}, 0)
tab := newTable(lookupTestnet, self.ID, &net.UDPAddr{}, "")
+ defer tab.Close()
// lookup on empty table returns no nodes
if results := tab.Lookup(lookupTestnet.target); len(results) > 0 {
diff --git a/p2p/nat/natupnp_test.go b/p2p/nat/natupnp_test.go
index c1e322af7..79f6d25ae 100644
--- a/p2p/nat/natupnp_test.go
+++ b/p2p/nat/natupnp_test.go
@@ -21,6 +21,7 @@ import (
"io"
"net"
"net/http"
+ "runtime"
"strings"
"testing"
@@ -28,6 +29,10 @@ import (
)
func TestUPNP_DDWRT(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skipf("disabled to avoid firewall prompt")
+ }
+
dev := &fakeIGD{
t: t,
ssdpResp: "HTTP/1.1 200 OK\r\n" +
diff --git a/rpc/api/eth.go b/rpc/api/eth.go
index 4041811f0..820ea761b 100644
--- a/rpc/api/eth.go
+++ b/rpc/api/eth.go
@@ -92,6 +92,7 @@ var (
"eth_hashrate": (*ethApi).Hashrate,
"eth_getWork": (*ethApi).GetWork,
"eth_submitWork": (*ethApi).SubmitWork,
+ "eth_submitHashrate": (*ethApi).SubmitHashrate,
"eth_resend": (*ethApi).Resend,
"eth_pendingTransactions": (*ethApi).PendingTransactions,
"eth_getTransactionReceipt": (*ethApi).GetTransactionReceipt,
@@ -573,6 +574,15 @@ func (self *ethApi) SubmitWork(req *shared.Request) (interface{}, error) {
return self.xeth.RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header)), nil
}
+func (self *ethApi) SubmitHashrate(req *shared.Request) (interface{}, error) {
+ args := new(SubmitHashRateArgs)
+ if err := self.codec.Decode(req.Params, &args); err != nil {
+ return nil, shared.NewDecodeParamError(err.Error())
+ }
+ self.xeth.RemoteMining().SubmitHashrate(common.HexToHash(args.Id), args.Rate)
+ return nil, nil
+}
+
func (self *ethApi) Resend(req *shared.Request) (interface{}, error) {
args := new(ResendArgs)
if err := self.codec.Decode(req.Params, &args); err != nil {
diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go
index 1218bd625..5a1841cbe 100644
--- a/rpc/api/eth_args.go
+++ b/rpc/api/eth_args.go
@@ -169,6 +169,37 @@ func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
return nil
}
+type SubmitHashRateArgs struct {
+ Id string
+ Rate uint64
+}
+
+func (args *SubmitHashRateArgs) UnmarshalJSON(b []byte) (err error) {
+ var obj []interface{}
+ if err := json.Unmarshal(b, &obj); err != nil {
+ return shared.NewDecodeParamError(err.Error())
+ }
+
+ if len(obj) < 2 {
+ return shared.NewInsufficientParamsError(len(obj), 2)
+ }
+
+ arg0, ok := obj[0].(string)
+ if !ok {
+ return shared.NewInvalidTypeError("hash", "not a string")
+ }
+ args.Id = arg0
+
+ arg1, ok := obj[1].(string)
+ if !ok {
+ return shared.NewInvalidTypeError("rate", "not a string")
+ }
+
+ args.Rate = common.String2Big(arg1).Uint64()
+
+ return nil
+}
+
type HashArgs struct {
Hash string
}
diff --git a/rpc/api/miner.go b/rpc/api/miner.go
index 3c3d1ee0b..5325a660a 100644
--- a/rpc/api/miner.go
+++ b/rpc/api/miner.go
@@ -17,12 +17,9 @@
package api
import (
- "fmt"
-
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
- "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
)
@@ -126,11 +123,10 @@ func (self *minerApi) SetExtra(req *shared.Request) (interface{}, error) {
return nil, err
}
- if uint64(len(args.Data)) > params.MaximumExtraDataSize.Uint64()*2 {
- return false, fmt.Errorf("extra datasize can be no longer than %v bytes", params.MaximumExtraDataSize)
+ if err := self.ethereum.Miner().SetExtra([]byte(args.Data)); err != nil {
+ return false, err
}
- self.ethereum.Miner().SetExtra([]byte(args.Data))
return true, nil
}
diff --git a/tests/state_test.go b/tests/state_test.go
index 1684614df..eb1900e1b 100644
--- a/tests/state_test.go
+++ b/tests/state_test.go
@@ -20,8 +20,25 @@ import (
"os"
"path/filepath"
"testing"
+
+ "github.com/ethereum/go-ethereum/core/vm"
)
+func init() {
+ if os.Getenv("JITVM") == "true" {
+ vm.ForceJit = true
+ } else {
+ vm.DisableJit = true
+ }
+}
+
+func BenchmarkStateCall1024(b *testing.B) {
+ fn := filepath.Join(stateTestDir, "stCallCreateCallCodeTest.json")
+ if err := BenchVmTest(fn, bconf{"Call1024BalanceTooLow", true, false}, b); err != nil {
+ b.Error(err)
+ }
+}
+
func TestStateSystemOperations(t *testing.T) {
fn := filepath.Join(stateTestDir, "stSystemOperationsTest.json")
if err := RunStateTest(fn, StateSkipTests); err != nil {
diff --git a/tests/state_test_util.go b/tests/state_test_util.go
index 7086de389..695e50852 100644
--- a/tests/state_test_util.go
+++ b/tests/state_test_util.go
@@ -23,6 +23,7 @@ import (
"io"
"math/big"
"strconv"
+ "testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
@@ -60,6 +61,61 @@ func RunStateTest(p string, skipTests []string) error {
}
+func BenchStateTest(p string, conf bconf, b *testing.B) error {
+ tests := make(map[string]VmTest)
+ if err := readJsonFile(p, &tests); err != nil {
+ return err
+ }
+ test, ok := tests[conf.name]
+ if !ok {
+ return fmt.Errorf("test not found: %s", conf.name)
+ }
+
+ pNoJit := vm.DisableJit
+ vm.DisableJit = conf.nojit
+ pForceJit := vm.ForceJit
+ vm.ForceJit = conf.precomp
+
+ // XXX Yeah, yeah...
+ env := make(map[string]string)
+ env["currentCoinbase"] = test.Env.CurrentCoinbase
+ env["currentDifficulty"] = test.Env.CurrentDifficulty
+ env["currentGasLimit"] = test.Env.CurrentGasLimit
+ env["currentNumber"] = test.Env.CurrentNumber
+ env["previousHash"] = test.Env.PreviousHash
+ if n, ok := test.Env.CurrentTimestamp.(float64); ok {
+ env["currentTimestamp"] = strconv.Itoa(int(n))
+ } else {
+ env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
+ }
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ benchStateTest(test, env, b)
+ }
+
+ vm.DisableJit = pNoJit
+ vm.ForceJit = pForceJit
+
+ return nil
+}
+
+func benchStateTest(test VmTest, env map[string]string, b *testing.B) {
+ b.StopTimer()
+ db, _ := ethdb.NewMemDatabase()
+ statedb := state.New(common.Hash{}, db)
+ for addr, account := range test.Pre {
+ obj := StateObjectFromAccount(db, addr, account)
+ statedb.SetStateObject(obj)
+ for a, v := range account.Storage {
+ obj.SetState(common.HexToHash(a), common.HexToHash(v))
+ }
+ }
+ b.StartTimer()
+
+ RunState(statedb, env, test.Exec)
+}
+
func runStateTests(tests map[string]VmTest, skipTests []string) error {
skipTest := make(map[string]bool, len(skipTests))
for _, name := range skipTests {
diff --git a/tests/util.go b/tests/util.go
index 6ee1a42db..3b94effc8 100644
--- a/tests/util.go
+++ b/tests/util.go
@@ -18,7 +18,6 @@ package tests
import (
"bytes"
- "errors"
"fmt"
"math/big"
@@ -192,18 +191,19 @@ func (self *Env) AddLog(log *state.Log) {
}
func (self *Env) Depth() int { return self.depth }
func (self *Env) SetDepth(i int) { self.depth = i }
-func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
+func (self *Env) CanTransfer(from vm.Account, balance *big.Int) bool {
if self.skipTransfer {
- // ugly hack
if self.initial {
self.initial = false
- return nil
+ return true
}
+ }
- if from.Balance().Cmp(amount) < 0 {
- return errors.New("Insufficient balance in account")
- }
+ return from.Balance().Cmp(balance) >= 0
+}
+func (self *Env) Transfer(from, to vm.Account, amount *big.Int) error {
+ if self.skipTransfer {
return nil
}
return vm.Transfer(from, to, amount)
diff --git a/tests/vm_test.go b/tests/vm_test.go
index 3674ed440..afa1424d5 100644
--- a/tests/vm_test.go
+++ b/tests/vm_test.go
@@ -21,6 +21,20 @@ import (
"testing"
)
+func BenchmarkVmAckermann32Tests(b *testing.B) {
+ fn := filepath.Join(vmTestDir, "vmPerformanceTest.json")
+ if err := BenchVmTest(fn, bconf{"ackermann32", true, false}, b); err != nil {
+ b.Error(err)
+ }
+}
+
+func BenchmarkVmFibonacci16Tests(b *testing.B) {
+ fn := filepath.Join(vmTestDir, "vmPerformanceTest.json")
+ if err := BenchVmTest(fn, bconf{"fibonacci16", true, false}, b); err != nil {
+ b.Error(err)
+ }
+}
+
// I've created a new function for each tests so it's easier to identify where the problem lies if any of them fail.
func TestVMArithmetic(t *testing.T) {
fn := filepath.Join(vmTestDir, "vmArithmeticTest.json")
diff --git a/tests/vm_test_util.go b/tests/vm_test_util.go
index e63a92558..b29dcd20f 100644
--- a/tests/vm_test_util.go
+++ b/tests/vm_test_util.go
@@ -22,6 +22,7 @@ import (
"io"
"math/big"
"strconv"
+ "testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
@@ -48,8 +49,79 @@ func RunVmTestWithReader(r io.Reader, skipTests []string) error {
return nil
}
-func RunVmTest(p string, skipTests []string) error {
+type bconf struct {
+ name string
+ precomp bool
+ nojit bool
+}
+
+func BenchVmTest(p string, conf bconf, b *testing.B) error {
+ tests := make(map[string]VmTest)
+ err := readJsonFile(p, &tests)
+ if err != nil {
+ return err
+ }
+
+ test, ok := tests[conf.name]
+ if !ok {
+ return fmt.Errorf("test not found: %s", conf.name)
+ }
+
+ pNoJit := vm.DisableJit
+ vm.DisableJit = conf.nojit
+ pForceJit := vm.ForceJit
+ vm.ForceJit = conf.precomp
+
+ env := make(map[string]string)
+ env["currentCoinbase"] = test.Env.CurrentCoinbase
+ env["currentDifficulty"] = test.Env.CurrentDifficulty
+ env["currentGasLimit"] = test.Env.CurrentGasLimit
+ env["currentNumber"] = test.Env.CurrentNumber
+ env["previousHash"] = test.Env.PreviousHash
+ if n, ok := test.Env.CurrentTimestamp.(float64); ok {
+ env["currentTimestamp"] = strconv.Itoa(int(n))
+ } else {
+ env["currentTimestamp"] = test.Env.CurrentTimestamp.(string)
+ }
+ /*
+ if conf.precomp {
+ program := vm.NewProgram(test.code)
+ err := vm.AttachProgram(program)
+ if err != nil {
+ return err
+ }
+ }
+ */
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ benchVmTest(test, env, b)
+ }
+
+ vm.DisableJit = pNoJit
+ vm.ForceJit = pForceJit
+
+ return nil
+}
+
+func benchVmTest(test VmTest, env map[string]string, b *testing.B) {
+ b.StopTimer()
+ db, _ := ethdb.NewMemDatabase()
+ statedb := state.New(common.Hash{}, db)
+ for addr, account := range test.Pre {
+ obj := StateObjectFromAccount(db, addr, account)
+ statedb.SetStateObject(obj)
+ for a, v := range account.Storage {
+ obj.SetState(common.HexToHash(a), common.HexToHash(v))
+ }
+ }
+ b.StartTimer()
+
+ RunVm(statedb, env, test.Exec)
+}
+
+func RunVmTest(p string, skipTests []string) error {
tests := make(map[string]VmTest)
err := readJsonFile(p, &tests)
if err != nil {
diff --git a/trie/encoding.go b/trie/encoding.go
index 524807f06..9c862d78f 100644
--- a/trie/encoding.go
+++ b/trie/encoding.go
@@ -16,13 +16,7 @@
package trie
-import (
- "bytes"
- "encoding/hex"
- "strings"
-)
-
-func CompactEncode(hexSlice []byte) string {
+func CompactEncode(hexSlice []byte) []byte {
terminator := 0
if hexSlice[len(hexSlice)-1] == 16 {
terminator = 1
@@ -40,15 +34,15 @@ func CompactEncode(hexSlice []byte) string {
hexSlice = append([]byte{flags, 0}, hexSlice...)
}
- var buff bytes.Buffer
- for i := 0; i < len(hexSlice); i += 2 {
- buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1]))
+ l := len(hexSlice) / 2
+ var buf = make([]byte, l)
+ for i := 0; i < l; i++ {
+ buf[i] = 16*hexSlice[2*i] + hexSlice[2*i+1]
}
-
- return buff.String()
+ return buf
}
-func CompactDecode(str string) []byte {
+func CompactDecode(str []byte) []byte {
base := CompactHexDecode(str)
base = base[:len(base)-1]
if base[0] >= 2 {
@@ -63,30 +57,23 @@ func CompactDecode(str string) []byte {
return base
}
-func CompactHexDecode(str string) []byte {
- base := "0123456789abcdef"
- var hexSlice []byte
-
- enc := hex.EncodeToString([]byte(str))
- for _, v := range enc {
- hexSlice = append(hexSlice, byte(strings.IndexByte(base, byte(v))))
+func CompactHexDecode(str []byte) []byte {
+ l := len(str)*2 + 1
+ var nibbles = make([]byte, l)
+ for i, b := range str {
+ nibbles[i*2] = b / 16
+ nibbles[i*2+1] = b % 16
}
- hexSlice = append(hexSlice, 16)
-
- return hexSlice
+ nibbles[l-1] = 16
+ return nibbles
}
-func DecodeCompact(key []byte) string {
- const base = "0123456789abcdef"
- var str string
-
- for _, v := range key {
- if v < 16 {
- str += string(base[v])
- }
+func DecodeCompact(key []byte) []byte {
+ l := len(key) / 2
+ var res = make([]byte, l)
+ for i := 0; i < l; i++ {
+ v1, v0 := key[2*i], key[2*i+1]
+ res[i] = v1*16 + v0
}
-
- res, _ := hex.DecodeString(str)
-
- return string(res)
+ return res
}
diff --git a/trie/encoding_test.go b/trie/encoding_test.go
index e52c6ba8d..e49b57ef0 100644
--- a/trie/encoding_test.go
+++ b/trie/encoding_test.go
@@ -17,9 +17,14 @@
package trie
import (
+ "encoding/hex"
+ "testing"
+
checker "gopkg.in/check.v1"
)
+func Test(t *testing.T) { checker.TestingT(t) }
+
type TrieEncodingSuite struct{}
var _ = checker.Suite(&TrieEncodingSuite{})
@@ -28,48 +33,93 @@ func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) {
// even compact encode
test1 := []byte{1, 2, 3, 4, 5}
res1 := CompactEncode(test1)
- c.Assert(res1, checker.Equals, "\x11\x23\x45")
+ c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45"))
// odd compact encode
test2 := []byte{0, 1, 2, 3, 4, 5}
res2 := CompactEncode(test2)
- c.Assert(res2, checker.Equals, "\x00\x01\x23\x45")
+ c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45"))
//odd terminated compact encode
test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
res3 := CompactEncode(test3)
- c.Assert(res3, checker.Equals, "\x20\x0f\x1c\xb8")
+ c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8"))
// even terminated compact encode
test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16}
res4 := CompactEncode(test4)
- c.Assert(res4, checker.Equals, "\x3f\x1c\xb8")
+ c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8"))
}
func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) {
exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
- res := CompactHexDecode("verb")
+ res := CompactHexDecode([]byte("verb"))
c.Assert(res, checker.DeepEquals, exp)
}
func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) {
// odd compact decode
exp := []byte{1, 2, 3, 4, 5}
- res := CompactDecode("\x11\x23\x45")
+ res := CompactDecode([]byte("\x11\x23\x45"))
c.Assert(res, checker.DeepEquals, exp)
// even compact decode
exp = []byte{0, 1, 2, 3, 4, 5}
- res = CompactDecode("\x00\x01\x23\x45")
+ res = CompactDecode([]byte("\x00\x01\x23\x45"))
c.Assert(res, checker.DeepEquals, exp)
// even terminated compact decode
exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
- res = CompactDecode("\x20\x0f\x1c\xb8")
+ res = CompactDecode([]byte("\x20\x0f\x1c\xb8"))
c.Assert(res, checker.DeepEquals, exp)
// even terminated compact decode
exp = []byte{15, 1, 12, 11, 8 /*term*/, 16}
- res = CompactDecode("\x3f\x1c\xb8")
+ res = CompactDecode([]byte("\x3f\x1c\xb8"))
+ c.Assert(res, checker.DeepEquals, exp)
+}
+
+func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) {
+ exp, _ := hex.DecodeString("012345")
+ res := DecodeCompact([]byte{0, 1, 2, 3, 4, 5})
c.Assert(res, checker.DeepEquals, exp)
+
+ exp, _ = hex.DecodeString("012345")
+ res = DecodeCompact([]byte{0, 1, 2, 3, 4, 5, 16})
+ c.Assert(res, checker.DeepEquals, exp)
+
+ exp, _ = hex.DecodeString("abcdef")
+ res = DecodeCompact([]byte{10, 11, 12, 13, 14, 15})
+ c.Assert(res, checker.DeepEquals, exp)
+}
+
+func BenchmarkCompactEncode(b *testing.B) {
+
+ testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
+ for i := 0; i < b.N; i++ {
+ CompactEncode(testBytes)
+ }
+}
+
+func BenchmarkCompactDecode(b *testing.B) {
+ testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16}
+ for i := 0; i < b.N; i++ {
+ CompactDecode(testBytes)
+ }
+}
+
+func BenchmarkCompactHexDecode(b *testing.B) {
+ testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
+ for i := 0; i < b.N; i++ {
+ CompactHexDecode(testBytes)
+ }
+
+}
+
+func BenchmarkDecodeCompact(b *testing.B) {
+ testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16}
+ for i := 0; i < b.N; i++ {
+ DecodeCompact(testBytes)
+ }
+
}
diff --git a/trie/iterator.go b/trie/iterator.go
index 698e64b34..9c4c7fbe5 100644
--- a/trie/iterator.go
+++ b/trie/iterator.go
@@ -41,7 +41,7 @@ func (self *Iterator) Next() bool {
self.Key = make([]byte, 32)
}
- key := RemTerm(CompactHexDecode(string(self.Key)))
+ key := RemTerm(CompactHexDecode(self.Key))
k := self.next(self.trie.root, key, isIterStart)
self.Key = []byte(DecodeCompact(k))
diff --git a/trie/shortnode.go b/trie/shortnode.go
index b5fc6d1f9..569d5f109 100644
--- a/trie/shortnode.go
+++ b/trie/shortnode.go
@@ -26,7 +26,7 @@ type ShortNode struct {
}
func NewShortNode(t *Trie, key []byte, value Node) *ShortNode {
- return &ShortNode{t, []byte(CompactEncode(key)), value, false}
+ return &ShortNode{t, CompactEncode(key), value, false}
}
func (self *ShortNode) Value() Node {
self.value = self.trie.trans(self.value)
@@ -49,7 +49,7 @@ func (self *ShortNode) Hash() interface{} {
}
func (self *ShortNode) Key() []byte {
- return CompactDecode(string(self.key))
+ return CompactDecode(self.key)
}
func (self *ShortNode) setDirty(dirty bool) {
diff --git a/trie/trie.go b/trie/trie.go
index e7ee86402..abf48a850 100644
--- a/trie/trie.go
+++ b/trie/trie.go
@@ -69,7 +69,7 @@ func (self *Trie) Iterator() *Iterator {
func (self *Trie) Copy() *Trie {
cpy := make([]byte, 32)
- copy(cpy, self.roothash)
+ copy(cpy, self.roothash) // NOTE: cpy isn't being used anywhere?
trie := New(nil, nil)
trie.cache = self.cache.Copy()
if self.root != nil {
@@ -131,7 +131,7 @@ func (self *Trie) Update(key, value []byte) Node {
self.mu.Lock()
defer self.mu.Unlock()
- k := CompactHexDecode(string(key))
+ k := CompactHexDecode(key)
if len(value) != 0 {
node := NewValueNode(self, value)
@@ -149,7 +149,7 @@ func (self *Trie) Get(key []byte) []byte {
self.mu.Lock()
defer self.mu.Unlock()
- k := CompactHexDecode(string(key))
+ k := CompactHexDecode(key)
n := self.get(self.root, k)
if n != nil {
@@ -164,7 +164,7 @@ func (self *Trie) Delete(key []byte) Node {
self.mu.Lock()
defer self.mu.Unlock()
- k := CompactHexDecode(string(key))
+ k := CompactHexDecode(key)
self.root = self.delete(self.root, k)
return self.root
@@ -336,7 +336,7 @@ func (self *Trie) mknode(value *common.Value) Node {
case 2:
// A value node may consists of 2 bytes.
if value.Get(0).Len() != 0 {
- key := CompactDecode(string(value.Get(0).Bytes()))
+ key := CompactDecode(value.Get(0).Bytes())
if key[len(key)-1] == 16 {
return NewShortNode(self, key, NewValueNode(self, value.Get(1).Bytes()))
} else {
diff --git a/xeth/xeth.go b/xeth/xeth.go
index 5d54c1f7e..f447a1ac3 100644
--- a/xeth/xeth.go
+++ b/xeth/xeth.go
@@ -20,8 +20,10 @@ package xeth
import (
"bytes"
"encoding/json"
+ "errors"
"fmt"
"math/big"
+ "regexp"
"sync"
"time"
@@ -45,6 +47,7 @@ var (
defaultGasPrice = big.NewInt(10000000000000) //150000000000
defaultGas = big.NewInt(90000) //500000
dappStorePre = []byte("dapp-")
+ addrReg = regexp.MustCompile(`^(0x)?[a-fA-F0-9]{40}$`)
)
// byte will be inferred
@@ -878,6 +881,10 @@ func (self *XEth) Sign(fromStr, hashStr string, didUnlock bool) (string, error)
return common.ToHex(sig), nil
}
+func isAddress(addr string) bool {
+ return addrReg.MatchString(addr)
+}
+
func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
// this minimalistic recoding is enough (works for natspec.js)
@@ -887,6 +894,10 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS
return "", err
}
+ if len(toStr) > 0 && toStr != "0x" && !isAddress(toStr) {
+ return "", errors.New("Invalid address")
+ }
+
var (
from = common.HexToAddress(fromStr)
to = common.HexToAddress(toStr)
diff --git a/xeth/xeth_test.go b/xeth/xeth_test.go
new file mode 100644
index 000000000..e649d20ef
--- /dev/null
+++ b/xeth/xeth_test.go
@@ -0,0 +1,26 @@
+package xeth
+
+import "testing"
+
+func TestIsAddress(t *testing.T) {
+ for _, invalid := range []string{
+ "0x00",
+ "0xNN",
+ "0x00000000000000000000000000000000000000NN",
+ "0xAAar000000000000000000000000000000000000",
+ } {
+ if isAddress(invalid) {
+ t.Error("Expected", invalid, "to be invalid")
+ }
+ }
+
+ for _, valid := range []string{
+ "0x0000000000000000000000000000000000000000",
+ "0xAABBbbCCccff9900000000000000000000000000",
+ "AABBbbCCccff9900000000000000000000000000",
+ } {
+ if !isAddress(valid) {
+ t.Error("Expected", valid, "to be valid")
+ }
+ }
+}