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