@@ -55,6 +55,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Types
55
55
, ChainSelMessage (.. )
56
56
, ChainSelQueue -- opaque
57
57
, addBlockToAdd
58
+ , addPerasCertToQueue
58
59
, addReprocessLoEBlocks
59
60
, closeChainSelQueue
60
61
, getChainSelMessage
@@ -66,6 +67,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Types
66
67
-- * Trace types
67
68
, SelectionChangedInfo (.. )
68
69
, TraceAddBlockEvent (.. )
70
+ , TraceAddPerasCertEvent (.. )
69
71
, TraceChainSelStarvationEvent (.. )
70
72
, TraceCopyToImmutableDBEvent (.. )
71
73
, TraceEvent (.. )
@@ -83,7 +85,6 @@ import Control.ResourceRegistry
83
85
import Control.Tracer
84
86
import Data.Foldable (traverse_ )
85
87
import Data.Map.Strict (Map )
86
- import Data.Maybe (mapMaybe )
87
88
import Data.Maybe.Strict (StrictMaybe (.. ))
88
89
import Data.MultiSet (MultiSet )
89
90
import qualified Data.MultiSet as MultiSet
@@ -104,6 +105,7 @@ import Ouroboros.Consensus.Protocol.Abstract
104
105
import Ouroboros.Consensus.Storage.ChainDB.API
105
106
( AddBlockPromise (.. )
106
107
, AddBlockResult (.. )
108
+ , AddPerasCertPromise (.. )
107
109
, ChainDbError (.. )
108
110
, ChainSelectionPromise (.. )
109
111
, ChainType
@@ -549,6 +551,11 @@ data BlockToAdd m blk = BlockToAdd
549
551
data ChainSelMessage m blk
550
552
= -- | Add a new block
551
553
ChainSelAddBlock ! (BlockToAdd m blk )
554
+ | -- | Add a Peras certificate
555
+ ChainSelAddPerasCert
556
+ ! (PerasCert blk )
557
+ -- | Used for 'AddPerasCertPromise'.
558
+ ! (StrictTMVar m () )
552
559
| -- | Reprocess blocks that have been postponed by the LoE.
553
560
ChainSelReprocessLoEBlocks
554
561
-- | Used for 'ChainSelectionPromise'.
@@ -597,6 +604,28 @@ addBlockToAdd tracer (ChainSelQueue{varChainSelQueue, varChainSelPoints}) punish
597
604
, blockProcessed = readTMVar varBlockProcessed
598
605
}
599
606
607
+ -- | Add a Peras certificate to the background queue.
608
+ addPerasCertToQueue ::
609
+ (IOLike m , StandardHash blk ) =>
610
+ Tracer m (TraceAddPerasCertEvent blk ) ->
611
+ ChainSelQueue m blk ->
612
+ PerasCert blk ->
613
+ m (AddPerasCertPromise m )
614
+ addPerasCertToQueue tracer ChainSelQueue {varChainSelQueue} cert = do
615
+ varProcessed <- newEmptyTMVarIO
616
+ traceWith tracer $ addedToQueue RisingEdge
617
+ queueSize <- atomically $ do
618
+ writeTBQueue varChainSelQueue $ ChainSelAddPerasCert cert varProcessed
619
+ lengthTBQueue varChainSelQueue
620
+ traceWith tracer $ addedToQueue $ FallingEdgeWith $ fromIntegral queueSize
621
+ pure
622
+ AddPerasCertPromise
623
+ { waitPerasCertProcessed = atomically $ takeTMVar varProcessed
624
+ }
625
+ where
626
+ addedToQueue =
627
+ AddedPerasCertToQueue (perasCertRound cert) (perasCertBoostedBlock cert)
628
+
600
629
-- | Try to add blocks again that were postponed due to the LoE.
601
630
addReprocessLoEBlocks ::
602
631
IOLike m =>
@@ -651,6 +680,7 @@ getChainSelMessage starvationTracer starvationVar chainSelQueue =
651
680
let pt = blockRealPoint block
652
681
traceWith starvationTracer $ ChainSelStarvation (FallingEdgeWith pt)
653
682
atomically . writeTVar starvationVar . ChainSelStarvationEndedAt =<< getMonotonicTime
683
+ ChainSelAddPerasCert {} -> pure ()
654
684
ChainSelReprocessLoEBlocks {} -> pure ()
655
685
656
686
-- TODO Can't use tryReadTBQueue from io-classes because it is broken for IOSim
@@ -661,18 +691,15 @@ tryReadTBQueue' q = (Just <$> readTBQueue q) `orElse` pure Nothing
661
691
-- | Flush the 'ChainSelQueue' queue and notify the waiting threads.
662
692
closeChainSelQueue :: IOLike m => ChainSelQueue m blk -> STM m ()
663
693
closeChainSelQueue ChainSelQueue {varChainSelQueue = queue} = do
664
- as <- mapMaybe blockAdd <$> flushTBQueue queue
665
- traverse_
666
- ( \ a ->
667
- tryPutTMVar
668
- (varBlockProcessed a)
669
- (FailedToAddBlock " Queue flushed" )
670
- )
671
- as
694
+ traverse_ deliverPromise =<< flushTBQueue queue
672
695
where
673
- blockAdd = \ case
674
- ChainSelAddBlock ab -> Just ab
675
- ChainSelReprocessLoEBlocks _ -> Nothing
696
+ deliverPromise = \ case
697
+ ChainSelAddBlock ab ->
698
+ tryPutTMVar (varBlockProcessed ab) (FailedToAddBlock " Queue flushed" )
699
+ ChainSelAddPerasCert _cert varProcessed ->
700
+ tryPutTMVar varProcessed ()
701
+ ChainSelReprocessLoEBlocks varProcessed ->
702
+ tryPutTMVar varProcessed ()
676
703
677
704
-- | To invoke when the given 'ChainSelMessage' has been processed by ChainSel.
678
705
-- This is used to remove the respective point from the multiset of points in
@@ -685,6 +712,8 @@ processedChainSelMessage ::
685
712
processedChainSelMessage ChainSelQueue {varChainSelPoints} = \ case
686
713
ChainSelAddBlock BlockToAdd {blockToAdd = blk} ->
687
714
modifyTVar varChainSelPoints $ MultiSet. delete (blockRealPoint blk)
715
+ ChainSelAddPerasCert {} ->
716
+ pure ()
688
717
ChainSelReprocessLoEBlocks {} ->
689
718
pure ()
690
719
@@ -729,6 +758,7 @@ data TraceEvent blk
729
758
| TracePerasCertDbEvent (PerasCertDB. TraceEvent blk )
730
759
| TraceLastShutdownUnclean
731
760
| TraceChainSelStarvationEvent (TraceChainSelStarvationEvent blk )
761
+ | TraceAddPerasCertEvent (TraceAddPerasCertEvent blk )
732
762
deriving Generic
733
763
734
764
deriving instance
@@ -1035,3 +1065,26 @@ data TraceIteratorEvent blk
1035
1065
newtype TraceChainSelStarvationEvent blk
1036
1066
= ChainSelStarvation (Enclosing' (RealPoint blk ))
1037
1067
deriving (Generic , Eq , Show )
1068
+
1069
+ data TraceAddPerasCertEvent blk
1070
+ = -- | The Peras certificate from the given round boosting the given block was
1071
+ -- added to the queue. The size of the queue is included.
1072
+ AddedPerasCertToQueue PerasRoundNo (Point blk ) (Enclosing' Word )
1073
+ | -- | The Peras certificate from the given round boosting the given block was
1074
+ -- popped from the queue.
1075
+ PoppedPerasCertFromQueue PerasRoundNo (Point blk )
1076
+ | -- | The Peras certificate from the given round boosting the given block was
1077
+ -- too old, ie its slot was older than the current immutable slot (the third
1078
+ -- argument).
1079
+ IgnorePerasCertTooOld PerasRoundNo (Point blk ) (Point blk )
1080
+ | -- | The Peras certificate from the given round boosts a block on the
1081
+ -- current selection.
1082
+ PerasCertBoostsCurrentChain PerasRoundNo (Point blk )
1083
+ | -- | The Peras certificate from the given round boosts the Genesis point.
1084
+ PerasCertBoostsGenesis PerasRoundNo
1085
+ | -- | The Peras certificate from the given round boosts a block that we have
1086
+ -- not (yet) received.
1087
+ PerasCertBoostsBlockNotYetReceived PerasRoundNo (Point blk )
1088
+ | -- | Perform chain selection for a block boosted by a Peras certificate.
1089
+ ChainSelectionForBoostedBlock PerasRoundNo (Point blk )
1090
+ deriving (Generic , Eq , Show )
0 commit comments