Skip to content

Commit

Permalink
[lbry] rpc: import getnetworkinfo from bchd
Browse files Browse the repository at this point in the history
  • Loading branch information
BrannonKing authored and roylee17 committed May 24, 2022
1 parent 5116f45 commit 81862c6
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 46 deletions.
47 changes: 28 additions & 19 deletions addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type AddrManager struct {
nTried int
nNew int
lamtx sync.Mutex
localAddresses map[string]*localAddress
localAddresses map[string]*LocalAddress
version int
}

Expand All @@ -69,9 +69,9 @@ type serializedAddrManager struct {
TriedBuckets [triedBucketCount][]string
}

type localAddress struct {
na *wire.NetAddress
score AddressPriority
type LocalAddress struct {
NA *wire.NetAddress
Score AddressPriority
}

// AddressPriority type is used to describe the hierarchy of local address
Expand Down Expand Up @@ -178,7 +178,7 @@ func (a *AddrManager) updateAddress(netAddr, srcAddr *wire.NetAddress) {
// note that to prevent causing excess garbage on getaddr
// messages the netaddresses in addrmanager are *immutable*,
// if we need to change them then we replace the pointer with a
// new copy so that we don't have to copy every na for getaddr.
// new copy so that we don't have to copy every NA for getaddr.
if netAddr.Timestamp.After(ka.na.Timestamp) ||
(ka.na.Services&netAddr.Services) !=
netAddr.Services {
Expand Down Expand Up @@ -755,7 +755,7 @@ func (a *AddrManager) HostToNetAddress(host string, port uint16, services wire.S
// the relevant .onion address.
func ipString(na *wire.NetAddress) string {
if IsOnionCatTor(na) {
// We know now that na.IP is long enough.
// We know now that NA.IP is long enough.
base32 := base32.StdEncoding.EncodeToString(na.IP[6:])
return strings.ToLower(base32) + ".onion"
}
Expand Down Expand Up @@ -882,7 +882,7 @@ func (a *AddrManager) Connected(addr *wire.NetAddress) {
// so.
now := time.Now()
if now.After(ka.na.Timestamp.Add(time.Minute * 20)) {
// ka.na is immutable, so replace it.
// ka.NA is immutable, so replace it.
naCopy := *ka.na
naCopy.Timestamp = time.Now()
ka.mtx.Lock()
Expand Down Expand Up @@ -994,7 +994,7 @@ func (a *AddrManager) SetServices(addr *wire.NetAddress, services wire.ServiceFl

// Update the services if needed.
if ka.na.Services != services {
// ka.na is immutable, so replace it.
// ka.NA is immutable, so replace it.
naCopy := *ka.na
naCopy.Services = services
ka.mtx.Lock()
Expand All @@ -1003,7 +1003,7 @@ func (a *AddrManager) SetServices(addr *wire.NetAddress, services wire.ServiceFl
}
}

// AddLocalAddress adds na to the list of known local addresses to advertise
// AddLocalAddress adds NA to the list of known local addresses to advertise
// with the given priority.
func (a *AddrManager) AddLocalAddress(na *wire.NetAddress, priority AddressPriority) error {
if !IsRoutable(na) {
Expand All @@ -1015,13 +1015,13 @@ func (a *AddrManager) AddLocalAddress(na *wire.NetAddress, priority AddressPrior

key := NetAddressKey(na)
la, ok := a.localAddresses[key]
if !ok || la.score < priority {
if !ok || la.Score < priority {
if ok {
la.score = priority + 1
la.Score = priority + 1
} else {
a.localAddresses[key] = &localAddress{
na: na,
score: priority,
a.localAddresses[key] = &LocalAddress{
NA: na,
Score: priority,
}
}
}
Expand Down Expand Up @@ -1117,12 +1117,12 @@ func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddress) *wire.Net
var bestscore AddressPriority
var bestAddress *wire.NetAddress
for _, la := range a.localAddresses {
reach := getReachabilityFrom(la.na, remoteAddr)
reach := getReachabilityFrom(la.NA, remoteAddr)
if reach > bestreach ||
(reach == bestreach && la.score > bestscore) {
(reach == bestreach && la.Score > bestscore) {
bestreach = reach
bestscore = la.score
bestAddress = la.na
bestscore = la.Score
bestAddress = la.NA
}
}
if bestAddress != nil {
Expand All @@ -1146,6 +1146,15 @@ func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddress) *wire.Net
return bestAddress
}

// LocalAddresses returns the list of local addresses for our node.
func (a *AddrManager) LocalAddresses() []*LocalAddress {
var addrs []*LocalAddress
for _, addr := range a.localAddresses {
addrs = append(addrs, addr)
}
return addrs
}

// New returns a new bitcoin address manager.
// Use Start to begin processing asynchronous address updates.
func New(dataDir string, lookupFunc func(string) ([]net.IP, error)) *AddrManager {
Expand All @@ -1154,7 +1163,7 @@ func New(dataDir string, lookupFunc func(string) ([]net.IP, error)) *AddrManager
lookupFunc: lookupFunc,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
quit: make(chan struct{}),
localAddresses: make(map[string]*localAddress),
localAddresses: make(map[string]*LocalAddress),
version: serialisationVersion,
}
am.reset()
Expand Down
9 changes: 9 additions & 0 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ func (b *BlockChain) HaveBlock(hash *chainhash.Hash) (bool, error) {
return exists || b.IsKnownOrphan(hash), nil
}

// GetWarnings returns a bool for whether unknownRules
// has been warned.
func (b *BlockChain) GetWarnings() bool {
b.chainLock.RLock()
defer b.chainLock.RUnlock()

return b.unknownRulesWarned
}

// IsKnownOrphan returns whether the passed hash is currently a known orphan.
// Keep in mind that only a limited number of orphans are held onto for a
// limited amount of time, so this function must not be used as an absolute
Expand Down
Loading

0 comments on commit 81862c6

Please sign in to comment.