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.

637 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
5 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
5 years ago
9 years ago
9 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
7 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
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
7 years ago
7 years ago
7 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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.ID)
  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.ID)
  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.ID)
  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(
  277. "got more good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)",
  278. got,
  279. expected,
  280. good,
  281. len(selection),
  282. )
  283. }
  284. if got < expected-slack {
  285. t.Fatalf(
  286. "got fewer good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)",
  287. got,
  288. expected,
  289. good,
  290. len(selection),
  291. )
  292. }
  293. }
  294. func TestAddrBookHasAddress(t *testing.T) {
  295. fname := createTempFileName("addrbook_test")
  296. defer deleteTempFile(fname)
  297. book := NewAddrBook(fname, true)
  298. book.SetLogger(log.TestingLogger())
  299. addr := randIPv4Address(t)
  300. book.AddAddress(addr, addr)
  301. assert.True(t, book.HasAddress(addr))
  302. book.RemoveAddress(addr)
  303. assert.False(t, book.HasAddress(addr))
  304. }
  305. func testCreatePrivateAddrs(t *testing.T, numAddrs int) ([]*p2p.NetAddress, []string) {
  306. addrs := make([]*p2p.NetAddress, numAddrs)
  307. for i := 0; i < numAddrs; i++ {
  308. addrs[i] = randIPv4Address(t)
  309. }
  310. private := make([]string, numAddrs)
  311. for i, addr := range addrs {
  312. private[i] = string(addr.ID)
  313. }
  314. return addrs, private
  315. }
  316. func TestAddrBookEmpty(t *testing.T) {
  317. fname := createTempFileName("addrbook_test")
  318. defer deleteTempFile(fname)
  319. book := NewAddrBook(fname, true)
  320. book.SetLogger(log.TestingLogger())
  321. // Check that empty book is empty
  322. require.True(t, book.Empty())
  323. // Check that book with our address is empty
  324. book.AddOurAddress(randIPv4Address(t))
  325. require.True(t, book.Empty())
  326. // Check that book with private addrs is empty
  327. _, privateIds := testCreatePrivateAddrs(t, 5)
  328. book.AddPrivateIDs(privateIds)
  329. require.True(t, book.Empty())
  330. // Check that book with address is not empty
  331. book.AddAddress(randIPv4Address(t), randIPv4Address(t))
  332. require.False(t, book.Empty())
  333. }
  334. func TestPrivatePeers(t *testing.T) {
  335. fname := createTempFileName("addrbook_test")
  336. defer deleteTempFile(fname)
  337. book := NewAddrBook(fname, true)
  338. book.SetLogger(log.TestingLogger())
  339. addrs, private := testCreatePrivateAddrs(t, 10)
  340. book.AddPrivateIDs(private)
  341. // private addrs must not be added
  342. for _, addr := range addrs {
  343. err := book.AddAddress(addr, addr)
  344. if assert.Error(t, err) {
  345. _, ok := err.(ErrAddrBookPrivate)
  346. assert.True(t, ok)
  347. }
  348. }
  349. // addrs coming from private peers must not be added
  350. err := book.AddAddress(randIPv4Address(t), addrs[0])
  351. if assert.Error(t, err) {
  352. _, ok := err.(ErrAddrBookPrivateSrc)
  353. assert.True(t, ok)
  354. }
  355. }
  356. func testAddrBookAddressSelection(t *testing.T, bookSize int) {
  357. // generate all combinations of old (m) and new addresses
  358. for nBookOld := 0; nBookOld <= bookSize; nBookOld++ {
  359. nBookNew := bookSize - nBookOld
  360. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nBookNew, nBookOld)
  361. // create book and get selection
  362. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nBookOld, nBookNew)
  363. defer deleteTempFile(fname)
  364. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  365. assert.NotNil(t, addrs, "%s - expected a non-nil selection", dbgStr)
  366. nAddrs := len(addrs)
  367. assert.NotZero(t, nAddrs, "%s - expected at least one address in selection", dbgStr)
  368. // check there's no nil addresses
  369. for _, addr := range addrs {
  370. if addr == nil {
  371. t.Fatalf("%s - got nil address in selection %v", dbgStr, addrs)
  372. }
  373. }
  374. // XXX: shadowing
  375. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  376. // Given:
  377. // n - num new addrs, m - num old addrs
  378. // k - num new addrs expected in the beginning (based on bias %)
  379. // i=min(n, max(k,r-m)), aka expNew
  380. // j=min(m, r-i), aka expOld
  381. //
  382. // We expect this layout:
  383. // indices: 0...i-1 i...i+j-1
  384. // addresses: N0..Ni-1 O0..Oj-1
  385. //
  386. // There is at least one partition and at most three.
  387. var (
  388. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  389. expNew = cmn.MinInt(nNew, cmn.MaxInt(k, nAddrs-nBookOld))
  390. expOld = cmn.MinInt(nOld, nAddrs-expNew)
  391. )
  392. // Verify that the number of old and new addresses are as expected
  393. if nNew != expNew {
  394. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  395. }
  396. if nOld != expOld {
  397. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  398. }
  399. // Verify that the order of addresses is as expected
  400. // Get the sequence types and lengths of the selection
  401. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  402. assert.NoError(t, err, "%s", dbgStr)
  403. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  404. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  405. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  406. var expSeqLens []int
  407. var expSeqTypes []int
  408. switch {
  409. case expOld == 0: // all new addresses
  410. expSeqLens = []int{nAddrs}
  411. expSeqTypes = []int{1}
  412. case expNew == 0: // all old addresses
  413. expSeqLens = []int{nAddrs}
  414. expSeqTypes = []int{2}
  415. case nAddrs-expNew-expOld == 0: // new addresses, old addresses
  416. expSeqLens = []int{expNew, expOld}
  417. expSeqTypes = []int{1, 2}
  418. }
  419. assert.Equal(t, expSeqLens, seqLens,
  420. "%s - expected sequence lengths of old/new %v, got %v",
  421. dbgStr, expSeqLens, seqLens)
  422. assert.Equal(t, expSeqTypes, seqTypes,
  423. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  424. dbgStr, expSeqTypes, seqTypes)
  425. }
  426. }
  427. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  428. // test books with smaller size, < N
  429. const N = 32
  430. for bookSize := 1; bookSize < N; bookSize++ {
  431. testAddrBookAddressSelection(t, bookSize)
  432. }
  433. // Test for two books with sizes from following ranges
  434. ranges := [...][]int{{33, 100}, {100, 175}}
  435. bookSizes := make([]int, 0, len(ranges))
  436. for _, r := range ranges {
  437. bookSizes = append(bookSizes, cmn.RandIntn(r[1]-r[0])+r[0])
  438. }
  439. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  440. for _, bookSize := range bookSizes {
  441. testAddrBookAddressSelection(t, bookSize)
  442. }
  443. }
  444. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  445. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  446. assert.Equal(t, m, nOld, "old addresses")
  447. assert.Equal(t, n, nNew, "new addresses")
  448. }
  449. func createTempFileName(prefix string) string {
  450. f, err := ioutil.TempFile("", prefix)
  451. if err != nil {
  452. panic(err)
  453. }
  454. fname := f.Name()
  455. err = f.Close()
  456. if err != nil {
  457. panic(err)
  458. }
  459. return fname
  460. }
  461. func deleteTempFile(fname string) {
  462. err := os.Remove(fname)
  463. if err != nil {
  464. panic(err)
  465. }
  466. }
  467. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  468. fname = createTempFileName("addrbook_test")
  469. book = NewAddrBook(fname, true)
  470. book.SetLogger(log.TestingLogger())
  471. assert.Zero(t, book.Size())
  472. randAddrs := randNetAddressPairs(t, nOld)
  473. for _, addr := range randAddrs {
  474. book.AddAddress(addr.addr, addr.src)
  475. book.MarkGood(addr.addr.ID)
  476. }
  477. randAddrs = randNetAddressPairs(t, nNew)
  478. for _, addr := range randAddrs {
  479. book.AddAddress(addr.addr, addr.src)
  480. }
  481. return
  482. }
  483. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  484. for _, addr := range addrs {
  485. if book.IsGood(addr) {
  486. nOld++
  487. } else {
  488. nNew++
  489. }
  490. }
  491. return
  492. }
  493. // Analyse the layout of the selection specified by 'addrs'
  494. // Returns:
  495. // - seqLens - the lengths of the sequences of addresses of same type
  496. // - seqTypes - the types of sequences in selection
  497. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  498. // address types are: 0 - nil, 1 - new, 2 - old
  499. var (
  500. prevType = 0
  501. currentSeqLen = 0
  502. )
  503. for _, addr := range addrs {
  504. addrType := 0
  505. if book.IsGood(addr) {
  506. addrType = 2
  507. } else {
  508. addrType = 1
  509. }
  510. if addrType != prevType && prevType != 0 {
  511. seqLens = append(seqLens, currentSeqLen)
  512. seqTypes = append(seqTypes, prevType)
  513. currentSeqLen = 0
  514. }
  515. currentSeqLen++
  516. prevType = addrType
  517. }
  518. seqLens = append(seqLens, currentSeqLen)
  519. seqTypes = append(seqTypes, prevType)
  520. return
  521. }