標籤:mutex spec keystore for soft ssi nal open ora
區塊鏈教程以太源碼分析accounts賬戶管理分析。
資料結構分析
ETH的賬戶管理定義在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 registeredupdaters []event.Subscription // Wallet update subscriptions for all backendsupdates chan WalletEvent // Subscription sink for backend wallet changeswallets []Wallet // Cache of all wallets from all registered backendsfeed event.Feed // Wallet feed notifying of arrivals/departuresquit chan chan errorlock 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 stringif 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 backendsbackends := []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 eventsks.mu.Lock()defer ks.mu.Unlock()// Initialize the set of unlocked keys and the account cacheks.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 cacheaccs := 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對賬戶進行管理。
區塊鏈教程以太源碼分析accounts賬戶管理分析