mirror of https://github.com/ethereum/go-ethereum
In order to avoid disk thrashing for Accounts and HasAccount, address->key file mappings are now cached in memory. This makes it no longer necessary to keep the key address in the file name. The address of each key is derived from file content instead. There are minor user-visible changes: - "geth account list" now reports key file paths alongside the address. - If multiple keys are present for an address, unlocking by address is not possible. Users are directed to remove the duplicate files instead. Unlocking by index is still possible. - Key files are overwritten written in place when updating the password.pull/2284/head
parent
ef63e9af55
commit
a9f26dcd0d
@ -0,0 +1,269 @@ |
|||||||
|
// Copyright 2016 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 ( |
||||||
|
"bufio" |
||||||
|
"encoding/json" |
||||||
|
"fmt" |
||||||
|
"io/ioutil" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
"sort" |
||||||
|
"strings" |
||||||
|
"sync" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/logger" |
||||||
|
"github.com/ethereum/go-ethereum/logger/glog" |
||||||
|
) |
||||||
|
|
||||||
|
// Minimum amount of time between cache reloads. This limit applies if the platform does
|
||||||
|
// not support change notifications. It also applies if the keystore directory does not
|
||||||
|
// exist yet, the code will attempt to create a watcher at most this often.
|
||||||
|
const minReloadInterval = 2 * time.Second |
||||||
|
|
||||||
|
type accountsByFile []Account |
||||||
|
|
||||||
|
func (s accountsByFile) Len() int { return len(s) } |
||||||
|
func (s accountsByFile) Less(i, j int) bool { return s[i].File < s[j].File } |
||||||
|
func (s accountsByFile) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
||||||
|
|
||||||
|
// AmbiguousAddrError is returned when attempting to unlock
|
||||||
|
// an address for which more than one file exists.
|
||||||
|
type AmbiguousAddrError struct { |
||||||
|
Addr common.Address |
||||||
|
Matches []Account |
||||||
|
} |
||||||
|
|
||||||
|
func (err *AmbiguousAddrError) Error() string { |
||||||
|
files := "" |
||||||
|
for i, a := range err.Matches { |
||||||
|
files += a.File |
||||||
|
if i < len(err.Matches)-1 { |
||||||
|
files += ", " |
||||||
|
} |
||||||
|
} |
||||||
|
return fmt.Sprintf("multiple keys match address (%s)", files) |
||||||
|
} |
||||||
|
|
||||||
|
// addrCache is a live index of all accounts in the keystore.
|
||||||
|
type addrCache struct { |
||||||
|
keydir string |
||||||
|
watcher *watcher |
||||||
|
mu sync.Mutex |
||||||
|
all accountsByFile |
||||||
|
byAddr map[common.Address][]Account |
||||||
|
throttle *time.Timer |
||||||
|
} |
||||||
|
|
||||||
|
func newAddrCache(keydir string) *addrCache { |
||||||
|
ac := &addrCache{ |
||||||
|
keydir: keydir, |
||||||
|
byAddr: make(map[common.Address][]Account), |
||||||
|
} |
||||||
|
ac.watcher = newWatcher(ac) |
||||||
|
return ac |
||||||
|
} |
||||||
|
|
||||||
|
func (ac *addrCache) accounts() []Account { |
||||||
|
ac.maybeReload() |
||||||
|
ac.mu.Lock() |
||||||
|
defer ac.mu.Unlock() |
||||||
|
cpy := make([]Account, len(ac.all)) |
||||||
|
copy(cpy, ac.all) |
||||||
|
return cpy |
||||||
|
} |
||||||
|
|
||||||
|
func (ac *addrCache) hasAddress(addr common.Address) bool { |
||||||
|
ac.maybeReload() |
||||||
|
ac.mu.Lock() |
||||||
|
defer ac.mu.Unlock() |
||||||
|
return len(ac.byAddr[addr]) > 0 |
||||||
|
} |
||||||
|
|
||||||
|
func (ac *addrCache) add(newAccount Account) { |
||||||
|
ac.mu.Lock() |
||||||
|
defer ac.mu.Unlock() |
||||||
|
|
||||||
|
i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].File >= newAccount.File }) |
||||||
|
if i < len(ac.all) && ac.all[i] == newAccount { |
||||||
|
return |
||||||
|
} |
||||||
|
// newAccount is not in the cache.
|
||||||
|
ac.all = append(ac.all, Account{}) |
||||||
|
copy(ac.all[i+1:], ac.all[i:]) |
||||||
|
ac.all[i] = newAccount |
||||||
|
ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount) |
||||||
|
} |
||||||
|
|
||||||
|
// note: removed needs to be unique here (i.e. both File and Address must be set).
|
||||||
|
func (ac *addrCache) delete(removed Account) { |
||||||
|
ac.mu.Lock() |
||||||
|
defer ac.mu.Unlock() |
||||||
|
ac.all = removeAccount(ac.all, removed) |
||||||
|
if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 { |
||||||
|
delete(ac.byAddr, removed.Address) |
||||||
|
} else { |
||||||
|
ac.byAddr[removed.Address] = ba |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func removeAccount(slice []Account, elem Account) []Account { |
||||||
|
for i := range slice { |
||||||
|
if slice[i] == elem { |
||||||
|
return append(slice[:i], slice[i+1:]...) |
||||||
|
} |
||||||
|
} |
||||||
|
return slice |
||||||
|
} |
||||||
|
|
||||||
|
// find returns the cached account for address if there is a unique match.
|
||||||
|
// The exact matching rules are explained by the documentation of Account.
|
||||||
|
// Callers must hold ac.mu.
|
||||||
|
func (ac *addrCache) find(a Account) (Account, error) { |
||||||
|
// Limit search to address candidates if possible.
|
||||||
|
matches := ac.all |
||||||
|
if (a.Address != common.Address{}) { |
||||||
|
matches = ac.byAddr[a.Address] |
||||||
|
} |
||||||
|
if a.File != "" { |
||||||
|
// If only the basename is specified, complete the path.
|
||||||
|
if !strings.ContainsRune(a.File, filepath.Separator) { |
||||||
|
a.File = filepath.Join(ac.keydir, a.File) |
||||||
|
} |
||||||
|
for i := range matches { |
||||||
|
if matches[i].File == a.File { |
||||||
|
return matches[i], nil |
||||||
|
} |
||||||
|
} |
||||||
|
if (a.Address == common.Address{}) { |
||||||
|
return Account{}, ErrNoMatch |
||||||
|
} |
||||||
|
} |
||||||
|
switch len(matches) { |
||||||
|
case 1: |
||||||
|
return matches[0], nil |
||||||
|
case 0: |
||||||
|
return Account{}, ErrNoMatch |
||||||
|
default: |
||||||
|
err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]Account, len(matches))} |
||||||
|
copy(err.Matches, matches) |
||||||
|
return Account{}, err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (ac *addrCache) maybeReload() { |
||||||
|
ac.mu.Lock() |
||||||
|
defer ac.mu.Unlock() |
||||||
|
if ac.watcher.running { |
||||||
|
return // A watcher is running and will keep the cache up-to-date.
|
||||||
|
} |
||||||
|
if ac.throttle == nil { |
||||||
|
ac.throttle = time.NewTimer(0) |
||||||
|
} else { |
||||||
|
select { |
||||||
|
case <-ac.throttle.C: |
||||||
|
default: |
||||||
|
return // The cache was reloaded recently.
|
||||||
|
} |
||||||
|
} |
||||||
|
ac.watcher.start() |
||||||
|
ac.reload() |
||||||
|
ac.throttle.Reset(minReloadInterval) |
||||||
|
} |
||||||
|
|
||||||
|
func (ac *addrCache) close() { |
||||||
|
ac.mu.Lock() |
||||||
|
ac.watcher.close() |
||||||
|
if ac.throttle != nil { |
||||||
|
ac.throttle.Stop() |
||||||
|
} |
||||||
|
ac.mu.Unlock() |
||||||
|
} |
||||||
|
|
||||||
|
// reload caches addresses of existing accounts.
|
||||||
|
// Callers must hold ac.mu.
|
||||||
|
func (ac *addrCache) reload() { |
||||||
|
accounts, err := ac.scan() |
||||||
|
if err != nil && glog.V(logger.Debug) { |
||||||
|
glog.Errorf("can't load keys: %v", err) |
||||||
|
} |
||||||
|
ac.all = accounts |
||||||
|
sort.Sort(ac.all) |
||||||
|
for k := range ac.byAddr { |
||||||
|
delete(ac.byAddr, k) |
||||||
|
} |
||||||
|
for _, a := range accounts { |
||||||
|
ac.byAddr[a.Address] = append(ac.byAddr[a.Address], a) |
||||||
|
} |
||||||
|
glog.V(logger.Debug).Infof("reloaded keys, cache has %d accounts", len(ac.all)) |
||||||
|
} |
||||||
|
|
||||||
|
func (ac *addrCache) scan() ([]Account, error) { |
||||||
|
files, err := ioutil.ReadDir(ac.keydir) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
var ( |
||||||
|
buf = new(bufio.Reader) |
||||||
|
addrs []Account |
||||||
|
keyJSON struct { |
||||||
|
Address common.Address `json:"address"` |
||||||
|
} |
||||||
|
) |
||||||
|
for _, fi := range files { |
||||||
|
path := filepath.Join(ac.keydir, fi.Name()) |
||||||
|
if skipKeyFile(fi) { |
||||||
|
glog.V(logger.Detail).Infof("ignoring file %s", path) |
||||||
|
continue |
||||||
|
} |
||||||
|
fd, err := os.Open(path) |
||||||
|
if err != nil { |
||||||
|
glog.V(logger.Detail).Infoln(err) |
||||||
|
continue |
||||||
|
} |
||||||
|
buf.Reset(fd) |
||||||
|
// Parse the address.
|
||||||
|
keyJSON.Address = common.Address{} |
||||||
|
err = json.NewDecoder(buf).Decode(&keyJSON) |
||||||
|
switch { |
||||||
|
case err != nil: |
||||||
|
glog.V(logger.Debug).Infof("can't decode key %s: %v", path, err) |
||||||
|
case (keyJSON.Address == common.Address{}): |
||||||
|
glog.V(logger.Debug).Infof("can't decode key %s: missing or zero address", path) |
||||||
|
default: |
||||||
|
addrs = append(addrs, Account{Address: keyJSON.Address, File: path}) |
||||||
|
} |
||||||
|
fd.Close() |
||||||
|
} |
||||||
|
return addrs, err |
||||||
|
} |
||||||
|
|
||||||
|
func skipKeyFile(fi os.FileInfo) bool { |
||||||
|
// Skip editor backups and UNIX-style hidden files.
|
||||||
|
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") { |
||||||
|
return true |
||||||
|
} |
||||||
|
// Skip misc special files, directories (yes, symlinks too).
|
||||||
|
if fi.IsDir() || fi.Mode()&os.ModeType != 0 { |
||||||
|
return true |
||||||
|
} |
||||||
|
return false |
||||||
|
} |
@ -0,0 +1,283 @@ |
|||||||
|
// Copyright 2016 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 ( |
||||||
|
"fmt" |
||||||
|
"math/rand" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
"reflect" |
||||||
|
"sort" |
||||||
|
"testing" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/cespare/cp" |
||||||
|
"github.com/davecgh/go-spew/spew" |
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
) |
||||||
|
|
||||||
|
var ( |
||||||
|
cachetestDir, _ = filepath.Abs(filepath.Join("testdata", "keystore")) |
||||||
|
cachetestAccounts = []Account{ |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), |
||||||
|
File: filepath.Join(cachetestDir, "UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("f466859ead1932d743d622cb74fc058882e8648a"), |
||||||
|
File: filepath.Join(cachetestDir, "aaa"), |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("289d485d9771714cce91d3393d764e1311907acc"), |
||||||
|
File: filepath.Join(cachetestDir, "zzz"), |
||||||
|
}, |
||||||
|
} |
||||||
|
) |
||||||
|
|
||||||
|
func TestWatchNewFile(t *testing.T) { |
||||||
|
t.Parallel() |
||||||
|
|
||||||
|
dir, am := tmpManager(t, false) |
||||||
|
defer os.RemoveAll(dir) |
||||||
|
|
||||||
|
// Ensure the watcher is started before adding any files.
|
||||||
|
am.Accounts() |
||||||
|
time.Sleep(200 * time.Millisecond) |
||||||
|
|
||||||
|
// Move in the files.
|
||||||
|
wantAccounts := make([]Account, len(cachetestAccounts)) |
||||||
|
for i := range cachetestAccounts { |
||||||
|
a := cachetestAccounts[i] |
||||||
|
a.File = filepath.Join(dir, filepath.Base(a.File)) |
||||||
|
wantAccounts[i] = a |
||||||
|
if err := cp.CopyFile(a.File, cachetestAccounts[i].File); err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// am should see the accounts.
|
||||||
|
var list []Account |
||||||
|
for d := 200 * time.Millisecond; d < 5*time.Second; d *= 2 { |
||||||
|
list = am.Accounts() |
||||||
|
if reflect.DeepEqual(list, wantAccounts) { |
||||||
|
return |
||||||
|
} |
||||||
|
time.Sleep(d) |
||||||
|
} |
||||||
|
t.Errorf("got %s, want %s", spew.Sdump(list), spew.Sdump(wantAccounts)) |
||||||
|
} |
||||||
|
|
||||||
|
func TestWatchNoDir(t *testing.T) { |
||||||
|
t.Parallel() |
||||||
|
|
||||||
|
// Create am but not the directory that it watches.
|
||||||
|
rand.Seed(time.Now().UnixNano()) |
||||||
|
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watch-test-%d-%d", os.Getpid(), rand.Int())) |
||||||
|
am := NewManager(dir, LightScryptN, LightScryptP) |
||||||
|
|
||||||
|
list := am.Accounts() |
||||||
|
if len(list) > 0 { |
||||||
|
t.Error("initial account list not empty:", list) |
||||||
|
} |
||||||
|
time.Sleep(100 * time.Millisecond) |
||||||
|
|
||||||
|
// Create the directory and copy a key file into it.
|
||||||
|
os.MkdirAll(dir, 0700) |
||||||
|
defer os.RemoveAll(dir) |
||||||
|
file := filepath.Join(dir, "aaa") |
||||||
|
if err := cp.CopyFile(file, cachetestAccounts[0].File); err != nil { |
||||||
|
t.Fatal(err) |
||||||
|
} |
||||||
|
|
||||||
|
// am should see the account.
|
||||||
|
wantAccounts := []Account{cachetestAccounts[0]} |
||||||
|
wantAccounts[0].File = file |
||||||
|
for d := 200 * time.Millisecond; d < 8*time.Second; d *= 2 { |
||||||
|
list = am.Accounts() |
||||||
|
if reflect.DeepEqual(list, wantAccounts) { |
||||||
|
return |
||||||
|
} |
||||||
|
time.Sleep(d) |
||||||
|
} |
||||||
|
t.Errorf("\ngot %v\nwant %v", list, wantAccounts) |
||||||
|
} |
||||||
|
|
||||||
|
func TestCacheInitialReload(t *testing.T) { |
||||||
|
cache := newAddrCache(cachetestDir) |
||||||
|
accounts := cache.accounts() |
||||||
|
if !reflect.DeepEqual(accounts, cachetestAccounts) { |
||||||
|
t.Fatalf("got initial accounts: %swant %s", spew.Sdump(accounts), spew.Sdump(cachetestAccounts)) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestCacheAddDeleteOrder(t *testing.T) { |
||||||
|
cache := newAddrCache("testdata/no-such-dir") |
||||||
|
cache.watcher.running = true // prevent unexpected reloads
|
||||||
|
|
||||||
|
accounts := []Account{ |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), |
||||||
|
File: "-309830980", |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("2cac1adea150210703ba75ed097ddfe24e14f213"), |
||||||
|
File: "ggg", |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), |
||||||
|
File: "zzzzzz-the-very-last-one.keyXXX", |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("d49ff4eeb0b2686ed89c0fc0f2b6ea533ddbbd5e"), |
||||||
|
File: "SOMETHING.key", |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), |
||||||
|
File: "UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8", |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("f466859ead1932d743d622cb74fc058882e8648a"), |
||||||
|
File: "aaa", |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("289d485d9771714cce91d3393d764e1311907acc"), |
||||||
|
File: "zzz", |
||||||
|
}, |
||||||
|
} |
||||||
|
for _, a := range accounts { |
||||||
|
cache.add(a) |
||||||
|
} |
||||||
|
// Add some of them twice to check that they don't get reinserted.
|
||||||
|
cache.add(accounts[0]) |
||||||
|
cache.add(accounts[2]) |
||||||
|
|
||||||
|
// Check that the account list is sorted by filename.
|
||||||
|
wantAccounts := make([]Account, len(accounts)) |
||||||
|
copy(wantAccounts, accounts) |
||||||
|
sort.Sort(accountsByFile(wantAccounts)) |
||||||
|
list := cache.accounts() |
||||||
|
if !reflect.DeepEqual(list, wantAccounts) { |
||||||
|
t.Fatalf("got accounts: %s\nwant %s", spew.Sdump(accounts), spew.Sdump(wantAccounts)) |
||||||
|
} |
||||||
|
for _, a := range accounts { |
||||||
|
if !cache.hasAddress(a.Address) { |
||||||
|
t.Errorf("expected hasAccount(%x) to return true", a.Address) |
||||||
|
} |
||||||
|
} |
||||||
|
if cache.hasAddress(common.HexToAddress("fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e")) { |
||||||
|
t.Errorf("expected hasAccount(%x) to return false", common.HexToAddress("fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e")) |
||||||
|
} |
||||||
|
|
||||||
|
// Delete a few keys from the cache.
|
||||||
|
for i := 0; i < len(accounts); i += 2 { |
||||||
|
cache.delete(wantAccounts[i]) |
||||||
|
} |
||||||
|
cache.delete(Account{Address: common.HexToAddress("fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e"), File: "something"}) |
||||||
|
|
||||||
|
// Check content again after deletion.
|
||||||
|
wantAccountsAfterDelete := []Account{ |
||||||
|
wantAccounts[1], |
||||||
|
wantAccounts[3], |
||||||
|
wantAccounts[5], |
||||||
|
} |
||||||
|
list = cache.accounts() |
||||||
|
if !reflect.DeepEqual(list, wantAccountsAfterDelete) { |
||||||
|
t.Fatalf("got accounts after delete: %s\nwant %s", spew.Sdump(list), spew.Sdump(wantAccountsAfterDelete)) |
||||||
|
} |
||||||
|
for _, a := range wantAccountsAfterDelete { |
||||||
|
if !cache.hasAddress(a.Address) { |
||||||
|
t.Errorf("expected hasAccount(%x) to return true", a.Address) |
||||||
|
} |
||||||
|
} |
||||||
|
if cache.hasAddress(wantAccounts[0].Address) { |
||||||
|
t.Errorf("expected hasAccount(%x) to return false", wantAccounts[0].Address) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestCacheFind(t *testing.T) { |
||||||
|
dir := filepath.Join("testdata", "dir") |
||||||
|
cache := newAddrCache(dir) |
||||||
|
cache.watcher.running = true // prevent unexpected reloads
|
||||||
|
|
||||||
|
accounts := []Account{ |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), |
||||||
|
File: filepath.Join(dir, "a.key"), |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("2cac1adea150210703ba75ed097ddfe24e14f213"), |
||||||
|
File: filepath.Join(dir, "b.key"), |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("d49ff4eeb0b2686ed89c0fc0f2b6ea533ddbbd5e"), |
||||||
|
File: filepath.Join(dir, "c.key"), |
||||||
|
}, |
||||||
|
{ |
||||||
|
Address: common.HexToAddress("d49ff4eeb0b2686ed89c0fc0f2b6ea533ddbbd5e"), |
||||||
|
File: filepath.Join(dir, "c2.key"), |
||||||
|
}, |
||||||
|
} |
||||||
|
for _, a := range accounts { |
||||||
|
cache.add(a) |
||||||
|
} |
||||||
|
|
||||||
|
nomatchAccount := Account{ |
||||||
|
Address: common.HexToAddress("f466859ead1932d743d622cb74fc058882e8648a"), |
||||||
|
File: filepath.Join(dir, "something"), |
||||||
|
} |
||||||
|
tests := []struct { |
||||||
|
Query Account |
||||||
|
WantResult Account |
||||||
|
WantError error |
||||||
|
}{ |
||||||
|
// by address
|
||||||
|
{Query: Account{Address: accounts[0].Address}, WantResult: accounts[0]}, |
||||||
|
// by file
|
||||||
|
{Query: Account{File: accounts[0].File}, WantResult: accounts[0]}, |
||||||
|
// by basename
|
||||||
|
{Query: Account{File: filepath.Base(accounts[0].File)}, WantResult: accounts[0]}, |
||||||
|
// by file and address
|
||||||
|
{Query: accounts[0], WantResult: accounts[0]}, |
||||||
|
// ambiguous address, tie resolved by file
|
||||||
|
{Query: accounts[2], WantResult: accounts[2]}, |
||||||
|
// ambiguous address error
|
||||||
|
{ |
||||||
|
Query: Account{Address: accounts[2].Address}, |
||||||
|
WantError: &AmbiguousAddrError{ |
||||||
|
Addr: accounts[2].Address, |
||||||
|
Matches: []Account{accounts[2], accounts[3]}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
// no match error
|
||||||
|
{Query: nomatchAccount, WantError: ErrNoMatch}, |
||||||
|
{Query: Account{File: nomatchAccount.File}, WantError: ErrNoMatch}, |
||||||
|
{Query: Account{File: filepath.Base(nomatchAccount.File)}, WantError: ErrNoMatch}, |
||||||
|
{Query: Account{Address: nomatchAccount.Address}, WantError: ErrNoMatch}, |
||||||
|
} |
||||||
|
for i, test := range tests { |
||||||
|
a, err := cache.find(test.Query) |
||||||
|
if !reflect.DeepEqual(err, test.WantError) { |
||||||
|
t.Errorf("test %d: error mismatch for query %v\ngot %q\nwant %q", i, test.Query, err, test.WantError) |
||||||
|
continue |
||||||
|
} |
||||||
|
if a != test.WantResult { |
||||||
|
t.Errorf("test %d: result mismatch for query %v\ngot %v\nwant %v", i, test.Query, a, test.WantResult) |
||||||
|
continue |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1 @@ |
|||||||
|
{"address":"45dea0fb0bba44f4fcf290bba71fd57d7117cbb8","crypto":{"cipher":"aes-128-ctr","ciphertext":"b87781948a1befd247bff51ef4063f716cf6c2d3481163e9a8f42e1f9bb74145","cipherparams":{"iv":"dc4926b48a105133d2f16b96833abf1e"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":2,"p":1,"r":8,"salt":"004244bbdc51cadda545b1cfa43cff9ed2ae88e08c61f1479dbb45410722f8f0"},"mac":"39990c1684557447940d4c69e06b1b82b2aceacb43f284df65c956daf3046b85"},"id":"ce541d8d-c79b-40f8-9f8c-20f59616faba","version":3} |
@ -0,0 +1,113 @@ |
|||||||
|
// Copyright 2016 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/>.
|
||||||
|
|
||||||
|
// +build darwin freebsd linux netbsd solaris windows
|
||||||
|
|
||||||
|
package accounts |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/logger" |
||||||
|
"github.com/ethereum/go-ethereum/logger/glog" |
||||||
|
"github.com/rjeczalik/notify" |
||||||
|
) |
||||||
|
|
||||||
|
type watcher struct { |
||||||
|
ac *addrCache |
||||||
|
starting bool |
||||||
|
running bool |
||||||
|
ev chan notify.EventInfo |
||||||
|
quit chan struct{} |
||||||
|
} |
||||||
|
|
||||||
|
func newWatcher(ac *addrCache) *watcher { |
||||||
|
return &watcher{ |
||||||
|
ac: ac, |
||||||
|
ev: make(chan notify.EventInfo, 10), |
||||||
|
quit: make(chan struct{}), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// starts the watcher loop in the background.
|
||||||
|
// Start a watcher in the background if that's not already in progress.
|
||||||
|
// The caller must hold w.ac.mu.
|
||||||
|
func (w *watcher) start() { |
||||||
|
if w.starting || w.running { |
||||||
|
return |
||||||
|
} |
||||||
|
w.starting = true |
||||||
|
go w.loop() |
||||||
|
} |
||||||
|
|
||||||
|
func (w *watcher) close() { |
||||||
|
close(w.quit) |
||||||
|
} |
||||||
|
|
||||||
|
func (w *watcher) loop() { |
||||||
|
defer func() { |
||||||
|
w.ac.mu.Lock() |
||||||
|
w.running = false |
||||||
|
w.starting = false |
||||||
|
w.ac.mu.Unlock() |
||||||
|
}() |
||||||
|
|
||||||
|
err := notify.Watch(w.ac.keydir, w.ev, notify.All) |
||||||
|
if err != nil { |
||||||
|
glog.V(logger.Detail).Infof("can't watch %s: %v", w.ac.keydir, err) |
||||||
|
return |
||||||
|
} |
||||||
|
defer notify.Stop(w.ev) |
||||||
|
glog.V(logger.Detail).Infof("now watching %s", w.ac.keydir) |
||||||
|
defer glog.V(logger.Detail).Infof("no longer watching %s", w.ac.keydir) |
||||||
|
|
||||||
|
w.ac.mu.Lock() |
||||||
|
w.running = true |
||||||
|
w.ac.mu.Unlock() |
||||||
|
|
||||||
|
// Wait for file system events and reload.
|
||||||
|
// When an event occurs, the reload call is delayed a bit so that
|
||||||
|
// multiple events arriving quickly only cause a single reload.
|
||||||
|
var ( |
||||||
|
debounce = time.NewTimer(0) |
||||||
|
debounceDuration = 500 * time.Millisecond |
||||||
|
inCycle, hadEvent bool |
||||||
|
) |
||||||
|
defer debounce.Stop() |
||||||
|
for { |
||||||
|
select { |
||||||
|
case <-w.quit: |
||||||
|
return |
||||||
|
case <-w.ev: |
||||||
|
if !inCycle { |
||||||
|
debounce.Reset(debounceDuration) |
||||||
|
inCycle = true |
||||||
|
} else { |
||||||
|
hadEvent = true |
||||||
|
} |
||||||
|
case <-debounce.C: |
||||||
|
w.ac.mu.Lock() |
||||||
|
w.ac.reload() |
||||||
|
w.ac.mu.Unlock() |
||||||
|
if hadEvent { |
||||||
|
debounce.Reset(debounceDuration) |
||||||
|
inCycle, hadEvent = true, false |
||||||
|
} else { |
||||||
|
inCycle, hadEvent = false, false |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
// Copyright 2016 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/>.
|
||||||
|
|
||||||
|
// +build !darwin,!freebsd,!linux,!netbsd,!solaris,!windows
|
||||||
|
|
||||||
|
// This is the fallback implementation of directory watching.
|
||||||
|
// It is used on unsupported platforms.
|
||||||
|
|
||||||
|
package accounts |
||||||
|
|
||||||
|
type watcher struct{ running bool } |
||||||
|
|
||||||
|
func newWatcher(*addrCache) *watcher { return new(watcher) } |
||||||
|
func (*watcher) start() {} |
||||||
|
func (*watcher) close() {} |
Loading…
Reference in new issue