Skip to content

Commit

Permalink
Update wallet to use ForEach- style functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed May 21, 2015
1 parent fe0f609 commit fbf744b
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 153 deletions.
30 changes: 17 additions & 13 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1636,17 +1636,13 @@ func GetAddressesByAccount(w *wallet.Wallet, chainSvr *chain.Client, icmd interf
return nil, err
}

addrs, err := w.Manager.AllAccountAddresses(account)
if err != nil {
return nil, err
}

addrStrs := make([]string, len(addrs))
for i, addr := range addrs {
addrStrs[i] = addr.Address().EncodeAddress()
}

return addrStrs, nil
var addrStrs []string
err = w.Manager.ForEachAccountAddress(account,
func(maddr waddrmgr.ManagedAddress) error {
addrStrs = append(addrStrs, maddr.Address().EncodeAddress())
return nil
})
return addrStrs, err
}

// GetBalance handles a getbalance request by returning the balance for an
Expand Down Expand Up @@ -2263,7 +2259,11 @@ func ListAccounts(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (i
cmd := icmd.(*btcjson.ListAccountsCmd)

accountBalances := map[string]float64{}
accounts, err := w.Manager.AllAccounts()
var accounts []uint32
err := w.Manager.ForEachAccount(func(account uint32) error {
accounts = append(accounts, account)
return nil
})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2302,7 +2302,11 @@ func ListLockUnspent(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{})
func ListReceivedByAccount(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (interface{}, error) {
cmd := icmd.(*btcjson.ListReceivedByAccountCmd)

accounts, err := w.Manager.AllAccounts()
var accounts []uint32
err := w.Manager.ForEachAccount(func(account uint32) error {
accounts = append(accounts, account)
return nil
})
if err != nil {
return nil, err
}
Expand Down
50 changes: 18 additions & 32 deletions waddrmgr/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,23 +577,18 @@ func serializeBIP0044AccountRow(encryptedPubKey,
return rawData
}

// fetchAllAccounts loads information about all accounts from the database.
// The returned value is a slice of account numbers which can be used to load
// the respective account rows.
// TODO(tuxcanfly): Switch over to an iterator to support the maximum of 2^31-2 accounts
func fetchAllAccounts(tx walletdb.Tx) ([]uint32, error) {
// forEachAccount calls the given function with each account stored in
// the manager, breaking early on error.
func forEachAccount(tx walletdb.Tx, fn func(account uint32) error) error {
bucket := tx.RootBucket().Bucket(acctBucketName)

var accounts []uint32
err := bucket.ForEach(func(k, v []byte) error {
return bucket.ForEach(func(k, v []byte) error {
// Skip buckets.
if v == nil {
return nil
}
accounts = append(accounts, binary.LittleEndian.Uint32(k))
return nil
return fn(binary.LittleEndian.Uint32(k))
})
return accounts, err
}

// fetchLastAccount retreives the last account from the database.
Expand Down Expand Up @@ -1187,19 +1182,17 @@ func fetchAddrAccount(tx walletdb.Tx, addressID []byte) (uint32, error) {
return binary.LittleEndian.Uint32(val), nil
}

// fetchAccountAddresses loads information about addresses of an account from the database.
// The returned value is a slice address rows for each specific address type.
// The caller should use type assertions to ascertain the types.
func fetchAccountAddresses(tx walletdb.Tx, account uint32) ([]interface{}, error) {
// forEachAccountAddress calls the given function with each address of
// the given account stored in the manager, breaking early on error.
func forEachAccountAddress(tx walletdb.Tx, account uint32, fn func(rowInterface interface{}) error) error {
bucket := tx.RootBucket().Bucket(addrAcctIdxBucketName).
Bucket(uint32ToBytes(account))
// if index bucket is missing the account, there hasn't been any address
// entries yet
if bucket == nil {
return nil, nil
return nil
}

var addrs []interface{}
err := bucket.ForEach(func(k, v []byte) error {
// Skip buckets.
if v == nil {
Expand All @@ -1216,24 +1209,19 @@ func fetchAccountAddresses(tx walletdb.Tx, account uint32) ([]interface{}, error
return err
}

addrs = append(addrs, addrRow)
return nil
return fn(addrRow)
})
if err != nil {
return nil, maybeConvertDbError(err)
return maybeConvertDbError(err)
}

return addrs, nil
return nil
}

// fetchAllAddresses loads information about all addresses from the database.
// The returned value is a slice of address rows for each specific address type.
// The caller should use type assertions to ascertain the types.
// TODO(tuxcanfly): Switch over to an iterator to support the maximum of 2^62 - 2^32 - 2^31 + 2 addrs
func fetchAllAddresses(tx walletdb.Tx) ([]interface{}, error) {
// forEachActiveAddress calls the given function with each active address
// stored in the manager, breaking early on error.
func forEachActiveAddress(tx walletdb.Tx, fn func(rowInterface interface{}) error) error {
bucket := tx.RootBucket().Bucket(addrBucketName)

var addrs []interface{}
err := bucket.ForEach(func(k, v []byte) error {
// Skip buckets.
if v == nil {
Expand All @@ -1253,14 +1241,12 @@ func fetchAllAddresses(tx walletdb.Tx) ([]interface{}, error) {
return err
}

addrs = append(addrs, addrRow)
return nil
return fn(addrRow)
})
if err != nil {
return nil, maybeConvertDbError(err)
return maybeConvertDbError(err)
}

return addrs, nil
return nil
}

// deletePrivateKeys removes all private key material from the database.
Expand Down
9 changes: 9 additions & 0 deletions waddrmgr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ const (
// ErrWrongNet indicates that the private key to be imported is not for the
// the same network the account manager is configured for.
ErrWrongNet

// ErrCallBackBreak is used to break from a callback function passed
// down to the manager.
ErrCallBackBreak
)

// Map of ErrorCode values back to their constant names for pretty printing.
Expand All @@ -154,6 +158,7 @@ var errorCodeStrings = map[ErrorCode]string{
ErrTooManyAddresses: "ErrTooManyAddresses",
ErrWrongPassphrase: "ErrWrongPassphrase",
ErrWrongNet: "ErrWrongNet",
ErrCallBackBreak: "ErrCallBackBreak",
}

// String returns the ErrorCode as a human-readable name.
Expand Down Expand Up @@ -195,3 +200,7 @@ func (e ManagerError) Error() string {
func managerError(c ErrorCode, desc string, err error) ManagerError {
return ManagerError{ErrorCode: c, Description: desc, Err: err}
}

// Break is a global err used to signal a break from the callback
// function by returning an error with the code ErrCallBackBreak
var Break = managerError(ErrCallBackBreak, "callback break", nil)
95 changes: 36 additions & 59 deletions waddrmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1821,22 +1821,14 @@ func (m *Manager) AccountName(account uint32) (string, error) {
return acctName, nil
}

// AllAccounts returns a slice of all the accounts stored in the manager.
func (m *Manager) AllAccounts() ([]uint32, error) {
// ForEachAccount calls the given function with each account stored in the
// manager, breaking early on error.
func (m *Manager) ForEachAccount(fn func(account uint32) error) error {
m.mtx.Lock()
defer m.mtx.Unlock()

var accounts []uint32
err := m.namespace.View(func(tx walletdb.Tx) error {
var err error
accounts, err = fetchAllAccounts(tx)
return err
return m.namespace.View(func(tx walletdb.Tx) error {
return forEachAccount(tx, fn)
})
if err != nil {
return nil, err
}

return accounts, nil
}

// LastAccount returns the last account stored in the manager.
Expand All @@ -1853,73 +1845,58 @@ func (m *Manager) LastAccount() (uint32, error) {
return account, err
}

// AllAccountAddresses returns a slice of addresses of an account stored in the manager.
func (m *Manager) AllAccountAddresses(account uint32) ([]ManagedAddress, error) {
// ForEachAccountAddress calls the given function with each address of
// the given account stored in the manager, breaking early on error.
func (m *Manager) ForEachAccountAddress(account uint32, fn func(maddr ManagedAddress) error) error {
m.mtx.Lock()
defer m.mtx.Unlock()

// Load the raw address information from the database.
var rowInterfaces []interface{}
err := m.namespace.View(func(tx walletdb.Tx) error {
var err error
rowInterfaces, err = fetchAccountAddresses(tx, account)
return err
})
if err != nil {
return nil, err
}

addrs := make([]ManagedAddress, 0, len(rowInterfaces))
for _, rowInterface := range rowInterfaces {
// Create a new managed address for the specific type of address
// based on type.
addrFn := func(rowInterface interface{}) error {
managedAddr, err := m.rowInterfaceToManaged(rowInterface)
if err != nil {
return nil, err
return err
}

addrs = append(addrs, managedAddr)
return fn(managedAddr)
}

return addrs, nil
err := m.namespace.View(func(tx walletdb.Tx) error {
return forEachAccountAddress(tx, account, addrFn)
})
if err != nil {
return maybeConvertDbError(err)
}
return nil
}

// ActiveAccountAddresses returns a slice of active addresses of an account
// stored in the manager.
// ForEachActiveAccountAddress calls the given function with each active
// address of the given account stored in the manager, breaking early on
// error.
// TODO(tuxcanfly): actually return only active addresses
func (m *Manager) ActiveAccountAddresses(account uint32) ([]ManagedAddress, error) {
return m.AllAccountAddresses(account)
func (m *Manager) ForEachActiveAccountAddress(account uint32, fn func(maddr ManagedAddress) error) error {
return m.ForEachAccountAddress(account, fn)
}

// AllActiveAddresses returns a slice of all addresses stored in the manager.
func (m *Manager) AllActiveAddresses() ([]btcutil.Address, error) {
// ForEachActiveAddress calls the given function with each active address
// stored in the manager, breaking early on error.
func (m *Manager) ForEachActiveAddress(fn func(addr btcutil.Address) error) error {
m.mtx.Lock()
defer m.mtx.Unlock()

// Load the raw address information from the database.
var rowInterfaces []interface{}
err := m.namespace.View(func(tx walletdb.Tx) error {
var err error
rowInterfaces, err = fetchAllAddresses(tx)
return err
})
if err != nil {
return nil, maybeConvertDbError(err)
}

addrs := make([]btcutil.Address, 0, len(rowInterfaces))
for _, rowInterface := range rowInterfaces {
// Create a new managed address for the specific type of address
// based on type.
addrFn := func(rowInterface interface{}) error {
managedAddr, err := m.rowInterfaceToManaged(rowInterface)
if err != nil {
return nil, err
return err
}

addrs = append(addrs, managedAddr.Address())
return fn(managedAddr.Address())
}

return addrs, nil
err := m.namespace.View(func(tx walletdb.Tx) error {
return forEachActiveAddress(tx, addrFn)
})
if err != nil {
return maybeConvertDbError(err)
}
return nil
}

// selectCryptoKey selects the appropriate crypto key based on the key type. An
Expand Down
42 changes: 26 additions & 16 deletions waddrmgr/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1267,48 +1267,58 @@ func testRenameAccount(tc *testContext) bool {
return true
}

// testAllAccounts tests the retrieve all accounts func of the address manager works
// as expected.
func testAllAccounts(tc *testContext) bool {
// testForEachAccount tests the retrieve all accounts func of the address
// manager works as expected.
func testForEachAccount(tc *testContext) bool {
prefix := testNamePrefix(tc) + " testForEachAccount"
expectedAccounts := []uint32{0, 1}
if !tc.create {
// Existing wallet manager will have 3 accounts
expectedAccounts = append(expectedAccounts, 2)
}
// Imported account
expectedAccounts = append(expectedAccounts, waddrmgr.ImportedAddrAccount)
accounts, err := tc.manager.AllAccounts()
var accounts []uint32
err := tc.manager.ForEachAccount(func(account uint32) error {
accounts = append(accounts, account)
return nil
})
if err != nil {
tc.t.Errorf("AllAccounts: unexpected error: %v", err)
tc.t.Errorf("%s: unexpected error: %v", prefix, err)
return false
}
if len(accounts) != len(expectedAccounts) {
tc.t.Errorf("AllAccounts: unexpected number of accounts - got "+
"%d, want %d", len(accounts),
tc.t.Errorf("%s: unexpected number of accounts - got "+
"%d, want %d", prefix, len(accounts),
len(expectedAccounts))
return false
}
for i, account := range accounts {
if expectedAccounts[i] != account {
tc.t.Errorf("AllAccounts %s: "+
tc.t.Errorf("%s #%d: "+
"account mismatch -- got %d, "+
"want %d", i, account, expectedAccounts[i])
"want %d", prefix, i, account, expectedAccounts[i])
}
}
return true
}

// testAllAccountAddresses tests the account addresses returned by the manager
// API.
func testAllAccountAddresses(tc *testContext) bool {
prefix := testNamePrefix(tc) + " testAllAccountAddresses"
// testForEachAccountAddress tests that iterating through the given
// account addresses using the manager API works as expected.
func testForEachAccountAddress(tc *testContext) bool {
prefix := testNamePrefix(tc) + " testForEachAccountAddress"
// Make a map of expected addresses
expectedAddrMap := make(map[string]*expectedAddr, len(expectedAddrs))
for i := 0; i < len(expectedAddrs); i++ {
expectedAddrMap[expectedAddrs[i].address] = &expectedAddrs[i]
}

addrs, err := tc.manager.AllAccountAddresses(tc.account)
var addrs []waddrmgr.ManagedAddress
err := tc.manager.ForEachAccountAddress(tc.account,
func(maddr waddrmgr.ManagedAddress) error {
addrs = append(addrs, maddr)
return nil
})
if err != nil {
tc.t.Errorf("%s: unexpected error: %v", prefix, err)
return false
Expand Down Expand Up @@ -1349,8 +1359,8 @@ func testManagerAPI(tc *testContext) {
tc.account = 0
testNewAccount(tc)
testLookupAccount(tc)
testAllAccounts(tc)
testAllAccountAddresses(tc)
testForEachAccount(tc)
testForEachAccountAddress(tc)

// Rename account 1 "acct-create"
tc.account = 1
Expand Down
Loading

0 comments on commit fbf744b

Please sign in to comment.