-- | 'AcquiredEbs', the endorser blocks whose closure the LeiosDB holds.
module Test.Consensus.Leios.AcquiredEbs (tests) where

import Cardano.Crypto.Hash (Hash (UnsafeHash))
import qualified Data.ByteString.Short as SBS
import Ouroboros.Consensus.Block (SlotNo (..))
import Ouroboros.Consensus.Leios.Types (EndorserBlockHash, LeiosPoint (..))
import Ouroboros.Consensus.Storage.ChainDB.Impl.Leios
  ( insertAcquiredEb
  , isAcquiredEb
  , mkAcquiredEbs
  , pruneAcquiredEbs
  )
import Test.Tasty
import Test.Tasty.HUnit

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup
    TestName
"Leios acquired endorser blocks"
    [ TestName -> Assertion -> TestTree
testCase TestName
"pruning forgets the points older than the slot" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (LeiosPoint -> Bool) -> [LeiosPoint] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (LeiosPoint -> AcquiredEbs -> Bool
`isAcquiredEb` AcquiredEbs
pruned) [LeiosPoint
pointA, LeiosPoint
pointB, LeiosPoint
laterPointA] [Bool] -> [Bool] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Bool
False, Bool
True, Bool
True]
    , TestName -> Assertion -> TestTree
testCase TestName
"a point older than the slot pruned to is not recorded afterwards" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        LeiosPoint -> AcquiredEbs -> Bool
isAcquiredEb LeiosPoint
pointA (LeiosPoint -> AcquiredEbs -> AcquiredEbs
insertAcquiredEb LeiosPoint
pointA AcquiredEbs
pruned)
          Bool -> Bool -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= Bool
False
    ]
 where
  pointA :: LeiosPoint
pointA = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (Word64 -> SlotNo
SlotNo Word64
1) EndorserBlockHash
ebA
  pointB :: LeiosPoint
pointB = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (Word64 -> SlotNo
SlotNo Word64
2) EndorserBlockHash
ebB
  laterPointA :: LeiosPoint
laterPointA = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint (Word64 -> SlotNo
SlotNo Word64
3) EndorserBlockHash
ebA
  pruned :: AcquiredEbs
pruned =
    SlotNo -> AcquiredEbs -> AcquiredEbs
pruneAcquiredEbs (Word64 -> SlotNo
SlotNo Word64
2) (AcquiredEbs -> AcquiredEbs) -> AcquiredEbs -> AcquiredEbs
forall a b. (a -> b) -> a -> b
$
      [LeiosPoint] -> AcquiredEbs
mkAcquiredEbs [LeiosPoint
pointA, LeiosPoint
pointB, LeiosPoint
laterPointA]

ebA, ebB :: EndorserBlockHash
ebA :: EndorserBlockHash
ebA = ShortByteString -> EndorserBlockHash
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
1)
ebB :: EndorserBlockHash
ebB = ShortByteString -> EndorserBlockHash
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
2)