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.

815 lines
20 KiB

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