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.

928 lines
24 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. "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. "github.com/tendermint/tendermint/p2p"
  20. )
  21. const (
  22. bucketTypeNew = 0x01
  23. bucketTypeOld = 0x02
  24. )
  25. // AddrBook is an address book used for tracking peers
  26. // so we can gossip about them to others and select
  27. // peers to dial.
  28. // TODO: break this up?
  29. type AddrBook interface {
  30. service.Service
  31. // Add our own addresses so we don't later add ourselves
  32. AddOurAddress(*p2p.NetAddress)
  33. // Check if it is our address
  34. OurAddress(*p2p.NetAddress) bool
  35. AddPrivateIDs([]string)
  36. // Add and remove an address
  37. AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error
  38. RemoveAddress(*p2p.NetAddress)
  39. // Check if the address is in the book
  40. HasAddress(*p2p.NetAddress) bool
  41. // Do we need more peers?
  42. NeedMoreAddrs() bool
  43. // Is Address Book Empty? Answer should not depend on being in your own
  44. // address book, or private peers
  45. Empty() bool
  46. // Pick an address to dial
  47. PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress
  48. // Mark address
  49. MarkGood(p2p.ID)
  50. MarkAttempt(*p2p.NetAddress)
  51. MarkBad(*p2p.NetAddress, time.Duration) // Move peer to bad peers list
  52. // Add bad peers back to addrBook
  53. ReinstateBadPeers()
  54. IsGood(*p2p.NetAddress) bool
  55. IsBanned(*p2p.NetAddress) bool
  56. // Send a selection of addresses to peers
  57. GetSelection() []*p2p.NetAddress
  58. // Send a selection of addresses with bias
  59. GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress
  60. Size() int
  61. // Persist to disk
  62. Save()
  63. }
  64. var _ AddrBook = (*addrBook)(nil)
  65. // addrBook - concurrency safe peer address manager.
  66. // Implements AddrBook.
  67. type addrBook struct {
  68. service.BaseService
  69. // accessed concurrently
  70. mtx sync.Mutex
  71. rand *tmrand.Rand
  72. ourAddrs map[string]struct{}
  73. privateIDs map[p2p.ID]struct{}
  74. addrLookup map[p2p.ID]*knownAddress // new & old
  75. badPeers map[p2p.ID]*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)
  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. rand: tmrand.NewRand(),
  97. ourAddrs: make(map[string]struct{}),
  98. privateIDs: make(map[p2p.ID]struct{}),
  99. addrLookup: make(map[p2p.ID]*knownAddress),
  100. badPeers: make(map[p2p.ID]*knownAddress),
  101. filePath: filePath,
  102. routabilityStrict: routabilityStrict,
  103. hashKey: newHashKey(),
  104. }
  105. am.init()
  106. am.BaseService = *service.NewBaseService(nil, "AddrBook", am)
  107. return am
  108. }
  109. // Initialize the buckets.
  110. // When modifying this, don't forget to update loadFromFile()
  111. func (a *addrBook) init() {
  112. a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
  113. // New addr buckets
  114. a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
  115. for i := range a.bucketsNew {
  116. a.bucketsNew[i] = make(map[string]*knownAddress)
  117. }
  118. // Old addr buckets
  119. a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
  120. for i := range a.bucketsOld {
  121. a.bucketsOld[i] = make(map[string]*knownAddress)
  122. }
  123. }
  124. // OnStart implements Service.
  125. func (a *addrBook) OnStart() error {
  126. if err := a.BaseService.OnStart(); err != nil {
  127. return err
  128. }
  129. a.loadFromFile(a.filePath)
  130. // wg.Add to ensure that any invocation of .Wait()
  131. // later on will wait for saveRoutine to terminate.
  132. a.wg.Add(1)
  133. go a.saveRoutine()
  134. return nil
  135. }
  136. // OnStop implements Service.
  137. func (a *addrBook) OnStop() {
  138. a.BaseService.OnStop()
  139. }
  140. func (a *addrBook) Wait() {
  141. a.wg.Wait()
  142. }
  143. func (a *addrBook) FilePath() string {
  144. return a.filePath
  145. }
  146. //-------------------------------------------------------
  147. // AddOurAddress one of our addresses.
  148. func (a *addrBook) AddOurAddress(addr *p2p.NetAddress) {
  149. a.mtx.Lock()
  150. defer a.mtx.Unlock()
  151. a.Logger.Info("Add our address to book", "addr", addr)
  152. a.ourAddrs[addr.String()] = struct{}{}
  153. }
  154. // OurAddress returns true if it is our address.
  155. func (a *addrBook) OurAddress(addr *p2p.NetAddress) bool {
  156. a.mtx.Lock()
  157. defer a.mtx.Unlock()
  158. _, ok := a.ourAddrs[addr.String()]
  159. return ok
  160. }
  161. func (a *addrBook) AddPrivateIDs(ids []string) {
  162. a.mtx.Lock()
  163. defer a.mtx.Unlock()
  164. for _, id := range ids {
  165. a.privateIDs[p2p.ID(id)] = struct{}{}
  166. }
  167. }
  168. // AddAddress implements AddrBook
  169. // Add address to a "new" bucket. If it's already in one, only add it probabilistically.
  170. // Returns error if the addr is non-routable. Does not add self.
  171. // NOTE: addr must not be nil
  172. func (a *addrBook) AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error {
  173. a.mtx.Lock()
  174. defer a.mtx.Unlock()
  175. return a.addAddress(addr, src)
  176. }
  177. // RemoveAddress implements AddrBook - removes the address from the book.
  178. func (a *addrBook) RemoveAddress(addr *p2p.NetAddress) {
  179. a.mtx.Lock()
  180. defer a.mtx.Unlock()
  181. a.removeAddress(addr)
  182. }
  183. // IsGood returns true if peer was ever marked as good and haven't
  184. // done anything wrong since then.
  185. func (a *addrBook) IsGood(addr *p2p.NetAddress) bool {
  186. a.mtx.Lock()
  187. defer a.mtx.Unlock()
  188. return a.addrLookup[addr.ID].isOld()
  189. }
  190. // IsBanned returns true if the peer is currently banned
  191. func (a *addrBook) IsBanned(addr *p2p.NetAddress) bool {
  192. a.mtx.Lock()
  193. _, ok := a.badPeers[addr.ID]
  194. a.mtx.Unlock()
  195. return ok
  196. }
  197. // HasAddress returns true if the address is in the book.
  198. func (a *addrBook) HasAddress(addr *p2p.NetAddress) bool {
  199. a.mtx.Lock()
  200. defer a.mtx.Unlock()
  201. ka := a.addrLookup[addr.ID]
  202. return ka != nil
  203. }
  204. // NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book.
  205. func (a *addrBook) NeedMoreAddrs() bool {
  206. return a.Size() < needAddressThreshold
  207. }
  208. // Empty implements AddrBook - returns true if there are no addresses in the address book.
  209. // Does not count the peer appearing in its own address book, or private peers.
  210. func (a *addrBook) Empty() bool {
  211. return a.Size() == 0
  212. }
  213. // PickAddress implements AddrBook. It picks an address to connect to.
  214. // The address is picked randomly from an old or new bucket according
  215. // to the biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to that range)
  216. // and determines how biased we are to pick an address from a new bucket.
  217. // PickAddress returns nil if the AddrBook is empty or if we try to pick
  218. // from an empty bucket.
  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)*a.rand.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[a.rand.Intn(len(a.bucketsOld))]
  249. } else {
  250. bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
  251. }
  252. }
  253. // pick a random index and loop over the map to return that index
  254. randIndex := a.rand.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.ID) {
  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. a.moveToOld(ka)
  275. }
  276. }
  277. // MarkAttempt implements AddrBook - it marks that an attempt was made to connect to the address.
  278. func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) {
  279. a.mtx.Lock()
  280. defer a.mtx.Unlock()
  281. ka := a.addrLookup[addr.ID]
  282. if ka == nil {
  283. return
  284. }
  285. ka.markAttempt()
  286. }
  287. // MarkBad implements AddrBook. Kicks address out from book, places
  288. // the address in the badPeers pool.
  289. func (a *addrBook) MarkBad(addr *p2p.NetAddress, banTime time.Duration) {
  290. a.mtx.Lock()
  291. defer a.mtx.Unlock()
  292. if a.addBadPeer(addr, banTime) {
  293. a.removeAddress(addr)
  294. }
  295. }
  296. // ReinstateBadPeers removes bad peers from ban list and places them into a new
  297. // bucket.
  298. func (a *addrBook) ReinstateBadPeers() {
  299. a.mtx.Lock()
  300. defer a.mtx.Unlock()
  301. for _, ka := range a.badPeers {
  302. if ka.isBanned() {
  303. continue
  304. }
  305. bucket, err := a.calcNewBucket(ka.Addr, ka.Src)
  306. if err != nil {
  307. a.Logger.Error("Failed to calculate new bucket (bad peer won't be reinstantiated)",
  308. "addr", ka.Addr, "err", err)
  309. continue
  310. }
  311. a.addToNewBucket(ka, bucket)
  312. delete(a.badPeers, ka.ID())
  313. a.Logger.Info("Reinstated address", "addr", ka.Addr)
  314. }
  315. }
  316. // GetSelection implements AddrBook.
  317. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  318. // Must never return a nil address.
  319. func (a *addrBook) GetSelection() []*p2p.NetAddress {
  320. a.mtx.Lock()
  321. defer a.mtx.Unlock()
  322. bookSize := a.size()
  323. if bookSize <= 0 {
  324. if bookSize < 0 {
  325. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  326. }
  327. return nil
  328. }
  329. numAddresses := tmmath.MaxInt(
  330. tmmath.MinInt(minGetSelection, bookSize),
  331. bookSize*getSelectionPercent/100)
  332. numAddresses = tmmath.MinInt(maxGetSelection, numAddresses)
  333. // XXX: instead of making a list of all addresses, shuffling, and slicing a random chunk,
  334. // could we just select a random numAddresses of indexes?
  335. allAddr := make([]*p2p.NetAddress, bookSize)
  336. i := 0
  337. for _, ka := range a.addrLookup {
  338. allAddr[i] = ka.Addr
  339. i++
  340. }
  341. // Fisher-Yates shuffle the array. We only need to do the first
  342. // `numAddresses' since we are throwing the rest.
  343. for i := 0; i < numAddresses; i++ {
  344. // pick a number between current index and the end
  345. j := tmrand.Intn(len(allAddr)-i) + i
  346. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  347. }
  348. // slice off the limit we are willing to share.
  349. return allAddr[:numAddresses]
  350. }
  351. func percentageOfNum(p, n int) int {
  352. return int(math.Round((float64(p) / float64(100)) * float64(n)))
  353. }
  354. // GetSelectionWithBias implements AddrBook.
  355. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  356. // Must never return a nil address.
  357. //
  358. // Each address is picked randomly from an old or new bucket according to the
  359. // biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to
  360. // that range) and determines how biased we are to pick an address from a new
  361. // bucket.
  362. func (a *addrBook) GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress {
  363. a.mtx.Lock()
  364. defer a.mtx.Unlock()
  365. bookSize := a.size()
  366. if bookSize <= 0 {
  367. if bookSize < 0 {
  368. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  369. }
  370. return nil
  371. }
  372. if biasTowardsNewAddrs > 100 {
  373. biasTowardsNewAddrs = 100
  374. }
  375. if biasTowardsNewAddrs < 0 {
  376. biasTowardsNewAddrs = 0
  377. }
  378. numAddresses := tmmath.MaxInt(
  379. tmmath.MinInt(minGetSelection, bookSize),
  380. bookSize*getSelectionPercent/100)
  381. numAddresses = tmmath.MinInt(maxGetSelection, numAddresses)
  382. // number of new addresses that, if possible, should be in the beginning of the selection
  383. // if there are no enough old addrs, will choose new addr instead.
  384. numRequiredNewAdd := tmmath.MaxInt(percentageOfNum(biasTowardsNewAddrs, numAddresses), numAddresses-a.nOld)
  385. selection := a.randomPickAddresses(bucketTypeNew, numRequiredNewAdd)
  386. selection = append(selection, a.randomPickAddresses(bucketTypeOld, numAddresses-len(selection))...)
  387. return selection
  388. }
  389. //------------------------------------------------
  390. // Size returns the number of addresses in the book.
  391. func (a *addrBook) Size() int {
  392. a.mtx.Lock()
  393. defer a.mtx.Unlock()
  394. return a.size()
  395. }
  396. func (a *addrBook) size() int {
  397. return a.nNew + a.nOld
  398. }
  399. //----------------------------------------------------------
  400. // Save persists the address book to disk.
  401. func (a *addrBook) Save() {
  402. a.saveToFile(a.filePath) // thread safe
  403. }
  404. func (a *addrBook) saveRoutine() {
  405. defer a.wg.Done()
  406. saveFileTicker := time.NewTicker(dumpAddressInterval)
  407. out:
  408. for {
  409. select {
  410. case <-saveFileTicker.C:
  411. a.saveToFile(a.filePath)
  412. case <-a.Quit():
  413. break out
  414. }
  415. }
  416. saveFileTicker.Stop()
  417. a.saveToFile(a.filePath)
  418. }
  419. //----------------------------------------------------------
  420. func (a *addrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  421. switch bucketType {
  422. case bucketTypeNew:
  423. return a.bucketsNew[bucketIdx]
  424. case bucketTypeOld:
  425. return a.bucketsOld[bucketIdx]
  426. default:
  427. panic("Invalid bucket type")
  428. }
  429. }
  430. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  431. // NOTE: currently it always returns true.
  432. func (a *addrBook) addToNewBucket(ka *knownAddress, bucketIdx int) {
  433. // Sanity check
  434. if ka.isOld() {
  435. a.Logger.Error("Failed Sanity Check! Cant add old address to new bucket", "ka", ka, "bucket", bucketIdx)
  436. return
  437. }
  438. addrStr := ka.Addr.String()
  439. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  440. // Already exists?
  441. if _, ok := bucket[addrStr]; ok {
  442. return
  443. }
  444. // Enforce max addresses.
  445. if len(bucket) > newBucketSize {
  446. a.Logger.Info("new bucket is full, expiring new")
  447. a.expireNew(bucketIdx)
  448. }
  449. // Add to bucket.
  450. bucket[addrStr] = ka
  451. // increment nNew if the peer doesnt already exist in a bucket
  452. if ka.addBucketRef(bucketIdx) == 1 {
  453. a.nNew++
  454. }
  455. // Add it to addrLookup
  456. a.addrLookup[ka.ID()] = ka
  457. }
  458. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  459. func (a *addrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  460. // Sanity check
  461. if ka.isNew() {
  462. a.Logger.Error(fmt.Sprintf("Cannot add new address to old bucket: %v", ka))
  463. return false
  464. }
  465. if len(ka.Buckets) != 0 {
  466. a.Logger.Error(fmt.Sprintf("Cannot add already old address to another old bucket: %v", ka))
  467. return false
  468. }
  469. addrStr := ka.Addr.String()
  470. bucket := a.getBucket(bucketTypeOld, bucketIdx)
  471. // Already exists?
  472. if _, ok := bucket[addrStr]; ok {
  473. return true
  474. }
  475. // Enforce max addresses.
  476. if len(bucket) > oldBucketSize {
  477. return false
  478. }
  479. // Add to bucket.
  480. bucket[addrStr] = ka
  481. if ka.addBucketRef(bucketIdx) == 1 {
  482. a.nOld++
  483. }
  484. // Ensure in addrLookup
  485. a.addrLookup[ka.ID()] = ka
  486. return true
  487. }
  488. func (a *addrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  489. if ka.BucketType != bucketType {
  490. a.Logger.Error(fmt.Sprintf("Bucket type mismatch: %v", ka))
  491. return
  492. }
  493. bucket := a.getBucket(bucketType, bucketIdx)
  494. delete(bucket, ka.Addr.String())
  495. if ka.removeBucketRef(bucketIdx) == 0 {
  496. if bucketType == bucketTypeNew {
  497. a.nNew--
  498. } else {
  499. a.nOld--
  500. }
  501. delete(a.addrLookup, ka.ID())
  502. }
  503. }
  504. func (a *addrBook) removeFromAllBuckets(ka *knownAddress) {
  505. for _, bucketIdx := range ka.Buckets {
  506. bucket := a.getBucket(ka.BucketType, bucketIdx)
  507. delete(bucket, ka.Addr.String())
  508. }
  509. ka.Buckets = nil
  510. if ka.BucketType == bucketTypeNew {
  511. a.nNew--
  512. } else {
  513. a.nOld--
  514. }
  515. delete(a.addrLookup, ka.ID())
  516. }
  517. //----------------------------------------------------------
  518. func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  519. bucket := a.getBucket(bucketType, bucketIdx)
  520. var oldest *knownAddress
  521. for _, ka := range bucket {
  522. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  523. oldest = ka
  524. }
  525. }
  526. return oldest
  527. }
  528. // adds the address to a "new" bucket. if its already in one,
  529. // it only adds it probabilistically
  530. func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
  531. if addr == nil || src == nil {
  532. return ErrAddrBookNilAddr{addr, src}
  533. }
  534. if err := addr.Valid(); err != nil {
  535. return ErrAddrBookInvalidAddr{Addr: addr, AddrErr: err}
  536. }
  537. if _, ok := a.badPeers[addr.ID]; ok {
  538. return ErrAddressBanned{addr}
  539. }
  540. if _, ok := a.privateIDs[addr.ID]; ok {
  541. return ErrAddrBookPrivate{addr}
  542. }
  543. if _, ok := a.privateIDs[src.ID]; ok {
  544. return ErrAddrBookPrivateSrc{src}
  545. }
  546. // TODO: we should track ourAddrs by ID and by IP:PORT and refuse both.
  547. if _, ok := a.ourAddrs[addr.String()]; ok {
  548. return ErrAddrBookSelf{addr}
  549. }
  550. if a.routabilityStrict && !addr.Routable() {
  551. return ErrAddrBookNonRoutable{addr}
  552. }
  553. ka := a.addrLookup[addr.ID]
  554. if ka != nil {
  555. // If its already old and the addr is the same, ignore it.
  556. if ka.isOld() && ka.Addr.Equals(addr) {
  557. return nil
  558. }
  559. // Already in max new buckets.
  560. if len(ka.Buckets) == maxNewBucketsPerAddress {
  561. return nil
  562. }
  563. // The more entries we have, the less likely we are to add more.
  564. factor := int32(2 * len(ka.Buckets))
  565. if a.rand.Int31n(factor) != 0 {
  566. return nil
  567. }
  568. } else {
  569. ka = newKnownAddress(addr, src)
  570. }
  571. bucket, err := a.calcNewBucket(addr, src)
  572. if err != nil {
  573. return err
  574. }
  575. a.addToNewBucket(ka, bucket)
  576. return nil
  577. }
  578. func (a *addrBook) randomPickAddresses(bucketType byte, num int) []*p2p.NetAddress {
  579. var buckets []map[string]*knownAddress
  580. switch bucketType {
  581. case bucketTypeNew:
  582. buckets = a.bucketsNew
  583. case bucketTypeOld:
  584. buckets = a.bucketsOld
  585. default:
  586. panic("unexpected bucketType")
  587. }
  588. total := 0
  589. for _, bucket := range buckets {
  590. total += len(bucket)
  591. }
  592. addresses := make([]*knownAddress, 0, total)
  593. for _, bucket := range buckets {
  594. for _, ka := range bucket {
  595. addresses = append(addresses, ka)
  596. }
  597. }
  598. selection := make([]*p2p.NetAddress, 0, num)
  599. chosenSet := make(map[string]bool, num)
  600. rand.Shuffle(total, func(i, j int) {
  601. addresses[i], addresses[j] = addresses[j], addresses[i]
  602. })
  603. for _, addr := range addresses {
  604. if chosenSet[addr.Addr.String()] {
  605. continue
  606. }
  607. chosenSet[addr.Addr.String()] = true
  608. selection = append(selection, addr.Addr)
  609. if len(selection) >= num {
  610. return selection
  611. }
  612. }
  613. return selection
  614. }
  615. // Make space in the new buckets by expiring the really bad entries.
  616. // If no bad entries are available we remove the oldest.
  617. func (a *addrBook) expireNew(bucketIdx int) {
  618. for addrStr, ka := range a.bucketsNew[bucketIdx] {
  619. // If an entry is bad, throw it away
  620. if ka.isBad() {
  621. a.Logger.Info(fmt.Sprintf("expiring bad address %v", addrStr))
  622. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  623. return
  624. }
  625. }
  626. // If we haven't thrown out a bad entry, throw out the oldest entry
  627. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  628. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  629. }
  630. // Promotes an address from new to old. If the destination bucket is full,
  631. // demote the oldest one to a "new" bucket.
  632. // TODO: Demote more probabilistically?
  633. func (a *addrBook) moveToOld(ka *knownAddress) error {
  634. // Sanity check
  635. if ka.isOld() {
  636. a.Logger.Error(fmt.Sprintf("Cannot promote address that is already old %v", ka))
  637. return nil
  638. }
  639. if len(ka.Buckets) == 0 {
  640. a.Logger.Error(fmt.Sprintf("Cannot promote address that isn't in any new buckets %v", ka))
  641. return nil
  642. }
  643. // Remove from all (new) buckets.
  644. a.removeFromAllBuckets(ka)
  645. // It's officially old now.
  646. ka.BucketType = bucketTypeOld
  647. // Try to add it to its oldBucket destination.
  648. oldBucketIdx, err := a.calcOldBucket(ka.Addr)
  649. if err != nil {
  650. return err
  651. }
  652. added := a.addToOldBucket(ka, oldBucketIdx)
  653. if !added {
  654. // No room; move the oldest to a new bucket
  655. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  656. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  657. newBucketIdx, err := a.calcNewBucket(oldest.Addr, oldest.Src)
  658. if err != nil {
  659. return err
  660. }
  661. a.addToNewBucket(oldest, newBucketIdx)
  662. // Finally, add our ka to old bucket again.
  663. added = a.addToOldBucket(ka, oldBucketIdx)
  664. if !added {
  665. a.Logger.Error(fmt.Sprintf("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  666. }
  667. }
  668. return nil
  669. }
  670. func (a *addrBook) removeAddress(addr *p2p.NetAddress) {
  671. ka := a.addrLookup[addr.ID]
  672. if ka == nil {
  673. return
  674. }
  675. a.Logger.Info("Remove address from book", "addr", addr)
  676. a.removeFromAllBuckets(ka)
  677. }
  678. func (a *addrBook) addBadPeer(addr *p2p.NetAddress, banTime time.Duration) bool {
  679. // check it exists in addrbook
  680. ka := a.addrLookup[addr.ID]
  681. // check address is not already there
  682. if ka == nil {
  683. return false
  684. }
  685. if _, alreadyBadPeer := a.badPeers[addr.ID]; !alreadyBadPeer {
  686. // add to bad peer list
  687. ka.ban(banTime)
  688. a.badPeers[addr.ID] = ka
  689. a.Logger.Info("Add address to blacklist", "addr", addr)
  690. }
  691. return true
  692. }
  693. //---------------------------------------------------------------------
  694. // calculate bucket placements
  695. // hash(key + sourcegroup + int64(hash(key + group + sourcegroup)) % bucket_per_group) % num_new_buckets
  696. func (a *addrBook) calcNewBucket(addr, src *p2p.NetAddress) (int, error) {
  697. data1 := []byte{}
  698. data1 = append(data1, []byte(a.key)...)
  699. data1 = append(data1, []byte(a.groupKey(addr))...)
  700. data1 = append(data1, []byte(a.groupKey(src))...)
  701. hash1, err := a.hash(data1)
  702. if err != nil {
  703. return 0, err
  704. }
  705. hash64 := binary.BigEndian.Uint64(hash1)
  706. hash64 %= newBucketsPerGroup
  707. var hashbuf [8]byte
  708. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  709. data2 := []byte{}
  710. data2 = append(data2, []byte(a.key)...)
  711. data2 = append(data2, a.groupKey(src)...)
  712. data2 = append(data2, hashbuf[:]...)
  713. hash2, err := a.hash(data2)
  714. if err != nil {
  715. return 0, err
  716. }
  717. result := int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  718. return result, nil
  719. }
  720. // hash(key + group + int64(hash(key + addr)) % buckets_per_group) % num_old_buckets
  721. func (a *addrBook) calcOldBucket(addr *p2p.NetAddress) (int, error) {
  722. data1 := []byte{}
  723. data1 = append(data1, []byte(a.key)...)
  724. data1 = append(data1, []byte(addr.String())...)
  725. hash1, err := a.hash(data1)
  726. if err != nil {
  727. return 0, err
  728. }
  729. hash64 := binary.BigEndian.Uint64(hash1)
  730. hash64 %= oldBucketsPerGroup
  731. var hashbuf [8]byte
  732. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  733. data2 := []byte{}
  734. data2 = append(data2, []byte(a.key)...)
  735. data2 = append(data2, a.groupKey(addr)...)
  736. data2 = append(data2, hashbuf[:]...)
  737. hash2, err := a.hash(data2)
  738. if err != nil {
  739. return 0, err
  740. }
  741. result := int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  742. return result, nil
  743. }
  744. // Return a string representing the network group of this address.
  745. // This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
  746. // "local" for a local address and the string "unroutable" for an unroutable
  747. // address.
  748. func (a *addrBook) groupKey(na *p2p.NetAddress) string {
  749. if a.routabilityStrict && na.Local() {
  750. return "local"
  751. }
  752. if a.routabilityStrict && !na.Routable() {
  753. return "unroutable"
  754. }
  755. if ipv4 := na.IP.To4(); ipv4 != nil {
  756. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(16, 32)}).String()
  757. }
  758. if na.RFC6145() || na.RFC6052() {
  759. // last four bytes are the ip address
  760. ip := na.IP[12:16]
  761. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  762. }
  763. if na.RFC3964() {
  764. ip := na.IP[2:7]
  765. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  766. }
  767. if na.RFC4380() {
  768. // teredo tunnels have the last 4 bytes as the v4 address XOR
  769. // 0xff.
  770. ip := net.IP(make([]byte, 4))
  771. for i, byte := range na.IP[12:16] {
  772. ip[i] = byte ^ 0xff
  773. }
  774. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  775. }
  776. // OK, so now we know ourselves to be a IPv6 address.
  777. // bitcoind uses /32 for everything, except for Hurricane Electric's
  778. // (he.net) IP range, which it uses /36 for.
  779. bits := 32
  780. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"),
  781. Mask: net.CIDRMask(32, 128)}
  782. if heNet.Contains(na.IP) {
  783. bits = 36
  784. }
  785. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(bits, 128)}).String()
  786. }
  787. func (a *addrBook) hash(b []byte) ([]byte, error) {
  788. hasher, err := highwayhash.New64(a.hashKey)
  789. if err != nil {
  790. return nil, err
  791. }
  792. hasher.Write(b)
  793. return hasher.Sum(nil), nil
  794. }