github.com/MetalBlockchain/subnet-evm@v0.4.9/accounts/keystore/account_cache.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2017 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package keystore
    28  
    29  import (
    30  	"bufio"
    31  	"encoding/json"
    32  	"fmt"
    33  	"os"
    34  	"path/filepath"
    35  	"sort"
    36  	"strings"
    37  	"sync"
    38  	"time"
    39  
    40  	"github.com/MetalBlockchain/subnet-evm/accounts"
    41  	mapset "github.com/deckarep/golang-set"
    42  	"github.com/ethereum/go-ethereum/common"
    43  	"github.com/ethereum/go-ethereum/log"
    44  )
    45  
    46  // Minimum amount of time between cache reloads. This limit applies if the platform does
    47  // not support change notifications. It also applies if the keystore directory does not
    48  // exist yet, the code will attempt to create a watcher at most this often.
    49  const minReloadInterval = 2 * time.Second
    50  
    51  type accountsByURL []accounts.Account
    52  
    53  func (s accountsByURL) Len() int           { return len(s) }
    54  func (s accountsByURL) Less(i, j int) bool { return s[i].URL.Cmp(s[j].URL) < 0 }
    55  func (s accountsByURL) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    56  
    57  // AmbiguousAddrError is returned when attempting to unlock
    58  // an address for which more than one file exists.
    59  type AmbiguousAddrError struct {
    60  	Addr    common.Address
    61  	Matches []accounts.Account
    62  }
    63  
    64  func (err *AmbiguousAddrError) Error() string {
    65  	files := ""
    66  	for i, a := range err.Matches {
    67  		files += a.URL.Path
    68  		if i < len(err.Matches)-1 {
    69  			files += ", "
    70  		}
    71  	}
    72  	return fmt.Sprintf("multiple keys match address (%s)", files)
    73  }
    74  
    75  // accountCache is a live index of all accounts in the keystore.
    76  type accountCache struct {
    77  	keydir   string
    78  	watcher  *watcher
    79  	mu       sync.Mutex
    80  	all      accountsByURL
    81  	byAddr   map[common.Address][]accounts.Account
    82  	throttle *time.Timer
    83  	notify   chan struct{}
    84  	fileC    fileCache
    85  }
    86  
    87  func newAccountCache(keydir string) (*accountCache, chan struct{}) {
    88  	ac := &accountCache{
    89  		keydir: keydir,
    90  		byAddr: make(map[common.Address][]accounts.Account),
    91  		notify: make(chan struct{}, 1),
    92  		fileC:  fileCache{all: mapset.NewThreadUnsafeSet()},
    93  	}
    94  	ac.watcher = newWatcher(ac)
    95  	return ac, ac.notify
    96  }
    97  
    98  func (ac *accountCache) accounts() []accounts.Account {
    99  	ac.maybeReload()
   100  	ac.mu.Lock()
   101  	defer ac.mu.Unlock()
   102  	cpy := make([]accounts.Account, len(ac.all))
   103  	copy(cpy, ac.all)
   104  	return cpy
   105  }
   106  
   107  func (ac *accountCache) hasAddress(addr common.Address) bool {
   108  	ac.maybeReload()
   109  	ac.mu.Lock()
   110  	defer ac.mu.Unlock()
   111  	return len(ac.byAddr[addr]) > 0
   112  }
   113  
   114  func (ac *accountCache) add(newAccount accounts.Account) {
   115  	ac.mu.Lock()
   116  	defer ac.mu.Unlock()
   117  
   118  	i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Cmp(newAccount.URL) >= 0 })
   119  	if i < len(ac.all) && ac.all[i] == newAccount {
   120  		return
   121  	}
   122  	// newAccount is not in the cache.
   123  	ac.all = append(ac.all, accounts.Account{})
   124  	copy(ac.all[i+1:], ac.all[i:])
   125  	ac.all[i] = newAccount
   126  	ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount)
   127  }
   128  
   129  // note: removed needs to be unique here (i.e. both File and Address must be set).
   130  func (ac *accountCache) delete(removed accounts.Account) {
   131  	ac.mu.Lock()
   132  	defer ac.mu.Unlock()
   133  
   134  	ac.all = removeAccount(ac.all, removed)
   135  	if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
   136  		delete(ac.byAddr, removed.Address)
   137  	} else {
   138  		ac.byAddr[removed.Address] = ba
   139  	}
   140  }
   141  
   142  // deleteByFile removes an account referenced by the given path.
   143  func (ac *accountCache) deleteByFile(path string) {
   144  	ac.mu.Lock()
   145  	defer ac.mu.Unlock()
   146  	i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Path >= path })
   147  
   148  	if i < len(ac.all) && ac.all[i].URL.Path == path {
   149  		removed := ac.all[i]
   150  		ac.all = append(ac.all[:i], ac.all[i+1:]...)
   151  		if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
   152  			delete(ac.byAddr, removed.Address)
   153  		} else {
   154  			ac.byAddr[removed.Address] = ba
   155  		}
   156  	}
   157  }
   158  
   159  func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account {
   160  	for i := range slice {
   161  		if slice[i] == elem {
   162  			return append(slice[:i], slice[i+1:]...)
   163  		}
   164  	}
   165  	return slice
   166  }
   167  
   168  // find returns the cached account for address if there is a unique match.
   169  // The exact matching rules are explained by the documentation of accounts.Account.
   170  // Callers must hold ac.mu.
   171  func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
   172  	// Limit search to address candidates if possible.
   173  	matches := ac.all
   174  	if (a.Address != common.Address{}) {
   175  		matches = ac.byAddr[a.Address]
   176  	}
   177  	if a.URL.Path != "" {
   178  		// If only the basename is specified, complete the path.
   179  		if !strings.ContainsRune(a.URL.Path, filepath.Separator) {
   180  			a.URL.Path = filepath.Join(ac.keydir, a.URL.Path)
   181  		}
   182  		for i := range matches {
   183  			if matches[i].URL == a.URL {
   184  				return matches[i], nil
   185  			}
   186  		}
   187  		if (a.Address == common.Address{}) {
   188  			return accounts.Account{}, ErrNoMatch
   189  		}
   190  	}
   191  	switch len(matches) {
   192  	case 1:
   193  		return matches[0], nil
   194  	case 0:
   195  		return accounts.Account{}, ErrNoMatch
   196  	default:
   197  		err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))}
   198  		copy(err.Matches, matches)
   199  		sort.Sort(accountsByURL(err.Matches))
   200  		return accounts.Account{}, err
   201  	}
   202  }
   203  
   204  func (ac *accountCache) maybeReload() {
   205  	ac.mu.Lock()
   206  
   207  	if ac.watcher.running {
   208  		ac.mu.Unlock()
   209  		return // A watcher is running and will keep the cache up-to-date.
   210  	}
   211  	if ac.throttle == nil {
   212  		ac.throttle = time.NewTimer(0)
   213  	} else {
   214  		select {
   215  		case <-ac.throttle.C:
   216  		default:
   217  			ac.mu.Unlock()
   218  			return // The cache was reloaded recently.
   219  		}
   220  	}
   221  	// No watcher running, start it.
   222  	ac.watcher.start()
   223  	ac.throttle.Reset(minReloadInterval)
   224  	ac.mu.Unlock()
   225  	ac.scanAccounts()
   226  }
   227  
   228  func (ac *accountCache) close() {
   229  	ac.mu.Lock()
   230  	ac.watcher.close()
   231  	if ac.throttle != nil {
   232  		ac.throttle.Stop()
   233  	}
   234  	if ac.notify != nil {
   235  		close(ac.notify)
   236  		ac.notify = nil
   237  	}
   238  	ac.mu.Unlock()
   239  }
   240  
   241  // scanAccounts checks if any changes have occurred on the filesystem, and
   242  // updates the account cache accordingly
   243  func (ac *accountCache) scanAccounts() error {
   244  	// Scan the entire folder metadata for file changes
   245  	creates, deletes, updates, err := ac.fileC.scan(ac.keydir)
   246  	if err != nil {
   247  		log.Debug("Failed to reload keystore contents", "err", err)
   248  		return err
   249  	}
   250  	if creates.Cardinality() == 0 && deletes.Cardinality() == 0 && updates.Cardinality() == 0 {
   251  		return nil
   252  	}
   253  	// Create a helper method to scan the contents of the key files
   254  	var (
   255  		buf = new(bufio.Reader)
   256  		key struct {
   257  			Address string `json:"address"`
   258  		}
   259  	)
   260  	readAccount := func(path string) *accounts.Account {
   261  		fd, err := os.Open(path)
   262  		if err != nil {
   263  			log.Trace("Failed to open keystore file", "path", path, "err", err)
   264  			return nil
   265  		}
   266  		defer fd.Close()
   267  		buf.Reset(fd)
   268  		// Parse the address.
   269  		key.Address = ""
   270  		err = json.NewDecoder(buf).Decode(&key)
   271  		addr := common.HexToAddress(key.Address)
   272  		switch {
   273  		case err != nil:
   274  			log.Debug("Failed to decode keystore key", "path", path, "err", err)
   275  		case addr == common.Address{}:
   276  			log.Debug("Failed to decode keystore key", "path", path, "err", "missing or zero address")
   277  		default:
   278  			return &accounts.Account{
   279  				Address: addr,
   280  				URL:     accounts.URL{Scheme: KeyStoreScheme, Path: path},
   281  			}
   282  		}
   283  		return nil
   284  	}
   285  	// Process all the file diffs
   286  	start := time.Now()
   287  
   288  	for _, p := range creates.ToSlice() {
   289  		if a := readAccount(p.(string)); a != nil {
   290  			ac.add(*a)
   291  		}
   292  	}
   293  	for _, p := range deletes.ToSlice() {
   294  		ac.deleteByFile(p.(string))
   295  	}
   296  	for _, p := range updates.ToSlice() {
   297  		path := p.(string)
   298  		ac.deleteByFile(path)
   299  		if a := readAccount(path); a != nil {
   300  			ac.add(*a)
   301  		}
   302  	}
   303  	end := time.Now()
   304  
   305  	select {
   306  	case ac.notify <- struct{}{}:
   307  	default:
   308  	}
   309  	log.Trace("Handled keystore changes", "time", end.Sub(start))
   310  	return nil
   311  }