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.

664 lines
18 KiB

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