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.

373 lines
16 KiB

  1. -------------------------- MODULE LCDetector_003_draft -----------------------------
  2. (**
  3. * This is a specification of the light client detector module.
  4. * It follows the English specification:
  5. *
  6. * https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/detection/detection_003_reviewed.md
  7. *
  8. * The assumptions made in this specification:
  9. *
  10. * - light client connects to one primary and one secondary peer
  11. *
  12. * - the light client has its own local clock that can drift from the reference clock
  13. * within the envelope [refClock - CLOCK_DRIFT, refClock + CLOCK_DRIFT].
  14. * The local clock may increase as well as decrease in the the envelope
  15. * (similar to clock synchronization).
  16. *
  17. * - the ratio of the faulty validators is set as the parameter.
  18. *
  19. * Igor Konnov, Josef Widder, 2020
  20. *)
  21. EXTENDS Integers
  22. \* the parameters of Light Client
  23. CONSTANTS
  24. AllNodes,
  25. (* a set of all nodes that can act as validators (correct and faulty) *)
  26. TRUSTED_HEIGHT,
  27. (* an index of the block header that the light client trusts by social consensus *)
  28. TARGET_HEIGHT,
  29. (* an index of the block header that the light client tries to verify *)
  30. TRUSTING_PERIOD,
  31. (* the period within which the validators are trusted *)
  32. CLOCK_DRIFT,
  33. (* the assumed precision of the clock *)
  34. REAL_CLOCK_DRIFT,
  35. (* the actual clock drift, which under normal circumstances should not
  36. be larger than CLOCK_DRIFT (otherwise, there will be a bug) *)
  37. FAULTY_RATIO,
  38. (* a pair <<a, b>> that limits that ratio of faulty validator in the blockchain
  39. from above (exclusive). Tendermint security model prescribes 1 / 3. *)
  40. IS_PRIMARY_CORRECT,
  41. IS_SECONDARY_CORRECT
  42. VARIABLES
  43. blockchain, (* the reference blockchain *)
  44. localClock, (* the local clock of the light client *)
  45. refClock, (* the reference clock in the reference blockchain *)
  46. Faulty, (* the set of faulty validators *)
  47. state, (* the state of the light client detector *)
  48. fetchedLightBlocks1, (* a function from heights to LightBlocks *)
  49. fetchedLightBlocks2, (* a function from heights to LightBlocks *)
  50. fetchedLightBlocks1b, (* a function from heights to LightBlocks *)
  51. commonHeight, (* the height that is trusted in CreateEvidenceForPeer *)
  52. nextHeightToTry, (* the index in CreateEvidenceForPeer *)
  53. evidences (* a set of evidences *)
  54. vars == <<state, blockchain, localClock, refClock, Faulty,
  55. fetchedLightBlocks1, fetchedLightBlocks2, fetchedLightBlocks1b,
  56. commonHeight, nextHeightToTry, evidences >>
  57. \* (old) type annotations in Apalache
  58. a <: b == a
  59. \* instantiate a reference chain
  60. ULTIMATE_HEIGHT == TARGET_HEIGHT + 1
  61. BC == INSTANCE Blockchain_003_draft
  62. WITH ULTIMATE_HEIGHT <- (TARGET_HEIGHT + 1)
  63. \* use the light client API
  64. LC == INSTANCE LCVerificationApi_003_draft
  65. \* evidence type
  66. ET == [peer |-> STRING, conflictingBlock |-> BC!LBT, commonHeight |-> Int]
  67. \* is the algorithm in the terminating state
  68. IsTerminated ==
  69. state \in { <<"NoEvidence", "PRIMARY">>,
  70. <<"NoEvidence", "SECONDARY">>,
  71. <<"FaultyPeer", "PRIMARY">>,
  72. <<"FaultyPeer", "SECONDARY">>,
  73. <<"FoundEvidence", "PRIMARY">> }
  74. (********************************* Initialization ******************************)
  75. \* initialization for the light blocks data structure
  76. InitLightBlocks(lb, Heights) ==
  77. \* BC!LightBlocks is an infinite set, as time is not restricted.
  78. \* Hence, we initialize the light blocks by picking the sets inside.
  79. \E vs, nextVS, lastCommit, commit \in [Heights -> SUBSET AllNodes]:
  80. \* although [Heights -> Int] is an infinite set,
  81. \* Apalache needs just one instance of this set, so it does not complain.
  82. \E timestamp \in [Heights -> Int]:
  83. LET hdr(h) ==
  84. [height |-> h,
  85. time |-> timestamp[h],
  86. VS |-> vs[h],
  87. NextVS |-> nextVS[h],
  88. lastCommit |-> lastCommit[h]]
  89. IN
  90. LET lightHdr(h) ==
  91. [header |-> hdr(h), Commits |-> commit[h]]
  92. IN
  93. lb = [ h \in Heights |-> lightHdr(h) ]
  94. \* initialize the detector algorithm
  95. Init ==
  96. \* initialize the blockchain to TARGET_HEIGHT + 1
  97. /\ BC!InitToHeight(FAULTY_RATIO)
  98. /\ \E tm \in Int:
  99. tm >= 0 /\ LC!IsLocalClockWithinDrift(tm, refClock) /\ localClock = tm
  100. \* start with the secondary looking for evidence
  101. /\ state = <<"Init", "SECONDARY">> /\ commonHeight = 0 /\ nextHeightToTry = 0
  102. /\ evidences = {} <: {ET}
  103. \* Precompute a possible result of light client verification for the primary.
  104. \* It is the input to the detection algorithm.
  105. /\ \E Heights1 \in SUBSET(TRUSTED_HEIGHT..TARGET_HEIGHT):
  106. /\ TRUSTED_HEIGHT \in Heights1
  107. /\ TARGET_HEIGHT \in Heights1
  108. /\ InitLightBlocks(fetchedLightBlocks1, Heights1)
  109. \* As we have a non-deterministic scheduler, for every trace that has
  110. \* an unverified block, there is a filtered trace that only has verified
  111. \* blocks. This is a deep observation.
  112. /\ LET status == [h \in Heights1 |-> "StateVerified"] IN
  113. LC!VerifyToTargetPost(blockchain, IS_PRIMARY_CORRECT,
  114. fetchedLightBlocks1, status,
  115. TRUSTED_HEIGHT, TARGET_HEIGHT, "finishedSuccess")
  116. \* initialize the other data structures to the default values
  117. /\ LET trustedBlock == blockchain[TRUSTED_HEIGHT]
  118. trustedLightBlock == [header |-> trustedBlock, Commits |-> AllNodes]
  119. IN
  120. /\ fetchedLightBlocks2 = [h \in {TRUSTED_HEIGHT} |-> trustedLightBlock]
  121. /\ fetchedLightBlocks1b = [h \in {TRUSTED_HEIGHT} |-> trustedLightBlock]
  122. (********************************* Transitions ******************************)
  123. \* a block should contain a copy of the block from the reference chain,
  124. \* with a matching commit
  125. CopyLightBlockFromChain(block, height) ==
  126. LET ref == blockchain[height]
  127. lastCommit ==
  128. IF height < ULTIMATE_HEIGHT
  129. THEN blockchain[height + 1].lastCommit
  130. \* for the ultimate block, which we never use,
  131. \* as ULTIMATE_HEIGHT = TARGET_HEIGHT + 1
  132. ELSE blockchain[height].VS
  133. IN
  134. block = [header |-> ref, Commits |-> lastCommit]
  135. \* Either the primary is correct and the block comes from the reference chain,
  136. \* or the block is produced by a faulty primary.
  137. \*
  138. \* [LCV-FUNC-FETCH.1::TLA.1]
  139. FetchLightBlockInto(isPeerCorrect, block, height) ==
  140. IF isPeerCorrect
  141. THEN CopyLightBlockFromChain(block, height)
  142. ELSE BC!IsLightBlockAllowedByDigitalSignatures(height, block)
  143. (**
  144. * Pick the next height, for which there is a block.
  145. *)
  146. PickNextHeight(fetchedBlocks, height) ==
  147. LET largerHeights == { h \in DOMAIN fetchedBlocks: h > height } IN
  148. IF largerHeights = ({} <: {Int})
  149. THEN -1
  150. ELSE CHOOSE h \in largerHeights:
  151. \A h2 \in largerHeights: h <= h2
  152. (**
  153. * Check, whether the target header matches at the secondary and primary.
  154. *)
  155. CompareLast ==
  156. /\ state = <<"Init", "SECONDARY">>
  157. \* fetch a block from the secondary:
  158. \* non-deterministically pick a block that matches the constraints
  159. /\ \E latest \in BC!LightBlocks:
  160. \* for the moment, we ignore the possibility of a timeout when fetching a block
  161. /\ FetchLightBlockInto(IS_SECONDARY_CORRECT, latest, TARGET_HEIGHT)
  162. /\ IF latest.header = fetchedLightBlocks1[TARGET_HEIGHT].header
  163. THEN \* if the headers match, CreateEvidence is not called
  164. /\ state' = <<"NoEvidence", "SECONDARY">>
  165. \* save the retrieved block for further analysis
  166. /\ fetchedLightBlocks2' =
  167. [h \in (DOMAIN fetchedLightBlocks2) \union {TARGET_HEIGHT} |->
  168. IF h = TARGET_HEIGHT THEN latest ELSE fetchedLightBlocks2[h]]
  169. /\ UNCHANGED <<commonHeight, nextHeightToTry>>
  170. ELSE \* prepare the parameters for CreateEvidence
  171. /\ commonHeight' = TRUSTED_HEIGHT
  172. /\ nextHeightToTry' = PickNextHeight(fetchedLightBlocks1, TRUSTED_HEIGHT)
  173. /\ state' = IF nextHeightToTry' >= 0
  174. THEN <<"CreateEvidence", "SECONDARY">>
  175. ELSE <<"FaultyPeer", "SECONDARY">>
  176. /\ UNCHANGED fetchedLightBlocks2
  177. /\ UNCHANGED <<blockchain, Faulty,
  178. fetchedLightBlocks1, fetchedLightBlocks1b, evidences>>
  179. \* the actual loop in CreateEvidence
  180. CreateEvidence(peer, isPeerCorrect, refBlocks, targetBlocks) ==
  181. /\ state = <<"CreateEvidence", peer>>
  182. \* precompute a possible result of light client verification for the secondary
  183. \* we have to introduce HeightRange, because Apalache can only handle a..b
  184. \* for constant a and b
  185. /\ LET HeightRange == { h \in TRUSTED_HEIGHT..TARGET_HEIGHT:
  186. commonHeight <= h /\ h <= nextHeightToTry } IN
  187. \E HeightsRange \in SUBSET(HeightRange):
  188. /\ commonHeight \in HeightsRange /\ nextHeightToTry \in HeightsRange
  189. /\ InitLightBlocks(targetBlocks, HeightsRange)
  190. \* As we have a non-deterministic scheduler, for every trace that has
  191. \* an unverified block, there is a filtered trace that only has verified
  192. \* blocks. This is a deep observation.
  193. /\ \E result \in {"finishedSuccess", "finishedFailure"}:
  194. LET targetStatus == [h \in HeightsRange |-> "StateVerified"] IN
  195. \* call VerifyToTarget for (commonHeight, nextHeightToTry).
  196. /\ LC!VerifyToTargetPost(blockchain, isPeerCorrect,
  197. targetBlocks, targetStatus,
  198. commonHeight, nextHeightToTry, result)
  199. \* case 1: the peer has failed (or the trusting period has expired)
  200. /\ \/ /\ result /= "finishedSuccess"
  201. /\ state' = <<"FaultyPeer", peer>>
  202. /\ UNCHANGED <<commonHeight, nextHeightToTry, evidences>>
  203. \* case 2: success
  204. \/ /\ result = "finishedSuccess"
  205. /\ LET block1 == refBlocks[nextHeightToTry] IN
  206. LET block2 == targetBlocks[nextHeightToTry] IN
  207. IF block1.header /= block2.header
  208. THEN \* the target blocks do not match
  209. /\ state' = <<"FoundEvidence", peer>>
  210. /\ evidences' = evidences \union
  211. {[peer |-> peer,
  212. conflictingBlock |-> block1,
  213. commonHeight |-> commonHeight]}
  214. /\ UNCHANGED <<commonHeight, nextHeightToTry>>
  215. ELSE \* the target blocks match
  216. /\ nextHeightToTry' = PickNextHeight(refBlocks, nextHeightToTry)
  217. /\ commonHeight' = nextHeightToTry
  218. /\ state' = IF nextHeightToTry' >= 0
  219. THEN state
  220. ELSE <<"NoEvidence", peer>>
  221. /\ UNCHANGED evidences
  222. SwitchToPrimary ==
  223. /\ state = <<"FoundEvidence", "SECONDARY">>
  224. /\ nextHeightToTry' = PickNextHeight(fetchedLightBlocks2, commonHeight)
  225. /\ state' = <<"CreateEvidence", "PRIMARY">>
  226. /\ UNCHANGED <<blockchain, refClock, Faulty, localClock,
  227. fetchedLightBlocks1, fetchedLightBlocks2, fetchedLightBlocks1b,
  228. commonHeight, evidences >>
  229. CreateEvidenceForSecondary ==
  230. /\ CreateEvidence("SECONDARY", IS_SECONDARY_CORRECT,
  231. fetchedLightBlocks1, fetchedLightBlocks2')
  232. /\ UNCHANGED <<blockchain, refClock, Faulty, localClock,
  233. fetchedLightBlocks1, fetchedLightBlocks1b>>
  234. CreateEvidenceForPrimary ==
  235. /\ CreateEvidence("PRIMARY", IS_PRIMARY_CORRECT,
  236. fetchedLightBlocks2,
  237. fetchedLightBlocks1b')
  238. /\ UNCHANGED <<blockchain, Faulty,
  239. fetchedLightBlocks1, fetchedLightBlocks2>>
  240. (*
  241. The local and global clocks can be updated. They can also drift from each other.
  242. Note that the local clock can actually go backwards in time.
  243. However, it still stays in the drift envelope
  244. of [refClock - REAL_CLOCK_DRIFT, refClock + REAL_CLOCK_DRIFT].
  245. *)
  246. AdvanceClocks ==
  247. /\ \E tm \in Int:
  248. tm >= refClock /\ refClock' = tm
  249. /\ \E tm \in Int:
  250. /\ tm >= localClock
  251. /\ LC!IsLocalClockWithinDrift(tm, refClock')
  252. /\ localClock' = tm
  253. (**
  254. Execute AttackDetector for one secondary.
  255. [LCD-FUNC-DETECTOR.2::LOOP.1]
  256. *)
  257. Next ==
  258. /\ AdvanceClocks
  259. /\ \/ CompareLast
  260. \/ CreateEvidenceForSecondary
  261. \/ SwitchToPrimary
  262. \/ CreateEvidenceForPrimary
  263. \* simple invariants to see the progress of the detector
  264. NeverNoEvidence == state[1] /= "NoEvidence"
  265. NeverFoundEvidence == state[1] /= "FoundEvidence"
  266. NeverFaultyPeer == state[1] /= "FaultyPeer"
  267. NeverCreateEvidence == state[1] /= "CreateEvidence"
  268. NeverFoundEvidencePrimary == state /= <<"FoundEvidence", "PRIMARY">>
  269. NeverReachTargetHeight == nextHeightToTry < TARGET_HEIGHT
  270. EvidenceWhenFaultyInv ==
  271. (state[1] = "FoundEvidence") => (~IS_PRIMARY_CORRECT \/ ~IS_SECONDARY_CORRECT)
  272. NoEvidenceForCorrectInv ==
  273. IS_PRIMARY_CORRECT /\ IS_SECONDARY_CORRECT => evidences = {} <: {ET}
  274. (**
  275. * If we find an evidence by peer A, peer B has ineded given us a corrupted
  276. * header following the common height. Also, we have a verification trace by peer A.
  277. *)
  278. CommonHeightOnEvidenceInv ==
  279. \A e \in evidences:
  280. LET conflicting == e.conflictingBlock IN
  281. LET conflictingHeader == conflicting.header IN
  282. \* the evidence by suspectingPeer can be verified by suspectingPeer in one step
  283. LET SoundEvidence(suspectingPeer, peerBlocks) ==
  284. \/ e.peer /= suspectingPeer
  285. \* the conflicting block from another peer verifies against the common height
  286. \/ /\ "SUCCESS" =
  287. LC!ValidAndVerifiedUntimed(peerBlocks[e.commonHeight], conflicting)
  288. \* and the headers of the same height by the two peers do not match
  289. /\ peerBlocks[conflictingHeader.height].header /= conflictingHeader
  290. IN
  291. /\ SoundEvidence("PRIMARY", fetchedLightBlocks1b)
  292. /\ SoundEvidence("SECONDARY", fetchedLightBlocks2)
  293. (**
  294. * If the light client does not find an evidence,
  295. * then there is no attack on the light client.
  296. *)
  297. AccuracyInv ==
  298. (LC!InTrustingPeriodLocal(fetchedLightBlocks1[TARGET_HEIGHT].header)
  299. /\ state = <<"NoEvidence", "SECONDARY">>)
  300. =>
  301. (fetchedLightBlocks1[TARGET_HEIGHT].header = blockchain[TARGET_HEIGHT]
  302. /\ fetchedLightBlocks2[TARGET_HEIGHT].header = blockchain[TARGET_HEIGHT])
  303. (**
  304. * The primary reports a corrupted block at the target height. If the secondary is
  305. * correct and the algorithm has terminated, we should get the evidence.
  306. * This property is violated due to clock drift. VerifyToTarget may fail with
  307. * the correct secondary within the trusting period (due to clock drift, locally
  308. * we think that we are outside of the trusting period).
  309. *)
  310. PrecisionInvGrayZone ==
  311. (/\ fetchedLightBlocks1[TARGET_HEIGHT].header /= blockchain[TARGET_HEIGHT]
  312. /\ BC!InTrustingPeriod(blockchain[TRUSTED_HEIGHT])
  313. /\ IS_SECONDARY_CORRECT
  314. /\ IsTerminated)
  315. =>
  316. evidences /= {} <: {ET}
  317. (**
  318. * The primary reports a corrupted block at the target height. If the secondary is
  319. * correct and the algorithm has terminated, we should get the evidence.
  320. * This invariant does not fail, as we are using the local clock to check the trusting
  321. * period.
  322. *)
  323. PrecisionInvLocal ==
  324. (/\ fetchedLightBlocks1[TARGET_HEIGHT].header /= blockchain[TARGET_HEIGHT]
  325. /\ LC!InTrustingPeriodLocalSurely(blockchain[TRUSTED_HEIGHT])
  326. /\ IS_SECONDARY_CORRECT
  327. /\ IsTerminated)
  328. =>
  329. evidences /= {} <: {ET}
  330. ====================================================================================