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.

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