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.

465 lines
18 KiB

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