Skip to content

Commit af05488

Browse files
committed
Handle drop-key messages from KES Agent
1 parent 4ca03a3 commit af05488

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Ledger/HotKey.hs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,21 @@ mkKESState maxKESEvolutions newOCert newKey evolution startPeriod@(Absolute.KESP
264264
, kesStateKey = KESKey newOCert newKey
265265
}
266266

267+
type KeyProducer c m =
268+
(OCert.OCert c -> KES.SignKeyKES (KES c) -> Word -> Absolute.KESPeriod -> m ())
269+
-- ^ Callback that will be invoked when a new key has been received
270+
-> m ()
271+
-- ^ Callback that will be invoked when a key deletion has been received
272+
-> m ()
273+
267274
-- | Create a new 'HotKey' that runs a key-producer action on a separate thread.
268275
-- The key producer action will receive a callback that can be used to pass
269276
-- keys into the HotKey; the HotKey will dynamically update its internal state
270277
-- to reflect new keys as they arrive.
271278
mkDynamicHotKey ::
272279
forall m c. (Crypto c, IOLike m)
273280
=> Word64 -- ^ Max KES evolutions
274-
-> Maybe (
275-
(OCert.OCert c -> KES.SignKeyKES (KES c) -> Word -> Absolute.KESPeriod -> m ())
276-
-> m ()
277-
)
281+
-> Maybe (KeyProducer c m)
278282
-> m ()
279283
-> m (HotKey c m)
280284
mkDynamicHotKey = mkHotKeyWith Nothing
@@ -285,10 +289,7 @@ mkHotKeyWith ::
285289
forall m c. (Crypto c, IOLike m)
286290
=> Maybe (OCert.OCert c, KES.SignKeyKES (KES c), Word, Absolute.KESPeriod)
287291
-> Word64 -- ^ Max KES evolutions
288-
-> Maybe (
289-
(OCert.OCert c -> KES.SignKeyKES (KES c) -> Word -> Absolute.KESPeriod -> m ())
290-
-> m ()
291-
)
292+
-> Maybe (KeyProducer c m)
292293
-> m ()
293294
-> m (HotKey c m)
294295
mkHotKeyWith initialStateMay maxKESEvolutions keyThreadMay finalizer = do
@@ -298,6 +299,8 @@ mkHotKeyWith initialStateMay maxKESEvolutions keyThreadMay finalizer = do
298299
modifyMVar_ varKESState $ \oldState -> do
299300
_ <- poisonState oldState
300301
return $ mkKESState maxKESEvolutions newOCert newKey evolution startPeriod
302+
unset =
303+
modifyMVar_ varKESState $ poisonState
301304

302305
forM_ initialStateMay $ \(newOCert, newKey, evolution, startPeriod) ->
303306
set newOCert newKey evolution startPeriod
@@ -306,7 +309,7 @@ mkHotKeyWith initialStateMay maxKESEvolutions keyThreadMay finalizer = do
306309
Just keyThread -> do
307310
keyThreadAsync <- async $ do
308311
labelThisThread "HotKey receiver"
309-
keyThread set
312+
keyThread set unset
310313
return (cancel keyThreadAsync >> finalizer)
311314
Nothing ->
312315
return finalizer
@@ -327,8 +330,7 @@ mkHotKeyWith initialStateMay maxKESEvolutions keyThreadMay finalizer = do
327330
KESKey _ key -> do
328331
let evolution = kesEvolution kesStateInfo
329332
KES.signedKES () evolution toSign key
330-
, forget = do
331-
modifyMVar_ varKESState $ poisonState
333+
, forget = unset
332334
, finalize_ = finalizer'
333335
}
334336
where

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos/AgentClient.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class ( Crypto c
8282
instance AgentCrypto StandardCrypto where
8383
type ACrypto StandardCrypto = Agent.StandardCrypto
8484

85+
convertPeriod :: Agent.KESPeriod -> OCert.KESPeriod
86+
convertPeriod (Agent.KESPeriod p) = OCert.KESPeriod p
87+
8588
convertOCert :: ( AgentCrypto c
8689
)
8790
=> Agent.OCert (ACrypto c) -> OCert.OCert c
@@ -130,9 +133,10 @@ runKESAgentClient :: forall m c.
130133
)
131134
=> Tracer m KESAgentClientTrace
132135
-> FilePath
133-
-> (OCert.OCert c -> SignKeyKES (KES c) -> Word -> m ())
136+
-> (OCert.OCert c -> SignKeyKES (KES c) -> Word -> OCert.KESPeriod -> m ())
137+
-> m ()
134138
-> m ()
135-
runKESAgentClient tracer path handleKey = do
139+
runKESAgentClient tracer path handleKey handleDropKey = do
136140
withAgentContext $ \snocket -> do
137141
forever $ do
138142
Agent.runServiceClient
@@ -151,9 +155,11 @@ runKESAgentClient tracer path handleKey = do
151155
-- finishes.
152156
_ <- acquireCRef skpRef
153157
withCRefValue skpRef $ \(SignKeyWithPeriodKES sk p) ->
154-
handleKey (convertOCert ocert) sk p
158+
handleKey (convertOCert ocert) sk p (convertPeriod $ Agent.ocertKESPeriod ocert)
159+
return Agent.RecvOK
160+
_ -> do
161+
handleDropKey
155162
return Agent.RecvOK
156-
_ -> undefined -- TODO
157163
)
158164
(contramap KESAgentClientTrace tracer)
159165
`catch` ( \(_e :: AsyncCancelled) ->

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos/Common.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,8 @@ instantiatePraosCredentials maxKESEvolutions (PraosCredentialsUnsound ocert skUn
300300
instantiatePraosCredentials maxKESEvolutions (PraosCredentialsAgent path) = do
301301
HotKey.mkDynamicHotKey
302302
maxKESEvolutions
303-
(Just $ \send -> do
304-
let handleKey ocert sk p = do
305-
send ocert sk p (OCert.ocertKESPeriod ocert)
306-
runKESAgentClient nullTracer path handleKey
303+
(Just $ \handleKey handleDrop -> do
304+
runKESAgentClient nullTracer path handleKey handleDrop
307305
)
308306
(pure ())
309307

0 commit comments

Comments
 (0)