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.

476 lines
19 KiB

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