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.

661 lines
17 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
6 years ago
  1. package pex
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/libs/log"
  8. tmmath "github.com/tendermint/tendermint/libs/math"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. "github.com/tendermint/tendermint/p2p"
  11. "io/ioutil"
  12. "math"
  13. "os"
  14. "testing"
  15. "time"
  16. )
  17. // FIXME These tests should not rely on .(*addrBook) assertions
  18. func TestAddrBookPickAddress(t *testing.T) {
  19. fname := createTempFileName("addrbook_test")
  20. defer deleteTempFile(fname)
  21. // 0 addresses
  22. book := NewAddrBook(fname, true)
  23. book.SetLogger(log.TestingLogger())
  24. assert.Zero(t, book.Size())
  25. addr := book.PickAddress(50)
  26. assert.Nil(t, addr, "expected no address")
  27. randAddrs := randNetAddressPairs(t, 1)
  28. addrSrc := randAddrs[0]
  29. book.AddAddress(addrSrc.addr, addrSrc.src)
  30. // pick an address when we only have new address
  31. addr = book.PickAddress(0)
  32. assert.NotNil(t, addr, "expected an address")
  33. addr = book.PickAddress(50)
  34. assert.NotNil(t, addr, "expected an address")
  35. addr = book.PickAddress(100)
  36. assert.NotNil(t, addr, "expected an address")
  37. // pick an address when we only have old address
  38. book.MarkGood(addrSrc.addr.ID)
  39. addr = book.PickAddress(0)
  40. assert.NotNil(t, addr, "expected an address")
  41. addr = book.PickAddress(50)
  42. assert.NotNil(t, addr, "expected an address")
  43. // in this case, nNew==0 but we biased 100% to new, so we return nil
  44. addr = book.PickAddress(100)
  45. assert.Nil(t, addr, "did not expected an address")
  46. }
  47. func TestAddrBookSaveLoad(t *testing.T) {
  48. fname := createTempFileName("addrbook_test")
  49. defer deleteTempFile(fname)
  50. // 0 addresses
  51. book := NewAddrBook(fname, true)
  52. book.SetLogger(log.TestingLogger())
  53. book.Save()
  54. book = NewAddrBook(fname, true)
  55. book.SetLogger(log.TestingLogger())
  56. book.Start()
  57. assert.True(t, book.Empty())
  58. // 100 addresses
  59. randAddrs := randNetAddressPairs(t, 100)
  60. for _, addrSrc := range randAddrs {
  61. book.AddAddress(addrSrc.addr, addrSrc.src)
  62. }
  63. assert.Equal(t, 100, book.Size())
  64. book.Save()
  65. book = NewAddrBook(fname, true)
  66. book.SetLogger(log.TestingLogger())
  67. book.Start()
  68. assert.Equal(t, 100, book.Size())
  69. }
  70. func TestAddrBookLookup(t *testing.T) {
  71. fname := createTempFileName("addrbook_test")
  72. defer deleteTempFile(fname)
  73. randAddrs := randNetAddressPairs(t, 100)
  74. book := NewAddrBook(fname, true)
  75. book.SetLogger(log.TestingLogger())
  76. for _, addrSrc := range randAddrs {
  77. addr := addrSrc.addr
  78. src := addrSrc.src
  79. book.AddAddress(addr, src)
  80. ka := book.HasAddress(addr)
  81. assert.True(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  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. tmrand.Intn(254)+1,
  145. tmrand.Intn(255),
  146. tmrand.Intn(255),
  147. tmrand.Intn(255),
  148. )
  149. port := tmrand.Intn(65535-1) + 1
  150. id := p2p.ID(hex.EncodeToString(tmrand.Bytes(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 TestBanBadPeers(t *testing.T) {
  317. fname := createTempFileName("addrbook_test")
  318. defer deleteTempFile(fname)
  319. book := NewAddrBook(fname, true)
  320. book.SetLogger(log.TestingLogger())
  321. addr := randIPv4Address(t)
  322. _ = book.AddAddress(addr, addr)
  323. book.MarkBad(addr, 1*time.Second)
  324. // addr should not reachable
  325. assert.False(t, book.HasAddress(addr))
  326. err := book.AddAddress(addr, addr)
  327. // book should not add address from the blacklist
  328. assert.Error(t, err)
  329. time.Sleep(1 * time.Second)
  330. book.ReinstateBadPeers()
  331. // address should be reinstated in the new bucket
  332. assert.EqualValues(t, 1, book.Size())
  333. assert.True(t, book.HasAddress(addr))
  334. assert.False(t, book.IsGood(addr))
  335. }
  336. func TestAddrBookEmpty(t *testing.T) {
  337. fname := createTempFileName("addrbook_test")
  338. defer deleteTempFile(fname)
  339. book := NewAddrBook(fname, true)
  340. book.SetLogger(log.TestingLogger())
  341. // Check that empty book is empty
  342. require.True(t, book.Empty())
  343. // Check that book with our address is empty
  344. book.AddOurAddress(randIPv4Address(t))
  345. require.True(t, book.Empty())
  346. // Check that book with private addrs is empty
  347. _, privateIds := testCreatePrivateAddrs(t, 5)
  348. book.AddPrivateIDs(privateIds)
  349. require.True(t, book.Empty())
  350. // Check that book with address is not empty
  351. book.AddAddress(randIPv4Address(t), randIPv4Address(t))
  352. require.False(t, book.Empty())
  353. }
  354. func TestPrivatePeers(t *testing.T) {
  355. fname := createTempFileName("addrbook_test")
  356. defer deleteTempFile(fname)
  357. book := NewAddrBook(fname, true)
  358. book.SetLogger(log.TestingLogger())
  359. addrs, private := testCreatePrivateAddrs(t, 10)
  360. book.AddPrivateIDs(private)
  361. // private addrs must not be added
  362. for _, addr := range addrs {
  363. err := book.AddAddress(addr, addr)
  364. if assert.Error(t, err) {
  365. _, ok := err.(ErrAddrBookPrivate)
  366. assert.True(t, ok)
  367. }
  368. }
  369. // addrs coming from private peers must not be added
  370. err := book.AddAddress(randIPv4Address(t), addrs[0])
  371. if assert.Error(t, err) {
  372. _, ok := err.(ErrAddrBookPrivateSrc)
  373. assert.True(t, ok)
  374. }
  375. }
  376. func testAddrBookAddressSelection(t *testing.T, bookSize int) {
  377. // generate all combinations of old (m) and new addresses
  378. for nBookOld := 0; nBookOld <= bookSize; nBookOld++ {
  379. nBookNew := bookSize - nBookOld
  380. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nBookNew, nBookOld)
  381. // create book and get selection
  382. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nBookOld, nBookNew)
  383. defer deleteTempFile(fname)
  384. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  385. assert.NotNil(t, addrs, "%s - expected a non-nil selection", dbgStr)
  386. nAddrs := len(addrs)
  387. assert.NotZero(t, nAddrs, "%s - expected at least one address in selection", dbgStr)
  388. // check there's no nil addresses
  389. for _, addr := range addrs {
  390. if addr == nil {
  391. t.Fatalf("%s - got nil address in selection %v", dbgStr, addrs)
  392. }
  393. }
  394. // XXX: shadowing
  395. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  396. // Given:
  397. // n - num new addrs, m - num old addrs
  398. // k - num new addrs expected in the beginning (based on bias %)
  399. // i=min(n, max(k,r-m)), aka expNew
  400. // j=min(m, r-i), aka expOld
  401. //
  402. // We expect this layout:
  403. // indices: 0...i-1 i...i+j-1
  404. // addresses: N0..Ni-1 O0..Oj-1
  405. //
  406. // There is at least one partition and at most three.
  407. var (
  408. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  409. expNew = tmmath.MinInt(nNew, tmmath.MaxInt(k, nAddrs-nBookOld))
  410. expOld = tmmath.MinInt(nOld, nAddrs-expNew)
  411. )
  412. // Verify that the number of old and new addresses are as expected
  413. if nNew != expNew {
  414. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  415. }
  416. if nOld != expOld {
  417. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  418. }
  419. // Verify that the order of addresses is as expected
  420. // Get the sequence types and lengths of the selection
  421. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  422. assert.NoError(t, err, "%s", dbgStr)
  423. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  424. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  425. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  426. var expSeqLens []int
  427. var expSeqTypes []int
  428. switch {
  429. case expOld == 0: // all new addresses
  430. expSeqLens = []int{nAddrs}
  431. expSeqTypes = []int{1}
  432. case expNew == 0: // all old addresses
  433. expSeqLens = []int{nAddrs}
  434. expSeqTypes = []int{2}
  435. case nAddrs-expNew-expOld == 0: // new addresses, old addresses
  436. expSeqLens = []int{expNew, expOld}
  437. expSeqTypes = []int{1, 2}
  438. }
  439. assert.Equal(t, expSeqLens, seqLens,
  440. "%s - expected sequence lengths of old/new %v, got %v",
  441. dbgStr, expSeqLens, seqLens)
  442. assert.Equal(t, expSeqTypes, seqTypes,
  443. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  444. dbgStr, expSeqTypes, seqTypes)
  445. }
  446. }
  447. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  448. // test books with smaller size, < N
  449. const N = 32
  450. for bookSize := 1; bookSize < N; bookSize++ {
  451. testAddrBookAddressSelection(t, bookSize)
  452. }
  453. // Test for two books with sizes from following ranges
  454. ranges := [...][]int{{33, 100}, {100, 175}}
  455. bookSizes := make([]int, 0, len(ranges))
  456. for _, r := range ranges {
  457. bookSizes = append(bookSizes, tmrand.Intn(r[1]-r[0])+r[0])
  458. }
  459. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  460. for _, bookSize := range bookSizes {
  461. testAddrBookAddressSelection(t, bookSize)
  462. }
  463. }
  464. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  465. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  466. assert.Equal(t, m, nOld, "old addresses")
  467. assert.Equal(t, n, nNew, "new addresses")
  468. }
  469. func createTempFileName(prefix string) string {
  470. f, err := ioutil.TempFile("", prefix)
  471. if err != nil {
  472. panic(err)
  473. }
  474. fname := f.Name()
  475. err = f.Close()
  476. if err != nil {
  477. panic(err)
  478. }
  479. return fname
  480. }
  481. func deleteTempFile(fname string) {
  482. err := os.Remove(fname)
  483. if err != nil {
  484. panic(err)
  485. }
  486. }
  487. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  488. fname = createTempFileName("addrbook_test")
  489. book = NewAddrBook(fname, true).(*addrBook)
  490. book.SetLogger(log.TestingLogger())
  491. assert.Zero(t, book.Size())
  492. randAddrs := randNetAddressPairs(t, nOld)
  493. for _, addr := range randAddrs {
  494. book.AddAddress(addr.addr, addr.src)
  495. book.MarkGood(addr.addr.ID)
  496. }
  497. randAddrs = randNetAddressPairs(t, nNew)
  498. for _, addr := range randAddrs {
  499. book.AddAddress(addr.addr, addr.src)
  500. }
  501. return
  502. }
  503. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  504. for _, addr := range addrs {
  505. if book.IsGood(addr) {
  506. nOld++
  507. } else {
  508. nNew++
  509. }
  510. }
  511. return
  512. }
  513. // Analyse the layout of the selection specified by 'addrs'
  514. // Returns:
  515. // - seqLens - the lengths of the sequences of addresses of same type
  516. // - seqTypes - the types of sequences in selection
  517. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  518. // address types are: 0 - nil, 1 - new, 2 - old
  519. var (
  520. prevType = 0
  521. currentSeqLen = 0
  522. )
  523. for _, addr := range addrs {
  524. addrType := 0
  525. if book.IsGood(addr) {
  526. addrType = 2
  527. } else {
  528. addrType = 1
  529. }
  530. if addrType != prevType && prevType != 0 {
  531. seqLens = append(seqLens, currentSeqLen)
  532. seqTypes = append(seqTypes, prevType)
  533. currentSeqLen = 0
  534. }
  535. currentSeqLen++
  536. prevType = addrType
  537. }
  538. seqLens = append(seqLens, currentSeqLen)
  539. seqTypes = append(seqTypes, prevType)
  540. return
  541. }