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.

432 lines
23 KiB

  1. # Lite client
  2. A lite client is a process that connects to Tendermint full nodes and then tries to verify application data using the Merkle proofs.
  3. ## Context of this document
  4. In order to make sure that full nodes have the incentive to follow the protocol, we have to address the following three Issues
  5. 1) The lite client needs a method to verify headers it obtains from a full node it connects to according to trust assumptions -- this document.
  6. 2) The lite client must be able to connect to other full nodes to detect and report on failures in the trust assumptions (i.e., conflicting headers) -- a future document (see #4215).
  7. 3) In the event the trust assumption fails (i.e., a lite client is fooled by a conflicting header), the Tendermint fork accountability protocol must account for the evidence -- a future document (see #3840).
  8. ## Problem statement
  9. We assume that the lite client knows a (base) header *inithead* it trusts (by social consensus or because the lite client has decided to trust the header before). The goal is to check whether another header *newhead* can be trusted based on the data in *inithead*.
  10. The correctness of the protocol is based on the assumption that *inithead* was generated by an instance of Tendermint consensus. The term "trusting" above indicates that the correctness of the protocol depends on this assumption. It is in the responsibility of the user that runs the lite client to make sure that the risk of trusting a corrupted/forged *inithead* is negligible.
  11. ## Definitions
  12. ### Data structures
  13. In the following, only the details of the data structures needed for this specification are given.
  14. * header fields
  15. - *height*
  16. - *bfttime*: the chain time when the header (block) was generated
  17. - *V*: validator set containing validators for this block.
  18. - *NextV*: validator set for next block.
  19. - *commit*: evidence that block with height *height* - 1 was committed by a set of validators (canonical commit). We will use ```signers(commit)``` to refer to the set of validators that committed the block.
  20. * signed header fields: contains a header and a *commit* for the current header; a "seen commit". In the Tendermint consensus the "canonical commit" is stored in header *height* + 1.
  21. * For each header *h* it has locally stored, the lite client stores whether
  22. it trusts *h*. We write *trust(h) = true*, if this is the case.
  23. * Validator fields. We will write a validator as a tuple *(v,p)* such that
  24. + *v* is the identifier (we assume identifiers are unique in each validator set)
  25. + *p* is its voting power
  26. ### Functions
  27. For the purpose of this lite client specification, we assume that the Tendermint Full Node exposes the following function over Tendermint RPC:
  28. ```go
  29. func Commit(height int64) (SignedHeader, error)
  30. // returns signed header: header (with the fields from
  31. // above) with Commit that include signatures of
  32. // validators that signed the header
  33. type SignedHeader struct {
  34. Header Header
  35. Commit Commit
  36. }
  37. ```
  38. ### Definitions
  39. * *TRUSTED_PERIOD*: trusting period
  40. * for realtime *t*, the predicate *correct(v,t)* is true if the validator *v*
  41. follows the protocol until time *t* (we will see about recovery later).
  42. ### Tendermint Failure Model
  43. If a block *h* is generated at time *bfttime* (and this time is stored in the block), then a set of validators that hold more than 2/3 of the voting power in h.Header.NextV is correct until time h.Header.bfttime + TRUSTED_PERIOD.
  44. Formally,
  45. \[
  46. \sum_{(v,p) \in h.Header.NextV \wedge correct(v,h.Header.bfttime + TRUSTED_PERIOD)} p >
  47. 2/3 \sum_{(v,p) \in h.Header.NextV} p
  48. \]
  49. *Assumption*: "correct" is defined w.r.t. realtime (some Newtonian global notion of time, i.e., wall time), while *bfttime* corresponds to the reading of the local clock of a validator (how this time is computed may change when the Tendermint consensus is modified). In this note, we assume that all clocks are synchronized to realtime. We can make this more precise eventually (incorporating clock drift, accuracy, precision, etc.). Right now, we consider this assumption sufficient, as clock synchronization (under NTP) is in the order of milliseconds and *tp* is in the order of weeks.
  50. *Remark*: This failure model might change to a hybrid version that takes heights into account in the future.
  51. The specification in this document considers an implementation of the lite client under this assumption. Issues like *counter-factual signing* and *fork accountability* and *evidence submission* are mechanisms that justify this assumption by incentivizing validators to follow the protocol.
  52. If they don't, and we have more that 1/3 faults, safety may be violated. Our approach then is to *detect* these cases (after the fact), and take suitable repair actions (automatic and social). This is discussed in an upcoming document on "Fork accountability". (These safety violations include the lite client wrongly trusting a header, a fork in the blockchain, etc.)
  53. ## Lite Client Trusting Spec
  54. The lite client communicates with a full node and learns new headers. The goal is to locally decide whether to trust a header. Our implementation needs to ensure the following two properties:
  55. - Lite Client Completeness: If header *h* was correctly generated by an instance of Tendermint consensus (and its age is less than the trusting period), then the lite client should eventually set *trust(h)* to true.
  56. - Lite Client Accuracy: If header *h* was *not generated* by an instance of Tendermint consensus, then the lite client should never set *trust(h)* to true.
  57. *Remark*: If in the course of the computation, the lite client obtains certainty that some headers were forged by adversaries (that is were not generated by an instance of Tendermint consensus), it may submit (a subset of) the headers it has seen as evidence of misbehavior.
  58. *Remark*: In Completeness we use "eventually", while in practice *trust(h)* should be set to true before *h.Header.bfttime + tp*. If not, the block cannot be trusted because it is too old.
  59. *Remark*: If a header *h* is marked with *trust(h)*, but it is too old (its bfttime is more than *tp* ago), then the lite client should set *trust(h)* to false again.
  60. *Assumption*: Initially, the lite client has a header *inithead* that it trusts correctly, that is, *inithead* was correctly generated by the Tendermint consensus.
  61. To reason about the correctness, we may prove the following invariant.
  62. *Verification Condition: Lite Client Invariant.*
  63. For each lite client *l* and each header *h*:
  64. if *l* has set *trust(h) = true*,
  65. then validators that are correct until time *h.Header.bfttime + tp* have more than two thirds of the voting power in *h.Header.NextV*.
  66. Formally,
  67. \[
  68. \sum_{(v,p) \in h.Header.NextV \wedge correct(v,h.Header.bfttime + tp)} p >
  69. 2/3 \sum_{(v,p) \in h.Header.NextV} p
  70. \]
  71. *Remark.* To prove the invariant, we will have to prove that the lite client only trusts headers that were correctly generated by Tendermint consensus, then the formula above follows from the Tendermint failure model.
  72. ## High Level Solution
  73. Upon initialization, the lite client is given a header *inithead* it trusts (by
  74. social consensus). It is assumed that *inithead* satisfies the lite client invariant. (If *inithead* has been correctly generated by Tendermint consensus, the invariant follows from the Tendermint Failure Model.)
  75. When a lite clients sees a signed new header *snh*, it has to decide whether to trust the new
  76. header. Trust can be obtained by (possibly) the combination of three methods.
  77. 1. **Uninterrupted sequence of proof.** If a block is appended to the chain, where the last block
  78. is trusted (and properly committed by the old validator set in the next block),
  79. and the new block contains a new validator set, the new block is trusted if the lite client knows all headers in the prefix.
  80. Intuitively, a trusted validator set is assumed to only chose a new validator set that will obey the Tendermint Failure Model.
  81. 2. **Trusting period.** Based on a trusted block *h*, and the lite client
  82. invariant, which ensures the fault assumption during the trusting period, we can check whether at least one validator, that has been continuously correct from *h.Header.bfttime* until now, has signed *snh*.
  83. If this is the case, similarly to above, the chosen validator set in *snh* does not violate the Tendermint Failure Model.
  84. 3. **Bisection.** If a check according to the trusting period fails, the lite client can try to obtain a header *hp* whose height lies between *h* and *snh* in order to check whether *h* can be used to get trust for *hp*, and *hp* can be used to get trust for *snh*. If this is the case we can trust *snh*; if not, we may continue recursively.
  85. ## How to use it
  86. We consider the following use case:
  87. the lite client wants to verify a header for some given height *k*. Thus:
  88. - it requests the signed header for height *k* from a full node
  89. - it tries to verify this header with the methods described here.
  90. This can be used in several settings:
  91. - someone tells the lite client that application data that is relevant for it can be read in the block of height *k*.
  92. - the lite clients wants the latest state. It asks a full nude for the current height, and uses the response for *k*.
  93. ## Details
  94. **Observation 1.** If *h.Header.bfttime + tp > now*, we trust the old
  95. validator set *h.Header.NextV*.
  96. When we say we trust *h.Header.NextV* we do *not* trust that each individual validator in *h.Header.NextV* is correct, but we only trust the fact that at most 1/3 of them are faulty (more precisely, the faulty ones have at most 1/3 of the total voting power).
  97. ### Functions
  98. The function *CanTrust* checks whether to trust header *h2* based on the trusted header *h1*. It does so by (potentially)
  99. building transitive trust relation between *h1* and *h2*, over some intermediate headers. For example, in case we cannot trust
  100. header *h2* based on the trusted header *h1*, the function *CanTrust* will try to find headers such that we can transition trust
  101. from *h1* over intermediate headers to *h2*. We will give two implementations of *CanTrust*, the one based
  102. on bisection that is recursive and the other that is non-recursive. We give two implementations as recursive version might be easier
  103. to understand but non-recursive version might be simpler to formally express and verify using TLA+/TLC.
  104. Both implementations of *CanTrust* function are based on *CheckSupport* function that implements the skipping conditions under which we can trust a
  105. header *h2* given the trust in the header *h1* as a single step,
  106. i.e., it does not assume ensuring transitive trust relation between headers through some intermediate headers.
  107. In order to incentivize correct behavior of validators that run Tendermint consensus protocol, fork detection protocol (it will be explained in different document) is executed in case of a fork (conflicting
  108. headers are detected). As detecting conflicting headers, its propagation through the network (by the gossip protocol) and execution of the fork accountability
  109. protocol on the chain takes time, the lite client logic assumes conservative value for trusted period. More precisely, in the context of lite client we always
  110. operate with a smaller trusted period that we call *lite client trusted period* (LITE_CLIENT_TRUSTED_PERIOD). If we assume that upper bound
  111. for fork detection, propagation and processing on the chain is denoted with *fork procession period* (FORK_PROCESSING_PERIOD), then the following formula
  112. holds:
  113. ```LITE_CLIENT_TRUSTED_PERIOD + FORK_PROCESSING_PERIOD < TRUSTED_PERIOD```, where TRUSTED_PERIOD comes from the Tendermint Failure Model.
  114. *Assumption*: In the following, we assume that *h2.Header.height > h1.Header.height*. We will quickly discuss the other case in the next section.
  115. We consider the following set-up:
  116. - the lite client communicates with one full node
  117. - the lite client locally stores all the headers that has passed basic verification and that are within lite client trust period. In the pseudo code below we write *Store(header)* for this. If a header failed to verify, then
  118. the full node we are talking to is faulty and we should disconnect from it and reinitialise lite client.
  119. - If *CanTrust* returns *error*, then the lite client has seen a forged header or the trusted header has expired (it is outside its trusted period).
  120. * In case of forged header, the full node is faulty so lite client should disconnect and reinitialise with new trusted header.
  121. **Auxiliary Functions.** We will use the function ```votingpower_in(V1,V2)``` to compute the voting power the validators in set V1 have according to their voting power in set V2;
  122. we will write ```totalVotingPower(V)``` for ```votingpower_in(V,V)```, which returns the total voting power in V.
  123. We further use the function ```signers(Commit)``` that returns the set of validators that signed the Commit.
  124. **CheckSupport.** The following function defines skipping condition under the Tendermint Failure model, i.e., it defines when we can trust the header h2 based on header h1.
  125. Time validity of a header is captured by the ```isWithinTrustedPeriodWithin``` function that depends on lite client trusted period (`LITE_CLIENT_TRUSTED_PERIOD`) and it returns
  126. true in case the header is within its lite client trusted period.
  127. ```verify``` function is capturing basic header verification, i.e., it ensures that the header is signed by more than 2/3 of the voting power of the corresponding validator set.
  128. ```go
  129. // return true if header is within its lite client trusted period; otherwise it returns false
  130. func isWithinTrustedPeriod(h) bool {
  131. return h.Header.bfttime + LITE_CLIENT_TRUSTED_PERIOD > now
  132. }
  133. // return true if header is correctly signed by 2/3+ voting power in the corresponding validator set;
  134. // otherwise false. Additional checks should be done in the implementation
  135. // to ensure header is well formed.
  136. func verify(h) bool {
  137. vp_all := totalVotingPower(h.Header.V) // total sum of voting power of validators in h
  138. return votingpower_in(signers(h.Commit),h.Header.V) > 2/3 * vp_all
  139. }
  140. // Captures skipping condition. h1 and h2 has already passed basic validation (function `verify`).
  141. // returns nil in case h2 can be trusted based on h1, otherwise returns error.
  142. // ErrHeaderExpired is used to signal that h1 has expired with respect lite client trusted period,
  143. // ErrInvalidAdjacentHeaders that adjacent headers are not consistent and
  144. // ErrTooMuchChange that there is not enough intersection between validator sets to have skipping condition true.
  145. func CheckSupport(h1,h2,trustlevel) error {
  146. assume h1.Header.Height < h2.header.Height and h1.Header.bfttime < h2.Header.bfttime and h2.Header.bfttime < now
  147. if !isWithinTrustedPeriod(h1) return ErrHeaderNotWithinTrustedPeriod(h1)
  148. // Although while executing the rest of CheckSupport function, h1 can expiry based on the lite client trusted period, this is not problem as
  149. // lite client trusted period is smaller than trusted period of the header based on Tendermint Failure model, i.e., there is a significant
  150. // time period (measure in days) during which validator set that has signed h1 can be trusted
  151. // Furthermore, CheckSupport function is not doing expensive operation (neither rpc nor signature verification), so it should execute fast.
  152. // total sum of voting power of validators in h1.NextV
  153. vp_all := totalVotingPower(h1.Header.NextV)
  154. // check for adjacent headers
  155. if (h2.Header.height == h1.Header.height + 1) {
  156. if h1.Header.NextV == h2.Header.V
  157. return nil
  158. return ErrInvalidAdjacentHeaders
  159. }
  160. // check for non-adjacent headers
  161. if votingpower_in(signers(h2.Commit),h1.Header.NextV) > max(1/3,trustlevel) * vp_all return nil
  162. return ErrTooMuchChange
  163. }
  164. ```
  165. *Correctness arguments*
  166. Towards Lite Client Accuracy:
  167. - Assume by contradiction that *h2* was not generated correctly and the lite client sets trust to true because *CheckSupport* returns true.
  168. - h1 is trusted and sufficiently new
  169. - by Tendermint Fault Model, less than 1/3 of voting power held by faulty validators => at least one correct validator *v* has signed *h2*.
  170. - as *v* is correct up to now, it followed the Tendermint consensus protocol at least up to signing *h2* => *h2* was correctly generated, we arrive at the required contradiction.
  171. Towards Lite Client Completeness:
  172. - The check is successful if sufficiently many validators of *h1* are still validators in *h2* and signed *h2*.
  173. - If *h2.Header.height = h1.Header.height + 1*, and both headers were generated correctly, the test passes
  174. *Verification Condition:* We may need a Tendermint invariant stating that if *h2.Header.height = h1.Header.height + 1* then *signers(h2.Commit) \subseteq h1.Header.NextV*.
  175. *Remark*: The variable *trustlevel* can be used if the user believes that relying on one correct validator is not sufficient. However, in case of (frequent) changes in the validator set, the higher the *trustlevel* is chosen, the more unlikely it becomes that CheckSupport returns true for non-adjacent headers.
  176. **VerifyHeader.** The function *VerifyHeader* captures high level logic, i.e., application call to the lite client module to (optionally download) and
  177. verify header for some height. The core verification logic is captured by *CanTrust* function that iteratively try to establish trust in given header
  178. by relying on *CheckSupport* function.
  179. ```go
  180. func VerifyHeader(height, trustlevel) error {
  181. if h2, exists := Store.Get(height); exists {
  182. if isWithinTrustedPeriod(h2) return nil
  183. return ErrHeaderNotWithinTrustedPeriod(h2)
  184. }
  185. else {
  186. h2 := Commit(height)
  187. if !verify(h2) return ErrInvalidHeader(h2)
  188. if !isWithinTrustedPeriod(h2) return ErrHeaderNotWithinTrustedPeriod(h2)
  189. }
  190. // get the highest trusted headers lower than h2
  191. h1 = Store.HighestTrustedSmallerThan(height)
  192. if h1 == nil
  193. return ErrNoTrustedHeader
  194. err = CanTrust(h1, h2, trustlevel) // or CanTrustBisection((h1, h2, trustlevel)
  195. if err != nil return err
  196. if isWithinTrustedPeriod(h2) {
  197. Store.add(h2) // we store only trusted headers, as we assume that only trusted headers are influencing end user business decisions.
  198. return nil
  199. }
  200. return ErrHeaderNotTrusted(h2)
  201. }
  202. // return nil in case we can trust header h2 based on header h1; otherwise return error where error captures the nature of the error.
  203. func CanTrust(h1,h2,trustlevel) error {
  204. assume h1.Header.Height < h2.header.Height
  205. err = CheckSupport(h1,h2,trustlevel)
  206. if err == nil {
  207. Store.Add(h2)
  208. return nil
  209. }
  210. if err != ErrTooMuchChange return err
  211. // we cannot verify h2 based on h1, so we try to move trusted header closer to h2 so we can verify h2
  212. th := h1 // th is trusted header
  213. untrustedHeaders := []
  214. while true {
  215. endHeight = h2.Header.height
  216. foundPivot = false
  217. while(!foundPivot) {
  218. pivot := (th.Header.height + endHeight) / 2
  219. hp := Commit(pivot)
  220. if !verify(hp) return ErrInvalidHeader(hp)
  221. // try to move trusted header forward to hp
  222. err = CheckSupport(th,hp,trustlevel)
  223. if (err != nil and err != ErrTooMuchChange) return err
  224. if err == nil {
  225. th = hp
  226. Store.Add(hp)
  227. foundPivot = true
  228. }
  229. untrustedHeaders.add(hp)
  230. endHeight = pivot
  231. }
  232. // try to move trusted header forward
  233. for h in untrustedHeaders {
  234. // we assume here that iteration is done in the order of header heights
  235. err = CheckSupport(th,h,trustlevel)
  236. if (err != nil and err != ErrTooMuchChange) return err
  237. if err == nil {
  238. th = h
  239. Store.Add(h)
  240. untrustedHeaders.Remove(h)
  241. }
  242. }
  243. // at this point we have potentially updated th based on stored headers so we try to verify h2
  244. // based on new trusted header
  245. err = CheckSupport(h1,h2,trustlevel)
  246. if err == nil {
  247. Store.Add(h2)
  248. return nil
  249. }
  250. if err != ErrTooMuchChange return err
  251. }
  252. return nil // this line should never be reached
  253. }
  254. ```
  255. ```go
  256. func CanTrustBisection(h1,h2,trustlevel) error {
  257. assume h1.Header.Height < h2.header.Height
  258. err = CheckSupport(h1,h2,trustlevel)
  259. if err == nil {
  260. Store.Add(h2)
  261. return nil
  262. }
  263. if err != ErrTooMuchChange return err
  264. pivot := (h1.Header.height + h2.Header.height) / 2
  265. hp := Commit(pivot)
  266. if !verify(hp) return ErrInvalidHeader(hp)
  267. err = CanTrustBisection(h1,hp,trustlevel)
  268. if err == nil {
  269. Store.Add(hp)
  270. err2 = CanTrustBisection(hp,h2,trustlevel)
  271. if err2 == nil {
  272. Store.Add(h2)
  273. return nil
  274. }
  275. return err2
  276. }
  277. return err
  278. }
  279. ```
  280. *Correctness arguments (sketch)*
  281. Lite Client Accuracy:
  282. - Assume by contradiction that *h2* was not generated correctly and the lite client sets trust to true because CanTrustBisection returns nil.
  283. - CanTrustBisection returns true only if all calls to CheckSupport in the recursion return nil.
  284. - Thus we have a sequence of headers that all satisfied the CheckSupport
  285. - again a contradiction
  286. Lite Client Completeness:
  287. This is only ensured if upon *Commit(pivot)* the lite client is always provided with a correctly generated header.
  288. *Stalling*
  289. With CanTrustBisection, a faulty full node could stall a lite client by creating a long sequence of headers that are queried one-by-one by the lite client and look OK, before the lite client eventually detects a problem. There are several ways to address this:
  290. * Each call to ```Commit``` could be issued to a different full node
  291. * Instead of querying header by header, the lite client tells a full node which header it trusts, and the height of the header it needs. The full node responds with the header along with a proof consisting of intermediate headers that the light client can use to verify. Roughly, Bisection would then be executed at the full node.
  292. * We may set a timeout how long bisection may take.
  293. ### The case *h2.Header.height < h1.Header.height*
  294. In the use case where someone tells the lite client that application data that is relevant for it can be read in the block of height *k* and the lite client trusts a more recent header, we can use the hashes to verify headers "down the chain." That is, we iterate down the heights and check the hashes in each step.
  295. *Remark.* For the case were the lite client trusts two headers *i* and *j* with *i < k < j*, we should discuss/experiment whether the forward or the backward method is more effective.
  296. ```go
  297. func Backwards(h1,h2) error {
  298. assert (h2.Header.height < h1.Header.height)
  299. if !isWithinTrustedPeriod(h1) return ErrHeaderNotTrusted(h1)
  300. old := h1
  301. for i := h1.Header.height - 1; i > h2.Header.height; i-- {
  302. new := Commit(i)
  303. if (hash(new) != old.Header.hash) {
  304. return ErrInvalidAdjacentHeaders
  305. }
  306. old := new
  307. if !isWithinTrustedPeriod(h1) return ErrHeaderNotTrusted(h1)
  308. }
  309. if hash(h2) == old.Header.hash return ErrInvalidAdjacentHeaders
  310. return nil
  311. }
  312. ```