diff options
-rw-r--r-- | .gitattributes | 2 | ||||
-rw-r--r-- | cmd/utils/customflags.go | 9 | ||||
-rw-r--r-- | cmd/utils/customflags_test.go | 5 | ||||
-rw-r--r-- | cmd/utils/flags.go | 2 | ||||
-rw-r--r-- | common/compiler/solidity_test.go | 5 | ||||
-rw-r--r-- | common/docserver/docserver.go | 1 | ||||
-rw-r--r-- | common/docserver/docserver_test.go | 19 | ||||
-rw-r--r-- | common/path.go | 11 | ||||
-rw-r--r-- | common/path_test.go | 52 | ||||
-rw-r--r-- | common/size_test.go | 2 | ||||
-rw-r--r-- | jsre/jsre_test.go | 30 | ||||
-rw-r--r-- | p2p/discover/table.go | 4 | ||||
-rw-r--r-- | p2p/discover/table_test.go | 8 | ||||
-rw-r--r-- | p2p/nat/natupnp_test.go | 5 | ||||
-rw-r--r-- | trie/encoding.go | 57 | ||||
-rw-r--r-- | trie/encoding_test.go | 68 | ||||
-rw-r--r-- | trie/iterator.go | 2 | ||||
-rw-r--r-- | trie/shortnode.go | 4 | ||||
-rw-r--r-- | trie/trie.go | 10 | ||||
-rw-r--r-- | xeth/xeth.go | 11 | ||||
-rw-r--r-- | xeth/xeth_test.go | 26 |
21 files changed, 185 insertions, 148 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..dfe077042 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go index e7efed4e3..4450065c1 100644 --- a/cmd/utils/customflags.go +++ b/cmd/utils/customflags.go @@ -21,7 +21,7 @@ import ( "fmt" "os" "os/user" - "path/filepath" + "path" "strings" "github.com/codegangsta/cli" @@ -138,11 +138,8 @@ func (self *DirectoryFlag) Set(value string) { func expandPath(p string) string { if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") { if user, err := user.Current(); err == nil { - if err == nil { - p = strings.Replace(p, "~", user.HomeDir, 1) - } + p = user.HomeDir + p[1:] } } - - return filepath.Clean(os.ExpandEnv(p)) + return path.Clean(os.ExpandEnv(p)) } diff --git a/cmd/utils/customflags_test.go b/cmd/utils/customflags_test.go index 0fb0af63b..de39ca36a 100644 --- a/cmd/utils/customflags_test.go +++ b/cmd/utils/customflags_test.go @@ -23,18 +23,15 @@ import ( ) func TestPathExpansion(t *testing.T) { - user, _ := user.Current() - tests := map[string]string{ "/home/someuser/tmp": "/home/someuser/tmp", "~/tmp": user.HomeDir + "/tmp", + "~thisOtherUser/b/": "~thisOtherUser/b", "$DDDXXX/a/b": "/tmp/a/b", "/a/b/": "/a/b", } - os.Setenv("DDDXXX", "/tmp") - for test, expected := range tests { got := expandPath(test) if got != expected { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 815d48124..cf969805d 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/compiler/solidity_test.go b/common/compiler/solidity_test.go index 8255e8e2d..3303bc15a 100644 --- a/common/compiler/solidity_test.go +++ b/common/compiler/solidity_test.go @@ -20,6 +20,7 @@ import ( "encoding/json" "io/ioutil" "os" + "path" "testing" "github.com/ethereum/go-ethereum/common" @@ -94,7 +95,7 @@ func TestSaveInfo(t *testing.T) { if err != nil { t.Errorf("%v", err) } - filename := "/tmp/solctest.info.json" + filename := path.Join(os.TempDir(), "solctest.info.json") os.Remove(filename) cinfohash, err := SaveInfo(&cinfo, filename) if err != nil { @@ -110,4 +111,4 @@ func TestSaveInfo(t *testing.T) { if cinfohash != infohash { t.Errorf("content hash for info is incorrect. expected %v, got %v", infohash.Hex(), cinfohash.Hex()) } -} +}
\ No newline at end of file diff --git a/common/docserver/docserver.go b/common/docserver/docserver.go index fa120fb38..dac542ba7 100644 --- a/common/docserver/docserver.go +++ b/common/docserver/docserver.go @@ -38,7 +38,6 @@ func New(docRoot string) (self *DocServer) { DocRoot: docRoot, schemes: []string{"file"}, } - self.DocRoot = "/tmp/" self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot))) return } diff --git a/common/docserver/docserver_test.go b/common/docserver/docserver_test.go index 92e39d167..632603add 100644 --- a/common/docserver/docserver_test.go +++ b/common/docserver/docserver_test.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "net/http" "os" + "path" "testing" "github.com/ethereum/go-ethereum/common" @@ -27,12 +28,18 @@ import ( ) func TestGetAuthContent(t *testing.T) { - text := "test" - hash := common.Hash{} - copy(hash[:], crypto.Sha3([]byte(text))) - ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm) + dir, err := ioutil.TempDir("", "docserver-test") + if err != nil { + t.Fatal("cannot create temporary directory:", err) + } + defer os.RemoveAll(dir) + ds := New(dir) - ds := New("/tmp/") + text := "test" + hash := crypto.Sha3Hash([]byte(text)) + if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil { + t.Fatal("could not write test file", err) + } content, err := ds.GetAuthContent("file:///test.content", hash) if err != nil { t.Errorf("no error expected, got %v", err) @@ -67,4 +74,4 @@ func TestRegisterScheme(t *testing.T) { if !ds.HasScheme("scheme") { t.Errorf("expected scheme to be registered") } -} +}
\ No newline at end of file diff --git a/common/path.go b/common/path.go index 0d7adb961..8b3c7d14b 100644 --- a/common/path.go +++ b/common/path.go @@ -116,14 +116,3 @@ func DefaultIpcPath() string { } return filepath.Join(DefaultDataDir(), "geth.ipc") } - -func IsWindows() bool { - return runtime.GOOS == "windows" -} - -func WindonizePath(path string) string { - if string(path[0]) == "/" && IsWindows() { - path = path[1:] - } - return path -} diff --git a/common/path_test.go b/common/path_test.go deleted file mode 100644 index 71ffd5fe1..000000000 --- a/common/path_test.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2014 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. - -package common - -import ( - "os" - // "testing" - - checker "gopkg.in/check.v1" -) - -type CommonSuite struct{} - -var _ = checker.Suite(&CommonSuite{}) - -func (s *CommonSuite) TestOS(c *checker.C) { - expwin := (os.PathSeparator == '\\' && os.PathListSeparator == ';') - res := IsWindows() - - if !expwin { - c.Assert(res, checker.Equals, expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator)) - } else { - c.Assert(res, checker.Not(checker.Equals), expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator)) - } -} - -func (s *CommonSuite) TestWindonziePath(c *checker.C) { - iswindowspath := os.PathSeparator == '\\' - path := "/opt/eth/test/file.ext" - res := WindonizePath(path) - ressep := string(res[0]) - - if !iswindowspath { - c.Assert(ressep, checker.Equals, "/") - } else { - c.Assert(ressep, checker.Not(checker.Equals), "/") - } -} diff --git a/common/size_test.go b/common/size_test.go index 8709a0237..ce19cab69 100644 --- a/common/size_test.go +++ b/common/size_test.go @@ -40,7 +40,7 @@ func (s *SizeSuite) TestStorageSizeString(c *checker.C) { c.Assert(StorageSize(data3).String(), checker.Equals, exp3) } -func (s *CommonSuite) TestCommon(c *checker.C) { +func (s *SizeSuite) TestCommon(c *checker.C) { ether := CurrencyToString(BigPow(10, 19)) finney := CurrencyToString(BigPow(10, 16)) szabo := CurrencyToString(BigPow(10, 13)) diff --git a/jsre/jsre_test.go b/jsre/jsre_test.go index ad210932a..93dc7d1f9 100644 --- a/jsre/jsre_test.go +++ b/jsre/jsre_test.go @@ -19,6 +19,7 @@ package jsre import ( "io/ioutil" "os" + "path" "testing" "time" @@ -40,10 +41,23 @@ func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value return v } +func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) { + dir, err := ioutil.TempDir("", "jsre-test") + if err != nil { + t.Fatal("cannot create temporary directory:", err) + } + if testjs != "" { + if err := ioutil.WriteFile(path.Join(dir, "test.js"), []byte(testjs), os.ModePerm); err != nil { + t.Fatal("cannot create test.js:", err) + } + } + return New(dir), dir +} + func TestExec(t *testing.T) { - jsre := New("/tmp") + jsre, dir := newWithTestJS(t, `msg = "testMsg"`) + defer os.RemoveAll(dir) - ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm) err := jsre.Exec("test.js") if err != nil { t.Errorf("expected no error, got %v", err) @@ -64,9 +78,9 @@ func TestExec(t *testing.T) { } func TestNatto(t *testing.T) { - jsre := New("/tmp") + jsre, dir := newWithTestJS(t, `setTimeout(function(){msg = "testMsg"}, 1);`) + defer os.RemoveAll(dir) - ioutil.WriteFile("/tmp/test.js", []byte(`setTimeout(function(){msg = "testMsg"}, 1);`), os.ModePerm) err := jsre.Exec("test.js") if err != nil { t.Errorf("expected no error, got %v", err) @@ -88,7 +102,7 @@ func TestNatto(t *testing.T) { } func TestBind(t *testing.T) { - jsre := New("/tmp") + jsre := New("") jsre.Bind("no", &testNativeObjectBinding{}) @@ -105,9 +119,9 @@ func TestBind(t *testing.T) { } func TestLoadScript(t *testing.T) { - jsre := New("/tmp") + jsre, dir := newWithTestJS(t, `msg = "testMsg"`) + defer os.RemoveAll(dir) - ioutil.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`), os.ModePerm) _, err := jsre.Run(`loadScript("test.js")`) if err != nil { t.Errorf("expected no error, got %v", err) @@ -125,4 +139,4 @@ func TestLoadScript(t *testing.T) { t.Errorf("expected '%v', got '%v'", exp, got) } jsre.Stop(false) -} +}
\ No newline at end of file diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 48c473475..67f7ec46f 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -164,7 +164,9 @@ func randUint(max uint32) uint32 { // Close terminates the network listener and flushes the node database. func (tab *Table) Close() { - tab.net.close() + if tab.net != nil { + tab.net.close() + } tab.db.close() } diff --git a/p2p/discover/table_test.go b/p2p/discover/table_test.go index 310fe2b7b..d259177bf 100644 --- a/p2p/discover/table_test.go +++ b/p2p/discover/table_test.go @@ -35,6 +35,7 @@ func TestTable_pingReplace(t *testing.T) { doit := func(newNodeIsResponding, lastInBucketIsResponding bool) { transport := newPingRecorder() tab := newTable(transport, NodeID{}, &net.UDPAddr{}, "") + defer tab.Close() pingSender := newNode(MustHexID("a502af0f59b2aab7746995408c79e9ca312d2793cc997e44fc55eda62f0150bbb8c59a6f9269ba3a081518b62699ee807c7c19c20125ddfccca872608af9e370"), net.IP{}, 99, 99) // fill up the sender's bucket. @@ -158,9 +159,7 @@ func newPingRecorder() *pingRecorder { func (t *pingRecorder) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node, error) { panic("findnode called on pingRecorder") } -func (t *pingRecorder) close() { - panic("close called on pingRecorder") -} +func (t *pingRecorder) close() {} func (t *pingRecorder) waitping(from NodeID) error { return nil // remote always pings } @@ -180,6 +179,7 @@ func TestTable_closest(t *testing.T) { // for any node table, Target and N tab := newTable(nil, test.Self, &net.UDPAddr{}, "") tab.add(test.All) + defer tab.Close() // check that doClosest(Target, N) returns nodes result := tab.closest(test.Target, test.N).entries @@ -237,6 +237,7 @@ func TestTable_ReadRandomNodesGetAll(t *testing.T) { } test := func(buf []*Node) bool { tab := newTable(nil, NodeID{}, &net.UDPAddr{}, "") + defer tab.Close() for i := 0; i < len(buf); i++ { ld := cfg.Rand.Intn(len(tab.buckets)) tab.add([]*Node{nodeAtDistance(tab.self.sha, ld)}) @@ -279,6 +280,7 @@ func (*closeTest) Generate(rand *rand.Rand, size int) reflect.Value { func TestTable_Lookup(t *testing.T) { self := nodeAtDistance(common.Hash{}, 0) tab := newTable(lookupTestnet, self.ID, &net.UDPAddr{}, "") + defer tab.Close() // lookup on empty table returns no nodes if results := tab.Lookup(lookupTestnet.target); len(results) > 0 { diff --git a/p2p/nat/natupnp_test.go b/p2p/nat/natupnp_test.go index c1e322af7..79f6d25ae 100644 --- a/p2p/nat/natupnp_test.go +++ b/p2p/nat/natupnp_test.go @@ -21,6 +21,7 @@ import ( "io" "net" "net/http" + "runtime" "strings" "testing" @@ -28,6 +29,10 @@ import ( ) func TestUPNP_DDWRT(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skipf("disabled to avoid firewall prompt") + } + dev := &fakeIGD{ t: t, ssdpResp: "HTTP/1.1 200 OK\r\n" + diff --git a/trie/encoding.go b/trie/encoding.go index 524807f06..9c862d78f 100644 --- a/trie/encoding.go +++ b/trie/encoding.go @@ -16,13 +16,7 @@ package trie -import ( - "bytes" - "encoding/hex" - "strings" -) - -func CompactEncode(hexSlice []byte) string { +func CompactEncode(hexSlice []byte) []byte { terminator := 0 if hexSlice[len(hexSlice)-1] == 16 { terminator = 1 @@ -40,15 +34,15 @@ func CompactEncode(hexSlice []byte) string { hexSlice = append([]byte{flags, 0}, hexSlice...) } - var buff bytes.Buffer - for i := 0; i < len(hexSlice); i += 2 { - buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1])) + l := len(hexSlice) / 2 + var buf = make([]byte, l) + for i := 0; i < l; i++ { + buf[i] = 16*hexSlice[2*i] + hexSlice[2*i+1] } - - return buff.String() + return buf } -func CompactDecode(str string) []byte { +func CompactDecode(str []byte) []byte { base := CompactHexDecode(str) base = base[:len(base)-1] if base[0] >= 2 { @@ -63,30 +57,23 @@ func CompactDecode(str string) []byte { return base } -func CompactHexDecode(str string) []byte { - base := "0123456789abcdef" - var hexSlice []byte - - enc := hex.EncodeToString([]byte(str)) - for _, v := range enc { - hexSlice = append(hexSlice, byte(strings.IndexByte(base, byte(v)))) +func CompactHexDecode(str []byte) []byte { + l := len(str)*2 + 1 + var nibbles = make([]byte, l) + for i, b := range str { + nibbles[i*2] = b / 16 + nibbles[i*2+1] = b % 16 } - hexSlice = append(hexSlice, 16) - - return hexSlice + nibbles[l-1] = 16 + return nibbles } -func DecodeCompact(key []byte) string { - const base = "0123456789abcdef" - var str string - - for _, v := range key { - if v < 16 { - str += string(base[v]) - } +func DecodeCompact(key []byte) []byte { + l := len(key) / 2 + var res = make([]byte, l) + for i := 0; i < l; i++ { + v1, v0 := key[2*i], key[2*i+1] + res[i] = v1*16 + v0 } - - res, _ := hex.DecodeString(str) - - return string(res) + return res } diff --git a/trie/encoding_test.go b/trie/encoding_test.go index e52c6ba8d..e49b57ef0 100644 --- a/trie/encoding_test.go +++ b/trie/encoding_test.go @@ -17,9 +17,14 @@ package trie import ( + "encoding/hex" + "testing" + checker "gopkg.in/check.v1" ) +func Test(t *testing.T) { checker.TestingT(t) } + type TrieEncodingSuite struct{} var _ = checker.Suite(&TrieEncodingSuite{}) @@ -28,48 +33,93 @@ func (s *TrieEncodingSuite) TestCompactEncode(c *checker.C) { // even compact encode test1 := []byte{1, 2, 3, 4, 5} res1 := CompactEncode(test1) - c.Assert(res1, checker.Equals, "\x11\x23\x45") + c.Assert(res1, checker.DeepEquals, []byte("\x11\x23\x45")) // odd compact encode test2 := []byte{0, 1, 2, 3, 4, 5} res2 := CompactEncode(test2) - c.Assert(res2, checker.Equals, "\x00\x01\x23\x45") + c.Assert(res2, checker.DeepEquals, []byte("\x00\x01\x23\x45")) //odd terminated compact encode test3 := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} res3 := CompactEncode(test3) - c.Assert(res3, checker.Equals, "\x20\x0f\x1c\xb8") + c.Assert(res3, checker.DeepEquals, []byte("\x20\x0f\x1c\xb8")) // even terminated compact encode test4 := []byte{15, 1, 12, 11, 8 /*term*/, 16} res4 := CompactEncode(test4) - c.Assert(res4, checker.Equals, "\x3f\x1c\xb8") + c.Assert(res4, checker.DeepEquals, []byte("\x3f\x1c\xb8")) } func (s *TrieEncodingSuite) TestCompactHexDecode(c *checker.C) { exp := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} - res := CompactHexDecode("verb") + res := CompactHexDecode([]byte("verb")) c.Assert(res, checker.DeepEquals, exp) } func (s *TrieEncodingSuite) TestCompactDecode(c *checker.C) { // odd compact decode exp := []byte{1, 2, 3, 4, 5} - res := CompactDecode("\x11\x23\x45") + res := CompactDecode([]byte("\x11\x23\x45")) c.Assert(res, checker.DeepEquals, exp) // even compact decode exp = []byte{0, 1, 2, 3, 4, 5} - res = CompactDecode("\x00\x01\x23\x45") + res = CompactDecode([]byte("\x00\x01\x23\x45")) c.Assert(res, checker.DeepEquals, exp) // even terminated compact decode exp = []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} - res = CompactDecode("\x20\x0f\x1c\xb8") + res = CompactDecode([]byte("\x20\x0f\x1c\xb8")) c.Assert(res, checker.DeepEquals, exp) // even terminated compact decode exp = []byte{15, 1, 12, 11, 8 /*term*/, 16} - res = CompactDecode("\x3f\x1c\xb8") + res = CompactDecode([]byte("\x3f\x1c\xb8")) + c.Assert(res, checker.DeepEquals, exp) +} + +func (s *TrieEncodingSuite) TestDecodeCompact(c *checker.C) { + exp, _ := hex.DecodeString("012345") + res := DecodeCompact([]byte{0, 1, 2, 3, 4, 5}) c.Assert(res, checker.DeepEquals, exp) + + exp, _ = hex.DecodeString("012345") + res = DecodeCompact([]byte{0, 1, 2, 3, 4, 5, 16}) + c.Assert(res, checker.DeepEquals, exp) + + exp, _ = hex.DecodeString("abcdef") + res = DecodeCompact([]byte{10, 11, 12, 13, 14, 15}) + c.Assert(res, checker.DeepEquals, exp) +} + +func BenchmarkCompactEncode(b *testing.B) { + + testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} + for i := 0; i < b.N; i++ { + CompactEncode(testBytes) + } +} + +func BenchmarkCompactDecode(b *testing.B) { + testBytes := []byte{0, 15, 1, 12, 11, 8 /*term*/, 16} + for i := 0; i < b.N; i++ { + CompactDecode(testBytes) + } +} + +func BenchmarkCompactHexDecode(b *testing.B) { + testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} + for i := 0; i < b.N; i++ { + CompactHexDecode(testBytes) + } + +} + +func BenchmarkDecodeCompact(b *testing.B) { + testBytes := []byte{7, 6, 6, 5, 7, 2, 6, 2, 16} + for i := 0; i < b.N; i++ { + DecodeCompact(testBytes) + } + } diff --git a/trie/iterator.go b/trie/iterator.go index 698e64b34..9c4c7fbe5 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -41,7 +41,7 @@ func (self *Iterator) Next() bool { self.Key = make([]byte, 32) } - key := RemTerm(CompactHexDecode(string(self.Key))) + key := RemTerm(CompactHexDecode(self.Key)) k := self.next(self.trie.root, key, isIterStart) self.Key = []byte(DecodeCompact(k)) diff --git a/trie/shortnode.go b/trie/shortnode.go index b5fc6d1f9..569d5f109 100644 --- a/trie/shortnode.go +++ b/trie/shortnode.go @@ -26,7 +26,7 @@ type ShortNode struct { } func NewShortNode(t *Trie, key []byte, value Node) *ShortNode { - return &ShortNode{t, []byte(CompactEncode(key)), value, false} + return &ShortNode{t, CompactEncode(key), value, false} } func (self *ShortNode) Value() Node { self.value = self.trie.trans(self.value) @@ -49,7 +49,7 @@ func (self *ShortNode) Hash() interface{} { } func (self *ShortNode) Key() []byte { - return CompactDecode(string(self.key)) + return CompactDecode(self.key) } func (self *ShortNode) setDirty(dirty bool) { diff --git a/trie/trie.go b/trie/trie.go index e7ee86402..abf48a850 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -69,7 +69,7 @@ func (self *Trie) Iterator() *Iterator { func (self *Trie) Copy() *Trie { cpy := make([]byte, 32) - copy(cpy, self.roothash) + copy(cpy, self.roothash) // NOTE: cpy isn't being used anywhere? trie := New(nil, nil) trie.cache = self.cache.Copy() if self.root != nil { @@ -131,7 +131,7 @@ func (self *Trie) Update(key, value []byte) Node { self.mu.Lock() defer self.mu.Unlock() - k := CompactHexDecode(string(key)) + k := CompactHexDecode(key) if len(value) != 0 { node := NewValueNode(self, value) @@ -149,7 +149,7 @@ func (self *Trie) Get(key []byte) []byte { self.mu.Lock() defer self.mu.Unlock() - k := CompactHexDecode(string(key)) + k := CompactHexDecode(key) n := self.get(self.root, k) if n != nil { @@ -164,7 +164,7 @@ func (self *Trie) Delete(key []byte) Node { self.mu.Lock() defer self.mu.Unlock() - k := CompactHexDecode(string(key)) + k := CompactHexDecode(key) self.root = self.delete(self.root, k) return self.root @@ -336,7 +336,7 @@ func (self *Trie) mknode(value *common.Value) Node { case 2: // A value node may consists of 2 bytes. if value.Get(0).Len() != 0 { - key := CompactDecode(string(value.Get(0).Bytes())) + key := CompactDecode(value.Get(0).Bytes()) if key[len(key)-1] == 16 { return NewShortNode(self, key, NewValueNode(self, value.Get(1).Bytes())) } else { diff --git a/xeth/xeth.go b/xeth/xeth.go index 5d54c1f7e..372068c14 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 000000000..e649d20ef --- /dev/null +++ b/xeth/xeth_test.go @@ -0,0 +1,26 @@ +package xeth + +import "testing" + +func TestIsAddress(t *testing.T) { + for _, invalid := range []string{ + "0x00", + "0xNN", + "0x00000000000000000000000000000000000000NN", + "0xAAar000000000000000000000000000000000000", + } { + if isAddress(invalid) { + t.Error("Expected", invalid, "to be invalid") + } + } + + for _, valid := range []string{ + "0x0000000000000000000000000000000000000000", + "0xAABBbbCCccff9900000000000000000000000000", + "AABBbbCCccff9900000000000000000000000000", + } { + if !isAddress(valid) { + t.Error("Expected", valid, "to be valid") + } + } +} |