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.

827 lines
22 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
5 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
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
5 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
5 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
5 years ago
p2p: 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
5 years ago
p2p: fix infinite loop in addrbook (#3232) * failing test * fix infinite loop in addrbook There are cases where we only have a small number of addresses marked good ("old"), but the selection mechanism keeps trying to select more of these addresses, and hence ends up in an infinite loop. Here we fix this to only try and select such "old" addresses if we have enough of them. Note this means, if we don't have enough of them, we may return more "new" addresses than otherwise expected by the newSelectionBias. This whole GetSelectionWithBias method probably needs to be rewritten, but this is a quick fix for the issue. * changelog * fix infinite loop if not enough new addrs * fix another potential infinite loop if a.nNew == 0 -> pickFromOldBucket=true, but we don't have enough items (a.nOld > len(oldBucketToAddrsMap) false) * Revert "fix another potential infinite loop" This reverts commit 146540c1125597162bd89820d611f6531f5e5e4b. * check num addresses instead of buckets, new test * fixed the int division * add slack to bias % in test, lint fixes * Added checks for selection content in test * test cleanup * Apply suggestions from code review Co-Authored-By: ebuchman <ethan@coinculture.info> * address review comments * change after Anton's review comments * use the same docker image we use for testing when building a binary for localnet * switch back to circleci classic * more review comments * more review comments * refactor addrbook_test * build linux binary inside docker in attempt to fix ``` --> Running dep + make build-linux GOOS=linux GOARCH=amd64 make build make[1]: Entering directory `/home/circleci/.go_workspace/src/github.com/tendermint/tendermint' CGO_ENABLED=0 go build -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD`" -tags 'tendermint' -o build/tendermint ./cmd/tendermint/ p2p/pex/addrbook.go:373:13: undefined: math.Round ``` * change dir from /usr to /go * use concrete Go version for localnet binary * check for nil addresses just to be sure
5 years ago
p2p: 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
5 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. "crypto/sha256"
  7. "encoding/binary"
  8. "fmt"
  9. "math"
  10. "math/rand"
  11. "net"
  12. "sync"
  13. "time"
  14. "github.com/tendermint/tendermint/crypto"
  15. cmn "github.com/tendermint/tendermint/libs/common"
  16. "github.com/tendermint/tendermint/p2p"
  17. )
  18. const (
  19. bucketTypeNew = 0x01
  20. bucketTypeOld = 0x02
  21. )
  22. // AddrBook is an address book used for tracking peers
  23. // so we can gossip about them to others and select
  24. // peers to dial.
  25. // TODO: break this up?
  26. type AddrBook interface {
  27. cmn.Service
  28. // Add our own addresses so we don't later add ourselves
  29. AddOurAddress(*p2p.NetAddress)
  30. // Check if it is our address
  31. OurAddress(*p2p.NetAddress) bool
  32. AddPrivateIDs([]string)
  33. // Add and remove an address
  34. AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error
  35. RemoveAddress(*p2p.NetAddress)
  36. // Check if the address is in the book
  37. HasAddress(*p2p.NetAddress) bool
  38. // Do we need more peers?
  39. NeedMoreAddrs() bool
  40. // Is Address Book Empty? Answer should not depend on being in your own
  41. // address book, or private peers
  42. Empty() bool
  43. // Pick an address to dial
  44. PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress
  45. // Mark address
  46. MarkGood(p2p.ID)
  47. MarkAttempt(*p2p.NetAddress)
  48. MarkBad(*p2p.NetAddress)
  49. IsGood(*p2p.NetAddress) bool
  50. // Send a selection of addresses to peers
  51. GetSelection() []*p2p.NetAddress
  52. // Send a selection of addresses with bias
  53. GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress
  54. Size() int
  55. // Persist to disk
  56. Save()
  57. }
  58. var _ AddrBook = (*addrBook)(nil)
  59. // addrBook - concurrency safe peer address manager.
  60. // Implements AddrBook.
  61. type addrBook struct {
  62. cmn.BaseService
  63. // immutable after creation
  64. filePath string
  65. routabilityStrict bool
  66. key string // random prefix for bucket placement
  67. // accessed concurrently
  68. mtx sync.Mutex
  69. rand *cmn.Rand
  70. ourAddrs map[string]struct{}
  71. privateIDs map[p2p.ID]struct{}
  72. addrLookup map[p2p.ID]*knownAddress // new & old
  73. bucketsOld []map[string]*knownAddress
  74. bucketsNew []map[string]*knownAddress
  75. nOld int
  76. nNew int
  77. wg sync.WaitGroup
  78. }
  79. // NewAddrBook creates a new address book.
  80. // Use Start to begin processing asynchronous address updates.
  81. func NewAddrBook(filePath string, routabilityStrict bool) *addrBook {
  82. am := &addrBook{
  83. rand: cmn.NewRand(),
  84. ourAddrs: make(map[string]struct{}),
  85. privateIDs: make(map[p2p.ID]struct{}),
  86. addrLookup: make(map[p2p.ID]*knownAddress),
  87. filePath: filePath,
  88. routabilityStrict: routabilityStrict,
  89. }
  90. am.init()
  91. am.BaseService = *cmn.NewBaseService(nil, "AddrBook", am)
  92. return am
  93. }
  94. // Initialize the buckets.
  95. // When modifying this, don't forget to update loadFromFile()
  96. func (a *addrBook) init() {
  97. a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
  98. // New addr buckets
  99. a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
  100. for i := range a.bucketsNew {
  101. a.bucketsNew[i] = make(map[string]*knownAddress)
  102. }
  103. // Old addr buckets
  104. a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
  105. for i := range a.bucketsOld {
  106. a.bucketsOld[i] = make(map[string]*knownAddress)
  107. }
  108. }
  109. // OnStart implements Service.
  110. func (a *addrBook) OnStart() error {
  111. if err := a.BaseService.OnStart(); err != nil {
  112. return err
  113. }
  114. a.loadFromFile(a.filePath)
  115. // wg.Add to ensure that any invocation of .Wait()
  116. // later on will wait for saveRoutine to terminate.
  117. a.wg.Add(1)
  118. go a.saveRoutine()
  119. return nil
  120. }
  121. // OnStop implements Service.
  122. func (a *addrBook) OnStop() {
  123. a.BaseService.OnStop()
  124. }
  125. func (a *addrBook) Wait() {
  126. a.wg.Wait()
  127. }
  128. func (a *addrBook) FilePath() string {
  129. return a.filePath
  130. }
  131. //-------------------------------------------------------
  132. // AddOurAddress one of our addresses.
  133. func (a *addrBook) AddOurAddress(addr *p2p.NetAddress) {
  134. a.mtx.Lock()
  135. defer a.mtx.Unlock()
  136. a.Logger.Info("Add our address to book", "addr", addr)
  137. a.ourAddrs[addr.String()] = struct{}{}
  138. }
  139. // OurAddress returns true if it is our address.
  140. func (a *addrBook) OurAddress(addr *p2p.NetAddress) bool {
  141. a.mtx.Lock()
  142. defer a.mtx.Unlock()
  143. _, ok := a.ourAddrs[addr.String()]
  144. return ok
  145. }
  146. func (a *addrBook) AddPrivateIDs(ids []string) {
  147. a.mtx.Lock()
  148. defer a.mtx.Unlock()
  149. for _, id := range ids {
  150. a.privateIDs[p2p.ID(id)] = struct{}{}
  151. }
  152. }
  153. // AddAddress implements AddrBook
  154. // Add address to a "new" bucket. If it's already in one, only add it probabilistically.
  155. // Returns error if the addr is non-routable. Does not add self.
  156. // NOTE: addr must not be nil
  157. func (a *addrBook) AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error {
  158. a.mtx.Lock()
  159. defer a.mtx.Unlock()
  160. return a.addAddress(addr, src)
  161. }
  162. // RemoveAddress implements AddrBook - removes the address from the book.
  163. func (a *addrBook) RemoveAddress(addr *p2p.NetAddress) {
  164. a.mtx.Lock()
  165. defer a.mtx.Unlock()
  166. ka := a.addrLookup[addr.ID]
  167. if ka == nil {
  168. return
  169. }
  170. a.Logger.Info("Remove address from book", "addr", addr)
  171. a.removeFromAllBuckets(ka)
  172. }
  173. // IsGood returns true if peer was ever marked as good and haven't
  174. // done anything wrong since then.
  175. func (a *addrBook) IsGood(addr *p2p.NetAddress) bool {
  176. a.mtx.Lock()
  177. defer a.mtx.Unlock()
  178. return a.addrLookup[addr.ID].isOld()
  179. }
  180. // HasAddress returns true if the address is in the book.
  181. func (a *addrBook) HasAddress(addr *p2p.NetAddress) bool {
  182. a.mtx.Lock()
  183. defer a.mtx.Unlock()
  184. ka := a.addrLookup[addr.ID]
  185. return ka != nil
  186. }
  187. // NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book.
  188. func (a *addrBook) NeedMoreAddrs() bool {
  189. return a.Size() < needAddressThreshold
  190. }
  191. // Empty implements AddrBook - returns true if there are no addresses in the address book.
  192. // Does not count the peer appearing in its own address book, or private peers.
  193. func (a *addrBook) Empty() bool {
  194. return a.Size() == 0
  195. }
  196. // PickAddress implements AddrBook. It picks an address to connect to.
  197. // The address is picked randomly from an old or new bucket according
  198. // to the biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to that range)
  199. // and determines how biased we are to pick an address from a new bucket.
  200. // PickAddress returns nil if the AddrBook is empty or if we try to pick
  201. // from an empty bucket.
  202. func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress {
  203. a.mtx.Lock()
  204. defer a.mtx.Unlock()
  205. bookSize := a.size()
  206. if bookSize <= 0 {
  207. if bookSize < 0 {
  208. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  209. }
  210. return nil
  211. }
  212. if biasTowardsNewAddrs > 100 {
  213. biasTowardsNewAddrs = 100
  214. }
  215. if biasTowardsNewAddrs < 0 {
  216. biasTowardsNewAddrs = 0
  217. }
  218. // Bias between new and old addresses.
  219. oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(biasTowardsNewAddrs))
  220. newCorrelation := math.Sqrt(float64(a.nNew)) * float64(biasTowardsNewAddrs)
  221. // pick a random peer from a random bucket
  222. var bucket map[string]*knownAddress
  223. pickFromOldBucket := (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation
  224. if (pickFromOldBucket && a.nOld == 0) ||
  225. (!pickFromOldBucket && a.nNew == 0) {
  226. return nil
  227. }
  228. // loop until we pick a random non-empty bucket
  229. for len(bucket) == 0 {
  230. if pickFromOldBucket {
  231. bucket = a.bucketsOld[a.rand.Intn(len(a.bucketsOld))]
  232. } else {
  233. bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
  234. }
  235. }
  236. // pick a random index and loop over the map to return that index
  237. randIndex := a.rand.Intn(len(bucket))
  238. for _, ka := range bucket {
  239. if randIndex == 0 {
  240. return ka.Addr
  241. }
  242. randIndex--
  243. }
  244. return nil
  245. }
  246. // MarkGood implements AddrBook - it marks the peer as good and
  247. // moves it into an "old" bucket.
  248. func (a *addrBook) MarkGood(id p2p.ID) {
  249. a.mtx.Lock()
  250. defer a.mtx.Unlock()
  251. ka := a.addrLookup[id]
  252. if ka == nil {
  253. return
  254. }
  255. ka.markGood()
  256. if ka.isNew() {
  257. a.moveToOld(ka)
  258. }
  259. }
  260. // MarkAttempt implements AddrBook - it marks that an attempt was made to connect to the address.
  261. func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) {
  262. a.mtx.Lock()
  263. defer a.mtx.Unlock()
  264. ka := a.addrLookup[addr.ID]
  265. if ka == nil {
  266. return
  267. }
  268. ka.markAttempt()
  269. }
  270. // MarkBad implements AddrBook. Currently it just ejects the address.
  271. // TODO: black list for some amount of time
  272. func (a *addrBook) MarkBad(addr *p2p.NetAddress) {
  273. a.RemoveAddress(addr)
  274. }
  275. // GetSelection implements AddrBook.
  276. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  277. // Must never return a nil address.
  278. func (a *addrBook) GetSelection() []*p2p.NetAddress {
  279. a.mtx.Lock()
  280. defer a.mtx.Unlock()
  281. bookSize := a.size()
  282. if bookSize <= 0 {
  283. if bookSize < 0 {
  284. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  285. }
  286. return nil
  287. }
  288. numAddresses := cmn.MaxInt(
  289. cmn.MinInt(minGetSelection, bookSize),
  290. bookSize*getSelectionPercent/100)
  291. numAddresses = cmn.MinInt(maxGetSelection, numAddresses)
  292. // XXX: instead of making a list of all addresses, shuffling, and slicing a random chunk,
  293. // could we just select a random numAddresses of indexes?
  294. allAddr := make([]*p2p.NetAddress, bookSize)
  295. i := 0
  296. for _, ka := range a.addrLookup {
  297. allAddr[i] = ka.Addr
  298. i++
  299. }
  300. // Fisher-Yates shuffle the array. We only need to do the first
  301. // `numAddresses' since we are throwing the rest.
  302. for i := 0; i < numAddresses; i++ {
  303. // pick a number between current index and the end
  304. j := cmn.RandIntn(len(allAddr)-i) + i
  305. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  306. }
  307. // slice off the limit we are willing to share.
  308. return allAddr[:numAddresses]
  309. }
  310. func percentageOfNum(p, n int) int {
  311. return int(math.Round((float64(p) / float64(100)) * float64(n)))
  312. }
  313. // GetSelectionWithBias implements AddrBook.
  314. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  315. // Must never return a nil address.
  316. //
  317. // Each address is picked randomly from an old or new bucket according to the
  318. // biasTowardsNewAddrs argument, which must be between [0, 100] (or else is truncated to
  319. // that range) and determines how biased we are to pick an address from a new
  320. // bucket.
  321. func (a *addrBook) GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddress {
  322. a.mtx.Lock()
  323. defer a.mtx.Unlock()
  324. bookSize := a.size()
  325. if bookSize <= 0 {
  326. if bookSize < 0 {
  327. panic(fmt.Sprintf("Addrbook size %d (new: %d + old: %d) is less than 0", a.nNew+a.nOld, a.nNew, a.nOld))
  328. }
  329. return nil
  330. }
  331. if biasTowardsNewAddrs > 100 {
  332. biasTowardsNewAddrs = 100
  333. }
  334. if biasTowardsNewAddrs < 0 {
  335. biasTowardsNewAddrs = 0
  336. }
  337. numAddresses := cmn.MaxInt(
  338. cmn.MinInt(minGetSelection, bookSize),
  339. bookSize*getSelectionPercent/100)
  340. numAddresses = cmn.MinInt(maxGetSelection, numAddresses)
  341. // number of new addresses that, if possible, should be in the beginning of the selection
  342. // if there are no enough old addrs, will choose new addr instead.
  343. numRequiredNewAdd := cmn.MaxInt(percentageOfNum(biasTowardsNewAddrs, numAddresses), numAddresses-a.nOld)
  344. selection := a.randomPickAddresses(bucketTypeNew, numRequiredNewAdd)
  345. selection = append(selection, a.randomPickAddresses(bucketTypeOld, numAddresses-len(selection))...)
  346. return selection
  347. }
  348. //------------------------------------------------
  349. // Size returns the number of addresses in the book.
  350. func (a *addrBook) Size() int {
  351. a.mtx.Lock()
  352. defer a.mtx.Unlock()
  353. return a.size()
  354. }
  355. func (a *addrBook) size() int {
  356. return a.nNew + a.nOld
  357. }
  358. //----------------------------------------------------------
  359. // Save persists the address book to disk.
  360. func (a *addrBook) Save() {
  361. a.saveToFile(a.filePath) // thread safe
  362. }
  363. func (a *addrBook) saveRoutine() {
  364. defer a.wg.Done()
  365. saveFileTicker := time.NewTicker(dumpAddressInterval)
  366. out:
  367. for {
  368. select {
  369. case <-saveFileTicker.C:
  370. a.saveToFile(a.filePath)
  371. case <-a.Quit():
  372. break out
  373. }
  374. }
  375. saveFileTicker.Stop()
  376. a.saveToFile(a.filePath)
  377. }
  378. //----------------------------------------------------------
  379. func (a *addrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  380. switch bucketType {
  381. case bucketTypeNew:
  382. return a.bucketsNew[bucketIdx]
  383. case bucketTypeOld:
  384. return a.bucketsOld[bucketIdx]
  385. default:
  386. panic("Invalid bucket type")
  387. }
  388. }
  389. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  390. // NOTE: currently it always returns true.
  391. func (a *addrBook) addToNewBucket(ka *knownAddress, bucketIdx int) {
  392. // Sanity check
  393. if ka.isOld() {
  394. a.Logger.Error("Failed Sanity Check! Cant add old address to new bucket", "ka", ka, "bucket", bucketIdx)
  395. return
  396. }
  397. addrStr := ka.Addr.String()
  398. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  399. // Already exists?
  400. if _, ok := bucket[addrStr]; ok {
  401. return
  402. }
  403. // Enforce max addresses.
  404. if len(bucket) > newBucketSize {
  405. a.Logger.Info("new bucket is full, expiring new")
  406. a.expireNew(bucketIdx)
  407. }
  408. // Add to bucket.
  409. bucket[addrStr] = ka
  410. // increment nNew if the peer doesnt already exist in a bucket
  411. if ka.addBucketRef(bucketIdx) == 1 {
  412. a.nNew++
  413. }
  414. // Add it to addrLookup
  415. a.addrLookup[ka.ID()] = ka
  416. }
  417. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  418. func (a *addrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  419. // Sanity check
  420. if ka.isNew() {
  421. a.Logger.Error(fmt.Sprintf("Cannot add new address to old bucket: %v", ka))
  422. return false
  423. }
  424. if len(ka.Buckets) != 0 {
  425. a.Logger.Error(fmt.Sprintf("Cannot add already old address to another old bucket: %v", ka))
  426. return false
  427. }
  428. addrStr := ka.Addr.String()
  429. bucket := a.getBucket(bucketTypeOld, bucketIdx)
  430. // Already exists?
  431. if _, ok := bucket[addrStr]; ok {
  432. return true
  433. }
  434. // Enforce max addresses.
  435. if len(bucket) > oldBucketSize {
  436. return false
  437. }
  438. // Add to bucket.
  439. bucket[addrStr] = ka
  440. if ka.addBucketRef(bucketIdx) == 1 {
  441. a.nOld++
  442. }
  443. // Ensure in addrLookup
  444. a.addrLookup[ka.ID()] = ka
  445. return true
  446. }
  447. func (a *addrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  448. if ka.BucketType != bucketType {
  449. a.Logger.Error(fmt.Sprintf("Bucket type mismatch: %v", ka))
  450. return
  451. }
  452. bucket := a.getBucket(bucketType, bucketIdx)
  453. delete(bucket, ka.Addr.String())
  454. if ka.removeBucketRef(bucketIdx) == 0 {
  455. if bucketType == bucketTypeNew {
  456. a.nNew--
  457. } else {
  458. a.nOld--
  459. }
  460. delete(a.addrLookup, ka.ID())
  461. }
  462. }
  463. func (a *addrBook) removeFromAllBuckets(ka *knownAddress) {
  464. for _, bucketIdx := range ka.Buckets {
  465. bucket := a.getBucket(ka.BucketType, bucketIdx)
  466. delete(bucket, ka.Addr.String())
  467. }
  468. ka.Buckets = nil
  469. if ka.BucketType == bucketTypeNew {
  470. a.nNew--
  471. } else {
  472. a.nOld--
  473. }
  474. delete(a.addrLookup, ka.ID())
  475. }
  476. //----------------------------------------------------------
  477. func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  478. bucket := a.getBucket(bucketType, bucketIdx)
  479. var oldest *knownAddress
  480. for _, ka := range bucket {
  481. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  482. oldest = ka
  483. }
  484. }
  485. return oldest
  486. }
  487. // adds the address to a "new" bucket. if its already in one,
  488. // it only adds it probabilistically
  489. func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
  490. if addr == nil || src == nil {
  491. return ErrAddrBookNilAddr{addr, src}
  492. }
  493. if err := addr.Valid(); err != nil {
  494. return ErrAddrBookInvalidAddr{Addr: addr, AddrErr: err}
  495. }
  496. if _, ok := a.privateIDs[addr.ID]; ok {
  497. return ErrAddrBookPrivate{addr}
  498. }
  499. if _, ok := a.privateIDs[src.ID]; ok {
  500. return ErrAddrBookPrivateSrc{src}
  501. }
  502. // TODO: we should track ourAddrs by ID and by IP:PORT and refuse both.
  503. if _, ok := a.ourAddrs[addr.String()]; ok {
  504. return ErrAddrBookSelf{addr}
  505. }
  506. if a.routabilityStrict && !addr.Routable() {
  507. return ErrAddrBookNonRoutable{addr}
  508. }
  509. ka := a.addrLookup[addr.ID]
  510. if ka != nil {
  511. // If its already old and the addr is the same, ignore it.
  512. if ka.isOld() && ka.Addr.Equals(addr) {
  513. return nil
  514. }
  515. // Already in max new buckets.
  516. if len(ka.Buckets) == maxNewBucketsPerAddress {
  517. return nil
  518. }
  519. // The more entries we have, the less likely we are to add more.
  520. factor := int32(2 * len(ka.Buckets))
  521. if a.rand.Int31n(factor) != 0 {
  522. return nil
  523. }
  524. } else {
  525. ka = newKnownAddress(addr, src)
  526. }
  527. bucket := a.calcNewBucket(addr, src)
  528. a.addToNewBucket(ka, bucket)
  529. return nil
  530. }
  531. func (a *addrBook) randomPickAddresses(bucketType byte, num int) []*p2p.NetAddress {
  532. var buckets []map[string]*knownAddress
  533. switch bucketType {
  534. case bucketTypeNew:
  535. buckets = a.bucketsNew
  536. case bucketTypeOld:
  537. buckets = a.bucketsOld
  538. default:
  539. panic("unexpected bucketType")
  540. }
  541. total := 0
  542. for _, bucket := range buckets {
  543. total += len(bucket)
  544. }
  545. addresses := make([]*knownAddress, 0, total)
  546. for _, bucket := range buckets {
  547. for _, ka := range bucket {
  548. addresses = append(addresses, ka)
  549. }
  550. }
  551. selection := make([]*p2p.NetAddress, 0, num)
  552. chosenSet := make(map[string]bool, num)
  553. rand.Shuffle(total, func(i, j int) {
  554. addresses[i], addresses[j] = addresses[j], addresses[i]
  555. })
  556. for _, addr := range addresses {
  557. if chosenSet[addr.Addr.String()] {
  558. continue
  559. }
  560. chosenSet[addr.Addr.String()] = true
  561. selection = append(selection, addr.Addr)
  562. if len(selection) >= num {
  563. return selection
  564. }
  565. }
  566. return selection
  567. }
  568. // Make space in the new buckets by expiring the really bad entries.
  569. // If no bad entries are available we remove the oldest.
  570. func (a *addrBook) expireNew(bucketIdx int) {
  571. for addrStr, ka := range a.bucketsNew[bucketIdx] {
  572. // If an entry is bad, throw it away
  573. if ka.isBad() {
  574. a.Logger.Info(fmt.Sprintf("expiring bad address %v", addrStr))
  575. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  576. return
  577. }
  578. }
  579. // If we haven't thrown out a bad entry, throw out the oldest entry
  580. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  581. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  582. }
  583. // Promotes an address from new to old. If the destination bucket is full,
  584. // demote the oldest one to a "new" bucket.
  585. // TODO: Demote more probabilistically?
  586. func (a *addrBook) moveToOld(ka *knownAddress) {
  587. // Sanity check
  588. if ka.isOld() {
  589. a.Logger.Error(fmt.Sprintf("Cannot promote address that is already old %v", ka))
  590. return
  591. }
  592. if len(ka.Buckets) == 0 {
  593. a.Logger.Error(fmt.Sprintf("Cannot promote address that isn't in any new buckets %v", ka))
  594. return
  595. }
  596. // Remove from all (new) buckets.
  597. a.removeFromAllBuckets(ka)
  598. // It's officially old now.
  599. ka.BucketType = bucketTypeOld
  600. // Try to add it to its oldBucket destination.
  601. oldBucketIdx := a.calcOldBucket(ka.Addr)
  602. added := a.addToOldBucket(ka, oldBucketIdx)
  603. if !added {
  604. // No room; move the oldest to a new bucket
  605. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  606. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  607. newBucketIdx := a.calcNewBucket(oldest.Addr, oldest.Src)
  608. a.addToNewBucket(oldest, newBucketIdx)
  609. // Finally, add our ka to old bucket again.
  610. added = a.addToOldBucket(ka, oldBucketIdx)
  611. if !added {
  612. a.Logger.Error(fmt.Sprintf("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  613. }
  614. }
  615. }
  616. //---------------------------------------------------------------------
  617. // calculate bucket placements
  618. // doublesha256( key + sourcegroup +
  619. // int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets
  620. func (a *addrBook) calcNewBucket(addr, src *p2p.NetAddress) int {
  621. data1 := []byte{}
  622. data1 = append(data1, []byte(a.key)...)
  623. data1 = append(data1, []byte(a.groupKey(addr))...)
  624. data1 = append(data1, []byte(a.groupKey(src))...)
  625. hash1 := doubleSha256(data1)
  626. hash64 := binary.BigEndian.Uint64(hash1)
  627. hash64 %= newBucketsPerGroup
  628. var hashbuf [8]byte
  629. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  630. data2 := []byte{}
  631. data2 = append(data2, []byte(a.key)...)
  632. data2 = append(data2, a.groupKey(src)...)
  633. data2 = append(data2, hashbuf[:]...)
  634. hash2 := doubleSha256(data2)
  635. return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  636. }
  637. // doublesha256( key + group +
  638. // int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets
  639. func (a *addrBook) calcOldBucket(addr *p2p.NetAddress) int {
  640. data1 := []byte{}
  641. data1 = append(data1, []byte(a.key)...)
  642. data1 = append(data1, []byte(addr.String())...)
  643. hash1 := doubleSha256(data1)
  644. hash64 := binary.BigEndian.Uint64(hash1)
  645. hash64 %= oldBucketsPerGroup
  646. var hashbuf [8]byte
  647. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  648. data2 := []byte{}
  649. data2 = append(data2, []byte(a.key)...)
  650. data2 = append(data2, a.groupKey(addr)...)
  651. data2 = append(data2, hashbuf[:]...)
  652. hash2 := doubleSha256(data2)
  653. return int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  654. }
  655. // Return a string representing the network group of this address.
  656. // This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
  657. // "local" for a local address and the string "unroutable" for an unroutable
  658. // address.
  659. func (a *addrBook) groupKey(na *p2p.NetAddress) string {
  660. if a.routabilityStrict && na.Local() {
  661. return "local"
  662. }
  663. if a.routabilityStrict && !na.Routable() {
  664. return "unroutable"
  665. }
  666. if ipv4 := na.IP.To4(); ipv4 != nil {
  667. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(16, 32)}).String()
  668. }
  669. if na.RFC6145() || na.RFC6052() {
  670. // last four bytes are the ip address
  671. ip := net.IP(na.IP[12:16])
  672. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  673. }
  674. if na.RFC3964() {
  675. ip := net.IP(na.IP[2:7])
  676. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  677. }
  678. if na.RFC4380() {
  679. // teredo tunnels have the last 4 bytes as the v4 address XOR
  680. // 0xff.
  681. ip := net.IP(make([]byte, 4))
  682. for i, byte := range na.IP[12:16] {
  683. ip[i] = byte ^ 0xff
  684. }
  685. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  686. }
  687. // OK, so now we know ourselves to be a IPv6 address.
  688. // bitcoind uses /32 for everything, except for Hurricane Electric's
  689. // (he.net) IP range, which it uses /36 for.
  690. bits := 32
  691. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"),
  692. Mask: net.CIDRMask(32, 128)}
  693. if heNet.Contains(na.IP) {
  694. bits = 36
  695. }
  696. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(bits, 128)}).String()
  697. }
  698. // doubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes.
  699. func doubleSha256(b []byte) []byte {
  700. hasher := sha256.New()
  701. hasher.Write(b) // nolint: errcheck, gas
  702. sum := hasher.Sum(nil)
  703. hasher.Reset()
  704. hasher.Write(sum) // nolint: errcheck, gas
  705. return hasher.Sum(nil)
  706. }