@@ -11,6 +11,9 @@ open Lean.ToJson (toJson)
1111open  Std (HashMap)
1212
1313
14+ /-- 
15+ Try to forge an RB in this substep, given the state and environment. 
16+ -/ 
1417def  forgeRb  {env : Environment} (state : State) : Outcomes :=
1518  let  state' :=
1619    {
@@ -23,6 +26,9 @@ def forgeRb {env : Environment} (state : State) : Outcomes :=
2326  , ({state' with  hasRb := false , canCertify := false }, 1  - p)
2427  ]
2528
29+ /-- 
30+ Try to include a certificate in the latest RB being forged in this substep, given the state and environment. 
31+ -/ 
2632def  certify  {env : Environment} (state : State) : Outcomes :=
2733  if  state.hasRb && state.canCertify
2834    then  let  p := env.pSpacingOkay
@@ -32,6 +38,9 @@ def certify {env : Environment} (state : State) : Outcomes :=
3238         ]
3339    else  [(state, 1 )]
3440
41+ /-- 
42+ Try to forge an EB in this substep, given the state and environment. 
43+ -/ 
3544def  forgeEb  {env : Environment} (state : State) : Outcomes :=
3645  if  state.hasRb
3746    then  let  p := 1  - env.pLate
@@ -41,6 +50,9 @@ def forgeEb {env : Environment} (state : State) : Outcomes :=
4150         ]
4251    else  [(state, 1 )]
4352
53+ /-- 
54+ Try to vote for an EB in this substep, given the state and environment. 
55+ -/ 
4456def  vote  {env : Environment} (state : State) : Outcomes :=
4557  if  state.hasRb
4658    then  let  p := env.pQuorum
@@ -50,13 +62,22 @@ def vote {env : Environment} (state : State) : Outcomes :=
5062         ]
5163    else  [(state, 1 )]
5264
65+ /-- 
66+ Step forward to the next potential block. 
67+ -/ 
5368def  step  {env : Environment} : List (State → Outcomes) :=
5469  [@forgeRb env, @certify env, @forgeEb env, @vote env]
5570
5671
72+ /-- 
73+ Discard probabilities below the specified threshold. 
74+ -/ 
5775def  prune  (ε : Float) : Probabilities → Probabilities :=
5876  HashMap.filter (fun  _ p => p > ε)
5977
78+ /-- 
79+ Evolve state probabilities on step forward according the a transition function. 
80+ -/ 
6081def  evolve  (transition : State → Outcomes) : Probabilities → Probabilities :=
6182  HashMap.fold
6283    (
@@ -68,19 +89,32 @@ def evolve (transition : State → Outcomes) : Probabilities → Probabilities :
6889    )
6990    ∅
7091
92+ /-- 
93+ Chain a sequence of transitions sequentially, evolving probabilities. 
94+ -/ 
7195def  chain  (transitions : List (State → Outcomes)) : Probabilities → Probabilities :=
7296  match  transitions with 
7397  | [] => id
7498  | (t :: ts) => chain ts ∘ evolve t
7599
100+ /-- 
101+ Simulate the specified number of potential blocks, given a starting set of probabilities. 
102+ -/ 
76103def  simulate  (env : Environment) (start : Probabilities) (ε : Float) : Nat → Probabilities
77104| 0      => start
78105| n + 1  => let  state' := prune ε $ chain (@step env) start
79106           simulate env state' ε n
80107
108+ 
109+ /-- 
110+ Compute the total probabilities of a set of states. 
111+ -/ 
81112def  totalProbability  (states : Probabilities) : Probability :=
82113  states.values.sum
83114
115+ /-- 
116+ Compute the distribution of EB counts. 
117+ -/ 
84118def  ebDistribution  : Probabilities → HashMap Nat Probability :=
85119  HashMap.fold
86120    (
@@ -90,13 +124,19 @@ def ebDistribution : Probabilities → HashMap Nat Probability :=
90124    )
91125    ∅
92126
127+ /-- 
128+ Format the distribution of EB counts as JSON. 
129+ -/ 
93130def  ebDistributionJson  : Probabilities → Json :=
94131  Json.mkObj
95132    ∘ List.map (fun  ⟨k, v⟩ => ⟨toString k, toJson v⟩)
96133    ∘ (fun  z => z.mergeSort (by  exact fun  x y => x.fst ≤ y.fst))
97134    ∘ HashMap.toList
98135    ∘ ebDistribution
99136
137+ /-- 
138+ Compute the RB efficiency, given a set of states. 
139+ -/ 
100140def  rbEfficiency  (states : Probabilities) : Float :=
101141  let  clock := states.keys.head!.clock
102142  let  rbCount :=
@@ -106,6 +146,9 @@ def rbEfficiency (states : Probabilities) : Float :=
106146      states
107147  rbCount / clock.toFloat
108148
149+ /-- 
150+ Compute the EB efficiency, given a set of states. 
151+ -/ 
109152def  ebEfficiency  (states : Probabilities) : Float :=
110153  let  clock := states.keys.head!.clock
111154  let  ebCount :=
@@ -115,6 +158,9 @@ def ebEfficiency (states : Probabilities) : Float :=
115158      states
116159  ebCount / (clock.toFloat - 1 )
117160
161+ /-- 
162+ Compute the overall efficiency, give a set of states. 
163+ -/ 
118164def  efficiency  (states : Probabilities) : Float :=
119165  let  rb := rbEfficiency states
120166  let  eb := ebEfficiency states
0 commit comments