|
|
@ -89,7 +89,7 @@ type AddrBook struct { |
|
|
|
mtx sync.Mutex |
|
|
|
rand *rand.Rand |
|
|
|
ourAddrs map[string]*NetAddress |
|
|
|
addrLookup map[string]*knownAddress // new & old
|
|
|
|
addrLookup map[ID]*knownAddress // new & old
|
|
|
|
bucketsOld []map[string]*knownAddress |
|
|
|
bucketsNew []map[string]*knownAddress |
|
|
|
nOld int |
|
|
@ -104,7 +104,7 @@ func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook { |
|
|
|
am := &AddrBook{ |
|
|
|
rand: rand.New(rand.NewSource(time.Now().UnixNano())), |
|
|
|
ourAddrs: make(map[string]*NetAddress), |
|
|
|
addrLookup: make(map[string]*knownAddress), |
|
|
|
addrLookup: make(map[ID]*knownAddress), |
|
|
|
filePath: filePath, |
|
|
|
routabilityStrict: routabilityStrict, |
|
|
|
} |
|
|
@ -244,11 +244,11 @@ func (a *AddrBook) PickAddress(newBias int) *NetAddress { |
|
|
|
} |
|
|
|
|
|
|
|
// MarkGood marks the peer as good and moves it into an "old" bucket.
|
|
|
|
// XXX: we never call this!
|
|
|
|
// TODO: call this from somewhere
|
|
|
|
func (a *AddrBook) MarkGood(addr *NetAddress) { |
|
|
|
a.mtx.Lock() |
|
|
|
defer a.mtx.Unlock() |
|
|
|
ka := a.addrLookup[addr.String()] |
|
|
|
ka := a.addrLookup[addr.ID] |
|
|
|
if ka == nil { |
|
|
|
return |
|
|
|
} |
|
|
@ -262,7 +262,7 @@ func (a *AddrBook) MarkGood(addr *NetAddress) { |
|
|
|
func (a *AddrBook) MarkAttempt(addr *NetAddress) { |
|
|
|
a.mtx.Lock() |
|
|
|
defer a.mtx.Unlock() |
|
|
|
ka := a.addrLookup[addr.String()] |
|
|
|
ka := a.addrLookup[addr.ID] |
|
|
|
if ka == nil { |
|
|
|
return |
|
|
|
} |
|
|
@ -279,11 +279,11 @@ func (a *AddrBook) MarkBad(addr *NetAddress) { |
|
|
|
func (a *AddrBook) RemoveAddress(addr *NetAddress) { |
|
|
|
a.mtx.Lock() |
|
|
|
defer a.mtx.Unlock() |
|
|
|
ka := a.addrLookup[addr.String()] |
|
|
|
ka := a.addrLookup[addr.ID] |
|
|
|
if ka == nil { |
|
|
|
return |
|
|
|
} |
|
|
|
a.Logger.Info("Remove address from book", "addr", addr) |
|
|
|
a.Logger.Info("Remove address from book", "addr", ka.Addr) |
|
|
|
a.removeFromAllBuckets(ka) |
|
|
|
} |
|
|
|
|
|
|
@ -300,8 +300,8 @@ func (a *AddrBook) GetSelection() []*NetAddress { |
|
|
|
|
|
|
|
allAddr := make([]*NetAddress, a.size()) |
|
|
|
i := 0 |
|
|
|
for _, v := range a.addrLookup { |
|
|
|
allAddr[i] = v.Addr |
|
|
|
for _, ka := range a.addrLookup { |
|
|
|
allAddr[i] = ka.Addr |
|
|
|
i++ |
|
|
|
} |
|
|
|
|
|
|
@ -388,7 +388,7 @@ func (a *AddrBook) loadFromFile(filePath string) bool { |
|
|
|
bucket := a.getBucket(ka.BucketType, bucketIndex) |
|
|
|
bucket[ka.Addr.String()] = ka |
|
|
|
} |
|
|
|
a.addrLookup[ka.Addr.String()] = ka |
|
|
|
a.addrLookup[ka.ID()] = ka |
|
|
|
if ka.BucketType == bucketTypeNew { |
|
|
|
a.nNew++ |
|
|
|
} else { |
|
|
@ -466,7 +466,7 @@ func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure in addrLookup
|
|
|
|
a.addrLookup[addrStr] = ka |
|
|
|
a.addrLookup[ka.ID()] = ka |
|
|
|
|
|
|
|
return true |
|
|
|
} |
|
|
@ -503,7 +503,7 @@ func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure in addrLookup
|
|
|
|
a.addrLookup[addrStr] = ka |
|
|
|
a.addrLookup[ka.ID()] = ka |
|
|
|
|
|
|
|
return true |
|
|
|
} |
|
|
@ -521,7 +521,7 @@ func (a *AddrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx |
|
|
|
} else { |
|
|
|
a.nOld-- |
|
|
|
} |
|
|
|
delete(a.addrLookup, ka.Addr.String()) |
|
|
|
delete(a.addrLookup, ka.ID()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -536,7 +536,7 @@ func (a *AddrBook) removeFromAllBuckets(ka *knownAddress) { |
|
|
|
} else { |
|
|
|
a.nOld-- |
|
|
|
} |
|
|
|
delete(a.addrLookup, ka.Addr.String()) |
|
|
|
delete(a.addrLookup, ka.ID()) |
|
|
|
} |
|
|
|
|
|
|
|
func (a *AddrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress { |
|
|
@ -559,7 +559,7 @@ func (a *AddrBook) addAddress(addr, src *NetAddress) error { |
|
|
|
return fmt.Errorf("Cannot add ourselves with address %v", addr) |
|
|
|
} |
|
|
|
|
|
|
|
ka := a.addrLookup[addr.String()] |
|
|
|
ka := a.addrLookup[addr.ID] |
|
|
|
|
|
|
|
if ka != nil { |
|
|
|
// Already old.
|
|
|
@ -768,6 +768,10 @@ func newKnownAddress(addr *NetAddress, src *NetAddress) *knownAddress { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (ka *knownAddress) ID() ID { |
|
|
|
return ka.Addr.ID |
|
|
|
} |
|
|
|
|
|
|
|
func (ka *knownAddress) isOld() bool { |
|
|
|
return ka.BucketType == bucketTypeOld |
|
|
|
} |
|
|
|