Blockchain tutorial ether Source Analysis Accounts account management analysis

Source: Internet
Author: User
Tags call back

Block chain tutorial ether Source Analysis Accounts account management analysis.

Data structure Analysis

The account management of ETH is defined in Accounts/manager.go, and its data structure is:

// 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 is all registered backend
Updaters is a subscription slot for all backend update subscribers
Updates is backend updated
Wallets is the cache of all registered backends wallets
Feeds are notifications of the arrival and departure of Wallets
Quit is the channel that exits the queue
Here's a look at the definition of backend. Backend is a wallet provider that contains a series of accounts. Backend can request a signed transaction.

 //backend is a "wallet provider" the May contain a batch of accounts they can//sign transactions with and up On request, does So.type backend interface {//wallets retrieves the list of wallets the backend is currently aware of.//// The returned wallets is not opened by default. For software HD wallets this//means that no base seeds is decrypted, and for hardware wallets that no actual//connectio N is established.////the resulting wallet list would be sorted alphabetically based on their 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 D uring//subsequent retrievals. Wallets () []wallet//Subscribe creates an async subscription to receive notifications when the//backend detects the Arriv Al or departure of a wallet. Subscribe (sink chan<-walletevent) event. Subscription}  

Backend is an interface. where wallets () returns the currently available wallets, sorted alphabetically.
Subscribe () is the method of creating an asynchronous subscription that receives a message and executes it through the channel when the wallet changes.
# #启动时账户管理加载
When you start using the Geth command, the code calls the Makefullnode method to produce a node. In this method, a Makeconfignode method is called.
In this method, the code parses the startup command we entered and places it in the Gethconfig. Next, node is called. The new method creates a node.
At node. In the new method, there is a Makeaccountmanager method, which is used to establish an account management system.

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),}...

In this method, Conf. The Accountconfig method parses the parameters we entered first and obtains the initial values of the KeyStore. Next through KeyStore. The Newkeystore method creates a 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}

In this method, KeyStore is initialized with the Init method.

  func (ks *keystore) init (Keydir string) {//Lock the mutex since the account caches might call back with Eventsk S.mu.lock () defer ks.mu.Unlock ()//Initialize The set of unlocked keys and the account cacheks.unlocked = Make (Map[common.a  ddress]*unlocked) Ks.cache, ks.changes = Newaccountcache (keydir)//Todo:in order for this finalizer to work, there must is No references//to KS. Addresscache doesn ' t keep a reference but unlocked keys do,//so the finalizer would not trigger until all timed unlocks ha ve Expired.runtime.SetFinalizer (KS, func (M *keystore) {m.cache.close ()})//Create The initial list of wallets from the CAC Heaccs: = 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}}}  

Here, the path to the file is first written to the KeyStore cache through the Newaccountcache method, and the data is written to the Ks.changes channel.
The account information is then written to the cache from the file using the accounts () method in the cache.
In accounts, step by step, you will find the Scanaccounts method. This method calculates the account information for Create,delete, and update, and writes the account information to the cache through the Readaccount method.
At this point, project management KeyStore and backend have been created and the account information is written to memory.
Next, you will pass the accounts. Newmanager Create an Account manager to manage the accounts.

Blockchain tutorial ether Source Analysis Accounts account management analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.