|
@ -386,7 +386,8 @@ func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAd |
|
|
func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
// Sanity check
|
|
|
// Sanity check
|
|
|
if ka.isOld() { |
|
|
if ka.isOld() { |
|
|
panic("Cannot add address already in old bucket to a new bucket") |
|
|
|
|
|
|
|
|
log.Warning("Cannot add address already in old bucket to a new bucket: %v", ka) |
|
|
|
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
addrStr := ka.Addr.String() |
|
|
addrStr := ka.Addr.String() |
|
@ -419,10 +420,12 @@ func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
// Sanity check
|
|
|
// Sanity check
|
|
|
if ka.isNew() { |
|
|
if ka.isNew() { |
|
|
panic("Cannot add new address to old bucket") |
|
|
|
|
|
|
|
|
log.Warning("Cannot add new address to old bucket: %v", ka) |
|
|
|
|
|
return false |
|
|
} |
|
|
} |
|
|
if len(ka.Buckets) != 0 { |
|
|
if len(ka.Buckets) != 0 { |
|
|
panic("Cannot add already old address to another old bucket") |
|
|
|
|
|
|
|
|
log.Warning("Cannot add already old address to another old bucket: %v", ka) |
|
|
|
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
addrStr := ka.Addr.String() |
|
|
addrStr := ka.Addr.String() |
|
@ -452,7 +455,8 @@ func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool { |
|
|
|
|
|
|
|
|
func (a *AddrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) { |
|
|
func (a *AddrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) { |
|
|
if ka.BucketType != bucketType { |
|
|
if ka.BucketType != bucketType { |
|
|
panic("Bucket type mismatch") |
|
|
|
|
|
|
|
|
log.Warning("Bucket type mismatch: %v", ka) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
bucket := a.getBucket(bucketType, bucketIdx) |
|
|
bucket := a.getBucket(bucketType, bucketIdx) |
|
|
delete(bucket, ka.Addr.String()) |
|
|
delete(bucket, ka.Addr.String()) |
|
@ -493,7 +497,8 @@ func (a *AddrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress { |
|
|
|
|
|
|
|
|
func (a *AddrBook) addAddress(addr, src *NetAddress) { |
|
|
func (a *AddrBook) addAddress(addr, src *NetAddress) { |
|
|
if !addr.Routable() { |
|
|
if !addr.Routable() { |
|
|
panic("Cannot add non-routable address") |
|
|
|
|
|
|
|
|
log.Warning("Cannot add non-routable address %v", addr) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ka := a.addrLookup[addr.String()] |
|
|
ka := a.addrLookup[addr.String()] |
|
@ -545,10 +550,12 @@ func (a *AddrBook) expireNew(bucketIdx int) { |
|
|
func (a *AddrBook) moveToOld(ka *knownAddress) { |
|
|
func (a *AddrBook) moveToOld(ka *knownAddress) { |
|
|
// Sanity check
|
|
|
// Sanity check
|
|
|
if ka.isOld() { |
|
|
if ka.isOld() { |
|
|
panic("Cannot promote address that is already old") |
|
|
|
|
|
|
|
|
log.Warning("Cannot promote address that is already old %v", ka) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
if len(ka.Buckets) == 0 { |
|
|
if len(ka.Buckets) == 0 { |
|
|
panic("Cannot promote address that isn't in any new buckets") |
|
|
|
|
|
|
|
|
log.Warning("Cannot promote address that isn't in any new buckets %v", ka) |
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Remember one of the buckets in which ka is in.
|
|
|
// Remember one of the buckets in which ka is in.
|
|
@ -725,7 +732,8 @@ func (ka *knownAddress) markGood() { |
|
|
func (ka *knownAddress) addBucketRef(bucketIdx int) int { |
|
|
func (ka *knownAddress) addBucketRef(bucketIdx int) int { |
|
|
for _, bucket := range ka.Buckets { |
|
|
for _, bucket := range ka.Buckets { |
|
|
if bucket == bucketIdx { |
|
|
if bucket == bucketIdx { |
|
|
panic("Bucket already exists in ka.Buckets") |
|
|
|
|
|
|
|
|
log.Warning("Bucket already exists in ka.Buckets: %v", ka) |
|
|
|
|
|
return -1 |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
ka.Buckets = append(ka.Buckets, bucketIdx) |
|
|
ka.Buckets = append(ka.Buckets, bucketIdx) |
|
@ -740,7 +748,8 @@ func (ka *knownAddress) removeBucketRef(bucketIdx int) int { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if len(buckets) != len(ka.Buckets)-1 { |
|
|
if len(buckets) != len(ka.Buckets)-1 { |
|
|
panic("bucketIdx not found in ka.Buckets") |
|
|
|
|
|
|
|
|
log.Warning("bucketIdx not found in ka.Buckets: %v", ka) |
|
|
|
|
|
return -1 |
|
|
} |
|
|
} |
|
|
ka.Buckets = buckets |
|
|
ka.Buckets = buckets |
|
|
return len(ka.Buckets) |
|
|
return len(ka.Buckets) |
|
|