Skip to content

Commit

Permalink
consolidate: merge internal/external branches
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 28, 2022
1 parent de408d4 commit 0410b7c
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 321 deletions.
68 changes: 16 additions & 52 deletions chain/block_filterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,18 @@ type BlockFilterer struct {
// Params specifies the chain params of the current network.
Params *chaincfg.Params

// ExReverseFilter holds a reverse index mapping an external address to
// ReverseFilter holds a reverse index mapping an external address to
// the scoped index from which it was derived.
ExReverseFilter map[string]waddrmgr.ScopedIndex

// InReverseFilter holds a reverse index mapping an internal address to
// the scoped index from which it was derived.
InReverseFilter map[string]waddrmgr.ScopedIndex
ReverseFilter map[string]waddrmgr.ScopedIndex

// WathcedOutPoints is a global set of outpoints being tracked by the
// wallet. This allows the block filterer to check for spends from an
// outpoint we own.
WatchedOutPoints map[wire.OutPoint]btcutil.Address

// FoundExternal is a two-layer map recording the scope and index of
// FoundAddresses is a two-layer map recording the scope and index of
// external addresses found in a single block.
FoundExternal map[waddrmgr.KeyScope]map[uint32]struct{}

// FoundInternal is a two-layer map recording the scope and index of
// internal addresses found in a single block.
FoundInternal map[waddrmgr.KeyScope]map[uint32]struct{}
FoundAddresses map[waddrmgr.ScopedIndex]struct{}

// FoundOutPoints is a set of outpoints found in a single block whose
// address belongs to the wallet.
Expand All @@ -71,31 +63,20 @@ func NewBlockFilterer(params *chaincfg.Params,

// Construct a reverse index by address string for the requested
// external addresses.
nExAddrs := len(req.ExternalAddrs)
exReverseFilter := make(map[string]waddrmgr.ScopedIndex, nExAddrs)
for scopedIndex, addr := range req.ExternalAddrs {
exReverseFilter[addr.EncodeAddress()] = scopedIndex
nAddrs := len(req.Addresses)
reverseFilter := make(map[string]waddrmgr.ScopedIndex, nAddrs)
for scopedIndex, addr := range req.Addresses {
reverseFilter[addr.EncodeAddress()] = scopedIndex
}

// Construct a reverse index by address string for the requested
// internal addresses.
nInAddrs := len(req.InternalAddrs)
inReverseFilter := make(map[string]waddrmgr.ScopedIndex, nInAddrs)
for scopedIndex, addr := range req.InternalAddrs {
inReverseFilter[addr.EncodeAddress()] = scopedIndex
}

foundExternal := make(map[waddrmgr.KeyScope]map[uint32]struct{})
foundInternal := make(map[waddrmgr.KeyScope]map[uint32]struct{})
foundAddresses := make(map[waddrmgr.ScopedIndex]struct{})
foundOutPoints := make(map[wire.OutPoint]btcutil.Address)

return &BlockFilterer{
Params: params,
ExReverseFilter: exReverseFilter,
InReverseFilter: inReverseFilter,
ReverseFilter: reverseFilter,
WatchedOutPoints: req.WatchedOutPoints,
FoundExternal: foundExternal,
FoundInternal: foundInternal,
FoundAddresses: foundAddresses,
FoundOutPoints: foundOutPoints,
}
}
Expand Down Expand Up @@ -183,35 +164,18 @@ func (bf *BlockFilterer) FilterOutputAddrs(addrs []btcutil.Address) bool {
var isRelevant bool
for _, addr := range addrs {
addrStr := addr.EncodeAddress()
if scopedIndex, ok := bf.ExReverseFilter[addrStr]; ok {
bf.foundExternal(scopedIndex)
isRelevant = true
}
if scopedIndex, ok := bf.InReverseFilter[addrStr]; ok {
bf.foundInternal(scopedIndex)
if scopedIndex, ok := bf.ReverseFilter[addrStr]; ok {
bf.found(scopedIndex)
isRelevant = true
}
}

return isRelevant
}

// foundExternal marks the scoped index as found within the block filterer's
// found marks the scoped index as found within the block filterer's
// FoundExternal map. If this the first index found for a particular scope, the
// scope's second layer map will be initialized before marking the index.
func (bf *BlockFilterer) foundExternal(scopedIndex waddrmgr.ScopedIndex) {
if _, ok := bf.FoundExternal[scopedIndex.Scope]; !ok {
bf.FoundExternal[scopedIndex.Scope] = make(map[uint32]struct{})
}
bf.FoundExternal[scopedIndex.Scope][scopedIndex.Index] = struct{}{}
}

// foundInternal marks the scoped index as found within the block filterer's
// FoundInternal map. If this the first index found for a particular scope, the
// scope's second layer map will be initialized before marking the index.
func (bf *BlockFilterer) foundInternal(scopedIndex waddrmgr.ScopedIndex) {
if _, ok := bf.FoundInternal[scopedIndex.Scope]; !ok {
bf.FoundInternal[scopedIndex.Scope] = make(map[uint32]struct{})
}
bf.FoundInternal[scopedIndex.Scope][scopedIndex.Index] = struct{}{}
func (bf *BlockFilterer) found(scopedIndex waddrmgr.ScopedIndex) {
bf.FoundAddresses[scopedIndex] = struct{}{}
}
14 changes: 6 additions & 8 deletions chain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ type (
// is also included to monitor for spends.
FilterBlocksRequest struct {
Blocks []wtxmgr.BlockMeta
ExternalAddrs map[waddrmgr.ScopedIndex]btcutil.Address
InternalAddrs map[waddrmgr.ScopedIndex]btcutil.Address
Addresses map[waddrmgr.ScopedIndex]btcutil.Address
WatchedOutPoints map[wire.OutPoint]btcutil.Address
}

Expand All @@ -88,12 +87,11 @@ type (
// caller can reinitiate a request for the subsequent block after
// updating the addresses of interest.
FilterBlocksResponse struct {
BatchIndex uint32
BlockMeta wtxmgr.BlockMeta
FoundExternalAddrs map[waddrmgr.KeyScope]map[uint32]struct{}
FoundInternalAddrs map[waddrmgr.KeyScope]map[uint32]struct{}
FoundOutPoints map[wire.OutPoint]btcutil.Address
RelevantTxns []*wire.MsgTx
BatchIndex uint32
BlockMeta wtxmgr.BlockMeta
FoundAddresses map[waddrmgr.ScopedIndex]struct{}
FoundOutPoints map[wire.OutPoint]btcutil.Address
RelevantTxns []*wire.MsgTx
}

// BlockDisconnected is a notifcation that the block described by the
Expand Down
14 changes: 2 additions & 12 deletions chain/neutrino.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,12 @@ func buildFilterBlocksWatchList(req *FilterBlocksRequest) ([][]byte, error) {
// Construct a watch list containing the script addresses of all
// internal and external addresses that were requested, in addition to
// the set of outpoints currently being watched.
watchListSize := len(req.ExternalAddrs) +
len(req.InternalAddrs) +
watchListSize := len(req.Addresses) +
len(req.WatchedOutPoints)

watchList := make([][]byte, 0, watchListSize)

for _, addr := range req.ExternalAddrs {
p2shAddr, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}

watchList = append(watchList, p2shAddr)
}

for _, addr := range req.InternalAddrs {
for _, addr := range req.Addresses {
p2shAddr, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, err
Expand Down
11 changes: 5 additions & 6 deletions chain/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,11 @@ func (c *RPCClient) FilterBlocks(
// `BatchIndex` is returned so that the caller can compute the
// *next* block from which to begin again.
resp := &FilterBlocksResponse{
BatchIndex: uint32(i),
BlockMeta: blk,
FoundExternalAddrs: blockFilterer.FoundExternal,
FoundInternalAddrs: blockFilterer.FoundInternal,
FoundOutPoints: blockFilterer.FoundOutPoints,
RelevantTxns: blockFilterer.RelevantTxns,
BatchIndex: uint32(i),
BlockMeta: blk,
FoundAddresses: blockFilterer.FoundAddresses,
FoundOutPoints: blockFilterer.FoundOutPoints,
RelevantTxns: blockFilterer.RelevantTxns,
}

return resp, nil
Expand Down
9 changes: 2 additions & 7 deletions waddrmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,8 @@ type accountInfo struct {

// The external branch is used for all addresses which are intended for
// external use.
nextExternalIndex uint32
lastExternalAddr ManagedAddress

// The internal branch is used for all adddresses which are only
// intended for internal wallet use such as change addresses.
nextInternalIndex uint32
lastInternalAddr ManagedAddress
nextIndex [2]uint32
lastAddr [2]ManagedAddress

// addrSchema serves as a way for an account to override its
// corresponding address schema with a custom one.
Expand Down
40 changes: 20 additions & 20 deletions waddrmgr/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ func testExternalAddresses(tc *testContext) bool {
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
var err error
addrs, err = tc.manager.NextExternalAddresses(
ns, tc.internalAccount, 5,
addrs, err = tc.manager.NextAddresses(
ns, tc.internalAccount, ExternalBranch, 5,
)
return err
})
Expand Down Expand Up @@ -371,8 +371,8 @@ func testExternalAddresses(tc *testContext) bool {
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
lastAddr, err = tc.manager.LastExternalAddress(
ns, tc.internalAccount,
lastAddr, err = tc.manager.LastAddress(
ns, tc.internalAccount, ExternalBranch,
)
return err
})
Expand Down Expand Up @@ -478,8 +478,8 @@ func testInternalAddresses(tc *testContext) bool {
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
var err error
addrs, err = tc.manager.NextInternalAddresses(
ns, tc.internalAccount, 5,
addrs, err = tc.manager.NextAddresses(
ns, tc.internalAccount, InternalBranch, 5,
)
return err
})
Expand Down Expand Up @@ -515,8 +515,8 @@ func testInternalAddresses(tc *testContext) bool {
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
lastAddr, err = tc.manager.LastInternalAddress(
ns, tc.internalAccount,
lastAddr, err = tc.manager.LastAddress(
ns, tc.internalAccount, InternalBranch,
)
return err
})
Expand Down Expand Up @@ -2032,8 +2032,8 @@ func TestScopedKeyManagerManagement(t *testing.T) {
t.Fatalf("unable to fetch scope %v: %v", scope, err)
}

externalAddr, err := sMgr.NextExternalAddresses(
ns, DefaultAccountNum, 1,
externalAddr, err := sMgr.NextAddresses(
ns, DefaultAccountNum, ExternalBranch, 1,
)
if err != nil {
t.Fatalf("unable to derive external addr: %v", err)
Expand All @@ -2047,8 +2047,8 @@ func TestScopedKeyManagerManagement(t *testing.T) {
ScopeAddrMap[scope].ExternalAddrType)
}

internalAddr, err := sMgr.NextInternalAddresses(
ns, DefaultAccountNum, 1,
internalAddr, err := sMgr.NextAddresses(
ns, DefaultAccountNum, InternalBranch, 1,
)
if err != nil {
t.Fatalf("unable to derive internal addr: %v", err)
Expand Down Expand Up @@ -2106,15 +2106,15 @@ func TestScopedKeyManagerManagement(t *testing.T) {

// We'll now create a new external address to ensure we
// retrieve the proper type.
externalAddr, err = scopedMgr.NextExternalAddresses(
ns, DefaultAccountNum, 1,
externalAddr, err = scopedMgr.NextAddresses(
ns, DefaultAccountNum, ExternalBranch, 1,
)
if err != nil {
t.Fatalf("unable to derive external addr: %v", err)
}

internalAddr, err = scopedMgr.NextInternalAddresses(
ns, DefaultAccountNum, 1,
internalAddr, err = scopedMgr.NextAddresses(
ns, DefaultAccountNum, InternalBranch, 1,
)
if err != nil {
t.Fatalf("unable to derive internal addr: %v", err)
Expand Down Expand Up @@ -2177,8 +2177,8 @@ func TestScopedKeyManagerManagement(t *testing.T) {
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)

lastAddr, err = scopedMgr.LastExternalAddress(
ns, DefaultAccountNum,
lastAddr, err = scopedMgr.LastAddress(
ns, DefaultAccountNum, ExternalBranch,
)
if err != nil {
return err
Expand Down Expand Up @@ -2384,8 +2384,8 @@ func testNewRawAccount(t *testing.T, _ *Manager, db walletdb.DB,
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)

addrs, err := scopedMgr.NextExternalAddresses(
ns, accountNum, 1,
addrs, err := scopedMgr.NextAddresses(
ns, accountNum, ExternalBranch, 1,
)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 0410b7c

Please sign in to comment.