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.

630 lines
17 KiB

9 years ago
9 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
9 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 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
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
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
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
  1. package pex
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "io/ioutil"
  6. "math"
  7. "os"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. cmn "github.com/tendermint/tendermint/libs/common"
  12. "github.com/tendermint/tendermint/libs/log"
  13. "github.com/tendermint/tendermint/p2p"
  14. )
  15. func TestAddrBookPickAddress(t *testing.T) {
  16. fname := createTempFileName("addrbook_test")
  17. defer deleteTempFile(fname)
  18. // 0 addresses
  19. book := NewAddrBook(fname, true)
  20. book.SetLogger(log.TestingLogger())
  21. assert.Zero(t, book.Size())
  22. addr := book.PickAddress(50)
  23. assert.Nil(t, addr, "expected no address")
  24. randAddrs := randNetAddressPairs(t, 1)
  25. addrSrc := randAddrs[0]
  26. book.AddAddress(addrSrc.addr, addrSrc.src)
  27. // pick an address when we only have new address
  28. addr = book.PickAddress(0)
  29. assert.NotNil(t, addr, "expected an address")
  30. addr = book.PickAddress(50)
  31. assert.NotNil(t, addr, "expected an address")
  32. addr = book.PickAddress(100)
  33. assert.NotNil(t, addr, "expected an address")
  34. // pick an address when we only have old address
  35. book.MarkGood(addrSrc.addr)
  36. addr = book.PickAddress(0)
  37. assert.NotNil(t, addr, "expected an address")
  38. addr = book.PickAddress(50)
  39. assert.NotNil(t, addr, "expected an address")
  40. // in this case, nNew==0 but we biased 100% to new, so we return nil
  41. addr = book.PickAddress(100)
  42. assert.Nil(t, addr, "did not expected an address")
  43. }
  44. func TestAddrBookSaveLoad(t *testing.T) {
  45. fname := createTempFileName("addrbook_test")
  46. defer deleteTempFile(fname)
  47. // 0 addresses
  48. book := NewAddrBook(fname, true)
  49. book.SetLogger(log.TestingLogger())
  50. book.saveToFile(fname)
  51. book = NewAddrBook(fname, true)
  52. book.SetLogger(log.TestingLogger())
  53. book.loadFromFile(fname)
  54. assert.Zero(t, book.Size())
  55. // 100 addresses
  56. randAddrs := randNetAddressPairs(t, 100)
  57. for _, addrSrc := range randAddrs {
  58. book.AddAddress(addrSrc.addr, addrSrc.src)
  59. }
  60. assert.Equal(t, 100, book.Size())
  61. book.saveToFile(fname)
  62. book = NewAddrBook(fname, true)
  63. book.SetLogger(log.TestingLogger())
  64. book.loadFromFile(fname)
  65. assert.Equal(t, 100, book.Size())
  66. }
  67. func TestAddrBookLookup(t *testing.T) {
  68. fname := createTempFileName("addrbook_test")
  69. defer deleteTempFile(fname)
  70. randAddrs := randNetAddressPairs(t, 100)
  71. book := NewAddrBook(fname, true)
  72. book.SetLogger(log.TestingLogger())
  73. for _, addrSrc := range randAddrs {
  74. addr := addrSrc.addr
  75. src := addrSrc.src
  76. book.AddAddress(addr, src)
  77. ka := book.addrLookup[addr.ID]
  78. assert.NotNil(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  79. if !(ka.Addr.Equals(addr) && ka.Src.Equals(src)) {
  80. t.Fatalf("KnownAddress doesn't match addr & src")
  81. }
  82. }
  83. }
  84. func TestAddrBookPromoteToOld(t *testing.T) {
  85. fname := createTempFileName("addrbook_test")
  86. defer deleteTempFile(fname)
  87. randAddrs := randNetAddressPairs(t, 100)
  88. book := NewAddrBook(fname, true)
  89. book.SetLogger(log.TestingLogger())
  90. for _, addrSrc := range randAddrs {
  91. book.AddAddress(addrSrc.addr, addrSrc.src)
  92. }
  93. // Attempt all addresses.
  94. for _, addrSrc := range randAddrs {
  95. book.MarkAttempt(addrSrc.addr)
  96. }
  97. // Promote half of them
  98. for i, addrSrc := range randAddrs {
  99. if i%2 == 0 {
  100. book.MarkGood(addrSrc.addr)
  101. }
  102. }
  103. // TODO: do more testing :)
  104. selection := book.GetSelection()
  105. t.Logf("selection: %v", selection)
  106. if len(selection) > book.Size() {
  107. t.Errorf("selection could not be bigger than the book")
  108. }
  109. selection = book.GetSelectionWithBias(30)
  110. t.Logf("selection: %v", selection)
  111. if len(selection) > book.Size() {
  112. t.Errorf("selection with bias could not be bigger than the book")
  113. }
  114. assert.Equal(t, book.Size(), 100, "expecting book size to be 100")
  115. }
  116. func TestAddrBookHandlesDuplicates(t *testing.T) {
  117. fname := createTempFileName("addrbook_test")
  118. defer deleteTempFile(fname)
  119. book := NewAddrBook(fname, true)
  120. book.SetLogger(log.TestingLogger())
  121. randAddrs := randNetAddressPairs(t, 100)
  122. differentSrc := randIPv4Address(t)
  123. for _, addrSrc := range randAddrs {
  124. book.AddAddress(addrSrc.addr, addrSrc.src)
  125. book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  126. book.AddAddress(addrSrc.addr, differentSrc) // different src
  127. }
  128. assert.Equal(t, 100, book.Size())
  129. }
  130. type netAddressPair struct {
  131. addr *p2p.NetAddress
  132. src *p2p.NetAddress
  133. }
  134. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  135. randAddrs := make([]netAddressPair, n)
  136. for i := 0; i < n; i++ {
  137. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  138. }
  139. return randAddrs
  140. }
  141. func randIPv4Address(t *testing.T) *p2p.NetAddress {
  142. for {
  143. ip := fmt.Sprintf("%v.%v.%v.%v",
  144. cmn.RandIntn(254)+1,
  145. cmn.RandIntn(255),
  146. cmn.RandIntn(255),
  147. cmn.RandIntn(255),
  148. )
  149. port := cmn.RandIntn(65535-1) + 1
  150. id := p2p.ID(hex.EncodeToString(cmn.RandBytes(p2p.IDByteLength)))
  151. idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
  152. addr, err := p2p.NewNetAddressString(idAddr)
  153. assert.Nil(t, err, "error generating rand network address")
  154. if addr.Routable() {
  155. return addr
  156. }
  157. }
  158. }
  159. func TestAddrBookRemoveAddress(t *testing.T) {
  160. fname := createTempFileName("addrbook_test")
  161. defer deleteTempFile(fname)
  162. book := NewAddrBook(fname, true)
  163. book.SetLogger(log.TestingLogger())
  164. addr := randIPv4Address(t)
  165. book.AddAddress(addr, addr)
  166. assert.Equal(t, 1, book.Size())
  167. book.RemoveAddress(addr)
  168. assert.Equal(t, 0, book.Size())
  169. nonExistingAddr := randIPv4Address(t)
  170. book.RemoveAddress(nonExistingAddr)
  171. assert.Equal(t, 0, book.Size())
  172. }
  173. func TestAddrBookGetSelectionWithOneMarkedGood(t *testing.T) {
  174. // create a book with 10 addresses, 1 good/old and 9 new
  175. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 1, 9)
  176. defer deleteTempFile(fname)
  177. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  178. assert.NotNil(t, addrs)
  179. assertMOldAndNNewAddrsInSelection(t, 1, 9, addrs, book)
  180. }
  181. func TestAddrBookGetSelectionWithOneNotMarkedGood(t *testing.T) {
  182. // create a book with 10 addresses, 9 good/old and 1 new
  183. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 9, 1)
  184. defer deleteTempFile(fname)
  185. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  186. assert.NotNil(t, addrs)
  187. assertMOldAndNNewAddrsInSelection(t, 9, 1, addrs, book)
  188. }
  189. func TestAddrBookGetSelectionReturnsNilWhenAddrBookIsEmpty(t *testing.T) {
  190. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 0, 0)
  191. defer deleteTempFile(fname)
  192. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  193. assert.Nil(t, addrs)
  194. }
  195. func TestAddrBookGetSelection(t *testing.T) {
  196. fname := createTempFileName("addrbook_test")
  197. defer deleteTempFile(fname)
  198. book := NewAddrBook(fname, true)
  199. book.SetLogger(log.TestingLogger())
  200. // 1) empty book
  201. assert.Empty(t, book.GetSelection())
  202. // 2) add one address
  203. addr := randIPv4Address(t)
  204. book.AddAddress(addr, addr)
  205. assert.Equal(t, 1, len(book.GetSelection()))
  206. assert.Equal(t, addr, book.GetSelection()[0])
  207. // 3) add a bunch of addresses
  208. randAddrs := randNetAddressPairs(t, 100)
  209. for _, addrSrc := range randAddrs {
  210. book.AddAddress(addrSrc.addr, addrSrc.src)
  211. }
  212. // check there is no duplicates
  213. addrs := make(map[string]*p2p.NetAddress)
  214. selection := book.GetSelection()
  215. for _, addr := range selection {
  216. if dup, ok := addrs[addr.String()]; ok {
  217. t.Fatalf("selection %v contains duplicates %v", selection, dup)
  218. }
  219. addrs[addr.String()] = addr
  220. }
  221. if len(selection) > book.Size() {
  222. t.Errorf("selection %v could not be bigger than the book", selection)
  223. }
  224. }
  225. func TestAddrBookGetSelectionWithBias(t *testing.T) {
  226. const biasTowardsNewAddrs = 30
  227. fname := createTempFileName("addrbook_test")
  228. defer deleteTempFile(fname)
  229. book := NewAddrBook(fname, true)
  230. book.SetLogger(log.TestingLogger())
  231. // 1) empty book
  232. selection := book.GetSelectionWithBias(biasTowardsNewAddrs)
  233. assert.Empty(t, selection)
  234. // 2) add one address
  235. addr := randIPv4Address(t)
  236. book.AddAddress(addr, addr)
  237. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  238. assert.Equal(t, 1, len(selection))
  239. assert.Equal(t, addr, selection[0])
  240. // 3) add a bunch of addresses
  241. randAddrs := randNetAddressPairs(t, 100)
  242. for _, addrSrc := range randAddrs {
  243. book.AddAddress(addrSrc.addr, addrSrc.src)
  244. }
  245. // check there is no duplicates
  246. addrs := make(map[string]*p2p.NetAddress)
  247. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  248. for _, addr := range selection {
  249. if dup, ok := addrs[addr.String()]; ok {
  250. t.Fatalf("selection %v contains duplicates %v", selection, dup)
  251. }
  252. addrs[addr.String()] = addr
  253. }
  254. if len(selection) > book.Size() {
  255. t.Fatalf("selection %v could not be bigger than the book", selection)
  256. }
  257. // 4) mark 80% of the addresses as good
  258. randAddrsLen := len(randAddrs)
  259. for i, addrSrc := range randAddrs {
  260. if int((float64(i)/float64(randAddrsLen))*100) >= 20 {
  261. book.MarkGood(addrSrc.addr)
  262. }
  263. }
  264. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  265. // check that ~70% of addresses returned are good
  266. good := 0
  267. for _, addr := range selection {
  268. if book.IsGood(addr) {
  269. good++
  270. }
  271. }
  272. got, expected := int((float64(good)/float64(len(selection)))*100), (100 - biasTowardsNewAddrs)
  273. // compute some slack to protect against small differences due to rounding:
  274. slack := int(math.Round(float64(100) / float64(len(selection))))
  275. if got > expected+slack {
  276. t.Fatalf("got more good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)", got, expected, good, len(selection))
  277. }
  278. if got < expected-slack {
  279. t.Fatalf("got fewer good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)", got, expected, good, len(selection))
  280. }
  281. }
  282. func TestAddrBookHasAddress(t *testing.T) {
  283. fname := createTempFileName("addrbook_test")
  284. defer deleteTempFile(fname)
  285. book := NewAddrBook(fname, true)
  286. book.SetLogger(log.TestingLogger())
  287. addr := randIPv4Address(t)
  288. book.AddAddress(addr, addr)
  289. assert.True(t, book.HasAddress(addr))
  290. book.RemoveAddress(addr)
  291. assert.False(t, book.HasAddress(addr))
  292. }
  293. func testCreatePrivateAddrs(t *testing.T, numAddrs int) ([]*p2p.NetAddress, []string) {
  294. addrs := make([]*p2p.NetAddress, numAddrs)
  295. for i := 0; i < numAddrs; i++ {
  296. addrs[i] = randIPv4Address(t)
  297. }
  298. private := make([]string, numAddrs)
  299. for i, addr := range addrs {
  300. private[i] = string(addr.ID)
  301. }
  302. return addrs, private
  303. }
  304. func TestAddrBookEmpty(t *testing.T) {
  305. fname := createTempFileName("addrbook_test")
  306. defer deleteTempFile(fname)
  307. book := NewAddrBook(fname, true)
  308. book.SetLogger(log.TestingLogger())
  309. // Check that empty book is empty
  310. require.True(t, book.Empty())
  311. // Check that book with our address is empty
  312. book.AddOurAddress(randIPv4Address(t))
  313. require.True(t, book.Empty())
  314. // Check that book with private addrs is empty
  315. _, privateIds := testCreatePrivateAddrs(t, 5)
  316. book.AddPrivateIDs(privateIds)
  317. require.True(t, book.Empty())
  318. // Check that book with address is not empty
  319. book.AddAddress(randIPv4Address(t), randIPv4Address(t))
  320. require.False(t, book.Empty())
  321. }
  322. func TestPrivatePeers(t *testing.T) {
  323. fname := createTempFileName("addrbook_test")
  324. defer deleteTempFile(fname)
  325. book := NewAddrBook(fname, true)
  326. book.SetLogger(log.TestingLogger())
  327. addrs, private := testCreatePrivateAddrs(t, 10)
  328. book.AddPrivateIDs(private)
  329. // private addrs must not be added
  330. for _, addr := range addrs {
  331. err := book.AddAddress(addr, addr)
  332. if assert.Error(t, err) {
  333. _, ok := err.(ErrAddrBookPrivate)
  334. assert.True(t, ok)
  335. }
  336. }
  337. // addrs coming from private peers must not be added
  338. err := book.AddAddress(randIPv4Address(t), addrs[0])
  339. if assert.Error(t, err) {
  340. _, ok := err.(ErrAddrBookPrivateSrc)
  341. assert.True(t, ok)
  342. }
  343. }
  344. func testAddrBookAddressSelection(t *testing.T, bookSize int) {
  345. // generate all combinations of old (m) and new addresses
  346. for nOld := 0; nOld <= bookSize; nOld++ {
  347. nNew := bookSize - nOld
  348. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nNew, nOld)
  349. // create book and get selection
  350. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nOld, nNew)
  351. defer deleteTempFile(fname)
  352. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  353. assert.NotNil(t, addrs, "%s - expected a non-nil selection", dbgStr)
  354. nAddrs := len(addrs)
  355. assert.NotZero(t, nAddrs, "%s - expected at least one address in selection", dbgStr)
  356. // check there's no nil addresses
  357. for _, addr := range addrs {
  358. if addr == nil {
  359. t.Fatalf("%s - got nil address in selection %v", dbgStr, addrs)
  360. }
  361. }
  362. // XXX: shadowing
  363. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  364. // Given:
  365. // n - num new addrs, m - num old addrs
  366. // k - num new addrs expected in the beginning (based on bias %)
  367. // i=min(n, k), aka expFirstNew
  368. // j=min(m, r-i), aka expOld
  369. //
  370. // We expect this layout:
  371. // indices: 0...i-1 i...i+j-1 i+j...r
  372. // addresses: N0..Ni-1 O0..Oj-1 Ni...
  373. //
  374. // There is at least one partition and at most three.
  375. var (
  376. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  377. expFirstNew = cmn.MinInt(nNew, k)
  378. expOld = cmn.MinInt(nOld, nAddrs-expFirstNew)
  379. expNew = nAddrs - expOld
  380. expLastNew = expNew - expFirstNew
  381. )
  382. // Verify that the number of old and new addresses are as expected
  383. if nNew < expNew || nNew > expNew {
  384. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  385. }
  386. if nOld < expOld || nOld > expOld {
  387. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  388. }
  389. // Verify that the order of addresses is as expected
  390. // Get the sequence types and lengths of the selection
  391. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  392. assert.NoError(t, err, "%s", dbgStr)
  393. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  394. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  395. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  396. var expSeqLens []int
  397. var expSeqTypes []int
  398. switch {
  399. case expOld == 0: // all new addresses
  400. expSeqLens = []int{nAddrs}
  401. expSeqTypes = []int{1}
  402. case expFirstNew == 0: // all old addresses
  403. expSeqLens = []int{nAddrs}
  404. expSeqTypes = []int{2}
  405. case nAddrs-expFirstNew-expOld == 0: // new addresses, old addresses
  406. expSeqLens = []int{expFirstNew, expOld}
  407. expSeqTypes = []int{1, 2}
  408. default: // new addresses, old addresses, new addresses
  409. expSeqLens = []int{expFirstNew, expOld, expLastNew}
  410. expSeqTypes = []int{1, 2, 1}
  411. }
  412. assert.Equal(t, expSeqLens, seqLens,
  413. "%s - expected sequence lengths of old/new %v, got %v",
  414. dbgStr, expSeqLens, seqLens)
  415. assert.Equal(t, expSeqTypes, seqTypes,
  416. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  417. dbgStr, expSeqTypes, seqTypes)
  418. }
  419. }
  420. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  421. // test books with smaller size, < N
  422. const N = 32
  423. for bookSize := 1; bookSize < N; bookSize++ {
  424. testAddrBookAddressSelection(t, bookSize)
  425. }
  426. // Test for two books with sizes from following ranges
  427. ranges := [...][]int{{33, 100}, {100, 175}}
  428. var bookSizes []int
  429. for _, r := range ranges {
  430. bookSizes = append(bookSizes, cmn.RandIntn(r[1]-r[0])+r[0])
  431. }
  432. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  433. for _, bookSize := range bookSizes {
  434. testAddrBookAddressSelection(t, bookSize)
  435. }
  436. }
  437. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  438. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  439. assert.Equal(t, m, nOld, "old addresses")
  440. assert.Equal(t, n, nNew, "new addresses")
  441. }
  442. func createTempFileName(prefix string) string {
  443. f, err := ioutil.TempFile("", prefix)
  444. if err != nil {
  445. panic(err)
  446. }
  447. fname := f.Name()
  448. err = f.Close()
  449. if err != nil {
  450. panic(err)
  451. }
  452. return fname
  453. }
  454. func deleteTempFile(fname string) {
  455. err := os.Remove(fname)
  456. if err != nil {
  457. panic(err)
  458. }
  459. }
  460. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  461. fname = createTempFileName("addrbook_test")
  462. book = NewAddrBook(fname, true)
  463. book.SetLogger(log.TestingLogger())
  464. assert.Zero(t, book.Size())
  465. randAddrs := randNetAddressPairs(t, nOld)
  466. for _, addr := range randAddrs {
  467. book.AddAddress(addr.addr, addr.src)
  468. book.MarkGood(addr.addr)
  469. }
  470. randAddrs = randNetAddressPairs(t, nNew)
  471. for _, addr := range randAddrs {
  472. book.AddAddress(addr.addr, addr.src)
  473. }
  474. return
  475. }
  476. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  477. for _, addr := range addrs {
  478. if book.IsGood(addr) {
  479. nOld++
  480. } else {
  481. nNew++
  482. }
  483. }
  484. return
  485. }
  486. // Analyse the layout of the selection specified by 'addrs'
  487. // Returns:
  488. // - seqLens - the lengths of the sequences of addresses of same type
  489. // - seqTypes - the types of sequences in selection
  490. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  491. // address types are: 0 - nil, 1 - new, 2 - old
  492. var (
  493. prevType = 0
  494. currentSeqLen = 0
  495. )
  496. for _, addr := range addrs {
  497. addrType := 0
  498. if book.IsGood(addr) {
  499. addrType = 2
  500. } else {
  501. addrType = 1
  502. }
  503. if addrType != prevType && prevType != 0 {
  504. seqLens = append(seqLens, currentSeqLen)
  505. seqTypes = append(seqTypes, prevType)
  506. currentSeqLen = 0
  507. }
  508. currentSeqLen++
  509. prevType = addrType
  510. }
  511. seqLens = append(seqLens, currentSeqLen)
  512. seqTypes = append(seqTypes, prevType)
  513. return
  514. }