p2p/addrbook: addrNew/Old -> bucketsNew/Old

This commit is contained in:
Ethan Buchman 2017-11-16 02:28:11 +00:00
parent 498a82784d
commit 2f067a3f65

View File

@ -89,8 +89,8 @@ type AddrBook struct {
rand *rand.Rand rand *rand.Rand
ourAddrs map[string]*NetAddress ourAddrs map[string]*NetAddress
addrLookup map[string]*knownAddress // new & old addrLookup map[string]*knownAddress // new & old
addrNew []map[string]*knownAddress bucketsOld []map[string]*knownAddress
addrOld []map[string]*knownAddress bucketsNew []map[string]*knownAddress
nOld int nOld int
nNew int nNew int
@ -116,14 +116,14 @@ func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook {
func (a *AddrBook) init() { func (a *AddrBook) init() {
a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
// New addr buckets // New addr buckets
a.addrNew = make([]map[string]*knownAddress, newBucketCount) a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
for i := range a.addrNew { for i := range a.bucketsNew {
a.addrNew[i] = make(map[string]*knownAddress) a.bucketsNew[i] = make(map[string]*knownAddress)
} }
// Old addr buckets // Old addr buckets
a.addrOld = make([]map[string]*knownAddress, oldBucketCount) a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
for i := range a.addrOld { for i := range a.bucketsOld {
a.addrOld[i] = make(map[string]*knownAddress) a.bucketsOld[i] = make(map[string]*knownAddress)
} }
} }
@ -214,7 +214,7 @@ func (a *AddrBook) PickAddress(newBias int) *NetAddress {
if pickFromOldBucket { if pickFromOldBucket {
var bucket map[string]*knownAddress var bucket map[string]*knownAddress
for len(bucket) == 0 { for len(bucket) == 0 {
bucket = a.addrOld[a.rand.Intn(len(a.addrOld))] bucket = a.bucketsOld[a.rand.Intn(len(a.bucketsOld))]
} }
// pick a random ka from bucket. // pick a random ka from bucket.
randIndex := a.rand.Intn(len(bucket)) randIndex := a.rand.Intn(len(bucket))
@ -228,7 +228,7 @@ func (a *AddrBook) PickAddress(newBias int) *NetAddress {
} else { } else {
var bucket map[string]*knownAddress = nil var bucket map[string]*knownAddress = nil
for len(bucket) == 0 { for len(bucket) == 0 {
bucket = a.addrNew[a.rand.Intn(len(a.addrNew))] bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
} }
// pick a random ka from bucket. // pick a random ka from bucket.
randIndex := a.rand.Intn(len(bucket)) randIndex := a.rand.Intn(len(bucket))
@ -380,7 +380,7 @@ func (a *AddrBook) loadFromFile(filePath string) bool {
// Restore all the fields... // Restore all the fields...
// Restore the key // Restore the key
a.key = aJSON.Key a.key = aJSON.Key
// Restore .addrNew & .addrOld // Restore .bucketsNew & .bucketsOld
for _, ka := range aJSON.Addrs { for _, ka := range aJSON.Addrs {
for _, bucketIndex := range ka.Buckets { for _, bucketIndex := range ka.Buckets {
bucket := a.getBucket(ka.BucketType, bucketIndex) bucket := a.getBucket(ka.BucketType, bucketIndex)
@ -425,9 +425,9 @@ out:
func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress { func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
switch bucketType { switch bucketType {
case bucketTypeNew: case bucketTypeNew:
return a.addrNew[bucketIdx] return a.bucketsNew[bucketIdx]
case bucketTypeOld: case bucketTypeOld:
return a.addrOld[bucketIdx] return a.bucketsOld[bucketIdx]
default: default:
cmn.PanicSanity("Should not happen") cmn.PanicSanity("Should not happen")
return nil return nil
@ -587,7 +587,7 @@ func (a *AddrBook) addAddress(addr, src *NetAddress) {
// Make space in the new buckets by expiring the really bad entries. // Make space in the new buckets by expiring the really bad entries.
// If no bad entries are available we remove the oldest. // If no bad entries are available we remove the oldest.
func (a *AddrBook) expireNew(bucketIdx int) { func (a *AddrBook) expireNew(bucketIdx int) {
for addrStr, ka := range a.addrNew[bucketIdx] { for addrStr, ka := range a.bucketsNew[bucketIdx] {
// If an entry is bad, throw it away // If an entry is bad, throw it away
if ka.isBad() { if ka.isBad() {
a.Logger.Info(cmn.Fmt("expiring bad address %v", addrStr)) a.Logger.Info(cmn.Fmt("expiring bad address %v", addrStr))