兄弟连区块链教程以太源码分析accounts账户管理分析

栏目: 编程工具 · 发布时间: 5年前

内容简介:兄弟连区块链教程以太源码分析accounts账户管理分析。数据结构分析ETH的账户管理定义在accounts/manager.go中,其数据结构为:

兄弟连区块链教程以太源码分析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 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对账户进行管理。


以上所述就是小编给大家介绍的《兄弟连区块链教程以太源码分析accounts账户管理分析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Linux二进制分析

Linux二进制分析

[美]瑞安 奥尼尔 / 棣琦 / 人民邮电出版社 / 2017-12-1 / CNY 59.00

二进制分析属于信息安全业界逆向工程中的一种技术,通过利用可执行的机器代码(二进制)来分析应用程序的控制结构和运行方式,有助于信息安全从业人员更好地分析各种漏洞、病毒以及恶意软件,从而找到相应的解决方案。 本书是一本剖析Linux ELF工作机制的图书,共分为9章,其内容涵盖了Linux环境和相关工具、ELF二进制格式、Linux进程追踪、ELF病毒技术、Linux二进制保护、Linux中的E......一起来看看 《Linux二进制分析》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码