accounts/mananger, internal/ethapi/api: Add new function AllAccounts on account manager to remove the duplication code on getting all wallets accounts

This commit is contained in:
Roc Yu 2019-01-11 07:16:08 +08:00
parent d70c4faf20
commit 8c890be561
2 changed files with 17 additions and 14 deletions

View file

@ -21,6 +21,7 @@ import (
"sort"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
)
@ -147,6 +148,20 @@ func (am *Manager) Wallet(url string) (Wallet, error) {
return nil, ErrUnknownWallet
}
// AllAccounts returns all account addresses of all wallets within the account manager
func (am *Manager) AllAccounts() []common.Address {
am.lock.RLock()
defer am.lock.RUnlock()
addresses := make([]common.Address, 0) // return [] instead of nil if empty
for _, wallet := range am.wallets {
for _, account := range wallet.Accounts() {
addresses = append(addresses, account.Address)
}
}
return addresses
}
// Find attempts to locate the wallet corresponding to a specific account. Since
// accounts can be dynamically added to and removed from wallets, this method has
// a linear runtime in the number of wallets.

View file

@ -190,13 +190,7 @@ func NewPublicAccountAPI(am *accounts.Manager) *PublicAccountAPI {
// Accounts returns the collection of accounts this node manages
func (s *PublicAccountAPI) Accounts() []common.Address {
addresses := make([]common.Address, 0) // return [] instead of nil if empty
for _, wallet := range s.am.Wallets() {
for _, account := range wallet.Accounts() {
addresses = append(addresses, account.Address)
}
}
return addresses
return s.am.AllAccounts()
}
// PrivateAccountAPI provides an API to access accounts managed by this node.
@ -219,13 +213,7 @@ func NewPrivateAccountAPI(b Backend, nonceLock *AddrLocker) *PrivateAccountAPI {
// ListAccounts will return a list of addresses for accounts this node manages.
func (s *PrivateAccountAPI) ListAccounts() []common.Address {
addresses := make([]common.Address, 0) // return [] instead of nil if empty
for _, wallet := range s.am.Wallets() {
for _, account := range wallet.Accounts() {
addresses = append(addresses, account.Address)
}
}
return addresses
return s.am.AllAccounts()
}
// rawWallet is a JSON representation of an accounts.Wallet interface, with its