以太坊原始碼分析(6)accounts賬戶管理分析

尹成發表於2018-05-13
##資料結構分析
以太坊的賬戶管理定義在accounts/manager.go中,其資料結構為:
```
// Manager is an overarching account manager that can communicate with various
// backends for signing transactions.
type Manager struct {
backends map[reflect.Type][]Backend // Index of backends currently registered
updaters []event.Subscription // Wallet update subscriptions for all backends
updates chan WalletEvent // Subscription sink for backend wallet changes
wallets []Wallet // Cache of all wallets from all registered backends
feed event.Feed // Wallet feed notifying of arrivals/departures
quit chan chan error
lock sync.RWMutex
}
```
backends是所有已註冊的Backend
updaters是所有的Backend的更新訂閱器
updates是Backend更新的訂閱槽
wallets是所有已經註冊的Backends的錢包的快取
feed是錢包到達和離開的通知
quit是退出佇列的通道
這裡主要來看一下Backend的定義。Backend是一個錢包的提供器,包含一系列的賬號。Backend可以請求籤名交易。
```
// Backend is a "wallet provider" that may contain a batch of accounts they can
// sign transactions with and upon request, do so.
type Backend interface {
// Wallets retrieves the list of wallets the backend is currently aware of.
//
// The returned wallets are not opened by default. For software HD wallets this
// means that no base seeds are decrypted, and for hardware wallets that no actual
// connection is established.
//
// The resulting wallet list will be sorted alphabetically based on its internal
// URL assigned by the backend. Since wallets (especially hardware) may come and
// go, the same wallet might appear at a different positions in the list during
// subsequent retrievals.
Wallets() []Wallet
// Subscribe creates an async subscription to receive notifications when the
// backend detects the arrival or departure of a wallet.
Subscribe(sink chan<- WalletEvent) event.Subscription
}
```
Backend是一個介面。其中,Wallets()返回當前可用的錢包,按字母順序排序。
Subscribe()是建立非同步訂閱的方法,當錢包發生變動時會通過通道接收到訊息並執行。
##啟動時賬戶管理載入
在使用geth命令啟動中,程式碼會呼叫makeFullNode方法產生一個節點。在這個方法中,會呼叫一個makeConfigNode方法。
在這個方法中,程式碼會將我們輸入的啟動命令進行解析,並放置在gethConfig中。接下來會呼叫node.New方法建立一個節點。
在node.New方法中,有一個makeAccountManager方法,這個方法是用來建立賬戶管理系統的。
```
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
scryptN, scryptP, keydir, err := conf.AccountConfig()
var ephemeral string
if keydir == "" {
// There is no datadir.
keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
ephemeral = keydir
}
if err != nil {
return nil, "", err
}
if err := os.MkdirAll(keydir, 0700); err != nil {
return nil, "", err
}
// Assemble the account manager and supported backends
backends := []accounts.Backend{
keystore.NewKeyStore(keydir, scryptN, scryptP),
}
...
```
在這個方法中,conf.AccountConfig方法會先將我們輸入的引數進行解析,並獲取keystore的初始值。接下來通過keystore.NewKeyStore方法建立一個Backend。
```
func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore {
keydir, _ = filepath.Abs(keydir)
ks := &KeyStore{storage: &keyStorePassphrase{keydir, scryptN, scryptP}}
ks.init(keydir)
return ks
}
```
在這個方法中,keystore會通過init方法進行初始化。
```
func (ks *KeyStore) init(keydir string) {
// Lock the mutex since the account cache might call back with events
ks.mu.Lock()
defer ks.mu.Unlock()
// Initialize the set of unlocked keys and the account cache
ks.unlocked = make(map[common.Address]*unlocked)
ks.cache, ks.changes = newAccountCache(keydir)
// TODO: In order for this finalizer to work, there must be no references
// to ks. addressCache doesn't keep a reference but unlocked keys do,
// so the finalizer will not trigger until all timed unlocks have expired.
runtime.SetFinalizer(ks, func(m *KeyStore) {
m.cache.close()
})
// Create the initial list of wallets from the cache
accs := ks.cache.accounts()
ks.wallets = make([]accounts.Wallet, len(accs))
for i := 0; i < len(accs); i++ {
ks.wallets[i] = &keystoreWallet{account: accs[i], keystore: ks}
}
}
```
這裡,首先會通過newAccountCache方法將檔案的路徑寫入到keystore的快取中,並在ks.changes通道中寫入資料。
然後會通過快取中的accounts()方法從檔案中將賬戶資訊寫入到快取中。
在accounts中,一步步跟進去,會找到scanAccounts方法。這個方法會計算create,delete,和update的賬戶資訊,並通過readAccount方法將賬戶資訊寫入到快取中。
至此,專案管理的keystore和backend已經建立好,並將賬戶資訊寫入到記憶體中。
接下來,會通過accounts.NewManager建立一個account manager對賬戶進行管理。






網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN


相關文章