Sjors / bitcoin

Bitcoin Core integration/staging tree
https://bitcoin.org/en/download
MIT License
9 stars 11 forks source link

Introduce Sv2Transport #27

Closed Sjors closed 10 months ago

Sjors commented 11 months ago

This PR introduces Sv2Transport, a new subclass of Transport.

It's most similar to V2Transport, though with a simpler state machine.

In order to make it easier in the future to move the Stratum v2 integration to its own process, this PR also moves Transport out of net.h and makes it part of bitcoin-common.

This also makes for a nice decluttering of net.{h,cpp}, and useful for utilities that need a one-shot p2p connection, e.g. to fetch a specific block.

It would also allow the wallet to make a one-shot tor connection to broadcast a transaction without going through the node mempool. Though this would require a lot more changes.

Once the Sv2Transport class in this PR works, and the Template Provider switched to use it, I plan to rewrite the history of the main PR https://github.com/bitcoin/bitcoin/pull/28983. The move-only commits should not change in that process.

Sjors commented 11 months ago

CI on my fork can be ignored (I wonder if I can just turn it off).

In terms of progress, I'm half-way the handshake phase. Next step is implement Act 2 of the handshake, which should be a simple call to WriteMsgES of Sv2HandshakeState m_handshake_state; and updating the cipher like Sv2NoiseSession::ProcessMaybeHandshake does. The test harness can already write it, just needs to read it.

The next challenge is actual message parsing / serialising. The Transport abstract class converts between Span<const uint8_t> and CNetMessage / CSerializedNetMsg, while sv2_messages.h introduces Sv2NetMsg with its own serialisation. I'll have to study both in more detail to figure out if Sv2NetMsg can be made into a subclass of CNetMessage, or if there's another approach.

Finally, the Sv2TemplateProvider comes with its own SendBuf and GenerateWaitSockets. This has a lot of overlap with CConnman in net.h, but CConnman has too much p2p specific stuff to be used as a whole. I might therefore extract its socket handling code into another class.