module Main (main) where

import qualified Cardano.Tools.DBAnalyser.Block.Cardano as Cardano
import qualified Cardano.Tools.DBAnalyser.Run as DBAnalyser
import           Cardano.Tools.DBAnalyser.Types
import qualified Cardano.Tools.DBImmutaliser.Run as DBImmutaliser
import qualified Cardano.Tools.DBSynthesizer.Run as DBSynthesizer
import           Cardano.Tools.DBSynthesizer.Types
import           Ouroboros.Consensus.Block
import           Ouroboros.Consensus.Cardano.Block
import qualified Test.Cardano.Tools.Headers
import           Test.Tasty
import           Test.Tasty.HUnit
import           Test.Util.TestEnv


nodeConfig, chainDB :: FilePath
nodeConfig :: FilePath
nodeConfig  = FilePath
"test/tools-test/disk/config/config.json"
chainDB :: FilePath
chainDB     = FilePath
"test/tools-test/disk/chaindb"


testSynthOptionsCreate :: DBSynthesizerOptions
testSynthOptionsCreate :: DBSynthesizerOptions
testSynthOptionsCreate =
    DBSynthesizerOptions {
        synthLimit :: ForgeLimit
synthLimit          = Word64 -> ForgeLimit
ForgeLimitEpoch Word64
1
      , synthOpenMode :: DBSynthesizerOpenMode
synthOpenMode       = DBSynthesizerOpenMode
OpenCreateForce
    }

testSynthOptionsAppend :: DBSynthesizerOptions
testSynthOptionsAppend :: DBSynthesizerOptions
testSynthOptionsAppend =
    DBSynthesizerOptions {
        synthLimit :: ForgeLimit
synthLimit          = SlotNo -> ForgeLimit
ForgeLimitSlot SlotNo
8192
      , synthOpenMode :: DBSynthesizerOpenMode
synthOpenMode       = DBSynthesizerOpenMode
OpenAppend
    }

testNodeFilePaths :: NodeFilePaths
testNodeFilePaths :: NodeFilePaths
testNodeFilePaths =
    NodeFilePaths {
        nfpConfig :: FilePath
nfpConfig   = FilePath
nodeConfig
      , nfpChainDB :: FilePath
nfpChainDB  = FilePath
chainDB
    }

testNodeCredentials :: NodeCredentials
testNodeCredentials :: NodeCredentials
testNodeCredentials =
    NodeCredentials {
        credCertFile :: Maybe FilePath
credCertFile  = Maybe FilePath
forall a. Maybe a
Nothing
      , credVRFFile :: Maybe FilePath
credVRFFile   = Maybe FilePath
forall a. Maybe a
Nothing
      , credKESFile :: Maybe FilePath
credKESFile   = Maybe FilePath
forall a. Maybe a
Nothing
      , credBulkFile :: Maybe FilePath
credBulkFile  = FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
"test/tools-test/disk/config/bulk-creds-k2.json"
    }

testImmutaliserConfig :: DBImmutaliser.Opts
testImmutaliserConfig :: Opts
testImmutaliserConfig =
  DBImmutaliser.Opts {
      dbDirs :: DBDirs FilePath
DBImmutaliser.dbDirs = DBImmutaliser.DBDirs {
          immDBDir :: FilePath
DBImmutaliser.immDBDir = FilePath
chainDB FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> FilePath
"/immutable"
        , volDBDir :: FilePath
DBImmutaliser.volDBDir = FilePath
chainDB FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> FilePath
"/volatile"
        }
   , configFile :: FilePath
DBImmutaliser.configFile = FilePath
nodeConfig
   }

testAnalyserConfig :: DBAnalyserConfig
testAnalyserConfig :: DBAnalyserConfig
testAnalyserConfig =
  DBAnalyserConfig {
      dbDir :: FilePath
dbDir                       = FilePath
chainDB
    , ldbBackend :: LedgerDBBackend
ldbBackend                  = LedgerDBBackend
V2InMem
    , verbose :: Bool
verbose                     = Bool
False
    , selectDB :: SelectDB
selectDB                    = WithOrigin SlotNo -> SelectDB
SelectImmutableDB WithOrigin SlotNo
forall t. WithOrigin t
Origin
    , validation :: Maybe ValidateBlocks
validation                  = ValidateBlocks -> Maybe ValidateBlocks
forall a. a -> Maybe a
Just ValidateBlocks
ValidateAllBlocks
    , analysis :: AnalysisName
analysis                    = AnalysisName
CountBlocks
    , confLimit :: Limit
confLimit                   = Limit
Unlimited
    }

testBlockArgs :: Cardano.Args (CardanoBlock StandardCrypto)
testBlockArgs :: Args (CardanoBlock StandardCrypto)
testBlockArgs = FilePath
-> Maybe PBftSignatureThreshold
-> Args (CardanoBlock StandardCrypto)
Cardano.CardanoBlockArgs FilePath
nodeConfig Maybe PBftSignatureThreshold
forall a. Maybe a
Nothing

-- | A multi-step test including synthesis and analysis 'SomeConsensusProtocol' using the Cardano instance.
--
-- 1. step: synthesize a ChainDB from scratch and count the amount of blocks forged.
-- 2. step: append to the previous ChainDB and coutn the amount of blocks forged.
-- 3. step: copy the VolatileDB into the ImmutableDB.
-- 3. step: analyze the ImmutableDB resulting from previous steps and confirm the total block count.

--
blockCountTest :: (String -> IO ()) -> Assertion
blockCountTest :: (FilePath -> IO ()) -> IO ()
blockCountTest FilePath -> IO ()
logStep = do
    FilePath -> IO ()
logStep FilePath
"running synthesis - create"
    (options, protocol) <- (FilePath
 -> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto))
-> ((DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
    -> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto))
-> Either
     FilePath
     (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
-> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either FilePath
-> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
forall a. HasCallStack => FilePath -> IO a
assertFailure (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
-> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (Either
   FilePath
   (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
 -> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto))
-> IO
     (Either
        FilePath
        (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto))
-> IO (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< NodeFilePaths
-> NodeCredentials
-> DBSynthesizerOptions
-> IO
     (Either
        FilePath
        (DBSynthesizerConfig, CardanoProtocolParams StandardCrypto))
DBSynthesizer.initialize
          NodeFilePaths
testNodeFilePaths
          NodeCredentials
testNodeCredentials
          DBSynthesizerOptions
testSynthOptionsCreate
    resultCreate <- DBSynthesizer.synthesize genTxs options protocol
    let blockCountCreate = ForgeResult -> Int
resultForged ForgeResult
resultCreate
    blockCountCreate > 0 @? "no blocks have been forged during create step"

    logStep "running synthesis - append"
    resultAppend <- DBSynthesizer.synthesize genTxs options {confOptions = testSynthOptionsAppend} protocol
    let blockCountAppend = ForgeResult -> Int
resultForged ForgeResult
resultAppend
    blockCountAppend > 0 @? "no blocks have been forged during append step"

    logStep "copy volatile to immutable DB"
    DBImmutaliser.run testImmutaliserConfig

    logStep "running analysis"
    resultAnalysis <- DBAnalyser.analyse testAnalyserConfig testBlockArgs

    let blockCount = Int
blockCountCreate Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
blockCountAppend
    resultAnalysis == Just (ResultCountBlock blockCount) @?
        "wrong number of blocks encountered during analysis \
        \ (counted: " ++ show resultAnalysis ++ "; expected: " ++ show blockCount ++ ")"
  where
    genTxs :: p -> p -> p -> p -> f [a]
genTxs p
_ p
_ p
_ p
_ = [a] -> f [a]
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

tests :: TestTree
tests :: TestTree
tests =
    FilePath -> [TestTree] -> TestTree
testGroup FilePath
"cardano-tools"
      [ FilePath -> ((FilePath -> IO ()) -> IO ()) -> TestTree
testCaseSteps FilePath
"synthesize and analyse: blockCount\n" (FilePath -> IO ()) -> IO ()
blockCountTest
      , TestTree
Test.Cardano.Tools.Headers.tests
      ]

main :: IO ()
IO ()
main = TestEnvConfig -> TestTree -> IO ()
defaultMainWithTestEnv TestEnvConfig
defaultTestEnvConfig TestTree
tests