Skip to content

Commit

Permalink
Rename reader to follower
Browse files Browse the repository at this point in the history
Fixes #2833

ouroboros-network has its own set of rules we have to adhere to, thus
the differences

Question is: in Test/ChainProducerState.hs why not going from r to f (as in other files):

1. the r name was somewhat incorrect in the first place because it was
referencing a ReaderState/FollowerState and in other places it was
referencing just the Reader/FollowerState
2. the f name was already taken in few places

Thus to make things a bit more consistent I've decided to go with
/s/r/fs.
Not that for the pattern matching on ChainProducerState I've also made
a decision on renaming the rs to cfs (because that is a chain follower
state) and nrid to cnfid (because that is a chain new follower id)
  • Loading branch information
EncodePanda committed Jan 20, 2021
1 parent 07b8d4c commit 6aee120
Show file tree
Hide file tree
Showing 16 changed files with 679 additions and 674 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
module Test.Ouroboros.Storage.ChainDB.Model (
Model -- opaque
, IteratorId
, CPS.ReaderId
, CPS.FollowerId
-- * Construction
, empty
, addBlock
Expand Down Expand Up @@ -53,11 +53,11 @@ module Test.Ouroboros.Storage.ChainDB.Model (
, stream
, iteratorNext
, iteratorClose
-- * Readers
, newReader
, readerInstruction
, readerForward
, readerClose
-- * Followers
, newFollower
, followerInstruction
, followerForward
, followerClose
-- * ModelSupportsBlock
, ModelSupportsBlock
-- * Exported for testing purposes
Expand Down Expand Up @@ -537,35 +537,37 @@ iteratorClose :: IteratorId -> Model blk -> Model blk
iteratorClose itrId m = m { iterators = Map.insert itrId [] (iterators m) }

{-------------------------------------------------------------------------------
Readers
Followers
-------------------------------------------------------------------------------}

readerExists :: CPS.ReaderId -> Model blk -> Bool
readerExists rdrId = CPS.readerExists rdrId . cps
followerExists :: CPS.FollowerId -> Model blk -> Bool
followerExists flrId = CPS.followerExists flrId . cps

checkIfReaderExists :: CPS.ReaderId -> Model blk
-> a
-> Either (ChainDbError blk) a
checkIfReaderExists rdrId m a
| readerExists rdrId m
checkIfFollowerExists ::
CPS.FollowerId
-> Model blk
-> a
-> Either (ChainDbError blk) a
checkIfFollowerExists flrId m a
| followerExists flrId m
= Right a
| otherwise
= Left ClosedReaderError
= Left ClosedFollowerError

newReader :: HasHeader blk => Model blk -> (CPS.ReaderId, Model blk)
newReader m = (rdrId, m { cps = cps' })
newFollower :: HasHeader blk => Model blk -> (CPS.FollowerId, Model blk)
newFollower m = (flrId, m { cps = cps' })
where
(cps', rdrId) = CPS.initReader GenesisPoint (cps m)
(cps', flrId) = CPS.initFollower GenesisPoint (cps m)

readerInstruction
:: forall blk b. ModelSupportsBlock blk
=> CPS.ReaderId
followerInstruction ::
forall blk b. ModelSupportsBlock blk
=> CPS.FollowerId
-> BlockComponent blk b
-> Model blk
-> Either (ChainDbError blk)
(Maybe (ChainUpdate blk b), Model blk)
readerInstruction rdrId blockComponent m = checkIfReaderExists rdrId m $
rewrap $ CPS.readerInstruction rdrId (cps m)
followerInstruction flrId blockComponent m = checkIfFollowerExists flrId m $
rewrap $ CPS.followerInstruction flrId (cps m)
where
toB :: blk -> b
toB blk = getBlockComponent blk blockComponent
Expand All @@ -576,25 +578,26 @@ readerInstruction rdrId blockComponent m = checkIfReaderExists rdrId m $
rewrap Nothing = (Nothing, m)
rewrap (Just (upd, cps')) = (Just (toB <$> upd), m { cps = cps' })

readerForward :: HasHeader blk
=> CPS.ReaderId
-> [Point blk]
-> Model blk
-> Either (ChainDbError blk)
(Maybe (Point blk), Model blk)
readerForward rdrId points m = checkIfReaderExists rdrId m $
followerForward ::
HasHeader blk
=> CPS.FollowerId
-> [Point blk]
-> Model blk
-> Either (ChainDbError blk) (Maybe (Point blk), Model blk)
followerForward flrId points m = checkIfFollowerExists flrId m $
case CPS.findFirstPoint points (cps m) of
Nothing -> (Nothing, m)
Just ipoint -> (Just ipoint, m { cps = cps' })
where
cps' = CPS.updateReader rdrId ipoint (cps m)
cps' = CPS.updateFollower flrId ipoint (cps m)

readerClose :: CPS.ReaderId
-> Model blk
-> Model blk
readerClose rdrId m
| readerExists rdrId m
= m { cps = CPS.deleteReader rdrId (cps m) }
followerClose ::
CPS.FollowerId
-> Model blk
-> Model blk
followerClose flrId m
| followerExists flrId m
= m { cps = CPS.deleteFollower flrId (cps m) }
| otherwise
= m

Expand Down Expand Up @@ -944,7 +947,7 @@ copyToImmutableDB secParam m = m {
closeDB :: Model blk -> Model blk
closeDB m@Model{..} = m {
isOpen = False
, cps = cps { CPS.chainReaders = Map.empty }
, cps = cps { CPS.chainFollowers = Map.empty }
, iterators = Map.empty
}

Expand Down
Loading

0 comments on commit 6aee120

Please sign in to comment.