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.

947 lines
25 KiB

p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748) * use READ lock/unlock in ConsensusState#GetLastHeight Refs #2721 * do not use defers when there's no need * fix peer formatting (output its address instead of the pointer) ``` [54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout" ``` https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581 * panic if peer has no state https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165 It's confusing that sometimes we check if peer has a state, but most of the times we expect it to be there 1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138 2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited) I will change everything to always assume peer has a state and panic otherwise that should help identify issues earlier * abci/localclient: extend lock on app callback App callback should be protected by lock as well (note this was already done for InitChainAsync, why not for others???). Otherwise, when we execute the block, tx might come in and call the callback in the same time we're updating it in execBlockOnProxyApp => DATA RACE Fixes #2721 Consensus state is locked ``` goroutine 113333 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*RWMutex).RLock(0xc001800090) /usr/local/go/src/sync/rwmutex.go:50 +0x4e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248 9c0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236 ``` because localClient is locked ``` goroutine 1899 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670 created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132 ``` tx comes in and CheckTx is executed right when we execute the block ``` goroutine 111044 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0x0) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8 reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...) /usr/local/go/src/reflect/value.go:447 +0x449 reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344) /usr/local/go/src/reflect/value.go:308 +0xa4 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188 net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2361 +0x127 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394 net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2741 +0xab net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0) /usr/local/go/src/net/http/server.go:1847 +0x646 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2851 +0x2f5 ``` * consensus: use read lock in Receive#VoteMessage * use defer to unlock mutex because application might panic * use defer in every method of the localClient * add a changelog entry * drain channels before Unsubscribe(All) Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13 for the detailed explanation of the issue. We'll need to fix it someday. Make sure to keep an eye on https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md * retry instead of panic when peer has no state in reactors other than consensus in /dump_consensus_state RPC endpoint, skip a peer with no state * rpc/core/mempool: simplify error messages * rpc/core/mempool: use time.After instead of timer also, do not log DeliverTx result (to be consistent with other memthods) * unlock before calling the callback in reqRes#SetCallback
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
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: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
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: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
  1. // Modified for Tendermint
  2. // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
  3. // https://github.com/conformal/btcd/blob/master/LICENSE
  4. package pex
  5. import (
  6. crand "crypto/rand"
  7. "encoding/binary"
  8. "fmt"
  9. "math"
  10. mrand "math/rand"
  11. "net"
  12. "sync"
  13. "time"
  14. "github.com/minio/highwayhash"
  15. "github.com/tendermint/tendermint/crypto"
  16. tmmath "github.com/tendermint/tendermint/libs/math"
  17. tmrand "github.com/tendermint/tendermint/libs/rand"
  18. "github.com/tendermint/tendermint/libs/service"
  19. tmsync "github.com/tendermint/tendermint/libs/sync"
  20. "github.com/tendermint/tendermint/p2p"
  21. )
  22. const (
  23. bucketTypeNew = 0x01
  24. bucketTypeOld = 0x02
  25. )
  26. // AddrBook is an address book used for tracking peers
  27. // so we can gossip about them to others and select
  28. // peers to dial.
  29. // TODO: break this up?
  30. type AddrBook interface {
  31. service.Service
  32. // Add our own addresses so we don't later add ourselves
  33. AddOurAddress(*p2p.NetAddress)
  34. // Check if it is our address
  35. OurAddress(*p2p.NetAddress) bool
  36. AddPrivateIDs([]string)
  37. // Add and remove an address
  38. AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error
  39. RemoveAddress(*p2p.NetAddress)
  40. // Check if the address is in the book
  41. HasAddress(*p2p.NetAddress) bool
  42. // Do we need more peers?
  43. NeedMoreAddrs() bool
  44. // Is Address Book Empty? Answer should not depend on being in your own
  45. // address book, or private peers
  46. Empty() bool
  47. // Pick an address to dial
  48. PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress
  49. // Mark address
  50. MarkGood(p2p.NodeID)
  51. MarkAttempt(*p2p.NetAddress)
  52. MarkBad(*p2p.NetAddress, time.Duration) // Move peer to bad peers list
  53. // Add bad peers back to addrBook
  54. ReinstateBadPeers()
  55. IsGood(*p2p.NetAddress) bool
  56. IsBanned(*p2p.NetAddress) bool
  57. // Send a selection of addresses to peers
  58. GetSelection() []*p2p.NetAddress
  59. // Send a selection of addresses with bias
  60. GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress
  61. Size() int
  62. // Persist to disk
  63. Save()
  64. }
  65. var _ AddrBook = (*addrBook)(nil)
  66. // addrBook - concurrency safe peer address manager.
  67. // Implements AddrBook.
  68. type addrBook struct {
  69. service.BaseService
  70. // accessed concurrently
  71. mtx tmsync.Mutex
  72. ourAddrs map[string]struct{}
  73. privateIDs map[p2p.NodeID]struct{}
  74. addrLookup map[p2p.NodeID]*knownAddress // new & old
  75. badPeers map[p2p.NodeID]*knownAddress // blacklisted peers
  76. bucketsOld []map[string]*knownAddress
  77. bucketsNew []map[string]*knownAddress
  78. nOld int
  79. nNew int
  80. // immutable after creation
  81. filePath string
  82. key string // random prefix for bucket placement
  83. routabilityStrict bool
  84. hashKey []byte
  85. wg sync.WaitGroup
  86. }
  87. func newHashKey() []byte {
  88. result := make([]byte, highwayhash.Size)
  89. crand.Read(result) //nolint:errcheck // ignore error
  90. return result
  91. }
  92. // NewAddrBook creates a new address book.
  93. // Use Start to begin processing asynchronous address updates.
  94. func NewAddrBook(filePath string, routabilityStrict bool) AddrBook {
  95. am := &addrBook{
  96. ourAddrs: make(map[string]struct{}),
  97. privateIDs: make(map[p2p.NodeID]struct{}),
  98. addrLookup: make(map[p2p.NodeID]*knownAddress),
  99. badPeers: make(map[p2p.NodeID]*knownAddress),
  100. filePath: filePath,
  101. routabilityStrict: routabilityStrict,
  102. hashKey: newHashKey(),
  103. }
  104. am.init()
  105. am.BaseService = *service.NewBaseService(nil, "AddrBook", am)
  106. return am
  107. }
  108. // Initialize the buckets.
  109. // When modifying this, don't forget to update loadFromFile()
  110. func (a *addrBook) init() {
  111. a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
  112. // New addr buckets
  113. a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
  114. for i := range a.bucketsNew {
  115. a.bucketsNew[i] = make(map[string]*knownAddress)
  116. }
  117. // Old addr buckets
  118. a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
  119. for i := range a.bucketsOld {
  120. a.bucketsOld[i] = make(map[string]*knownAddress)
  121. }
  122. }
  123. // OnStart implements Service.
  124. func (a *addrBook) OnStart() error {
  125. if err := a.BaseService.OnStart(); err != nil {
  126. return err
  127. }
  128. a.loadFromFile(a.filePath)
  129. // wg.Add to ensure that any invocation of .Wait()
  130. // later on will wait for saveRoutine to terminate.
  131. a.wg.Add(1)
  132. go a.saveRoutine()
  133. return nil
  134. }
  135. // OnStop implements Service.
  136. func (a *addrBook) OnStop() {
  137. a.BaseService.OnStop()
  138. }
  139. func (a *addrBook) Wait() {
  140. a.wg.Wait()
  141. }
  142. func (a *addrBook) FilePath() string {
  143. return a.filePath
  144. }
  145. //-------------------------------------------------------
  146. // AddOurAddress one of our addresses.
  147. func (a *addrBook) AddOurAddress(addr *p2p.NetAddress) {
  148. a.mtx.Lock()
  149. defer a.mtx.Unlock()
  150. a.Logger.Info("Add our address to book", "addr", addr)
  151. a.ourAddrs[addr.String()] = struct{}{}
  152. }
  153. // OurAddress returns true if it is our address.
  154. func (a *addrBook) OurAddress(addr *p2p.NetAddress) bool {
  155. a.mtx.Lock()
  156. defer a.mtx.Unlock()
  157. _, ok := a.ourAddrs[addr.String()]
  158. return ok
  159. }
  160. func (a *addrBook) AddPrivateIDs(ids []string) {
  161. a.mtx.Lock()
  162. defer a.mtx.Unlock()
  163. for _, id := range ids {
  164. a.privateIDs[p2p.NodeID(id)] = struct{}{}
  165. }
  166. }
  167. // AddAddress implements AddrBook
  168. // Add address to a "new" bucket. If it's already in one, only add it probabilistically.
  169. // Returns error if the addr is non-routable. Does not add self.
  170. // NOTE: addr must not be nil
  171. func (a *addrBook) AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error {
  172. a.mtx.Lock()
  173. defer a.mtx.Unlock()
  174. return a.addAddress(addr, src)
  175. }
  176. // RemoveAddress implements AddrBook - removes the address from the book.
  177. func (a *addrBook) RemoveAddress(addr *p2p.NetAddress) {
  178. a.mtx.Lock()
  179. defer a.mtx.Unlock()
  180. a.removeAddress(addr)
  181. }
  182. // IsGood returns true if peer was ever marked as good and haven't
  183. // done anything wrong since then.
  184. func (a *addrBook) IsGood(addr *p2p.NetAddress) bool {
  185. a.mtx.Lock()
  186. defer a.mtx.Unlock()
  187. return a.addrLookup[addr.ID].isOld()
  188. }
  189. // IsBanned returns true if the peer is currently banned
  190. func (a *addrBook) IsBanned(addr *p2p.NetAddress) bool {
  191. a.mtx.Lock()
  192. _, ok := a.badPeers[addr.ID]
  193. a.mtx.Unlock()
  194. return ok
  195. }
  196. // HasAddress returns true if the address is in the book.
  197. func (a *addrBook) HasAddress(addr *p2p.NetAddress) bool {
  198. a.mtx.Lock()
  199. defer a.mtx.Unlock()
  200. ka := a.addrLookup[addr.ID]
  201. return ka != nil
  202. }
  203. // NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book.
  204. func (a *addrBook) NeedMoreAddrs() bool {
  205. return a.Size() < needAddressThreshold
  206. }
  207. // Empty implements AddrBook - returns true if there are no addresses in the address book.
  208. // Does not count the peer appearing in its own address book, or private peers.
  209. func (a *addrBook) Empty() bool {
  210. return a.Size() == 0
  211. }
  212. // PickAddress implements AddrBook. It picks an address to connect to.
  213. // The address is picked randomly from an old or new bucket according
  214. // to the biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to that range)
  215. // and determines how biased we are to pick an address from a new bucket.
  216. // PickAddress returns nil if the AddrBook is empty or if we try to pick
  217. // from an empty bucket.
  218. // nolint:gosec // G404: Use of weak random number generator
  219. func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress {
  220. a.mtx.Lock()
  221. defer a.mtx.Unlock()
  222. bookSize := a.size()
  223. if bookSize <= 0 {
  224. if bookSize < 0 {
  225. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  226. }
  227. return nil
  228. }
  229. if biasTowardsNewAddrs > 100 {
  230. biasTowardsNewAddrs = 100
  231. }
  232. if biasTowardsNewAddrs < 0 {
  233. biasTowardsNewAddrs = 0
  234. }
  235. // Bias between new and old addresses.
  236. oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(biasTowardsNewAddrs))
  237. newCorrelation := math.Sqrt(float64(a.nNew)) * float64(biasTowardsNewAddrs)
  238. // pick a random peer from a random bucket
  239. var bucket map[string]*knownAddress
  240. pickFromOldBucket := (newCorrelation+oldCorrelation)*mrand.Float64() < oldCorrelation
  241. if (pickFromOldBucket && a.nOld == 0) ||
  242. (!pickFromOldBucket && a.nNew == 0) {
  243. return nil
  244. }
  245. // loop until we pick a random non-empty bucket
  246. for len(bucket) == 0 {
  247. if pickFromOldBucket {
  248. bucket = a.bucketsOld[mrand.Intn(len(a.bucketsOld))]
  249. } else {
  250. bucket = a.bucketsNew[mrand.Intn(len(a.bucketsNew))]
  251. }
  252. }
  253. // pick a random index and loop over the map to return that index
  254. randIndex := mrand.Intn(len(bucket))
  255. for _, ka := range bucket {
  256. if randIndex == 0 {
  257. return ka.Addr
  258. }
  259. randIndex--
  260. }
  261. return nil
  262. }
  263. // MarkGood implements AddrBook - it marks the peer as good and
  264. // moves it into an "old" bucket.
  265. func (a *addrBook) MarkGood(id p2p.NodeID) {
  266. a.mtx.Lock()
  267. defer a.mtx.Unlock()
  268. ka := a.addrLookup[id]
  269. if ka == nil {
  270. return
  271. }
  272. ka.markGood()
  273. if ka.isNew() {
  274. if err := a.moveToOld(ka); err != nil {
  275. a.Logger.Error("Error moving address to old", "err", err)
  276. }
  277. }
  278. }
  279. // MarkAttempt implements AddrBook - it marks that an attempt was made to connect to the address.
  280. func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) {
  281. a.mtx.Lock()
  282. defer a.mtx.Unlock()
  283. ka := a.addrLookup[addr.ID]
  284. if ka == nil {
  285. return
  286. }
  287. ka.markAttempt()
  288. }
  289. // MarkBad implements AddrBook. Kicks address out from book, places
  290. // the address in the badPeers pool.
  291. func (a *addrBook) MarkBad(addr *p2p.NetAddress, banTime time.Duration) {
  292. a.mtx.Lock()
  293. defer a.mtx.Unlock()
  294. if a.addBadPeer(addr, banTime) {
  295. a.removeAddress(addr)
  296. }
  297. }
  298. // ReinstateBadPeers removes bad peers from ban list and places them into a new
  299. // bucket.
  300. func (a *addrBook) ReinstateBadPeers() {
  301. a.mtx.Lock()
  302. defer a.mtx.Unlock()
  303. for _, ka := range a.badPeers {
  304. if ka.isBanned() {
  305. continue
  306. }
  307. bucket, err := a.calcNewBucket(ka.Addr, ka.Src)
  308. if err != nil {
  309. a.Logger.Error("Failed to calculate new bucket (bad peer won't be reinstantiated)",
  310. "addr", ka.Addr, "err", err)
  311. continue
  312. }
  313. if err := a.addToNewBucket(ka, bucket); err != nil {
  314. a.Logger.Error("Error adding peer to new bucket", "err", err)
  315. }
  316. delete(a.badPeers, ka.ID())
  317. a.Logger.Info("Reinstated address", "addr", ka.Addr)
  318. }
  319. }
  320. // GetSelection implements AddrBook.
  321. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  322. // Must never return a nil address.
  323. func (a *addrBook) GetSelection() []*p2p.NetAddress {
  324. a.mtx.Lock()
  325. defer a.mtx.Unlock()
  326. bookSize := a.size()
  327. if bookSize <= 0 {
  328. if bookSize < 0 {
  329. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  330. }
  331. return nil
  332. }
  333. numAddresses := tmmath.MaxInt(
  334. tmmath.MinInt(minGetSelection, bookSize),
  335. bookSize*getSelectionPercent/100)
  336. numAddresses = tmmath.MinInt(maxGetSelection, numAddresses)
  337. // XXX: instead of making a list of all addresses, shuffling, and slicing a random chunk,
  338. // could we just select a random numAddresses of indexes?
  339. allAddr := make([]*p2p.NetAddress, bookSize)
  340. i := 0
  341. for _, ka := range a.addrLookup {
  342. allAddr[i] = ka.Addr
  343. i++
  344. }
  345. // Fisher-Yates shuffle the array. We only need to do the first
  346. // `numAddresses' since we are throwing the rest.
  347. for i := 0; i < numAddresses; i++ {
  348. // pick a number between current index and the end
  349. // nolint:gosec // G404: Use of weak random number generator
  350. j := mrand.Intn(len(allAddr)-i) + i
  351. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  352. }
  353. // slice off the limit we are willing to share.
  354. return allAddr[:numAddresses]
  355. }
  356. func percentageOfNum(p, n int) int {
  357. return int(math.Round((float64(p) / float64(100)) * float64(n)))
  358. }
  359. // GetSelectionWithBias implements AddrBook.
  360. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  361. // Must never return a nil address.
  362. //
  363. // Each address is picked randomly from an old or new bucket according to the
  364. // biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to
  365. // that range) and determines how biased we are to pick an address from a new
  366. // bucket.
  367. func (a *addrBook) GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress {
  368. a.mtx.Lock()
  369. defer a.mtx.Unlock()
  370. bookSize := a.size()
  371. if bookSize <= 0 {
  372. if bookSize < 0 {
  373. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  374. }
  375. return nil
  376. }
  377. if biasTowardsNewAddrs > 100 {
  378. biasTowardsNewAddrs = 100
  379. }
  380. if biasTowardsNewAddrs < 0 {
  381. biasTowardsNewAddrs = 0
  382. }
  383. numAddresses := tmmath.MaxInt(
  384. tmmath.MinInt(minGetSelection, bookSize),
  385. bookSize*getSelectionPercent/100)
  386. numAddresses = tmmath.MinInt(maxGetSelection, numAddresses)
  387. // number of new addresses that, if possible, should be in the beginning of the selection
  388. // if there are no enough old addrs, will choose new addr instead.
  389. numRequiredNewAdd := tmmath.MaxInt(percentageOfNum(biasTowardsNewAddrs, numAddresses), numAddresses-a.nOld)
  390. selection := a.randomPickAddresses(bucketTypeNew, numRequiredNewAdd)
  391. selection = append(selection, a.randomPickAddresses(bucketTypeOld, numAddresses-len(selection))...)
  392. return selection
  393. }
  394. //------------------------------------------------
  395. // Size returns the number of addresses in the book.
  396. func (a *addrBook) Size() int {
  397. a.mtx.Lock()
  398. defer a.mtx.Unlock()
  399. return a.size()
  400. }
  401. func (a *addrBook) size() int {
  402. return a.nNew + a.nOld
  403. }
  404. //----------------------------------------------------------
  405. // Save persists the address book to disk.
  406. func (a *addrBook) Save() {
  407. a.saveToFile(a.filePath) // thread safe
  408. }
  409. func (a *addrBook) saveRoutine() {
  410. defer a.wg.Done()
  411. saveFileTicker := time.NewTicker(dumpAddressInterval)
  412. out:
  413. for {
  414. select {
  415. case <-saveFileTicker.C:
  416. a.saveToFile(a.filePath)
  417. case <-a.Quit():
  418. break out
  419. }
  420. }
  421. saveFileTicker.Stop()
  422. a.saveToFile(a.filePath)
  423. }
  424. //----------------------------------------------------------
  425. func (a *addrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  426. switch bucketType {
  427. case bucketTypeNew:
  428. return a.bucketsNew[bucketIdx]
  429. case bucketTypeOld:
  430. return a.bucketsOld[bucketIdx]
  431. default:
  432. panic("Invalid bucket type")
  433. }
  434. }
  435. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  436. // NOTE: currently it always returns true.
  437. func (a *addrBook) addToNewBucket(ka *knownAddress, bucketIdx int) error {
  438. // Consistency check to ensure we don't add an already known address
  439. if ka.isOld() {
  440. return errAddrBookOldAddressNewBucket{ka.Addr, bucketIdx}
  441. }
  442. addrStr := ka.Addr.String()
  443. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  444. // Already exists?
  445. if _, ok := bucket[addrStr]; ok {
  446. return nil
  447. }
  448. // Enforce max addresses.
  449. if len(bucket) > newBucketSize {
  450. a.Logger.Info("new bucket is full, expiring new")
  451. a.expireNew(bucketIdx)
  452. }
  453. // Add to bucket.
  454. bucket[addrStr] = ka
  455. // increment nNew if the peer doesnt already exist in a bucket
  456. if ka.addBucketRef(bucketIdx) == 1 {
  457. a.nNew++
  458. }
  459. // Add it to addrLookup
  460. a.addrLookup[ka.ID()] = ka
  461. return nil
  462. }
  463. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  464. func (a *addrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  465. // Sanity check
  466. if ka.isNew() {
  467. a.Logger.Error(fmt.Sprintf("Cannot add new address to old bucket: %v", ka))
  468. return false
  469. }
  470. if len(ka.Buckets) != 0 {
  471. a.Logger.Error(fmt.Sprintf("Cannot add already old address to another old bucket: %v", ka))
  472. return false
  473. }
  474. addrStr := ka.Addr.String()
  475. bucket := a.getBucket(bucketTypeOld, bucketIdx)
  476. // Already exists?
  477. if _, ok := bucket[addrStr]; ok {
  478. return true
  479. }
  480. // Enforce max addresses.
  481. if len(bucket) > oldBucketSize {
  482. return false
  483. }
  484. // Add to bucket.
  485. bucket[addrStr] = ka
  486. if ka.addBucketRef(bucketIdx) == 1 {
  487. a.nOld++
  488. }
  489. // Ensure in addrLookup
  490. a.addrLookup[ka.ID()] = ka
  491. return true
  492. }
  493. func (a *addrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  494. if ka.BucketType != bucketType {
  495. a.Logger.Error(fmt.Sprintf("Bucket type mismatch: %v", ka))
  496. return
  497. }
  498. bucket := a.getBucket(bucketType, bucketIdx)
  499. delete(bucket, ka.Addr.String())
  500. if ka.removeBucketRef(bucketIdx) == 0 {
  501. if bucketType == bucketTypeNew {
  502. a.nNew--
  503. } else {
  504. a.nOld--
  505. }
  506. delete(a.addrLookup, ka.ID())
  507. }
  508. }
  509. func (a *addrBook) removeFromAllBuckets(ka *knownAddress) {
  510. for _, bucketIdx := range ka.Buckets {
  511. bucket := a.getBucket(ka.BucketType, bucketIdx)
  512. delete(bucket, ka.Addr.String())
  513. }
  514. ka.Buckets = nil
  515. if ka.BucketType == bucketTypeNew {
  516. a.nNew--
  517. } else {
  518. a.nOld--
  519. }
  520. delete(a.addrLookup, ka.ID())
  521. }
  522. //----------------------------------------------------------
  523. func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  524. bucket := a.getBucket(bucketType, bucketIdx)
  525. var oldest *knownAddress
  526. for _, ka := range bucket {
  527. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  528. oldest = ka
  529. }
  530. }
  531. return oldest
  532. }
  533. // adds the address to a "new" bucket. if its already in one,
  534. // it only adds it probabilistically
  535. func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
  536. if addr == nil || src == nil {
  537. return ErrAddrBookNilAddr{addr, src}
  538. }
  539. if err := addr.Valid(); err != nil {
  540. return ErrAddrBookInvalidAddr{Addr: addr, AddrErr: err}
  541. }
  542. if _, ok := a.badPeers[addr.ID]; ok {
  543. return ErrAddressBanned{addr}
  544. }
  545. if _, ok := a.privateIDs[addr.ID]; ok {
  546. return ErrAddrBookPrivate{addr}
  547. }
  548. if _, ok := a.privateIDs[src.ID]; ok {
  549. return ErrAddrBookPrivateSrc{src}
  550. }
  551. // TODO: we should track ourAddrs by ID and by IP:PORT and refuse both.
  552. if _, ok := a.ourAddrs[addr.String()]; ok {
  553. return ErrAddrBookSelf{addr}
  554. }
  555. if a.routabilityStrict && !addr.Routable() {
  556. return ErrAddrBookNonRoutable{addr}
  557. }
  558. ka := a.addrLookup[addr.ID]
  559. if ka != nil {
  560. // If its already old and the address ID's are the same, ignore it.
  561. // Thereby avoiding issues with a node on the network attempting to change
  562. // the IP of a known node ID. (Which could yield an eclipse attack on the node)
  563. if ka.isOld() && ka.Addr.ID == addr.ID {
  564. return nil
  565. }
  566. // Already in max new buckets.
  567. if len(ka.Buckets) == maxNewBucketsPerAddress {
  568. return nil
  569. }
  570. // The more entries we have, the less likely we are to add more.
  571. factor := int32(2 * len(ka.Buckets))
  572. // nolint:gosec // G404: Use of weak random number generator
  573. if mrand.Int31n(factor) != 0 {
  574. return nil
  575. }
  576. } else {
  577. ka = newKnownAddress(addr, src)
  578. }
  579. bucket, err := a.calcNewBucket(addr, src)
  580. if err != nil {
  581. return err
  582. }
  583. return a.addToNewBucket(ka, bucket)
  584. }
  585. func (a *addrBook) randomPickAddresses(bucketType byte, num int) []*p2p.NetAddress {
  586. var buckets []map[string]*knownAddress
  587. switch bucketType {
  588. case bucketTypeNew:
  589. buckets = a.bucketsNew
  590. case bucketTypeOld:
  591. buckets = a.bucketsOld
  592. default:
  593. panic("unexpected bucketType")
  594. }
  595. total := 0
  596. for _, bucket := range buckets {
  597. total += len(bucket)
  598. }
  599. addresses := make([]*knownAddress, 0, total)
  600. for _, bucket := range buckets {
  601. for _, ka := range bucket {
  602. addresses = append(addresses, ka)
  603. }
  604. }
  605. selection := make([]*p2p.NetAddress, 0, num)
  606. chosenSet := make(map[string]bool, num)
  607. rand := tmrand.NewRand()
  608. rand.Shuffle(total, func(i, j int) {
  609. addresses[i], addresses[j] = addresses[j], addresses[i]
  610. })
  611. for _, addr := range addresses {
  612. if chosenSet[addr.Addr.String()] {
  613. continue
  614. }
  615. chosenSet[addr.Addr.String()] = true
  616. selection = append(selection, addr.Addr)
  617. if len(selection) >= num {
  618. return selection
  619. }
  620. }
  621. return selection
  622. }
  623. // Make space in the new buckets by expiring the really bad entries.
  624. // If no bad entries are available we remove the oldest.
  625. func (a *addrBook) expireNew(bucketIdx int) {
  626. for addrStr, ka := range a.bucketsNew[bucketIdx] {
  627. // If an entry is bad, throw it away
  628. if ka.isBad() {
  629. a.Logger.Info(fmt.Sprintf("expiring bad address %v", addrStr))
  630. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  631. return
  632. }
  633. }
  634. // If we haven't thrown out a bad entry, throw out the oldest entry
  635. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  636. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  637. }
  638. // Promotes an address from new to old. If the destination bucket is full,
  639. // demote the oldest one to a "new" bucket.
  640. // TODO: Demote more probabilistically?
  641. func (a *addrBook) moveToOld(ka *knownAddress) error {
  642. // Sanity check
  643. if ka.isOld() {
  644. a.Logger.Error(fmt.Sprintf("Cannot promote address that is already old %v", ka))
  645. return nil
  646. }
  647. if len(ka.Buckets) == 0 {
  648. a.Logger.Error(fmt.Sprintf("Cannot promote address that isn't in any new buckets %v", ka))
  649. return nil
  650. }
  651. // Remove from all (new) buckets.
  652. a.removeFromAllBuckets(ka)
  653. // It's officially old now.
  654. ka.BucketType = bucketTypeOld
  655. // Try to add it to its oldBucket destination.
  656. oldBucketIdx, err := a.calcOldBucket(ka.Addr)
  657. if err != nil {
  658. return err
  659. }
  660. added := a.addToOldBucket(ka, oldBucketIdx)
  661. if !added {
  662. // No room; move the oldest to a new bucket
  663. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  664. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  665. newBucketIdx, err := a.calcNewBucket(oldest.Addr, oldest.Src)
  666. if err != nil {
  667. return err
  668. }
  669. if err := a.addToNewBucket(oldest, newBucketIdx); err != nil {
  670. a.Logger.Error("Error adding peer to old bucket", "err", err)
  671. }
  672. // Finally, add our ka to old bucket again.
  673. added = a.addToOldBucket(ka, oldBucketIdx)
  674. if !added {
  675. a.Logger.Error(fmt.Sprintf("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  676. }
  677. }
  678. return nil
  679. }
  680. func (a *addrBook) removeAddress(addr *p2p.NetAddress) {
  681. ka := a.addrLookup[addr.ID]
  682. if ka == nil {
  683. return
  684. }
  685. a.Logger.Info("Remove address from book", "addr", addr)
  686. a.removeFromAllBuckets(ka)
  687. }
  688. func (a *addrBook) addBadPeer(addr *p2p.NetAddress, banTime time.Duration) bool {
  689. // check it exists in addrbook
  690. ka := a.addrLookup[addr.ID]
  691. // check address is not already there
  692. if ka == nil {
  693. return false
  694. }
  695. if _, alreadyBadPeer := a.badPeers[addr.ID]; !alreadyBadPeer {
  696. // add to bad peer list
  697. ka.ban(banTime)
  698. a.badPeers[addr.ID] = ka
  699. a.Logger.Info("Add address to blacklist", "addr", addr)
  700. }
  701. return true
  702. }
  703. //---------------------------------------------------------------------
  704. // calculate bucket placements
  705. // hash(key + sourcegroup + int64(hash(key + group + sourcegroup)) % bucket_per_group) % num_new_buckets
  706. func (a *addrBook) calcNewBucket(addr, src *p2p.NetAddress) (int, error) {
  707. data1 := []byte{}
  708. data1 = append(data1, []byte(a.key)...)
  709. data1 = append(data1, []byte(a.groupKey(addr))...)
  710. data1 = append(data1, []byte(a.groupKey(src))...)
  711. hash1, err := a.hash(data1)
  712. if err != nil {
  713. return 0, err
  714. }
  715. hash64 := binary.BigEndian.Uint64(hash1)
  716. hash64 %= newBucketsPerGroup
  717. var hashbuf [8]byte
  718. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  719. data2 := []byte{}
  720. data2 = append(data2, []byte(a.key)...)
  721. data2 = append(data2, a.groupKey(src)...)
  722. data2 = append(data2, hashbuf[:]...)
  723. hash2, err := a.hash(data2)
  724. if err != nil {
  725. return 0, err
  726. }
  727. result := int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  728. return result, nil
  729. }
  730. // hash(key + group + int64(hash(key + addr)) % buckets_per_group) % num_old_buckets
  731. func (a *addrBook) calcOldBucket(addr *p2p.NetAddress) (int, error) {
  732. data1 := []byte{}
  733. data1 = append(data1, []byte(a.key)...)
  734. data1 = append(data1, []byte(addr.String())...)
  735. hash1, err := a.hash(data1)
  736. if err != nil {
  737. return 0, err
  738. }
  739. hash64 := binary.BigEndian.Uint64(hash1)
  740. hash64 %= oldBucketsPerGroup
  741. var hashbuf [8]byte
  742. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  743. data2 := []byte{}
  744. data2 = append(data2, []byte(a.key)...)
  745. data2 = append(data2, a.groupKey(addr)...)
  746. data2 = append(data2, hashbuf[:]...)
  747. hash2, err := a.hash(data2)
  748. if err != nil {
  749. return 0, err
  750. }
  751. result := int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  752. return result, nil
  753. }
  754. // Return a string representing the network group of this address.
  755. // This is the /16 for IPv4 (e.g. 1.2.0.0), the /32 (/36 for he.net) for IPv6, the string
  756. // "local" for a local address and the string "unroutable" for an unroutable
  757. // address.
  758. func (a *addrBook) groupKey(na *p2p.NetAddress) string {
  759. return groupKeyFor(na, a.routabilityStrict)
  760. }
  761. func groupKeyFor(na *p2p.NetAddress, routabilityStrict bool) string {
  762. if routabilityStrict && na.Local() {
  763. return "local"
  764. }
  765. if routabilityStrict && !na.Routable() {
  766. return "unroutable"
  767. }
  768. if ipv4 := na.IP.To4(); ipv4 != nil {
  769. return na.IP.Mask(net.CIDRMask(16, 32)).String()
  770. }
  771. if na.RFC6145() || na.RFC6052() {
  772. // last four bytes are the ip address
  773. ip := na.IP[12:16]
  774. return ip.Mask(net.CIDRMask(16, 32)).String()
  775. }
  776. if na.RFC3964() {
  777. ip := na.IP[2:6]
  778. return ip.Mask(net.CIDRMask(16, 32)).String()
  779. }
  780. if na.RFC4380() {
  781. // teredo tunnels have the last 4 bytes as the v4 address XOR
  782. // 0xff.
  783. ip := net.IP(make([]byte, 4))
  784. for i, byte := range na.IP[12:16] {
  785. ip[i] = byte ^ 0xff
  786. }
  787. return ip.Mask(net.CIDRMask(16, 32)).String()
  788. }
  789. if na.OnionCatTor() {
  790. // group is keyed off the first 4 bits of the actual onion key.
  791. return fmt.Sprintf("tor:%d", na.IP[6]&((1<<4)-1))
  792. }
  793. // OK, so now we know ourselves to be a IPv6 address.
  794. // bitcoind uses /32 for everything, except for Hurricane Electric's
  795. // (he.net) IP range, which it uses /36 for.
  796. bits := 32
  797. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"), Mask: net.CIDRMask(32, 128)}
  798. if heNet.Contains(na.IP) {
  799. bits = 36
  800. }
  801. ipv6Mask := net.CIDRMask(bits, 128)
  802. return na.IP.Mask(ipv6Mask).String()
  803. }
  804. func (a *addrBook) hash(b []byte) ([]byte, error) {
  805. hasher, err := highwayhash.New64(a.hashKey)
  806. if err != nil {
  807. return nil, err
  808. }
  809. hasher.Write(b)
  810. return hasher.Sum(nil), nil
  811. }