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.

166 lines
6.7 KiB

  1. ------------------------ MODULE Blockchain_003_draft -----------------------------
  2. (*
  3. This is a high-level specification of Tendermint blockchain
  4. that is designed specifically for the light client.
  5. Validators have the voting power of one. If you like to model various
  6. voting powers, introduce multiple copies of the same validator
  7. (do not forget to give them unique names though).
  8. *)
  9. EXTENDS Integers, FiniteSets, Apalache
  10. Min(a, b) == IF a < b THEN a ELSE b
  11. CONSTANT
  12. AllNodes,
  13. (* a set of all nodes that can act as validators (correct and faulty) *)
  14. ULTIMATE_HEIGHT,
  15. (* a maximal height that can be ever reached (modelling artifact) *)
  16. TRUSTING_PERIOD
  17. (* the period within which the validators are trusted *)
  18. Heights == 1..ULTIMATE_HEIGHT (* possible heights *)
  19. (* A commit is just a set of nodes who have committed the block *)
  20. Commits == SUBSET AllNodes
  21. (* The set of all block headers that can be on the blockchain.
  22. This is a simplified version of the Block data structure in the actual implementation. *)
  23. BlockHeaders == [
  24. height: Heights,
  25. \* the block height
  26. time: Int,
  27. \* the block timestamp in some integer units
  28. lastCommit: Commits,
  29. \* the nodes who have voted on the previous block, the set itself instead of a hash
  30. (* in the implementation, only the hashes of V and NextV are stored in a block,
  31. as V and NextV are stored in the application state *)
  32. VS: SUBSET AllNodes,
  33. \* the validators of this bloc. We store the validators instead of the hash.
  34. NextVS: SUBSET AllNodes
  35. \* the validators of the next block. We store the next validators instead of the hash.
  36. ]
  37. (* A signed header is just a header together with a set of commits *)
  38. LightBlocks == [header: BlockHeaders, Commits: Commits]
  39. VARIABLES
  40. refClock,
  41. (* the current global time in integer units as perceived by the reference chain *)
  42. blockchain,
  43. (* A sequence of BlockHeaders, which gives us a bird view of the blockchain. *)
  44. Faulty
  45. (* A set of faulty nodes, which can act as validators. We assume that the set
  46. of faulty processes is non-decreasing. If a process has recovered, it should
  47. connect using a different id. *)
  48. (* all variables, to be used with UNCHANGED *)
  49. vars == <<refClock, blockchain, Faulty>>
  50. (* The set of all correct nodes in a state *)
  51. Corr == AllNodes \ Faulty
  52. (* APALACHE annotations *)
  53. a <: b == a \* type annotation
  54. NT == STRING
  55. NodeSet(S) == S <: {NT}
  56. EmptyNodeSet == NodeSet({})
  57. BT == [height |-> Int, time |-> Int, lastCommit |-> {NT}, VS |-> {NT}, NextVS |-> {NT}]
  58. LBT == [header |-> BT, Commits |-> {NT}]
  59. (* end of APALACHE annotations *)
  60. (****************************** BLOCKCHAIN ************************************)
  61. (* the header is still within the trusting period *)
  62. InTrustingPeriod(header) ==
  63. refClock < header.time + TRUSTING_PERIOD
  64. (*
  65. Given a function pVotingPower \in D -> Powers for some D \subseteq AllNodes
  66. and pNodes \subseteq D, test whether the set pNodes \subseteq AllNodes has
  67. more than 2/3 of voting power among the nodes in D.
  68. *)
  69. TwoThirds(pVS, pNodes) ==
  70. LET TP == Cardinality(pVS)
  71. SP == Cardinality(pVS \intersect pNodes)
  72. IN
  73. 3 * SP > 2 * TP \* when thinking in real numbers, not integers: SP > 2.0 / 3.0 * TP
  74. (*
  75. Given a set of FaultyNodes, test whether the voting power of the correct nodes in D
  76. is more than 2/3 of the voting power of the faulty nodes in D.
  77. Parameters:
  78. - pFaultyNodes is a set of nodes that are considered faulty
  79. - pVS is a set of all validators, maybe including Faulty, intersecting with it, etc.
  80. - pMaxFaultRatio is a pair <<a, b>> that limits the ratio a / b of the faulty
  81. validators from above (exclusive)
  82. *)
  83. FaultyValidatorsFewerThan(pFaultyNodes, pVS, maxRatio) ==
  84. LET FN == pFaultyNodes \intersect pVS \* faulty nodes in pNodes
  85. CN == pVS \ pFaultyNodes \* correct nodes in pNodes
  86. CP == Cardinality(CN) \* power of the correct nodes
  87. FP == Cardinality(FN) \* power of the faulty nodes
  88. IN
  89. \* CP + FP = TP is the total voting power
  90. LET TP == CP + FP IN
  91. FP * maxRatio[2] < TP * maxRatio[1]
  92. (* Can a block be produced by a correct peer, or an authenticated Byzantine peer *)
  93. IsLightBlockAllowedByDigitalSignatures(ht, block) ==
  94. \/ block.header = blockchain[ht] \* signed by correct and faulty (maybe)
  95. \/ /\ block.Commits \subseteq Faulty
  96. /\ block.header.height = ht
  97. /\ block.header.time >= 0 \* signed only by faulty
  98. (*
  99. Initialize the blockchain to the ultimate height right in the initial states.
  100. We pick the faulty validators statically, but that should not affect the light client.
  101. Parameters:
  102. - pMaxFaultyRatioExclusive is a pair <<a, b>> that bound the number of
  103. faulty validators in each block by the ratio a / b (exclusive)
  104. *)
  105. InitToHeight(pMaxFaultyRatioExclusive) ==
  106. /\ \E Nodes \in SUBSET AllNodes:
  107. Faulty := Nodes \* pick a subset of nodes to be faulty
  108. \* pick the validator sets and last commits
  109. /\ \E vs, lastCommit \in [Heights -> SUBSET AllNodes]:
  110. \E timestamp \in [Heights -> Int]:
  111. \* refClock is at least as early as the timestamp in the last block
  112. /\ \E tm \in Int:
  113. refClock := tm /\ tm >= timestamp[ULTIMATE_HEIGHT]
  114. \* the genesis starts on day 1
  115. /\ timestamp[1] = 1
  116. /\ vs[1] = AllNodes
  117. /\ lastCommit[1] = EmptyNodeSet
  118. /\ \A h \in Heights \ {1}:
  119. /\ lastCommit[h] \subseteq vs[h - 1] \* the non-validators cannot commit
  120. /\ TwoThirds(vs[h - 1], lastCommit[h]) \* the commit has >2/3 of validator votes
  121. \* the faulty validators have the power below the threshold
  122. /\ FaultyValidatorsFewerThan(Faulty, vs[h], pMaxFaultyRatioExclusive)
  123. /\ timestamp[h] > timestamp[h - 1] \* the time grows monotonically
  124. /\ timestamp[h] < timestamp[h - 1] + TRUSTING_PERIOD \* but not too fast
  125. \* form the block chain out of validator sets and commits (this makes apalache faster)
  126. /\ blockchain := [h \in Heights |->
  127. [height |-> h,
  128. time |-> timestamp[h],
  129. VS |-> vs[h],
  130. NextVS |-> IF h < ULTIMATE_HEIGHT THEN vs[h + 1] ELSE AllNodes,
  131. lastCommit |-> lastCommit[h]]
  132. ] \******
  133. (********************* BLOCKCHAIN ACTIONS ********************************)
  134. (*
  135. Advance the clock by zero or more time units.
  136. *)
  137. AdvanceTime ==
  138. /\ \E tm \in Int: tm >= refClock /\ refClock' = tm
  139. /\ UNCHANGED <<blockchain, Faulty>>
  140. =============================================================================
  141. \* Modification History
  142. \* Last modified Wed Jun 10 14:10:54 CEST 2020 by igor
  143. \* Created Fri Oct 11 15:45:11 CEST 2019 by igor