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