Skip to content

Commit d0cea6e

Browse files
committed
Implemented adversarial stake
1 parent 9ed8c53 commit d0cea6e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

analysis/markov/Linleios/Evolve.lean

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,36 @@ open Lean.ToJson (toJson)
1111
open Std (HashMap)
1212

1313

14-
def certify {env : Environment} (state : State) : Outcomes :=
14+
def forge {env : Environment} (state : State) : Outcomes :=
1515
let state' :=
1616
{
1717
state with
18-
rbCount := state.rbCount + 1
19-
canCertify := true
18+
clock := state.clock + 1
2019
}
20+
let p := 1 - env.fAdversary
21+
[
22+
({state' with rbCount := state.rbCount + 1}, p)
23+
, (state', 1 - p)
24+
]
25+
26+
def certify {env : Environment} (state : State) : Outcomes :=
27+
let p := env.pSpacingOkay * (1 - env.fAdversary)
2128
if state.canCertify
2229
then [
23-
⟨{state' with ebCount := state.ebCount + 1}, env.pSpacingOkay
24-
, ⟨state', 1 - env.pSpacingOkay
30+
⟨{state with ebCount := state.ebCount + 1}, p
31+
, ⟨state, 1 - p
2532
]
26-
else [(state', 1)]
33+
else [(state, 1)]
2734

2835
def vote {env : Environment} (state : State) : Outcomes :=
36+
let p := env.pQuorum * (1 - env.fAdversary)
2937
[
30-
(state, env.pQuorum)
31-
, ({state with canCertify := false}, 1 - env.pQuorum)
38+
({state with canCertify := true}, p)
39+
, ({state with canCertify := false}, 1 - p)
3240
]
3341

3442
def step {env : Environment} : List (State → Outcomes) :=
35-
[@certify env, @vote env]
43+
[@forge env, @certify env, @vote env]
3644

3745

3846
def prune (ε : Float) : Probabilities → Probabilities :=
@@ -75,10 +83,10 @@ def ebDistributionJson : Probabilities → Json :=
7583
Json.mkObj ∘ List.map (fun ⟨k, v⟩ => ⟨toString k, toJson v⟩) ∘ HashMap.toList ∘ ebDistribution
7684

7785
def ebEfficiency (states : Probabilities) : Float :=
78-
let rbCount := states.keys.head!.rbCount
86+
let clock := states.keys.head!.clock
7987
let ebCount :=
8088
HashMap.fold
8189
(fun acc state p =>acc + state.ebCount.toFloat * p)
8290
0
8391
states
84-
ebCount / (rbCount.toFloat - 1)
92+
ebCount / (clock.toFloat - 1)

analysis/markov/Linleios/Types.lean

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,31 @@ structure Environment where
1717
Ldiff : Nat
1818
pSpacingOkay : Probability
1919
pQuorum : Probability
20+
fAdversary : Float
21+
deriving Repr
2022

21-
def makeEnvironment (activeSlotCoefficient pRbHeaderArrives pEbValidates committeeSize τ : Float) (Lheader Lvote Ldiff : Nat) : Environment :=
23+
def makeEnvironment (activeSlotCoefficient pRbHeaderArrives pEbValidates committeeSize τ fAdversary : Float) (Lheader Lvote Ldiff : Nat) : Environment :=
2224
{
2325
activeSlotCoefficient := activeSlotCoefficient
2426
Lheader := Lheader
2527
Lvote := Lvote
2628
Ldiff := Ldiff
2729
pSpacingOkay := (1 - activeSlotCoefficient).pow (3 * Lheader + Lvote + Ldiff - 1).toFloat
2830
pQuorum := pQuorum (pRbHeaderArrives * pEbValidates) committeeSize τ
31+
fAdversary := fAdversary
2932
}
3033

3134

3235
structure State where
36+
clock : Nat
3337
rbCount : Nat
3438
ebCount : Nat
3539
canCertify : Bool
3640
deriving Repr, BEq, Hashable, Inhabited
3741

38-
theorem genesis : (default : State).rbCount = 0 ∧ (default : State).ebCount = 0 := by
42+
theorem genesis : (default : State).clock = 0 ∧ (default : State).rbCount = 0 ∧ (default : State).ebCount = 0 := by
43+
constructor
44+
rfl
3945
constructor
4046
rfl
4147
rfl

analysis/markov/Main.lean

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ def runMarkovCmd (p : Parsed) : IO UInt32 :=
2525
let τ : Float := p.flag! "quorum-fraction" |>.as! Float
2626
let pRbHeaderArrives : Float := p.flag! "p-rb-header-arrives" |>.as! Float
2727
let pEbValidates : Float := p.flag! "p-eb-validates" |>.as! Float
28+
let fAdversary : Float := p.flag! "adversary-fraction" |>.as! Float
2829
let ε : Float := p.flag! "tolerance" |>.as! Float
2930
let rbCount : Nat := p.flag! "rb-count" |>.as! Nat
30-
let env := makeEnvironment activeSlotCoefficient pRbHeaderArrives pEbValidates committeeSize.toFloat τ Lheader Lvote Ldiff
31+
let env := makeEnvironment activeSlotCoefficient pRbHeaderArrives pEbValidates committeeSize.toFloat τ fAdversary Lheader Lvote Ldiff
3132
let sn := simulate env default ε rbCount
3233
if p.hasFlag "output-file"
3334
then IO.FS.writeFile (p.flag! "output-file" |>.as! String) (Json.pretty $ ebDistributionJson sn)
@@ -47,8 +48,9 @@ def markov : Cmd := `[Cli|
4748
"quorum-fraction" : Float ; "τ protocol parameter, in %/100."
4849
"p-rb-header-arrives" : Float ; "Probability that the RB header arrives before L_header."
4950
"p-eb-validates" : Float ; "Probability that the EB is fully validated before 3 L_header + L_vote."
51+
"adversary-fraction" : Float ; "Fraction of stake that is adversarial."
5052
"tolerance" : Float ; "Discard states with less than this probability."
51-
"rb-count" : Nat ; "Number of RBs to simulate."
53+
"rb-count" : Nat ; "Number of potential RBs to simulate."
5254
"output-file" : String ; "Path to the JSON output file for the EB distribution."
5355
EXTENSIONS:
5456
defaultValues! #[
@@ -60,6 +62,7 @@ def markov : Cmd := `[Cli|
6062
, ("quorum-fraction" , "0.75")
6163
, ("p-rb-header-arrives" , "0.95")
6264
, ("p-eb-validates" , "0.90")
65+
, ("adversary-fraction" , "0" )
6366
, ("tolerance" , "1e-8")
6467
, ("rb-count" , "100" )
6568
]

0 commit comments

Comments
 (0)