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.

636 lines
17 KiB

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