-- | 'LeiosValidClaims', the claims that some valid certificate established.
module Test.Consensus.Leios.ValidClaims (tests) where

import Cardano.Crypto.Hash (Hash (UnsafeHash))
import qualified Data.ByteString.Short as SBS
import Ouroboros.Consensus.Block (SlotNo (..))
import Ouroboros.Consensus.Leios.Cert (LeiosClaim (..))
import Ouroboros.Consensus.Leios.Types (RankingBlockHash (..))
import Ouroboros.Consensus.Storage.ChainDB.Impl.Leios
  ( emptyLeiosValidClaims
  , insertLeiosValidClaim
  , isLeiosValidClaim
  , pruneLeiosValidClaims
  )
import Test.Tasty
import Test.Tasty.HUnit

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup
    TestName
"Leios valid claims"
    [ TestName -> Assertion -> TestTree
testCase TestName
"a recorded claim is valid, and others are not" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (RankingBlockHash -> Bool) -> [RankingBlockHash] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (RankingBlockHash -> LeiosValidClaims -> Bool
`isLeiosValidClaim` LeiosValidClaims
claims) [RankingBlockHash
rbA, RankingBlockHash
rbB, RankingBlockHash
rbC, RankingBlockHash
rbD]
          [Bool] -> [Bool] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Bool
True, Bool
True, Bool
True, Bool
False]
    , TestName -> Assertion -> TestTree
testCase TestName
"pruning forgets the claims older than the slot" (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
        (RankingBlockHash -> Bool) -> [RankingBlockHash] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (RankingBlockHash -> LeiosValidClaims -> Bool
`isLeiosValidClaim` SlotNo -> LeiosValidClaims -> LeiosValidClaims
pruneLeiosValidClaims (Word64 -> SlotNo
SlotNo Word64
2) LeiosValidClaims
claims) [RankingBlockHash
rbA, RankingBlockHash
rbB, RankingBlockHash
rbC]
          [Bool] -> [Bool] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Bool
False, Bool
False, Bool
True]
    ]
 where
  claims :: LeiosValidClaims
claims =
    LeiosClaim -> LeiosValidClaims -> LeiosValidClaims
insertLeiosValidClaim (SlotNo -> RankingBlockHash -> LeiosClaim
LeiosClaim (Word64 -> SlotNo
SlotNo Word64
2) RankingBlockHash
rbC) (LeiosValidClaims -> LeiosValidClaims)
-> LeiosValidClaims -> LeiosValidClaims
forall a b. (a -> b) -> a -> b
$
      LeiosClaim -> LeiosValidClaims -> LeiosValidClaims
insertLeiosValidClaim (SlotNo -> RankingBlockHash -> LeiosClaim
LeiosClaim (Word64 -> SlotNo
SlotNo Word64
1) RankingBlockHash
rbB) (LeiosValidClaims -> LeiosValidClaims)
-> LeiosValidClaims -> LeiosValidClaims
forall a b. (a -> b) -> a -> b
$
        LeiosClaim -> LeiosValidClaims -> LeiosValidClaims
insertLeiosValidClaim (SlotNo -> RankingBlockHash -> LeiosClaim
LeiosClaim (Word64 -> SlotNo
SlotNo Word64
0) RankingBlockHash
rbA) LeiosValidClaims
emptyLeiosValidClaims

rbA, rbB, rbC, rbD :: RankingBlockHash
rbA :: RankingBlockHash
rbA = Hash HASH EraIndependentBlockHeader -> RankingBlockHash
MkRankingBlockHash (ShortByteString -> Hash HASH EraIndependentBlockHeader
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
1))
rbB :: RankingBlockHash
rbB = Hash HASH EraIndependentBlockHeader -> RankingBlockHash
MkRankingBlockHash (ShortByteString -> Hash HASH EraIndependentBlockHeader
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
2))
rbC :: RankingBlockHash
rbC = Hash HASH EraIndependentBlockHeader -> RankingBlockHash
MkRankingBlockHash (ShortByteString -> Hash HASH EraIndependentBlockHeader
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
3))
rbD :: RankingBlockHash
rbD = Hash HASH EraIndependentBlockHeader -> RankingBlockHash
MkRankingBlockHash (ShortByteString -> Hash HASH EraIndependentBlockHeader
forall h a.
(HashAlgorithm h, HasCallStack) =>
ShortByteString -> Hash h a
UnsafeHash (Int -> Word8 -> ShortByteString
SBS.replicate Int
32 Word8
4))