In my last update, I discussed the construction of fork data from fork parameters and the implementation of SSZ decoding on GossipSub messages. I also mentioned an issue with bad peer score that caused connection shutdowns due to the lack of Req/Res Protocol implementation. This is the main topic of this update.
This is the PR concerning this feature:
This protocol enables consensus layer peers to communicate directly in order to check liveness or request data, such as specific blocks. My implementation was based on the Lighthouse client, however it is not generic across multiple network specifications.
I have decided not to include the BlocksByRange
and BlocksByRoot
methods in this implementation, as they do not impact peer scoring.
The supported protocol messages are:
It can be seen that the StatusMessage
holds block data. Currently I’m not storing any data so it won’t be possible to build this info, I have two options:
It’s also possible that I might be missing something and this could be done in other way.
The RPC
struct consists of a vector of the events taking place in the behavior.
The implementation of the NetworkBehaviour
trait for our RPC
struct is relatively simple. The challenge resides in the ConnectionHandler
which in our case is a RPCHandler<Id>
, that deals with the events.
This struct has to implement the ConnectionHandler trait in order to be used as such in the NetworkBehaviour
.
Below is an overview of the implementation of this trait for this struct:
The encoding for the inbound and outbound streams using the tokio_util
crates is implemented as a separated module.
As it can be seen there is a lot going on when dealing with raw streams of data, although we're lucky that the protocols have already been negotiated once they get here.
The addition of this new protocol to the current network behavior was the most straightforward part.
I’m really amazed about the tools and flexibility that libp2p provides for one to build their own custom protocol on top of it.
I’m currently testing this locally and I am able to receive the RPC requests correctly. However I’m still not handling the responses correctly and keep getting shutdown.
For my next steps is the creation of the response data and the proper handling of the requests.
Until then!