github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/docs/architecture/adr-007-trust-metric-usage.md (about)

     1  # ADR 007: Trust Metric Usage Guide
     2  
     3  ## Context
     4  
     5  Tendermint is required to monitor peer quality in order to inform its peer dialing and peer exchange strategies.
     6  
     7  When a node first connects to the network, it is important that it can quickly find good peers.
     8  Thus, while a node has fewer connections, it should prioritize connecting to higher quality peers.
     9  As the node becomes well connected to the rest of the network, it can dial lesser known or lesser
    10  quality peers and help assess their quality. Similarly, when queried for peers, a node should make
    11  sure they dont return low quality peers.
    12  
    13  Peer quality can be tracked using a trust metric that flags certain behaviours as good or bad. When enough
    14  bad behaviour accumulates, we can mark the peer as bad and disconnect.
    15  For example, when the PEXReactor makes a request for peers network addresses from an already known peer, and the returned network addresses are unreachable, this undesirable behavior should be tracked. Returning a few bad network addresses probably shouldn’t cause a peer to be dropped, while excessive amounts of this behavior does qualify the peer for removal. The originally proposed approach and design document for the trust metric can be found in the [ADR 006](adr-006-trust-metric.md) document.
    16  
    17  The trust metric implementation allows a developer to obtain a peer's trust metric from a trust metric store, and track good and bad events relevant to a peer's behavior, and at any time, the peer's metric can be queried for a current trust value. The current trust value is calculated with a formula that utilizes current behavior, previous behavior, and change between the two. Current behavior is calculated as the percentage of good behavior within a time interval. The time interval is short; probably set between 30 seconds and 5 minutes. On the other hand, the historic data can estimate a peer's behavior over days worth of tracking. At the end of a time interval, the current behavior becomes part of the historic data, and a new time interval begins with the good and bad counters reset to zero.
    18  
    19  These are some important things to keep in mind regarding how the trust metrics handle time intervals and scoring:
    20  
    21  - Each new time interval begins with a perfect score
    22  - Bad events quickly bring the score down and good events cause the score to slowly rise
    23  - When the time interval is over, the percentage of good events becomes historic data.
    24  
    25  Some useful information about the inner workings of the trust metric:
    26  
    27  - When a trust metric is first instantiated, a timer (ticker) periodically fires in order to handle transitions between trust metric time intervals
    28  - If a peer is disconnected from a node, the timer should be paused, since the node is no longer connected to that peer
    29  - The ability to pause the metric is supported with the store **PeerDisconnected** method and the metric **Pause** method
    30  - After a pause, if a good or bad event method is called on a metric, it automatically becomes unpaused and begins a new time interval.
    31  
    32  ## Decision
    33  
    34  The trust metric capability is now available, yet, it still leaves the question of how should it be applied throughout Tendermint in order to properly track the quality of peers?
    35  
    36  ### Proposed Process
    37  
    38  Peers are managed using an address book and a trust metric:
    39  
    40  - The address book keeps a record of peers and provides selection methods
    41  - The trust metric tracks the quality of the peers
    42  
    43  #### Presence in Address Book
    44  
    45  Outbound peers are added to the address book before they are dialed,
    46  and inbound peers are added once the peer connection is set up.
    47  Peers are also added to the address book when they are received in response to
    48  a pexRequestMessage.
    49  
    50  While a node has less than `needAddressThreshold`, it will periodically request more,
    51  via pexRequestMessage, from randomly selected peers and from newly dialed outbound peers.
    52  
    53  When a new address is added to an address book that has more than `0.5*needAddressThreshold` addresses,
    54  then with some low probability, a randomly chosen low quality peer is removed.
    55  
    56  #### Outbound Peers
    57  
    58  Peers attempt to maintain a minimum number of outbound connections by
    59  repeatedly querying the address book for peers to connect to.
    60  While a node has few to no outbound connections, the address book is biased to return
    61  higher quality peers. As the node increases the number of outbound connections,
    62  the address book is biased to return less-vetted or lower-quality peers.
    63  
    64  #### Inbound Peers
    65  
    66  Peers also maintain a maximum number of total connections, MaxNumPeers.
    67  If a peer has MaxNumPeers, new incoming connections will be accepted with low probability.
    68  When such a new connection is accepted, the peer disconnects from a probabilistically chosen low ranking peer
    69  so it does not exceed MaxNumPeers.
    70  
    71  #### Peer Exchange
    72  
    73  When a peer receives a pexRequestMessage, it returns a random sample of high quality peers from the address book. Peers with no score or low score should not be inclided in a response to pexRequestMessage.
    74  
    75  #### Peer Quality
    76  
    77  Peer quality is tracked in the connection and across the reactors by storing the TrustMetric in the peer's
    78  thread safe Data store.
    79  
    80  Peer behaviour is then defined as one of the following:
    81  
    82  - Fatal - something outright malicious that causes us to disconnect the peer and ban it from the address book for some amount of time
    83  - Bad - Any kind of timeout, messages that don't unmarshal, fail other validity checks, or messages we didn't ask for or aren't expecting (usually worth one bad event)
    84  - Neutral - Unknown channels/message types/version upgrades (no good or bad events recorded)
    85  - Correct - Normal correct behavior (worth one good event)
    86  - Good - some random majority of peers per reactor sending us useful messages (worth more than one good event).
    87  
    88  Note that Fatal behaviour causes us to remove the peer, and neutral behaviour does not affect the score.
    89  
    90  ## Status
    91  
    92  Proposed.
    93  
    94  ## Consequences
    95  
    96  ### Positive
    97  
    98  - Bringing the address book and trust metric store together will cause the network to be built in a way that encourages greater security and reliability.
    99  
   100  ### Negative
   101  
   102  - TBD
   103  
   104  ### Neutral
   105  
   106  - Keep in mind that, good events need to be recorded just as bad events do using this implementation.