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.

805 lines
22 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 TestAddrBookAddDoesNotOverwriteOldIP(t *testing.T) {
  483. fname := createTempFileName("addrbook_test")
  484. defer deleteTempFile(fname)
  485. // This test creates adds a peer to the address book and marks it good
  486. // It then attempts to override the peer's IP, by adding a peer with the same ID
  487. // but different IP. We distinguish the IP's by "RealIP" and "OverrideAttemptIP"
  488. peerID := "678503e6c8f50db7279c7da3cb9b072aac4bc0d5"
  489. peerRealIP := "1.1.1.1:26656"
  490. peerOverrideAttemptIP := "2.2.2.2:26656"
  491. SrcAddr := "b0dd378c3fbc4c156cd6d302a799f0d2e4227201@159.89.121.174:26656"
  492. // There is a chance that AddAddress will ignore the new peer its given.
  493. // So we repeat trying to override the peer several times,
  494. // to ensure we aren't in a case that got probabilistically ignored
  495. numOverrideAttempts := 10
  496. peerRealAddr, err := p2p.NewNetAddressString(peerID + "@" + peerRealIP)
  497. require.Nil(t, err)
  498. peerOverrideAttemptAddr, err := p2p.NewNetAddressString(peerID + "@" + peerOverrideAttemptIP)
  499. require.Nil(t, err)
  500. src, err := p2p.NewNetAddressString(SrcAddr)
  501. require.Nil(t, err)
  502. book := NewAddrBook(fname, true)
  503. book.SetLogger(log.TestingLogger())
  504. err = book.AddAddress(peerRealAddr, src)
  505. require.Nil(t, err)
  506. book.MarkAttempt(peerRealAddr)
  507. book.MarkGood(peerRealAddr.ID)
  508. // Double check that adding a peer again doesn't error
  509. err = book.AddAddress(peerRealAddr, src)
  510. require.Nil(t, err)
  511. // Try changing ip but keeping the same node id. (change 1.1.1.1 to 2.2.2.2)
  512. // This should just be ignored, and not error.
  513. for i := 0; i < numOverrideAttempts; i++ {
  514. err = book.AddAddress(peerOverrideAttemptAddr, src)
  515. require.Nil(t, err)
  516. }
  517. // Now check that the IP was not overridden.
  518. // This is done by sampling several peers from addr book
  519. // and ensuring they all have the correct IP.
  520. // In the expected functionality, this test should only have 1 Peer, hence will pass.
  521. for i := 0; i < numOverrideAttempts; i++ {
  522. selection := book.GetSelection()
  523. for _, addr := range selection {
  524. require.Equal(t, addr.IP, peerRealAddr.IP)
  525. }
  526. }
  527. }
  528. func TestAddrBookGroupKey(t *testing.T) {
  529. // non-strict routability
  530. testCases := []struct {
  531. name string
  532. ip string
  533. expKey string
  534. }{
  535. // IPv4 normal.
  536. {"ipv4 normal class a", "12.1.2.3", "12.1.0.0"},
  537. {"ipv4 normal class b", "173.1.2.3", "173.1.0.0"},
  538. {"ipv4 normal class c", "196.1.2.3", "196.1.0.0"},
  539. // IPv6/IPv4 translations.
  540. {"ipv6 rfc3964 with ipv4 encap", "2002:0c01:0203::", "12.1.0.0"},
  541. {"ipv6 rfc4380 toredo ipv4", "2001:0:1234::f3fe:fdfc", "12.1.0.0"},
  542. {"ipv6 rfc6052 well-known prefix with ipv4", "64:ff9b::0c01:0203", "12.1.0.0"},
  543. {"ipv6 rfc6145 translated ipv4", "::ffff:0:0c01:0203", "12.1.0.0"},
  544. // Tor.
  545. {"ipv6 tor onioncat", "fd87:d87e:eb43:1234::5678", "tor:2"},
  546. {"ipv6 tor onioncat 2", "fd87:d87e:eb43:1245::6789", "tor:2"},
  547. {"ipv6 tor onioncat 3", "fd87:d87e:eb43:1345::6789", "tor:3"},
  548. // IPv6 normal.
  549. {"ipv6 normal", "2602:100::1", "2602:100::"},
  550. {"ipv6 normal 2", "2602:0100::1234", "2602:100::"},
  551. {"ipv6 hurricane electric", "2001:470:1f10:a1::2", "2001:470:1000::"},
  552. {"ipv6 hurricane electric 2", "2001:0470:1f10:a1::2", "2001:470:1000::"},
  553. }
  554. for i, tc := range testCases {
  555. nip := net.ParseIP(tc.ip)
  556. key := groupKeyFor(p2p.NewNetAddressIPPort(nip, 26656), false)
  557. assert.Equal(t, tc.expKey, key, "#%d", i)
  558. }
  559. // strict routability
  560. testCases = []struct {
  561. name string
  562. ip string
  563. expKey string
  564. }{
  565. // Local addresses.
  566. {"ipv4 localhost", "127.0.0.1", "local"},
  567. {"ipv6 localhost", "::1", "local"},
  568. {"ipv4 zero", "0.0.0.0", "local"},
  569. {"ipv4 first octet zero", "0.1.2.3", "local"},
  570. // Unroutable addresses.
  571. {"ipv4 invalid bcast", "255.255.255.255", "unroutable"},
  572. {"ipv4 rfc1918 10/8", "10.1.2.3", "unroutable"},
  573. {"ipv4 rfc1918 172.16/12", "172.16.1.2", "unroutable"},
  574. {"ipv4 rfc1918 192.168/16", "192.168.1.2", "unroutable"},
  575. {"ipv6 rfc3849 2001:db8::/32", "2001:db8::1234", "unroutable"},
  576. {"ipv4 rfc3927 169.254/16", "169.254.1.2", "unroutable"},
  577. {"ipv6 rfc4193 fc00::/7", "fc00::1234", "unroutable"},
  578. {"ipv6 rfc4843 2001:10::/28", "2001:10::1234", "unroutable"},
  579. {"ipv6 rfc4862 fe80::/64", "fe80::1234", "unroutable"},
  580. }
  581. for i, tc := range testCases {
  582. nip := net.ParseIP(tc.ip)
  583. key := groupKeyFor(p2p.NewNetAddressIPPort(nip, 26656), true)
  584. assert.Equal(t, tc.expKey, key, "#%d", i)
  585. }
  586. }
  587. func assertMOldAndNNewAddrsInSelection(t *testing.T, m, n int, addrs []*p2p.NetAddress, book *addrBook) {
  588. nOld, nNew := countOldAndNewAddrsInSelection(addrs, book)
  589. assert.Equal(t, m, nOld, "old addresses")
  590. assert.Equal(t, n, nNew, "new addresses")
  591. }
  592. func createTempFileName(prefix string) string {
  593. f, err := ioutil.TempFile("", prefix)
  594. if err != nil {
  595. panic(err)
  596. }
  597. fname := f.Name()
  598. err = f.Close()
  599. if err != nil {
  600. panic(err)
  601. }
  602. return fname
  603. }
  604. func deleteTempFile(fname string) {
  605. err := os.Remove(fname)
  606. if err != nil {
  607. panic(err)
  608. }
  609. }
  610. func createAddrBookWithMOldAndNNewAddrs(t *testing.T, nOld, nNew int) (book *addrBook, fname string) {
  611. fname = createTempFileName("addrbook_test")
  612. book = NewAddrBook(fname, true).(*addrBook)
  613. book.SetLogger(log.TestingLogger())
  614. assert.Zero(t, book.Size())
  615. randAddrs := randNetAddressPairs(t, nOld)
  616. for _, addr := range randAddrs {
  617. err := book.AddAddress(addr.addr, addr.src)
  618. require.NoError(t, err)
  619. book.MarkGood(addr.addr.ID)
  620. }
  621. randAddrs = randNetAddressPairs(t, nNew)
  622. for _, addr := range randAddrs {
  623. err := book.AddAddress(addr.addr, addr.src)
  624. require.NoError(t, err)
  625. }
  626. return
  627. }
  628. func countOldAndNewAddrsInSelection(addrs []*p2p.NetAddress, book *addrBook) (nOld, nNew int) {
  629. for _, addr := range addrs {
  630. if book.IsGood(addr) {
  631. nOld++
  632. } else {
  633. nNew++
  634. }
  635. }
  636. return
  637. }
  638. // Analyse the layout of the selection specified by 'addrs'
  639. // Returns:
  640. // - seqLens - the lengths of the sequences of addresses of same type
  641. // - seqTypes - the types of sequences in selection
  642. func analyseSelectionLayout(book *addrBook, addrs []*p2p.NetAddress) (seqLens, seqTypes []int, err error) {
  643. // address types are: 0 - nil, 1 - new, 2 - old
  644. var (
  645. prevType = 0
  646. currentSeqLen = 0
  647. )
  648. for _, addr := range addrs {
  649. addrType := 0
  650. if book.IsGood(addr) {
  651. addrType = 2
  652. } else {
  653. addrType = 1
  654. }
  655. if addrType != prevType && prevType != 0 {
  656. seqLens = append(seqLens, currentSeqLen)
  657. seqTypes = append(seqTypes, prevType)
  658. currentSeqLen = 0
  659. }
  660. currentSeqLen++
  661. prevType = addrType
  662. }
  663. seqLens = append(seqLens, currentSeqLen)
  664. seqTypes = append(seqTypes, prevType)
  665. return
  666. }