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.

440 lines
18 KiB

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