From ee5728ec039f0efe10676172430161726d1b2793 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Wed, 5 Aug 2015 18:41:00 +0200 Subject: [PATCH 01/26] fake commit for build server :( --- cmd/geth/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index cd903e62bf..74f4e90c3a 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -54,7 +54,7 @@ const ( ) var ( - gitCommit string // set via linker flag + gitCommit string // set via linker flagg nodeNameVersion string app *cli.App ) From c1d516546d221de898ddeb12a77477d992d125df Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 6 Aug 2015 03:11:10 -0400 Subject: [PATCH 02/26] faster hex-prefix codec and string -> []byte --- trie/encoding.go | 43 +++++++++++++++++-------------------------- trie/encoding_test.go | 10 +++++----- trie/iterator.go | 2 +- trie/shortnode.go | 4 ++-- trie/trie.go | 10 +++++----- 5 files changed, 30 insertions(+), 39 deletions(-) diff --git a/trie/encoding.go b/trie/encoding.go index 524807f06b..8daf503a3f 100644 --- a/trie/encoding.go +++ b/trie/encoding.go @@ -18,11 +18,9 @@ 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 @@ -45,10 +43,10 @@ func CompactEncode(hexSlice []byte) string { buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1])) } - return buff.String() + return buff.Bytes() } -func CompactDecode(str string) []byte { +func CompactDecode(str []byte) []byte { base := CompactHexDecode(str) base = base[:len(base)-1] if base[0] >= 2 { @@ -63,30 +61,23 @@ func CompactDecode(str string) []byte { return base } -func CompactHexDecode(str string) []byte { - base := "0123456789abcdef" - var hexSlice []byte +func CompactHexDecode(str []byte) []byte { + var nibbles []byte - enc := hex.EncodeToString([]byte(str)) - for _, v := range enc { - hexSlice = append(hexSlice, byte(strings.IndexByte(base, byte(v)))) + for _, b := range str { + nibbles = append(nibbles, b/16) + nibbles = append(nibbles, b%16) } - hexSlice = append(hexSlice, 16) - - return hexSlice + nibbles = append(nibbles, 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]) - } +// assumes key is odd length +func DecodeCompact(key []byte) []byte { + var res []byte + for i := 0; i < len(key)-1; i += 2 { + v1, v0 := key[i], key[i+1] + res = append(res, 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 e52c6ba8d8..faaa7f5833 100644 --- a/trie/encoding_test.go +++ b/trie/encoding_test.go @@ -48,28 +48,28 @@ func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) { 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) } diff --git a/trie/iterator.go b/trie/iterator.go index 698e64b34d..9c4c7fbe5b 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 b5fc6d1f95..569d5f1096 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 e7ee864021..abf48a8509 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 { From b23b4dbd79b4699abde4b3954c7480e137ffc3be Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 6 Aug 2015 12:27:59 +0200 Subject: [PATCH 03/26] p2p/discover: close Table during testing Not closing the table used to be fine, but now the table has a database. --- p2p/discover/table.go | 4 +++- p2p/discover/table_test.go | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 48c4734757..67f7ec46fb 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 310fe2b7bc..d259177bf8 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 { From c32073b11f12c3735c117b3b3c814505974d5a92 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 6 Aug 2015 11:58:14 +0200 Subject: [PATCH 04/26] miner, rpc: added submit hashrate for remote agents --- miner/miner.go | 11 +++++++++-- miner/remote_agent.go | 37 +++++++++++++++++++++++++++++++++++-- rpc/api/eth.go | 10 ++++++++++ rpc/api/eth_args.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/miner/miner.go b/miner/miner.go index bf6a488020..5087785794 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -139,8 +139,15 @@ 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) { diff --git a/miner/remote_agent.go b/miner/remote_agent.go index 674ca40acd..5c672a6e02 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/rpc/api/eth.go b/rpc/api/eth.go index 4041811f06..820ea761b3 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 1218bd6254..5a1841cbed 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 } From 74f6d90153a4d62eefaa8a49f98dc6ed8e0100f6 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 6 Aug 2015 13:29:06 +0200 Subject: [PATCH 05/26] cmd/utils, core: disable exp diff for olympic net --- cmd/utils/cmd.go | 2 ++ core/chain_util.go | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index f8f7f6376e..983762db8f 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -21,6 +21,7 @@ import ( "bufio" "fmt" "io" + "math" "math/big" "os" "os/signal" @@ -152,6 +153,7 @@ func InitOlympic() { params.MaximumExtraDataSize = big.NewInt(1024) NetworkIdFlag.Value = 0 core.BlockReward = big.NewInt(1.5e+18) + core.ExpDiffPeriod = big.NewInt(math.MaxInt64) } func FormatTransactionData(data string) []byte { diff --git a/core/chain_util.go b/core/chain_util.go index 34f6c8d0aa..84b462ce3d 100644 --- a/core/chain_util.go +++ b/core/chain_util.go @@ -32,7 +32,7 @@ import ( var ( blockHashPre = []byte("block-hash-") blockNumPre = []byte("block-num-") - expDiffPeriod = big.NewInt(100000) + ExpDiffPeriod = big.NewInt(100000) ) // CalcDifficulty is the difficulty adjustment algorithm. It returns @@ -57,7 +57,7 @@ func CalcDifficulty(time, parentTime uint64, parentNumber, parentDiff *big.Int) } periodCount := new(big.Int).Add(parentNumber, common.Big1) - periodCount.Div(periodCount, expDiffPeriod) + periodCount.Div(periodCount, ExpDiffPeriod) if periodCount.Cmp(common.Big1) > 0 { // diff = diff + 2^(periodCount - 2) expDiff := periodCount.Sub(periodCount, common.Big2) From 78b101e15d4a92d9f0f1d9ca49e45d5699adb3d2 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 6 Aug 2015 15:15:36 +0100 Subject: [PATCH 06/26] common: remove windows path functions They were unused and their tests failed on Windows. --- cmd/utils/flags.go | 2 +- common/path.go | 11 ---------- common/path_test.go | 52 --------------------------------------------- common/size_test.go | 2 +- 4 files changed, 2 insertions(+), 65 deletions(-) delete mode 100644 common/path_test.go diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 815d481244..cf969805da 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -478,7 +478,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/path.go b/common/path.go index 0d7adb9617..8b3c7d14b8 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 71ffd5fe1e..0000000000 --- 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 . - -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 8709a02374..ce19cab69f 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)) From eae1191904fd739cdffdd2903509a44a35c4c2f2 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 6 Aug 2015 15:18:32 +0100 Subject: [PATCH 07/26] cmd/utils: fix path expansion on windows --- cmd/utils/customflags.go | 9 +++------ cmd/utils/customflags_test.go | 5 +---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go index e7efed4e3c..4450065c14 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 0fb0af63b7..de39ca36a1 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 { From 383201996463ddb91aa754f81d00d472e3436380 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 6 Aug 2015 15:19:09 +0100 Subject: [PATCH 08/26] common/compiler, common/docserver, jsre: fix tests on windows --- common/compiler/solidity_test.go | 5 +++-- common/docserver/docserver.go | 1 - common/docserver/docserver_test.go | 19 ++++++++++++------ jsre/jsre_test.go | 32 +++++++++++++++++++++--------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/common/compiler/solidity_test.go b/common/compiler/solidity_test.go index 8255e8e2d9..3303bc15a1 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 fa120fb380..dac542ba7e 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 92e39d167a..632603addb 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/jsre/jsre_test.go b/jsre/jsre_test.go index ad210932a0..93dc7d1f9a 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 TestExec(t *testing.T) { - jsre := New("/tmp") +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, 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 From 6ee908848cef9ec82e4d20e1bb79063de5500207 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 6 Aug 2015 15:19:40 +0100 Subject: [PATCH 09/26] p2p/nat: disable UPnP test on windows --- p2p/nat/natupnp_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/p2p/nat/natupnp_test.go b/p2p/nat/natupnp_test.go index c1e322af70..79f6d25ae8 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" + From 803096ca0f262be7d03081482777c3e293f5f7ac Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 6 Aug 2015 15:33:14 +0100 Subject: [PATCH 10/26] .gitattributes: add --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..dfe0770424 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto From 46c95940812ccd3f474dc5fab7a5351351e8f105 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 6 Aug 2015 12:39:07 -0400 Subject: [PATCH 11/26] trie: run codec tests, add benchmarks, faster --- trie/encoding.go | 36 ++++++++++++--------------- trie/encoding_test.go | 58 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 24 deletions(-) diff --git a/trie/encoding.go b/trie/encoding.go index 8daf503a3f..9c862d78fa 100644 --- a/trie/encoding.go +++ b/trie/encoding.go @@ -16,10 +16,6 @@ package trie -import ( - "bytes" -) - func CompactEncode(hexSlice []byte) []byte { terminator := 0 if hexSlice[len(hexSlice)-1] == 16 { @@ -38,12 +34,12 @@ func CompactEncode(hexSlice []byte) []byte { 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.Bytes() + return buf } func CompactDecode(str []byte) []byte { @@ -62,22 +58,22 @@ func CompactDecode(str []byte) []byte { } func CompactHexDecode(str []byte) []byte { - var nibbles []byte - - for _, b := range str { - nibbles = append(nibbles, b/16) - nibbles = append(nibbles, b%16) + 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 } - nibbles = append(nibbles, 16) + nibbles[l-1] = 16 return nibbles } -// assumes key is odd length func DecodeCompact(key []byte) []byte { - var res []byte - for i := 0; i < len(key)-1; i += 2 { - v1, v0 := key[i], key[i+1] - res = append(res, v1*16+v0) + 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 } return res } diff --git a/trie/encoding_test.go b/trie/encoding_test.go index faaa7f5833..e49b57ef02 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,22 +33,22 @@ 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) { @@ -73,3 +78,48 @@ func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) { 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) + } + +} From cf7cef4293cf1b1a9b393f1030f8c8e648c2975b Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 7 Aug 2015 09:52:12 +0200 Subject: [PATCH 12/26] xeth: added address hex check and length check --- xeth/xeth.go | 11 +++++++++++ xeth/xeth_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 xeth/xeth_test.go diff --git a/xeth/xeth.go b/xeth/xeth.go index 5d54c1f7e4..372068c148 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 !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 0000000000..e649d20ef1 --- /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") + } + } +} From 785b3e7a575f26f3c33e3369d8a75fb131f90584 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 6 Aug 2015 11:20:15 +0200 Subject: [PATCH 13/26] cmd/geth, eth: added canonical extra data Implemented canonical extra data according to https://github.com/ethereum/wiki/wiki/Extra-Data --- cmd/geth/main.go | 32 ++++++++++++++++++++++++++++++-- eth/backend.go | 9 ++------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 74f4e90c3a..ddfa8e661f 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,11 +51,14 @@ import ( ) const ( - ClientIdentifier = "Geth" - Version = "1.0.1" + ClientIdentifier = "Geth " + VersionMajor = 1 + VersionMinor = 0 + VersionPatch = 1 ) var ( + Version = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch) gitCommit string // set via linker flagg nodeNameVersion string app *cli.App @@ -346,6 +351,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 +379,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/eth/backend.go b/eth/backend.go index 4795000e09..ed46a4ab36 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() From 132df860d90c163a8be55260bdd219892f9e1bef Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 7 Aug 2015 12:12:05 +0200 Subject: [PATCH 14/26] miner, rpc: added length check for extra data --- miner/miner.go | 9 ++++++++- rpc/api/miner.go | 8 ++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/miner/miner.go b/miner/miner.go index bf6a488020..3095d98281 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" ) @@ -143,8 +145,13 @@ func (self *Miner) HashRate() int64 { return self.pow.GetHashrate() } -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/rpc/api/miner.go b/rpc/api/miner.go index 3c3d1ee0b3..5325a660af 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 } From a33726b7db98fcb754c6d9d96538ce21cfc35f9a Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 7 Aug 2015 12:33:12 +0200 Subject: [PATCH 15/26] web3: regression. Fixes #1613 --- jsre/ethereum_js.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jsre/ethereum_js.go b/jsre/ethereum_js.go index 27dbed24cc..012e5af709 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); From 846f34f78b5f76233655d0cf3611706e99f2efe2 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 17 Jul 2015 23:09:36 +0200 Subject: [PATCH 16/26] core/vm, tests: implemented semi-jit vm * changed stack and removed stack ptr. Let go decide on slice reuse. --- Makefile | 6 +- cmd/evm/main.go | 26 +- cmd/geth/main.go | 3 + cmd/utils/flags.go | 31 ++- core/block_processor.go | 2 - core/chain_manager.go | 2 +- core/vm/context.go | 1 + core/vm/contracts.go | 2 +- core/vm/gas.go | 4 +- core/vm/instructions.go | 587 +++++++++++++++++++++++++++++++++++++++ core/vm/jit.go | 537 +++++++++++++++++++++++++++++++++++ core/vm/jit_test.go | 119 ++++++++ core/vm/settings.go | 24 ++ core/vm/stack.go | 21 +- core/vm/vm.go | 125 +++++---- tests/state_test.go | 17 ++ tests/state_test_util.go | 56 ++++ tests/vm_test.go | 14 + tests/vm_test_util.go | 74 ++++- 19 files changed, 1572 insertions(+), 79 deletions(-) create mode 100644 core/vm/instructions.go create mode 100644 core/vm/jit.go create mode 100644 core/vm/jit_test.go create mode 100644 core/vm/settings.go diff --git a/Makefile b/Makefile index 03e3bf4c6f..3478b54333 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 9659943820..7dd375b148 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() { diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 74f4e90c3a..68c66a4abe 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -307,6 +307,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, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index cf969805da..e7b30cfa0d 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) diff --git a/core/block_processor.go b/core/block_processor.go index 5a2ad8377f..6ed1bc8ef1 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 fc1d1304fd..1b792933c9 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/vm/context.go b/core/vm/context.go index 162666ef22..d17934ba59 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 2d70f173e9..b965fa095c 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/gas.go b/core/vm/gas.go index af2e586a71..b2f068e6e1 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 0000000000..7793ff1698 --- /dev/null +++ b/core/vm/instructions.go @@ -0,0 +1,587 @@ +// 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 . +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(new(big.Int).Add(x, y))) +} + +func opSub(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + stack.push(U256(new(big.Int).Sub(x, y))) +} + +func opMul(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + stack.push(U256(new(big.Int).Mul(x, y))) +} + +func opDiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x, y := stack.pop(), stack.pop() + + if y.Cmp(common.Big0) != 0 { + base.Div(x, y) + } + + // pop result back on the stack + stack.push(U256(base)) +} + +func opSdiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x, y := S256(stack.pop()), S256(stack.pop()) + + if y.Cmp(common.Big0) == 0 { + base.Set(common.Big0) + } else { + n := new(big.Int) + if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 { + n.SetInt64(-1) + } else { + n.SetInt64(1) + } + + base.Div(x.Abs(x), y.Abs(y)).Mul(base, n) + + U256(base) + } + + stack.push(base) +} + +func opMod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x, y := stack.pop(), stack.pop() + + if y.Cmp(common.Big0) == 0 { + base.Set(common.Big0) + } else { + base.Mod(x, y) + } + + U256(base) + + stack.push(base) +} + +func opSmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x, y := S256(stack.pop()), S256(stack.pop()) + + if y.Cmp(common.Big0) == 0 { + base.Set(common.Big0) + } else { + n := new(big.Int) + if x.Cmp(common.Big0) < 0 { + n.SetInt64(-1) + } else { + n.SetInt64(1) + } + + base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n) + + U256(base) + } + + stack.push(base) +} + +func opExp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x, y := stack.pop(), stack.pop() + + base.Exp(x, y, Pow256) + + U256(base) + + stack.push(base) +} + +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 := new(big.Int).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) + } + + num = U256(num) + + stack.push(num) + } +} + +func opNot(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + stack.push(U256(new(big.Int).Not(stack.pop()))) +} + +func opLt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + // x < y + if x.Cmp(y) < 0 { + stack.push(common.BigTrue) + } else { + stack.push(common.BigFalse) + } +} + +func opGt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + // x > y + if x.Cmp(y) > 0 { + stack.push(common.BigTrue) + } else { + stack.push(common.BigFalse) + } +} + +func opSlt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := S256(stack.pop()), S256(stack.pop()) + + // x < y + if x.Cmp(S256(y)) < 0 { + stack.push(common.BigTrue) + } else { + stack.push(common.BigFalse) + } +} + +func opSgt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := S256(stack.pop()), S256(stack.pop()) + + // x > y + if x.Cmp(y) > 0 { + stack.push(common.BigTrue) + } else { + stack.push(common.BigFalse) + } +} + +func opEq(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + // x == y + if x.Cmp(y) == 0 { + stack.push(common.BigTrue) + } else { + stack.push(common.BigFalse) + } +} + +func opIszero(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x := stack.pop() + if x.Cmp(common.BigFalse) > 0 { + stack.push(common.BigFalse) + } else { + stack.push(common.BigTrue) + } +} + +func opAnd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + stack.push(new(big.Int).And(x, y)) +} +func opOr(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + stack.push(new(big.Int).Or(x, y)) +} +func opXor(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + x, y := stack.pop(), stack.pop() + + stack.push(new(big.Int).Xor(x, y)) +} +func opByte(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + th, val := stack.pop(), stack.pop() + + if th.Cmp(big.NewInt(32)) < 0 { + byt := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()])) + + base.Set(byt) + } else { + base.Set(common.BigFalse) + } + + stack.push(base) +} +func opAddmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x := stack.pop() + y := stack.pop() + z := stack.pop() + + if z.Cmp(Zero) > 0 { + add := new(big.Int).Add(x, y) + base.Mod(add, z) + + base = U256(base) + } + + stack.push(base) +} +func opMulmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { + base := new(big.Int) + x := stack.pop() + y := stack.pop() + z := stack.pop() + + if z.Cmp(Zero) > 0 { + mul := new(big.Int).Mul(x, y) + base.Mod(mul, z) + + U256(base) + } + + stack.push(base) +} + +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.BigD(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(common.Big0) + } +} + +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(common.BigFalse) + + } 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(common.BigFalse) + + } else { + stack.push(common.BigTrue) + + 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(common.BigFalse) + + } else { + stack.push(common.BigTrue) + + 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 0000000000..a773092234 --- /dev/null +++ b/core/vm/jit.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 . +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-existant +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] + + gas.Add(gas, params.LogGas) + gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(n)), params.LogTopicGas)) + gas.Add(gas, new(big.Int).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 { + oldSize := toWordSize(big.NewInt(int64(mem.Len()))) + pow := new(big.Int).Exp(oldSize, common.Big2, Zero) + linCoef := new(big.Int).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 = new(big.Int).Mul(newMemSizeWords, params.MemoryGas) + quadCoef = new(big.Int).Div(pow, params.QuadCoeffDiv) + newTotalFee := new(big.Int).Add(linCoef, quadCoef) + + fee := new(big.Int).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 0000000000..70432d47b0 --- /dev/null +++ b/core/vm/jit_test.go @@ -0,0 +1,119 @@ +// 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 . +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) 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 0000000000..9e30d3adde --- /dev/null +++ b/core/vm/settings.go @@ -0,0 +1,24 @@ +// 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 . +package vm + +var ( + DisableJit bool // 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 3d669b2f20..23c1094550 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -27,32 +27,27 @@ func newstack() *stack { type stack struct { data []*big.Int - ptr int } 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 +55,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 21e0a46656..c292b45d1a 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/tests/state_test.go b/tests/state_test.go index 1684614df2..eb1900e1b8 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 7086de3897..695e508522 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/vm_test.go b/tests/vm_test.go index 3674ed440e..6b6b179fd9 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, true}, 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 e63a92558d..b29dcd20f6 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 { From 184e9ae9a81df2db6381e18d3daa035d913ae341 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Sun, 2 Aug 2015 02:20:41 +0200 Subject: [PATCH 17/26] core, tests: reduced state copy by N calls Reduced the amount of state copied that are required by N calls by doing a balance check prior to any state modifications. --- cmd/evm/main.go | 3 +++ core/execution.go | 27 +++++++++++++++++---------- core/vm/environment.go | 1 + core/vm/instructions.go | 1 + core/vm/jit.go | 3 ++- core/vm/jit_test.go | 3 +++ core/vm/settings.go | 1 + core/vm_env.go | 4 ++++ tests/util.go | 14 +++++++------- 9 files changed, 39 insertions(+), 18 deletions(-) diff --git a/cmd/evm/main.go b/cmd/evm/main.go index 7dd375b148..be6546c953 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -206,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/core/execution.go b/core/execution.go index 699bad9a38..3a136515df 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/environment.go b/core/vm/environment.go index 723924b6fc..5a1bf32010 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/instructions.go b/core/vm/instructions.go index 7793ff1698..d7605e5a29 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . + package vm import ( diff --git a/core/vm/jit.go b/core/vm/jit.go index a773092234..c66630ae83 100644 --- a/core/vm/jit.go +++ b/core/vm/jit.go @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . + package vm import ( @@ -48,7 +49,7 @@ func SetJITCacheSize(size int) { programs, _ = lru.New(size) } -// GetProgram returns the program by id or nil when non-existant +// 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) diff --git a/core/vm/jit_test.go b/core/vm/jit_test.go index 70432d47b0..5b3feea992 100644 --- a/core/vm/jit_test.go +++ b/core/vm/jit_test.go @@ -105,6 +105,9 @@ 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 } diff --git a/core/vm/settings.go b/core/vm/settings.go index 9e30d3adde..b94efd9ab1 100644 --- a/core/vm/settings.go +++ b/core/vm/settings.go @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . + package vm var ( diff --git a/core/vm_env.go b/core/vm_env.go index c1a86d63e2..7198295437 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/tests/util.go b/tests/util.go index 6ee1a42db7..3b94effc8f 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) From ac697326a6045eaa760b159e4bda37c57be61cbf Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 6 Aug 2015 23:06:47 +0200 Subject: [PATCH 18/26] core/vm: reduced big int allocations Reduced big int allocation by making stack items modifiable. Instead of adding items such as `common.Big0` to the stack, `new(big.Int)` is added instead. One must expect that any item that is added to the stack might change. --- core/vm/instructions.go | 171 ++++++++++++++-------------------------- core/vm/jit.go | 17 ++-- core/vm/stack.go | 11 ++- tests/vm_test.go | 2 +- 4 files changed, 78 insertions(+), 123 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index d7605e5a29..6b7b412209 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -46,40 +46,33 @@ func opStaticJump(instr instruction, ret *big.Int, env Environment, context *Con func opAdd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { x, y := stack.pop(), stack.pop() - - stack.push(U256(new(big.Int).Add(x, y))) + 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(new(big.Int).Sub(x, y))) + 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(new(big.Int).Mul(x, y))) + stack.push(U256(x.Mul(x, y))) } func opDiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) x, y := stack.pop(), stack.pop() - if y.Cmp(common.Big0) != 0 { - base.Div(x, y) + stack.push(U256(x.Div(x, y))) + } else { + stack.push(new(big.Int)) } - - // pop result back on the stack - stack.push(U256(base)) } func opSdiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) x, y := S256(stack.pop()), S256(stack.pop()) - if y.Cmp(common.Big0) == 0 { - base.Set(common.Big0) + stack.push(new(big.Int)) + return } else { n := new(big.Int) if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 { @@ -88,35 +81,27 @@ func opSdiv(instr instruction, env Environment, context *Context, memory *Memory n.SetInt64(1) } - base.Div(x.Abs(x), y.Abs(y)).Mul(base, n) + res := x.Div(x.Abs(x), y.Abs(y)) + res.Mul(res, n) - U256(base) + stack.push(U256(res)) } - - stack.push(base) } func opMod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) x, y := stack.pop(), stack.pop() - if y.Cmp(common.Big0) == 0 { - base.Set(common.Big0) + stack.push(new(big.Int)) } else { - base.Mod(x, y) + stack.push(U256(x.Mod(x, y))) } - - U256(base) - - stack.push(base) } func opSmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) x, y := S256(stack.pop()), S256(stack.pop()) if y.Cmp(common.Big0) == 0 { - base.Set(common.Big0) + stack.push(new(big.Int)) } else { n := new(big.Int) if x.Cmp(common.Big0) < 0 { @@ -125,23 +110,16 @@ func opSmod(instr instruction, env Environment, context *Context, memory *Memory n.SetInt64(1) } - base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n) + res := x.Mod(x.Abs(x), y.Abs(y)) + res.Mul(res, n) - U256(base) + stack.push(U256(res)) } - - stack.push(base) } func opExp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) x, y := stack.pop(), stack.pop() - - base.Exp(x, y, Pow256) - - U256(base) - - stack.push(base) + stack.push(U256(x.Exp(x, y, Pow256))) } func opSignExtend(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { @@ -149,7 +127,7 @@ func opSignExtend(instr instruction, env Environment, context *Context, memory * if back.Cmp(big.NewInt(31)) < 0 { bit := uint(back.Uint64()*8 + 7) num := stack.pop() - mask := new(big.Int).Lsh(common.Big1, bit) + mask := back.Lsh(common.Big1, bit) mask.Sub(mask, common.Big1) if common.BitTest(num, int(bit)) { num.Or(num, mask.Not(mask)) @@ -157,145 +135,116 @@ func opSignExtend(instr instruction, env Environment, context *Context, memory * num.And(num, mask) } - num = U256(num) - - stack.push(num) + stack.push(U256(num)) } } func opNot(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - stack.push(U256(new(big.Int).Not(stack.pop()))) + 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() - - // x < y if x.Cmp(y) < 0 { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) } else { - stack.push(common.BigFalse) + stack.push(new(big.Int)) } } func opGt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { x, y := stack.pop(), stack.pop() - - // x > y if x.Cmp(y) > 0 { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) } else { - stack.push(common.BigFalse) + 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()) - - // x < y if x.Cmp(S256(y)) < 0 { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) } else { - stack.push(common.BigFalse) + 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()) - - // x > y if x.Cmp(y) > 0 { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) } else { - stack.push(common.BigFalse) + stack.push(new(big.Int)) } } func opEq(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { x, y := stack.pop(), stack.pop() - - // x == y if x.Cmp(y) == 0 { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) } else { - stack.push(common.BigFalse) + stack.push(new(big.Int)) } } func opIszero(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { x := stack.pop() - if x.Cmp(common.BigFalse) > 0 { - stack.push(common.BigFalse) + if x.Cmp(common.Big0) > 0 { + stack.push(new(big.Int)) } else { - stack.push(common.BigTrue) + 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(new(big.Int).And(x, y)) + 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(new(big.Int).Or(x, y)) + 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(new(big.Int).Xor(x, y)) + stack.push(x.Xor(x, y)) } func opByte(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) th, val := stack.pop(), stack.pop() - if th.Cmp(big.NewInt(32)) < 0 { - byt := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()])) - - base.Set(byt) + byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()])) + stack.push(byte) } else { - base.Set(common.BigFalse) + stack.push(new(big.Int)) } - - stack.push(base) } func opAddmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) - x := stack.pop() - y := stack.pop() - z := stack.pop() - + x, y, z := stack.pop(), stack.pop(), stack.pop() if z.Cmp(Zero) > 0 { - add := new(big.Int).Add(x, y) - base.Mod(add, z) - - base = U256(base) + add := x.Add(x, y) + add.Mod(add, z) + stack.push(U256(add)) + } else { + stack.push(new(big.Int)) } - - stack.push(base) } func opMulmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { - base := new(big.Int) - x := stack.pop() - y := stack.pop() - z := stack.pop() - + x, y, z := stack.pop(), stack.pop(), stack.pop() if z.Cmp(Zero) > 0 { - mul := new(big.Int).Mul(x, y) - base.Mod(mul, z) - - U256(base) + mul := x.Mul(x, y) + mul.Mod(mul, z) + stack.push(U256(mul)) + } else { + stack.push(new(big.Int)) } - - stack.push(base) } 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.BigD(hash)) + stack.push(common.BytesToBig(hash)) } func opAddress(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) { @@ -383,7 +332,7 @@ func opBlockhash(instr instruction, env Environment, context *Context, memory *M if num.Cmp(n) > 0 && num.Cmp(env.BlockNumber()) < 0 { stack.push(env.GetHash(num.Uint64()).Big()) } else { - stack.push(common.Big0) + stack.push(new(big.Int)) } } @@ -497,7 +446,7 @@ func opCreate(instr instruction, env Environment, context *Context, memory *Memo context.UseGas(context.Gas) ret, suberr, ref := env.Create(context, input, gas, context.Price, value) if suberr != nil { - stack.push(common.BigFalse) + stack.push(new(big.Int)) } else { // gas < len(ret) * Createinstr.dataGas == NO_CODE @@ -535,10 +484,10 @@ func opCall(instr instruction, env Environment, context *Context, memory *Memory ret, err := env.Call(context, address, args, gas, context.Price, value) if err != nil { - stack.push(common.BigFalse) + stack.push(new(big.Int)) } else { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } @@ -566,10 +515,10 @@ func opCallCode(instr instruction, env Environment, context *Context, memory *Me ret, err := env.CallCode(context, address, args, gas, context.Price, value) if err != nil { - stack.push(common.BigFalse) + stack.push(new(big.Int)) } else { - stack.push(common.BigTrue) + stack.push(big.NewInt(1)) memory.Set(retOffset.Uint64(), retSize.Uint64(), ret) } diff --git a/core/vm/jit.go b/core/vm/jit.go index c66630ae83..d5c2d78300 100644 --- a/core/vm/jit.go +++ b/core/vm/jit.go @@ -404,9 +404,10 @@ func jitCalculateGasAndSize(env Environment, context *Context, caller ContextRef mSize, mStart := stack.data[stack.len()-2], stack.data[stack.len()-1] + add := new(big.Int) gas.Add(gas, params.LogGas) - gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(n)), params.LogTopicGas)) - gas.Add(gas, new(big.Int).Mul(mSize, params.LogDataGas)) + 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: @@ -496,18 +497,20 @@ func jitCalculateGasAndSize(env Environment, context *Context, caller ContextRef 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 := new(big.Int).Mul(oldSize, params.MemoryGas) + 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 = new(big.Int).Mul(newMemSizeWords, params.MemoryGas) - quadCoef = new(big.Int).Div(pow, params.QuadCoeffDiv) - newTotalFee := new(big.Int).Add(linCoef, quadCoef) + linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas) + quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv) + newTotalFee := linCoef.Add(linCoef, quadCoef) - fee := new(big.Int).Sub(newTotalFee, oldTotalFee) + fee := newTotalFee.Sub(newTotalFee, oldTotalFee) gas.Add(gas, fee) } } diff --git a/core/vm/stack.go b/core/vm/stack.go index 23c1094550..009ac9e1b6 100644 --- a/core/vm/stack.go +++ b/core/vm/stack.go @@ -21,14 +21,17 @@ 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 } +func newstack() *stack { + return &stack{} +} + func (st *stack) Data() []*big.Int { return st.data } diff --git a/tests/vm_test.go b/tests/vm_test.go index 6b6b179fd9..afa1424d57 100644 --- a/tests/vm_test.go +++ b/tests/vm_test.go @@ -30,7 +30,7 @@ func BenchmarkVmAckermann32Tests(b *testing.B) { func BenchmarkVmFibonacci16Tests(b *testing.B) { fn := filepath.Join(vmTestDir, "vmPerformanceTest.json") - if err := BenchVmTest(fn, bconf{"fibonacci16", true, true}, b); err != nil { + if err := BenchVmTest(fn, bconf{"fibonacci16", true, false}, b); err != nil { b.Error(err) } } From b6c5b3b4a77eb2e725cf2e9b257fff7b2861e95e Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Fri, 7 Aug 2015 14:30:45 +0200 Subject: [PATCH 19/26] xeth: fixed contract addr check --- xeth/xeth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index 372068c148..f447a1ac35 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -894,7 +894,7 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS return "", err } - if !isAddress(toStr) { + if len(toStr) > 0 && toStr != "0x" && !isAddress(toStr) { return "", errors.New("Invalid address") } From 3ccab5a1e8c4a6c4245f62dfaf7120dc54745997 Mon Sep 17 00:00:00 2001 From: caktux Date: Fri, 7 Aug 2015 14:13:33 -0400 Subject: [PATCH 20/26] string version for build server --- cmd/geth/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index ddfa8e661f..df44397035 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -52,13 +52,13 @@ import ( const ( ClientIdentifier = "Geth " + Version = "1.0.1" VersionMajor = 1 VersionMinor = 0 VersionPatch = 1 ) var ( - Version = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch) gitCommit string // set via linker flagg nodeNameVersion string app *cli.App From a23478c0be94e1e727a64d20341b8d6f98d7f0a0 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 6 Aug 2015 19:57:39 +0200 Subject: [PATCH 21/26] core, eth, trie, xeth: merged state, chain, extra databases in one --- cmd/geth/chaincmd.go | 26 ++--- cmd/utils/flags.go | 18 ++-- core/bench_test.go | 4 +- core/block_processor.go | 16 ++- core/block_processor_test.go | 8 +- core/chain_makers.go | 6 +- core/chain_makers_test.go | 4 +- core/chain_manager.go | 48 ++++----- core/chain_manager_test.go | 18 ++-- core/genesis.go | 18 ++-- core/manager.go | 5 +- eth/backend.go | 192 ++++++++++++++++++++++------------- eth/protocol_test.go | 4 +- ethdb/database.go | 5 +- miner/worker.go | 10 +- rpc/api/debug.go | 2 +- tests/block_test_util.go | 2 +- trie/cache.go | 6 +- trie/trie.go | 2 + xeth/state.go | 2 +- xeth/xeth.go | 16 +-- 21 files changed, 224 insertions(+), 188 deletions(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 2d8eb15c29..876b8c6bad 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -74,10 +74,10 @@ func importChain(ctx *cli.Context) { if len(ctx.Args()) != 1 { utils.Fatalf("This command requires an argument.") } - chain, blockDB, stateDB, extraDB := utils.MakeChain(ctx) + chain, chainDb := utils.MakeChain(ctx) start := time.Now() err := utils.ImportChain(chain, ctx.Args().First()) - closeAll(blockDB, stateDB, extraDB) + chainDb.Close() if err != nil { utils.Fatalf("Import error: %v", err) } @@ -88,7 +88,7 @@ func exportChain(ctx *cli.Context) { if len(ctx.Args()) < 1 { utils.Fatalf("This command requires an argument.") } - chain, _, _, _ := utils.MakeChain(ctx) + chain, _ := utils.MakeChain(ctx) start := time.Now() var err error @@ -136,8 +136,8 @@ func removeDB(ctx *cli.Context) { func upgradeDB(ctx *cli.Context) { glog.Infoln("Upgrading blockchain database") - chain, blockDB, stateDB, extraDB := utils.MakeChain(ctx) - v, _ := blockDB.Get([]byte("BlockchainVersion")) + chain, chainDb := utils.MakeChain(ctx) + v, _ := chainDb.Get([]byte("BlockchainVersion")) bcVersion := int(common.NewValue(v).Uint()) if bcVersion == 0 { bcVersion = core.BlockChainVersion @@ -149,15 +149,14 @@ func upgradeDB(ctx *cli.Context) { if err := utils.ExportChain(chain, exportFile); err != nil { utils.Fatalf("Unable to export chain for reimport %s", err) } - closeAll(blockDB, stateDB, extraDB) - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "blockchain")) - os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state")) + chainDb.Close() + os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "chaindata")) // Import the chain file. - chain, blockDB, stateDB, extraDB = utils.MakeChain(ctx) - blockDB.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes()) + chain, chainDb = utils.MakeChain(ctx) + chainDb.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes()) err := utils.ImportChain(chain, exportFile) - closeAll(blockDB, stateDB, extraDB) + chainDb.Close() if err != nil { utils.Fatalf("Import error %v (a backup is made in %s, use the import command to import it)", err, exportFile) } else { @@ -167,7 +166,7 @@ func upgradeDB(ctx *cli.Context) { } func dump(ctx *cli.Context) { - chain, _, stateDB, _ := utils.MakeChain(ctx) + chain, chainDb := utils.MakeChain(ctx) for _, arg := range ctx.Args() { var block *types.Block if hashish(arg) { @@ -180,10 +179,11 @@ func dump(ctx *cli.Context) { fmt.Println("{}") utils.Fatalf("block not found") } else { - state := state.New(block.Root(), stateDB) + state := state.New(block.Root(), chainDb) fmt.Printf("%s\n", state.Dump()) } } + chainDb.Close() } // hashish returns true for strings that look like hashes. diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index cf969805da..e0ea7116d1 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -435,23 +435,17 @@ func SetupLogger(ctx *cli.Context) { } // MakeChain creates a chain manager from set command line flags. -func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) { +func MakeChain(ctx *cli.Context) (chain *core.ChainManager, chainDb common.Database) { datadir := ctx.GlobalString(DataDirFlag.Name) cache := ctx.GlobalInt(CacheFlag.Name) var err error - if blockDB, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "blockchain"), cache); err != nil { - Fatalf("Could not open database: %v", err) - } - if stateDB, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "state"), cache); err != nil { - Fatalf("Could not open database: %v", err) - } - if extraDB, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "extra"), cache); err != nil { + if chainDb, err = ethdb.NewLDBDatabase(filepath.Join(datadir, "chaindata"), cache); err != nil { Fatalf("Could not open database: %v", err) } if ctx.GlobalBool(OlympicFlag.Name) { InitOlympic() - _, err := core.WriteTestNetGenesisBlock(stateDB, blockDB, 42) + _, err := core.WriteTestNetGenesisBlock(chainDb, 42) if err != nil { glog.Fatalln(err) } @@ -460,14 +454,14 @@ func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, ex eventMux := new(event.TypeMux) pow := ethash.New() //genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB) - chain, err = core.NewChainManager(blockDB, stateDB, extraDB, pow, eventMux) + chain, err = core.NewChainManager(chainDb, pow, eventMux) if err != nil { Fatalf("Could not start chainmanager: %v", err) } - proc := core.NewBlockProcessor(stateDB, extraDB, pow, chain, eventMux) + proc := core.NewBlockProcessor(chainDb, pow, chain, eventMux) chain.SetProcessor(proc) - return chain, blockDB, stateDB, extraDB + return chain, chainDb } // MakeChain creates an account manager from set command line flags. diff --git a/core/bench_test.go b/core/bench_test.go index 67ba15970f..baae8a7a5e 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -168,8 +168,8 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) { // Time the insertion of the new chain. // State and blocks are stored in the same DB. evmux := new(event.TypeMux) - chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) - chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux)) + chainman, _ := NewChainManager(db, FakePow{}, evmux) + chainman.SetProcessor(NewBlockProcessor(db, FakePow{}, chainman, evmux)) defer chainman.Stop() b.ReportAllocs() b.ResetTimer() diff --git a/core/block_processor.go b/core/block_processor.go index 5a2ad8377f..38bf772fbf 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -41,8 +41,7 @@ const ( ) type BlockProcessor struct { - db common.Database - extraDb common.Database + chainDb common.Database // Mutex for locking the block processor. Blocks can only be handled one at a time mutex sync.Mutex // Canonical block chain @@ -57,10 +56,9 @@ type BlockProcessor struct { eventMux *event.TypeMux } -func NewBlockProcessor(db, extra common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor { +func NewBlockProcessor(db common.Database, pow pow.PoW, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor { sm := &BlockProcessor{ - db: db, - extraDb: extra, + chainDb: db, mem: make(map[string]*big.Int), Pow: pow, bc: chainManager, @@ -201,7 +199,7 @@ func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, receipts func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, receipts types.Receipts, err error) { // Create a new state based on the parent's root (e.g., create copy) - state := state.New(parent.Root(), sm.db) + state := state.New(parent.Root(), sm.chainDb) header := block.Header() uncles := block.Uncles() txs := block.Transactions() @@ -342,7 +340,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty // GetBlockReceipts returns the receipts beloniging to the block hash func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) types.Receipts { if block := sm.ChainManager().GetBlock(bhash); block != nil { - return GetBlockReceipts(sm.extraDb, block.Hash()) + return GetBlockReceipts(sm.chainDb, block.Hash()) } return nil @@ -352,7 +350,7 @@ func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) types.Receipts { // where it tries to get it from the (updated) method which gets them from the receipts or // the depricated way by re-processing the block. func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err error) { - receipts := GetBlockReceipts(sm.extraDb, block.Hash()) + receipts := GetBlockReceipts(sm.chainDb, block.Hash()) if len(receipts) > 0 { // coalesce logs for _, receipt := range receipts { @@ -364,7 +362,7 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro // TODO: remove backward compatibility var ( parent = sm.bc.GetBlock(block.ParentHash()) - state = state.New(parent.Root(), sm.db) + state = state.New(parent.Root(), sm.chainDb) ) sm.TransitionState(state, parent, block, true) diff --git a/core/block_processor_test.go b/core/block_processor_test.go index f48ce9607f..4525f417bf 100644 --- a/core/block_processor_test.go +++ b/core/block_processor_test.go @@ -33,19 +33,19 @@ func proc() (*BlockProcessor, *ChainManager) { db, _ := ethdb.NewMemDatabase() var mux event.TypeMux - WriteTestNetGenesisBlock(db, db, 0) - chainMan, err := NewChainManager(db, db, db, thePow(), &mux) + WriteTestNetGenesisBlock(db, 0) + chainMan, err := NewChainManager(db, thePow(), &mux) if err != nil { fmt.Println(err) } - return NewBlockProcessor(db, db, ezp.New(), chainMan, &mux), chainMan + return NewBlockProcessor(db, ezp.New(), chainMan, &mux), chainMan } func TestNumber(t *testing.T) { pow := ezp.New() _, chain := proc() - statedb := state.New(chain.Genesis().Root(), chain.stateDb) + statedb := state.New(chain.Genesis().Root(), chain.chainDb) header := makeHeader(chain.Genesis(), statedb) header.Number = big.NewInt(3) err := ValidateHeader(pow, header, chain.Genesis(), false) diff --git a/core/chain_makers.go b/core/chain_makers.go index 85a6175dc9..0bb1df95a8 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -184,9 +184,9 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header { func newCanonical(n int, db common.Database) (*BlockProcessor, error) { evmux := &event.TypeMux{} - WriteTestNetGenesisBlock(db, db, 0) - chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) - bman := NewBlockProcessor(db, db, FakePow{}, chainman, evmux) + WriteTestNetGenesisBlock(db, 0) + chainman, _ := NewChainManager(db, FakePow{}, evmux) + bman := NewBlockProcessor(db, FakePow{}, chainman, evmux) bman.bc.SetProcessor(bman) parent := bman.bc.CurrentBlock() if n == 0 { diff --git a/core/chain_makers_test.go b/core/chain_makers_test.go index 98a585f9be..1c868624df 100644 --- a/core/chain_makers_test.go +++ b/core/chain_makers_test.go @@ -77,8 +77,8 @@ func ExampleGenerateChain() { // Import the chain. This runs all block validation rules. evmux := &event.TypeMux{} - chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) - chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux)) + chainman, _ := NewChainManager(db, FakePow{}, evmux) + chainman.SetProcessor(NewBlockProcessor(db, FakePow{}, chainman, evmux)) if i, err := chainman.InsertChain(chain); err != nil { fmt.Printf("insert error (block %d): %v\n", i, err) return diff --git a/core/chain_manager.go b/core/chain_manager.go index fc1d1304fd..87353b9446 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -56,9 +56,7 @@ const ( type ChainManager struct { //eth EthManager - blockDb common.Database - stateDb common.Database - extraDb common.Database + chainDb common.Database processor types.BlockProcessor eventMux *event.TypeMux genesisBlock *types.Block @@ -85,12 +83,10 @@ type ChainManager struct { pow pow.PoW } -func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) { +func NewChainManager(chainDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) { cache, _ := lru.New(blockCacheLimit) bc := &ChainManager{ - blockDb: blockDb, - stateDb: stateDb, - extraDb: extraDb, + chainDb: chainDb, eventMux: mux, quit: make(chan struct{}), cache: cache, @@ -103,7 +99,7 @@ func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux if err != nil { return nil, err } - bc.genesisBlock, err = WriteGenesisBlock(stateDb, blockDb, reader) + bc.genesisBlock, err = WriteGenesisBlock(chainDb, reader) if err != nil { return nil, err } @@ -195,15 +191,15 @@ func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { } func (self *ChainManager) State() *state.StateDB { - return state.New(self.CurrentBlock().Root(), self.stateDb) + return state.New(self.CurrentBlock().Root(), self.chainDb) } func (bc *ChainManager) recover() bool { - data, _ := bc.blockDb.Get([]byte("checkpoint")) + data, _ := bc.chainDb.Get([]byte("checkpoint")) if len(data) != 0 { block := bc.GetBlock(common.BytesToHash(data)) if block != nil { - err := bc.blockDb.Put([]byte("LastBlock"), block.Hash().Bytes()) + err := bc.chainDb.Put([]byte("LastBlock"), block.Hash().Bytes()) if err != nil { glog.Fatalln("db write err:", err) } @@ -217,7 +213,7 @@ func (bc *ChainManager) recover() bool { } func (bc *ChainManager) setLastState() error { - data, _ := bc.blockDb.Get([]byte("LastBlock")) + data, _ := bc.chainDb.Get([]byte("LastBlock")) if len(data) != 0 { block := bc.GetBlock(common.BytesToHash(data)) if block != nil { @@ -264,7 +260,7 @@ func (bc *ChainManager) Reset() { bc.cache, _ = lru.New(blockCacheLimit) // Prepare the genesis block - err := WriteBlock(bc.blockDb, bc.genesisBlock) + err := WriteBlock(bc.chainDb, bc.genesisBlock) if err != nil { glog.Fatalln("db err:", err) } @@ -277,7 +273,7 @@ func (bc *ChainManager) Reset() { } func (bc *ChainManager) removeBlock(block *types.Block) { - bc.blockDb.Delete(append(blockHashPre, block.Hash().Bytes()...)) + bc.chainDb.Delete(append(blockHashPre, block.Hash().Bytes()...)) } func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) { @@ -292,7 +288,7 @@ func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) { gb.Td = gb.Difficulty() bc.genesisBlock = gb - err := WriteBlock(bc.blockDb, bc.genesisBlock) + err := WriteBlock(bc.chainDb, bc.genesisBlock) if err != nil { glog.Fatalln("db err:", err) } @@ -339,14 +335,14 @@ func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error // insert injects a block into the current chain block chain. Note, this function // assumes that the `mu` mutex is held! func (bc *ChainManager) insert(block *types.Block) { - err := WriteHead(bc.blockDb, block) + err := WriteHead(bc.chainDb, block) if err != nil { glog.Fatal("db write fail:", err) } bc.checkpoint++ if bc.checkpoint > checkpointLimit { - err = bc.blockDb.Put([]byte("checkpoint"), block.Hash().Bytes()) + err = bc.chainDb.Put([]byte("checkpoint"), block.Hash().Bytes()) if err != nil { glog.Fatal("db write fail:", err) } @@ -369,7 +365,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool { return true } - data, _ := bc.blockDb.Get(append(blockHashPre, hash[:]...)) + data, _ := bc.chainDb.Get(append(blockHashPre, hash[:]...)) return len(data) != 0 } @@ -399,7 +395,7 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block { return block.(*types.Block) } - block := GetBlockByHash(self.blockDb, hash) + block := GetBlockByHash(self.chainDb, hash) if block == nil { return nil } @@ -433,7 +429,7 @@ func (self *ChainManager) GetBlocksFromHash(hash common.Hash, n int) (blocks []* // non blocking version func (self *ChainManager) getBlockByNumber(num uint64) *types.Block { - return GetBlockByNumber(self.blockDb, num) + return GetBlockByNumber(self.chainDb, num) } func (self *ChainManager) GetUnclesInChain(block *types.Block, length int) (uncles []*types.Header) { @@ -521,7 +517,7 @@ func (self *ChainManager) WriteBlock(block *types.Block, queued bool) (status wr status = SideStatTy } - err = WriteBlock(self.blockDb, block) + err = WriteBlock(self.chainDb, block) if err != nil { glog.Fatalln("db err:", err) } @@ -638,9 +634,9 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { queueEvent.canonicalCount++ // This puts transactions in a extra db for rpc - PutTransactions(self.extraDb, block, block.Transactions()) + PutTransactions(self.chainDb, block, block.Transactions()) // store the receipts - PutReceipts(self.extraDb, receipts) + PutReceipts(self.chainDb, receipts) case SideStatTy: if glog.V(logger.Detail) { glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart)) @@ -651,7 +647,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { queue[i] = ChainSplitEvent{block, logs} queueEvent.splitCount++ } - PutBlockReceipts(self.extraDb, block, receipts) + PutBlockReceipts(self.chainDb, block, receipts) stats.processed++ } @@ -733,8 +729,8 @@ func (self *ChainManager) merge(oldBlock, newBlock *types.Block) error { // insert the block in the canonical way, re-writing history self.insert(block) // write canonical receipts and transactions - PutTransactions(self.extraDb, block, block.Transactions()) - PutReceipts(self.extraDb, GetBlockReceipts(self.extraDb, block.Hash())) + PutTransactions(self.chainDb, block, block.Transactions()) + PutReceipts(self.chainDb, GetBlockReceipts(self.chainDb, block.Hash())) } self.mu.Unlock() diff --git a/core/chain_manager_test.go b/core/chain_manager_test.go index f0c097df6c..002dcbe446 100644 --- a/core/chain_manager_test.go +++ b/core/chain_manager_test.go @@ -48,14 +48,14 @@ func thePow() pow.PoW { func theChainManager(db common.Database, t *testing.T) *ChainManager { var eventMux event.TypeMux - WriteTestNetGenesisBlock(db, db, 0) - chainMan, err := NewChainManager(db, db, db, thePow(), &eventMux) + WriteTestNetGenesisBlock(db, 0) + chainMan, err := NewChainManager(db, thePow(), &eventMux) if err != nil { t.Error("failed creating chainmanager:", err) t.FailNow() return nil } - blockMan := NewBlockProcessor(db, db, nil, chainMan, &eventMux) + blockMan := NewBlockProcessor(db, nil, chainMan, &eventMux) chainMan.SetProcessor(blockMan) return chainMan @@ -125,7 +125,7 @@ func testChain(chainB types.Blocks, bman *BlockProcessor) (*big.Int, error) { bman.bc.mu.Lock() { - WriteBlock(bman.bc.blockDb, block) + WriteBlock(bman.bc.chainDb, block) } bman.bc.mu.Unlock() } @@ -387,7 +387,7 @@ func makeChainWithDiff(genesis *types.Block, d []int, seed byte) []*types.Block func chm(genesis *types.Block, db common.Database) *ChainManager { var eventMux event.TypeMux - bc := &ChainManager{extraDb: db, blockDb: db, stateDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}} + bc := &ChainManager{chainDb: db, genesisBlock: genesis, eventMux: &eventMux, pow: FakePow{}} bc.cache, _ = lru.New(100) bc.futureBlocks, _ = lru.New(100) bc.processor = bproc{} @@ -399,7 +399,7 @@ func chm(genesis *types.Block, db common.Database) *ChainManager { func TestReorgLongest(t *testing.T) { db, _ := ethdb.NewMemDatabase() - genesis, err := WriteTestNetGenesisBlock(db, db, 0) + genesis, err := WriteTestNetGenesisBlock(db, 0) if err != nil { t.Error(err) t.FailNow() @@ -422,7 +422,7 @@ func TestReorgLongest(t *testing.T) { func TestReorgShortest(t *testing.T) { db, _ := ethdb.NewMemDatabase() - genesis, err := WriteTestNetGenesisBlock(db, db, 0) + genesis, err := WriteTestNetGenesisBlock(db, 0) if err != nil { t.Error(err) t.FailNow() @@ -446,13 +446,13 @@ func TestReorgShortest(t *testing.T) { func TestInsertNonceError(t *testing.T) { for i := 1; i < 25 && !t.Failed(); i++ { db, _ := ethdb.NewMemDatabase() - genesis, err := WriteTestNetGenesisBlock(db, db, 0) + genesis, err := WriteTestNetGenesisBlock(db, 0) if err != nil { t.Error(err) t.FailNow() } bc := chm(genesis, db) - bc.processor = NewBlockProcessor(db, db, bc.pow, bc, bc.eventMux) + bc.processor = NewBlockProcessor(db, bc.pow, bc, bc.eventMux) blocks := makeChain(bc.currentBlock, i, db, 0) fail := rand.Int() % len(blocks) diff --git a/core/genesis.go b/core/genesis.go index 4c0323c176..97afb3a4ae 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -33,7 +33,7 @@ import ( ) // WriteGenesisBlock writes the genesis block to the database as block number 0 -func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*types.Block, error) { +func WriteGenesisBlock(chainDb common.Database, reader io.Reader) (*types.Block, error) { contents, err := ioutil.ReadAll(reader) if err != nil { return nil, err @@ -59,7 +59,7 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ return nil, err } - statedb := state.New(common.Hash{}, stateDb) + statedb := state.New(common.Hash{}, chainDb) for addr, account := range genesis.Alloc { address := common.HexToAddress(addr) statedb.AddBalance(address, common.String2Big(account.Balance)) @@ -84,9 +84,9 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ }, nil, nil, nil) block.Td = difficulty - if block := GetBlockByHash(blockDb, block.Hash()); block != nil { + if block := GetBlockByHash(chainDb, block.Hash()); block != nil { glog.V(logger.Info).Infoln("Genesis block already in chain. Writing canonical number") - err := WriteCanonNumber(blockDb, block) + err := WriteCanonNumber(chainDb, block) if err != nil { return nil, err } @@ -95,11 +95,11 @@ func WriteGenesisBlock(stateDb, blockDb common.Database, reader io.Reader) (*typ statedb.Sync() - err = WriteBlock(blockDb, block) + err = WriteBlock(chainDb, block) if err != nil { return nil, err } - err = WriteHead(blockDb, block) + err = WriteHead(chainDb, block) if err != nil { return nil, err } @@ -133,11 +133,11 @@ func WriteGenesisBlockForTesting(db common.Database, addr common.Address, balanc "0x%x":{"balance":"0x%x"} } }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), addr, balance.Bytes()) - block, _ := WriteGenesisBlock(db, db, strings.NewReader(testGenesis)) + block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis)) return block } -func WriteTestNetGenesisBlock(stateDb, blockDb common.Database, nonce uint64) (*types.Block, error) { +func WriteTestNetGenesisBlock(chainDb common.Database, nonce uint64) (*types.Block, error) { testGenesis := fmt.Sprintf(`{ "nonce":"0x%x", "gasLimit":"0x%x", @@ -157,5 +157,5 @@ func WriteTestNetGenesisBlock(stateDb, blockDb common.Database, nonce uint64) (* "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {"balance": "1606938044258990275541962092341162602522202993782792835301376"} } }`, types.EncodeNonce(nonce), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes()) - return WriteGenesisBlock(stateDb, blockDb, strings.NewReader(testGenesis)) + return WriteGenesisBlock(chainDb, strings.NewReader(testGenesis)) } diff --git a/core/manager.go b/core/manager.go index a07c326592..8b0401b031 100644 --- a/core/manager.go +++ b/core/manager.go @@ -28,8 +28,7 @@ type Backend interface { BlockProcessor() *BlockProcessor ChainManager() *ChainManager TxPool() *TxPool - BlockDb() common.Database - StateDb() common.Database - ExtraDb() common.Database + ChainDb() common.Database + DappDb() common.Database EventMux() *event.TypeMux } diff --git a/eth/backend.go b/eth/backend.go index ed46a4ab36..c9b71803fb 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -45,6 +45,7 @@ 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/trie" "github.com/ethereum/go-ethereum/whisper" ) @@ -206,9 +207,8 @@ type Ethereum struct { shutdownChan chan bool // DB interfaces - blockDb common.Database // Block chain database - stateDb common.Database // State changes database - extraDb common.Database // Extra database (txs, etc) + chainDb common.Database // Block chain databe + dappDb common.Database // Dapp database // Closed when databases are flushed and closed databasesClosed chan bool @@ -266,27 +266,27 @@ func New(config *Config) (*Ethereum, error) { if newdb == nil { newdb = func(path string) (common.Database, error) { return ethdb.NewLDBDatabase(path, config.DatabaseCache) } } - blockDb, err := newdb(filepath.Join(config.DataDir, "blockchain")) + + // attempt to merge database together, upgrading from an old version + if err := mergeDatabases(config.DataDir, newdb); err != nil { + return nil, err + } + + chainDb, err := newdb(filepath.Join(config.DataDir, "chaindata")) if err != nil { return nil, fmt.Errorf("blockchain db err: %v", err) } - if db, ok := blockDb.(*ethdb.LDBDatabase); ok { - db.Meter("eth/db/block/") + if db, ok := chainDb.(*ethdb.LDBDatabase); ok { + db.Meter("eth/db/chaindata/") } - stateDb, err := newdb(filepath.Join(config.DataDir, "state")) + dappDb, err := newdb(filepath.Join(config.DataDir, "dapp")) if err != nil { - return nil, fmt.Errorf("state db err: %v", err) + return nil, fmt.Errorf("dapp db err: %v", err) } - if db, ok := stateDb.(*ethdb.LDBDatabase); ok { - db.Meter("eth/db/state/") - } - extraDb, err := newdb(filepath.Join(config.DataDir, "extra")) - if err != nil { - return nil, fmt.Errorf("extra db err: %v", err) - } - if db, ok := extraDb.(*ethdb.LDBDatabase); ok { - db.Meter("eth/db/extra/") + if db, ok := dappDb.(*ethdb.LDBDatabase); ok { + db.Meter("eth/db/dapp/") } + nodeDb := filepath.Join(config.DataDir, "nodes") glog.V(logger.Info).Infof("Protocol Versions: %v, Network Id: %v", ProtocolVersions, config.NetworkId) @@ -296,7 +296,7 @@ func New(config *Config) (*Ethereum, error) { return nil, err } - block, err := core.WriteGenesisBlock(stateDb, blockDb, fr) + block, err := core.WriteGenesisBlock(chainDb, fr) if err != nil { return nil, err } @@ -304,7 +304,7 @@ func New(config *Config) (*Ethereum, error) { } if config.Olympic { - _, err := core.WriteTestNetGenesisBlock(stateDb, blockDb, 42) + _, err := core.WriteTestNetGenesisBlock(chainDb, 42) if err != nil { return nil, err } @@ -313,26 +313,25 @@ func New(config *Config) (*Ethereum, error) { // This is for testing only. if config.GenesisBlock != nil { - core.WriteBlock(blockDb, config.GenesisBlock) - core.WriteHead(blockDb, config.GenesisBlock) + core.WriteBlock(chainDb, config.GenesisBlock) + core.WriteHead(chainDb, config.GenesisBlock) } if !config.SkipBcVersionCheck { - b, _ := blockDb.Get([]byte("BlockchainVersion")) + b, _ := chainDb.Get([]byte("BlockchainVersion")) bcVersion := int(common.NewValue(b).Uint()) if bcVersion != config.BlockChainVersion && bcVersion != 0 { return nil, fmt.Errorf("Blockchain DB version mismatch (%d / %d). Run geth upgradedb.\n", bcVersion, config.BlockChainVersion) } - saveBlockchainVersion(blockDb, config.BlockChainVersion) + saveBlockchainVersion(chainDb, config.BlockChainVersion) } glog.V(logger.Info).Infof("Blockchain DB Version: %d", config.BlockChainVersion) eth := &Ethereum{ shutdownChan: make(chan bool), databasesClosed: make(chan bool), - blockDb: blockDb, - stateDb: stateDb, - extraDb: extraDb, + chainDb: chainDb, + dappDb: dappDb, eventMux: &event.TypeMux{}, accountManager: config.AccountManager, DataDir: config.DataDir, @@ -362,7 +361,7 @@ func New(config *Config) (*Ethereum, error) { eth.pow = ethash.New() } //genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb) - eth.chainManager, err = core.NewChainManager(blockDb, stateDb, extraDb, eth.pow, eth.EventMux()) + eth.chainManager, err = core.NewChainManager(chainDb, eth.pow, eth.EventMux()) if err != nil { if err == core.ErrNoGenesis { return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`) @@ -372,7 +371,7 @@ func New(config *Config) (*Ethereum, error) { } eth.txPool = core.NewTxPool(eth.EventMux(), eth.chainManager.State, eth.chainManager.GasLimit) - eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.chainManager, eth.EventMux()) + eth.blockProcessor = core.NewBlockProcessor(chainDb, eth.pow, eth.chainManager, eth.EventMux()) eth.chainManager.SetProcessor(eth.blockProcessor) eth.protocolManager = NewProtocolManager(config.NetworkId, eth.eventMux, eth.txPool, eth.pow, eth.chainManager) @@ -520,9 +519,8 @@ func (s *Ethereum) BlockProcessor() *core.BlockProcessor { return s.blockProcess func (s *Ethereum) TxPool() *core.TxPool { return s.txPool } func (s *Ethereum) Whisper() *whisper.Whisper { return s.whisper } func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } -func (s *Ethereum) BlockDb() common.Database { return s.blockDb } -func (s *Ethereum) StateDb() common.Database { return s.stateDb } -func (s *Ethereum) ExtraDb() common.Database { return s.extraDb } +func (s *Ethereum) ChainDb() common.Database { return s.chainDb } +func (s *Ethereum) DappDb() common.Database { return s.dappDb } func (s *Ethereum) IsListening() bool { return true } // Always listening func (s *Ethereum) PeerCount() int { return s.net.PeerCount() } func (s *Ethereum) Peers() []*p2p.Peer { return s.net.Peers() } @@ -569,23 +567,19 @@ done: select { case <-ticker.C: // don't change the order of database flushes - if err := s.extraDb.Flush(); err != nil { - glog.Fatalf("fatal error: flush extraDb: %v (Restart your node. We are aware of this issue)\n", err) + if err := s.dappDb.Flush(); err != nil { + glog.Fatalf("fatal error: flush dappDb: %v (Restart your node. We are aware of this issue)\n", err) } - if err := s.stateDb.Flush(); err != nil { - glog.Fatalf("fatal error: flush stateDb: %v (Restart your node. We are aware of this issue)\n", err) - } - if err := s.blockDb.Flush(); err != nil { - glog.Fatalf("fatal error: flush blockDb: %v (Restart your node. We are aware of this issue)\n", err) + if err := s.chainDb.Flush(); err != nil { + glog.Fatalf("fatal error: flush chainDb: %v (Restart your node. We are aware of this issue)\n", err) } case <-s.shutdownChan: break done } } - s.blockDb.Close() - s.stateDb.Close() - s.extraDb.Close() + s.chainDb.Close() + s.dappDb.Close() close(s.databasesClosed) } @@ -683,14 +677,6 @@ func (self *Ethereum) StartAutoDAG() { }() } -// dagFiles(epoch) returns the two alternative DAG filenames (not a path) -// 1) - 2) full-R- -func dagFiles(epoch uint64) (string, string) { - seedHash, _ := ethash.GetSeedHash(epoch * epochLength) - dag := fmt.Sprintf("full-R%d-%x", ethashRevision, seedHash[:8]) - return dag, "full-R" + dag -} - // stopAutoDAG stops automatic DAG pregeneration by quitting the loop func (self *Ethereum) StopAutoDAG() { if self.autodagquit != nil { @@ -700,30 +686,6 @@ func (self *Ethereum) StopAutoDAG() { glog.V(logger.Info).Infof("Automatic pregeneration of ethash DAG OFF (ethash dir: %s)", ethash.DefaultDir) } -/* - // The databases were previously tied to protocol versions. Currently we - // are moving away from this decision as approaching Frontier. The below - // code was left in for now but should eventually be just dropped. - - func saveProtocolVersion(db common.Database, protov int) { - d, _ := db.Get([]byte("ProtocolVersion")) - protocolVersion := common.NewValue(d).Uint() - - if protocolVersion == 0 { - db.Put([]byte("ProtocolVersion"), common.NewValue(protov).Bytes()) - } - } -*/ - -func saveBlockchainVersion(db common.Database, bcVersion int) { - d, _ := db.Get([]byte("BlockchainVersion")) - blockchainVersion := common.NewValue(d).Uint() - - if blockchainVersion == 0 { - db.Put([]byte("BlockchainVersion"), common.NewValue(bcVersion).Bytes()) - } -} - func (self *Ethereum) Solc() (*compiler.Solidity, error) { var err error if self.solc == nil { @@ -738,3 +700,87 @@ func (self *Ethereum) SetSolc(solcPath string) (*compiler.Solidity, error) { self.solc = nil return self.Solc() } + +// dagFiles(epoch) returns the two alternative DAG filenames (not a path) +// 1) - 2) full-R- +func dagFiles(epoch uint64) (string, string) { + seedHash, _ := ethash.GetSeedHash(epoch * epochLength) + dag := fmt.Sprintf("full-R%d-%x", ethashRevision, seedHash[:8]) + return dag, "full-R" + dag +} + +func saveBlockchainVersion(db common.Database, bcVersion int) { + d, _ := db.Get([]byte("BlockchainVersion")) + blockchainVersion := common.NewValue(d).Uint() + + if blockchainVersion == 0 { + db.Put([]byte("BlockchainVersion"), common.NewValue(bcVersion).Bytes()) + } +} + +// mergeDatabases when required merge old database layout to one single database +func mergeDatabases(datadir string, newdb func(path string) (common.Database, error)) error { + // Check if already upgraded + data := filepath.Join(datadir, "chaindata") + if _, err := os.Stat(data); !os.IsNotExist(err) { + return nil + } + // make sure it's not just a clean path + chainPath := filepath.Join(datadir, "blockchain") + if _, err := os.Stat(chainPath); os.IsNotExist(err) { + return nil + } + glog.Infoln("Database upgrade required. Upgrading...") + + database, err := newdb(data) + if err != nil { + return fmt.Errorf("creating data db err: %v", err) + } + defer database.Close() + + glog.Infoln("Merging blockchain database...") + chainDb, err := newdb(chainPath) + if err != nil { + return fmt.Errorf("state db err: %v", err) + } + defer chainDb.Close() + + if db, ok := chainDb.(*ethdb.LDBDatabase); ok { + it := db.NewIterator() + for it.Next() { + database.Put(it.Key(), it.Value()) + } + } + + glog.Infoln("Merging state database...") + state := filepath.Join(datadir, "state") + stateDb, err := newdb(state) + if err != nil { + return fmt.Errorf("state db err: %v", err) + } + defer stateDb.Close() + + if db, ok := chainDb.(*ethdb.LDBDatabase); ok { + it := db.NewIterator() + for it.Next() { + database.Put(append(trie.StatePre, it.Key()...), it.Value()) + } + } + + glog.Infoln("Merging transaction database...") + extra := filepath.Join(datadir, "extra") + extraDb, err := newdb(extra) + if err != nil { + return fmt.Errorf("state db err: %v", err) + } + defer extraDb.Close() + + if db, ok := chainDb.(*ethdb.LDBDatabase); ok { + it := db.NewIterator() + for it.Next() { + database.Put(it.Key(), it.Value()) + } + } + + return nil +} diff --git a/eth/protocol_test.go b/eth/protocol_test.go index a24d98f696..08c9b6a063 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -179,10 +179,10 @@ type testPeer struct { func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *ProtocolManager { db, _ := ethdb.NewMemDatabase() - core.WriteTestNetGenesisBlock(db, db, 0) + core.WriteTestNetGenesisBlock(db, 0) var ( em = new(event.TypeMux) - chain, _ = core.NewChainManager(db, db, db, core.FakePow{}, em) + chain, _ = core.NewChainManager(db, core.FakePow{}, em) txpool = &fakeTxPool{added: txAdded} pm = NewProtocolManager(NetworkId, em, txpool, core.FakePow{}, chain) ) diff --git a/ethdb/database.go b/ethdb/database.go index ace56c6c74..9e80e54091 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -39,9 +39,8 @@ var OpenFileLimit = 64 // cacheRatio specifies how the total alloted cache is distributed between the // various system databases. var cacheRatio = map[string]float64{ - "blockchain": 1.0 / 13.0, - "extra": 2.0 / 13.0, - "state": 10.0 / 13.0, + "dapp": 2.0 / 13.0, + "chaindata": 11.0 / 13.0, } type LDBDatabase struct { diff --git a/miner/worker.go b/miner/worker.go index 535ce51447..df3681470d 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -100,7 +100,7 @@ type worker struct { eth core.Backend chain *core.ChainManager proc *core.BlockProcessor - extraDb common.Database + chainDb common.Database coinbase common.Address gasPrice *big.Int @@ -126,7 +126,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { worker := &worker{ eth: eth, mux: eth.EventMux(), - extraDb: eth.ExtraDb(), + chainDb: eth.ChainDb(), recv: make(chan *Result, resultQueueSize), gasPrice: new(big.Int), chain: eth.ChainManager(), @@ -291,9 +291,9 @@ func (self *worker) wait() { // check if canon block and write transactions if stat == core.CanonStatTy { // This puts transactions in a extra db for rpc - core.PutTransactions(self.extraDb, block, block.Transactions()) + core.PutTransactions(self.chainDb, block, block.Transactions()) // store the receipts - core.PutReceipts(self.extraDb, work.receipts) + core.PutReceipts(self.chainDb, work.receipts) } // broadcast before waiting for validation @@ -344,7 +344,7 @@ func (self *worker) push(work *Work) { // makeCurrent creates a new environment for the current cycle. func (self *worker) makeCurrent(parent *types.Block, header *types.Header) { - state := state.New(parent.Root(), self.eth.StateDb()) + state := state.New(parent.Root(), self.eth.ChainDb()) work := &Work{ state: state, ancestors: set.New(), diff --git a/rpc/api/debug.go b/rpc/api/debug.go index cdacd6c62e..d325b17209 100644 --- a/rpc/api/debug.go +++ b/rpc/api/debug.go @@ -119,7 +119,7 @@ func (self *debugApi) DumpBlock(req *shared.Request) (interface{}, error) { return nil, fmt.Errorf("block #%d not found", args.BlockNumber) } - stateDb := state.New(block.Root(), self.ethereum.StateDb()) + stateDb := state.New(block.Root(), self.ethereum.ChainDb()) if stateDb == nil { return nil, nil } diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 42e4383d1c..8cb7b78825 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -204,7 +204,7 @@ func (test *BlockTest) makeEthConfig() *eth.Config { // InsertPreState populates the given database with the genesis // accounts defined by the test. func (t *BlockTest) InsertPreState(ethereum *eth.Ethereum) (*state.StateDB, error) { - db := ethereum.StateDb() + db := ethereum.ChainDb() statedb := state.New(common.Hash{}, db) for addrString, acct := range t.preAccounts { addr, err := hex.DecodeString(addrString) diff --git a/trie/cache.go b/trie/cache.go index 2705b0e450..99d8033a6d 100644 --- a/trie/cache.go +++ b/trie/cache.go @@ -38,6 +38,8 @@ func NewCache(backend Backend) *Cache { } func (self *Cache) Get(key []byte) []byte { + key = append(StatePre, key...) + data := self.store[string(key)] if data == nil { data, _ = self.backend.Get(key) @@ -47,8 +49,8 @@ func (self *Cache) Get(key []byte) []byte { } func (self *Cache) Put(key []byte, data []byte) { - // write the data to the ldb batch - //self.batch.Put(key, rle.Compress(data)) + key = append(StatePre, key...) + self.batch.Put(key, data) self.store[string(key)] = data } diff --git a/trie/trie.go b/trie/trie.go index abf48a8509..2970bc185e 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -27,6 +27,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" ) +var StatePre = []byte("state-") + func ParanoiaCheck(t1 *Trie, backend Backend) (bool, *Trie) { t2 := New(nil, backend) diff --git a/xeth/state.go b/xeth/state.go index 16bfb523d2..981fe63b7e 100644 --- a/xeth/state.go +++ b/xeth/state.go @@ -45,7 +45,7 @@ func (self *State) SafeGet(addr string) *Object { func (self *State) safeGet(addr string) *state.StateObject { object := self.state.GetStateObject(common.HexToAddress(addr)) if object == nil { - object = state.NewStateObject(common.HexToAddress(addr), self.xeth.backend.StateDb()) + object = state.NewStateObject(common.HexToAddress(addr), self.xeth.backend.ChainDb()) } return object diff --git a/xeth/xeth.go b/xeth/xeth.go index 372068c148..b80b621551 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -213,9 +213,9 @@ func (self *XEth) AtStateNum(num int64) *XEth { st = self.backend.Miner().PendingState().Copy() default: if block := self.getBlockByHeight(num); block != nil { - st = state.New(block.Root(), self.backend.StateDb()) + st = state.New(block.Root(), self.backend.ChainDb()) } else { - st = state.New(self.backend.ChainManager().GetBlockByNumber(0).Root(), self.backend.StateDb()) + st = state.New(self.backend.ChainManager().GetBlockByNumber(0).Root(), self.backend.ChainDb()) } } @@ -259,7 +259,7 @@ func (self *XEth) UpdateState() (wait chan *big.Int) { wait <- n n = nil } - statedb := state.New(ev.Block.Root(), self.backend.StateDb()) + statedb := state.New(ev.Block.Root(), self.backend.ChainDb()) self.state = NewState(self, statedb) } case n, ok = <-wait: @@ -311,7 +311,7 @@ func (self *XEth) EthBlockByHash(strHash string) *types.Block { func (self *XEth) EthTransactionByHash(hash string) (tx *types.Transaction, blhash common.Hash, blnum *big.Int, txi uint64) { // Due to increasing return params and need to determine if this is from transaction pool or // some chain, this probably needs to be refactored for more expressiveness - data, _ := self.backend.ExtraDb().Get(common.FromHex(hash)) + data, _ := self.backend.ChainDb().Get(common.FromHex(hash)) if len(data) != 0 { dtx := new(types.Transaction) if err := rlp.DecodeBytes(data, dtx); err != nil { @@ -330,7 +330,7 @@ func (self *XEth) EthTransactionByHash(hash string) (tx *types.Transaction, blha Index uint64 } - v, dberr := self.backend.ExtraDb().Get(append(common.FromHex(hash), 0x0001)) + v, dberr := self.backend.ChainDb().Get(append(common.FromHex(hash), 0x0001)) // TODO check specifically for ErrNotFound if dberr != nil { return @@ -365,7 +365,7 @@ func (self *XEth) GetBlockReceipts(bhash common.Hash) types.Receipts { } func (self *XEth) GetTxReceipt(txhash common.Hash) *types.Receipt { - return core.GetReceipt(self.backend.ExtraDb(), txhash) + return core.GetReceipt(self.backend.ChainDb(), txhash) } func (self *XEth) GasLimit() *big.Int { @@ -408,13 +408,13 @@ func (self *XEth) SetSolc(solcPath string) (*compiler.Solidity, error) { // store DApp value in extra database func (self *XEth) DbPut(key, val []byte) bool { - self.backend.ExtraDb().Put(append(dappStorePre, key...), val) + self.backend.DappDb().Put(append(dappStorePre, key...), val) return true } // retrieve DApp value from extra database func (self *XEth) DbGet(key []byte) ([]byte, error) { - val, err := self.backend.ExtraDb().Get(append(dappStorePre, key...)) + val, err := self.backend.DappDb().Get(append(dappStorePre, key...)) return val, err } From eec38c5853526f0cf8df0ccbd8e13f0a6c76a04a Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Sun, 9 Aug 2015 02:06:16 +0200 Subject: [PATCH 22/26] cmd/geth, core/vm: setup vm settings and defaulted JIT disabled --- cmd/geth/main.go | 1 + core/vm/settings.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 0e73822f37..895e55b44d 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -336,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) } diff --git a/core/vm/settings.go b/core/vm/settings.go index b94efd9ab1..0cd931b6ae 100644 --- a/core/vm/settings.go +++ b/core/vm/settings.go @@ -17,9 +17,9 @@ package vm var ( - DisableJit bool // Disable the JIT VM - ForceJit bool // Force the JIT, skip byte VM - MaxProgSize int // Max cache size for JIT Programs + 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 From 01ed3fa1a9414328eb6c4fc839e1b2044a786a7a Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 7 Aug 2015 00:10:26 +0200 Subject: [PATCH 23/26] p2p/discover: unlock the table during ping replacement Table.mutex was being held while waiting for a reply packet, which effectively made many parts of the whole stack block on that packet, including the net_peerCount RPC call. --- p2p/discover/table.go | 121 ++++++++++++++++++++++--------------- p2p/discover/table_test.go | 6 +- p2p/discover/udp_test.go | 2 +- 3 files changed, 77 insertions(+), 52 deletions(-) diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 67f7ec46fb..b077f010c7 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -78,9 +78,8 @@ type transport interface { close() } -// bucket contains nodes, ordered by their last activity. -// the entry that was most recently active is the last element -// in entries. +// bucket contains nodes, ordered by their last activity. the entry +// that was most recently active is the first element in entries. type bucket struct { lastLookup time.Time entries []*Node @@ -235,7 +234,7 @@ func (tab *Table) Lookup(targetID NodeID) []*Node { if fails >= maxFindnodeFailures { glog.V(logger.Detail).Infof("Evacuating node %x: %d findnode failures", n.ID[:8], fails) - tab.del(n) + tab.delete(n) } } reply <- tab.bondall(r) @@ -401,15 +400,11 @@ func (tab *Table) bond(pinged bool, id NodeID, addr *net.UDPAddr, tcpPort uint16 node = w.n } } - // Even if bonding temporarily failed, give the node a chance if node != nil { - tab.mutex.Lock() - defer tab.mutex.Unlock() - - b := tab.buckets[logdist(tab.self.sha, node.sha)] - if !b.bump(node) { - tab.pingreplace(node, b) - } + // Add the node to the table even if the bonding ping/pong + // fails. It will be relaced quickly if it continues to be + // unresponsive. + tab.add(node) tab.db.updateFindFails(id, 0) } return node, result @@ -420,7 +415,7 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd <-tab.bondslots defer func() { tab.bondslots <- struct{}{} }() - // Ping the remote side and wait for a pong + // Ping the remote side and wait for a pong. if w.err = tab.ping(id, addr); w.err != nil { close(w.done) return @@ -431,33 +426,14 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd // waitping will simply time out. tab.net.waitping(id) } - // Bonding succeeded, update the node database + // Bonding succeeded, update the node database. w.n = newNode(id, addr.IP, uint16(addr.Port), tcpPort) tab.db.updateNode(w.n) close(w.done) } -func (tab *Table) pingreplace(new *Node, b *bucket) { - if len(b.entries) == bucketSize { - oldest := b.entries[bucketSize-1] - if err := tab.ping(oldest.ID, oldest.addr()); err == nil { - // The node responded, we don't need to replace it. - return - } - } else { - // Add a slot at the end so the last entry doesn't - // fall off when adding the new node. - b.entries = append(b.entries, nil) - } - copy(b.entries[1:], b.entries) - b.entries[0] = new - if tab.nodeAddedHook != nil { - tab.nodeAddedHook(new) - } -} - -// ping a remote endpoint and wait for a reply, also updating the node database -// accordingly. +// ping a remote endpoint and wait for a reply, also updating the node +// database accordingly. func (tab *Table) ping(id NodeID, addr *net.UDPAddr) error { // Update the last ping and send the message tab.db.updateLastPing(id, time.Now()) @@ -467,24 +443,53 @@ func (tab *Table) ping(id NodeID, addr *net.UDPAddr) error { // Pong received, update the database and return tab.db.updateLastPong(id, time.Now()) tab.db.ensureExpirer() - return nil } -// add puts the entries into the table if their corresponding -// bucket is not full. The caller must hold tab.mutex. -func (tab *Table) add(entries []*Node) { +// add attempts to add the given node its corresponding bucket. If the +// bucket has space available, adding the node succeeds immediately. +// Otherwise, the node is added if the least recently active node in +// the bucket does not respond to a ping packet. +// +// The caller must not hold tab.mutex. +func (tab *Table) add(new *Node) { + b := tab.buckets[logdist(tab.self.sha, new.sha)] + tab.mutex.Lock() + if b.bump(new) { + tab.mutex.Unlock() + return + } + var oldest *Node + if len(b.entries) == bucketSize { + oldest = b.entries[bucketSize-1] + // Let go of the mutex so other goroutines can access + // the table while we ping the least recently active node. + tab.mutex.Unlock() + if err := tab.ping(oldest.ID, oldest.addr()); err == nil { + // The node responded, don't replace it. + return + } + tab.mutex.Lock() + } + added := b.replace(new, oldest) + tab.mutex.Unlock() + if added && tab.nodeAddedHook != nil { + tab.nodeAddedHook(new) + } +} + +// stuff adds nodes the table to the end of their corresponding bucket +// if the bucket is not full. The caller must hold tab.mutex. +func (tab *Table) stuff(nodes []*Node) { outer: - for _, n := range entries { + for _, n := range nodes { if n.ID == tab.self.ID { - // don't add self. - continue + continue // don't add self } bucket := tab.buckets[logdist(tab.self.sha, n.sha)] for i := range bucket.entries { if bucket.entries[i].ID == n.ID { - // already in bucket - continue outer + continue outer // already in bucket } } if len(bucket.entries) < bucketSize { @@ -496,12 +501,11 @@ outer: } } -// del removes an entry from the node table (used to evacuate failed/non-bonded -// discovery peers). -func (tab *Table) del(node *Node) { +// delete removes an entry from the node table (used to evacuate +// failed/non-bonded discovery peers). +func (tab *Table) delete(node *Node) { tab.mutex.Lock() defer tab.mutex.Unlock() - bucket := tab.buckets[logdist(tab.self.sha, node.sha)] for i := range bucket.entries { if bucket.entries[i].ID == node.ID { @@ -511,6 +515,27 @@ func (tab *Table) del(node *Node) { } } +func (b *bucket) replace(n *Node, last *Node) bool { + // Don't add if b already contains n. + for i := range b.entries { + if b.entries[i].ID == n.ID { + return false + } + } + // Replace last if it is still the last entry or just add n if b + // isn't full. If is no longer the last entry, it has either been + // replaced with someone else or became active. + if len(b.entries) == bucketSize && (last == nil || b.entries[bucketSize-1].ID != last.ID) { + return false + } + if len(b.entries) < bucketSize { + b.entries = append(b.entries, nil) + } + copy(b.entries[1:], b.entries) + b.entries[0] = n + return true +} + func (b *bucket) bump(n *Node) bool { for i := range b.entries { if b.entries[i].ID == n.ID { diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index d259177bf8..426f4e9ccc 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -178,8 +178,8 @@ func TestTable_closest(t *testing.T) { test := func(test *closeTest) bool { // for any node table, Target and N tab := newTable(nil, test.Self, &net.UDPAddr{}, "") - tab.add(test.All) defer tab.Close() + tab.stuff(test.All) // check that doClosest(Target, N) returns nodes result := tab.closest(test.Target, test.N).entries @@ -240,7 +240,7 @@ func TestTable_ReadRandomNodesGetAll(t *testing.T) { 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)}) + tab.stuff([]*Node{nodeAtDistance(tab.self.sha, ld)}) } gotN := tab.ReadRandomNodes(buf) if gotN != tab.len() { @@ -288,7 +288,7 @@ func TestTable_Lookup(t *testing.T) { } // seed table with initial node (otherwise lookup will terminate immediately) seed := newNode(lookupTestnet.dists[256][0], net.IP{}, 256, 0) - tab.add([]*Node{seed}) + tab.stuff([]*Node{seed}) results := tab.Lookup(lookupTestnet.target) t.Logf("results:") diff --git a/p2p/discover/udp_test.go b/p2p/discover/udp_test.go index 8d6d3e8557..b913424dd0 100644 --- a/p2p/discover/udp_test.go +++ b/p2p/discover/udp_test.go @@ -167,7 +167,7 @@ func TestUDP_findnode(t *testing.T) { for i := 0; i < bucketSize; i++ { nodes.push(nodeAtDistance(test.table.self.sha, i+2), bucketSize) } - test.table.add(nodes.entries) + test.table.stuff(nodes.entries) // ensure there's a bond with the test node, // findnode won't be accepted otherwise. From 590c99a98fd4e58fd34db7e31639b240461ee532 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 8 Aug 2015 02:49:28 +0200 Subject: [PATCH 24/26] p2p/discover: fix UDP reply packet timeout handling If the timeout fired (even just nanoseconds) before the deadline of the next pending reply, the timer was not rescheduled. The timer would've been rescheduled anyway once the next packet was sent, but there were cases where no next packet could ever be sent due to the locking issue fixed in the previous commit. As timing-related bugs go, this issue had been present for a long time and I could never reproduce it. The test added in this commit did reproduce the issue on about one out of 15 runs. --- p2p/discover/udp.go | 78 ++++++++++++++++++++++++---------------- p2p/discover/udp_test.go | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 31 deletions(-) diff --git a/p2p/discover/udp.go b/p2p/discover/udp.go index d7ca9000d4..008e63937d 100644 --- a/p2p/discover/udp.go +++ b/p2p/discover/udp.go @@ -18,6 +18,7 @@ package discover import ( "bytes" + "container/list" "crypto/ecdsa" "errors" "fmt" @@ -43,6 +44,7 @@ var ( errUnsolicitedReply = errors.New("unsolicited reply") errUnknownNode = errors.New("unknown node") errTimeout = errors.New("RPC timeout") + errClockWarp = errors.New("reply deadline too far in the future") errClosed = errors.New("socket closed") ) @@ -296,7 +298,7 @@ func (t *udp) pending(id NodeID, ptype byte, callback func(interface{}) bool) <- } func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool { - matched := make(chan bool) + matched := make(chan bool, 1) select { case t.gotreply <- reply{from, ptype, req, matched}: // loop will handle it @@ -310,68 +312,82 @@ func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool { // the refresh timer and the pending reply queue. func (t *udp) loop() { var ( - pending []*pending - nextDeadline time.Time - timeout = time.NewTimer(0) - refresh = time.NewTicker(refreshInterval) + plist = list.New() + timeout = time.NewTimer(0) + nextTimeout *pending // head of plist when timeout was last reset + refresh = time.NewTicker(refreshInterval) ) <-timeout.C // ignore first timeout defer refresh.Stop() defer timeout.Stop() - rearmTimeout := func() { - now := time.Now() - if len(pending) == 0 || now.Before(nextDeadline) { + resetTimeout := func() { + if plist.Front() == nil || nextTimeout == plist.Front().Value { return } - nextDeadline = pending[0].deadline - timeout.Reset(nextDeadline.Sub(now)) + // Start the timer so it fires when the next pending reply has expired. + now := time.Now() + for el := plist.Front(); el != nil; el = el.Next() { + nextTimeout = el.Value.(*pending) + if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout { + timeout.Reset(dist) + return + } + // Remove pending replies whose deadline is too far in the + // future. These can occur if the system clock jumped + // backwards after the deadline was assigned. + nextTimeout.errc <- errClockWarp + plist.Remove(el) + } + nextTimeout = nil + timeout.Stop() } for { + resetTimeout() + select { case <-refresh.C: go t.refresh() case <-t.closing: - for _, p := range pending { - p.errc <- errClosed + for el := plist.Front(); el != nil; el = el.Next() { + el.Value.(*pending).errc <- errClosed } - pending = nil return case p := <-t.addpending: p.deadline = time.Now().Add(respTimeout) - pending = append(pending, p) - rearmTimeout() + plist.PushBack(p) case r := <-t.gotreply: var matched bool - for i := 0; i < len(pending); i++ { - if p := pending[i]; p.from == r.from && p.ptype == r.ptype { + for el := plist.Front(); el != nil; el = el.Next() { + p := el.Value.(*pending) + if p.from == r.from && p.ptype == r.ptype { matched = true + // Remove the matcher if its callback indicates + // that all replies have been received. This is + // required for packet types that expect multiple + // reply packets. if p.callback(r.data) { - // callback indicates the request is done, remove it. p.errc <- nil - copy(pending[i:], pending[i+1:]) - pending = pending[:len(pending)-1] - i-- + plist.Remove(el) } } } r.matched <- matched case now := <-timeout.C: - // notify and remove callbacks whose deadline is in the past. - i := 0 - for ; i < len(pending) && now.After(pending[i].deadline); i++ { - pending[i].errc <- errTimeout + nextTimeout = nil + // Notify and remove callbacks whose deadline is in the past. + for el := plist.Front(); el != nil; el = el.Next() { + p := el.Value.(*pending) + if now.After(p.deadline) || now.Equal(p.deadline) { + p.errc <- errTimeout + plist.Remove(el) + } } - if i > 0 { - copy(pending, pending[i:]) - pending = pending[:len(pending)-i] - } - rearmTimeout() } } } @@ -385,7 +401,7 @@ const ( var ( headSpace = make([]byte, headSize) - // Neighbors responses are sent across multiple packets to + // Neighbors replies are sent across multiple packets to // stay below the 1280 byte limit. We compute the maximum number // of entries by stuffing a packet until it grows too large. maxNeighbors int diff --git a/p2p/discover/udp_test.go b/p2p/discover/udp_test.go index b913424dd0..a86d3737bc 100644 --- a/p2p/discover/udp_test.go +++ b/p2p/discover/udp_test.go @@ -19,10 +19,12 @@ package discover import ( "bytes" "crypto/ecdsa" + "encoding/binary" "errors" "fmt" "io" logpkg "log" + "math/rand" "net" "os" "path/filepath" @@ -138,6 +140,77 @@ func TestUDP_pingTimeout(t *testing.T) { } } +func TestUDP_responseTimeouts(t *testing.T) { + t.Parallel() + test := newUDPTest(t) + defer test.table.Close() + + rand.Seed(time.Now().UnixNano()) + randomDuration := func(max time.Duration) time.Duration { + return time.Duration(rand.Int63n(int64(max))) + } + + var ( + nReqs = 200 + nTimeouts = 0 // number of requests with ptype > 128 + nilErr = make(chan error, nReqs) // for requests that get a reply + timeoutErr = make(chan error, nReqs) // for requests that time out + ) + for i := 0; i < nReqs; i++ { + // Create a matcher for a random request in udp.loop. Requests + // with ptype <= 128 will not get a reply and should time out. + // For all other requests, a reply is scheduled to arrive + // within the timeout window. + p := &pending{ + ptype: byte(rand.Intn(255)), + callback: func(interface{}) bool { return true }, + } + binary.BigEndian.PutUint64(p.from[:], uint64(i)) + if p.ptype <= 128 { + p.errc = timeoutErr + nTimeouts++ + } else { + p.errc = nilErr + time.AfterFunc(randomDuration(60*time.Millisecond), func() { + if !test.udp.handleReply(p.from, p.ptype, nil) { + t.Logf("not matched: %v", p) + } + }) + } + test.udp.addpending <- p + time.Sleep(randomDuration(30 * time.Millisecond)) + } + + // Check that all timeouts were delivered and that the rest got nil errors. + // The replies must be delivered. + var ( + recvDeadline = time.After(20 * time.Second) + nTimeoutsRecv, nNil = 0, 0 + ) + for i := 0; i < nReqs; i++ { + select { + case err := <-timeoutErr: + if err != errTimeout { + t.Fatalf("got non-timeout error on timeoutErr %d: %v", i, err) + } + nTimeoutsRecv++ + case err := <-nilErr: + if err != nil { + t.Fatalf("got non-nil error on nilErr %d: %v", i, err) + } + nNil++ + case <-recvDeadline: + t.Fatalf("exceeded recv deadline") + } + } + if nTimeoutsRecv != nTimeouts { + t.Errorf("wrong number of timeout errors received: got %d, want %d", nTimeoutsRecv, nTimeouts) + } + if nNil != nReqs-nTimeouts { + t.Errorf("wrong number of successful replies: got %d, want %d", nNil, nReqs-nTimeouts) + } +} + func TestUDP_findnodeTimeout(t *testing.T) { t.Parallel() test := newUDPTest(t) From 67c8ccc309733b0664e287a5b29f1cc62d22a78d Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 11 Aug 2015 11:19:14 +0200 Subject: [PATCH 25/26] cmd/ethtest: added trace flag for debugging --- cmd/ethtest/main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/ethtest/main.go b/cmd/ethtest/main.go index 5429cab31a..67b9653960 100644 --- a/cmd/ethtest/main.go +++ b/cmd/ethtest/main.go @@ -26,6 +26,7 @@ import ( "strings" "github.com/codegangsta/cli" + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/tests" ) @@ -62,6 +63,10 @@ var ( Name: "skip", Usage: "Tests names to skip", } + TraceFlag = cli.BoolFlag{ + Name: "trace", + Usage: "Enable VM tracing", + } ) func runTestWithReader(test string, r io.Reader) error { @@ -173,7 +178,6 @@ func runSuite(test, file string) { glog.Fatalln(err) } } - } } } @@ -184,6 +188,7 @@ func setupApp(c *cli.Context) { continueOnError = c.GlobalBool(ContinueOnErrorFlag.Name) useStdIn := c.GlobalBool(ReadStdInFlag.Name) skipTests = strings.Split(c.GlobalString(SkipTestsFlag.Name), " ") + vm.Debug = c.GlobalBool(TraceFlag.Name) if !useStdIn { runSuite(flagTest, flagFile) @@ -211,6 +216,7 @@ func main() { ContinueOnErrorFlag, ReadStdInFlag, SkipTestsFlag, + TraceFlag, } if err := app.Run(os.Args); err != nil { From bf6ea2919dc424586eff00974b813bbbedcede9b Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 11 Aug 2015 17:17:20 +0200 Subject: [PATCH 26/26] web3: updated --- jsre/ethereum_js.go | 3075 +++++++++++++++++++++++++++++-------------- 1 file changed, 2122 insertions(+), 953 deletions(-) diff --git a/jsre/ethereum_js.go b/jsre/ethereum_js.go index 012e5af709..f33bb7c25a 100644 --- a/jsre/ethereum_js.go +++ b/jsre/ethereum_js.go @@ -18,6 +18,622 @@ package jsre const Web3_JS = ` require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 64 || this.offset !== undefined; + return this.offset !== undefined; }; /** @@ -708,71 +1360,398 @@ SolidityParam.encodeList = function (params) { }, '')); }; -/** - * This method should be used to decode plain (static) solidity param at given index - * - * @method decodeParam - * @param {String} bytes - * @param {Number} index - * @returns {SolidityParam} - */ -SolidityParam.decodeParam = function (bytes, index) { - index = index || 0; - return new SolidityParam(bytes.substr(index * 64, 64)); -}; -/** - * This method should be called to get offset value from bytes at given index - * - * @method getOffset - * @param {String} bytes - * @param {Number} index - * @returns {Number} offset as number - */ -var getOffset = function (bytes, index) { - // we can do this cause offset is rather small - return parseInt('0x' + bytes.substr(index * 64, 64)); -}; - -/** - * This method should be called to decode solidity bytes param at given index - * - * @method decodeBytes - * @param {String} bytes - * @param {Number} index - * @returns {SolidityParam} - */ -SolidityParam.decodeBytes = function (bytes, index) { - index = index || 0; - - var offset = getOffset(bytes, index); - - var l = parseInt('0x' + bytes.substr(offset * 2, 64)); - l = Math.floor((l + 31) / 32); - - // (1 + l) * , cause we also parse length - return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0); -}; - -/** - * This method should be used to decode solidity array at given index - * - * @method decodeArray - * @param {String} bytes - * @param {Number} index - * @returns {SolidityParam} - */ -SolidityParam.decodeArray = function (bytes, index) { - index = index || 0; - var offset = getOffset(bytes, index); - var length = parseInt('0x' + bytes.substr(offset * 2, 64)); - return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64), 0); -}; module.exports = SolidityParam; -},{"../utils/utils":7}],4:[function(require,module,exports){ +},{"../utils/utils":20}],12:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); + +/** + * SolidityTypeReal is a prootype that represents real type + * It matches: + * real + * real[] + * real[4] + * real[][] + * real[3][] + * real[][6][], ... + * real32 + * real64[] + * real8[4] + * real256[][] + * real[3][] + * real64[][6][], ... + */ +var SolidityTypeReal = function () { + this._inputFormatter = f.formatInputReal; + this._outputFormatter = f.formatOutputReal; +}; + +SolidityTypeReal.prototype = new SolidityType({}); +SolidityTypeReal.prototype.constructor = SolidityTypeReal; + +SolidityTypeReal.prototype.isType = function (name) { + return !!name.match(/real([0-9]*)?(\[([0-9]*)\])?/); +}; + +SolidityTypeReal.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; + +module.exports = SolidityTypeReal; + +},{"./formatters":9,"./type":14}],13:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); + +var SolidityTypeString = function () { + this._inputFormatter = f.formatInputString; + this._outputFormatter = f.formatOutputString; +}; + +SolidityTypeString.prototype = new SolidityType({}); +SolidityTypeString.prototype.constructor = SolidityTypeString; + +SolidityTypeString.prototype.isType = function (name) { + return !!name.match(/^string(\[([0-9]*)\])*$/); +}; + +SolidityTypeString.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; + +SolidityTypeString.prototype.isDynamicType = function () { + return true; +}; + +module.exports = SolidityTypeString; + + +},{"./formatters":9,"./type":14}],14:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityParam = require('./param'); + +/** + * SolidityType prototype is used to encode/decode solidity params of certain type + */ +var SolidityType = function (config) { + this._inputFormatter = config.inputFormatter; + this._outputFormatter = config.outputFormatter; +}; + +/** + * Should be used to determine if this SolidityType do match given name + * + * @method isType + * @param {String} name + * @return {Bool} true if type match this SolidityType, otherwise false + */ +SolidityType.prototype.isType = function (name) { + throw "this method should be overrwritten for type " + name; +}; + +/** + * Should be used to determine what is the length of static part in given type + * + * @method staticPartLength + * @param {String} name + * @return {Number} length of static part in bytes + */ +SolidityType.prototype.staticPartLength = function (name) { + throw "this method should be overrwritten for type: " + name; +}; + +/** + * Should be used to determine if type is dynamic array + * eg: + * "type[]" => true + * "type[4]" => false + * + * @method isDynamicArray + * @param {String} name + * @return {Bool} true if the type is dynamic array + */ +SolidityType.prototype.isDynamicArray = function (name) { + var nestedTypes = this.nestedTypes(name); + return !!nestedTypes && !nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); +}; + +/** + * Should be used to determine if type is static array + * eg: + * "type[]" => false + * "type[4]" => true + * + * @method isStaticArray + * @param {String} name + * @return {Bool} true if the type is static array + */ +SolidityType.prototype.isStaticArray = function (name) { + var nestedTypes = this.nestedTypes(name); + return !!nestedTypes && !!nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); +}; + +/** + * Should return length of static array + * eg. + * "int[32]" => 32 + * "int256[14]" => 14 + * "int[2][3]" => 3 + * "int" => 1 + * "int[1]" => 1 + * "int[]" => 1 + * + * @method staticArrayLength + * @param {String} name + * @return {Number} static array length + */ +SolidityType.prototype.staticArrayLength = function (name) { + var nestedTypes = this.nestedTypes(name); + if (nestedTypes) { + return parseInt(nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g) || 1); + } + return 1; +}; + +/** + * Should return nested type + * eg. + * "int[32]" => "int" + * "int256[14]" => "int256" + * "int[2][3]" => "int[2]" + * "int" => "int" + * "int[]" => "int" + * + * @method nestedName + * @param {String} name + * @return {String} nested name + */ +SolidityType.prototype.nestedName = function (name) { + // remove last [] in name + var nestedTypes = this.nestedTypes(name); + if (!nestedTypes) { + return name; + } + + return name.substr(0, name.length - nestedTypes[nestedTypes.length - 1].length); +}; + +/** + * Should return true if type has dynamic size by default + * such types are "string", "bytes" + * + * @method isDynamicType + * @param {String} name + * @return {Bool} true if is dynamic, otherwise false + */ +SolidityType.prototype.isDynamicType = function () { + return false; +}; + +/** + * Should return array of nested types + * eg. + * "int[2][3][]" => ["[2]", "[3]", "[]"] + * "int[] => ["[]"] + * "int" => null + * + * @method nestedTypes + * @param {String} name + * @return {Array} array of nested types + */ +SolidityType.prototype.nestedTypes = function (name) { + // return list of strings eg. "[]", "[3]", "[]", "[2]" + return name.match(/(\[[0-9]*\])/g); +}; + +/** + * Should be used to encode the value + * + * @method encode + * @param {Object} value + * @param {String} name + * @return {String} encoded value + */ +SolidityType.prototype.encode = function (value, name) { + var self = this; + if (this.isDynamicArray(name)) { + + return (function () { + var length = value.length; // in int + var nestedName = self.nestedName(name); + + var result = []; + result.push(f.formatInputInt(length).encode()); + + value.forEach(function (v) { + result.push(self.encode(v, nestedName)); + }); + + return result; + })(); + + } else if (this.isStaticArray(name)) { + + return (function () { + var length = self.staticArrayLength(name); // in int + var nestedName = self.nestedName(name); + + var result = []; + for (var i = 0; i < length; i++) { + result.push(self.encode(value[i], nestedName)); + } + + return result; + })(); + + } + + return this._inputFormatter(value, name).encode(); +}; + +/** + * Should be used to decode value from bytes + * + * @method decode + * @param {String} bytes + * @param {Number} offset in bytes + * @param {String} name type name + * @returns {Object} decoded value + */ +SolidityType.prototype.decode = function (bytes, offset, name) { + var self = this; + + if (this.isDynamicArray(name)) { + + return (function () { + var arrayOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes + var length = parseInt('0x' + bytes.substr(arrayOffset * 2, 64)); // in int + var arrayStart = arrayOffset + 32; // array starts after length; // in bytes + + var nestedName = self.nestedName(name); + var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes + var result = []; + + for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) { + result.push(self.decode(bytes, arrayStart + i, nestedName)); + } + + return result; + })(); + + } else if (this.isStaticArray(name)) { + + return (function () { + var length = self.staticArrayLength(name); // in int + var arrayStart = offset; // in bytes + + var nestedName = self.nestedName(name); + var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes + var result = []; + + for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) { + result.push(self.decode(bytes, arrayStart + i, nestedName)); + } + + return result; + })(); + } else if (this.isDynamicType(name)) { + + return (function () { + var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes + var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes + var roundedLength = Math.floor((length + 31) / 32); // in int + + return self._outputFormatter(new SolidityParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0)); + })(); + } + + var length = this.staticPartLength(name); + return this._outputFormatter(new SolidityParam(bytes.substr(offset * 2, length * 2))); +}; + +module.exports = SolidityType; + +},{"./formatters":9,"./param":11}],15:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); + +/** + * SolidityTypeUInt is a prootype that represents uint type + * It matches: + * uint + * uint[] + * uint[4] + * uint[][] + * uint[3][] + * uint[][6][], ... + * uint32 + * uint64[] + * uint8[4] + * uint256[][] + * uint[3][] + * uint64[][6][], ... + */ +var SolidityTypeUInt = function () { + this._inputFormatter = f.formatInputInt; + this._outputFormatter = f.formatOutputInt; +}; + +SolidityTypeUInt.prototype = new SolidityType({}); +SolidityTypeUInt.prototype.constructor = SolidityTypeUInt; + +SolidityTypeUInt.prototype.isType = function (name) { + return !!name.match(/^uint([0-9]*)?(\[([0-9]*)\])*$/); +}; + +SolidityTypeUInt.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; + +module.exports = SolidityTypeUInt; + +},{"./formatters":9,"./type":14}],16:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); + +/** + * SolidityTypeUReal is a prootype that represents ureal type + * It matches: + * ureal + * ureal[] + * ureal[4] + * ureal[][] + * ureal[3][] + * ureal[][6][], ... + * ureal32 + * ureal64[] + * ureal8[4] + * ureal256[][] + * ureal[3][] + * ureal64[][6][], ... + */ +var SolidityTypeUReal = function () { + this._inputFormatter = f.formatInputReal; + this._outputFormatter = f.formatOutputUReal; +}; + +SolidityTypeUReal.prototype = new SolidityType({}); +SolidityTypeUReal.prototype.constructor = SolidityTypeUReal; + +SolidityTypeUReal.prototype.isType = function (name) { + return !!name.match(/^ureal([0-9]*)?(\[([0-9]*)\])*$/); +}; + +SolidityTypeUReal.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; + +module.exports = SolidityTypeUReal; + +},{"./formatters":9,"./type":14}],17:[function(require,module,exports){ 'use strict'; // go env doesn't have and need XMLHttpRequest @@ -783,7 +1762,7 @@ if (typeof XMLHttpRequest === 'undefined') { } -},{}],5:[function(require,module,exports){ +},{}],18:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -819,6 +1798,7 @@ if (typeof XMLHttpRequest === 'undefined') { * @constructor */ + /// required to define ETH_BIGNUMBER_ROUNDING_MODE var BigNumber = require('bignumber.js'); @@ -863,7 +1843,7 @@ module.exports = { }; -},{"bignumber.js":"bignumber.js"}],6:[function(require,module,exports){ +},{"bignumber.js":"bignumber.js"}],19:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -886,6 +1866,7 @@ module.exports = { * @date 2015 */ + var utils = require('./utils'); var sha3 = require('crypto-js/sha3'); @@ -904,7 +1885,7 @@ module.exports = function (str, isNew) { }; -},{"./utils":7,"crypto-js/sha3":34}],7:[function(require,module,exports){ +},{"./utils":20,"crypto-js/sha3":47}],20:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -940,6 +1921,7 @@ module.exports = function (str, isNew) { * @constructor */ + var BigNumber = require('bignumber.js'); var unitMap = { @@ -1015,7 +1997,7 @@ var toAscii = function(hex) { str += String.fromCharCode(code); } - return decodeURIComponent(escape(str)); + return decodeURIComponent(escape(str)); // jshint ignore:line }; /** @@ -1026,7 +2008,7 @@ var toAscii = function(hex) { * @returns {String} hex representation of input string */ var toHexNative = function(str) { - str = unescape(encodeURIComponent(str)); + str = unescape(encodeURIComponent(str)); // jshint ignore:line var hex = ""; for(var i = 0; i < str.length; i++) { var n = str.charCodeAt(i).toString(16); @@ -1377,18 +2359,6 @@ var isJson = function (str) { } }; -/** - * This method should be called to check if string is valid ethereum IBAN number - * Supports direct and indirect IBANs - * - * @method isIBAN - * @param {String} - * @return {Boolean} - */ -var isIBAN = function (iban) { - return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30})$/.test(iban); -}; - module.exports = { padLeft: padLeft, padRight: padRight, @@ -1413,17 +2383,16 @@ module.exports = { isObject: isObject, isBoolean: isBoolean, isArray: isArray, - isJson: isJson, - isIBAN: isIBAN + isJson: isJson }; -},{"bignumber.js":"bignumber.js"}],8:[function(require,module,exports){ +},{"bignumber.js":"bignumber.js"}],21:[function(require,module,exports){ module.exports={ - "version": "0.9.1" + "version": "0.12.1" } -},{}],9:[function(require,module,exports){ +},{}],22:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -1451,11 +2420,11 @@ module.exports={ */ var version = require('./version.json'); -var net = require('./web3/net'); -var eth = require('./web3/eth'); -var db = require('./web3/db'); -var shh = require('./web3/shh'); -var watches = require('./web3/watches'); +var net = require('./web3/methods/net'); +var eth = require('./web3/methods/eth'); +var db = require('./web3/methods/db'); +var shh = require('./web3/methods/shh'); +var watches = require('./web3/methods/watches'); var Filter = require('./web3/filter'); var utils = require('./utils/utils'); var formatters = require('./web3/formatters'); @@ -1600,7 +2569,7 @@ setupMethods(web3.shh, shh.methods); module.exports = web3; -},{"./utils/config":5,"./utils/sha3":6,"./utils/utils":7,"./version.json":8,"./web3/batch":11,"./web3/db":13,"./web3/eth":15,"./web3/filter":17,"./web3/formatters":18,"./web3/method":24,"./web3/net":26,"./web3/property":27,"./web3/requestmanager":28,"./web3/shh":29,"./web3/watches":31}],10:[function(require,module,exports){ +},{"./utils/config":18,"./utils/sha3":19,"./utils/utils":20,"./version.json":21,"./web3/batch":24,"./web3/filter":28,"./web3/formatters":29,"./web3/method":35,"./web3/methods/db":36,"./web3/methods/eth":37,"./web3/methods/net":38,"./web3/methods/shh":39,"./web3/methods/watches":40,"./web3/property":42,"./web3/requestmanager":43}],23:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -1628,7 +2597,7 @@ var SolidityEvent = require('./event'); var formatters = require('./formatters'); var utils = require('../utils/utils'); var Filter = require('./filter'); -var watches = require('./watches'); +var watches = require('./methods/watches'); var AllSolidityEvents = function (json, address) { this._json = json; @@ -1669,6 +2638,13 @@ AllSolidityEvents.prototype.decode = function (data) { }; AllSolidityEvents.prototype.execute = function (options, callback) { + + if (utils.isFunction(arguments[arguments.length - 1])) { + callback = arguments[arguments.length - 1]; + if(arguments.length === 1) + options = null; + } + var o = this.encode(options); var formatter = this.decode.bind(this); return new Filter(o, watches.eth(), formatter, callback); @@ -1682,7 +2658,7 @@ AllSolidityEvents.prototype.attachToContract = function (contract) { module.exports = AllSolidityEvents; -},{"../utils/sha3":6,"../utils/utils":7,"./event":16,"./filter":17,"./formatters":18,"./watches":31}],11:[function(require,module,exports){ +},{"../utils/sha3":19,"../utils/utils":20,"./event":27,"./filter":28,"./formatters":29,"./methods/watches":40}],24:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -1750,7 +2726,7 @@ Batch.prototype.execute = function () { module.exports = Batch; -},{"./errors":14,"./jsonrpc":23,"./requestmanager":28}],12:[function(require,module,exports){ +},{"./errors":26,"./jsonrpc":34,"./requestmanager":43}],25:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2029,65 +3005,7 @@ var Contract = function (abi, address) { module.exports = contract; -},{"../solidity/coder":1,"../utils/utils":7,"../web3":9,"./allevents":10,"./event":16,"./function":19}],13:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js 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. - - ethereum.js 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 ethereum.js. If not, see . -*/ -/** @file db.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var Method = require('./method'); - -var putString = new Method({ - name: 'putString', - call: 'db_putString', - params: 3 -}); - - -var getString = new Method({ - name: 'getString', - call: 'db_getString', - params: 2 -}); - -var putHex = new Method({ - name: 'putHex', - call: 'db_putHex', - params: 3 -}); - -var getHex = new Method({ - name: 'getHex', - call: 'db_getHex', - params: 2 -}); - -var methods = [ - putString, getString, putHex, getHex -]; - -module.exports = { - methods: methods -}; - -},{"./method":24}],14:[function(require,module,exports){ +},{"../solidity/coder":7,"../utils/utils":20,"../web3":22,"./allevents":23,"./event":27,"./function":30}],26:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2127,300 +3045,7 @@ module.exports = { }; -},{}],15:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js 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. - - ethereum.js 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 ethereum.js. If not, see . -*/ -/** - * @file eth.js - * @author Marek Kotewicz - * @author Fabian Vogelsteller - * @date 2015 - */ - -/** - * Web3 - * - * @module web3 - */ - -/** - * Eth methods and properties - * - * An example method object can look as follows: - * - * { - * name: 'getBlock', - * call: blockCall, - * params: 2, - * outputFormatter: formatters.outputBlockFormatter, - * inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter - * utils.toHex, // formats paramter 1 - * function(param){ return !!param; } // formats paramter 2 - * ] - * }, - * - * @class [web3] eth - * @constructor - */ - -"use strict"; - -var formatters = require('./formatters'); -var utils = require('../utils/utils'); -var Method = require('./method'); -var Property = require('./property'); - -var blockCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber"; -}; - -var transactionFromBlockCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; -}; - -var uncleCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; -}; - -var getBlockTransactionCountCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber'; -}; - -var uncleCountCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber'; -}; - -/// @returns an array of objects describing web3.eth api methods - -var getBalance = new Method({ - name: 'getBalance', - call: 'eth_getBalance', - params: 2, - inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter], - outputFormatter: formatters.outputBigNumberFormatter -}); - -var getStorageAt = new Method({ - name: 'getStorageAt', - call: 'eth_getStorageAt', - params: 3, - inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter] -}); - -var getCode = new Method({ - name: 'getCode', - call: 'eth_getCode', - params: 2, - inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter] -}); - -var getBlock = new Method({ - name: 'getBlock', - call: blockCall, - params: 2, - inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }], - outputFormatter: formatters.outputBlockFormatter -}); - -var getUncle = new Method({ - name: 'getUncle', - call: uncleCall, - params: 2, - inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], - outputFormatter: formatters.outputBlockFormatter, - -}); - -var getCompilers = new Method({ - name: 'getCompilers', - call: 'eth_getCompilers', - params: 0 -}); - -var getBlockTransactionCount = new Method({ - name: 'getBlockTransactionCount', - call: getBlockTransactionCountCall, - params: 1, - inputFormatter: [formatters.inputBlockNumberFormatter], - outputFormatter: utils.toDecimal -}); - -var getBlockUncleCount = new Method({ - name: 'getBlockUncleCount', - call: uncleCountCall, - params: 1, - inputFormatter: [formatters.inputBlockNumberFormatter], - outputFormatter: utils.toDecimal -}); - -var getTransaction = new Method({ - name: 'getTransaction', - call: 'eth_getTransactionByHash', - params: 1, - outputFormatter: formatters.outputTransactionFormatter -}); - -var getTransactionFromBlock = new Method({ - name: 'getTransactionFromBlock', - call: transactionFromBlockCall, - params: 2, - inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], - outputFormatter: formatters.outputTransactionFormatter -}); - -var getTransactionReceipt = new Method({ - name: 'getTransactionReceipt', - call: 'eth_getTransactionReceipt', - params: 1, - outputFormatter: formatters.outputTransactionReceiptFormatter -}); - -var getTransactionCount = new Method({ - name: 'getTransactionCount', - call: 'eth_getTransactionCount', - params: 2, - inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter], - outputFormatter: utils.toDecimal -}); - -var sendRawTransaction = new Method({ - name: 'sendRawTransaction', - call: 'eth_sendRawTransaction', - params: 1, - inputFormatter: [null] -}); - -var sendTransaction = new Method({ - name: 'sendTransaction', - call: 'eth_sendTransaction', - params: 1, - inputFormatter: [formatters.inputTransactionFormatter] -}); - -var call = new Method({ - name: 'call', - call: 'eth_call', - params: 2, - inputFormatter: [formatters.inputTransactionFormatter, formatters.inputDefaultBlockNumberFormatter] -}); - -var estimateGas = new Method({ - name: 'estimateGas', - call: 'eth_estimateGas', - params: 1, - inputFormatter: [formatters.inputTransactionFormatter], - outputFormatter: utils.toDecimal -}); - -var compileSolidity = new Method({ - name: 'compile.solidity', - call: 'eth_compileSolidity', - params: 1 -}); - -var compileLLL = new Method({ - name: 'compile.lll', - call: 'eth_compileLLL', - params: 1 -}); - -var compileSerpent = new Method({ - name: 'compile.serpent', - call: 'eth_compileSerpent', - params: 1 -}); - -var submitWork = new Method({ - name: 'submitWork', - call: 'eth_submitWork', - params: 3 -}); - -var getWork = new Method({ - name: 'getWork', - call: 'eth_getWork', - params: 0 -}); - -var methods = [ - getBalance, - getStorageAt, - getCode, - getBlock, - getUncle, - getCompilers, - getBlockTransactionCount, - getBlockUncleCount, - getTransaction, - getTransactionFromBlock, - getTransactionReceipt, - getTransactionCount, - call, - estimateGas, - sendRawTransaction, - sendTransaction, - compileSolidity, - compileLLL, - compileSerpent, - submitWork, - getWork -]; - -/// @returns an array of objects describing web3.eth api properties - - - -var properties = [ - new Property({ - name: 'coinbase', - getter: 'eth_coinbase' - }), - new Property({ - name: 'mining', - getter: 'eth_mining' - }), - new Property({ - name: 'hashrate', - getter: 'eth_hashrate', - outputFormatter: utils.toDecimal - }), - new Property({ - name: 'gasPrice', - getter: 'eth_gasPrice', - outputFormatter: formatters.outputBigNumberFormatter - }), - new Property({ - name: 'accounts', - getter: 'eth_accounts' - }), - new Property({ - name: 'blockNumber', - getter: 'eth_blockNumber', - outputFormatter: utils.toDecimal - }) -]; - -module.exports = { - methods: methods, - properties: properties -}; - - -},{"../utils/utils":7,"./formatters":18,"./method":24,"./property":27}],16:[function(require,module,exports){ +},{}],27:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2448,7 +3073,7 @@ var coder = require('../solidity/coder'); var formatters = require('./formatters'); var sha3 = require('../utils/sha3'); var Filter = require('./filter'); -var watches = require('./watches'); +var watches = require('./methods/watches'); /** * This prototype should be used to create event filters @@ -2629,7 +3254,7 @@ SolidityEvent.prototype.attachToContract = function (contract) { module.exports = SolidityEvent; -},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"./filter":17,"./formatters":18,"./watches":31}],17:[function(require,module,exports){ +},{"../solidity/coder":7,"../utils/sha3":19,"../utils/utils":20,"./filter":28,"./formatters":29,"./methods/watches":40}],28:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2795,6 +3420,7 @@ var Filter = function (options, methods, formatter, callback) { } }); + return this; }; Filter.prototype.watch = function (callback) { @@ -2840,7 +3466,7 @@ Filter.prototype.get = function (callback) { module.exports = Filter; -},{"../utils/utils":7,"./formatters":18,"./requestmanager":28}],18:[function(require,module,exports){ +},{"../utils/utils":20,"./formatters":29,"./requestmanager":43}],29:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2866,6 +3492,7 @@ module.exports = Filter; var utils = require('../utils/utils'); var config = require('../utils/config'); +var Iban = require('./iban'); /** * Should the format output to a big number @@ -2898,6 +3525,34 @@ var inputBlockNumberFormatter = function (blockNumber) { return utils.toHex(blockNumber); }; +/** + * Formats the input of a transaction and converts all values to HEX + * + * @method inputCallFormatter + * @param {Object} transaction options + * @returns object +*/ +var inputCallFormatter = function (options){ + + options.from = options.from || config.defaultAccount; + + if (options.from) { + options.from = inputAddressFormatter(options.from); + } + + if (options.to) { // it might be contract creation + options.to = inputAddressFormatter(options.to); + } + + ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) { + return options[key] !== undefined; + }).forEach(function(key){ + options[key] = utils.fromDecimal(options[key]); + }); + + return options; +}; + /** * Formats the input of a transaction and converts all values to HEX * @@ -2908,11 +3563,10 @@ var inputBlockNumberFormatter = function (blockNumber) { var inputTransactionFormatter = function (options){ options.from = options.from || config.defaultAccount; + options.from = inputAddressFormatter(options.from); - // make code -> data - if (options.code) { - options.data = options.code; - delete options.code; + if (options.to) { // it might be contract creation + options.to = inputAddressFormatter(options.to); } ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) { @@ -3073,10 +3727,24 @@ var outputPostFormatter = function(post){ return post; }; +var inputAddressFormatter = function (address) { + var iban = new Iban(address); + if (iban.isValid() && iban.isDirect()) { + return '0x' + iban.address(); + } else if (utils.isStrictAddress(address)) { + return address; + } else if (utils.isAddress(address)) { + return '0x' + address; + } + throw 'invalid address'; +}; + module.exports = { inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter, inputBlockNumberFormatter: inputBlockNumberFormatter, + inputCallFormatter: inputCallFormatter, inputTransactionFormatter: inputTransactionFormatter, + inputAddressFormatter: inputAddressFormatter, inputPostFormatter: inputPostFormatter, outputBigNumberFormatter: outputBigNumberFormatter, outputTransactionFormatter: outputTransactionFormatter, @@ -3087,7 +3755,7 @@ module.exports = { }; -},{"../utils/config":5,"../utils/utils":7}],19:[function(require,module,exports){ +},{"../utils/config":18,"../utils/utils":20,"./iban":32}],30:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3324,7 +3992,7 @@ SolidityFunction.prototype.attachToContract = function (contract) { module.exports = SolidityFunction; -},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":18}],20:[function(require,module,exports){ +},{"../solidity/coder":7,"../utils/sha3":19,"../utils/utils":20,"../web3":22,"./formatters":29}],31:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3351,51 +4019,61 @@ module.exports = SolidityFunction; "use strict"; -var XMLHttpRequest = (typeof window !== 'undefined' && window.XMLHttpRequest) ? window.XMLHttpRequest : require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line var errors = require('./errors'); +// workaround to use httpprovider in different envs +var XMLHttpRequest; // jshint ignore: line + +// meteor server environment +if (typeof Meteor !== 'undefined' && Meteor.isServer) { // jshint ignore: line + XMLHttpRequest = Npm.require('xmlhttprequest').XMLHttpRequest; // jshint ignore: line + +// browser +} else if (typeof window !== 'undefined' && window.XMLHttpRequest) { + XMLHttpRequest = window.XMLHttpRequest; // jshint ignore: line + +// node +} else { + XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore: line +} + +/** + * HttpProvider should be used to send rpc calls over http + */ var HttpProvider = function (host) { this.host = host || 'http://localhost:8545'; }; -HttpProvider.prototype.isConnected = function() { +/** + * Should be called to prepare new XMLHttpRequest + * + * @method prepareRequest + * @param {Boolean} true if request should be async + * @return {XMLHttpRequest} object + */ +HttpProvider.prototype.prepareRequest = function (async) { var request = new XMLHttpRequest(); - - request.open('POST', this.host, false); - request.setRequestHeader('Content-type','application/json'); - - try { - request.send(JSON.stringify({ - id: 9999999999, - jsonrpc: '2.0', - method: 'net_listening', - params: [] - })); - return true; - } catch(e) { - return false; - } + request.open('POST', this.host, async); + request.setRequestHeader('Content-Type','application/json'); + return request; }; +/** + * Should be called to make sync request + * + * @method send + * @param {Object} payload + * @return {Object} result + */ HttpProvider.prototype.send = function (payload) { - var request = new XMLHttpRequest(); + var request = this.prepareRequest(false); - request.open('POST', this.host, false); - request.setRequestHeader('Content-type','application/json'); - try { request.send(JSON.stringify(payload)); } catch(error) { throw errors.InvalidConnection(this.host); } - - // check request.status - // TODO: throw an error here! it cannot silently fail!!! - //if (request.status !== 200) { - //return; - //} - var result = request.responseText; try { @@ -3407,8 +4085,16 @@ HttpProvider.prototype.send = function (payload) { return result; }; +/** + * Should be used to make async request + * + * @method sendAsync + * @param {Object} payload + * @param {Function} callback triggered on end with (err, result) + */ HttpProvider.prototype.sendAsync = function (payload, callback) { - var request = new XMLHttpRequest(); + var request = this.prepareRequest(true); + request.onreadystatechange = function() { if (request.readyState === 4) { var result = request.responseText; @@ -3423,9 +4109,6 @@ HttpProvider.prototype.sendAsync = function (payload, callback) { callback(error, result); } }; - - request.open('POST', this.host, true); - request.setRequestHeader('Content-type','application/json'); try { request.send(JSON.stringify(payload)); @@ -3434,10 +4117,30 @@ HttpProvider.prototype.sendAsync = function (payload, callback) { } }; +/** + * Synchronously tries to make Http request + * + * @method isConnected + * @return {Boolean} returns true if request haven't failed. Otherwise false + */ +HttpProvider.prototype.isConnected = function() { + try { + this.send({ + id: 9999999999, + jsonrpc: '2.0', + method: 'net_listening', + params: [] + }); + return true; + } catch(e) { + return false; + } +}; + module.exports = HttpProvider; -},{"./errors":14,"xmlhttprequest":4}],21:[function(require,module,exports){ +},{"./errors":26,"xmlhttprequest":17}],32:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3455,30 +4158,139 @@ module.exports = HttpProvider; along with ethereum.js. If not, see . */ /** - * @file icap.js + * @file iban.js * @author Marek Kotewicz * @date 2015 */ -var utils = require('../utils/utils'); +var BigNumber = require('bignumber.js'); + +var padLeft = function (string, bytes) { + var result = string; + while (result.length < bytes * 2) { + result = '00' + result; + } + return result; +}; /** - * This prototype should be used to extract necessary information from iban address + * Prepare an IBAN for mod 97 computation by moving the first 4 chars to the end and transforming the letters to + * numbers (A = 10, B = 11, ..., Z = 35), as specified in ISO13616. + * + * @method iso13616Prepare + * @param {String} iban the IBAN + * @returns {String} the prepared IBAN + */ +var iso13616Prepare = function (iban) { + var A = 'A'.charCodeAt(0); + var Z = 'Z'.charCodeAt(0); + + iban = iban.toUpperCase(); + iban = iban.substr(4) + iban.substr(0,4); + + return iban.split('').map(function(n){ + var code = n.charCodeAt(0); + if (code >= A && code <= Z){ + // A = 10, B = 11, ... Z = 35 + return code - A + 10; + } else { + return n; + } + }).join(''); +}; + +/** + * Calculates the MOD 97 10 of the passed IBAN as specified in ISO7064. + * + * @method mod9710 + * @param {String} iban + * @returns {Number} + */ +var mod9710 = function (iban) { + var remainder = iban, + block; + + while (remainder.length > 2){ + block = remainder.slice(0, 9); + remainder = parseInt(block, 10) % 97 + remainder.slice(block.length); + } + + return parseInt(remainder, 10) % 97; +}; + +/** + * This prototype should be used to create iban object from iban correct string * * @param {String} iban */ -var ICAP = function (iban) { +var Iban = function (iban) { this._iban = iban; }; /** - * Should be called to check if icap is correct + * This method should be used to create iban object from ethereum address + * + * @method fromAddress + * @param {String} address + * @return {Iban} the IBAN object + */ +Iban.fromAddress = function (address) { + var asBn = new BigNumber(address, 16); + var base36 = asBn.toString(36); + var padded = padLeft(base36, 15); + return Iban.fromBban(padded.toUpperCase()); +}; + +/** + * Convert the passed BBAN to an IBAN for this country specification. + * Please note that "generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account". + * This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits + * + * @method fromBban + * @param {String} bban the BBAN to convert to IBAN + * @returns {Iban} the IBAN object + */ +Iban.fromBban = function (bban) { + var countryCode = 'XE'; + + var remainder = mod9710(iso13616Prepare(countryCode + '00' + bban)); + var checkDigit = ('0' + (98 - remainder)).slice(-2); + + return new Iban(countryCode + checkDigit + bban); +}; + +/** + * Should be used to create IBAN object for given institution and identifier + * + * @method createIndirect + * @param {Object} options, required options are "institution" and "identifier" + * @return {Iban} the IBAN object + */ +Iban.createIndirect = function (options) { + return Iban.fromBban('ETH' + options.institution + options.identifier); +}; + +/** + * Thos method should be used to check if given string is valid iban object + * + * @method isValid + * @param {String} iban string + * @return {Boolean} true if it is valid IBAN + */ +Iban.isValid = function (iban) { + var i = new Iban(iban); + return i.isValid(); +}; + +/** + * Should be called to check if iban is correct * * @method isValid * @returns {Boolean} true if it is, otherwise false */ -ICAP.prototype.isValid = function () { - return utils.isIBAN(this._iban); +Iban.prototype.isValid = function () { + return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30})$/.test(this._iban) && + mod9710(iso13616Prepare(this._iban)) === 1; }; /** @@ -3487,8 +4299,8 @@ ICAP.prototype.isValid = function () { * @method isDirect * @returns {Boolean} true if it is, otherwise false */ -ICAP.prototype.isDirect = function () { - return this._iban.length === 34; +Iban.prototype.isDirect = function () { + return this._iban.length === 34 || this._iban.length === 35; }; /** @@ -3497,7 +4309,7 @@ ICAP.prototype.isDirect = function () { * @method isIndirect * @returns {Boolean} true if it is, otherwise false */ -ICAP.prototype.isIndirect = function () { +Iban.prototype.isIndirect = function () { return this._iban.length === 20; }; @@ -3508,7 +4320,7 @@ ICAP.prototype.isIndirect = function () { * @method checksum * @returns {String} checksum */ -ICAP.prototype.checksum = function () { +Iban.prototype.checksum = function () { return this._iban.substr(2, 2); }; @@ -3519,7 +4331,7 @@ ICAP.prototype.checksum = function () { * @method institution * @returns {String} institution identifier */ -ICAP.prototype.institution = function () { +Iban.prototype.institution = function () { return this.isIndirect() ? this._iban.substr(7, 4) : ''; }; @@ -3530,7 +4342,7 @@ ICAP.prototype.institution = function () { * @method client * @returns {String} client identifier */ -ICAP.prototype.client = function () { +Iban.prototype.client = function () { return this.isIndirect() ? this._iban.substr(11) : ''; }; @@ -3540,14 +4352,24 @@ ICAP.prototype.client = function () { * @method address * @returns {String} client direct address */ -ICAP.prototype.address = function () { - return this.isDirect() ? this._iban.substr(4) : ''; +Iban.prototype.address = function () { + if (this.isDirect()) { + var base36 = this._iban.substr(4); + var asBn = new BigNumber(base36, 36); + return padLeft(asBn.toString(16), 20); + } + + return ''; }; -module.exports = ICAP; +Iban.prototype.toString = function () { + return this._iban; +}; + +module.exports = Iban; -},{"../utils/utils":7}],22:[function(require,module,exports){ +},{"bignumber.js":"bignumber.js"}],33:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3575,16 +4397,23 @@ module.exports = ICAP; var utils = require('../utils/utils'); var errors = require('./errors'); -var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}'; - +var errorTimeout = function (method, id) { + var err = { + "jsonrpc": "2.0", + "error": { + "code": -32603, + "message": "IPC Request timed out for method \'" + method + "\'" + }, + "id": id + }; + return JSON.stringify(err); +}; var IpcProvider = function (path, net) { var _this = this; this.responseCallbacks = {}; this.path = path; - net = net || require('net'); - this.connection = net.connect({path: this.path}); this.connection.on('error', function(e){ @@ -3701,7 +4530,7 @@ Timeout all requests when the end/error event is fired IpcProvider.prototype._timeout = function() { for(var key in this.responseCallbacks) { if(this.responseCallbacks.hasOwnProperty(key)){ - this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method)); + this.responseCallbacks[key](errorTimeout(this.responseCallbacks[key].method, key)); delete this.responseCallbacks[key]; } } @@ -3760,7 +4589,7 @@ IpcProvider.prototype.sendAsync = function (payload, callback) { module.exports = IpcProvider; -},{"../utils/utils":7,"./errors":14,"net":32}],23:[function(require,module,exports){ +},{"../utils/utils":20,"./errors":26}],34:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3853,7 +4682,7 @@ Jsonrpc.prototype.toBatchPayload = function (messages) { module.exports = Jsonrpc; -},{}],24:[function(require,module,exports){ +},{}],35:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4027,7 +4856,7 @@ Method.prototype.send = function () { module.exports = Method; -},{"../utils/utils":7,"./errors":14,"./requestmanager":28}],25:[function(require,module,exports){ +},{"../utils/utils":20,"./errors":26,"./requestmanager":43}],36:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4044,38 +4873,341 @@ module.exports = Method; You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** - * @file namereg.js - * @author Marek Kotewicz +/** @file db.js + * @authors: + * Marek Kotewicz * @date 2015 */ -var contract = require('./contract'); +var Method = require('../method'); -var address = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; +var putString = new Method({ + name: 'putString', + call: 'db_putString', + params: 3 +}); -var abi = [ - {"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"type":"function"}, - {"constant":false,"inputs":[],"name":"Registrar","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"}, - {"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"}],"name":"PrimaryChanged","type":"event"} + +var getString = new Method({ + name: 'getString', + call: 'db_getString', + params: 2 +}); + +var putHex = new Method({ + name: 'putHex', + call: 'db_putHex', + params: 3 +}); + +var getHex = new Method({ + name: 'getHex', + call: 'db_getHex', + params: 2 +}); + +var methods = [ + putString, getString, putHex, getHex ]; -module.exports = contract(abi).at(address); +module.exports = { + methods: methods +}; + +},{"../method":35}],37:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js 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. + + ethereum.js 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 ethereum.js. If not, see . +*/ +/** + * @file eth.js + * @author Marek Kotewicz + * @author Fabian Vogelsteller + * @date 2015 + */ + +/** + * Web3 + * + * @module web3 + */ + +/** + * Eth methods and properties + * + * An example method object can look as follows: + * + * { + * name: 'getBlock', + * call: blockCall, + * params: 2, + * outputFormatter: formatters.outputBlockFormatter, + * inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter + * utils.toHex, // formats paramter 1 + * function(param){ return !!param; } // formats paramter 2 + * ] + * }, + * + * @class [web3] eth + * @constructor + */ + +"use strict"; + +var formatters = require('../formatters'); +var utils = require('../../utils/utils'); +var Method = require('../method'); +var Property = require('../property'); + +var blockCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber"; +}; + +var transactionFromBlockCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; +}; + +var uncleCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; +}; + +var getBlockTransactionCountCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber'; +}; + +var uncleCountCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber'; +}; + +/// @returns an array of objects describing web3.eth api methods + +var getBalance = new Method({ + name: 'getBalance', + call: 'eth_getBalance', + params: 2, + inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], + outputFormatter: formatters.outputBigNumberFormatter +}); + +var getStorageAt = new Method({ + name: 'getStorageAt', + call: 'eth_getStorageAt', + params: 3, + inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter] +}); + +var getCode = new Method({ + name: 'getCode', + call: 'eth_getCode', + params: 2, + inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter] +}); + +var getBlock = new Method({ + name: 'getBlock', + call: blockCall, + params: 2, + inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }], + outputFormatter: formatters.outputBlockFormatter +}); + +var getUncle = new Method({ + name: 'getUncle', + call: uncleCall, + params: 2, + inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], + outputFormatter: formatters.outputBlockFormatter, + +}); + +var getCompilers = new Method({ + name: 'getCompilers', + call: 'eth_getCompilers', + params: 0 +}); + +var getBlockTransactionCount = new Method({ + name: 'getBlockTransactionCount', + call: getBlockTransactionCountCall, + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + outputFormatter: utils.toDecimal +}); + +var getBlockUncleCount = new Method({ + name: 'getBlockUncleCount', + call: uncleCountCall, + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + outputFormatter: utils.toDecimal +}); + +var getTransaction = new Method({ + name: 'getTransaction', + call: 'eth_getTransactionByHash', + params: 1, + outputFormatter: formatters.outputTransactionFormatter +}); + +var getTransactionFromBlock = new Method({ + name: 'getTransactionFromBlock', + call: transactionFromBlockCall, + params: 2, + inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], + outputFormatter: formatters.outputTransactionFormatter +}); + +var getTransactionReceipt = new Method({ + name: 'getTransactionReceipt', + call: 'eth_getTransactionReceipt', + params: 1, + outputFormatter: formatters.outputTransactionReceiptFormatter +}); + +var getTransactionCount = new Method({ + name: 'getTransactionCount', + call: 'eth_getTransactionCount', + params: 2, + inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter], + outputFormatter: utils.toDecimal +}); + +var sendRawTransaction = new Method({ + name: 'sendRawTransaction', + call: 'eth_sendRawTransaction', + params: 1, + inputFormatter: [null] +}); + +var sendTransaction = new Method({ + name: 'sendTransaction', + call: 'eth_sendTransaction', + params: 1, + inputFormatter: [formatters.inputTransactionFormatter] +}); + +var call = new Method({ + name: 'call', + call: 'eth_call', + params: 2, + inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter] +}); + +var estimateGas = new Method({ + name: 'estimateGas', + call: 'eth_estimateGas', + params: 1, + inputFormatter: [formatters.inputCallFormatter], + outputFormatter: utils.toDecimal +}); + +var compileSolidity = new Method({ + name: 'compile.solidity', + call: 'eth_compileSolidity', + params: 1 +}); + +var compileLLL = new Method({ + name: 'compile.lll', + call: 'eth_compileLLL', + params: 1 +}); + +var compileSerpent = new Method({ + name: 'compile.serpent', + call: 'eth_compileSerpent', + params: 1 +}); + +var submitWork = new Method({ + name: 'submitWork', + call: 'eth_submitWork', + params: 3 +}); + +var getWork = new Method({ + name: 'getWork', + call: 'eth_getWork', + params: 0 +}); + +var methods = [ + getBalance, + getStorageAt, + getCode, + getBlock, + getUncle, + getCompilers, + getBlockTransactionCount, + getBlockUncleCount, + getTransaction, + getTransactionFromBlock, + getTransactionReceipt, + getTransactionCount, + call, + estimateGas, + sendRawTransaction, + sendTransaction, + compileSolidity, + compileLLL, + compileSerpent, + submitWork, + getWork +]; + +/// @returns an array of objects describing web3.eth api properties -},{"./contract":12}],26:[function(require,module,exports){ + +var properties = [ + new Property({ + name: 'coinbase', + getter: 'eth_coinbase' + }), + new Property({ + name: 'mining', + getter: 'eth_mining' + }), + new Property({ + name: 'hashrate', + getter: 'eth_hashrate', + outputFormatter: utils.toDecimal + }), + new Property({ + name: 'gasPrice', + getter: 'eth_gasPrice', + outputFormatter: formatters.outputBigNumberFormatter + }), + new Property({ + name: 'accounts', + getter: 'eth_accounts' + }), + new Property({ + name: 'blockNumber', + getter: 'eth_blockNumber', + outputFormatter: utils.toDecimal + }) +]; + +module.exports = { + methods: methods, + properties: properties +}; + + +},{"../../utils/utils":20,"../formatters":29,"../method":35,"../property":42}],38:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4098,8 +5230,8 @@ module.exports = contract(abi).at(address); * @date 2015 */ -var utils = require('../utils/utils'); -var Property = require('./property'); +var utils = require('../../utils/utils'); +var Property = require('../property'); /// @returns an array of objects describing web3.eth api methods var methods = [ @@ -4125,7 +5257,229 @@ module.exports = { }; -},{"../utils/utils":7,"./property":27}],27:[function(require,module,exports){ +},{"../../utils/utils":20,"../property":42}],39:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js 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. + + ethereum.js 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 ethereum.js. If not, see . +*/ +/** @file shh.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +var Method = require('../method'); +var formatters = require('../formatters'); + +var post = new Method({ + name: 'post', + call: 'shh_post', + params: 1, + inputFormatter: [formatters.inputPostFormatter] +}); + +var newIdentity = new Method({ + name: 'newIdentity', + call: 'shh_newIdentity', + params: 0 +}); + +var hasIdentity = new Method({ + name: 'hasIdentity', + call: 'shh_hasIdentity', + params: 1 +}); + +var newGroup = new Method({ + name: 'newGroup', + call: 'shh_newGroup', + params: 0 +}); + +var addToGroup = new Method({ + name: 'addToGroup', + call: 'shh_addToGroup', + params: 0 +}); + +var methods = [ + post, + newIdentity, + hasIdentity, + newGroup, + addToGroup +]; + +module.exports = { + methods: methods +}; + + +},{"../formatters":29,"../method":35}],40:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js 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. + + ethereum.js 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 ethereum.js. If not, see . +*/ +/** @file watches.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +var Method = require('../method'); + +/// @returns an array of objects describing web3.eth.filter api methods +var eth = function () { + var newFilterCall = function (args) { + var type = args[0]; + + switch(type) { + case 'latest': + args.shift(); + this.params = 0; + return 'eth_newBlockFilter'; + case 'pending': + args.shift(); + this.params = 0; + return 'eth_newPendingTransactionFilter'; + default: + return 'eth_newFilter'; + } + }; + + var newFilter = new Method({ + name: 'newFilter', + call: newFilterCall, + params: 1 + }); + + var uninstallFilter = new Method({ + name: 'uninstallFilter', + call: 'eth_uninstallFilter', + params: 1 + }); + + var getLogs = new Method({ + name: 'getLogs', + call: 'eth_getFilterLogs', + params: 1 + }); + + var poll = new Method({ + name: 'poll', + call: 'eth_getFilterChanges', + params: 1 + }); + + return [ + newFilter, + uninstallFilter, + getLogs, + poll + ]; +}; + +/// @returns an array of objects describing web3.shh.watch api methods +var shh = function () { + var newFilter = new Method({ + name: 'newFilter', + call: 'shh_newFilter', + params: 1 + }); + + var uninstallFilter = new Method({ + name: 'uninstallFilter', + call: 'shh_uninstallFilter', + params: 1 + }); + + var getLogs = new Method({ + name: 'getLogs', + call: 'shh_getMessages', + params: 1 + }); + + var poll = new Method({ + name: 'poll', + call: 'shh_getFilterChanges', + params: 1 + }); + + return [ + newFilter, + uninstallFilter, + getLogs, + poll + ]; +}; + +module.exports = { + eth: eth, + shh: shh +}; + + +},{"../method":35}],41:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js 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. + + ethereum.js 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 ethereum.js. If not, see . +*/ +/** + * @file namereg.js + * @author Marek Kotewicz + * @date 2015 + */ + +var contract = require('./contract'); +var globalRegistrarAbi = require('../contracts/GlobalRegistrar.json'); +var icapRegistrarAbi= require('../contracts/ICAPRegistrar.json'); + +var globalNameregAddress = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; +var ibanNameregAddress = '0xa1a111bc074c9cfa781f0c38e63bd51c91b8af00'; + +module.exports = { + namereg: contract(globalRegistrarAbi).at(globalNameregAddress), + ibanNamereg: contract(icapRegistrarAbi).at(ibanNameregAddress) +}; + + +},{"../contracts/GlobalRegistrar.json":1,"../contracts/ICAPRegistrar.json":2,"./contract":25}],42:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4277,7 +5631,7 @@ Property.prototype.request = function () { module.exports = Property; -},{"../utils/utils":7,"./requestmanager":28}],28:[function(require,module,exports){ +},{"../utils/utils":20,"./requestmanager":43}],43:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4427,8 +5781,6 @@ RequestManager.prototype.setProvider = function (p) { } }; -/*jshint maxparams:4 */ - /** * Should be used to start polling * @@ -4441,9 +5793,8 @@ RequestManager.prototype.setProvider = function (p) { * @todo cleanup number of params */ RequestManager.prototype.startPolling = function (data, pollId, callback, uninstall) { - this.polls['poll_'+ pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall}; + this.polls[pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall}; }; -/*jshint maxparams:3 */ /** * Should be used to stop polling for filter with given id @@ -4452,7 +5803,7 @@ RequestManager.prototype.startPolling = function (data, pollId, callback, uninst * @param {Number} pollId */ RequestManager.prototype.stopPolling = function (pollId) { - delete this.polls['poll_'+ pollId]; + delete this.polls[pollId]; }; /** @@ -4542,77 +5893,7 @@ RequestManager.prototype.poll = function () { module.exports = RequestManager; -},{"../utils/config":5,"../utils/utils":7,"./errors":14,"./jsonrpc":23}],29:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js 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. - - ethereum.js 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 ethereum.js. If not, see . -*/ -/** @file shh.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var Method = require('./method'); -var formatters = require('./formatters'); - -var post = new Method({ - name: 'post', - call: 'shh_post', - params: 1, - inputFormatter: [formatters.inputPostFormatter] -}); - -var newIdentity = new Method({ - name: 'newIdentity', - call: 'shh_newIdentity', - params: 0 -}); - -var hasIdentity = new Method({ - name: 'hasIdentity', - call: 'shh_hasIdentity', - params: 1 -}); - -var newGroup = new Method({ - name: 'newGroup', - call: 'shh_newGroup', - params: 0 -}); - -var addToGroup = new Method({ - name: 'addToGroup', - call: 'shh_addToGroup', - params: 0 -}); - -var methods = [ - post, - newIdentity, - hasIdentity, - newGroup, - addToGroup -]; - -module.exports = { - methods: methods -}; - - -},{"./formatters":18,"./method":24}],30:[function(require,module,exports){ +},{"../utils/config":18,"../utils/utils":20,"./errors":26,"./jsonrpc":34}],44:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4636,36 +5917,37 @@ module.exports = { */ var web3 = require('../web3'); -var ICAP = require('./icap'); -var namereg = require('./namereg'); +var Iban = require('./iban'); +var namereg = require('./namereg').ibanNamereg; var contract = require('./contract'); +var exchangeAbi = require('../contracts/SmartExchange.json'); /** - * Should be used to make ICAP transfer + * Should be used to make Iban transfer * * @method transfer - * @param {String} iban number - * @param {String} from (address) + * @param {String} from + * @param {String} to iban * @param {Value} value to be tranfered * @param {Function} callback, callback */ -var transfer = function (from, iban, value, callback) { - var icap = new ICAP(iban); - if (!icap.isValid()) { +var transfer = function (from, to, value, callback) { + var iban = new Iban(to); + if (!iban.isValid()) { throw new Error('invalid iban address'); } - if (icap.isDirect()) { - return transferToAddress(from, icap.address(), value, callback); + if (iban.isDirect()) { + return transferToAddress(from, iban.address(), value, callback); } if (!callback) { - var address = namereg.addr(icap.institution()); - return deposit(from, address, value, icap.client()); + var address = namereg.addr(iban.institution()); + return deposit(from, address, value, iban.client()); } - namereg.addr(icap.insitution(), function (err, address) { - return deposit(from, address, value, icap.client(), callback); + namereg.addr(iban.institution(), function (err, address) { + return deposit(from, address, value, iban.client(), callback); }); }; @@ -4674,14 +5956,14 @@ var transfer = function (from, iban, value, callback) { * Should be used to transfer funds to certain address * * @method transferToAddress - * @param {String} address - * @param {String} from (address) + * @param {String} from + * @param {String} to * @param {Value} value to be tranfered * @param {Function} callback, callback */ -var transferToAddress = function (from, address, value, callback) { +var transferToAddress = function (from, to, value, callback) { return web3.eth.sendTransaction({ - address: address, + address: to, from: from, value: value }, callback); @@ -4691,15 +5973,15 @@ var transferToAddress = function (from, address, value, callback) { * Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!) * * @method deposit - * @param {String} address - * @param {String} from (address) - * @param {Value} value to be tranfered + * @param {String} from + * @param {String} to + * @param {Value} value to be transfered * @param {String} client unique identifier * @param {Function} callback, callback */ -var deposit = function (from, address, value, client, callback) { - var abi = [{"constant":false,"inputs":[{"name":"name","type":"bytes32"}],"name":"deposit","outputs":[],"type":"function"}]; - return contract(abi).at(address).deposit(client, { +var deposit = function (from, to, value, client, callback) { + var abi = exchangeAbi; + return contract(abi).at(to).deposit(client, { from: from, value: value }, callback); @@ -4708,125 +5990,9 @@ var deposit = function (from, address, value, client, callback) { module.exports = transfer; -},{"../web3":9,"./contract":12,"./icap":21,"./namereg":25}],31:[function(require,module,exports){ -/* - This file is part of ethereum.js. +},{"../contracts/SmartExchange.json":3,"../web3":22,"./contract":25,"./iban":32,"./namereg":41}],45:[function(require,module,exports){ - ethereum.js 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. - - ethereum.js 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 ethereum.js. If not, see . -*/ -/** @file watches.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var Method = require('./method'); - -/// @returns an array of objects describing web3.eth.filter api methods -var eth = function () { - var newFilterCall = function (args) { - var type = args[0]; - - switch(type) { - case 'latest': - args.shift(); - this.params = 0; - return 'eth_newBlockFilter'; - case 'pending': - args.shift(); - this.params = 0; - return 'eth_newPendingTransactionFilter'; - default: - return 'eth_newFilter'; - } - }; - - var newFilter = new Method({ - name: 'newFilter', - call: newFilterCall, - params: 1 - }); - - var uninstallFilter = new Method({ - name: 'uninstallFilter', - call: 'eth_uninstallFilter', - params: 1 - }); - - var getLogs = new Method({ - name: 'getLogs', - call: 'eth_getFilterLogs', - params: 1 - }); - - var poll = new Method({ - name: 'poll', - call: 'eth_getFilterChanges', - params: 1 - }); - - return [ - newFilter, - uninstallFilter, - getLogs, - poll - ]; -}; - -/// @returns an array of objects describing web3.shh.watch api methods -var shh = function () { - var newFilter = new Method({ - name: 'newFilter', - call: 'shh_newFilter', - params: 1 - }); - - var uninstallFilter = new Method({ - name: 'uninstallFilter', - call: 'shh_uninstallFilter', - params: 1 - }); - - var getLogs = new Method({ - name: 'getLogs', - call: 'shh_getMessages', - params: 1 - }); - - var poll = new Method({ - name: 'poll', - call: 'shh_getFilterChanges', - params: 1 - }); - - return [ - newFilter, - uninstallFilter, - getLogs, - poll - ]; -}; - -module.exports = { - eth: eth, - shh: shh -}; - - -},{"./method":24}],32:[function(require,module,exports){ - -},{}],33:[function(require,module,exports){ +},{}],46:[function(require,module,exports){ ;(function (root, factory) { if (typeof exports === "object") { // CommonJS @@ -5569,7 +6735,7 @@ module.exports = { return CryptoJS; })); -},{}],34:[function(require,module,exports){ +},{}],47:[function(require,module,exports){ ;(function (root, factory, undef) { if (typeof exports === "object") { // CommonJS @@ -5893,7 +7059,7 @@ module.exports = { return CryptoJS.SHA3; })); -},{"./core":33,"./x64-core":35}],35:[function(require,module,exports){ +},{"./core":46,"./x64-core":48}],48:[function(require,module,exports){ ;(function (root, factory) { if (typeof exports === "object") { // CommonJS @@ -6198,7 +7364,7 @@ module.exports = { return CryptoJS; })); -},{"./core":33}],"bignumber.js":[function(require,module,exports){ +},{"./core":46}],"bignumber.js":[function(require,module,exports){ 'use strict'; module.exports = BigNumber; // jshint ignore:line @@ -6206,13 +7372,16 @@ module.exports = BigNumber; // jshint ignore:line },{}],"web3":[function(require,module,exports){ var web3 = require('./lib/web3'); +var namereg = require('./lib/web3/namereg'); web3.providers.HttpProvider = require('./lib/web3/httpprovider'); web3.providers.IpcProvider = require('./lib/web3/ipcprovider'); web3.eth.contract = require('./lib/web3/contract'); -web3.eth.namereg = require('./lib/web3/namereg'); +web3.eth.namereg = namereg.namereg; +web3.eth.ibanNamereg = namereg.ibanNamereg; web3.eth.sendIBANTransaction = require('./lib/web3/transfer'); +web3.eth.iban = require('./lib/web3/iban'); // dont override global variable if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') { @@ -6222,6 +7391,6 @@ if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') { module.exports = web3; -},{"./lib/web3":9,"./lib/web3/contract":12,"./lib/web3/httpprovider":20,"./lib/web3/ipcprovider":22,"./lib/web3/namereg":25,"./lib/web3/transfer":30}]},{},["web3"]) +},{"./lib/web3":22,"./lib/web3/contract":25,"./lib/web3/httpprovider":31,"./lib/web3/iban":32,"./lib/web3/ipcprovider":33,"./lib/web3/namereg":41,"./lib/web3/transfer":44}]},{},["web3"]) //# sourceMappingURL=web3-light.js.map `