github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/docs/source/architecture/validator_network.rst (about)

     1  *****************
     2  Validator Network
     3  *****************
     4  
     5  The network layer is responsible for communication between validators in a
     6  Sawtooth network, including performing initial connectivity, peer discovery,
     7  and message handling. Upon startup, validator instances begin listening on a
     8  specified interface and port for incoming connections. Upon connection and
     9  peering, validators exchange messages with each other based on the rules of a
    10  gossip or epidemic [#f1]_ protocol.
    11  
    12  A primary design goal is to keep the network layer as self-contained as
    13  possible. For example, the network layer should not need knowledge of the
    14  payload of application messages, nor should it need application-layer provided
    15  data to connect to peers or to build out the connectivity of the network.
    16  Conversely, the application should not need to understand implementation
    17  details of the network in order to send and receive messages.
    18  
    19  Services
    20  ========
    21  
    22  The choice of 0MQ provides considerable flexibility in both available
    23  connectivity patterns and the underlying capabilities of the transport layer
    24  (IPv4, IPv6, etc.)
    25  
    26  We have adopted the 0MQ Asynchronous Client/Server Pattern [#f2]_ which consists
    27  of a 0MQ ROUTER socket on the server side which listens on a provided
    28  endpoint, with a number of connected 0MQ DEALER sockets as the connected
    29  clients. The 0MQ guide describes the features of this pattern as follows:
    30  
    31  - Clients connect to the server and send requests.
    32  - For each request, the server sends 0 or more replies.
    33  - Clients can send multiple requests without waiting for a reply.
    34  - Servers can send multiple replies without waiting for new requests.
    35  
    36  
    37  .. figure:: ../images/multiple_dealer_to_router.*
    38     :width: 50%
    39     :align: center
    40     :alt: Multiple dealer to router diagram
    41  
    42     Multiple DEALER to ROUTER socket pattern
    43  
    44  
    45  |
    46  
    47  States
    48  ======
    49  
    50  We define three states related to the connection between any two validator
    51  nodes:
    52  
    53  - Unconnected
    54  - Connected - A connection is a required prerequisite for peering.
    55  - Peered - A bidirectional relationship that forms the base case for
    56    application level message passing (gossip).
    57  
    58  
    59  Wire Protocol
    60  =============
    61  We have standardized on protobuf serialization for any structured messages that
    62  need to be passed over the network. All payloads to or from the application
    63  layer are treated as opaque.
    64  
    65  CONNECT
    66  
    67  Connect is the mechanism for initiating the connection to the remote node.
    68  Connect performs a basic 0MQ DEALER->ROUTER connection to the remote node and
    69  exchanges identity information for the purpose of supporting a two-way
    70  conversation. Connections sit atop 0MQ sockets and allow the DEALER/ROUTER
    71  conversation.
    72  
    73  PING
    74  
    75  Ping messages allow for keep alive between ROUTER and DEALER sockets.
    76  
    77  PEER
    78  
    79  Peer requests establish a bidirectional peering relationship between the two
    80  nodes. A Peer request can be rejected by the remote node. If a peer request is
    81  rejected, the expectation is that a node attempts to connect with other
    82  nodes in the network via some strategy until the peering minimum connectivity
    83  threshold for that node is reached. If possible, the bi-directional
    84  relationship occurs over the already established 0MQ socket between
    85  DEALER and ROUTER.
    86  
    87  GET_PEERS
    88  
    89  Returns a list of peers of a given node. This can be performed in a basic
    90  Connected state and does not require peering to have occurred. The intent is to
    91  allow a node attempting to reach its minimum connectivity peering threshold to
    92  build a view of active candidate peers via a neighbor of neighbors approach.
    93  
    94  BROADCAST(MSG)
    95  
    96  Transmits an application message to the network following a 'gossipy' pattern.
    97  This does not guarantee 100% delivery of the message to the whole network, but
    98  based on the gossip parameters, nearly complete delivery is likely. A node
    99  only accepts messages for broadcast/forwarding from peers.
   100  
   101  SEND(NODE, MSG)
   102  
   103  Attempts to send a message to a particular node over the bidirectional 0MQ
   104  connection. Delivery is not guaranteed. If a node has reason to believe that
   105  delivery to the destination node is impossible, it can return an error response.
   106  A node only accepts a message for sending from peer nodes.
   107  
   108  REQUEST(MSG)
   109  
   110  A request is a special type of broadcast message that can be examined and
   111  replied to, rather than forwarded. The intent is for the application layer to
   112  construct a message payload which can be examined by a special request handler
   113  and replied to, rather than forwarded on to connected peers. If the application
   114  layer reports that the request can’t be satisfied, the message will be
   115  forwarded to peers per the rules of a standard broadcast message. A node
   116  only accepts request messages from peer nodes.
   117  
   118  UNPEER
   119  
   120  Breaks the peering relationship between nodes. This may occur in several
   121  scenarios, for example a node leaving the network (nodes may also silently
   122  leave the network, in which case their departure will be detected by the
   123  failure of the ping/keepalive). An unpeer request does not necessarily imply a
   124  disconnect.
   125  
   126  DISCONNECT
   127  
   128  Breaks the wire protocol connection to the remote node. Informs the ROUTER end
   129  to clean up the connection.
   130  
   131  Peer Discovery
   132  ==============
   133  
   134  A bidirectional peering via a neighbor of neighbors approach gives reliable
   135  connectivity (messages delivered to all nodes >99% of the time based on random
   136  construction of the network).
   137  
   138  Peer connections are established by collecting a suitable population of
   139  candidate peers through successive CONNECT/GET_PEERS calls
   140  (neighbors of neighbors). The connecting validator then selects a candidate
   141  peer randomly from the list and attempts to connect and peer with it. If this
   142  succeeds, and the connecting validator has reached minimum connectivity, the
   143  process halts. If minimum connectivity has not yet been reached, the validator
   144  continues attempting to connect to new candidate peers, refreshing its view of
   145  the neighbors of neighbors if it exhausts candidates.
   146  
   147  .. figure:: ../images/bidirectional_peering.*
   148     :width: 75%
   149     :align: center
   150     :alt: Output of bidirectional peering with targeted connectivity of 4.
   151  
   152     Output of bidirectional peering with targeted connectivity of 4.
   153  
   154  |
   155  
   156  The network component continues to perform a peer search if its number of
   157  peers is less than the minimum connectivity. The network component rejects
   158  peering attempts if its number of peers is equal to or greater than the maximum
   159  connectivity. Even if maximum peer connections is reached, a network service
   160  should still accept and respond to a reasonable number of connections (for the
   161  purposes of other node topology build outs, etc.)
   162  
   163  Related Components
   164  ==================
   165  .. figure:: ../images/related_components.*
   166     :width: 75%
   167     :align: center
   168     :alt: Related Components Diagram.
   169  
   170  |
   171  
   172  Message Delivery
   173  ================
   174  
   175  The network delivers application messages (payloads received via BROADCAST
   176  or SEND) to the application layer. The network also performs a basic
   177  validation of messages prior to forwarding by calling a handler in the Message
   178  Validation component.
   179  
   180  When the network receives a REQUEST message, it calls a provided handler
   181  (a "Responder”, for example) to determine if the request can be
   182  satisfied. If so, the expectation is that the application layer generates a
   183  SEND message with a response that satisfies the request. In this condition, the
   184  network layer does not continue to propagate the REQUEST message to the network.
   185  
   186  In the case where a node could not satisfy the request, the node stores who
   187  it received the request from and BROADCASTs the request on to its peers. If that
   188  node receives a SEND message with the response to the request, it forwards
   189  the SEND message back to the original requester.
   190  
   191  The network accepts application payloads for BROADCAST, SEND, and REQUEST
   192  from the application layer.
   193  
   194  Network Layer Security
   195  ======================
   196  
   197  0MQ includes a TLS [#f3]_ like certificate exchange mechanism and protocol
   198  encryption capability which is transparent to the socket implementation.
   199  Support for socket level encryption is currently implemented with
   200  server keys being read from the validator.toml config file. For each client,
   201  ephemeral certificates are generated on connect. If the server key pair is not
   202  configured, network communications between validators will not be authenticated
   203  or encrypted.
   204  
   205  Network Permissioning
   206  =====================
   207  One of the permissioning requirements is that the validator network be able to
   208  limit the nodes that are able to connect to it. The permissioning rules
   209  determine the roles a connection is able to play on the network. The roles
   210  control the types of messages that can be sent and received over a given
   211  connection. The components and nodes that wish to take on these roles must
   212  participate in an authorization “handshake” and request the roles they want to
   213  take on. The entities acting in the different roles will be referred to as
   214  requesters below.
   215  
   216  Validators are able to determine whether messages delivered to them should
   217  be handled or dropped based on a set of role and identities stored within the
   218  Identity namespace. Each requester will be identified by the public key derived
   219  from their identity signing key. Permission verifiers examine incoming
   220  messages against the policy and the current configuration and either permit,
   221  drop, or respond with an error. In certain cases, the connection will be
   222  forcibly closed -- for example: if a node is not allowed to connect to the
   223  validator network.
   224  
   225  The following describes the procedure for establishing a new connection with
   226  the validator. The procedure supports implementing different authorization
   227  types that require the requester to prove their identity. If a requester
   228  deviates from the procedure in any way, the requester will be rejected and the
   229  connection will be closed. The same is true if the requester sends multiple
   230  ConnectionRequests or multiple of any authorization-type message. Certain low
   231  level messages, such as Ping, can be used before the procedure is complete, but
   232  are rate limited. If too many of the low level messages are received or
   233  they are received too close together, the connection may be considered
   234  malicious and rejected.
   235  
   236  The validator receiving a new connection receives a ConnectionRequest.
   237  The validator responds with a ConnectionResponse message. The
   238  ConnectionResponse message contains a list of RoleEntry messages and an
   239  AuthorizationType. Role entries are the accepted type of connections that are
   240  supported on the endpoint that the ConnectionRequest was sent to.
   241  AuthorizationType describes the procedure required to gain access to that role.
   242  Trust is the simplest authorization type and must be implemented by all
   243  requesters at a minimum. If the requester cannot comply with the given
   244  authorization type for that role entry, it is unable to gain access to that
   245  role.
   246  
   247  .. code-block:: protobuf
   248  
   249    message ConnectionRequest {
   250      // This is the first message that must be sent to start off authorization.
   251      // The endpoint of the connection.
   252      string endpoint = 1;
   253    }
   254  
   255    enum RoleType {
   256      // A shorthand request for asking for all allowed roles.
   257      ALL = 0;
   258  
   259      // Role defining validator to validator communication
   260      NETWORK = 1;
   261    }
   262  
   263    message ConnectionResponse {
   264      // Whether the connection can participate in authorization
   265      enum Status {
   266        OK = 0;
   267        ERROR = 1;
   268      }
   269  
   270      //Authorization Type required for the authorization procedure
   271      enum AuthorizationType {
   272        TRUST = 0;
   273        CHALLENGE = 1;
   274      }
   275  
   276      message RoleEntry {
   277        // The role type for this role entry
   278        RoleType role = 1;
   279  
   280        // The Authorization Type required for the above role
   281        AuthorizationType auth_type = 2;
   282      }
   283  
   284      repeated RoleEntry roles = 1;
   285      Status status = 2;
   286    }
   287  
   288  In the future, RoleType will include other roles such as CLIENT, STATE_DELTA,
   289  and TP.
   290  
   291  .. _Authorization_Types:
   292  
   293  Authorization Types
   294  -------------------
   295  Presented here are the two authorization types that will be implemented
   296  initially: Trust and Challenge.
   297  
   298  Trust
   299    The simplest authorization type is Trust. If Trust authorization is
   300    enabled, the validator will trust the connection and approve any roles
   301    requested that are available on that endpoint. If the requester wishes to gain
   302    access to every role it has permission to access, it can request access to the
   303    role ALL, and the validator will respond with all available roles. However, if
   304    a role that is not available is requested, the requester is rejected and the
   305    connection will be closed.
   306  
   307    .. code-block:: protobuf
   308  
   309      message AuthorizationTrustRequest {
   310        // A set of requested RoleTypes
   311        repeated RoleType roles = 1;
   312        string public_key = 2;
   313      }
   314  
   315      message AuthorizationTrustResponse {
   316        // The actual set the requester has access to
   317        repeated RoleType roles = 1;
   318      }
   319  
   320    Message flow for Trust Authorization:
   321  
   322    .. image:: ../images/trust_authorization.*
   323       :width: 80%
   324       :align: center
   325       :alt: Trust Authorization Flow
   326  
   327  Challenge
   328    If the connection wants to take on a role that requires a challenge to be
   329    signed, it will request the challenge by sending the following to the
   330    validator it wishes to connect to.
   331  
   332    .. code-block:: protobuf
   333  
   334      message AuthorizationChallengeRequest {
   335        // Empty message sent to request a payload to sign
   336      }
   337  
   338    The validator will send back a random payload that must be signed.
   339  
   340    .. code-block:: protobuf
   341  
   342      message AuthorizationChallengeResponse {
   343        // Random payload that the connecting node must sign
   344        bytes payload = 1;
   345      }
   346  
   347    The requester then signs the payload message and returns a response that
   348    includes the following:
   349  
   350    .. code-block:: protobuf
   351  
   352      message AuthorizationChallengeSubmit {
   353        // public key of node
   354        string public_key = 1;
   355  
   356        // signature derived from signing the challenge payload
   357        string signature = 3;
   358  
   359        // A set of requested Roles
   360        repeated RoleType roles = 4;
   361      }
   362  
   363    The requester may also request ALL. The validator will respond with a
   364    status that says whether the challenge was accepted and the roles that the
   365    connection is allowed take on.
   366  
   367    .. code-block:: protobuf
   368  
   369      message AuthorizationChallengeResult {
   370        // The approved roles for that connection
   371        repeated RoleType roles = 1;
   372      }
   373  
   374    Message flow for Challenge Authorization:
   375  
   376    .. image:: ../images/challenge_authorization.*
   377       :width: 80%
   378       :align: center
   379       :alt: Challenge Authorization Flow
   380  
   381    When the validator receives an AuthorizationChallengeSubmit message, it
   382    verifies the public key against the signature. If the public key is verified,
   383    the requested roles is checked against the stored roles to see if the
   384    public key is included in the policy. If the node’s response is accepted, the
   385    node’s public key is stored and the requester may start sending messages
   386    for the approved roles.
   387  
   388    If the requester wanted a role that is either not available on the endpoint
   389    or the requester does not have access to one of the roles requested, the
   390    challenge will be rejected and the connection is closed. At that point
   391    the requester will need to restart the connection process.
   392  
   393  Authorization Violation
   394    If at any time a requester tries to send a message that is against its
   395    allowed permission, the validator responds with an AuthorizationViolation
   396    message and the connection is closed. If that requester wishes to rejoin
   397    the network, it will need to go back through the connection and authorization
   398    process described above.
   399  
   400    .. code-block:: protobuf
   401  
   402      message AuthorizationViolation {
   403        // The Role the requester did not have access to
   404        RoleType violation = 1;
   405      }
   406  
   407  .. rubric:: Footnotes
   408  
   409  .. [#f1] http://web.mit.edu/devavrat/www/GossipBook.pdf
   410  .. [#f2] http://zguide.zeromq.org/php:chapter3#toc19
   411  .. [#f3] https://github.com/zeromq/pyzmq/blob/master/examples/security/ironhouse.py
   412  
   413  .. Licensed under Creative Commons Attribution 4.0 International License
   414  .. https://creativecommons.org/licenses/by/4.0/