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.

865 lines
22 KiB

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
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
9 years ago
9 years ago
8 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
9 years ago
9 years ago
9 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
8 years ago
8 years ago
9 years ago
8 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
8 years ago
8 years ago
8 years ago
9 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
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 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
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 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
8 years ago
9 years ago
8 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
9 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 p2p
  5. import (
  6. "encoding/binary"
  7. "encoding/json"
  8. "fmt"
  9. "math"
  10. "math/rand"
  11. "net"
  12. "os"
  13. "sync"
  14. "time"
  15. crypto "github.com/tendermint/go-crypto"
  16. cmn "github.com/tendermint/tmlibs/common"
  17. )
  18. const (
  19. // addresses under which the address manager will claim to need more addresses.
  20. needAddressThreshold = 1000
  21. // interval used to dump the address cache to disk for future use.
  22. dumpAddressInterval = time.Minute * 2
  23. // max addresses in each old address bucket.
  24. oldBucketSize = 64
  25. // buckets we split old addresses over.
  26. oldBucketCount = 64
  27. // max addresses in each new address bucket.
  28. newBucketSize = 64
  29. // buckets that we spread new addresses over.
  30. newBucketCount = 256
  31. // old buckets over which an address group will be spread.
  32. oldBucketsPerGroup = 4
  33. // new buckets over which a source address group will be spread.
  34. newBucketsPerGroup = 32
  35. // buckets a frequently seen new address may end up in.
  36. maxNewBucketsPerAddress = 4
  37. // days before which we assume an address has vanished
  38. // if we have not seen it announced in that long.
  39. numMissingDays = 30
  40. // tries without a single success before we assume an address is bad.
  41. numRetries = 3
  42. // max failures we will accept without a success before considering an address bad.
  43. maxFailures = 10
  44. // days since the last success before we will consider evicting an address.
  45. minBadDays = 7
  46. // % of total addresses known returned by GetSelection.
  47. getSelectionPercent = 23
  48. // min addresses that must be returned by GetSelection. Useful for bootstrapping.
  49. minGetSelection = 32
  50. // max addresses returned by GetSelection
  51. // NOTE: this must match "maxPexMessageSize"
  52. maxGetSelection = 250
  53. )
  54. const (
  55. bucketTypeNew = 0x01
  56. bucketTypeOld = 0x02
  57. )
  58. // AddrBook - concurrency safe peer address manager.
  59. type AddrBook struct {
  60. cmn.BaseService
  61. // immutable after creation
  62. filePath string
  63. routabilityStrict bool
  64. key string
  65. // accessed concurrently
  66. mtx sync.Mutex
  67. rand *rand.Rand
  68. ourAddrs map[string]*NetAddress
  69. addrLookup map[string]*knownAddress // new & old
  70. bucketsOld []map[string]*knownAddress
  71. bucketsNew []map[string]*knownAddress
  72. nOld int
  73. nNew int
  74. wg sync.WaitGroup
  75. }
  76. // NewAddrBook creates a new address book.
  77. // Use Start to begin processing asynchronous address updates.
  78. func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook {
  79. am := &AddrBook{
  80. rand: rand.New(rand.NewSource(time.Now().UnixNano())),
  81. ourAddrs: make(map[string]*NetAddress),
  82. addrLookup: make(map[string]*knownAddress),
  83. filePath: filePath,
  84. routabilityStrict: routabilityStrict,
  85. }
  86. am.init()
  87. am.BaseService = *cmn.NewBaseService(nil, "AddrBook", am)
  88. return am
  89. }
  90. // When modifying this, don't forget to update loadFromFile()
  91. func (a *AddrBook) init() {
  92. a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
  93. // New addr buckets
  94. a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
  95. for i := range a.bucketsNew {
  96. a.bucketsNew[i] = make(map[string]*knownAddress)
  97. }
  98. // Old addr buckets
  99. a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
  100. for i := range a.bucketsOld {
  101. a.bucketsOld[i] = make(map[string]*knownAddress)
  102. }
  103. }
  104. // OnStart implements Service.
  105. func (a *AddrBook) OnStart() error {
  106. if err := a.BaseService.OnStart(); err != nil {
  107. return err
  108. }
  109. a.loadFromFile(a.filePath)
  110. // wg.Add to ensure that any invocation of .Wait()
  111. // later on will wait for saveRoutine to terminate.
  112. a.wg.Add(1)
  113. go a.saveRoutine()
  114. return nil
  115. }
  116. // OnStop implements Service.
  117. func (a *AddrBook) OnStop() {
  118. a.BaseService.OnStop()
  119. }
  120. func (a *AddrBook) Wait() {
  121. a.wg.Wait()
  122. }
  123. // AddOurAddress adds another one of our addresses.
  124. func (a *AddrBook) AddOurAddress(addr *NetAddress) {
  125. a.mtx.Lock()
  126. defer a.mtx.Unlock()
  127. a.Logger.Info("Add our address to book", "addr", addr)
  128. a.ourAddrs[addr.String()] = addr
  129. }
  130. // OurAddresses returns a list of our addresses.
  131. func (a *AddrBook) OurAddresses() []*NetAddress {
  132. addrs := []*NetAddress{}
  133. for _, addr := range a.ourAddrs {
  134. addrs = append(addrs, addr)
  135. }
  136. return addrs
  137. }
  138. // AddAddress adds the given address as received from the given source.
  139. // NOTE: addr must not be nil
  140. func (a *AddrBook) AddAddress(addr *NetAddress, src *NetAddress) error {
  141. a.mtx.Lock()
  142. defer a.mtx.Unlock()
  143. return a.addAddress(addr, src)
  144. }
  145. // NeedMoreAddrs returns true if there are not have enough addresses in the book.
  146. func (a *AddrBook) NeedMoreAddrs() bool {
  147. return a.Size() < needAddressThreshold
  148. }
  149. // Size returns the number of addresses in the book.
  150. func (a *AddrBook) Size() int {
  151. a.mtx.Lock()
  152. defer a.mtx.Unlock()
  153. return a.size()
  154. }
  155. func (a *AddrBook) size() int {
  156. return a.nNew + a.nOld
  157. }
  158. // PickAddress picks an address to connect to.
  159. // The address is picked randomly from an old or new bucket according
  160. // to the newBias argument, which must be between [0, 100] (or else is truncated to that range)
  161. // and determines how biased we are to pick an address from a new bucket.
  162. // PickAddress returns nil if the AddrBook is empty or if we try to pick
  163. // from an empty bucket.
  164. func (a *AddrBook) PickAddress(newBias int) *NetAddress {
  165. a.mtx.Lock()
  166. defer a.mtx.Unlock()
  167. if a.size() == 0 {
  168. return nil
  169. }
  170. if newBias > 100 {
  171. newBias = 100
  172. }
  173. if newBias < 0 {
  174. newBias = 0
  175. }
  176. // Bias between new and old addresses.
  177. oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(newBias))
  178. newCorrelation := math.Sqrt(float64(a.nNew)) * float64(newBias)
  179. // pick a random peer from a random bucket
  180. var bucket map[string]*knownAddress
  181. pickFromOldBucket := (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation
  182. if (pickFromOldBucket && a.nOld == 0) ||
  183. (!pickFromOldBucket && a.nNew == 0) {
  184. return nil
  185. }
  186. // loop until we pick a random non-empty bucket
  187. for len(bucket) == 0 {
  188. if pickFromOldBucket {
  189. bucket = a.bucketsOld[a.rand.Intn(len(a.bucketsOld))]
  190. } else {
  191. bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
  192. }
  193. }
  194. // pick a random index and loop over the map to return that index
  195. randIndex := a.rand.Intn(len(bucket))
  196. for _, ka := range bucket {
  197. if randIndex == 0 {
  198. return ka.Addr
  199. }
  200. randIndex--
  201. }
  202. return nil
  203. }
  204. // MarkGood marks the peer as good and moves it into an "old" bucket.
  205. // XXX: we never call this!
  206. func (a *AddrBook) MarkGood(addr *NetAddress) {
  207. a.mtx.Lock()
  208. defer a.mtx.Unlock()
  209. ka := a.addrLookup[addr.String()]
  210. if ka == nil {
  211. return
  212. }
  213. ka.markGood()
  214. if ka.isNew() {
  215. a.moveToOld(ka)
  216. }
  217. }
  218. // MarkAttempt marks that an attempt was made to connect to the address.
  219. func (a *AddrBook) MarkAttempt(addr *NetAddress) {
  220. a.mtx.Lock()
  221. defer a.mtx.Unlock()
  222. ka := a.addrLookup[addr.String()]
  223. if ka == nil {
  224. return
  225. }
  226. ka.markAttempt()
  227. }
  228. // MarkBad currently just ejects the address. In the future, consider
  229. // blacklisting.
  230. func (a *AddrBook) MarkBad(addr *NetAddress) {
  231. a.RemoveAddress(addr)
  232. }
  233. // RemoveAddress removes the address from the book.
  234. func (a *AddrBook) RemoveAddress(addr *NetAddress) {
  235. a.mtx.Lock()
  236. defer a.mtx.Unlock()
  237. ka := a.addrLookup[addr.String()]
  238. if ka == nil {
  239. return
  240. }
  241. a.Logger.Info("Remove address from book", "addr", addr)
  242. a.removeFromAllBuckets(ka)
  243. }
  244. /* Peer exchange */
  245. // GetSelection randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  246. func (a *AddrBook) GetSelection() []*NetAddress {
  247. a.mtx.Lock()
  248. defer a.mtx.Unlock()
  249. if a.size() == 0 {
  250. return nil
  251. }
  252. allAddr := make([]*NetAddress, a.size())
  253. i := 0
  254. for _, v := range a.addrLookup {
  255. allAddr[i] = v.Addr
  256. i++
  257. }
  258. numAddresses := cmn.MaxInt(
  259. cmn.MinInt(minGetSelection, len(allAddr)),
  260. len(allAddr)*getSelectionPercent/100)
  261. numAddresses = cmn.MinInt(maxGetSelection, numAddresses)
  262. // Fisher-Yates shuffle the array. We only need to do the first
  263. // `numAddresses' since we are throwing the rest.
  264. // XXX: What's the point of this if we already loop randomly through addrLookup ?
  265. for i := 0; i < numAddresses; i++ {
  266. // pick a number between current index and the end
  267. j := rand.Intn(len(allAddr)-i) + i
  268. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  269. }
  270. // slice off the limit we are willing to share.
  271. return allAddr[:numAddresses]
  272. }
  273. /* Loading & Saving */
  274. type addrBookJSON struct {
  275. Key string
  276. Addrs []*knownAddress
  277. }
  278. func (a *AddrBook) saveToFile(filePath string) {
  279. a.Logger.Info("Saving AddrBook to file", "size", a.Size())
  280. a.mtx.Lock()
  281. defer a.mtx.Unlock()
  282. // Compile Addrs
  283. addrs := []*knownAddress{}
  284. for _, ka := range a.addrLookup {
  285. addrs = append(addrs, ka)
  286. }
  287. aJSON := &addrBookJSON{
  288. Key: a.key,
  289. Addrs: addrs,
  290. }
  291. jsonBytes, err := json.MarshalIndent(aJSON, "", "\t")
  292. if err != nil {
  293. a.Logger.Error("Failed to save AddrBook to file", "err", err)
  294. return
  295. }
  296. err = cmn.WriteFileAtomic(filePath, jsonBytes, 0644)
  297. if err != nil {
  298. a.Logger.Error("Failed to save AddrBook to file", "file", filePath, "err", err)
  299. }
  300. }
  301. // Returns false if file does not exist.
  302. // cmn.Panics if file is corrupt.
  303. func (a *AddrBook) loadFromFile(filePath string) bool {
  304. // If doesn't exist, do nothing.
  305. _, err := os.Stat(filePath)
  306. if os.IsNotExist(err) {
  307. return false
  308. }
  309. // Load addrBookJSON{}
  310. r, err := os.Open(filePath)
  311. if err != nil {
  312. cmn.PanicCrisis(cmn.Fmt("Error opening file %s: %v", filePath, err))
  313. }
  314. defer r.Close() // nolint: errcheck
  315. aJSON := &addrBookJSON{}
  316. dec := json.NewDecoder(r)
  317. err = dec.Decode(aJSON)
  318. if err != nil {
  319. cmn.PanicCrisis(cmn.Fmt("Error reading file %s: %v", filePath, err))
  320. }
  321. // Restore all the fields...
  322. // Restore the key
  323. a.key = aJSON.Key
  324. // Restore .bucketsNew & .bucketsOld
  325. for _, ka := range aJSON.Addrs {
  326. for _, bucketIndex := range ka.Buckets {
  327. bucket := a.getBucket(ka.BucketType, bucketIndex)
  328. bucket[ka.Addr.String()] = ka
  329. }
  330. a.addrLookup[ka.Addr.String()] = ka
  331. if ka.BucketType == bucketTypeNew {
  332. a.nNew++
  333. } else {
  334. a.nOld++
  335. }
  336. }
  337. return true
  338. }
  339. // Save saves the book.
  340. func (a *AddrBook) Save() {
  341. a.Logger.Info("Saving AddrBook to file", "size", a.Size())
  342. a.saveToFile(a.filePath)
  343. }
  344. /* Private methods */
  345. func (a *AddrBook) saveRoutine() {
  346. defer a.wg.Done()
  347. saveFileTicker := time.NewTicker(dumpAddressInterval)
  348. out:
  349. for {
  350. select {
  351. case <-saveFileTicker.C:
  352. a.saveToFile(a.filePath)
  353. case <-a.Quit:
  354. break out
  355. }
  356. }
  357. saveFileTicker.Stop()
  358. a.saveToFile(a.filePath)
  359. a.Logger.Info("Address handler done")
  360. }
  361. func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  362. switch bucketType {
  363. case bucketTypeNew:
  364. return a.bucketsNew[bucketIdx]
  365. case bucketTypeOld:
  366. return a.bucketsOld[bucketIdx]
  367. default:
  368. cmn.PanicSanity("Should not happen")
  369. return nil
  370. }
  371. }
  372. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  373. // NOTE: currently it always returns true.
  374. func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool {
  375. // Sanity check
  376. if ka.isOld() {
  377. a.Logger.Error(cmn.Fmt("Cannot add address already in old bucket to a new bucket: %v", ka))
  378. return false
  379. }
  380. addrStr := ka.Addr.String()
  381. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  382. // Already exists?
  383. if _, ok := bucket[addrStr]; ok {
  384. return true
  385. }
  386. // Enforce max addresses.
  387. if len(bucket) > newBucketSize {
  388. a.Logger.Info("new bucket is full, expiring old ")
  389. a.expireNew(bucketIdx)
  390. }
  391. // Add to bucket.
  392. bucket[addrStr] = ka
  393. if ka.addBucketRef(bucketIdx) == 1 {
  394. a.nNew++
  395. }
  396. // Ensure in addrLookup
  397. a.addrLookup[addrStr] = ka
  398. return true
  399. }
  400. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  401. func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  402. // Sanity check
  403. if ka.isNew() {
  404. a.Logger.Error(cmn.Fmt("Cannot add new address to old bucket: %v", ka))
  405. return false
  406. }
  407. if len(ka.Buckets) != 0 {
  408. a.Logger.Error(cmn.Fmt("Cannot add already old address to another old bucket: %v", ka))
  409. return false
  410. }
  411. addrStr := ka.Addr.String()
  412. bucket := a.getBucket(bucketTypeOld, bucketIdx)
  413. // Already exists?
  414. if _, ok := bucket[addrStr]; ok {
  415. return true
  416. }
  417. // Enforce max addresses.
  418. if len(bucket) > oldBucketSize {
  419. return false
  420. }
  421. // Add to bucket.
  422. bucket[addrStr] = ka
  423. if ka.addBucketRef(bucketIdx) == 1 {
  424. a.nOld++
  425. }
  426. // Ensure in addrLookup
  427. a.addrLookup[addrStr] = ka
  428. return true
  429. }
  430. func (a *AddrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  431. if ka.BucketType != bucketType {
  432. a.Logger.Error(cmn.Fmt("Bucket type mismatch: %v", ka))
  433. return
  434. }
  435. bucket := a.getBucket(bucketType, bucketIdx)
  436. delete(bucket, ka.Addr.String())
  437. if ka.removeBucketRef(bucketIdx) == 0 {
  438. if bucketType == bucketTypeNew {
  439. a.nNew--
  440. } else {
  441. a.nOld--
  442. }
  443. delete(a.addrLookup, ka.Addr.String())
  444. }
  445. }
  446. func (a *AddrBook) removeFromAllBuckets(ka *knownAddress) {
  447. for _, bucketIdx := range ka.Buckets {
  448. bucket := a.getBucket(ka.BucketType, bucketIdx)
  449. delete(bucket, ka.Addr.String())
  450. }
  451. ka.Buckets = nil
  452. if ka.BucketType == bucketTypeNew {
  453. a.nNew--
  454. } else {
  455. a.nOld--
  456. }
  457. delete(a.addrLookup, ka.Addr.String())
  458. }
  459. func (a *AddrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  460. bucket := a.getBucket(bucketType, bucketIdx)
  461. var oldest *knownAddress
  462. for _, ka := range bucket {
  463. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  464. oldest = ka
  465. }
  466. }
  467. return oldest
  468. }
  469. func (a *AddrBook) addAddress(addr, src *NetAddress) error {
  470. if a.routabilityStrict && !addr.Routable() {
  471. return fmt.Errorf("Cannot add non-routable address %v", addr)
  472. }
  473. if _, ok := a.ourAddrs[addr.String()]; ok {
  474. // Ignore our own listener address.
  475. return fmt.Errorf("Cannot add ourselves with address %v", addr)
  476. }
  477. ka := a.addrLookup[addr.String()]
  478. if ka != nil {
  479. // Already old.
  480. if ka.isOld() {
  481. return nil
  482. }
  483. // Already in max new buckets.
  484. if len(ka.Buckets) == maxNewBucketsPerAddress {
  485. return nil
  486. }
  487. // The more entries we have, the less likely we are to add more.
  488. factor := int32(2 * len(ka.Buckets))
  489. if a.rand.Int31n(factor) != 0 {
  490. return nil
  491. }
  492. } else {
  493. ka = newKnownAddress(addr, src)
  494. }
  495. bucket := a.calcNewBucket(addr, src)
  496. a.addToNewBucket(ka, bucket)
  497. a.Logger.Info("Added new address", "address", addr, "total", a.size())
  498. return nil
  499. }
  500. // Make space in the new buckets by expiring the really bad entries.
  501. // If no bad entries are available we remove the oldest.
  502. func (a *AddrBook) expireNew(bucketIdx int) {
  503. for addrStr, ka := range a.bucketsNew[bucketIdx] {
  504. // If an entry is bad, throw it away
  505. if ka.isBad() {
  506. a.Logger.Info(cmn.Fmt("expiring bad address %v", addrStr))
  507. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  508. return
  509. }
  510. }
  511. // If we haven't thrown out a bad entry, throw out the oldest entry
  512. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  513. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  514. }
  515. // Promotes an address from new to old.
  516. // TODO: Move to old probabilistically.
  517. // The better a node is, the less likely it should be evicted from an old bucket.
  518. func (a *AddrBook) moveToOld(ka *knownAddress) {
  519. // Sanity check
  520. if ka.isOld() {
  521. a.Logger.Error(cmn.Fmt("Cannot promote address that is already old %v", ka))
  522. return
  523. }
  524. if len(ka.Buckets) == 0 {
  525. a.Logger.Error(cmn.Fmt("Cannot promote address that isn't in any new buckets %v", ka))
  526. return
  527. }
  528. // Remember one of the buckets in which ka is in.
  529. freedBucket := ka.Buckets[0]
  530. // Remove from all (new) buckets.
  531. a.removeFromAllBuckets(ka)
  532. // It's officially old now.
  533. ka.BucketType = bucketTypeOld
  534. // Try to add it to its oldBucket destination.
  535. oldBucketIdx := a.calcOldBucket(ka.Addr)
  536. added := a.addToOldBucket(ka, oldBucketIdx)
  537. if !added {
  538. // No room, must evict something
  539. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  540. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  541. // Find new bucket to put oldest in
  542. newBucketIdx := a.calcNewBucket(oldest.Addr, oldest.Src)
  543. added := a.addToNewBucket(oldest, newBucketIdx)
  544. // No space in newBucket either, just put it in freedBucket from above.
  545. if !added {
  546. added := a.addToNewBucket(oldest, freedBucket)
  547. if !added {
  548. a.Logger.Error(cmn.Fmt("Could not migrate oldest %v to freedBucket %v", oldest, freedBucket))
  549. }
  550. }
  551. // Finally, add to bucket again.
  552. added = a.addToOldBucket(ka, oldBucketIdx)
  553. if !added {
  554. a.Logger.Error(cmn.Fmt("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  555. }
  556. }
  557. }
  558. // doublesha256( key + sourcegroup +
  559. // int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets
  560. func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
  561. data1 := []byte{}
  562. data1 = append(data1, []byte(a.key)...)
  563. data1 = append(data1, []byte(a.groupKey(addr))...)
  564. data1 = append(data1, []byte(a.groupKey(src))...)
  565. hash1 := doubleSha256(data1)
  566. hash64 := binary.BigEndian.Uint64(hash1)
  567. hash64 %= newBucketsPerGroup
  568. var hashbuf [8]byte
  569. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  570. data2 := []byte{}
  571. data2 = append(data2, []byte(a.key)...)
  572. data2 = append(data2, a.groupKey(src)...)
  573. data2 = append(data2, hashbuf[:]...)
  574. hash2 := doubleSha256(data2)
  575. return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  576. }
  577. // doublesha256( key + group +
  578. // int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets
  579. func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
  580. data1 := []byte{}
  581. data1 = append(data1, []byte(a.key)...)
  582. data1 = append(data1, []byte(addr.String())...)
  583. hash1 := doubleSha256(data1)
  584. hash64 := binary.BigEndian.Uint64(hash1)
  585. hash64 %= oldBucketsPerGroup
  586. var hashbuf [8]byte
  587. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  588. data2 := []byte{}
  589. data2 = append(data2, []byte(a.key)...)
  590. data2 = append(data2, a.groupKey(addr)...)
  591. data2 = append(data2, hashbuf[:]...)
  592. hash2 := doubleSha256(data2)
  593. return int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  594. }
  595. // Return a string representing the network group of this address.
  596. // This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
  597. // "local" for a local address and the string "unroutable" for an unroutable
  598. // address.
  599. func (a *AddrBook) groupKey(na *NetAddress) string {
  600. if a.routabilityStrict && na.Local() {
  601. return "local"
  602. }
  603. if a.routabilityStrict && !na.Routable() {
  604. return "unroutable"
  605. }
  606. if ipv4 := na.IP.To4(); ipv4 != nil {
  607. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(16, 32)}).String()
  608. }
  609. if na.RFC6145() || na.RFC6052() {
  610. // last four bytes are the ip address
  611. ip := net.IP(na.IP[12:16])
  612. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  613. }
  614. if na.RFC3964() {
  615. ip := net.IP(na.IP[2:7])
  616. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  617. }
  618. if na.RFC4380() {
  619. // teredo tunnels have the last 4 bytes as the v4 address XOR
  620. // 0xff.
  621. ip := net.IP(make([]byte, 4))
  622. for i, byte := range na.IP[12:16] {
  623. ip[i] = byte ^ 0xff
  624. }
  625. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  626. }
  627. // OK, so now we know ourselves to be a IPv6 address.
  628. // bitcoind uses /32 for everything, except for Hurricane Electric's
  629. // (he.net) IP range, which it uses /36 for.
  630. bits := 32
  631. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"),
  632. Mask: net.CIDRMask(32, 128)}
  633. if heNet.Contains(na.IP) {
  634. bits = 36
  635. }
  636. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(bits, 128)}).String()
  637. }
  638. //-----------------------------------------------------------------------------
  639. /*
  640. knownAddress
  641. tracks information about a known network address that is used
  642. to determine how viable an address is.
  643. */
  644. type knownAddress struct {
  645. Addr *NetAddress
  646. Src *NetAddress
  647. Attempts int32
  648. LastAttempt time.Time
  649. LastSuccess time.Time
  650. BucketType byte
  651. Buckets []int
  652. }
  653. func newKnownAddress(addr *NetAddress, src *NetAddress) *knownAddress {
  654. return &knownAddress{
  655. Addr: addr,
  656. Src: src,
  657. Attempts: 0,
  658. LastAttempt: time.Now(),
  659. BucketType: bucketTypeNew,
  660. Buckets: nil,
  661. }
  662. }
  663. func (ka *knownAddress) isOld() bool {
  664. return ka.BucketType == bucketTypeOld
  665. }
  666. func (ka *knownAddress) isNew() bool {
  667. return ka.BucketType == bucketTypeNew
  668. }
  669. func (ka *knownAddress) markAttempt() {
  670. now := time.Now()
  671. ka.LastAttempt = now
  672. ka.Attempts += 1
  673. }
  674. func (ka *knownAddress) markGood() {
  675. now := time.Now()
  676. ka.LastAttempt = now
  677. ka.Attempts = 0
  678. ka.LastSuccess = now
  679. }
  680. func (ka *knownAddress) addBucketRef(bucketIdx int) int {
  681. for _, bucket := range ka.Buckets {
  682. if bucket == bucketIdx {
  683. // TODO refactor to return error?
  684. // log.Warn(Fmt("Bucket already exists in ka.Buckets: %v", ka))
  685. return -1
  686. }
  687. }
  688. ka.Buckets = append(ka.Buckets, bucketIdx)
  689. return len(ka.Buckets)
  690. }
  691. func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
  692. buckets := []int{}
  693. for _, bucket := range ka.Buckets {
  694. if bucket != bucketIdx {
  695. buckets = append(buckets, bucket)
  696. }
  697. }
  698. if len(buckets) != len(ka.Buckets)-1 {
  699. // TODO refactor to return error?
  700. // log.Warn(Fmt("bucketIdx not found in ka.Buckets: %v", ka))
  701. return -1
  702. }
  703. ka.Buckets = buckets
  704. return len(ka.Buckets)
  705. }
  706. /*
  707. An address is bad if the address in question is a New address, has not been tried in the last
  708. minute, and meets one of the following criteria:
  709. 1) It claims to be from the future
  710. 2) It hasn't been seen in over a month
  711. 3) It has failed at least three times and never succeeded
  712. 4) It has failed ten times in the last week
  713. All addresses that meet these criteria are assumed to be worthless and not
  714. worth keeping hold of.
  715. XXX: so a good peer needs us to call MarkGood before the conditions above are reached!
  716. */
  717. func (ka *knownAddress) isBad() bool {
  718. // Is Old --> good
  719. if ka.BucketType == bucketTypeOld {
  720. return false
  721. }
  722. // Has been attempted in the last minute --> good
  723. if ka.LastAttempt.Before(time.Now().Add(-1 * time.Minute)) {
  724. return false
  725. }
  726. // Too old?
  727. // XXX: does this mean if we've kept a connection up for this long we'll disconnect?!
  728. // and shouldn't it be .Before ?
  729. if ka.LastAttempt.After(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
  730. return true
  731. }
  732. // Never succeeded?
  733. if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
  734. return true
  735. }
  736. // Hasn't succeeded in too long?
  737. // XXX: does this mean if we've kept a connection up for this long we'll disconnect?!
  738. if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
  739. ka.Attempts >= maxFailures {
  740. return true
  741. }
  742. return false
  743. }