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.

732 lines
20 KiB

9 years ago
9 years ago
4 years ago
4 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 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
7 years ago
9 years ago
7 years ago
7 years ago
7 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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
5 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. book.AddAddress(addrSrc.addr, addrSrc.src)
  31. // pick an address when we only have new address
  32. addr = book.PickAddress(0)
  33. assert.NotNil(t, addr, "expected an address")
  34. addr = book.PickAddress(50)
  35. assert.NotNil(t, addr, "expected an address")
  36. addr = book.PickAddress(100)
  37. assert.NotNil(t, addr, "expected an address")
  38. // pick an address when we only have old address
  39. book.MarkGood(addrSrc.addr.ID)
  40. addr = book.PickAddress(0)
  41. assert.NotNil(t, addr, "expected an address")
  42. addr = book.PickAddress(50)
  43. assert.NotNil(t, addr, "expected an address")
  44. // in this case, nNew==0 but we biased 100% to new, so we return nil
  45. addr = book.PickAddress(100)
  46. assert.Nil(t, addr, "did not expected an address")
  47. }
  48. func TestAddrBookSaveLoad(t *testing.T) {
  49. fname := createTempFileName("addrbook_test")
  50. defer deleteTempFile(fname)
  51. // 0 addresses
  52. book := NewAddrBook(fname, true)
  53. book.SetLogger(log.TestingLogger())
  54. book.Save()
  55. book = NewAddrBook(fname, true)
  56. book.SetLogger(log.TestingLogger())
  57. book.Start()
  58. assert.True(t, book.Empty())
  59. // 100 addresses
  60. randAddrs := randNetAddressPairs(t, 100)
  61. for _, addrSrc := range randAddrs {
  62. book.AddAddress(addrSrc.addr, addrSrc.src)
  63. }
  64. assert.Equal(t, 100, book.Size())
  65. book.Save()
  66. book = NewAddrBook(fname, true)
  67. book.SetLogger(log.TestingLogger())
  68. book.Start()
  69. assert.Equal(t, 100, book.Size())
  70. }
  71. func TestAddrBookLookup(t *testing.T) {
  72. fname := createTempFileName("addrbook_test")
  73. defer deleteTempFile(fname)
  74. randAddrs := randNetAddressPairs(t, 100)
  75. book := NewAddrBook(fname, true)
  76. book.SetLogger(log.TestingLogger())
  77. for _, addrSrc := range randAddrs {
  78. addr := addrSrc.addr
  79. src := addrSrc.src
  80. book.AddAddress(addr, src)
  81. ka := book.HasAddress(addr)
  82. assert.True(t, ka, "Expected to find KnownAddress %v but wasn't there.", addr)
  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 TestBanBadPeers(t *testing.T) {
  318. fname := createTempFileName("addrbook_test")
  319. defer deleteTempFile(fname)
  320. book := NewAddrBook(fname, true)
  321. book.SetLogger(log.TestingLogger())
  322. addr := randIPv4Address(t)
  323. _ = book.AddAddress(addr, addr)
  324. book.MarkBad(addr, 1*time.Second)
  325. // addr should not reachable
  326. assert.False(t, book.HasAddress(addr))
  327. assert.True(t, book.IsBanned(addr))
  328. err := book.AddAddress(addr, addr)
  329. // book should not add address from the blacklist
  330. assert.Error(t, err)
  331. time.Sleep(1 * time.Second)
  332. book.ReinstateBadPeers()
  333. // address should be reinstated in the new bucket
  334. assert.EqualValues(t, 1, book.Size())
  335. assert.True(t, book.HasAddress(addr))
  336. assert.False(t, book.IsGood(addr))
  337. }
  338. func TestAddrBookEmpty(t *testing.T) {
  339. fname := createTempFileName("addrbook_test")
  340. defer deleteTempFile(fname)
  341. book := NewAddrBook(fname, true)
  342. book.SetLogger(log.TestingLogger())
  343. // Check that empty book is empty
  344. require.True(t, book.Empty())
  345. // Check that book with our address is empty
  346. book.AddOurAddress(randIPv4Address(t))
  347. require.True(t, book.Empty())
  348. // Check that book with private addrs is empty
  349. _, privateIds := testCreatePrivateAddrs(t, 5)
  350. book.AddPrivateIDs(privateIds)
  351. require.True(t, book.Empty())
  352. // Check that book with address is not empty
  353. book.AddAddress(randIPv4Address(t), randIPv4Address(t))
  354. require.False(t, book.Empty())
  355. }
  356. func TestPrivatePeers(t *testing.T) {
  357. fname := createTempFileName("addrbook_test")
  358. defer deleteTempFile(fname)
  359. book := NewAddrBook(fname, true)
  360. book.SetLogger(log.TestingLogger())
  361. addrs, private := testCreatePrivateAddrs(t, 10)
  362. book.AddPrivateIDs(private)
  363. // private addrs must not be added
  364. for _, addr := range addrs {
  365. err := book.AddAddress(addr, addr)
  366. if assert.Error(t, err) {
  367. _, ok := err.(ErrAddrBookPrivate)
  368. assert.True(t, ok)
  369. }
  370. }
  371. // addrs coming from private peers must not be added
  372. err := book.AddAddress(randIPv4Address(t), addrs[0])
  373. if assert.Error(t, err) {
  374. _, ok := err.(ErrAddrBookPrivateSrc)
  375. assert.True(t, ok)
  376. }
  377. }
  378. func testAddrBookAddressSelection(t *testing.T, bookSize int) {
  379. // generate all combinations of old (m) and new addresses
  380. for nBookOld := 0; nBookOld <= bookSize; nBookOld++ {
  381. nBookNew := bookSize - nBookOld
  382. dbgStr := fmt.Sprintf("book of size %d (new %d, old %d)", bookSize, nBookNew, nBookOld)
  383. // create book and get selection
  384. book, fname := createAddrBookWithMOldAndNNewAddrs(t, nBookOld, nBookNew)
  385. defer deleteTempFile(fname)
  386. addrs := book.GetSelectionWithBias(biasToSelectNewPeers)
  387. assert.NotNil(t, addrs, "%s - expected a non-nil selection", dbgStr)
  388. nAddrs := len(addrs)
  389. assert.NotZero(t, nAddrs, "%s - expected at least one address in selection", dbgStr)
  390. // check there's no nil addresses
  391. for _, addr := range addrs {
  392. if addr == nil {
  393. t.Fatalf("%s - got nil address in selection %v", dbgStr, addrs)
  394. }
  395. }
  396. // XXX: shadowing
  397. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  398. // Given:
  399. // n - num new addrs, m - num old addrs
  400. // k - num new addrs expected in the beginning (based on bias %)
  401. // i=min(n, max(k,r-m)), aka expNew
  402. // j=min(m, r-i), aka expOld
  403. //
  404. // We expect this layout:
  405. // indices: 0...i-1 i...i+j-1
  406. // addresses: N0..Ni-1 O0..Oj-1
  407. //
  408. // There is at least one partition and at most three.
  409. var (
  410. k = percentageOfNum(biasToSelectNewPeers, nAddrs)
  411. expNew = tmmath.MinInt(nNew, tmmath.MaxInt(k, nAddrs-nBookOld))
  412. expOld = tmmath.MinInt(nOld, nAddrs-expNew)
  413. )
  414. // Verify that the number of old and new addresses are as expected
  415. if nNew != expNew {
  416. t.Fatalf("%s - expected new addrs %d, got %d", dbgStr, expNew, nNew)
  417. }
  418. if nOld != expOld {
  419. t.Fatalf("%s - expected old addrs %d, got %d", dbgStr, expOld, nOld)
  420. }
  421. // Verify that the order of addresses is as expected
  422. // Get the sequence types and lengths of the selection
  423. seqLens, seqTypes, err := analyseSelectionLayout(book, addrs)
  424. assert.NoError(t, err, "%s", dbgStr)
  425. // Build a list with the expected lengths of partitions and another with the expected types, e.g.:
  426. // expSeqLens = [10, 22], expSeqTypes = [1, 2]
  427. // means we expect 10 new (type 1) addresses followed by 22 old (type 2) addresses.
  428. var expSeqLens []int
  429. var expSeqTypes []int
  430. switch {
  431. case expOld == 0: // all new addresses
  432. expSeqLens = []int{nAddrs}
  433. expSeqTypes = []int{1}
  434. case expNew == 0: // all old addresses
  435. expSeqLens = []int{nAddrs}
  436. expSeqTypes = []int{2}
  437. case nAddrs-expNew-expOld == 0: // new addresses, old addresses
  438. expSeqLens = []int{expNew, expOld}
  439. expSeqTypes = []int{1, 2}
  440. }
  441. assert.Equal(t, expSeqLens, seqLens,
  442. "%s - expected sequence lengths of old/new %v, got %v",
  443. dbgStr, expSeqLens, seqLens)
  444. assert.Equal(t, expSeqTypes, seqTypes,
  445. "%s - expected sequence types (1-new, 2-old) was %v, got %v",
  446. dbgStr, expSeqTypes, seqTypes)
  447. }
  448. }
  449. func TestMultipleAddrBookAddressSelection(t *testing.T) {
  450. // test books with smaller size, < N
  451. const N = 32
  452. for bookSize := 1; bookSize < N; bookSize++ {
  453. testAddrBookAddressSelection(t, bookSize)
  454. }
  455. // Test for two books with sizes from following ranges
  456. ranges := [...][]int{{33, 100}, {100, 175}}
  457. bookSizes := make([]int, 0, len(ranges))
  458. for _, r := range ranges {
  459. bookSizes = append(bookSizes, tmrand.Intn(r[1]-r[0])+r[0])
  460. }
  461. t.Logf("Testing address selection for the following book sizes %v\n", bookSizes)
  462. for _, bookSize := range bookSizes {
  463. testAddrBookAddressSelection(t, bookSize)
  464. }
  465. }
  466. func TestAddrBookGroupKey(t *testing.T) {
  467. // non-strict routability
  468. testCases := []struct {
  469. name string
  470. ip string
  471. expKey string
  472. }{
  473. // IPv4 normal.
  474. {"ipv4 normal class a", "12.1.2.3", "12.1.0.0"},
  475. {"ipv4 normal class b", "173.1.2.3", "173.1.0.0"},
  476. {"ipv4 normal class c", "196.1.2.3", "196.1.0.0"},
  477. // IPv6/IPv4 translations.
  478. {"ipv6 rfc3964 with ipv4 encap", "2002:0c01:0203::", "12.1.0.0"},
  479. {"ipv6 rfc4380 toredo ipv4", "2001:0:1234::f3fe:fdfc", "12.1.0.0"},
  480. {"ipv6 rfc6052 well-known prefix with ipv4", "64:ff9b::0c01:0203", "12.1.0.0"},
  481. {"ipv6 rfc6145 translated ipv4", "::ffff:0:0c01:0203", "12.1.0.0"},
  482. // Tor.
  483. {"ipv6 tor onioncat", "fd87:d87e:eb43:1234::5678", "tor:2"},
  484. {"ipv6 tor onioncat 2", "fd87:d87e:eb43:1245::6789", "tor:2"},
  485. {"ipv6 tor onioncat 3", "fd87:d87e:eb43:1345::6789", "tor:3"},
  486. // IPv6 normal.
  487. {"ipv6 normal", "2602:100::1", "2602:100::"},
  488. {"ipv6 normal 2", "2602:0100::1234", "2602:100::"},
  489. {"ipv6 hurricane electric", "2001:470:1f10:a1::2", "2001:470:1000::"},
  490. {"ipv6 hurricane electric 2", "2001:0470:1f10:a1::2", "2001:470:1000::"},
  491. }
  492. for i, tc := range testCases {
  493. nip := net.ParseIP(tc.ip)
  494. key := groupKeyFor(p2p.NewNetAddressIPPort(nip, 26656), false)
  495. assert.Equal(t, tc.expKey, key, "#%d", i)
  496. }
  497. // strict routability
  498. testCases = []struct {
  499. name string
  500. ip string
  501. expKey string
  502. }{
  503. // Local addresses.
  504. {"ipv4 localhost", "127.0.0.1", "local"},
  505. {"ipv6 localhost", "::1", "local"},
  506. {"ipv4 zero", "0.0.0.0", "local"},
  507. {"ipv4 first octet zero", "0.1.2.3", "local"},
  508. // Unroutable addresses.
  509. {"ipv4 invalid bcast", "255.255.255.255", "unroutable"},
  510. {"ipv4 rfc1918 10/8", "10.1.2.3", "unroutable"},
  511. {"ipv4 rfc1918 172.16/12", "172.16.1.2", "unroutable"},
  512. {"ipv4 rfc1918 192.168/16", "192.168.1.2", "unroutable"},
  513. {"ipv6 rfc3849 2001:db8::/32", "2001:db8::1234", "unroutable"},
  514. {"ipv4 rfc3927 169.254/16", "169.254.1.2", "unroutable"},
  515. {"ipv6 rfc4193 fc00::/7", "fc00::1234", "unroutable"},
  516. {"ipv6 rfc4843 2001:10::/28", "2001:10::1234", "unroutable"},
  517. {"ipv6 rfc4862 fe80::/64", "fe80::1234", "unroutable"},
  518. }
  519. for i, tc := range testCases {
  520. nip := net.ParseIP(tc.ip)
  521. key := groupKeyFor(p2p.NewNetAddressIPPort(nip, 26656), true)
  522. assert.Equal(t, tc.expKey, key, "#%d", i)
  523. }
  524. }
  525. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  526. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  527. assert.Equal(t, m, nOld, "old addresses")
  528. assert.Equal(t, n, nNew, "new addresses")
  529. }
  530. func createTempFileName(prefix string) string {
  531. f, err := ioutil.TempFile("", prefix)
  532. if err != nil {
  533. panic(err)
  534. }
  535. fname := f.Name()
  536. err = f.Close()
  537. if err != nil {
  538. panic(err)
  539. }
  540. return fname
  541. }
  542. func deleteTempFile(fname string) {
  543. err := os.Remove(fname)
  544. if err != nil {
  545. panic(err)
  546. }
  547. }
  548. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  549. fname = createTempFileName("addrbook_test")
  550. book = NewAddrBook(fname, true).(*addrBook)
  551. book.SetLogger(log.TestingLogger())
  552. assert.Zero(t, book.Size())
  553. randAddrs := randNetAddressPairs(t, nOld)
  554. for _, addr := range randAddrs {
  555. book.AddAddress(addr.addr, addr.src)
  556. book.MarkGood(addr.addr.ID)
  557. }
  558. randAddrs = randNetAddressPairs(t, nNew)
  559. for _, addr := range randAddrs {
  560. book.AddAddress(addr.addr, addr.src)
  561. }
  562. return
  563. }
  564. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  565. for _, addr := range addrs {
  566. if book.IsGood(addr) {
  567. nOld++
  568. } else {
  569. nNew++
  570. }
  571. }
  572. return
  573. }
  574. // Analyse the layout of the selection specified by 'addrs'
  575. // Returns:
  576. // - seqLens - the lengths of the sequences of addresses of same type
  577. // - seqTypes - the types of sequences in selection
  578. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  579. // address types are: 0 - nil, 1 - new, 2 - old
  580. var (
  581. prevType = 0
  582. currentSeqLen = 0
  583. )
  584. for _, addr := range addrs {
  585. addrType := 0
  586. if book.IsGood(addr) {
  587. addrType = 2
  588. } else {
  589. addrType = 1
  590. }
  591. if addrType != prevType && prevType != 0 {
  592. seqLens = append(seqLens, currentSeqLen)
  593. seqTypes = append(seqTypes, prevType)
  594. currentSeqLen = 0
  595. }
  596. currentSeqLen++
  597. prevType = addrType
  598. }
  599. seqLens = append(seqLens, currentSeqLen)
  600. seqTypes = append(seqTypes, prevType)
  601. return
  602. }