aboutsummaryrefslogtreecommitdiffstats
path: root/accounts
diff options
context:
space:
mode:
Diffstat (limited to 'accounts')
-rw-r--r--accounts/abi/method.go4
-rw-r--r--accounts/keystore/account_cache.go12
-rw-r--r--accounts/keystore/file_cache.go18
-rw-r--r--accounts/url.go6
-rw-r--r--accounts/url_test.go96
5 files changed, 115 insertions, 21 deletions
diff --git a/accounts/abi/method.go b/accounts/abi/method.go
index f434ffdbe..583105765 100644
--- a/accounts/abi/method.go
+++ b/accounts/abi/method.go
@@ -47,10 +47,8 @@ type Method struct {
// Please note that "int" is substitute for its canonical representation "int256"
func (method Method) Sig() string {
types := make([]string, len(method.Inputs))
- i := 0
- for _, input := range method.Inputs {
+ for i, input := range method.Inputs {
types[i] = input.Type.String()
- i++
}
return fmt.Sprintf("%v(%v)", method.Name, strings.Join(types, ","))
}
diff --git a/accounts/keystore/account_cache.go b/accounts/keystore/account_cache.go
index 71f698ece..da3a46eb8 100644
--- a/accounts/keystore/account_cache.go
+++ b/accounts/keystore/account_cache.go
@@ -27,10 +27,10 @@ import (
"sync"
"time"
+ mapset "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
- "gopkg.in/fatih/set.v0"
)
// Minimum amount of time between cache reloads. This limit applies if the platform does
@@ -79,7 +79,7 @@ func newAccountCache(keydir string) (*accountCache, chan struct{}) {
keydir: keydir,
byAddr: make(map[common.Address][]accounts.Account),
notify: make(chan struct{}, 1),
- fileC: fileCache{all: set.NewNonTS()},
+ fileC: fileCache{all: mapset.NewThreadUnsafeSet()},
}
ac.watcher = newWatcher(ac)
return ac, ac.notify
@@ -237,7 +237,7 @@ func (ac *accountCache) scanAccounts() error {
log.Debug("Failed to reload keystore contents", "err", err)
return err
}
- if creates.Size() == 0 && deletes.Size() == 0 && updates.Size() == 0 {
+ if creates.Cardinality() == 0 && deletes.Cardinality() == 0 && updates.Cardinality() == 0 {
return nil
}
// Create a helper method to scan the contents of the key files
@@ -272,15 +272,15 @@ func (ac *accountCache) scanAccounts() error {
// Process all the file diffs
start := time.Now()
- for _, p := range creates.List() {
+ for _, p := range creates.ToSlice() {
if a := readAccount(p.(string)); a != nil {
ac.add(*a)
}
}
- for _, p := range deletes.List() {
+ for _, p := range deletes.ToSlice() {
ac.deleteByFile(p.(string))
}
- for _, p := range updates.List() {
+ for _, p := range updates.ToSlice() {
path := p.(string)
ac.deleteByFile(path)
if a := readAccount(path); a != nil {
diff --git a/accounts/keystore/file_cache.go b/accounts/keystore/file_cache.go
index c91b7b7b6..26da34dcf 100644
--- a/accounts/keystore/file_cache.go
+++ b/accounts/keystore/file_cache.go
@@ -24,20 +24,20 @@ import (
"sync"
"time"
+ mapset "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/log"
- set "gopkg.in/fatih/set.v0"
)
// fileCache is a cache of files seen during scan of keystore.
type fileCache struct {
- all *set.SetNonTS // Set of all files from the keystore folder
- lastMod time.Time // Last time instance when a file was modified
+ all mapset.Set // Set of all files from the keystore folder
+ lastMod time.Time // Last time instance when a file was modified
mu sync.RWMutex
}
// scan performs a new scan on the given directory, compares against the already
// cached filenames, and returns file sets: creates, deletes, updates.
-func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Interface, error) {
+func (fc *fileCache) scan(keyDir string) (mapset.Set, mapset.Set, mapset.Set, error) {
t0 := time.Now()
// List all the failes from the keystore folder
@@ -51,8 +51,8 @@ func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Inte
defer fc.mu.Unlock()
// Iterate all the files and gather their metadata
- all := set.NewNonTS()
- mods := set.NewNonTS()
+ all := mapset.NewThreadUnsafeSet()
+ mods := mapset.NewThreadUnsafeSet()
var newLastMod time.Time
for _, fi := range files {
@@ -76,9 +76,9 @@ func (fc *fileCache) scan(keyDir string) (set.Interface, set.Interface, set.Inte
t2 := time.Now()
// Update the tracked files and return the three sets
- deletes := set.Difference(fc.all, all) // Deletes = previous - current
- creates := set.Difference(all, fc.all) // Creates = current - previous
- updates := set.Difference(mods, creates) // Updates = modified - creates
+ deletes := fc.all.Difference(all) // Deletes = previous - current
+ creates := all.Difference(fc.all) // Creates = current - previous
+ updates := mods.Difference(creates) // Updates = modified - creates
fc.all, fc.lastMod = all, newLastMod
t3 := time.Now()
diff --git a/accounts/url.go b/accounts/url.go
index 21df668ef..a5add1021 100644
--- a/accounts/url.go
+++ b/accounts/url.go
@@ -76,12 +76,12 @@ func (u URL) MarshalJSON() ([]byte, error) {
// UnmarshalJSON parses url.
func (u *URL) UnmarshalJSON(input []byte) error {
- var textUrl string
- err := json.Unmarshal(input, &textUrl)
+ var textURL string
+ err := json.Unmarshal(input, &textURL)
if err != nil {
return err
}
- url, err := parseURL(textUrl)
+ url, err := parseURL(textURL)
if err != nil {
return err
}
diff --git a/accounts/url_test.go b/accounts/url_test.go
new file mode 100644
index 000000000..802772871
--- /dev/null
+++ b/accounts/url_test.go
@@ -0,0 +1,96 @@
+// Copyright 2017 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
+
+package accounts
+
+import (
+ "testing"
+)
+
+func TestURLParsing(t *testing.T) {
+ url, err := parseURL("https://ethereum.org")
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ if url.Scheme != "https" {
+ t.Errorf("expected: %v, got: %v", "https", url.Scheme)
+ }
+ if url.Path != "ethereum.org" {
+ t.Errorf("expected: %v, got: %v", "ethereum.org", url.Path)
+ }
+
+ _, err = parseURL("ethereum.org")
+ if err == nil {
+ t.Error("expected err, got: nil")
+ }
+}
+
+func TestURLString(t *testing.T) {
+ url := URL{Scheme: "https", Path: "ethereum.org"}
+ if url.String() != "https://ethereum.org" {
+ t.Errorf("expected: %v, got: %v", "https://ethereum.org", url.String())
+ }
+
+ url = URL{Scheme: "", Path: "ethereum.org"}
+ if url.String() != "ethereum.org" {
+ t.Errorf("expected: %v, got: %v", "ethereum.org", url.String())
+ }
+}
+
+func TestURLMarshalJSON(t *testing.T) {
+ url := URL{Scheme: "https", Path: "ethereum.org"}
+ json, err := url.MarshalJSON()
+ if err != nil {
+ t.Errorf("unexpcted error: %v", err)
+ }
+ if string(json) != "\"https://ethereum.org\"" {
+ t.Errorf("expected: %v, got: %v", "\"https://ethereum.org\"", string(json))
+ }
+}
+
+func TestURLUnmarshalJSON(t *testing.T) {
+ url := &URL{}
+ err := url.UnmarshalJSON([]byte("\"https://ethereum.org\""))
+ if err != nil {
+ t.Errorf("unexpcted error: %v", err)
+ }
+ if url.Scheme != "https" {
+ t.Errorf("expected: %v, got: %v", "https", url.Scheme)
+ }
+ if url.Path != "ethereum.org" {
+ t.Errorf("expected: %v, got: %v", "https", url.Path)
+ }
+}
+
+func TestURLComparison(t *testing.T) {
+ tests := []struct {
+ urlA URL
+ urlB URL
+ expect int
+ }{
+ {URL{"https", "ethereum.org"}, URL{"https", "ethereum.org"}, 0},
+ {URL{"http", "ethereum.org"}, URL{"https", "ethereum.org"}, -1},
+ {URL{"https", "ethereum.org/a"}, URL{"https", "ethereum.org"}, 1},
+ {URL{"https", "abc.org"}, URL{"https", "ethereum.org"}, -1},
+ }
+
+ for i, tt := range tests {
+ result := tt.urlA.Cmp(tt.urlB)
+ if result != tt.expect {
+ t.Errorf("test %d: cmp mismatch: expected: %d, got: %d", i, tt.expect, result)
+ }
+ }
+}