数据结构分析

以太坊的账户管理定义在accounts/manager.go中,其数据结构为:

  1. // Manager is an overarching account manager that can communicate with various
  2. // backends for signing transactions.
  3. type Manager struct {
  4. backends map[reflect.Type][]Backend // Index of backends currently registered
  5. updaters []event.Subscription // Wallet update subscriptions for all backends
  6. updates chan WalletEvent // Subscription sink for backend wallet changes
  7. wallets []Wallet // Cache of all wallets from all registered backends
  8. feed event.Feed // Wallet feed notifying of arrivals/departures
  9. quit chan chan error
  10. lock sync.RWMutex
  11. }

backends是所有已注册的Backend
updaters是所有的Backend的更新订阅器
updates是Backend更新的订阅槽
wallets是所有已经注册的Backends的钱包的缓存
feed是钱包到达和离开的通知
quit是退出队列的通道
这里主要来看一下Backend的定义。Backend是一个钱包的提供器,包含一系列的账号。Backend可以请求签名交易。

  1. // Backend is a "wallet provider" that may contain a batch of accounts they can
  2. // sign transactions with and upon request, do so.
  3. type Backend interface {
  4. // Wallets retrieves the list of wallets the backend is currently aware of.
  5. //
  6. // The returned wallets are not opened by default. For software HD wallets this
  7. // means that no base seeds are decrypted, and for hardware wallets that no actual
  8. // connection is established.
  9. //
  10. // The resulting wallet list will be sorted alphabetically based on its internal
  11. // URL assigned by the backend. Since wallets (especially hardware) may come and
  12. // go, the same wallet might appear at a different positions in the list during
  13. // subsequent retrievals.
  14. Wallets() []Wallet
  15. // Subscribe creates an async subscription to receive notifications when the
  16. // backend detects the arrival or departure of a wallet.
  17. Subscribe(sink chan<- WalletEvent) event.Subscription
  18. }

Backend是一个接口。其中,Wallets()返回当前可用的钱包,按字母顺序排序。
Subscribe()是创建异步订阅的方法,当钱包发生变动时会通过通道接收到消息并执行。

启动时账户管理加载

在使用geth命令启动中,代码会调用makeFullNode方法产生一个节点。在这个方法中,会调用一个makeConfigNode方法。
在这个方法中,代码会将我们输入的启动命令进行解析,并放置在gethConfig中。接下来会调用node.New方法创建一个节点。
在node.New方法中,有一个makeAccountManager方法,这个方法是用来建立账户管理系统的。

  1. func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
  2. scryptN, scryptP, keydir, err := conf.AccountConfig()
  3. var ephemeral string
  4. if keydir == "" {
  5. // There is no datadir.
  6. keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
  7. ephemeral = keydir
  8. }
  9. if err != nil {
  10. return nil, "", err
  11. }
  12. if err := os.MkdirAll(keydir, 0700); err != nil {
  13. return nil, "", err
  14. }
  15. // Assemble the account manager and supported backends
  16. backends := []accounts.Backend{
  17. keystore.NewKeyStore(keydir, scryptN, scryptP),
  18. }
  19. ...

在这个方法中,conf.AccountConfig方法会先将我们输入的参数进行解析,并获取keystore的初始值。接下来通过keystore.NewKeyStore方法创建一个Backend。

  1. func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore {
  2. keydir, _ = filepath.Abs(keydir)
  3. ks := &KeyStore{storage: &keyStorePassphrase{keydir, scryptN, scryptP}}
  4. ks.init(keydir)
  5. return ks
  6. }

在这个方法中,keystore会通过init方法进行初始化。

  1. func (ks *KeyStore) init(keydir string) {
  2. // Lock the mutex since the account cache might call back with events
  3. ks.mu.Lock()
  4. defer ks.mu.Unlock()
  5. // Initialize the set of unlocked keys and the account cache
  6. ks.unlocked = make(map[common.Address]*unlocked)
  7. ks.cache, ks.changes = newAccountCache(keydir)
  8. // TODO: In order for this finalizer to work, there must be no references
  9. // to ks. addressCache doesn't keep a reference but unlocked keys do,
  10. // so the finalizer will not trigger until all timed unlocks have expired.
  11. runtime.SetFinalizer(ks, func(m *KeyStore) {
  12. m.cache.close()
  13. })
  14. // Create the initial list of wallets from the cache
  15. accs := ks.cache.accounts()
  16. ks.wallets = make([]accounts.Wallet, len(accs))
  17. for i := 0; i < len(accs); i++ {
  18. ks.wallets[i] = &keystoreWallet{account: accs[i], keystore: ks}
  19. }
  20. }

这里,首先会通过newAccountCache方法将文件的路径写入到keystore的缓存中,并在ks.changes通道中写入数据。
然后会通过缓存中的accounts()方法从文件中将账户信息写入到缓存中。
在accounts中,一步步跟进去,会找到scanAccounts方法。这个方法会计算create,delete,和update的账户信息,并通过readAccount方法将账户信息写入到缓存中。
至此,项目管理的keystore和backend已经创建好,并将账户信息写入到内存中。
接下来,会通过accounts.NewManager创建一个account manager对账户进行管理。