Commit Graph

13648 Commits

Author SHA1 Message Date
Oleg Girko
42c784dc7a Backport Bitcoin PR#9289: net: drop boost::thread_group (#1568)
* net: a few small cleanups before replacing boost threads

- Drop the interruption point directly after the pnode allocation. This would
    be leaky if hit.
- Rearrange thread creation so that the socket handler comes first

* net: add CThreadInterrupt and InterruptibleSleep

* net: make net interruptible

Also now that net threads are interruptible, switch them to use std
threads/binds/mutexes/condvars.

* net: make net processing interruptible

* net: remove thread_interrupted catch

This is now a std::thread, so there's no hope of catching a boost interruption
point.

* net: make proxy receives interruptible

* net: misc header cleanups
2017-08-09 19:06:31 +03:00
Oleg Girko
df6d458b85 Backport Bitcoin PR#9260: Mrs Peacock in The Library with The Candlestick (killed main.{h,cpp}) (#1566)
* Remove orphan state wipe from UnloadBlockIndex.

As orphan state is now "network state", like in
d6ea737be1,

UnloadBlockIndex is only used during init if we end up reindexing
to clear our block state so that we can start over. However, at
that time no connections have been brought up as CConnman hasn't
been started yet, so all of the network processing state logic is
empty when its called.

* Move network-msg-processing code out of main to its own file

* Rename the remaining main.{h,cpp} to validation.{h,cpp}
2017-08-09 03:19:06 +03:00
Oleg Girko
bcf5455bfd Backport Bitcoin PR#8822: net: Consistent checksum handling (#1565)
* net: Consistent checksum handling

In principle, the checksums of P2P packets are simply 4-byte blobs which
are the first four bytes of SHA256(SHA256(payload)).

Currently they are handled as little-endian 32-bit integers half of the
time, as blobs the other half, sometimes copying the one to the other,
resulting in somewhat confused code.

This PR changes the handling to be consistent both at packet creation
and receiving, making it (I think) easier to understand.

* net: Hardcode protocol sizes and use fixed-size types

The P2P network uses a fixed protocol, these sizes shouldn't change
based on what happens to be the architecture.
2017-08-08 20:57:36 +03:00
UdjinM6
87707c0127 fix CDSNotificationInterface::UpdatedBlockTip signature (#1562)
to match the one in CValidationInterface
2017-08-04 20:43:48 +03:00
Oleg Girko
415085c732 Backport Bitcoin PR#9183: Final Preparation for main.cpp Split (#1561)
* Expose AcceptBlockHeader through main.h

* Split ::HEADERS processing into two separate cs_main locks

This will allow NotifyHeaderTip to be called from an
AcceptBlockHeader wrapper function without holding cs_main.

* Use exposed ProcessNewBlockHeaders from ProcessMessages
2017-08-03 01:19:34 +03:00
Oleg Girko
b4b3431458 Backport Bitcoin PR#9075: Decouple peer-processing-logic from block-connection-logic (#3) (#1560)
* Remove pfrom parameter from ProcessNewBlock

This further decouples ProcessNewBlock from networking/peer logic.

* Replace CValidationState param in ProcessNewBlock with BlockChecked
2017-08-02 21:35:04 +03:00
Oleg Girko
e7e106e228 Backport Bitcoin PR#8969: Decouple peer-processing-logic from block-connection-logic (#2) (#1558)
* Move MarkBlockAsReceived out of ProcessNewMessage

* Remove network state wipe from UnloadBlockIndex.

UnloadBlockIndex is only used during init if we end up reindexing
to clear our block state so that we can start over. However, at
that time no connections have been brought up as CConnman hasn't
been started yet, so all of the network processing state logic is
empty when its called.

Additionally, the initialization of the recentRejects set is moved
to InitPeerLogic.

* Move all calls to CheckBlockIndex out of net-processing logic

This will result in many more calls to CheckBlockIndex when
connecting a list of headers (eg in ::HEADERS messages processing)
but its only enabled in debug mode, and that should mostly just be
during IBD, so it should be OK.

* Move FlushStateToDisk call out of ProcessMessages::TX into ATMP

* Move nTimeBestReceived updating into net processing code
2017-08-01 18:11:32 +03:00
Oleg Girko
a3c8cb20df Backport Bitcoin PR#8865: Decouple peer-processing-logic from block-connection-logic (#1556)
* Make validationinterface.UpdatedBlockTip more verbose

In anticipation of making all the callbacks out of block processing
flow through it. Note that vHashes will always have something in it
since pindexFork != pindexNewTip.

* Remove duplicate nBlocksEstimate cmp (we already checked IsIBD())

* Remove CConnman parameter from ProcessNewBlock/ActivateBestChain

* Remove SyncWithWallets wrapper function

* Move net-processing logic definitions together in main.h

* Use CValidationInterface from chain logic to notify peer logic

This adds a new CValidationInterface subclass, defined in main.h,
to receive notifications of UpdatedBlockTip and use that to push
blocks to peers, instead of doing it directly from
ActivateBestChain.

* Always call UpdatedBlockTip, even if blocks were only disconnected

* Use BlockChecked signal to send reject messages from mapBlockSource
2017-07-28 17:10:10 +03:00
Oleg Girko
cc4db34f4d net: only delete CConnman if it's been created (#1555)
In the case of (for example) an already-running bitcoind, the shutdown sequence
begins before CConnman has been created, leading to a null-pointer dereference
when g_connman->Stop() is called.

Instead, Just let the CConnman dtor take care of stopping.
2017-07-28 01:39:53 +03:00
Oleg Girko
b621cfb5fb Backport Bitcoin PR#8708: net: have CConnman handle message sending (#1553)
* serialization: teach serializers variadics

Also add a variadic CDataStream ctor for ease-of-use.

* connman is in charge of pushing messages

The changes here are dense and subtle, but hopefully all is more explicit
than before.

- CConnman is now in charge of sending data rather than the nodes themselves.
  This is necessary because many decisions need to be made with all nodes in
  mind, and a model that requires the nodes calling up to their manager quickly
  turns to spaghetti.

- The per-node-serializer (ssSend) has been replaced with a (quasi-)const
  send-version. Since the send version for serialization can only change once
  per connection, we now explicitly tag messages with INIT_PROTO_VERSION if
  they are sent before the handshake. With this done, there's no need to lock
  for access to nSendVersion.

  Also, a new stream is used for each message, so there's no need to lock
  during the serialization process.

- This takes care of accounting for optimistic sends, so the
  nOptimisticBytesWritten hack can be removed.

- -dropmessagestest and -fuzzmessagestest have not been preserved, as I suspect
  they haven't been used in years.

* net: switch all callers to connman for pushing messages

Drop all of the old stuff.

* drop the optimistic write counter hack

This is now handled properly in realtime.

* net: remove now-unused ssSend and Fuzz

* net: construct CNodeStates in place

* net: handle version push in InitializeNode
2017-07-27 17:28:05 +03:00
UdjinM6
8b7dffbb64 Update startup shortcuts (#1551) 2017-07-25 13:58:27 +03:00
UdjinM6
4df8a20f96 Fix windows installer script, should handle dash: uri correctly now (#1550) 2017-07-25 13:58:08 +03:00
UdjinM6
9694658cd3 Make sure mixing messages are relayed/accepted properly (#1547)
* make sure addr of mixing mn matches peer's addr exactly (including port)

* Store addr of every mixing client and relay mixing mesasges to them only
2017-07-25 13:57:26 +03:00
Nathan Marley
a0851494db add masternodelist pubkey to rpc (#1549)
* add masternodelist pubkey to rpc

* add method in alphabetical order
2017-07-22 23:33:42 +03:00
UdjinM6
7707c07897 backport 9008: Remove assert(nMaxInbound > 0) (#1548) 2017-07-22 22:37:10 +03:00
Oleg Girko
82851b439c Backport Bitcoin PR#8049: Expose information on whether transaction relay is enabled in getnetwork (#1545)
* net: Add fRelayTxes flag

Add a fRelayTxes to keep track of the relay transaction flag
we send to other peers.

* rpc: Add `relaytxes` flag to `getnetworkinfo`

Re-work of PR #7841 by dragongem45.
Closes #7771.
2017-07-21 21:31:47 +03:00
Ilya Savinov
4942884c71 fix potential deadlock in CMasternodeMan::CheckMnbAndUpdateMasternodeList (#1543)
governance.cs -> mnodeman.cs:
CGovernanceManager::ProcessMessage - LOCK2(cs_main, governance.cs) -> AddGovernanceObject -> UpdateSentinelVariables -> CountEnabled - LOCK(mnodeman.cs)

mnodeman.cs -> governance.cs:
CheckMnbAndUpdateMasternodeList - LOCK2(cs_main, mnodeman.cs) -> activeMasternode.ManageState -> ManageStateLocal -> NotifyMasternodeUpdates -> СheckMasternodeOrphanObjects - LOCK2(cs_main, governance.cs)
... -> NotifyMasternodeUpdates -> CheckMasternodeOrphanVotes - LOCK2(cs_main, governance.cs)
... -> NotifyMasternodeUpdates -> UpdateCachesAndClean - LOCK2(cs_main, governance.cs)
2017-07-21 18:21:09 +03:00
Ilya Savinov
1c4e2946af fix potential deadlock in CGovernanceManager::ProcessVote (#1541)
govman.cs -> cs_vSend:
CGovernanceManager::ProcessVote - LOCK(govman.cs) -> RequestGovernanceObject -> PushMessage -> BeginMessage - LOCK(cs_vSend)

cs_vSend -> govman.cs:
ThreadMessageHandler -> TRY_LOCK(pnode->cs_vSend, lockSend) -> SendMessages -> AlreadyHave -> ConfirmInventoryRequest -> LOCK(govman.cs)
2017-07-21 12:47:38 +03:00
chaeplin
f47f0daf9a add 6 to strAllowedChars (#1542) 2017-07-21 12:36:34 +03:00
Oleg Girko
a9d771e497 Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp

This will eventually solve a circular dependency

* net: Create CConnman to encapsulate p2p connections

* net: Move socket binding into CConnman

* net: move OpenNetworkConnection into CConnman

* net: move ban and addrman functions into CConnman

* net: Add oneshot functions to CConnman

* net: move added node functions to CConnman

* net: Add most functions needed for vNodes to CConnman

* net: handle nodesignals in CConnman

* net: Pass CConnection to wallet rather than using the global

* net: Add rpc error for missing/disabled p2p functionality

* net: Pass CConnman around as needed

* gui: add NodeID to the peer table

* net: create generic functor accessors and move vNodes to CConnman

* net: move whitelist functions into CConnman

* net: move nLastNodeId to CConnman

* net: move nLocalHostNonce to CConnman

This behavior seems to have been quite racy and broken.

Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.

* net: move messageHandlerCondition to CConnman

* net: move send/recv statistics to CConnman

* net: move SendBufferSize/ReceiveFloodSize to CConnman

* net: move nLocalServices/nRelevantServices to CConnman

These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).

* net: move semOutbound and semMasternodeOutbound to CConnman

* net: SocketSendData returns written size

* net: move max/max-outbound to CConnman

* net: Pass best block known height into CConnman

CConnman then passes the current best height into CNode at creation time.

This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.

This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.

This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.

* net: pass CClientUIInterface into CConnman

* net: Drop StartNode/StopNode and use CConnman directly

* net: Introduce CConnection::Options to avoid passing so many params

* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options

* net: move vNodesDisconnected into CConnman

* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting

* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead

* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 12:35:19 +03:00
Ilya Savinov
15958c594c fix potential deadlock (PR#1536 fix) (#1538)
mempool.cs -> mnodeman.cs:
`CreateNewBlock - LOCK2(cs_main, mempool.cs) -> FillBlockPayments -> FillBlockPayee -> GetNextMasternodeInQueueForPayment - LOCK(mnodeman.cs)`

mnodeman.cs -> mempool.cs
`CheckMnbAndUpdateMasternodeList - LOCK2(cs_main, mnodeman.cs) -> CheckOutpoint -> GetTransaction -> lookup - LOCK(mempool.cs)`
2017-07-20 13:48:30 +03:00
UdjinM6
0f05e25c71 fix a bug in CommitFinalTransaction (#1540) 2017-07-19 21:49:48 +02:00
Ilya Savinov
6cb3fddcc1 fixed potential deadlock in CSuperblockManager::IsSuperblockTriggered (#1536)
governance.cs -> mempool.cs:
`CGovernanceManager::ProcessMessage - LOCK2(cs_main, governance.cs) ->  GetTransaction ->  lookup - LOCK(mempool.cs)`

mempool.cs -> governance.cs:
`CreateNewBlock - LOCK2(cs_main, mempool.cs)  -> FillBlockPayments ->  IsSuperblockTriggered - LOCK(governance.cs)`
2017-07-18 19:25:02 +03:00
Oleg Girko
290fb3b57e Backport Bitcoin PR#7942: locking for Misbehave() and other cs_main locking fixes (#1535)
* lock cs_main for chainActive

ActivateBestChain uses chainActive after releasing the lock; reorder operations
to move all access to synchronized object into existing LOCK(cs_main) block.

* lock cs_main for State/Misbehaving

ProcessMessage calls State(...) and Misbehaving(...) without holding the
required lock; add LOCK(cs_main) blocks.
2017-07-17 20:49:17 +03:00
gladcow
8075370d1e change invalid version string constant (#1532) 2017-07-17 17:38:32 +03:00
Ilya Savinov
e0d6c5b5a2 fixed potential deadlock in CMasternodePing::SimpleCheck (#1534) 2017-07-17 16:59:04 +03:00
UdjinM6
9e9df28203 do not use masternode connections in feeler logic (#1533) 2017-07-17 16:54:35 +03:00
Oleg Girko
d934ffb2ff Added feeler connections increasing good addrs in the tried table. (#1530)
Tests if addresses are online or offline by briefly connecting to them. These short lived connections are referred to as feeler connections. Feeler connections are designed to increase the number of fresh online addresses in tried by selecting and connecting to addresses in new. One feeler connection is attempted on average once every two minutes.

This change was suggested as Countermeasure 4 in
Eclipse Attacks on Bitcoin’s Peer-to-Peer Network, Ethan Heilman,
Alison Kendler, Aviv Zohar, Sharon Goldberg. ePrint Archive Report
2015/263. March 2015.
2017-07-17 13:39:12 +03:00
UdjinM6
27b6f36338 fix deadlock (#1531) 2017-07-17 10:06:15 +02:00
Oleg Girko
aa32f1dc97 Remove bad chain alert partition check (#1529)
As per meeting 2016-03-31
https://bitcoincore.org/en/meetings/2016/03/31/#bad-chain-alerts

The partition checker was producing huge number of false-positives
and was disabled in 0.12.1 on the understanding it would either be
fixed in 0.13 or removed entirely from master if not.

*** Dash specific note: ***
This check was disabled in Dash already.
2017-07-16 23:17:04 +03:00
Oleg Girko
9ce2b966cd Backport Bitcoin PR#8113: Rework addnode behaviour (#1525)
* Rework addnode behaviour

* Use CNode::addeName to track whether a connection to a name is already open
  * A new connection to a previously-connected by-name addednode is only opened when
    the previous one closes (even if the name starts resolving to something else)
  * At most one connection is opened per addednode (even if the name resolves to multiple)
* Unify the code between ThreadOpenAddedNodeConnections and getaddednodeinfo
  * Information about open connections is always returned, and the dns argument becomes a dummy
  * An IP address and inbound/outbound is only reported for the (at most 1) open connection

* Prevent duplicate connections where one is by name and another by ip

* Randomize name lookup result in ConnectSocketByName
2017-07-14 19:58:57 +03:00
UdjinM6
1b1d52ac3e workaround for travis (#1526) 2017-07-14 17:19:07 +03:00
Ilya Savinov
916af52c0a Fix vulnerability with mapMasternodeOrphanObjects (#1512)
* fix vulnerability with mapMasternodeOrphanObjects

The vulnerability is that a malicious node can send a lot of NetMsgType::MNGOVERNANCEOBJECT messages which refer to many arbitrary MN's. In this case, mapMasternodeOrphanObjects will grow unrestrictedly.

* MN collateral moved to governance-object.cpp; ban score applied to misbehaving nodes

* recursive locks removed

* check for the mn collateral code segregated to a separate function

* CheckCollateral implementation moved to cpp
2017-07-13 12:38:00 +03:00
Oleg Girko
eea78d45ed Revert "fixed regtest+ds issues" (#1524)
This reverts commit 1f828f45ec.

The commit being reverted changed FindNode(const CService& addr)
to make no difference between nodes with the same IP address,
but different ports, but only for regtest network.
As functional tests run several nodes on different ports or the same
IP address (127.0.0.1), this eventually started breaking functional tests.
The only use for regtest network I know is for functional tests,
so it's time to revert that commit.
2017-07-13 09:07:50 +03:00
chaeplin
f9dd40888e include atomic (#1523) 2017-07-12 23:08:18 +03:00
Ilya Savinov
f7aa81586f Fix issues with mapSeenGovernanceObjects (#1511)
* fix issues with mapSeenGovernanceObjects

Removed seen-governance-objects optimization except for deleted objects. Otherwise some nodes can permanently lost proposals if they received them too early.
Beside of that there is a vulnerability with seen-governance-objects mechanism if malicious node send us a lot of invalid governance objects.

* mapSeenGovernanceObjects renamed to mapErasedGovernanceObjects

* current fixes

* use int64_t for expiration timestamp
2017-07-12 23:08:06 +03:00
Oleg Girko
7b5556a294 Backport Bitcoin PR#8084: Add recently accepted blocks and txn to AttemptToEvictConnection (#1522)
* Add recently accepted blocks and txn to AttemptToEvictConnection.

This protects any not-already-protected peers who were the most
 recent four to relay transactions and most recent four to send
 blocks to us.

* Allow disconnecting a netgroup with only one member in eviction.

With the latest additions there are enough protective measures that
 we can take the training wheels off.
2017-07-12 14:13:38 +03:00
Oleg Girko
5a1961e5e3 Backport Bitcoin PR#7906: net: prerequisites for p2p encapsulation changes (#1521)
* net: don't import std namespace

This file is about to be broken up into chunks and moved around. Drop the
namespace now rather than requiring other files to use it.

* net: remove unused set

* net: use the exposed GetNodeSignals() rather than g_signals directly

* net: make Ban/Unban/ClearBan functionality consistent

- Ban/Unban/ClearBan call uiInterface.BannedListChanged() as necessary
- Ban/Unban/ClearBan sync to disk if the operation is user-invoked
- Mark node for disconnection automatically when banning
- Lock cs_vNodes while setting disconnected
- Don't spin in a tight loop while setting disconnected

* net: No need to export DumpBanlist
2017-07-12 04:20:12 +03:00
Oleg Girko
9a8a290b87 fix race that could fail to persist a ban (#1518)
DumpBanList currently does this:
  - with lock: take a copy of the banmap
  - perform I/O (write out the banmap)
  - with lock: mark the banmap non-dirty
If a new ban is added during the I/O operation, it may never be persisted to
disk.

Reorder operations so that the data to be persisted cannot be older than the
time at which the banmap was marked non-dirty.
2017-07-11 09:15:14 +03:00
Oleg Girko
b47984f303 Remove non-determinism which is breaking net_tests #8069 (#1517) 2017-07-11 02:03:14 +03:00
UdjinM6
397ea95dbb Implement BIP69 outside of CTxIn/CTxOut (#1514)
Apply BIP69 on transaction creation only, fix inputs order to match the BIP
2017-07-10 17:42:59 +03:00
UdjinM6
7abac068b6 fix MakeCollateralAmounts (#1500)
apply same logic as in CreateDenominated to avoid joining inputs from different addresses
2017-07-10 17:42:32 +03:00
UdjinM6
739ef9a681 Expire confirmed DSTXes after ~1h since confirmation (#1499)
* Expire confirmed DSTXes after ~1h since confirmation

* add missing cs_main lock
2017-07-10 17:42:09 +03:00
gladcow
70eb83a5ce New rpc call "masternodelist info" (#1513)
* add masternodelist info call

* safe version conversion function

* change default sentinel version value

* fix issues
2017-07-10 17:41:42 +03:00
Oleg Girko
9268a336dd Backport Bitcoin PR#7917: Optimize reindex (#1515)
* Make ProcessNewBlock dbp const and update comment

* Switch reindexing to AcceptBlock in-loop and ActivateBestChain afterwards

* Optimize ActivateBestChain for long chains

* Add -reindex-chainstate that does not rebuild block index

* Report reindexing progress in GUI
2017-07-10 17:41:14 +03:00
Oleg Girko
b39c518d5a Backport Bitcoin PR#7749: Enforce expected outbound services (#1508)
* Only store and connect to NODE_NETWORK nodes

* Keep addrman's nService bits consistent with outbound observations

* Verify that outbound connections have expected services

* Don't require services in -addnode

* Introduce enum ServiceFlags for service flags

* Introduce REQUIRED_SERVICES constant
2017-07-05 06:45:23 +03:00
Oleg Girko
6af9955faa Backport Bitcoin PR#7696: Fix de-serialization bug where AddrMan is corrupted after exception (#1507)
* CAddrDB modified so that when de-serialization code throws an exception Addrman is reset to a clean state
* CAddrDB modified to make unit tests possible
* Regression test created to ensure bug is fixed
* StartNode modifed to clear adrman if CAddrDB::Read returns an error code.
2017-07-05 03:40:22 +03:00
Ilya Savinov
109c5fd1d8 Fixed issues with propagation of governance objects (#1489)
* process governance objects in CheckMasternodeOrphanObjects as usual

* code refactoring: SetRateChecksHelper class added

* fixed race condition issues with propagation of governance objects

* change GetCollateralConfirmations signature

* code refactoring

* reduced minimum number of collateral confirmations required for relaying proposals

* bug fixes and improvements
2017-07-05 03:31:50 +03:00
Oleg Girko
d787fe4ab6 Backport Bitcoin PR#7458 : [Net] peers.dat, banlist.dat recreated when missing (#1506) 2017-07-05 02:26:13 +03:00
Oleg Girko
ac6c3c9008 Backport Bitcoin PR#7350: Banlist updates (#1505)
* banlist: update set dirty to be more fine grained

- move the SetBannedSetDirty(false) call from DumpData() into DumpBanlist()
- ensure we only set false, if the write succeeded

* banlist: better handling of banlist in StartNode()

- only start working on/with banlist data, if reading in the banlist from
  disk didn't fail
- as CNode::setBannedIsDirty is false (default) when reading fails, we
  don't need to explicitly set it to false to prevent writing
  banlist.dat in that case either

* banlist: add more banlist infos to log / add GUI signal

- to match the peers.dat handling also supply a debug.log entry for how
  many entries were loaded from banlist.dat and how long it took
- add a GUI init message for loading the banlist (same as with peers.dat)

- move the same message for peers.dat upwards in the code, to be able to
  reuse the timing variable nStart and also just log, if our read from
  peers.dat didn't fail

* banlist (bugfix): allow CNode::SweepBanned() to run on interval

- allows CNode::SweepBanned() to run, even if !CNode::BannedSetIsDirty(),
  because if nBanUntil is over we want the ban to be disabled for these
  nodes
2017-07-05 00:39:05 +03:00