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.

640 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).(*addrBook)
  51. book.SetLogger(log.TestingLogger())
  52. book.saveToFile(fname)
  53. book = NewAddrBook(fname, true).(*addrBook)
  54. book.SetLogger(log.TestingLogger())
  55. book.loadFromFile(fname)
  56. assert.Zero(t, book.Size())
  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.saveToFile(fname)
  64. book = NewAddrBook(fname, true).(*addrBook)
  65. book.SetLogger(log.TestingLogger())
  66. book.loadFromFile(fname)
  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).(*addrBook)
  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.addrLookup[addr.ID]
  80. assert.NotNil(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  81. if !(ka.Addr.Equals(addr) && ka.Src.Equals(src)) {
  82. t.Fatalf("KnownAddress doesn't match addr & src")
  83. }
  84. }
  85. }
  86. func TestAddrBookPromoteToOld(t *testing.T) {
  87. fname := createTempFileName("addrbook_test")
  88. defer deleteTempFile(fname)
  89. randAddrs := randNetAddressPairs(t, 100)
  90. book := NewAddrBook(fname, true)
  91. book.SetLogger(log.TestingLogger())
  92. for _, addrSrc := range randAddrs {
  93. book.AddAddress(addrSrc.addr, addrSrc.src)
  94. }
  95. // Attempt all addresses.
  96. for _, addrSrc := range randAddrs {
  97. book.MarkAttempt(addrSrc.addr)
  98. }
  99. // Promote half of them
  100. for i, addrSrc := range randAddrs {
  101. if i%2 == 0 {
  102. book.MarkGood(addrSrc.addr.ID)
  103. }
  104. }
  105. // TODO: do more testing :)
  106. selection := book.GetSelection()
  107. t.Logf("selection: %v", selection)
  108. if len(selection) > book.Size() {
  109. t.Errorf("selection could not be bigger than the book")
  110. }
  111. selection = book.GetSelectionWithBias(30)
  112. t.Logf("selection: %v", selection)
  113. if len(selection) > book.Size() {
  114. t.Errorf("selection with bias could not be bigger than the book")
  115. }
  116. assert.Equal(t, book.Size(), 100, "expecting book size to be 100")
  117. }
  118. func TestAddrBookHandlesDuplicates(t *testing.T) {
  119. fname := createTempFileName("addrbook_test")
  120. defer deleteTempFile(fname)
  121. book := NewAddrBook(fname, true)
  122. book.SetLogger(log.TestingLogger())
  123. randAddrs := randNetAddressPairs(t, 100)
  124. differentSrc := randIPv4Address(t)
  125. for _, addrSrc := range randAddrs {
  126. book.AddAddress(addrSrc.addr, addrSrc.src)
  127. book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  128. book.AddAddress(addrSrc.addr, differentSrc) // different src
  129. }
  130. assert.Equal(t, 100, book.Size())
  131. }
  132. type netAddressPair struct {
  133. addr *p2p.NetAddress
  134. src *p2p.NetAddress
  135. }
  136. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  137. randAddrs := make([]netAddressPair, n)
  138. for i := 0; i < n; i++ {
  139. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  140. }
  141. return randAddrs
  142. }
  143. func randIPv4Address(t *testing.T) *p2p.NetAddress {
  144. for {
  145. ip := fmt.Sprintf("%v.%v.%v.%v",
  146. tmrand.Intn(254)+1,
  147. tmrand.Intn(255),
  148. tmrand.Intn(255),
  149. tmrand.Intn(255),
  150. )
  151. port := tmrand.Intn(65535-1) + 1
  152. id := p2p.ID(hex.EncodeToString(tmrand.Bytes(p2p.IDByteLength)))
  153. idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
  154. addr, err := p2p.NewNetAddressString(idAddr)
  155. assert.Nil(t, err, "error generating rand network address")
  156. if addr.Routable() {
  157. return addr
  158. }
  159. }
  160. }
  161. func TestAddrBookRemoveAddress(t *testing.T) {
  162. fname := createTempFileName("addrbook_test")
  163. defer deleteTempFile(fname)
  164. book := NewAddrBook(fname, true)
  165. book.SetLogger(log.TestingLogger())
  166. addr := randIPv4Address(t)
  167. book.AddAddress(addr, addr)
  168. assert.Equal(t, 1, book.Size())
  169. book.RemoveAddress(addr)
  170. assert.Equal(t, 0, book.Size())
  171. nonExistingAddr := randIPv4Address(t)
  172. book.RemoveAddress(nonExistingAddr)
  173. assert.Equal(t, 0, book.Size())
  174. }
  175. func TestAddrBookGetSelectionWithOneMarkedGood(t *testing.T) {
  176. // create a book with 10 addresses, 1 good/old and 9 new
  177. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 1, 9)
  178. defer deleteTempFile(fname)
  179. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  180. assert.NotNil(t, addrs)
  181. assertMOldAndNNewAddrsInSelection(t, 1, 9, addrs, book)
  182. }
  183. func TestAddrBookGetSelectionWithOneNotMarkedGood(t *testing.T) {
  184. // create a book with 10 addresses, 9 good/old and 1 new
  185. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 9, 1)
  186. defer deleteTempFile(fname)
  187. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  188. assert.NotNil(t, addrs)
  189. assertMOldAndNNewAddrsInSelection(t, 9, 1, addrs, book)
  190. }
  191. func TestAddrBookGetSelectionReturnsNilWhenAddrBookIsEmpty(t *testing.T) {
  192. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 0, 0)
  193. defer deleteTempFile(fname)
  194. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  195. assert.Nil(t, addrs)
  196. }
  197. func TestAddrBookGetSelection(t *testing.T) {
  198. fname := createTempFileName("addrbook_test")
  199. defer deleteTempFile(fname)
  200. book := NewAddrBook(fname, true)
  201. book.SetLogger(log.TestingLogger())
  202. // 1) empty book
  203. assert.Empty(t, book.GetSelection())
  204. // 2) add one address
  205. addr := randIPv4Address(t)
  206. book.AddAddress(addr, addr)
  207. assert.Equal(t, 1, len(book.GetSelection()))
  208. assert.Equal(t, addr, book.GetSelection()[0])
  209. // 3) add a bunch of addresses
  210. randAddrs := randNetAddressPairs(t, 100)
  211. for _, addrSrc := range randAddrs {
  212. book.AddAddress(addrSrc.addr, addrSrc.src)
  213. }
  214. // check there is no duplicates
  215. addrs := make(map[string]*p2p.NetAddress)
  216. selection := book.GetSelection()
  217. for _, addr := range selection {
  218. if dup, ok := addrs[addr.String()]; ok {
  219. t.Fatalf("selection %v contains duplicates %v", selection, dup)
  220. }
  221. addrs[addr.String()] = addr
  222. }
  223. if len(selection) > book.Size() {
  224. t.Errorf("selection %v could not be bigger than the book", selection)
  225. }
  226. }
  227. func TestAddrBookGetSelectionWithBias(t *testing.T) {
  228. const biasTowardsNewAddrs = 30
  229. fname := createTempFileName("addrbook_test")
  230. defer deleteTempFile(fname)
  231. book := NewAddrBook(fname, true)
  232. book.SetLogger(log.TestingLogger())
  233. // 1) empty book
  234. selection := book.GetSelectionWithBias(biasTowardsNewAddrs)
  235. assert.Empty(t, selection)
  236. // 2) add one address
  237. addr := randIPv4Address(t)
  238. book.AddAddress(addr, addr)
  239. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  240. assert.Equal(t, 1, len(selection))
  241. assert.Equal(t, addr, selection[0])
  242. // 3) add a bunch of addresses
  243. randAddrs := randNetAddressPairs(t, 100)
  244. for _, addrSrc := range randAddrs {
  245. book.AddAddress(addrSrc.addr, addrSrc.src)
  246. }
  247. // check there is no duplicates
  248. addrs := make(map[string]*p2p.NetAddress)
  249. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  250. for _, addr := range selection {
  251. if dup, ok := addrs[addr.String()]; ok {
  252. t.Fatalf("selection %v contains duplicates %v", selection, dup)
  253. }
  254. addrs[addr.String()] = addr
  255. }
  256. if len(selection) > book.Size() {
  257. t.Fatalf("selection %v could not be bigger than the book", selection)
  258. }
  259. // 4) mark 80% of the addresses as good
  260. randAddrsLen := len(randAddrs)
  261. for i, addrSrc := range randAddrs {
  262. if int((float64(i)/float64(randAddrsLen))*100) >= 20 {
  263. book.MarkGood(addrSrc.addr.ID)
  264. }
  265. }
  266. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  267. // check that ~70% of addresses returned are good
  268. good := 0
  269. for _, addr := range selection {
  270. if book.IsGood(addr) {
  271. good++
  272. }
  273. }
  274. got, expected := int((float64(good)/float64(len(selection)))*100), (100 - biasTowardsNewAddrs)
  275. // compute some slack to protect against small differences due to rounding:
  276. slack := int(math.Round(float64(100) / float64(len(selection))))
  277. if got > expected+slack {
  278. t.Fatalf(
  279. "got more good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)",
  280. got,
  281. expected,
  282. good,
  283. len(selection),
  284. )
  285. }
  286. if got < expected-slack {
  287. t.Fatalf(
  288. "got fewer good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)",
  289. got,
  290. expected,
  291. good,
  292. len(selection),
  293. )
  294. }
  295. }
  296. func TestAddrBookHasAddress(t *testing.T) {
  297. fname := createTempFileName("addrbook_test")
  298. defer deleteTempFile(fname)
  299. book := NewAddrBook(fname, true)
  300. book.SetLogger(log.TestingLogger())
  301. addr := randIPv4Address(t)
  302. book.AddAddress(addr, addr)
  303. assert.True(t, book.HasAddress(addr))
  304. book.RemoveAddress(addr)
  305. assert.False(t, book.HasAddress(addr))
  306. }
  307. func testCreatePrivateAddrs(t *testing.T, numAddrs int) ([]*p2p.NetAddress, []string) {
  308. addrs := make([]*p2p.NetAddress, numAddrs)
  309. for i := 0; i < numAddrs; i++ {
  310. addrs[i] = randIPv4Address(t)
  311. }
  312. private := make([]string, numAddrs)
  313. for i, addr := range addrs {
  314. private[i] = string(addr.ID)
  315. }
  316. return addrs, private
  317. }
  318. func TestAddrBookEmpty(t *testing.T) {
  319. fname := createTempFileName("addrbook_test")
  320. defer deleteTempFile(fname)
  321. book := NewAddrBook(fname, true)
  322. book.SetLogger(log.TestingLogger())
  323. // Check that empty book is empty
  324. require.True(t, book.Empty())
  325. // Check that book with our address is empty
  326. book.AddOurAddress(randIPv4Address(t))
  327. require.True(t, book.Empty())
  328. // Check that book with private addrs is empty
  329. _, privateIds := testCreatePrivateAddrs(t, 5)
  330. book.AddPrivateIDs(privateIds)
  331. require.True(t, book.Empty())
  332. // Check that book with address is not empty
  333. book.AddAddress(randIPv4Address(t), randIPv4Address(t))
  334. require.False(t, book.Empty())
  335. }
  336. func TestPrivatePeers(t *testing.T) {
  337. fname := createTempFileName("addrbook_test")
  338. defer deleteTempFile(fname)
  339. book := NewAddrBook(fname, true)
  340. book.SetLogger(log.TestingLogger())
  341. addrs, private := testCreatePrivateAddrs(t, 10)
  342. book.AddPrivateIDs(private)
  343. // private addrs must not be added
  344. for _, addr := range addrs {
  345. err := book.AddAddress(addr, addr)
  346. if assert.Error(t, err) {
  347. _, ok := err.(ErrAddrBookPrivate)
  348. assert.True(t, ok)
  349. }
  350. }
  351. // addrs coming from private peers must not be added
  352. err := book.AddAddress(randIPv4Address(t), addrs[0])
  353. if assert.Error(t, err) {
  354. _, ok := err.(ErrAddrBookPrivateSrc)
  355. assert.True(t, ok)
  356. }
  357. }
  358. func testAddrBookAddressSelection(t *testing.T, bookSize int) {
  359. // generate all combinations of old (m) and new addresses
  360. for nBookOld := 0; nBookOld <= bookSize; nBookOld++ {
  361. nBookNew := bookSize - nBookOld
  362. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nBookNew, nBookOld)
  363. // create book and get selection
  364. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nBookOld, nBookNew)
  365. defer deleteTempFile(fname)
  366. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  367. assert.NotNil(t, addrs, "%s - expected a non-nil selection", dbgStr)
  368. nAddrs := len(addrs)
  369. assert.NotZero(t, nAddrs, "%s - expected at least one address in selection", dbgStr)
  370. // check there's no nil addresses
  371. for _, addr := range addrs {
  372. if addr == nil {
  373. t.Fatalf("%s - got nil address in selection %v", dbgStr, addrs)
  374. }
  375. }
  376. // XXX: shadowing
  377. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  378. // Given:
  379. // n - num new addrs, m - num old addrs
  380. // k - num new addrs expected in the beginning (based on bias %)
  381. // i=min(n, max(k,r-m)), aka expNew
  382. // j=min(m, r-i), aka expOld
  383. //
  384. // We expect this layout:
  385. // indices: 0...i-1 i...i+j-1
  386. // addresses: N0..Ni-1 O0..Oj-1
  387. //
  388. // There is at least one partition and at most three.
  389. var (
  390. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  391. expNew = tmmath.MinInt(nNew, tmmath.MaxInt(k, nAddrs-nBookOld))
  392. expOld = tmmath.MinInt(nOld, nAddrs-expNew)
  393. )
  394. // Verify that the number of old and new addresses are as expected
  395. if nNew != expNew {
  396. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  397. }
  398. if nOld != expOld {
  399. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  400. }
  401. // Verify that the order of addresses is as expected
  402. // Get the sequence types and lengths of the selection
  403. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  404. assert.NoError(t, err, "%s", dbgStr)
  405. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  406. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  407. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  408. var expSeqLens []int
  409. var expSeqTypes []int
  410. switch {
  411. case expOld == 0: // all new addresses
  412. expSeqLens = []int{nAddrs}
  413. expSeqTypes = []int{1}
  414. case expNew == 0: // all old addresses
  415. expSeqLens = []int{nAddrs}
  416. expSeqTypes = []int{2}
  417. case nAddrs-expNew-expOld == 0: // new addresses, old addresses
  418. expSeqLens = []int{expNew, expOld}
  419. expSeqTypes = []int{1, 2}
  420. }
  421. assert.Equal(t, expSeqLens, seqLens,
  422. "%s - expected sequence lengths of old/new %v, got %v",
  423. dbgStr, expSeqLens, seqLens)
  424. assert.Equal(t, expSeqTypes, seqTypes,
  425. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  426. dbgStr, expSeqTypes, seqTypes)
  427. }
  428. }
  429. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  430. // test books with smaller size, < N
  431. const N = 32
  432. for bookSize := 1; bookSize < N; bookSize++ {
  433. testAddrBookAddressSelection(t, bookSize)
  434. }
  435. // Test for two books with sizes from following ranges
  436. ranges := [...][]int{{33, 100}, {100, 175}}
  437. bookSizes := make([]int, 0, len(ranges))
  438. for _, r := range ranges {
  439. bookSizes = append(bookSizes, tmrand.Intn(r[1]-r[0])+r[0])
  440. }
  441. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  442. for _, bookSize := range bookSizes {
  443. testAddrBookAddressSelection(t, bookSize)
  444. }
  445. }
  446. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  447. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  448. assert.Equal(t, m, nOld, "old addresses")
  449. assert.Equal(t, n, nNew, "new addresses")
  450. }
  451. func createTempFileName(prefix string) string {
  452. f, err := ioutil.TempFile("", prefix)
  453. if err != nil {
  454. panic(err)
  455. }
  456. fname := f.Name()
  457. err = f.Close()
  458. if err != nil {
  459. panic(err)
  460. }
  461. return fname
  462. }
  463. func deleteTempFile(fname string) {
  464. err := os.Remove(fname)
  465. if err != nil {
  466. panic(err)
  467. }
  468. }
  469. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  470. fname = createTempFileName("addrbook_test")
  471. book = NewAddrBook(fname, true).(*addrBook)
  472. book.SetLogger(log.TestingLogger())
  473. assert.Zero(t, book.Size())
  474. randAddrs := randNetAddressPairs(t, nOld)
  475. for _, addr := range randAddrs {
  476. book.AddAddress(addr.addr, addr.src)
  477. book.MarkGood(addr.addr.ID)
  478. }
  479. randAddrs = randNetAddressPairs(t, nNew)
  480. for _, addr := range randAddrs {
  481. book.AddAddress(addr.addr, addr.src)
  482. }
  483. return
  484. }
  485. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  486. for _, addr := range addrs {
  487. if book.IsGood(addr) {
  488. nOld++
  489. } else {
  490. nNew++
  491. }
  492. }
  493. return
  494. }
  495. // Analyse the layout of the selection specified by 'addrs'
  496. // Returns:
  497. // - seqLens - the lengths of the sequences of addresses of same type
  498. // - seqTypes - the types of sequences in selection
  499. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  500. // address types are: 0 - nil, 1 - new, 2 - old
  501. var (
  502. prevType = 0
  503. currentSeqLen = 0
  504. )
  505. for _, addr := range addrs {
  506. addrType := 0
  507. if book.IsGood(addr) {
  508. addrType = 2
  509. } else {
  510. addrType = 1
  511. }
  512. if addrType != prevType && prevType != 0 {
  513. seqLens = append(seqLens, currentSeqLen)
  514. seqTypes = append(seqTypes, prevType)
  515. currentSeqLen = 0
  516. }
  517. currentSeqLen++
  518. prevType = addrType
  519. }
  520. seqLens = append(seqLens, currentSeqLen)
  521. seqTypes = append(seqTypes, prevType)
  522. return
  523. }