github.com/evdatsion/aphelion-dpos-bft@v0.32.1/lite/doc.go (about)

     1  /*
     2  Package lite allows you to securely validate headers without a full node.
     3  
     4  This library pulls together all the crypto and algorithms, so given a
     5  relatively recent (< unbonding period) known validator set, one can get
     6  indisputable proof that data is in the chain (current state) or detect if the
     7  node is lying to the client.
     8  
     9  Tendermint RPC exposes a lot of info, but a malicious node could return any
    10  data it wants to queries, or even to block headers, even making up fake
    11  signatures from non-existent validators to justify it. This is a lot of logic
    12  to get right, to be contained in a small, easy to use library, that does this
    13  for you, so you can just build nice applications.
    14  
    15  We design for clients who have no strong trust relationship with any Tendermint
    16  node, just the blockchain and validator set as a whole.
    17  
    18  SignedHeader
    19  
    20  SignedHeader is a block header along with a commit -- enough validator
    21  precommit-vote signatures to prove its validity (> 2/3 of the voting power)
    22  given the validator set responsible for signing that header. A FullCommit is a
    23  SignedHeader along with the current and next validator sets.
    24  
    25  The hash of the next validator set is included and signed in the SignedHeader.
    26  This lets the lite client keep track of arbitrary changes to the validator set,
    27  as every change to the validator set must be approved by inclusion in the
    28  header and signed in the commit.
    29  
    30  In the worst case, with every block changing the validators around completely,
    31  a lite client can sync up with every block header to verify each validator set
    32  change on the chain. In practice, most applications will not have frequent
    33  drastic updates to the validator set, so the logic defined in this package for
    34  lite client syncing is optimized to use intelligent bisection and
    35  block-skipping for efficient sourcing and verification of these data structures
    36  and updates to the validator set (see the DynamicVerifier for more
    37  information).
    38  
    39  The FullCommit is also declared in this package as a convenience structure,
    40  which includes the SignedHeader along with the full current and next
    41  ValidatorSets.
    42  
    43  Verifier
    44  
    45  A Verifier validates a new SignedHeader given the currently known state. There
    46  are two different types of Verifiers provided.
    47  
    48  BaseVerifier - given a validator set and a height, this Verifier verifies
    49  that > 2/3 of the voting power of the given validator set had signed the
    50  SignedHeader, and that the SignedHeader was to be signed by the exact given
    51  validator set, and that the height of the commit is at least height (or
    52  greater).
    53  
    54  DynamicVerifier - this Verifier implements an auto-update and persistence
    55  strategy to verify any SignedHeader of the blockchain.
    56  
    57  Provider and PersistentProvider
    58  
    59  A Provider allows us to store and retrieve the FullCommits.
    60  
    61      type Provider interface {
    62          // LatestFullCommit returns the latest commit with
    63          // minHeight <= height <= maxHeight.
    64          // If maxHeight is zero, returns the latest where
    65          // minHeight <= height.
    66          LatestFullCommit(chainID string, minHeight, maxHeight int64) (FullCommit, error)
    67      }
    68  
    69  * client.NewHTTPProvider - query Tendermint rpc.
    70  
    71  A PersistentProvider is a Provider that also allows for saving state.  This is
    72  used by the DynamicVerifier for persistence.
    73  
    74      type PersistentProvider interface {
    75          Provider
    76  
    77          // SaveFullCommit saves a FullCommit (without verification).
    78          SaveFullCommit(fc FullCommit) error
    79      }
    80  
    81  * DBProvider - persistence provider for use with any libs/DB.
    82  
    83  * MultiProvider - combine multiple providers.
    84  
    85  The suggested use for local light clients is client.NewHTTPProvider(...) for
    86  getting new data (Source), and NewMultiProvider(NewDBProvider("label",
    87  dbm.NewMemDB()), NewDBProvider("label", db.NewFileDB(...))) to store confirmed
    88  full commits (Trusted)
    89  
    90  
    91  How We Track Validators
    92  
    93  Unless you want to blindly trust the node you talk with, you need to trace
    94  every response back to a hash in a block header and validate the commit
    95  signatures of that block header match the proper validator set.  If there is a
    96  static validator set, you store it locally upon initialization of the client,
    97  and check against that every time.
    98  
    99  If the validator set for the blockchain is dynamic, verifying block commits is
   100  a bit more involved -- if there is a block at height H with a known (trusted)
   101  validator set V, and another block at height H' (H' > H) with validator set V'
   102  != V, then we want a way to safely update it.
   103  
   104  First, we get the new (unconfirmed) validator set V' and verify that H' is
   105  internally consistent and properly signed by this V'. Assuming it is a valid
   106  block, we check that at least 2/3 of the validators in V also signed it,
   107  meaning it would also be valid under our old assumptions.  Then, we accept H'
   108  and V' as valid and trusted and use that to validate for heights X > H' until a
   109  more recent and updated validator set is found.
   110  
   111  If we cannot update directly from H -> H' because there was too much change to
   112  the validator set, then we can look for some Hm (H < Hm < H') with a validator
   113  set Vm.  Then we try to update H -> Hm and then Hm -> H' in two steps.  If one
   114  of these steps doesn't work, then we continue bisecting, until we eventually
   115  have to externally validate the validator set changes at every block.
   116  
   117  Since we never trust any server in this protocol, only the signatures
   118  themselves, it doesn't matter if the seed comes from a (possibly malicious)
   119  node or a (possibly malicious) user.  We can accept it or reject it based only
   120  on our trusted validator set and cryptographic proofs. This makes it extremely
   121  important to verify that you have the proper validator set when initializing
   122  the client, as that is the root of all trust.
   123  
   124  The software currently assumes that the unbonding period is infinite in
   125  duration.  If the DynamicVerifier hasn't been updated in a while, you should
   126  manually verify the block headers using other sources.
   127  
   128  TODO: Update the software to handle cases around the unbonding period.
   129  
   130  */
   131  package lite