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.

493 lines
20 KiB

  1. -------------------------- MODULE Lightclient_003_draft ----------------------------
  2. (**
  3. * A state-machine specification of the lite client verification,
  4. * following the English spec:
  5. *
  6. * https://github.com/informalsystems/tendermint-rs/blob/master/docs/spec/lightclient/verification.md
  7. *)
  8. EXTENDS Integers, FiniteSets
  9. \* the parameters of Light Client
  10. CONSTANTS
  11. TRUSTED_HEIGHT,
  12. (* an index of the block header that the light client trusts by social consensus *)
  13. TARGET_HEIGHT,
  14. (* an index of the block header that the light client tries to verify *)
  15. TRUSTING_PERIOD,
  16. (* the period within which the validators are trusted *)
  17. CLOCK_DRIFT,
  18. (* the assumed precision of the clock *)
  19. REAL_CLOCK_DRIFT,
  20. (* the actual clock drift, which under normal circumstances should not
  21. be larger than CLOCK_DRIFT (otherwise, there will be a bug) *)
  22. IS_PRIMARY_CORRECT,
  23. (* is primary correct? *)
  24. FAULTY_RATIO
  25. (* a pair <<a, b>> that limits that ratio of faulty validator in the blockchain
  26. from above (exclusive). Tendermint security model prescribes 1 / 3. *)
  27. VARIABLES (* see TypeOK below for the variable types *)
  28. localClock, (* the local clock of the light client *)
  29. state, (* the current state of the light client *)
  30. nextHeight, (* the next height to explore by the light client *)
  31. nprobes (* the lite client iteration, or the number of block tests *)
  32. (* the light store *)
  33. VARIABLES
  34. fetchedLightBlocks, (* a function from heights to LightBlocks *)
  35. lightBlockStatus, (* a function from heights to block statuses *)
  36. latestVerified (* the latest verified block *)
  37. (* the variables of the lite client *)
  38. lcvars == <<localClock, state, nextHeight,
  39. fetchedLightBlocks, lightBlockStatus, latestVerified>>
  40. (* the light client previous state components, used for monitoring *)
  41. VARIABLES
  42. prevVerified,
  43. prevCurrent,
  44. prevLocalClock,
  45. prevVerdict
  46. InitMonitor(verified, current, pLocalClock, verdict) ==
  47. /\ prevVerified = verified
  48. /\ prevCurrent = current
  49. /\ prevLocalClock = pLocalClock
  50. /\ prevVerdict = verdict
  51. NextMonitor(verified, current, pLocalClock, verdict) ==
  52. /\ prevVerified' = verified
  53. /\ prevCurrent' = current
  54. /\ prevLocalClock' = pLocalClock
  55. /\ prevVerdict' = verdict
  56. (******************* Blockchain instance ***********************************)
  57. \* the parameters that are propagated into Blockchain
  58. CONSTANTS
  59. AllNodes
  60. (* a set of all nodes that can act as validators (correct and faulty) *)
  61. \* the state variables of Blockchain, see Blockchain.tla for the details
  62. VARIABLES refClock, blockchain, Faulty
  63. \* All the variables of Blockchain. For some reason, BC!vars does not work
  64. bcvars == <<refClock, blockchain, Faulty>>
  65. (* Create an instance of Blockchain.
  66. We could write EXTENDS Blockchain, but then all the constants and state variables
  67. would be hidden inside the Blockchain module.
  68. *)
  69. ULTIMATE_HEIGHT == TARGET_HEIGHT + 1
  70. BC == INSTANCE Blockchain_003_draft WITH
  71. refClock <- refClock, blockchain <- blockchain, Faulty <- Faulty
  72. (************************** Lite client ************************************)
  73. (* the heights on which the light client is working *)
  74. HEIGHTS == TRUSTED_HEIGHT..TARGET_HEIGHT
  75. (* the control states of the lite client *)
  76. States == { "working", "finishedSuccess", "finishedFailure" }
  77. \* The verification functions are implemented in the API
  78. API == INSTANCE LCVerificationApi_003_draft
  79. (*
  80. Initial states of the light client.
  81. Initially, only the trusted light block is present.
  82. *)
  83. LCInit ==
  84. /\ \E tm \in Int:
  85. tm >= 0 /\ API!IsLocalClockWithinDrift(tm, refClock) /\ localClock = tm
  86. /\ state = "working"
  87. /\ nextHeight = TARGET_HEIGHT
  88. /\ nprobes = 0 \* no tests have been done so far
  89. /\ LET trustedBlock == blockchain[TRUSTED_HEIGHT]
  90. trustedLightBlock == [header |-> trustedBlock, Commits |-> AllNodes]
  91. IN
  92. \* initially, fetchedLightBlocks is a function of one element, i.e., TRUSTED_HEIGHT
  93. /\ fetchedLightBlocks = [h \in {TRUSTED_HEIGHT} |-> trustedLightBlock]
  94. \* initially, lightBlockStatus is a function of one element, i.e., TRUSTED_HEIGHT
  95. /\ lightBlockStatus = [h \in {TRUSTED_HEIGHT} |-> "StateVerified"]
  96. \* the latest verified block the the trusted block
  97. /\ latestVerified = trustedLightBlock
  98. /\ InitMonitor(trustedLightBlock, trustedLightBlock, localClock, "SUCCESS")
  99. \* block should contain a copy of the block from the reference chain, with a matching commit
  100. CopyLightBlockFromChain(block, height) ==
  101. LET ref == blockchain[height]
  102. lastCommit ==
  103. IF height < ULTIMATE_HEIGHT
  104. THEN blockchain[height + 1].lastCommit
  105. \* for the ultimate block, which we never use, as ULTIMATE_HEIGHT = TARGET_HEIGHT + 1
  106. ELSE blockchain[height].VS
  107. IN
  108. block = [header |-> ref, Commits |-> lastCommit]
  109. \* Either the primary is correct and the block comes from the reference chain,
  110. \* or the block is produced by a faulty primary.
  111. \*
  112. \* [LCV-FUNC-FETCH.1::TLA.1]
  113. FetchLightBlockInto(block, height) ==
  114. IF IS_PRIMARY_CORRECT
  115. THEN CopyLightBlockFromChain(block, height)
  116. ELSE BC!IsLightBlockAllowedByDigitalSignatures(height, block)
  117. \* add a block into the light store
  118. \*
  119. \* [LCV-FUNC-UPDATE.1::TLA.1]
  120. LightStoreUpdateBlocks(lightBlocks, block) ==
  121. LET ht == block.header.height IN
  122. [h \in DOMAIN lightBlocks \union {ht} |->
  123. IF h = ht THEN block ELSE lightBlocks[h]]
  124. \* update the state of a light block
  125. \*
  126. \* [LCV-FUNC-UPDATE.1::TLA.1]
  127. LightStoreUpdateStates(statuses, ht, blockState) ==
  128. [h \in DOMAIN statuses \union {ht} |->
  129. IF h = ht THEN blockState ELSE statuses[h]]
  130. \* Check, whether newHeight is a possible next height for the light client.
  131. \*
  132. \* [LCV-FUNC-SCHEDULE.1::TLA.1]
  133. CanScheduleTo(newHeight, pLatestVerified, pNextHeight, pTargetHeight) ==
  134. LET ht == pLatestVerified.header.height IN
  135. \/ /\ ht = pNextHeight
  136. /\ ht < pTargetHeight
  137. /\ pNextHeight < newHeight
  138. /\ newHeight <= pTargetHeight
  139. \/ /\ ht < pNextHeight
  140. /\ ht < pTargetHeight
  141. /\ ht < newHeight
  142. /\ newHeight < pNextHeight
  143. \/ /\ ht = pTargetHeight
  144. /\ newHeight = pTargetHeight
  145. \* The loop of VerifyToTarget.
  146. \*
  147. \* [LCV-FUNC-MAIN.1::TLA-LOOP.1]
  148. VerifyToTargetLoop ==
  149. \* the loop condition is true
  150. /\ latestVerified.header.height < TARGET_HEIGHT
  151. \* pick a light block, which will be constrained later
  152. /\ \E current \in BC!LightBlocks:
  153. \* Get next LightBlock for verification
  154. /\ IF nextHeight \in DOMAIN fetchedLightBlocks
  155. THEN \* copy the block from the light store
  156. /\ current = fetchedLightBlocks[nextHeight]
  157. /\ UNCHANGED fetchedLightBlocks
  158. ELSE \* retrieve a light block and save it in the light store
  159. /\ FetchLightBlockInto(current, nextHeight)
  160. /\ fetchedLightBlocks' = LightStoreUpdateBlocks(fetchedLightBlocks, current)
  161. \* Record that one more probe has been done (for complexity and model checking)
  162. /\ nprobes' = nprobes + 1
  163. \* Verify the current block
  164. /\ LET verdict == API!ValidAndVerified(latestVerified, current, TRUE) IN
  165. NextMonitor(latestVerified, current, localClock, verdict) /\
  166. \* Decide whether/how to continue
  167. CASE verdict = "SUCCESS" ->
  168. /\ lightBlockStatus' = LightStoreUpdateStates(lightBlockStatus, nextHeight, "StateVerified")
  169. /\ latestVerified' = current
  170. /\ state' =
  171. IF latestVerified'.header.height < TARGET_HEIGHT
  172. THEN "working"
  173. ELSE "finishedSuccess"
  174. /\ \E newHeight \in HEIGHTS:
  175. /\ CanScheduleTo(newHeight, current, nextHeight, TARGET_HEIGHT)
  176. /\ nextHeight' = newHeight
  177. [] verdict = "NOT_ENOUGH_TRUST" ->
  178. (*
  179. do nothing: the light block current passed validation, but the validator
  180. set is too different to verify it. We keep the state of
  181. current at StateUnverified. For a later iteration, Schedule
  182. might decide to try verification of that light block again.
  183. *)
  184. /\ lightBlockStatus' = LightStoreUpdateStates(lightBlockStatus, nextHeight, "StateUnverified")
  185. /\ \E newHeight \in HEIGHTS:
  186. /\ CanScheduleTo(newHeight, latestVerified, nextHeight, TARGET_HEIGHT)
  187. /\ nextHeight' = newHeight
  188. /\ UNCHANGED <<latestVerified, state>>
  189. [] OTHER ->
  190. \* verdict is some error code
  191. /\ lightBlockStatus' = LightStoreUpdateStates(lightBlockStatus, nextHeight, "StateFailed")
  192. /\ state' = "finishedFailure"
  193. /\ UNCHANGED <<latestVerified, nextHeight>>
  194. \* The terminating condition of VerifyToTarget.
  195. \*
  196. \* [LCV-FUNC-MAIN.1::TLA-LOOPCOND.1]
  197. VerifyToTargetDone ==
  198. /\ latestVerified.header.height >= TARGET_HEIGHT
  199. /\ state' = "finishedSuccess"
  200. /\ UNCHANGED <<nextHeight, nprobes, fetchedLightBlocks, lightBlockStatus, latestVerified>>
  201. /\ UNCHANGED <<prevVerified, prevCurrent, prevLocalClock, prevVerdict>>
  202. (*
  203. The local and global clocks can be updated. They can also drift from each other.
  204. Note that the local clock can actually go backwards in time.
  205. However, it still stays in the drift envelope
  206. of [refClock - REAL_CLOCK_DRIFT, refClock + REAL_CLOCK_DRIFT].
  207. *)
  208. AdvanceClocks ==
  209. /\ BC!AdvanceTime
  210. /\ \E tm \in Int:
  211. /\ tm >= 0
  212. /\ API!IsLocalClockWithinDrift(tm, refClock')
  213. /\ localClock' = tm
  214. \* if you like the clock to always grow monotonically, uncomment the next line:
  215. \*/\ localClock' > localClock
  216. (********************* Lite client + Blockchain *******************)
  217. Init ==
  218. \* the blockchain is initialized immediately to the ULTIMATE_HEIGHT
  219. /\ BC!InitToHeight(FAULTY_RATIO)
  220. \* the light client starts
  221. /\ LCInit
  222. (*
  223. The system step is very simple.
  224. The light client is either executing VerifyToTarget, or it has terminated.
  225. (In the latter case, a model checker reports a deadlock.)
  226. Simultaneously, the global clock may advance.
  227. *)
  228. Next ==
  229. /\ state = "working"
  230. /\ VerifyToTargetLoop \/ VerifyToTargetDone
  231. /\ AdvanceClocks
  232. (************************* Types ******************************************)
  233. TypeOK ==
  234. /\ state \in States
  235. /\ localClock \in Nat
  236. /\ refClock \in Nat
  237. /\ nextHeight \in HEIGHTS
  238. /\ latestVerified \in BC!LightBlocks
  239. /\ \E HS \in SUBSET HEIGHTS:
  240. /\ fetchedLightBlocks \in [HS -> BC!LightBlocks]
  241. /\ lightBlockStatus
  242. \in [HS -> {"StateVerified", "StateUnverified", "StateFailed"}]
  243. (************************* Properties ******************************************)
  244. (* The properties to check *)
  245. \* this invariant candidate is false
  246. NeverFinish ==
  247. state = "working"
  248. \* this invariant candidate is false
  249. NeverFinishNegative ==
  250. state /= "finishedFailure"
  251. \* This invariant holds true, when the primary is correct.
  252. \* This invariant candidate is false when the primary is faulty.
  253. NeverFinishNegativeWhenTrusted ==
  254. BC!InTrustingPeriod(blockchain[TRUSTED_HEIGHT])
  255. => state /= "finishedFailure"
  256. \* this invariant candidate is false
  257. NeverFinishPositive ==
  258. state /= "finishedSuccess"
  259. (**
  260. Check that the target height has been reached upon successful termination.
  261. *)
  262. TargetHeightOnSuccessInv ==
  263. state = "finishedSuccess" =>
  264. /\ TARGET_HEIGHT \in DOMAIN fetchedLightBlocks
  265. /\ lightBlockStatus[TARGET_HEIGHT] = "StateVerified"
  266. (**
  267. Correctness states that all the obtained headers are exactly like in the blockchain.
  268. It is always the case that every verified header in LightStore was generated by
  269. an instance of Tendermint consensus.
  270. [LCV-DIST-SAFE.1::CORRECTNESS-INV.1]
  271. *)
  272. CorrectnessInv ==
  273. \A h \in DOMAIN fetchedLightBlocks:
  274. lightBlockStatus[h] = "StateVerified" =>
  275. fetchedLightBlocks[h].header = blockchain[h]
  276. (**
  277. No faulty block was used to construct a proof. This invariant holds,
  278. only if FAULTY_RATIO < 1/3.
  279. *)
  280. NoTrustOnFaultyBlockInv ==
  281. (state = "finishedSuccess"
  282. /\ fetchedLightBlocks[TARGET_HEIGHT].header = blockchain[TARGET_HEIGHT])
  283. => CorrectnessInv
  284. (**
  285. Check that the sequence of the headers in storedLightBlocks satisfies ValidAndVerified = "SUCCESS" pairwise
  286. This property is easily violated, whenever a header cannot be trusted anymore.
  287. *)
  288. StoredHeadersAreVerifiedInv ==
  289. state = "finishedSuccess"
  290. =>
  291. \A lh, rh \in DOMAIN fetchedLightBlocks: \* for every pair of different stored headers
  292. \/ lh >= rh
  293. \* either there is a header between them
  294. \/ \E mh \in DOMAIN fetchedLightBlocks:
  295. lh < mh /\ mh < rh
  296. \* or we can verify the right one using the left one
  297. \/ "SUCCESS" = API!ValidAndVerified(fetchedLightBlocks[lh],
  298. fetchedLightBlocks[rh], FALSE)
  299. \* An improved version of StoredHeadersAreVerifiedInv,
  300. \* assuming that a header may be not trusted.
  301. \* This invariant candidate is also violated,
  302. \* as there may be some unverified blocks left in the middle.
  303. \* This property is violated under two conditions:
  304. \* (1) the primary is faulty and there are at least 4 blocks,
  305. \* (2) the primary is correct and there are at least 5 blocks.
  306. StoredHeadersAreVerifiedOrNotTrustedInv ==
  307. state = "finishedSuccess"
  308. =>
  309. \A lh, rh \in DOMAIN fetchedLightBlocks: \* for every pair of different stored headers
  310. \/ lh >= rh
  311. \* either there is a header between them
  312. \/ \E mh \in DOMAIN fetchedLightBlocks:
  313. lh < mh /\ mh < rh
  314. \* or we can verify the right one using the left one
  315. \/ "SUCCESS" = API!ValidAndVerified(fetchedLightBlocks[lh],
  316. fetchedLightBlocks[rh], FALSE)
  317. \* or the left header is outside the trusting period, so no guarantees
  318. \/ ~API!InTrustingPeriodLocal(fetchedLightBlocks[lh].header)
  319. (**
  320. * An improved version of StoredHeadersAreSoundOrNotTrusted,
  321. * checking the property only for the verified headers.
  322. * This invariant holds true if CLOCK_DRIFT <= REAL_CLOCK_DRIFT.
  323. *)
  324. ProofOfChainOfTrustInv ==
  325. state = "finishedSuccess"
  326. =>
  327. \A lh, rh \in DOMAIN fetchedLightBlocks:
  328. \* for every pair of stored headers that have been verified
  329. \/ lh >= rh
  330. \/ lightBlockStatus[lh] = "StateUnverified"
  331. \/ lightBlockStatus[rh] = "StateUnverified"
  332. \* either there is a header between them
  333. \/ \E mh \in DOMAIN fetchedLightBlocks:
  334. lh < mh /\ mh < rh /\ lightBlockStatus[mh] = "StateVerified"
  335. \* or the left header is outside the trusting period, so no guarantees
  336. \/ ~(API!InTrustingPeriodLocal(fetchedLightBlocks[lh].header))
  337. \* or we can verify the right one using the left one
  338. \/ "SUCCESS" = API!ValidAndVerified(fetchedLightBlocks[lh],
  339. fetchedLightBlocks[rh], FALSE)
  340. (**
  341. * When the light client terminates, there are no failed blocks. (Otherwise, someone lied to us.)
  342. *)
  343. NoFailedBlocksOnSuccessInv ==
  344. state = "finishedSuccess" =>
  345. \A h \in DOMAIN fetchedLightBlocks:
  346. lightBlockStatus[h] /= "StateFailed"
  347. \* This property states that whenever the light client finishes with a positive outcome,
  348. \* the trusted header is still within the trusting period.
  349. \* We expect this property to be violated. And Apalache shows us a counterexample.
  350. PositiveBeforeTrustedHeaderExpires ==
  351. (state = "finishedSuccess") =>
  352. BC!InTrustingPeriod(blockchain[TRUSTED_HEIGHT])
  353. \* If the primary is correct and the initial trusted block has not expired,
  354. \* then whenever the algorithm terminates, it reports "success".
  355. \* This property fails.
  356. CorrectPrimaryAndTimeliness ==
  357. (BC!InTrustingPeriod(blockchain[TRUSTED_HEIGHT])
  358. /\ state /= "working" /\ IS_PRIMARY_CORRECT) =>
  359. state = "finishedSuccess"
  360. (**
  361. If the primary is correct and there is a trusted block that has not expired,
  362. then whenever the algorithm terminates, it reports "success".
  363. This property only holds true, if the local clock is always growing monotonically.
  364. If the local clock can go backwards in the envelope
  365. [refClock - CLOCK_DRIFT, refClock + CLOCK_DRIFT], then the property fails.
  366. [LCV-DIST-LIVE.1::SUCCESS-CORR-PRIMARY-CHAIN-OF-TRUST.1]
  367. *)
  368. SuccessOnCorrectPrimaryAndChainOfTrustLocal ==
  369. (\E h \in DOMAIN fetchedLightBlocks:
  370. /\ lightBlockStatus[h] = "StateVerified"
  371. /\ API!InTrustingPeriodLocal(blockchain[h])
  372. /\ state /= "working" /\ IS_PRIMARY_CORRECT) =>
  373. state = "finishedSuccess"
  374. (**
  375. Similar to SuccessOnCorrectPrimaryAndChainOfTrust, but using the blockchain clock.
  376. It fails because the local clock of the client drifted away, so it rejects a block
  377. that has not expired yet (according to the local clock).
  378. *)
  379. SuccessOnCorrectPrimaryAndChainOfTrustGlobal ==
  380. (\E h \in DOMAIN fetchedLightBlocks:
  381. lightBlockStatus[h] = "StateVerified" /\ BC!InTrustingPeriod(blockchain[h])
  382. /\ state /= "working" /\ IS_PRIMARY_CORRECT) =>
  383. state = "finishedSuccess"
  384. \* Lite Client Completeness: If header h was correctly generated by an instance
  385. \* of Tendermint consensus (and its age is less than the trusting period),
  386. \* then the lite client should eventually set trust(h) to true.
  387. \*
  388. \* Note that Completeness assumes that the lite client communicates with a correct full node.
  389. \*
  390. \* We decompose completeness into Termination (liveness) and Precision (safety).
  391. \* Once again, Precision is an inverse version of the safety property in Completeness,
  392. \* as A => B is logically equivalent to ~B => ~A.
  393. \*
  394. \* This property holds only when CLOCK_DRIFT = 0 and REAL_CLOCK_DRIFT = 0.
  395. PrecisionInv ==
  396. (state = "finishedFailure")
  397. => \/ ~BC!InTrustingPeriod(blockchain[TRUSTED_HEIGHT]) \* outside of the trusting period
  398. \/ \E h \in DOMAIN fetchedLightBlocks:
  399. LET lightBlock == fetchedLightBlocks[h] IN
  400. \* the full node lied to the lite client about the block header
  401. \/ lightBlock.header /= blockchain[h]
  402. \* the full node lied to the lite client about the commits
  403. \/ lightBlock.Commits /= lightBlock.header.VS
  404. \* the old invariant that was found to be buggy by TLC
  405. PrecisionBuggyInv ==
  406. (state = "finishedFailure")
  407. => \/ ~BC!InTrustingPeriod(blockchain[TRUSTED_HEIGHT]) \* outside of the trusting period
  408. \/ \E h \in DOMAIN fetchedLightBlocks:
  409. LET lightBlock == fetchedLightBlocks[h] IN
  410. \* the full node lied to the lite client about the block header
  411. lightBlock.header /= blockchain[h]
  412. \* the worst complexity
  413. Complexity ==
  414. LET N == TARGET_HEIGHT - TRUSTED_HEIGHT + 1 IN
  415. state /= "working" =>
  416. (2 * nprobes <= N * (N - 1))
  417. (**
  418. If the light client has terminated, then the expected postcondition holds true.
  419. *)
  420. ApiPostInv ==
  421. state /= "working" =>
  422. API!VerifyToTargetPost(blockchain, IS_PRIMARY_CORRECT,
  423. fetchedLightBlocks, lightBlockStatus,
  424. TRUSTED_HEIGHT, TARGET_HEIGHT, state)
  425. (*
  426. We omit termination, as the algorithm deadlocks in the end.
  427. So termination can be demonstrated by finding a deadlock.
  428. Of course, one has to analyze the deadlocked state and see that
  429. the algorithm has indeed terminated there.
  430. *)
  431. =============================================================================
  432. \* Modification History
  433. \* Last modified Fri Jun 26 12:08:28 CEST 2020 by igor
  434. \* Created Wed Oct 02 16:39:42 CEST 2019 by igor