Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Macros to define subprotocol packet ids in chain sync#10340

Closed
elferdo wants to merge 16 commits into
masterfrom
fhc-syncpacket-macros
Closed

Macros to define subprotocol packet ids in chain sync#10340
elferdo wants to merge 16 commits into
masterfrom
fhc-syncpacket-macros

Conversation

@elferdo
Copy link
Copy Markdown
Contributor

@elferdo elferdo commented Feb 13, 2019

This PR elaborates on comments by @tomusdrw in #10315

Synchronization of a node's blockchain with other nodes happens through an exchange of packets over the network. In parity we support a number of different synchronization protocols that are transmitted and received as devp2p subprotocols.

In devp2p, subprotocol and the particular message within it are identified with a single number instead of two, one for protocol and one for message. Care must be taken to ensure that each type of package within each subprotocol gets a globally unique id that does not collide with other subprotocols.

This PR is a first proposal of a derive-macro that can be applied to an enum and attributes for the enum variants. Code would possibly look like the following:

#[derive(SyncPackets)]
enum Packet {
   #[eth] StatusMessage = 0x01,
...
   #[par] GetSnapshotManifestPacket = 0x11,
}

Benefits of the approach:

  • The enum takes care itself that ids are globally unique
  • The derive-macro implements conversion method from_u8 so that ids read from the network can be directly converted to the appropriate identifier.
  • The provided from_u8 method returns an Option<Packet>
  • The derive-macro automatically provides functionality to query id and protocol from an instance of Packet
  • Protocol and Id are specified in one single compact line

Drawbacks:

  • Protocols are defined as attributes of a derive-macro. Defining new protocols involves changing the macro (this should not happen frequently, though)
  • Each message needs to be individually tagged with its protocol attribute. Whole blocks would be more ergonomic.
  • The derive-macro implements the trait PacketInfo and its method protocol(). The constants returned in the method must be already defined outside the scope of this macro.

The first version of the PR is lacking error checks.

@elferdo elferdo requested review from ngotchac and tomusdrw February 13, 2019 09:00
@parity-cla-bot
Copy link
Copy Markdown

It looks like @elferdo signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@elferdo elferdo closed this Feb 13, 2019
@5chdn 5chdn added this to the 2.4 milestone Feb 13, 2019
@5chdn 5chdn added A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. M4-core ⛓ Core client code / Rust. labels Feb 13, 2019
@elferdo
Copy link
Copy Markdown
Contributor Author

elferdo commented Feb 13, 2019

Sorry, had some issues with git. I need to create a new PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. M4-core ⛓ Core client code / Rust.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants