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.

625 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
  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("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 nBookOld := 0; nBookOld <= bookSize; nBookOld++ {
  347. nBookNew := bookSize - nBookOld
  348. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nBookNew, nBookOld)
  349. // create book and get selection
  350. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nBookOld, nBookNew)
  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, max(k,r-m)), aka expNew
  368. // j=min(m, r-i), aka expOld
  369. //
  370. // We expect this layout:
  371. // indices: 0...i-1 i...i+j-1
  372. // addresses: N0..Ni-1 O0..Oj-1
  373. //
  374. // There is at least one partition and at most three.
  375. var (
  376. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  377. expNew = cmn.MinInt(nNew, cmn.MaxInt(k, nAddrs-nBookOld))
  378. expOld = cmn.MinInt(nOld, nAddrs-expNew)
  379. )
  380. // Verify that the number of old and new addresses are as expected
  381. if nNew != expNew {
  382. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  383. }
  384. if nOld != expOld {
  385. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  386. }
  387. // Verify that the order of addresses is as expected
  388. // Get the sequence types and lengths of the selection
  389. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  390. assert.NoError(t, err, "%s", dbgStr)
  391. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  392. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  393. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  394. var expSeqLens []int
  395. var expSeqTypes []int
  396. switch {
  397. case expOld == 0: // all new addresses
  398. expSeqLens = []int{nAddrs}
  399. expSeqTypes = []int{1}
  400. case expNew == 0: // all old addresses
  401. expSeqLens = []int{nAddrs}
  402. expSeqTypes = []int{2}
  403. case nAddrs-expNew-expOld == 0: // new addresses, old addresses
  404. expSeqLens = []int{expNew, expOld}
  405. expSeqTypes = []int{1, 2}
  406. }
  407. assert.Equal(t, expSeqLens, seqLens,
  408. "%s - expected sequence lengths of old/new %v, got %v",
  409. dbgStr, expSeqLens, seqLens)
  410. assert.Equal(t, expSeqTypes, seqTypes,
  411. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  412. dbgStr, expSeqTypes, seqTypes)
  413. }
  414. }
  415. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  416. // test books with smaller size, < N
  417. const N = 32
  418. for bookSize := 1; bookSize < N; bookSize++ {
  419. testAddrBookAddressSelection(t, bookSize)
  420. }
  421. // Test for two books with sizes from following ranges
  422. ranges := [...][]int{{33, 100}, {100, 175}}
  423. bookSizes := make([]int, 0, len(ranges))
  424. for _, r := range ranges {
  425. bookSizes = append(bookSizes, cmn.RandIntn(r[1]-r[0])+r[0])
  426. }
  427. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  428. for _, bookSize := range bookSizes {
  429. testAddrBookAddressSelection(t, bookSize)
  430. }
  431. }
  432. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  433. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  434. assert.Equal(t, m, nOld, "old addresses")
  435. assert.Equal(t, n, nNew, "new addresses")
  436. }
  437. func createTempFileName(prefix string) string {
  438. f, err := ioutil.TempFile("", prefix)
  439. if err != nil {
  440. panic(err)
  441. }
  442. fname := f.Name()
  443. err = f.Close()
  444. if err != nil {
  445. panic(err)
  446. }
  447. return fname
  448. }
  449. func deleteTempFile(fname string) {
  450. err := os.Remove(fname)
  451. if err != nil {
  452. panic(err)
  453. }
  454. }
  455. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  456. fname = createTempFileName("addrbook_test")
  457. book = NewAddrBook(fname, true)
  458. book.SetLogger(log.TestingLogger())
  459. assert.Zero(t, book.Size())
  460. randAddrs := randNetAddressPairs(t, nOld)
  461. for _, addr := range randAddrs {
  462. book.AddAddress(addr.addr, addr.src)
  463. book.MarkGood(addr.addr.ID)
  464. }
  465. randAddrs = randNetAddressPairs(t, nNew)
  466. for _, addr := range randAddrs {
  467. book.AddAddress(addr.addr, addr.src)
  468. }
  469. return
  470. }
  471. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  472. for _, addr := range addrs {
  473. if book.IsGood(addr) {
  474. nOld++
  475. } else {
  476. nNew++
  477. }
  478. }
  479. return
  480. }
  481. // Analyse the layout of the selection specified by 'addrs'
  482. // Returns:
  483. // - seqLens - the lengths of the sequences of addresses of same type
  484. // - seqTypes - the types of sequences in selection
  485. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  486. // address types are: 0 - nil, 1 - new, 2 - old
  487. var (
  488. prevType = 0
  489. currentSeqLen = 0
  490. )
  491. for _, addr := range addrs {
  492. addrType := 0
  493. if book.IsGood(addr) {
  494. addrType = 2
  495. } else {
  496. addrType = 1
  497. }
  498. if addrType != prevType && prevType != 0 {
  499. seqLens = append(seqLens, currentSeqLen)
  500. seqTypes = append(seqTypes, prevType)
  501. currentSeqLen = 0
  502. }
  503. currentSeqLen++
  504. prevType = addrType
  505. }
  506. seqLens = append(seqLens, currentSeqLen)
  507. seqTypes = append(seqTypes, prevType)
  508. return
  509. }