Module Mavryk_shell.P2p_reader

A P2p_reader.t is a worker that answers the application messages (see Messages) received from a remote peer on a P2p.connection.

The worker behavior is parameterized by callback functions passed by the caller (see type callback).

Terminology:

A chain can only be considered active for the remote peer if it is active for this peer. As a general rule, received messages that refer to a specific chain_id that is not active are ignored.

The protocol is defined as a message handling loop. We can classify a message in three categories. For each category, we describe the worker behavior when receiving the messages.

1. Administrative message

2. Chain-related messages

These messages are used for peers to get the most recent view of the chains they manage.

Only if chain_id is not active for the remote peer, sends a Get_current_branch chain_id message.

Then (in any case) sends a Current_branch (chain_id, chain_locator) message.

Otherwise, sends a message Protocol_branch (chain_id, protocol_locator) where protocol_locator encodes the longest branch where all the blocks are on proto_level. This branch is a subbranch of the current branch for the requested chain_id.

3. "Database" messages

These messages are used for peers to exchange "static" resources such as block headers, operations, protocols. The worker collaborates with the requesters defined in Distributed_db. Resources are requested by the requester, and the requester is notified by a P2p_reader upon reception of the resource.

The other database messages work similarly

type t
module Message = Distributed_db_message
module Block_hash_cache : Aches.Vache.MAP with type key = Mavryk_base.TzPervasives.Block_hash.t
type callback = {
  1. notify_branch : Mavryk_base.P2p_peer.Id.t -> Mavryk_base.Block_locator.t -> unit;
    (*

    callback function called on reception of a Current_branch message

    *)
  2. notify_head : Mavryk_base.P2p_peer.Id.t -> Mavryk_base.TzPervasives.Block_hash.t -> Mavryk_base.Block_header.t -> Mavryk_base.Mempool.t -> unit;
    (*

    callback function called on reception of a Current_head message

    *)
  3. disconnection : Mavryk_base.P2p_peer.Id.t -> unit;
}
type chain_db = {
  1. chain_store : Mavryk_store.Store.Chain.t;
  2. operation_db : Distributed_db_requester.Raw_operation.t;
  3. block_header_db : Distributed_db_requester.Raw_block_header.t;
  4. operations_db : Distributed_db_requester.Raw_operations.t;
  5. callback : callback;
  6. active_peers : Mavryk_base.P2p_peer.Set.t Stdlib.ref;
    (*

    Set of remote peers for which this chain is active.

    *)
  7. active_connections : connection Mavryk_base.P2p_peer.Table.t;
}

Lookup for block header in any active chains

run ~register ~unregister p2p state protocol_db active_chains peer_id conn runs an answering worker on a p2p connection connection. peer_id is the peer id of the remote peer. register is called once the worker is created, and unregister when the worker stops.

active_chains is the table of active chains (i.e. test chain, main chain...)

val shutdown : t -> unit Lwt.t