-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SPV-742): adjust comments- move network to wire package; reimple…
…ment address book with buckets to improve geting random address; connect to one randomly chosen seed at start instead of all of them
- Loading branch information
1 parent
bb1004c
commit 25f2fbb
Showing
5 changed files
with
165 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package network | ||
|
||
type addrBucket struct { | ||
items []*knownAddress | ||
lookup map[string]int | ||
} | ||
|
||
func newAddrBucket(initCapacity uint) *addrBucket { | ||
return &addrBucket{ | ||
items: make([]*knownAddress, 0, initCapacity), | ||
lookup: make(map[string]int, initCapacity), | ||
} | ||
} | ||
|
||
func (a *addrBucket) find(key string) *knownAddress { | ||
addrIndex, ok := a.lookup[key] | ||
|
||
if ok { | ||
return a.items[addrIndex] | ||
} | ||
return nil | ||
} | ||
|
||
func (a *addrBucket) add(key string, addr *knownAddress) { | ||
newItemIndex := len(a.items) | ||
|
||
a.items = append(a.items, addr) | ||
a.lookup[key] = newItemIndex | ||
} | ||
|
||
func (a *addrBucket) rm(key string) { | ||
addrIndex, ok := a.lookup[key] | ||
|
||
if ok { | ||
// substitute with last element | ||
a.items[addrIndex] = a.items[len(a.items)-1] | ||
// remove last element | ||
a.items = a.items[:len(a.items)-1] | ||
|
||
delete(a.lookup, key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.