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.

750 lines
20 KiB

9 years ago
9 years ago
5 years ago
5 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
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
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
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. "net"
  8. "os"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "github.com/tendermint/tendermint/libs/log"
  14. tmmath "github.com/tendermint/tendermint/libs/math"
  15. tmrand "github.com/tendermint/tendermint/libs/rand"
  16. "github.com/tendermint/tendermint/p2p"
  17. )
  18. // FIXME These tests should not rely on .(*addrBook) assertions
  19. func TestAddrBookPickAddress(t *testing.T) {
  20. fname := createTempFileName("addrbook_test")
  21. defer deleteTempFile(fname)
  22. // 0 addresses
  23. book := NewAddrBook(fname, true)
  24. book.SetLogger(log.TestingLogger())
  25. assert.Zero(t, book.Size())
  26. addr := book.PickAddress(50)
  27. assert.Nil(t, addr, "expected no address")
  28. randAddrs := randNetAddressPairs(t, 1)
  29. addrSrc := randAddrs[0]
  30. err := book.AddAddress(addrSrc.addr, addrSrc.src)
  31. require.NoError(t, err)
  32. // pick an address when we only have new address
  33. addr = book.PickAddress(0)
  34. assert.NotNil(t, addr, "expected an address")
  35. addr = book.PickAddress(50)
  36. assert.NotNil(t, addr, "expected an address")
  37. addr = book.PickAddress(100)
  38. assert.NotNil(t, addr, "expected an address")
  39. // pick an address when we only have old address
  40. book.MarkGood(addrSrc.addr.ID)
  41. addr = book.PickAddress(0)
  42. assert.NotNil(t, addr, "expected an address")
  43. addr = book.PickAddress(50)
  44. assert.NotNil(t, addr, "expected an address")
  45. // in this case, nNew==0 but we biased 100% to new, so we return nil
  46. addr = book.PickAddress(100)
  47. assert.Nil(t, addr, "did not expected an address")
  48. }
  49. func TestAddrBookSaveLoad(t *testing.T) {
  50. fname := createTempFileName("addrbook_test")
  51. defer deleteTempFile(fname)
  52. // 0 addresses
  53. book := NewAddrBook(fname, true)
  54. book.SetLogger(log.TestingLogger())
  55. book.Save()
  56. book = NewAddrBook(fname, true)
  57. book.SetLogger(log.TestingLogger())
  58. err := book.Start()
  59. require.NoError(t, err)
  60. assert.True(t, book.Empty())
  61. // 100 addresses
  62. randAddrs := randNetAddressPairs(t, 100)
  63. for _, addrSrc := range randAddrs {
  64. err := book.AddAddress(addrSrc.addr, addrSrc.src)
  65. require.NoError(t, err)
  66. }
  67. assert.Equal(t, 100, book.Size())
  68. book.Save()
  69. book = NewAddrBook(fname, true)
  70. book.SetLogger(log.TestingLogger())
  71. err = book.Start()
  72. require.NoError(t, err)
  73. assert.Equal(t, 100, book.Size())
  74. }
  75. func TestAddrBookLookup(t *testing.T) {
  76. fname := createTempFileName("addrbook_test")
  77. defer deleteTempFile(fname)
  78. randAddrs := randNetAddressPairs(t, 100)
  79. book := NewAddrBook(fname, true)
  80. book.SetLogger(log.TestingLogger())
  81. for _, addrSrc := range randAddrs {
  82. addr := addrSrc.addr
  83. src := addrSrc.src
  84. err := book.AddAddress(addr, src)
  85. require.NoError(t, err)
  86. ka := book.HasAddress(addr)
  87. assert.True(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  88. }
  89. }
  90. func TestAddrBookPromoteToOld(t *testing.T) {
  91. fname := createTempFileName("addrbook_test")
  92. defer deleteTempFile(fname)
  93. randAddrs := randNetAddressPairs(t, 100)
  94. book := NewAddrBook(fname, true)
  95. book.SetLogger(log.TestingLogger())
  96. for _, addrSrc := range randAddrs {
  97. err := book.AddAddress(addrSrc.addr, addrSrc.src)
  98. require.NoError(t, err)
  99. }
  100. // Attempt all addresses.
  101. for _, addrSrc := range randAddrs {
  102. book.MarkAttempt(addrSrc.addr)
  103. }
  104. // Promote half of them
  105. for i, addrSrc := range randAddrs {
  106. if i%2 == 0 {
  107. book.MarkGood(addrSrc.addr.ID)
  108. }
  109. }
  110. // TODO: do more testing :)
  111. selection := book.GetSelection()
  112. t.Logf("selection: %v", selection)
  113. if len(selection) > book.Size() {
  114. t.Errorf("selection could not be bigger than the book")
  115. }
  116. selection = book.GetSelectionWithBias(30)
  117. t.Logf("selection: %v", selection)
  118. if len(selection) > book.Size() {
  119. t.Errorf("selection with bias could not be bigger than the book")
  120. }
  121. assert.Equal(t, book.Size(), 100, "expecting book size to be 100")
  122. }
  123. func TestAddrBookHandlesDuplicates(t *testing.T) {
  124. fname := createTempFileName("addrbook_test")
  125. defer deleteTempFile(fname)
  126. book := NewAddrBook(fname, true)
  127. book.SetLogger(log.TestingLogger())
  128. randAddrs := randNetAddressPairs(t, 100)
  129. differentSrc := randIPv4Address(t)
  130. for _, addrSrc := range randAddrs {
  131. err := book.AddAddress(addrSrc.addr, addrSrc.src)
  132. require.NoError(t, err)
  133. err = book.AddAddress(addrSrc.addr, addrSrc.src) // duplicate
  134. require.NoError(t, err)
  135. err = book.AddAddress(addrSrc.addr, differentSrc) // different src
  136. require.NoError(t, err)
  137. }
  138. assert.Equal(t, 100, book.Size())
  139. }
  140. type netAddressPair struct {
  141. addr *p2p.NetAddress
  142. src *p2p.NetAddress
  143. }
  144. func randNetAddressPairs(t *testing.T, n int) []netAddressPair {
  145. randAddrs := make([]netAddressPair, n)
  146. for i := 0; i < n; i++ {
  147. randAddrs[i] = netAddressPair{addr: randIPv4Address(t), src: randIPv4Address(t)}
  148. }
  149. return randAddrs
  150. }
  151. func randIPv4Address(t *testing.T) *p2p.NetAddress {
  152. for {
  153. ip := fmt.Sprintf("%v.%v.%v.%v",
  154. tmrand.Intn(254)+1,
  155. tmrand.Intn(255),
  156. tmrand.Intn(255),
  157. tmrand.Intn(255),
  158. )
  159. port := tmrand.Intn(65535-1) + 1
  160. id := p2p.ID(hex.EncodeToString(tmrand.Bytes(p2p.IDByteLength)))
  161. idAddr := p2p.IDAddressString(id, fmt.Sprintf("%v:%v", ip, port))
  162. addr, err := p2p.NewNetAddressString(idAddr)
  163. assert.Nil(t, err, "error generating rand network address")
  164. if addr.Routable() {
  165. return addr
  166. }
  167. }
  168. }
  169. func TestAddrBookRemoveAddress(t *testing.T) {
  170. fname := createTempFileName("addrbook_test")
  171. defer deleteTempFile(fname)
  172. book := NewAddrBook(fname, true)
  173. book.SetLogger(log.TestingLogger())
  174. addr := randIPv4Address(t)
  175. err := book.AddAddress(addr, addr)
  176. require.NoError(t, err)
  177. assert.Equal(t, 1, book.Size())
  178. book.RemoveAddress(addr)
  179. assert.Equal(t, 0, book.Size())
  180. nonExistingAddr := randIPv4Address(t)
  181. book.RemoveAddress(nonExistingAddr)
  182. assert.Equal(t, 0, book.Size())
  183. }
  184. func TestAddrBookGetSelectionWithOneMarkedGood(t *testing.T) {
  185. // create a book with 10 addresses, 1 good/old and 9 new
  186. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 1, 9)
  187. defer deleteTempFile(fname)
  188. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  189. assert.NotNil(t, addrs)
  190. assertMOldAndNNewAddrsInSelection(t, 1, 9, addrs, book)
  191. }
  192. func TestAddrBookGetSelectionWithOneNotMarkedGood(t *testing.T) {
  193. // create a book with 10 addresses, 9 good/old and 1 new
  194. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 9, 1)
  195. defer deleteTempFile(fname)
  196. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  197. assert.NotNil(t, addrs)
  198. assertMOldAndNNewAddrsInSelection(t, 9, 1, addrs, book)
  199. }
  200. func TestAddrBookGetSelectionReturnsNilWhenAddrBookIsEmpty(t *testing.T) {
  201. book, fname := createAddrBookWithMOldAndNNewAddrs(t, 0, 0)
  202. defer deleteTempFile(fname)
  203. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  204. assert.Nil(t, addrs)
  205. }
  206. func TestAddrBookGetSelection(t *testing.T) {
  207. fname := createTempFileName("addrbook_test")
  208. defer deleteTempFile(fname)
  209. book := NewAddrBook(fname, true)
  210. book.SetLogger(log.TestingLogger())
  211. // 1) empty book
  212. assert.Empty(t, book.GetSelection())
  213. // 2) add one address
  214. addr := randIPv4Address(t)
  215. err := book.AddAddress(addr, addr)
  216. require.NoError(t, err)
  217. assert.Equal(t, 1, len(book.GetSelection()))
  218. assert.Equal(t, addr, book.GetSelection()[0])
  219. // 3) add a bunch of addresses
  220. randAddrs := randNetAddressPairs(t, 100)
  221. for _, addrSrc := range randAddrs {
  222. err := book.AddAddress(addrSrc.addr, addrSrc.src)
  223. require.NoError(t, err)
  224. }
  225. // check there is no duplicates
  226. addrs := make(map[string]*p2p.NetAddress)
  227. selection := book.GetSelection()
  228. for _, addr := range selection {
  229. if dup, ok := addrs[addr.String()]; ok {
  230. t.Fatalf("selection %v contains duplicates %v", selection, dup)
  231. }
  232. addrs[addr.String()] = addr
  233. }
  234. if len(selection) > book.Size() {
  235. t.Errorf("selection %v could not be bigger than the book", selection)
  236. }
  237. }
  238. func TestAddrBookGetSelectionWithBias(t *testing.T) {
  239. const biasTowardsNewAddrs = 30
  240. fname := createTempFileName("addrbook_test")
  241. defer deleteTempFile(fname)
  242. book := NewAddrBook(fname, true)
  243. book.SetLogger(log.TestingLogger())
  244. // 1) empty book
  245. selection := book.GetSelectionWithBias(biasTowardsNewAddrs)
  246. assert.Empty(t, selection)
  247. // 2) add one address
  248. addr := randIPv4Address(t)
  249. err := book.AddAddress(addr, addr)
  250. require.NoError(t, err)
  251. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  252. assert.Equal(t, 1, len(selection))
  253. assert.Equal(t, addr, selection[0])
  254. // 3) add a bunch of addresses
  255. randAddrs := randNetAddressPairs(t, 100)
  256. for _, addrSrc := range randAddrs {
  257. err := book.AddAddress(addrSrc.addr, addrSrc.src)
  258. require.NoError(t, err)
  259. }
  260. // check there is no duplicates
  261. addrs := make(map[string]*p2p.NetAddress)
  262. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  263. for _, addr := range selection {
  264. if dup, ok := addrs[addr.String()]; ok {
  265. t.Fatalf("selection %v contains duplicates %v", selection, dup)
  266. }
  267. addrs[addr.String()] = addr
  268. }
  269. if len(selection) > book.Size() {
  270. t.Fatalf("selection %v could not be bigger than the book", selection)
  271. }
  272. // 4) mark 80% of the addresses as good
  273. randAddrsLen := len(randAddrs)
  274. for i, addrSrc := range randAddrs {
  275. if int((float64(i)/float64(randAddrsLen))*100) >= 20 {
  276. book.MarkGood(addrSrc.addr.ID)
  277. }
  278. }
  279. selection = book.GetSelectionWithBias(biasTowardsNewAddrs)
  280. // check that ~70% of addresses returned are good
  281. good := 0
  282. for _, addr := range selection {
  283. if book.IsGood(addr) {
  284. good++
  285. }
  286. }
  287. got, expected := int((float64(good)/float64(len(selection)))*100), 100-biasTowardsNewAddrs
  288. // compute some slack to protect against small differences due to rounding:
  289. slack := int(math.Round(float64(100) / float64(len(selection))))
  290. if got > expected+slack {
  291. t.Fatalf(
  292. "got more good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)",
  293. got,
  294. expected,
  295. good,
  296. len(selection),
  297. )
  298. }
  299. if got < expected-slack {
  300. t.Fatalf(
  301. "got fewer good peers (%% got: %d, %% expected: %d, number of good addrs: %d, total: %d)",
  302. got,
  303. expected,
  304. good,
  305. len(selection),
  306. )
  307. }
  308. }
  309. func TestAddrBookHasAddress(t *testing.T) {
  310. fname := createTempFileName("addrbook_test")
  311. defer deleteTempFile(fname)
  312. book := NewAddrBook(fname, true)
  313. book.SetLogger(log.TestingLogger())
  314. addr := randIPv4Address(t)
  315. err := book.AddAddress(addr, addr)
  316. require.NoError(t, err)
  317. assert.True(t, book.HasAddress(addr))
  318. book.RemoveAddress(addr)
  319. assert.False(t, book.HasAddress(addr))
  320. }
  321. func testCreatePrivateAddrs(t *testing.T, numAddrs int) ([]*p2p.NetAddress, []string) {
  322. addrs := make([]*p2p.NetAddress, numAddrs)
  323. for i := 0; i < numAddrs; i++ {
  324. addrs[i] = randIPv4Address(t)
  325. }
  326. private := make([]string, numAddrs)
  327. for i, addr := range addrs {
  328. private[i] = string(addr.ID)
  329. }
  330. return addrs, private
  331. }
  332. func TestBanBadPeers(t *testing.T) {
  333. fname := createTempFileName("addrbook_test")
  334. defer deleteTempFile(fname)
  335. book := NewAddrBook(fname, true)
  336. book.SetLogger(log.TestingLogger())
  337. addr := randIPv4Address(t)
  338. _ = book.AddAddress(addr, addr)
  339. book.MarkBad(addr, 1*time.Second)
  340. // addr should not reachable
  341. assert.False(t, book.HasAddress(addr))
  342. assert.True(t, book.IsBanned(addr))
  343. err := book.AddAddress(addr, addr)
  344. // book should not add address from the blacklist
  345. assert.Error(t, err)
  346. time.Sleep(1 * time.Second)
  347. book.ReinstateBadPeers()
  348. // address should be reinstated in the new bucket
  349. assert.EqualValues(t, 1, book.Size())
  350. assert.True(t, book.HasAddress(addr))
  351. assert.False(t, book.IsGood(addr))
  352. }
  353. func TestAddrBookEmpty(t *testing.T) {
  354. fname := createTempFileName("addrbook_test")
  355. defer deleteTempFile(fname)
  356. book := NewAddrBook(fname, true)
  357. book.SetLogger(log.TestingLogger())
  358. // Check that empty book is empty
  359. require.True(t, book.Empty())
  360. // Check that book with our address is empty
  361. book.AddOurAddress(randIPv4Address(t))
  362. require.True(t, book.Empty())
  363. // Check that book with private addrs is empty
  364. _, privateIds := testCreatePrivateAddrs(t, 5)
  365. book.AddPrivateIDs(privateIds)
  366. require.True(t, book.Empty())
  367. // Check that book with address is not empty
  368. err := book.AddAddress(randIPv4Address(t), randIPv4Address(t))
  369. require.NoError(t, err)
  370. require.False(t, book.Empty())
  371. }
  372. func TestPrivatePeers(t *testing.T) {
  373. fname := createTempFileName("addrbook_test")
  374. defer deleteTempFile(fname)
  375. book := NewAddrBook(fname, true)
  376. book.SetLogger(log.TestingLogger())
  377. addrs, private := testCreatePrivateAddrs(t, 10)
  378. book.AddPrivateIDs(private)
  379. // private addrs must not be added
  380. for _, addr := range addrs {
  381. err := book.AddAddress(addr, addr)
  382. if assert.Error(t, err) {
  383. _, ok := err.(ErrAddrBookPrivate)
  384. assert.True(t, ok)
  385. }
  386. }
  387. // addrs coming from private peers must not be added
  388. err := book.AddAddress(randIPv4Address(t), addrs[0])
  389. if assert.Error(t, err) {
  390. _, ok := err.(ErrAddrBookPrivateSrc)
  391. assert.True(t, ok)
  392. }
  393. }
  394. func testAddrBookAddressSelection(t *testing.T, bookSize int) {
  395. // generate all combinations of old (m) and new addresses
  396. for nBookOld := 0; nBookOld <= bookSize; nBookOld++ {
  397. nBookNew := bookSize - nBookOld
  398. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nBookNew, nBookOld)
  399. // create book and get selection
  400. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nBookOld, nBookNew)
  401. defer deleteTempFile(fname)
  402. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  403. assert.NotNil(t, addrs, "%s - expected a non-nil selection", dbgStr)
  404. nAddrs := len(addrs)
  405. assert.NotZero(t, nAddrs, "%s - expected at least one address in selection", dbgStr)
  406. // check there's no nil addresses
  407. for _, addr := range addrs {
  408. if addr == nil {
  409. t.Fatalf("%s - got nil address in selection %v", dbgStr, addrs)
  410. }
  411. }
  412. // XXX: shadowing
  413. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  414. // Given:
  415. // n - num new addrs, m - num old addrs
  416. // k - num new addrs expected in the beginning (based on bias %)
  417. // i=min(n, max(k,r-m)), aka expNew
  418. // j=min(m, r-i), aka expOld
  419. //
  420. // We expect this layout:
  421. // indices: 0...i-1 i...i+j-1
  422. // addresses: N0..Ni-1 O0..Oj-1
  423. //
  424. // There is at least one partition and at most three.
  425. var (
  426. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  427. expNew = tmmath.MinInt(nNew, tmmath.MaxInt(k, nAddrs-nBookOld))
  428. expOld = tmmath.MinInt(nOld, nAddrs-expNew)
  429. )
  430. // Verify that the number of old and new addresses are as expected
  431. if nNew != expNew {
  432. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  433. }
  434. if nOld != expOld {
  435. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  436. }
  437. // Verify that the order of addresses is as expected
  438. // Get the sequence types and lengths of the selection
  439. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  440. assert.NoError(t, err, "%s", dbgStr)
  441. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  442. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  443. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  444. var expSeqLens []int
  445. var expSeqTypes []int
  446. switch {
  447. case expOld == 0: // all new addresses
  448. expSeqLens = []int{nAddrs}
  449. expSeqTypes = []int{1}
  450. case expNew == 0: // all old addresses
  451. expSeqLens = []int{nAddrs}
  452. expSeqTypes = []int{2}
  453. case nAddrs-expNew-expOld == 0: // new addresses, old addresses
  454. expSeqLens = []int{expNew, expOld}
  455. expSeqTypes = []int{1, 2}
  456. }
  457. assert.Equal(t, expSeqLens, seqLens,
  458. "%s - expected sequence lengths of old/new %v, got %v",
  459. dbgStr, expSeqLens, seqLens)
  460. assert.Equal(t, expSeqTypes, seqTypes,
  461. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  462. dbgStr, expSeqTypes, seqTypes)
  463. }
  464. }
  465. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  466. // test books with smaller size, < N
  467. const N = 32
  468. for bookSize := 1; bookSize < N; bookSize++ {
  469. testAddrBookAddressSelection(t, bookSize)
  470. }
  471. // Test for two books with sizes from following ranges
  472. ranges := [...][]int{{33, 100}, {100, 175}}
  473. bookSizes := make([]int, 0, len(ranges))
  474. for _, r := range ranges {
  475. bookSizes = append(bookSizes, tmrand.Intn(r[1]-r[0])+r[0])
  476. }
  477. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  478. for _, bookSize := range bookSizes {
  479. testAddrBookAddressSelection(t, bookSize)
  480. }
  481. }
  482. func TestAddrBookGroupKey(t *testing.T) {
  483. // non-strict routability
  484. testCases := []struct {
  485. name string
  486. ip string
  487. expKey string
  488. }{
  489. // IPv4 normal.
  490. {"ipv4 normal class a", "12.1.2.3", "12.1.0.0"},
  491. {"ipv4 normal class b", "173.1.2.3", "173.1.0.0"},
  492. {"ipv4 normal class c", "196.1.2.3", "196.1.0.0"},
  493. // IPv6/IPv4 translations.
  494. {"ipv6 rfc3964 with ipv4 encap", "2002:0c01:0203::", "12.1.0.0"},
  495. {"ipv6 rfc4380 toredo ipv4", "2001:0:1234::f3fe:fdfc", "12.1.0.0"},
  496. {"ipv6 rfc6052 well-known prefix with ipv4", "64:ff9b::0c01:0203", "12.1.0.0"},
  497. {"ipv6 rfc6145 translated ipv4", "::ffff:0:0c01:0203", "12.1.0.0"},
  498. // Tor.
  499. {"ipv6 tor onioncat", "fd87:d87e:eb43:1234::5678", "tor:2"},
  500. {"ipv6 tor onioncat 2", "fd87:d87e:eb43:1245::6789", "tor:2"},
  501. {"ipv6 tor onioncat 3", "fd87:d87e:eb43:1345::6789", "tor:3"},
  502. // IPv6 normal.
  503. {"ipv6 normal", "2602:100::1", "2602:100::"},
  504. {"ipv6 normal 2", "2602:0100::1234", "2602:100::"},
  505. {"ipv6 hurricane electric", "2001:470:1f10:a1::2", "2001:470:1000::"},
  506. {"ipv6 hurricane electric 2", "2001:0470:1f10:a1::2", "2001:470:1000::"},
  507. }
  508. for i, tc := range testCases {
  509. nip := net.ParseIP(tc.ip)
  510. key := groupKeyFor(p2p.NewNetAddressIPPort(nip, 26656), false)
  511. assert.Equal(t, tc.expKey, key, "#%d", i)
  512. }
  513. // strict routability
  514. testCases = []struct {
  515. name string
  516. ip string
  517. expKey string
  518. }{
  519. // Local addresses.
  520. {"ipv4 localhost", "127.0.0.1", "local"},
  521. {"ipv6 localhost", "::1", "local"},
  522. {"ipv4 zero", "0.0.0.0", "local"},
  523. {"ipv4 first octet zero", "0.1.2.3", "local"},
  524. // Unroutable addresses.
  525. {"ipv4 invalid bcast", "255.255.255.255", "unroutable"},
  526. {"ipv4 rfc1918 10/8", "10.1.2.3", "unroutable"},
  527. {"ipv4 rfc1918 172.16/12", "172.16.1.2", "unroutable"},
  528. {"ipv4 rfc1918 192.168/16", "192.168.1.2", "unroutable"},
  529. {"ipv6 rfc3849 2001:db8::/32", "2001:db8::1234", "unroutable"},
  530. {"ipv4 rfc3927 169.254/16", "169.254.1.2", "unroutable"},
  531. {"ipv6 rfc4193 fc00::/7", "fc00::1234", "unroutable"},
  532. {"ipv6 rfc4843 2001:10::/28", "2001:10::1234", "unroutable"},
  533. {"ipv6 rfc4862 fe80::/64", "fe80::1234", "unroutable"},
  534. }
  535. for i, tc := range testCases {
  536. nip := net.ParseIP(tc.ip)
  537. key := groupKeyFor(p2p.NewNetAddressIPPort(nip, 26656), true)
  538. assert.Equal(t, tc.expKey, key, "#%d", i)
  539. }
  540. }
  541. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  542. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  543. assert.Equal(t, m, nOld, "old addresses")
  544. assert.Equal(t, n, nNew, "new addresses")
  545. }
  546. func createTempFileName(prefix string) string {
  547. f, err := ioutil.TempFile("", prefix)
  548. if err != nil {
  549. panic(err)
  550. }
  551. fname := f.Name()
  552. err = f.Close()
  553. if err != nil {
  554. panic(err)
  555. }
  556. return fname
  557. }
  558. func deleteTempFile(fname string) {
  559. err := os.Remove(fname)
  560. if err != nil {
  561. panic(err)
  562. }
  563. }
  564. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  565. fname = createTempFileName("addrbook_test")
  566. book = NewAddrBook(fname, true).(*addrBook)
  567. book.SetLogger(log.TestingLogger())
  568. assert.Zero(t, book.Size())
  569. randAddrs := randNetAddressPairs(t, nOld)
  570. for _, addr := range randAddrs {
  571. err := book.AddAddress(addr.addr, addr.src)
  572. require.NoError(t, err)
  573. book.MarkGood(addr.addr.ID)
  574. }
  575. randAddrs = randNetAddressPairs(t, nNew)
  576. for _, addr := range randAddrs {
  577. err := book.AddAddress(addr.addr, addr.src)
  578. require.NoError(t, err)
  579. }
  580. return
  581. }
  582. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  583. for _, addr := range addrs {
  584. if book.IsGood(addr) {
  585. nOld++
  586. } else {
  587. nNew++
  588. }
  589. }
  590. return
  591. }
  592. // Analyse the layout of the selection specified by 'addrs'
  593. // Returns:
  594. // - seqLens - the lengths of the sequences of addresses of same type
  595. // - seqTypes - the types of sequences in selection
  596. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  597. // address types are: 0 - nil, 1 - new, 2 - old
  598. var (
  599. prevType = 0
  600. currentSeqLen = 0
  601. )
  602. for _, addr := range addrs {
  603. addrType := 0
  604. if book.IsGood(addr) {
  605. addrType = 2
  606. } else {
  607. addrType = 1
  608. }
  609. if addrType != prevType && prevType != 0 {
  610. seqLens = append(seqLens, currentSeqLen)
  611. seqTypes = append(seqTypes, prevType)
  612. currentSeqLen = 0
  613. }
  614. currentSeqLen++
  615. prevType = addrType
  616. }
  617. seqLens = append(seqLens, currentSeqLen)
  618. seqTypes = append(seqTypes, prevType)
  619. return
  620. }