You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

806 lines
21 KiB

9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
8 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
  1. // Modified for Tendermint
  2. // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
  3. // https://github.com/conformal/btcd/blob/master/LICENSE
  4. package pex
  5. import (
  6. "crypto/sha256"
  7. "encoding/binary"
  8. "math"
  9. "net"
  10. "sync"
  11. "time"
  12. crypto "github.com/tendermint/go-crypto"
  13. "github.com/tendermint/tendermint/p2p"
  14. cmn "github.com/tendermint/tmlibs/common"
  15. )
  16. const (
  17. bucketTypeNew = 0x01
  18. bucketTypeOld = 0x02
  19. )
  20. // AddrBook is an address book used for tracking peers
  21. // so we can gossip about them to others and select
  22. // peers to dial.
  23. // TODO: break this up?
  24. type AddrBook interface {
  25. cmn.Service
  26. // Add our own addresses so we don't later add ourselves
  27. AddOurAddress(*p2p.NetAddress)
  28. // Check if it is our address
  29. OurAddress(*p2p.NetAddress) bool
  30. // Add and remove an address
  31. AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error
  32. RemoveAddress(*p2p.NetAddress)
  33. // Check if the address is in the book
  34. HasAddress(*p2p.NetAddress) bool
  35. // Do we need more peers?
  36. NeedMoreAddrs() bool
  37. // Pick an address to dial
  38. PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress
  39. // Mark address
  40. MarkGood(*p2p.NetAddress)
  41. MarkAttempt(*p2p.NetAddress)
  42. MarkBad(*p2p.NetAddress)
  43. IsGood(*p2p.NetAddress) bool
  44. // Send a selection of addresses to peers
  45. GetSelection() []*p2p.NetAddress
  46. // Send a selection of addresses with bias
  47. GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress
  48. // TODO: remove
  49. ListOfKnownAddresses() []*knownAddress
  50. // Persist to disk
  51. Save()
  52. }
  53. var _ AddrBook = (*addrBook)(nil)
  54. // addrBook - concurrency safe peer address manager.
  55. // Implements AddrBook.
  56. type addrBook struct {
  57. cmn.BaseService
  58. // immutable after creation
  59. filePath string
  60. routabilityStrict bool
  61. key string // random prefix for bucket placement
  62. // accessed concurrently
  63. mtx sync.Mutex
  64. rand *cmn.Rand
  65. ourAddrs map[string]struct{}
  66. addrLookup map[p2p.ID]*knownAddress // new & old
  67. bucketsOld []map[string]*knownAddress
  68. bucketsNew []map[string]*knownAddress
  69. nOld int
  70. nNew int
  71. wg sync.WaitGroup
  72. }
  73. // NewAddrBook creates a new address book.
  74. // Use Start to begin processing asynchronous address updates.
  75. func NewAddrBook(filePath string, routabilityStrict bool) *addrBook {
  76. am := &addrBook{
  77. rand: cmn.NewRand(),
  78. ourAddrs: make(map[string]struct{}),
  79. addrLookup: make(map[p2p.ID]*knownAddress),
  80. filePath: filePath,
  81. routabilityStrict: routabilityStrict,
  82. }
  83. am.init()
  84. am.BaseService = *cmn.NewBaseService(nil, "AddrBook", am)
  85. return am
  86. }
  87. // Initialize the buckets.
  88. // When modifying this, don't forget to update loadFromFile()
  89. func (a *addrBook) init() {
  90. a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
  91. // New addr buckets
  92. a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
  93. for i := range a.bucketsNew {
  94. a.bucketsNew[i] = make(map[string]*knownAddress)
  95. }
  96. // Old addr buckets
  97. a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
  98. for i := range a.bucketsOld {
  99. a.bucketsOld[i] = make(map[string]*knownAddress)
  100. }
  101. }
  102. // OnStart implements Service.
  103. func (a *addrBook) OnStart() error {
  104. if err := a.BaseService.OnStart(); err != nil {
  105. return err
  106. }
  107. a.loadFromFile(a.filePath)
  108. // wg.Add to ensure that any invocation of .Wait()
  109. // later on will wait for saveRoutine to terminate.
  110. a.wg.Add(1)
  111. go a.saveRoutine()
  112. return nil
  113. }
  114. // OnStop implements Service.
  115. func (a *addrBook) OnStop() {
  116. a.BaseService.OnStop()
  117. }
  118. func (a *addrBook) Wait() {
  119. a.wg.Wait()
  120. }
  121. func (a *addrBook) FilePath() string {
  122. return a.filePath
  123. }
  124. //-------------------------------------------------------
  125. // AddOurAddress one of our addresses.
  126. func (a *addrBook) AddOurAddress(addr *p2p.NetAddress) {
  127. a.mtx.Lock()
  128. defer a.mtx.Unlock()
  129. a.Logger.Info("Add our address to book", "addr", addr)
  130. a.ourAddrs[addr.String()] = struct{}{}
  131. }
  132. // OurAddress returns true if it is our address.
  133. func (a *addrBook) OurAddress(addr *p2p.NetAddress) bool {
  134. a.mtx.Lock()
  135. _, ok := a.ourAddrs[addr.String()]
  136. a.mtx.Unlock()
  137. return ok
  138. }
  139. // AddAddress implements AddrBook
  140. // Add address to a "new" bucket. If it's already in one, only add it probabilistically.
  141. // Returns error if the addr is non-routable. Does not add self.
  142. // NOTE: addr must not be nil
  143. func (a *addrBook) AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error {
  144. a.mtx.Lock()
  145. defer a.mtx.Unlock()
  146. return a.addAddress(addr, src)
  147. }
  148. // RemoveAddress implements AddrBook - removes the address from the book.
  149. func (a *addrBook) RemoveAddress(addr *p2p.NetAddress) {
  150. a.mtx.Lock()
  151. defer a.mtx.Unlock()
  152. ka := a.addrLookup[addr.ID]
  153. if ka == nil {
  154. return
  155. }
  156. a.Logger.Info("Remove address from book", "addr", ka.Addr, "ID", ka.ID)
  157. a.removeFromAllBuckets(ka)
  158. }
  159. // IsGood returns true if peer was ever marked as good and haven't
  160. // done anything wrong since then.
  161. func (a *addrBook) IsGood(addr *p2p.NetAddress) bool {
  162. a.mtx.Lock()
  163. defer a.mtx.Unlock()
  164. return a.addrLookup[addr.ID].isOld()
  165. }
  166. // HasAddress returns true if the address is in the book.
  167. func (a *addrBook) HasAddress(addr *p2p.NetAddress) bool {
  168. a.mtx.Lock()
  169. defer a.mtx.Unlock()
  170. ka := a.addrLookup[addr.ID]
  171. return ka != nil
  172. }
  173. // NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book.
  174. func (a *addrBook) NeedMoreAddrs() bool {
  175. return a.Size() < needAddressThreshold
  176. }
  177. // PickAddress implements AddrBook. It picks an address to connect to.
  178. // The address is picked randomly from an old or new bucket according
  179. // to the biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to that range)
  180. // and determines how biased we are to pick an address from a new bucket.
  181. // PickAddress returns nil if the AddrBook is empty or if we try to pick
  182. // from an empty bucket.
  183. func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress {
  184. a.mtx.Lock()
  185. defer a.mtx.Unlock()
  186. if a.size() == 0 {
  187. return nil
  188. }
  189. if biasTowardsNewAddrs > 100 {
  190. biasTowardsNewAddrs = 100
  191. }
  192. if biasTowardsNewAddrs < 0 {
  193. biasTowardsNewAddrs = 0
  194. }
  195. // Bias between new and old addresses.
  196. oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(biasTowardsNewAddrs))
  197. newCorrelation := math.Sqrt(float64(a.nNew)) * float64(biasTowardsNewAddrs)
  198. // pick a random peer from a random bucket
  199. var bucket map[string]*knownAddress
  200. pickFromOldBucket := (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation
  201. if (pickFromOldBucket && a.nOld == 0) ||
  202. (!pickFromOldBucket && a.nNew == 0) {
  203. return nil
  204. }
  205. // loop until we pick a random non-empty bucket
  206. for len(bucket) == 0 {
  207. if pickFromOldBucket {
  208. bucket = a.bucketsOld[a.rand.Intn(len(a.bucketsOld))]
  209. } else {
  210. bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
  211. }
  212. }
  213. // pick a random index and loop over the map to return that index
  214. randIndex := a.rand.Intn(len(bucket))
  215. for _, ka := range bucket {
  216. if randIndex == 0 {
  217. return ka.Addr
  218. }
  219. randIndex--
  220. }
  221. return nil
  222. }
  223. // MarkGood implements AddrBook - it marks the peer as good and
  224. // moves it into an "old" bucket.
  225. func (a *addrBook) MarkGood(addr *p2p.NetAddress) {
  226. a.mtx.Lock()
  227. defer a.mtx.Unlock()
  228. ka := a.addrLookup[addr.ID]
  229. if ka == nil {
  230. return
  231. }
  232. ka.markGood()
  233. if ka.isNew() {
  234. a.moveToOld(ka)
  235. }
  236. }
  237. // MarkAttempt implements AddrBook - it marks that an attempt was made to connect to the address.
  238. func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) {
  239. a.mtx.Lock()
  240. defer a.mtx.Unlock()
  241. ka := a.addrLookup[addr.ID]
  242. if ka == nil {
  243. return
  244. }
  245. ka.markAttempt()
  246. }
  247. // MarkBad implements AddrBook. Currently it just ejects the address.
  248. // TODO: black list for some amount of time
  249. func (a *addrBook) MarkBad(addr *p2p.NetAddress) {
  250. a.RemoveAddress(addr)
  251. }
  252. // GetSelection implements AddrBook.
  253. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  254. // Must never return a nil address.
  255. func (a *addrBook) GetSelection() []*p2p.NetAddress {
  256. a.mtx.Lock()
  257. defer a.mtx.Unlock()
  258. bookSize := a.size()
  259. if bookSize == 0 {
  260. return nil
  261. }
  262. numAddresses := cmn.MaxInt(
  263. cmn.MinInt(minGetSelection, bookSize),
  264. bookSize*getSelectionPercent/100)
  265. numAddresses = cmn.MinInt(maxGetSelection, numAddresses)
  266. // XXX: instead of making a list of all addresses, shuffling, and slicing a random chunk,
  267. // could we just select a random numAddresses of indexes?
  268. allAddr := make([]*p2p.NetAddress, bookSize)
  269. i := 0
  270. for _, ka := range a.addrLookup {
  271. allAddr[i] = ka.Addr
  272. i++
  273. }
  274. // Fisher-Yates shuffle the array. We only need to do the first
  275. // `numAddresses' since we are throwing the rest.
  276. for i := 0; i < numAddresses; i++ {
  277. // pick a number between current index and the end
  278. j := cmn.RandIntn(len(allAddr)-i) + i
  279. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  280. }
  281. // slice off the limit we are willing to share.
  282. return allAddr[:numAddresses]
  283. }
  284. // GetSelectionWithBias implements AddrBook.
  285. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  286. // Must never return a nil address.
  287. //
  288. // Each address is picked randomly from an old or new bucket according to the
  289. // biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to
  290. // that range) and determines how biased we are to pick an address from a new
  291. // bucket.
  292. func (a *addrBook) GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress {
  293. a.mtx.Lock()
  294. defer a.mtx.Unlock()
  295. bookSize := a.size()
  296. if bookSize == 0 {
  297. return nil
  298. }
  299. if biasTowardsNewAddrs > 100 {
  300. biasTowardsNewAddrs = 100
  301. }
  302. if biasTowardsNewAddrs < 0 {
  303. biasTowardsNewAddrs = 0
  304. }
  305. numAddresses := cmn.MaxInt(
  306. cmn.MinInt(minGetSelection, bookSize),
  307. bookSize*getSelectionPercent/100)
  308. numAddresses = cmn.MinInt(maxGetSelection, numAddresses)
  309. selection := make([]*p2p.NetAddress, numAddresses)
  310. oldBucketToAddrsMap := make(map[int]map[string]struct{})
  311. var oldIndex int
  312. newBucketToAddrsMap := make(map[int]map[string]struct{})
  313. var newIndex int
  314. selectionIndex := 0
  315. ADDRS_LOOP:
  316. for selectionIndex < numAddresses {
  317. pickFromOldBucket := int((float64(selectionIndex)/float64(numAddresses))*100) >= biasTowardsNewAddrs
  318. pickFromOldBucket = (pickFromOldBucket && a.nOld > 0) || a.nNew == 0
  319. bucket := make(map[string]*knownAddress)
  320. // loop until we pick a random non-empty bucket
  321. for len(bucket) == 0 {
  322. if pickFromOldBucket {
  323. oldIndex = a.rand.Intn(len(a.bucketsOld))
  324. bucket = a.bucketsOld[oldIndex]
  325. } else {
  326. newIndex = a.rand.Intn(len(a.bucketsNew))
  327. bucket = a.bucketsNew[newIndex]
  328. }
  329. }
  330. // pick a random index
  331. randIndex := a.rand.Intn(len(bucket))
  332. // loop over the map to return that index
  333. var selectedAddr *p2p.NetAddress
  334. for _, ka := range bucket {
  335. if randIndex == 0 {
  336. selectedAddr = ka.Addr
  337. break
  338. }
  339. randIndex--
  340. }
  341. // if we have selected the address before, restart the loop
  342. // otherwise, record it and continue
  343. if pickFromOldBucket {
  344. if addrsMap, ok := oldBucketToAddrsMap[oldIndex]; ok {
  345. if _, ok = addrsMap[selectedAddr.String()]; ok {
  346. continue ADDRS_LOOP
  347. }
  348. } else {
  349. oldBucketToAddrsMap[oldIndex] = make(map[string]struct{})
  350. }
  351. oldBucketToAddrsMap[oldIndex][selectedAddr.String()] = struct{}{}
  352. } else {
  353. if addrsMap, ok := newBucketToAddrsMap[newIndex]; ok {
  354. if _, ok = addrsMap[selectedAddr.String()]; ok {
  355. continue ADDRS_LOOP
  356. }
  357. } else {
  358. newBucketToAddrsMap[newIndex] = make(map[string]struct{})
  359. }
  360. newBucketToAddrsMap[newIndex][selectedAddr.String()] = struct{}{}
  361. }
  362. selection[selectionIndex] = selectedAddr
  363. selectionIndex++
  364. }
  365. return selection
  366. }
  367. // ListOfKnownAddresses returns the new and old addresses.
  368. func (a *addrBook) ListOfKnownAddresses() []*knownAddress {
  369. a.mtx.Lock()
  370. defer a.mtx.Unlock()
  371. addrs := []*knownAddress{}
  372. for _, addr := range a.addrLookup {
  373. addrs = append(addrs, addr.copy())
  374. }
  375. return addrs
  376. }
  377. //------------------------------------------------
  378. // Size returns the number of addresses in the book.
  379. func (a *addrBook) Size() int {
  380. a.mtx.Lock()
  381. defer a.mtx.Unlock()
  382. return a.size()
  383. }
  384. func (a *addrBook) size() int {
  385. return a.nNew + a.nOld
  386. }
  387. //----------------------------------------------------------
  388. // Save persists the address book to disk.
  389. func (a *addrBook) Save() {
  390. a.saveToFile(a.filePath) // thread safe
  391. }
  392. func (a *addrBook) saveRoutine() {
  393. defer a.wg.Done()
  394. saveFileTicker := time.NewTicker(dumpAddressInterval)
  395. out:
  396. for {
  397. select {
  398. case <-saveFileTicker.C:
  399. a.saveToFile(a.filePath)
  400. case <-a.Quit():
  401. break out
  402. }
  403. }
  404. saveFileTicker.Stop()
  405. a.saveToFile(a.filePath)
  406. a.Logger.Info("Address handler done")
  407. }
  408. //----------------------------------------------------------
  409. func (a *addrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  410. switch bucketType {
  411. case bucketTypeNew:
  412. return a.bucketsNew[bucketIdx]
  413. case bucketTypeOld:
  414. return a.bucketsOld[bucketIdx]
  415. default:
  416. cmn.PanicSanity("Should not happen")
  417. return nil
  418. }
  419. }
  420. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  421. // NOTE: currently it always returns true.
  422. func (a *addrBook) addToNewBucket(ka *knownAddress, bucketIdx int) {
  423. // Sanity check
  424. if ka.isOld() {
  425. a.Logger.Error("Failed Sanity Check! Cant add old address to new bucket", "ka", ka, "bucket", bucketIdx)
  426. return
  427. }
  428. addrStr := ka.Addr.String()
  429. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  430. // Already exists?
  431. if _, ok := bucket[addrStr]; ok {
  432. return
  433. }
  434. // Enforce max addresses.
  435. if len(bucket) > newBucketSize {
  436. a.Logger.Info("new bucket is full, expiring new")
  437. a.expireNew(bucketIdx)
  438. }
  439. // Add to bucket.
  440. bucket[addrStr] = ka
  441. // increment nNew if the peer doesnt already exist in a bucket
  442. if ka.addBucketRef(bucketIdx) == 1 {
  443. a.nNew++
  444. }
  445. // Add it to addrLookup
  446. a.addrLookup[ka.ID()] = ka
  447. }
  448. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  449. func (a *addrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  450. // Sanity check
  451. if ka.isNew() {
  452. a.Logger.Error(cmn.Fmt("Cannot add new address to old bucket: %v", ka))
  453. return false
  454. }
  455. if len(ka.Buckets) != 0 {
  456. a.Logger.Error(cmn.Fmt("Cannot add already old address to another old bucket: %v", ka))
  457. return false
  458. }
  459. addrStr := ka.Addr.String()
  460. bucket := a.getBucket(bucketTypeOld, bucketIdx)
  461. // Already exists?
  462. if _, ok := bucket[addrStr]; ok {
  463. return true
  464. }
  465. // Enforce max addresses.
  466. if len(bucket) > oldBucketSize {
  467. return false
  468. }
  469. // Add to bucket.
  470. bucket[addrStr] = ka
  471. if ka.addBucketRef(bucketIdx) == 1 {
  472. a.nOld++
  473. }
  474. // Ensure in addrLookup
  475. a.addrLookup[ka.ID()] = ka
  476. return true
  477. }
  478. func (a *addrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  479. if ka.BucketType != bucketType {
  480. a.Logger.Error(cmn.Fmt("Bucket type mismatch: %v", ka))
  481. return
  482. }
  483. bucket := a.getBucket(bucketType, bucketIdx)
  484. delete(bucket, ka.Addr.String())
  485. if ka.removeBucketRef(bucketIdx) == 0 {
  486. if bucketType == bucketTypeNew {
  487. a.nNew--
  488. } else {
  489. a.nOld--
  490. }
  491. delete(a.addrLookup, ka.ID())
  492. }
  493. }
  494. func (a *addrBook) removeFromAllBuckets(ka *knownAddress) {
  495. for _, bucketIdx := range ka.Buckets {
  496. bucket := a.getBucket(ka.BucketType, bucketIdx)
  497. delete(bucket, ka.Addr.String())
  498. }
  499. ka.Buckets = nil
  500. if ka.BucketType == bucketTypeNew {
  501. a.nNew--
  502. } else {
  503. a.nOld--
  504. }
  505. delete(a.addrLookup, ka.ID())
  506. }
  507. //----------------------------------------------------------
  508. func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  509. bucket := a.getBucket(bucketType, bucketIdx)
  510. var oldest *knownAddress
  511. for _, ka := range bucket {
  512. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  513. oldest = ka
  514. }
  515. }
  516. return oldest
  517. }
  518. // adds the address to a "new" bucket. if its already in one,
  519. // it only adds it probabilistically
  520. func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
  521. if addr == nil {
  522. return ErrAddrBookNilAddr{addr, src}
  523. }
  524. if src == nil {
  525. return ErrAddrBookNilAddr{addr, src}
  526. }
  527. if a.routabilityStrict && !addr.Routable() {
  528. return ErrAddrBookNonRoutable{addr}
  529. }
  530. // TODO: we should track ourAddrs by ID and by IP:PORT and refuse both.
  531. if _, ok := a.ourAddrs[addr.String()]; ok {
  532. return ErrAddrBookSelf{addr}
  533. }
  534. ka := a.addrLookup[addr.ID]
  535. if ka != nil {
  536. // Already old.
  537. if ka.isOld() {
  538. return nil
  539. }
  540. // Already in max new buckets.
  541. if len(ka.Buckets) == maxNewBucketsPerAddress {
  542. return nil
  543. }
  544. // The more entries we have, the less likely we are to add more.
  545. factor := int32(2 * len(ka.Buckets))
  546. if a.rand.Int31n(factor) != 0 {
  547. return nil
  548. }
  549. } else {
  550. ka = newKnownAddress(addr, src)
  551. }
  552. bucket := a.calcNewBucket(addr, src)
  553. a.addToNewBucket(ka, bucket)
  554. return nil
  555. }
  556. // Make space in the new buckets by expiring the really bad entries.
  557. // If no bad entries are available we remove the oldest.
  558. func (a *addrBook) expireNew(bucketIdx int) {
  559. for addrStr, ka := range a.bucketsNew[bucketIdx] {
  560. // If an entry is bad, throw it away
  561. if ka.isBad() {
  562. a.Logger.Info(cmn.Fmt("expiring bad address %v", addrStr))
  563. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  564. return
  565. }
  566. }
  567. // If we haven't thrown out a bad entry, throw out the oldest entry
  568. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  569. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  570. }
  571. // Promotes an address from new to old. If the destination bucket is full,
  572. // demote the oldest one to a "new" bucket.
  573. // TODO: Demote more probabilistically?
  574. func (a *addrBook) moveToOld(ka *knownAddress) {
  575. // Sanity check
  576. if ka.isOld() {
  577. a.Logger.Error(cmn.Fmt("Cannot promote address that is already old %v", ka))
  578. return
  579. }
  580. if len(ka.Buckets) == 0 {
  581. a.Logger.Error(cmn.Fmt("Cannot promote address that isn't in any new buckets %v", ka))
  582. return
  583. }
  584. // Remove from all (new) buckets.
  585. a.removeFromAllBuckets(ka)
  586. // It's officially old now.
  587. ka.BucketType = bucketTypeOld
  588. // Try to add it to its oldBucket destination.
  589. oldBucketIdx := a.calcOldBucket(ka.Addr)
  590. added := a.addToOldBucket(ka, oldBucketIdx)
  591. if !added {
  592. // No room; move the oldest to a new bucket
  593. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  594. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  595. newBucketIdx := a.calcNewBucket(oldest.Addr, oldest.Src)
  596. a.addToNewBucket(oldest, newBucketIdx)
  597. // Finally, add our ka to old bucket again.
  598. added = a.addToOldBucket(ka, oldBucketIdx)
  599. if !added {
  600. a.Logger.Error(cmn.Fmt("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  601. }
  602. }
  603. }
  604. //---------------------------------------------------------------------
  605. // calculate bucket placements
  606. // doublesha256( key + sourcegroup +
  607. // int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets
  608. func (a *addrBook) calcNewBucket(addr, src *p2p.NetAddress) int {
  609. data1 := []byte{}
  610. data1 = append(data1, []byte(a.key)...)
  611. data1 = append(data1, []byte(a.groupKey(addr))...)
  612. data1 = append(data1, []byte(a.groupKey(src))...)
  613. hash1 := doubleSha256(data1)
  614. hash64 := binary.BigEndian.Uint64(hash1)
  615. hash64 %= newBucketsPerGroup
  616. var hashbuf [8]byte
  617. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  618. data2 := []byte{}
  619. data2 = append(data2, []byte(a.key)...)
  620. data2 = append(data2, a.groupKey(src)...)
  621. data2 = append(data2, hashbuf[:]...)
  622. hash2 := doubleSha256(data2)
  623. return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  624. }
  625. // doublesha256( key + group +
  626. // int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets
  627. func (a *addrBook) calcOldBucket(addr *p2p.NetAddress) int {
  628. data1 := []byte{}
  629. data1 = append(data1, []byte(a.key)...)
  630. data1 = append(data1, []byte(addr.String())...)
  631. hash1 := doubleSha256(data1)
  632. hash64 := binary.BigEndian.Uint64(hash1)
  633. hash64 %= oldBucketsPerGroup
  634. var hashbuf [8]byte
  635. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  636. data2 := []byte{}
  637. data2 = append(data2, []byte(a.key)...)
  638. data2 = append(data2, a.groupKey(addr)...)
  639. data2 = append(data2, hashbuf[:]...)
  640. hash2 := doubleSha256(data2)
  641. return int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  642. }
  643. // Return a string representing the network group of this address.
  644. // This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
  645. // "local" for a local address and the string "unroutable" for an unroutable
  646. // address.
  647. func (a *addrBook) groupKey(na *p2p.NetAddress) string {
  648. if a.routabilityStrict && na.Local() {
  649. return "local"
  650. }
  651. if a.routabilityStrict && !na.Routable() {
  652. return "unroutable"
  653. }
  654. if ipv4 := na.IP.To4(); ipv4 != nil {
  655. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(16, 32)}).String()
  656. }
  657. if na.RFC6145() || na.RFC6052() {
  658. // last four bytes are the ip address
  659. ip := net.IP(na.IP[12:16])
  660. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  661. }
  662. if na.RFC3964() {
  663. ip := net.IP(na.IP[2:7])
  664. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  665. }
  666. if na.RFC4380() {
  667. // teredo tunnels have the last 4 bytes as the v4 address XOR
  668. // 0xff.
  669. ip := net.IP(make([]byte, 4))
  670. for i, byte := range na.IP[12:16] {
  671. ip[i] = byte ^ 0xff
  672. }
  673. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  674. }
  675. // OK, so now we know ourselves to be a IPv6 address.
  676. // bitcoind uses /32 for everything, except for Hurricane Electric's
  677. // (he.net) IP range, which it uses /36 for.
  678. bits := 32
  679. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"),
  680. Mask: net.CIDRMask(32, 128)}
  681. if heNet.Contains(na.IP) {
  682. bits = 36
  683. }
  684. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(bits, 128)}).String()
  685. }
  686. // doubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes.
  687. func doubleSha256(b []byte) []byte {
  688. hasher := sha256.New()
  689. hasher.Write(b) // nolint: errcheck, gas
  690. sum := hasher.Sum(nil)
  691. hasher.Reset()
  692. hasher.Write(sum) // nolint: errcheck, gas
  693. return hasher.Sum(nil)
  694. }