@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"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
// Minimum amount of time between cache reloads. This limit applies if the platform does
@ -71,6 +72,14 @@ type accountCache struct {
byAddr map [ common . Address ] [ ] accounts . Account
byAddr map [ common . Address ] [ ] accounts . Account
throttle * time . Timer
throttle * time . Timer
notify chan struct { }
notify chan struct { }
fileC fileCache
}
// fileCache is a cache of files seen during scan of keystore
type fileCache struct {
all * set . SetNonTS // list of all files
mtime time . Time // latest mtime seen
mu sync . RWMutex
}
}
func newAccountCache ( keydir string ) ( * accountCache , chan struct { } ) {
func newAccountCache ( keydir string ) ( * accountCache , chan struct { } ) {
@ -78,6 +87,7 @@ func newAccountCache(keydir string) (*accountCache, chan struct{}) {
keydir : keydir ,
keydir : keydir ,
byAddr : make ( map [ common . Address ] [ ] accounts . Account ) ,
byAddr : make ( map [ common . Address ] [ ] accounts . Account ) ,
notify : make ( chan struct { } , 1 ) ,
notify : make ( chan struct { } , 1 ) ,
fileC : fileCache { all : set . NewNonTS ( ) } ,
}
}
ac . watcher = newWatcher ( ac )
ac . watcher = newWatcher ( ac )
return ac , ac . notify
return ac , ac . notify
@ -127,6 +137,23 @@ func (ac *accountCache) delete(removed accounts.Account) {
}
}
}
}
// deleteByFile removes an account referenced by the given path.
func ( ac * accountCache ) deleteByFile ( path string ) {
ac . mu . Lock ( )
defer ac . mu . Unlock ( )
i := sort . Search ( len ( ac . all ) , func ( i int ) bool { return ac . all [ i ] . URL . Path >= path } )
if i < len ( ac . all ) && ac . all [ i ] . URL . Path == path {
removed := ac . all [ i ]
ac . all = append ( ac . all [ : i ] , ac . all [ i + 1 : ] ... )
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 [ ] accounts . Account , elem accounts . Account ) [ ] accounts . Account {
func removeAccount ( slice [ ] accounts . Account , elem accounts . Account ) [ ] accounts . Account {
for i := range slice {
for i := range slice {
if slice [ i ] == elem {
if slice [ i ] == elem {
@ -167,15 +194,16 @@ func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
default :
default :
err := & AmbiguousAddrError { Addr : a . Address , Matches : make ( [ ] accounts . Account , len ( matches ) ) }
err := & AmbiguousAddrError { Addr : a . Address , Matches : make ( [ ] accounts . Account , len ( matches ) ) }
copy ( err . Matches , matches )
copy ( err . Matches , matches )
sort . Sort ( accountsByURL ( err . Matches ) )
return accounts . Account { } , err
return accounts . Account { } , err
}
}
}
}
func ( ac * accountCache ) maybeReload ( ) {
func ( ac * accountCache ) maybeReload ( ) {
ac . mu . Lock ( )
ac . mu . Lock ( )
defer ac . mu . Unlock ( )
if ac . watcher . running {
if ac . watcher . running {
ac . mu . Unlock ( )
return // A watcher is running and will keep the cache up-to-date.
return // A watcher is running and will keep the cache up-to-date.
}
}
if ac . throttle == nil {
if ac . throttle == nil {
@ -184,12 +212,15 @@ func (ac *accountCache) maybeReload() {
select {
select {
case <- ac . throttle . C :
case <- ac . throttle . C :
default :
default :
ac . mu . Unlock ( )
return // The cache was reloaded recently.
return // The cache was reloaded recently.
}
}
}
}
// No watcher running, start it.
ac . watcher . start ( )
ac . watcher . start ( )
ac . reload ( )
ac . throttle . Reset ( minReloadInterval )
ac . throttle . Reset ( minReloadInterval )
ac . mu . Unlock ( )
ac . scanAccounts ( )
}
}
func ( ac * accountCache ) close ( ) {
func ( ac * accountCache ) close ( ) {
@ -205,54 +236,76 @@ func (ac *accountCache) close() {
ac . mu . Unlock ( )
ac . mu . Unlock ( )
}
}
// reload caches addresses of existing accounts.
// scanFiles performs a new scan on the given directory, compares against the already
// Callers must hold ac.mu.
// cached filenames, and returns file sets: new, missing , modified
func ( ac * accountCache ) reload ( ) {
func ( fc * fileCache ) scanFiles ( keyDir string ) ( set . Interface , set . Interface , set . Interface , error ) {
accounts , err := ac . scan ( )
t0 := time . Now ( )
files , err := ioutil . ReadDir ( keyDir )
t1 := time . Now ( )
if err != nil {
if err != nil {
log . Debug ( "Failed to reload keystore contents" , "err" , err )
return nil , nil , nil , err
}
}
ac . all = accounts
fc . mu . RLock ( )
sort . Sort ( ac . all )
prevMtime := fc . mtime
for k := range ac . byAddr {
fc . mu . RUnlock ( )
delete ( ac . byAddr , k )
}
filesNow := set . NewNonTS ( )
for _ , a := range accounts {
moddedFiles := set . NewNonTS ( )
ac . byAddr [ a . Address ] = append ( ac . byAddr [ a . Address ] , a )
var newMtime time . Time
}
for _ , fi := range files {
select {
modTime := fi . ModTime ( )
case ac . notify <- struct { } { } :
path := filepath . Join ( keyDir , fi . Name ( ) )
default :
if skipKeyFile ( fi ) {
log . Trace ( "Ignoring file on account scan" , "path" , path )
continue
}
filesNow . Add ( path )
if modTime . After ( prevMtime ) {
moddedFiles . Add ( path )
}
if modTime . After ( newMtime ) {
newMtime = modTime
}
}
}
log . Debug ( "Reloaded keystore contents" , "accounts" , len ( ac . all ) )
t2 := time . Now ( )
fc . mu . Lock ( )
// Missing = previous - current
missing := set . Difference ( fc . all , filesNow )
// New = current - previous
newFiles := set . Difference ( filesNow , fc . all )
// Modified = modified - new
modified := set . Difference ( moddedFiles , newFiles )
fc . all = filesNow
fc . mtime = newMtime
fc . mu . Unlock ( )
t3 := time . Now ( )
log . Debug ( "FS scan times" , "list" , t1 . Sub ( t0 ) , "set" , t2 . Sub ( t1 ) , "diff" , t3 . Sub ( t2 ) )
return newFiles , missing , modified , nil
}
}
func ( ac * accountCache ) scan ( ) ( [ ] accounts . Account , error ) {
// scanAccounts checks if any changes have occurred on the filesystem, and
files , err := ioutil . ReadDir ( ac . keydir )
// updates the account cache accordingly
func ( ac * accountCache ) scanAccounts ( ) error {
newFiles , missingFiles , modified , err := ac . fileC . scanFiles ( ac . keydir )
t1 := time . Now ( )
if err != nil {
if err != nil {
return nil , err
log . Debug ( "Failed to reload keystore contents" , "err" , err )
return err
}
}
var (
var (
buf = new ( bufio . Reader )
buf = new ( bufio . Reader )
addrs [ ] accounts . Account
keyJSON struct {
keyJSON struct {
Address string ` json:"address" `
Address string ` json:"address" `
}
}
)
)
for _ , fi := range files {
readAccount := func ( path string ) * accounts . Account {
path := filepath . Join ( ac . keydir , fi . Name ( ) )
if skipKeyFile ( fi ) {
log . Trace ( "Ignoring file on account scan" , "path" , path )
continue
}
logger := log . New ( "path" , path )
fd , err := os . Open ( path )
fd , err := os . Open ( path )
if err != nil {
if err != nil {
logger . Trace ( "Failed to open keystore file" , "err" , err )
log . Trace ( "Failed to open keystore file" , "path" , path , "err" , err )
continue
return nil
}
}
defer fd . Close ( )
buf . Reset ( fd )
buf . Reset ( fd )
// Parse the address.
// Parse the address.
keyJSON . Address = ""
keyJSON . Address = ""
@ -260,15 +313,45 @@ func (ac *accountCache) scan() ([]accounts.Account, error) {
addr := common . HexToAddress ( keyJSON . Address )
addr := common . HexToAddress ( keyJSON . Address )
switch {
switch {
case err != nil :
case err != nil :
logger . Debug ( "Failed to decode keystore key" , "err" , err )
log . Debug ( "Failed to decode keystore key" , "path" , path , "err" , err )
case ( addr == common . Address { } ) :
case ( addr == common . Address { } ) :
logger . Debug ( "Failed to decode keystore key" , "err" , "missing or zero address" )
log . Debug ( "Failed to decode keystore key" , "path" , path , "err" , "missing or zero address" )
default :
default :
addrs = append ( addrs , accounts . Account { Address : addr , URL : accounts . URL { Scheme : KeyStoreScheme , Path : path } } )
return & accounts . Account { Address : addr , URL : accounts . URL { Scheme : KeyStoreScheme , Path : path } }
}
}
fd . Close ( )
return nil
}
}
return addrs , err
for _ , p := range newFiles . List ( ) {
path , _ := p . ( string )
a := readAccount ( path )
if a != nil {
ac . add ( * a )
}
}
for _ , p := range missingFiles . List ( ) {
path , _ := p . ( string )
ac . deleteByFile ( path )
}
for _ , p := range modified . List ( ) {
path , _ := p . ( string )
a := readAccount ( path )
ac . deleteByFile ( path )
if a != nil {
ac . add ( * a )
}
}
t2 := time . Now ( )
select {
case ac . notify <- struct { } { } :
default :
}
log . Trace ( "Handled keystore changes" , "time" , t2 . Sub ( t1 ) )
return nil
}
}
func skipKeyFile ( fi os . FileInfo ) bool {
func skipKeyFile ( fi os . FileInfo ) bool {