Commit Graph

55 Commits

Author SHA1 Message Date
tomFlowee b6fc84696a Wrap PrivacySegment in a shared pointer.
This is an abstract class that the application using this library needs
to subclass. Ownership and lifetime don't change, it still lies with the
app using the library and they still need to add and remove it from the
connectionManager, but this makes it much more stable for multi-
threading environments and avoids issues on misuse.
2024-11-29 15:12:13 +01:00
tomFlowee 6ade318b2e Introduce interleaving during download
The download from a peer is now limited to just 4000 blocks, at which
point the download proceeds from another peer.
Naturally it can go back to the first afterwards too.

The idea is that we download a month worth of blocks at a time and then
fetch the same blocks from a backup peer in order to be certain we
actually see the real state.
The problem we've seen is that after the first downloads we end up
starting the second and if the initial sync or a similar large download
happens, many people won't leave it running until the backup actually
completes since the UI looks like its finished.

This is also a good starting point for rating peers at download speed by
having predictable chunks and that means we could time and aim to use
the faster peers.
This is becoming relevant as we can see a huge difference where peers
give you 10x performance compared to some of the slower ones.
2024-02-23 22:08:28 +01:00
tomFlowee 6b88f6ad7b Ensure addressDB is updated upon connection.
Even if the user disqualifies the connection, we should still register
that there was a successful handshake.
2024-02-14 16:20:02 +01:00
tomFlowee 9fd2e37bba Ensure we ask for the mempool details also when there are no new blocks 2024-01-28 21:31:28 +01:00
tomFlowee 9d980fdcad Minor tweaks to IP selection.
When we didn't personally verify the services, don't judge the IP based
on them and connect anyway.

In addition to not trusting the services we receive from the net, be
more careful with the usage of the 'lastConnected' field.
Notice that we don't actually use the result of that variable in the end
if the everConnected is likewise incorrect.
2024-01-28 17:01:01 +01:00
tomFlowee ffca9b3418 Refactor the BroadcastTxData slightly and pass in the peer. 2024-01-18 21:46:08 +01:00
tomFlowee 542eaa0605 fix logic and be faster to sync.
The intended logic now works which avoids us now finding a download
source faster.
2024-01-14 12:26:01 +01:00
tomFlowee e16fb40146 Avoid destructors more for de-registration
This allows us to use a shared pointer while at the same time not having
the problem that a peer de-registration hits an already deleted
PrivacySegment.
2024-01-13 19:26:55 +01:00
tomFlowee fb927b9436 Continue shared_ptr convertion of Peer
Last year we started wrapping the Peer object in a shared pointer,
which is used now in the managers.
This continues the idea by making the PrivacySegment use smart
pointers too and the P2PNetInterface is changed to do the same
for downstream applications.
2024-01-04 17:06:32 +01:00
tomFlowee bc47a700a4 Refactor; wrap BufferPool in shared_ptr
As we moved most of the creation of a BufferPool to be via the
Streaming::pool() method, which uses a thread-local, it makes sense
to start cleaning up the design and make it more modern C++.
The above mentioned method would return a reference and you'd see
loads of places use `auto &pool =` which is less than ideal.

As the number of places where we actually instantiate a BufferPool
goes down, the usage of some sort of smart pointer makes more sense.

This now makes all APIs use BufferPool be wrapped in a shared_ptr.
2023-12-21 15:23:23 +01:00
tomFlowee 33616fdcec Keep the DB cleaner on peer rejection
Should the peer have been rejected on status or similar, we don't
register a successful connection and avoid the peer from being selected
again since then we don't remove the punishment done on connection
start.
2023-08-17 21:32:09 +02:00
tomFlowee 991eadb311 Add some more debugging output 2023-08-17 21:32:09 +02:00
tomFlowee 9435c318f2 Clarify usage of punish() method
We removed the default value in order to avoid misusage of the method
and mistaking the amount with the connection-id.

From Peer we now se the faster overload of punish(), avoiding looping
through the peer list.

Also check return-code when needed.
2023-05-08 11:34:47 +02:00
tomFlowee 9d61366ff4 Be slightly more agressive.
Protocol violations should take less time to have effect.
2023-04-19 15:32:21 +02:00
tomFlowee 3c8cabfc9f Fix copy paste error.
The mempool message has no body.
2023-04-19 13:18:14 +02:00
tomFlowee a088a7867a Make GetHeaders replies more relevant
Lets avoid just getting empty answers when we are up-to-date.
2023-04-17 19:24:40 +02:00
tomFlowee c6302bd7c0 Limit the amount of times we send mempool 2023-04-06 13:32:30 +02:00
tomFlowee 2545060a2f Tweaks some (debug level) logging. 2023-04-05 18:03:37 +02:00
tomFlowee 1d309cd7ae P2PNet: re-do SVP's bloom filter and mempool.
Every peer that we connect to should get the bloom filter set and it
should also get the 'mempool' call sent once which will make that peer
respond with all matches of the bloom filters which are in the mempool.

The tricky part is that we should have the latest bloom filter set on
each of those peers before the mempool is sent, since they work
together.
Also if we are behind, the mempool should not be sent. (It can cause
problems in the wallet if we receive unconfirmed transactions before
mined transactions)

So, when a peer starts downloading merkle blocks, the bloom filter of all
the other peers becomes invalid the moment a match is found by an actual
wallet.
This is Ok on one peer because the merkleblock automatically updates the
filter on match on the server side, but obviously not on the other peers
we have for that wallet.

The approach we follow is that as soon as a sync-run is done on a single
peer (we do a main and also a backup sync), we tell the wallet to re-
build its bloom filter for _all_ peers and if the sync that peer did
leads us to be at the chain-tip, we also send each peer the mempool
command.
2023-04-01 22:54:35 +02:00
tomFlowee 6002803fa4 Add more bannable messages.
Alert:
This has been sunset so long ago, no complient BCH client will (/can) use it.

Protoconf:
This looks like its a BSV message.
2023-02-25 19:38:06 +01:00
tomFlowee 18fed67872 Use known not-bch p2p messages to disconnect
When we receive a known message (currently just avahello) we now
intantly ban and disconnect the peer, no need to
test compliance when they openly greet us saying they are not following
the same chain.
2023-02-25 12:02:19 +01:00
tomFlowee 70e4f2292e Refactor the SPV merkleblock/mempool sending
Tuesdays idea of adding some code into the SyncSPVAction didn't feel
right.
A second look made clear that bloom filter updates make much more sense
to go hand in hand with sending a mempool message. Especially since they
depend on each other on the server side.

To-rehash:
the wallet may decide at any time that a new bloom filter is needed. It
then uses the superclass (code in p2plib) PrivacySegment, to build that
filter. As part of that we get a lock object which, when going out of
scope, makes the peers that are subscribed to the privacySegment send
out the filter.

This separation of concerns means that the subclass wallet in the app
doens't know about peers or messages, only its superclass PrivacySegment
does.

What we did in this change is make the PrivacySegment class decide to
combine a bloom update with a mempool call. Typicall only once per
connection.

This means I can remove hacks in the SyncSPVAction which forced the
sending of the mempool message separately.
2023-02-24 19:41:42 +01:00
tomFlowee 62a6f5c7d9 Cleanups in spv sync for multiple peers
This makes the logic much more pridictable.
2022-07-13 14:00:47 +02:00
tomFlowee 1ab4d88993 Split version.h
Version.h held mostly stuff for protocol.h, which is a hub-specific file.
The only thing that we actually use is the PROTOCOL_VERSION in our code
and as such that one moved to the interfaces dir.
2022-07-06 22:50:53 +02:00
tomFlowee 640d327311 Make disabled wallet stop getting data. 2022-06-24 14:13:32 +02:00
tomFlowee 7376c28a3c Move pool thread-local getter to utils.
This nicely cleans up the calling usage a static on the connection
manager instance.
2022-01-24 12:06:37 +01:00
tomFlowee c71ebb9bee Avoid reporting incorrect updates 2021-11-26 18:10:31 +01:00
tomFlowee 701698d6e1 Fix merkleblock download as bloom filters change.
In HD wallets we get an issue where not-yet-generated keys are needed in
the merkle-block request and we notice this as a chunk of blocks is
being downloaded.
This is solved with some extra code.

Also avoid re-uploading the bloom filter during initial sync.
2021-10-27 19:14:09 +02:00
tomFlowee 6692a0a11a Follow API change in NWM.
Setting the ringbuffers to not-so-tiny values
2021-08-05 23:03:19 +02:00
tomFlowee 70f363cabb Also allow downloading of dsproofs. 2021-02-04 18:09:38 +01:00
tomFlowee 44e8717d1e Send a MEMPOOL message on SPV/merkleblock sync. 2021-02-04 17:39:44 +01:00
tomFlowee adfe4081cb Update copyright notice and email address 2021-02-02 13:08:07 +01:00
TomZ 301a4ca3ee Fix properly following the chain-tip
This is done in several steps:

1. Separate my height from the remote peer heights.
Instead of assuming we have one height, recognize that a peer may
not be at the tip at the same time we are. We monitor headers/invs
to update the 'peerHeight' variable.

2. Ask for merkle blocks from a peer to the maximum height of that
peer (but not later than what we validated to be true).
This avoids us asking past the remotes tip which they didn't like.

3. Redo the SyncSPVAction to use all this and make it much more
reliable in finding peers to download from and getting all the
changes as fast as possible.
2020-11-13 21:03:56 +01:00
TomZ 0556bc56d1 Fix bugs in SPV sync behavior. 2020-11-09 18:38:38 +01:00
TomZ ae58d68058 Fix loop never increasing. 2020-11-05 16:56:02 +01:00
TomZ 027c1a19d4 Don't ban without logline. 2020-10-28 22:06:15 +01:00
TomZ 652fc1e161 Introduce structure to broadcast transactions.
To send out transactions in the p2p net is quite a lot of work,
you need to find multiple peers to send the transaction to. First
you send an INV, then you respond to a getData to actually send
the transaction and last you wait for 'reject' messages that may
indicate that there is something wrong with the transaction.

This introduces the BroadcastTxData class that wraps a transaction
and gets callbacks for sending and for rejects, abstracting away
all the complexity for the user.
2020-06-08 21:35:10 +02:00
TomZ c2de615c97 Use enum 2020-06-08 21:31:00 +02:00
TomZ 5b63dd4c9b P2PNet: Peer downgrades log-level 2020-05-20 15:20:09 +02:00
TomZ 45d9ac3bd2 Make Peer listen to Bloom filter changes 2020-05-18 14:32:34 +02:00
TomZ 01e8bcfb09 Announce all transactions per block at once.
Instead of forwarding one transaction at a time as the peer sends them
to us, bundle them in a group of transactions known to be merkle-checked
and all belonging together in one block.

Since the peer has no obligation (and with CTOR even less) to send the
transactions in natural order, we should get them per block so we know
all transactions forwarded have parents.
2020-05-16 11:01:27 +02:00
TomZ c14aa3c1f7 Forgot to copy this. 2020-05-14 22:50:51 +02:00
TomZ 148f9c0e93 Improve tracking of 'good' peers.
This avoids a peer once sending acceptable headers and never
being bothered again. Instead we now check regularly and keep
track of when the peer was known to follow the same chain as us.
2020-05-11 18:49:16 +02:00
TomZ ebbbdcd3a2 P2PNet: log improvement.
Also fix cron disconnecting peers before they connect.
2020-05-10 16:02:14 +02:00
TomZ d49b1ae5e2 Make NetworkManager connections leaner
The NetworkManager usage was mostly for low connection counts and this
made defaults selection easy.
With more usages it is important to allow the NWM-connection to be more
configurable about memory usage and leaner in general.

This changes the headers-buffers (used to create envelopes) to not be per
connection anymore but per thread using the tread_local keyword.

This changes the ring-buffers to become configurable using
NetworkConnections::setMessageQueueSizes().

Also removing some include statements where they were not really needed
in the P2PNet lib.
2020-05-09 20:23:05 +02:00
TomZ 5bb095f8a2 Minor fixes in usage of shared pointers. 2020-05-07 18:26:41 +02:00
TomZ 6a5ccf30cf Move 'sendheaders' msg to after version handshake 2020-05-06 23:59:02 +02:00
TomZ 39fb4723ef Lost this in the refactor. 2020-05-06 18:38:37 +02:00
TomZ 7ad2592d40 P2PNet: API cleanups and review + docs. 2020-05-06 10:42:58 +02:00
TomZ fb6f91bf79 Simplify p2pnet ownership
Make the Peer object be owned by a shared pointer.
2020-05-05 22:53:25 +02:00