You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.1 KiB

7 years ago
  1. ## MConnection
  2. `MConnection` is a multiplex connection:
  3. __multiplex__ *noun* a system or signal involving simultaneous transmission of
  4. several messages along a single channel of communication.
  5. Each `MConnection` handles message transmission on multiple abstract communication
  6. `Channel`s. Each channel has a globally unique byte id.
  7. The byte id and the relative priorities of each `Channel` are configured upon
  8. initialization of the connection.
  9. The `MConnection` supports three packet types: Ping, Pong, and Msg.
  10. ### Ping and Pong
  11. The ping and pong messages consist of writing a single byte to the connection; 0x1 and 0x2, respectively
  12. When we haven't received any messages on an `MConnection` in a time `pingTimeout`, we send a ping message.
  13. When a ping is received on the `MConnection`, a pong is sent in response.
  14. If a pong is not received in sufficient time, the peer's score should be decremented (TODO).
  15. ### Msg
  16. Messages in channels are chopped into smaller msgPackets for multiplexing.
  17. ```
  18. type msgPacket struct {
  19. ChannelID byte
  20. EOF byte // 1 means message ends here.
  21. Bytes []byte
  22. }
  23. ```
  24. The msgPacket is serialized using go-wire, and prefixed with a 0x3.
  25. The received `Bytes` of a sequential set of packets are appended together
  26. until a packet with `EOF=1` is received, at which point the complete serialized message
  27. is returned for processing by the corresponding channels `onReceive` function.
  28. ### Multiplexing
  29. Messages are sent from a single `sendRoutine`, which loops over a select statement that results in the sending
  30. of a ping, a pong, or a batch of data messages. The batch of data messages may include messages from multiple channels.
  31. Message bytes are queued for sending in their respective channel, with each channel holding one unsent message at a time.
  32. Messages are chosen for a batch one a time from the channel with the lowest ratio of recently sent bytes to channel priority.
  33. ## Sending Messages
  34. There are two methods for sending messages:
  35. ```go
  36. func (m MConnection) Send(chID byte, msg interface{}) bool {}
  37. func (m MConnection) TrySend(chID byte, msg interface{}) bool {}
  38. ```
  39. `Send(chID, msg)` is a blocking call that waits until `msg` is successfully queued
  40. for the channel with the given id byte `chID`. The message `msg` is serialized
  41. using the `tendermint/wire` submodule's `WriteBinary()` reflection routine.
  42. `TrySend(chID, msg)` is a nonblocking call that returns false if the channel's
  43. queue is full.
  44. `Send()` and `TrySend()` are also exposed for each `Peer`.
  45. ## Peer
  46. Each peer has one `MConnection` instance, and includes other information such as whether the connection
  47. was outbound, whether the connection should be recreated if it closes, various identity information about the node,
  48. and other higher level thread-safe data used by the reactors.
  49. ## Switch/Reactor
  50. The `Switch` handles peer connections and exposes an API to receive incoming messages
  51. on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  52. or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  53. incoming messages are received on the reactor.
  54. ```go
  55. // Declare a MyReactor reactor that handles messages on MyChannelID.
  56. type MyReactor struct{}
  57. func (reactor MyReactor) GetChannels() []*ChannelDescriptor {
  58. return []*ChannelDescriptor{ChannelDescriptor{ID:MyChannelID, Priority: 1}}
  59. }
  60. func (reactor MyReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {
  61. r, n, err := bytes.NewBuffer(msgBytes), new(int64), new(error)
  62. msgString := ReadString(r, n, err)
  63. fmt.Println(msgString)
  64. }
  65. // Other Reactor methods omitted for brevity
  66. ...
  67. switch := NewSwitch([]Reactor{MyReactor{}})
  68. ...
  69. // Send a random message to all outbound connections
  70. for _, peer := range switch.Peers().List() {
  71. if peer.IsOutbound() {
  72. peer.Send(MyChannelID, "Here's a random message")
  73. }
  74. }
  75. ```
  76. ### PexReactor/AddrBook
  77. A `PEXReactor` reactor implementation is provided to automate peer discovery.
  78. ```go
  79. book := p2p.NewAddrBook(addrBookFilePath)
  80. pexReactor := p2p.NewPEXReactor(book)
  81. ...
  82. switch := NewSwitch([]Reactor{pexReactor, myReactor, ...})
  83. ```