Skip to content

Commit a34d495

Browse files
authored
Reference formal specification that includes Full-Short Leios (#405)
1 parent 96b6a65 commit a34d495

File tree

7 files changed

+91
-50
lines changed

7 files changed

+91
-50
lines changed

flake.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
url = "github:input-output-hk/iogx";
88
};
99

10-
leios-spec.url = "github:input-output-hk/ouroboros-leios-formal-spec?rev=5f1b814dbe8abd4fda4c817899432754d43f929d";
10+
leios-spec.url = "github:input-output-hk/ouroboros-leios-formal-spec?rev=86c5d8b8ccf3b16e279bd71efa26fb69000d8416";
1111
};
1212

1313

leios-trace-verifier/Parser.agda

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ record Endorsement : Type where
7575

7676
postulate
7777
Nullable : Type Type
78+
unwrap : {a} Nullable a Maybe a
7879

7980
{-# COMPILE GHC Nullable = type Nullable #-}
81+
{-# FOREIGN GHC
82+
unwrap' :: () -> Nullable a -> Maybe a
83+
unwrap' _ (Nullable x) = x
84+
#-}
85+
{-# COMPILE GHC unwrap = unwrap' #-}
8086

8187
data Event : Type where
8288
Slot : String SlotNo Event
@@ -98,13 +104,20 @@ record TraceEvent : Type where
98104

99105
{-# COMPILE GHC TraceEvent = data TraceEvent (TraceEvent) #-}
100106

101-
module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String ℕ)) (sl : ℕ) where
107+
module _
108+
(numberOfParties : ℕ)
109+
(sutId : ℕ)
110+
(stakeDistr : List (Pair String ℕ))
111+
(stageLength : ℕ)
112+
(ledgerQuality : ℕ)
113+
(lateIBInclusion : Bool) -- TODO: Pass config and topology instead
114+
where
102115

103116
from-id : Fin numberOfParties
104117
from-id n =
105118
case n <? numberOfParties of λ where
106119
(yes p) #_ n {numberOfParties} {fromWitness p}
107-
(no _) error "Conversion to Fin not possible!"
120+
(no _) error $ "Conversion to Fin not possible! " ◇ show n ◇ " / " ◇ show numberOfParties
108121

109122
nodePrefix : String
110123
nodePrefix = "node-"
@@ -113,20 +126,26 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
113126
SUT-id = from-id sutId
114127

115128
instance
116-
sl-NonZero : NonZero sl
117-
sl-NonZero with sl0
129+
stageLength-NonZero : NonZero stageLength
130+
stageLength-NonZero with stageLength0
118131
... | yes _ = error "Stage length is 0"
119132
... | no ¬p = ≢-nonZero ¬p
120133

134+
numberOfParties-NonZero : NonZero numberOfParties
135+
numberOfParties-NonZero with numberOfParties ≟ 0
136+
... | yes _ = error "Number of parties is 0"
137+
... | no ¬p = ≢-nonZero ¬p
138+
139+
121140
nodeId : String Fin numberOfParties
122141
nodeId s with S.readMaybe 10 (S.fromList (drop (S.length nodePrefix) $ S.toList s))
123142
... | nothing = error ("Unknown node: " ◇ s)
124143
... | just n = from-id n
125144

126145
open FunTot (completeFin numberOfParties) (maximalFin numberOfParties)
127146

128-
sd : TotalMap (Fin numberOfParties) ℕ
129-
sd =
147+
stakeDistribution : TotalMap (Fin numberOfParties) ℕ
148+
stakeDistribution =
130149
let (r , l) = fromListᵐ (L.map (λ (x , y) (nodeId x , y)) stakeDistr)
131150
in case (¿ total r ¿) of λ where
132151
(yes p) record { rel = r ; left-unique-rel = l ; total-rel = p }
@@ -177,11 +196,16 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
177196
params : Params
178197
params =
179198
record
180-
{ numberOfParties = numberOfParties
181-
; sutId = SUT-id
182-
; stakeDistribution = sd
183-
; stageLength = sl
184-
; winning-slots = fromList (L.catMaybes $ L.map winningSlot l)
199+
{ networkParams =
200+
record
201+
{ numberOfParties = numberOfParties
202+
; ledgerQuality = ledgerQuality
203+
; stakeDistribution = stakeDistribution
204+
; stageLength = stageLength
205+
; lateIBInclusion = lateIBInclusion
206+
}
207+
; sutId = SUT-id
208+
; winning-slots = fromList ∘ L.catMaybes $ L.map winningSlot l
185209
}
186210

187211
open import Leios.Short.Trace.Verifier params renaming (verifyTrace to checkTrace)
@@ -190,13 +214,14 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
190214
IB-Blk : InputBlock Blk
191215
EB-Blk : EndorserBlock Blk
192216
VT-Blk : List Vote Blk
217+
RB-Blk : EndorserBlock Blk
193218

194219
record State : Type where
195220
field refs : AssocList String Blk
196221

197222
instance
198-
hhx : Hashable InputBlock (List ℕ)
199-
hhx .hash record { header = h } = hash h
223+
Hashable-InputBlock : Hashable InputBlock (List ℕ)
224+
Hashable-InputBlock .hash record { header = h } = hash h
200225

201226
_ = Show-List
202227
_ = Show-×
@@ -210,17 +235,12 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
210235
unquoteDecl Show-EndorserBlockOSig = derive-Show [ (quote EndorserBlockOSig , Show-EndorserBlockOSig) ]
211236
unquoteDecl Show-Blk = derive-Show [ (quote Blk , Show-Blk) ]
212237

213-
del : String
214-
del = ", "
215-
216-
nl : String
217-
nl = "\n"
218-
219238
blockRefToNat : AssocList String Blk String IBRef
220239
blockRefToNat refs r with refs ⁉ r
221240
... | just (IB-Blk ib) = hash ib
222241
... | just (EB-Blk eb) = error $ "IB expected, got EB instead, " ◇ show eb
223242
... | just (VT-Blk vt) = error $ "IB expected, got VT instead"
243+
... | just (RB-Blk eb) = error $ "IB expected, got RB instead"
224244
... | nothing = error $ "IB expected, got nothing (" ◇ r ◇ " / " ◇ show refs ◇ ")"
225245

226246
open State
@@ -292,7 +312,7 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
292312
where
293313
actions : List (Action × LeiosInput ⊎ FFDUpdate)
294314
actions with p ≟ SUT
295-
... | yes _ = (inj₁ (EB-Role-Action (primWord64ToNat s) [] , SLOT)) ∷ []
315+
... | yes _ = (inj₁ (EB-Role-Action (primWord64ToNat s) [] [] , SLOT)) ∷ []
296316
... | no _ = []
297317
traceEvent→action l record { message = VTBundleGenerated p i s _ _ vts } =
298318
let vt = map (const tt) (elems vts)
@@ -302,7 +322,13 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
302322
actions with p ≟ SUT
303323
... | yes _ = (inj₁ (VT-Role-Action (primWord64ToNat s) , SLOT)) ∷ []
304324
... | no _ = []
305-
traceEvent→action l record { message = RBGenerated _ _ _ _ _ _ _ _ } = l , []
325+
traceEvent→action l record { message = RBGenerated p i s _ eb _ _ _ }
326+
with (unwrap eb)
327+
... | nothing = l , []
328+
... | just b
329+
with refs l ⁉ BlockRef.id (Endorsement.eb b)
330+
... | nothing = l , []
331+
... | just e = record l { refs = (i , e) ∷ refs l } , []
306332

307333
mapAccuml : {A B S : Set} (S A S × B) S List A S × List B
308334
mapAccuml f s [] = s , []
@@ -325,17 +351,17 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
325351
Show-FFDBuffers .show _ = "ffd buffers"
326352

327353
Show-Action : Show Action
328-
Show-Action .show (IB-Role-Action x) = "IB-Role-Action " ◇ show x
329-
Show-Action .show (EB-Role-Action x _) = "EB-Role-Action " ◇ show x
330-
Show-Action .show (VT-Role-Action x) = "VT-Role-Action " ◇ show x
331-
Show-Action .show (No-IB-Role-Action x) = "No-IB-Role-Action " ◇ show x
332-
Show-Action .show (No-EB-Role-Action x) = "No-EB-Role-Action " ◇ show x
333-
Show-Action .show (No-VT-Role-Action x) = "No-VT-Role-Action " ◇ show x
334-
Show-Action .show (Ftch-Action x) = "Ftch-Action " ◇ show x
335-
Show-Action .show (Slot-Action x) = "Slot-Action " ◇ show x
336-
Show-Action .show (Base₁-Action x) = "Base₁-Action " ◇ show x
337-
Show-Action .show (Base₂a-Action x _) = "Base₂a-Action " ◇ show x
338-
Show-Action .show (Base₂b-Action x) = "Base₂b-Action " ◇ show x
354+
Show-Action .show (IB-Role-Action x) = "IB-Role-Action " ◇ show x
355+
Show-Action .show (EB-Role-Action x _ _) = "EB-Role-Action " ◇ show x
356+
Show-Action .show (VT-Role-Action x) = "VT-Role-Action " ◇ show x
357+
Show-Action .show (No-IB-Role-Action x) = "No-IB-Role-Action " ◇ show x
358+
Show-Action .show (No-EB-Role-Action x) = "No-EB-Role-Action " ◇ show x
359+
Show-Action .show (No-VT-Role-Action x) = "No-VT-Role-Action " ◇ show x
360+
Show-Action .show (Ftch-Action x) = "Ftch-Action " ◇ show x
361+
Show-Action .show (Slot-Action x) = "Slot-Action " ◇ show x
362+
Show-Action .show (Base₁-Action x) = "Base₁-Action " ◇ show x
363+
Show-Action .show (Base₂a-Action x _) = "Base₂a-Action " ◇ show x
364+
Show-Action .show (Base₂b-Action x) = "Base₂b-Action " ◇ show x
339365

340366
instance
341367
Show-NonZero : {n : ℕ} Show (NonZero n)
@@ -351,22 +377,26 @@ module _ (numberOfParties : ℕ) (sutId : ℕ) (stakeDistr : List (Pair String
351377
Show-⊎ .show (inj₁ x) = show x
352378
Show-⊎ .show (inj₂ y) = show y
353379

354-
unquoteDecl Show-FFDUpdate = derive-Show [ (quote FFDUpdate , Show-FFDUpdate) ]
355-
unquoteDecl Show-Params = derive-Show [ (quote Params , Show-Params) ]
356-
unquoteDecl Show-Upkeep = derive-Show [ (quote SlotUpkeep , Show-Upkeep) ]
357-
unquoteDecl Show-Upkeep-Stage = derive-Show [ (quote StageUpkeep , Show-Upkeep-Stage) ]
358-
unquoteDecl Show-LeiosState = derive-Show [ (quote LeiosState , Show-LeiosState) ]
359-
unquoteDecl Show-LeiosInput = derive-Show [ (quote LeiosInput , Show-LeiosInput) ]
380+
unquoteDecl Show-FFDUpdate = derive-Show [ (quote FFDUpdate , Show-FFDUpdate) ]
381+
unquoteDecl Show-NetworkParams = derive-Show [ (quote NetworkParams , Show-NetworkParams) ]
382+
unquoteDecl Show-Params = derive-Show [ (quote Params , Show-Params) ]
383+
unquoteDecl Show-Upkeep = derive-Show [ (quote SlotUpkeep , Show-Upkeep) ]
384+
unquoteDecl Show-Upkeep-Stage = derive-Show [ (quote StageUpkeep , Show-Upkeep-Stage) ]
385+
unquoteDecl Show-LeiosState = derive-Show [ (quote LeiosState , Show-LeiosState) ]
386+
unquoteDecl Show-LeiosInput = derive-Show [ (quote LeiosInput , Show-LeiosInput) ]
360387

361388
s₀ : LeiosState
362-
s₀ = initLeiosState tt sd tt ((SUT-id , tt) ∷ [])
389+
s₀ = initLeiosState tt stakeDistribution tt ((SUT-id , tt) ∷ [])
363390

364391
format-Err-verifyAction : {α i s} Err-verifyAction α i s Pair String String
365392
format-Err-verifyAction {α} {i} {s} (E-Err e) =
366393
"Invalid Action: Slot " ◇ show α ,
367394
"Parameters: " ◇ show params ◇ nl ◇
368395
"Input: " ◇ show i ◇ nl ◇
369396
"LeiosState: " ◇ show s
397+
where
398+
nl : String
399+
nl = "\n"
370400

371401
format-Err-verifyUpdate : {μ s} Err-verifyUpdate μ s Pair String String
372402
format-Err-verifyUpdate {μ} (E-Err _) = "Invalid Update" , show μ

leios-trace-verifier/examples/valid/agda-2.jsonl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
{"message":{"slot":7,"type":"IBGenerated","producer":"node-0","id":"0-7","rb_ref":"-7674734470404649268","pipeline":0,"size_bytes":98608,"tx_payload_bytes":98304},"time_s":7.01}
121121
#
122122
# ∷ inj₁ (VT-Role-Action 7 , SLOT)
123-
{"message":{"slot":7,"type":"VTBundleGenerated","producer":"node-0","id":"0-7","votes":{},"pipeline":0,"size_bytes":105},"time_s":7.02}
123+
#{"message":{"slot":7,"type":"VTBundleGenerated","producer":"node-0","id":"0-7","votes":{},"pipeline":0,"size_bytes":105},"time_s":7.02}
124124
#
125125
# ∷ inj₁ (Base₂b-Action 7 , SLOT)
126126
# ∷ inj₁ (Slot-Action 7 , SLOT)

leios-trace-verifier/hs-src/app/Main.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@ main :: IO ()
2222
main =
2323
do
2424
Command{..} <- execParser commandParser
25+
26+
-- Prameters from topology
2527
(top :: Topology COORD2D) <- decodeFileThrow topologyFile
26-
(config :: Config) <- decodeFileThrow configFile
2728
let nrNodes = toInteger $ Prelude.length (elems $ nodes top)
2829
let nodeNames = Prelude.map unNodeName (keys $ nodes top)
2930
let stakes = Prelude.map (toInteger . stake . nodeInfo) (elems $ nodes top)
3031
let stakeDistribution = Prelude.zip nodeNames stakes
32+
33+
-- Parameters from config
34+
(config :: Config) <- decodeFileThrow configFile
3135
let stageLength = toInteger (leiosStageLengthSlots config)
36+
let ledgerQuality = ceiling (praosChainQuality config) -- TODO: int in schema?
37+
let lateIBInclusion = leiosLateIbInclusion config
38+
3239
result <-
33-
verifyTrace nrNodes idSut stakeDistribution stageLength
40+
verifyTrace nrNodes idSut stakeDistribution stageLength ledgerQuality lateIBInclusion
3441
. decodeJSONL
3542
<$> BSL.readFile logFile
3643
hPutStrLn stderr $ "Applying " <> show (fst result) <> " actions"

leios-trace-verifier/hs-src/test/Spec/Generated.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Spec.Generated where
88
import Control.Monad (join, liftM2, mzero, replicateM)
99
import Data.List (inits)
1010
import Data.Text (Text)
11-
import LeiosConfig (leiosStageLengthSlots)
11+
import LeiosConfig
1212
import LeiosEvents
1313
import LeiosTopology (nodeInfo, nodes, stake, unNodeName)
1414
import Lib (verifyTrace)
@@ -28,8 +28,10 @@ verify =
2828
stakes = toInteger . stake . nodeInfo <$> (M.elems $ nodes Scenario.topology)
2929
stakeDistribution = zip nodeNames stakes
3030
stageLength' = toInteger $ leiosStageLengthSlots Scenario.config
31+
ledgerQuality = ceiling (praosChainQuality Scenario.config) -- TODO: int in schema?
32+
lateIBInclusion = leiosLateIbInclusion Scenario.config
3133
in
32-
verifyTrace nrNodes Scenario.idSut stakeDistribution stageLength'
34+
verifyTrace nrNodes Scenario.idSut stakeDistribution stageLength' ledgerQuality lateIBInclusion
3335

3436
data Check
3537
= MustBeOkay

leios-trace-verifier/hs-src/test/Spec/Golden.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ golden = do
3535
let stakeDistribution = Prelude.zip nodeNames stakes
3636
let stageLength = toInteger (leiosStageLengthSlots config)
3737
let idSut = 0
38+
let ledgerQuality = ceiling (praosChainQuality config) -- TODO: int in schema?
39+
let lateIBInclusion = leiosLateIbInclusion config
3840
let check :: String -> String -> [FilePath] -> (Text -> Text -> Expectation) -> SpecWith ()
3941
check label folder files predicate =
4042
describe label $ do
4143
forM_ files $ \file ->
4244
it file $ do
4345
result <-
44-
verifyTrace nrNodes idSut stakeDistribution stageLength
46+
verifyTrace nrNodes idSut stakeDistribution stageLength ledgerQuality lateIBInclusion
4547
. decodeJSONL
4648
<$> BSL.readFile (dir </> folder </> file)
4749
fst (snd result) `predicate` "ok"

0 commit comments

Comments
 (0)