Skip to content

Commit 18bfc45

Browse files
committed
[WIP] set structure for model-based testing for PerasCertDB
1 parent 7267afd commit 18bfc45

File tree

7 files changed

+117
-1
lines changed

7 files changed

+117
-1
lines changed

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,9 @@ test-suite storage-test
721721
Test.Ouroboros.Storage.LedgerDB.V1.DbChangelog
722722
Test.Ouroboros.Storage.LedgerDB.V1.LMDB
723723
Test.Ouroboros.Storage.Orphans
724+
Test.Ouroboros.Storage.PerasCertDB
725+
Test.Ouroboros.Storage.PerasCertDB.Model
726+
Test.Ouroboros.Storage.PerasCertDB.StateMachine
724727
Test.Ouroboros.Storage.TestBlock
725728
Test.Ouroboros.Storage.VolatileDB
726729
Test.Ouroboros.Storage.VolatileDB.Mock

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/SupportsPeras.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Ouroboros.Consensus.Block.Abstract
2323

2424
newtype PerasRoundNo = PerasRoundNo {unPerasRoundNo :: Word64}
2525
deriving stock Show
26+
deriving Generic
2627
deriving newtype (Eq, Ord, NoThunks)
2728

2829
newtype PerasWeight = PerasWeight {unPerasWeight :: Word64}
@@ -50,7 +51,7 @@ instance StandardHash blk => BlockSupportsPeras blk where
5051
{ pcCertRound :: PerasRoundNo
5152
, pcCertBoostedBlock :: Point blk
5253
}
53-
deriving stock Generic
54+
deriving stock (Generic, Eq, Ord, Show)
5455
deriving anyclass NoThunks
5556

5657
perasCertRound = pcCertRound

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/PerasCertDB/API.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DerivingVia #-}
33
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE DeriveGeneric #-}
56

67
module Ouroboros.Consensus.Storage.PerasCertDB.API
78
( PerasCertDB (..)
@@ -17,6 +18,7 @@ import Ouroboros.Consensus.Block
1718
import Ouroboros.Consensus.Util.IOLike
1819
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
1920
import qualified Ouroboros.Network.AnchoredFragment as AF
21+
import GHC.Generics (Generic)
2022

2123
data PerasCertDB m blk = PerasCertDB
2224
{ addCert :: PerasCert blk -> m ()
@@ -29,6 +31,7 @@ newtype PerasWeightSnapshot blk = PerasWeightSnapshot
2931
{ getPerasWeightSnapshot :: Map (Point blk) PerasWeight
3032
}
3133
deriving stock Show
34+
deriving Generic
3235
deriving newtype NoThunks
3336

3437
boostedWeightForPoint ::

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import qualified Test.Ouroboros.Storage.ChainDB as ChainDB
66
import qualified Test.Ouroboros.Storage.ImmutableDB as ImmutableDB
77
import qualified Test.Ouroboros.Storage.LedgerDB as LedgerDB
88
import qualified Test.Ouroboros.Storage.VolatileDB as VolatileDB
9+
import qualified Test.Ouroboros.Storage.PerasCertDB as PerasCertDB
10+
911
import Test.Tasty (TestTree, testGroup)
1012

1113
--
@@ -20,4 +22,5 @@ tests =
2022
, VolatileDB.tests
2123
, LedgerDB.tests
2224
, ChainDB.tests
25+
, PerasCertDB.tests
2326
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
module Test.Ouroboros.Storage.PerasCertDB (tests) where
4+
5+
import qualified Test.Ouroboros.Storage.PerasCertDB.StateMachine as StateMachine
6+
import Test.Tasty (TestTree, testGroup)
7+
8+
--
9+
-- The list of all PerasCertDB tests
10+
--
11+
12+
tests :: TestTree
13+
tests =
14+
testGroup
15+
"PerasCertDB"
16+
[ StateMachine.tests
17+
]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE StandaloneDeriving #-}
3+
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE UndecidableInstances #-}
5+
module Test.Ouroboros.Storage.PerasCertDB.Model
6+
( PerasCertDBModel,
7+
initPerasCertDBModel,
8+
openDBModel,
9+
closeDBModel,
10+
addCertModel,
11+
getWeightSnapshotModel
12+
) where
13+
import Ouroboros.Consensus.Block (PerasCert, boostPerCert, perasCertBoostedBlock, StandardHash)
14+
import Data.Set (Set)
15+
import GHC.Generics (Generic)
16+
import Data.Proxy (Proxy)
17+
import qualified Data.Set as Set
18+
import Ouroboros.Consensus.Storage.PerasCertDB.API
19+
import qualified Data.Map as Map
20+
import Data.Vector.Internal.Check (HasCallStack)
21+
import Ouroboros.Consensus.Storage.PerasCertDB.Impl (PerasCertDbError(..))
22+
import Ouroboros.Consensus.Util.CallStack (prettyCallStack)
23+
24+
data PerasCertDBModel blk = PerasCertDBModel
25+
{
26+
open :: Bool,
27+
certs :: Set (PerasCert blk)
28+
} deriving Generic
29+
30+
deriving instance (StandardHash blk) => Show (PerasCertDBModel blk)
31+
32+
initPerasCertDBModel :: Proxy blk -> PerasCertDBModel blk
33+
initPerasCertDBModel _ = PerasCertDBModel
34+
{ open = False
35+
, certs = Set.empty
36+
}
37+
38+
openDBModel :: PerasCertDBModel blk -> PerasCertDBModel blk
39+
openDBModel model = model { open = True }
40+
41+
closeDBModel :: PerasCertDBModel blk -> PerasCertDBModel blk
42+
closeDBModel model = model { open = False }
43+
44+
addCertModel :: (HasCallStack, StandardHash blk) => PerasCertDBModel blk -> PerasCert blk -> Either PerasCertDbError (PerasCertDBModel blk)
45+
addCertModel model cert =
46+
if open model
47+
then Right model { certs = Set.insert cert (certs model) }
48+
else Left (ClosedDBError prettyCallStack)
49+
50+
getWeightSnapshotModel :: (HasCallStack, StandardHash blk) => PerasCertDBModel blk -> Either PerasCertDbError (PerasWeightSnapshot blk)
51+
getWeightSnapshotModel model =
52+
if open model
53+
then
54+
Right $ PerasWeightSnapshot {
55+
getPerasWeightSnapshot = Set.fold
56+
(\cert acc -> Map.insertWith (<>) (perasCertBoostedBlock cert) boostPerCert acc)
57+
Map.empty
58+
(certs model)
59+
}
60+
else Left (ClosedDBError prettyCallStack)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{-# LANGUAGE TypeFamilies #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE DeriveGeneric #-}
5+
{-# LANGUAGE StandaloneDeriving #-}
6+
{-# LANGUAGE FlexibleInstances #-}
7+
module Test.Ouroboros.Storage.PerasCertDB.StateMachine (tests) where
8+
import Test.Tasty (TestTree)
9+
import Test.Ouroboros.Storage.TestBlock (TestBlock)
10+
import Ouroboros.Consensus.Block.SupportsPeras
11+
import Test.Ouroboros.Storage.PerasCertDB.Model
12+
import Test.QuickCheck.StateModel
13+
import Ouroboros.Consensus.Storage.PerasCertDB.API (PerasWeightSnapshot)
14+
15+
tests :: TestTree
16+
tests = undefined
17+
18+
type Block = TestBlock
19+
newtype Model = Model (PerasCertDBModel Block) deriving (Show, Generic)
20+
21+
instance StateModel Model where
22+
data Action Model a where
23+
OpenDB :: Action Model ()
24+
CloseDB :: Action Model ()
25+
AddCert :: PerasCert Block -> Action Model ()
26+
GetWeightSnapshot :: Action Model (PerasWeightSnapshot Block)
27+
28+
arbitraryAction _ _ = error "arbitraryAction not implemented"
29+
initialState = error "initialState not implemented"

0 commit comments

Comments
 (0)