Skip to content

Commit

Permalink
Add rollback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kderme committed Nov 28, 2021
1 parent ef4a80e commit 763d6e5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
13 changes: 10 additions & 3 deletions cardano-chain-gen/src/Cardano/Mock/ChainSync/Server.hs
Expand Up @@ -18,6 +18,7 @@ module Cardano.Mock.ChainSync.Server
, IOManager
, replaceGenesis
, addBlock
, rollback
, readChain
, stopServer
, withIOManager
Expand Down Expand Up @@ -74,11 +75,10 @@ import Ouroboros.Network.Protocol.ChainSync.Server (ChainSyncServer, S
import Ouroboros.Network.Protocol.Handshake.Version (simpleSingletonVersions)
import Ouroboros.Network.Snocket (LocalAddress, LocalSnocket, LocalSocket (..))
import qualified Ouroboros.Network.Snocket as Snocket
import Ouroboros.Network.Socket (debuggingNetworkServerTracers)
import Ouroboros.Network.Util.ShowProxy (ShowProxy, Proxy(..))

import Cardano.Mock.ChainSync.State
import Cardano.Mock.Chain
import Cardano.Mock.Chain hiding (rollback)
import Cardano.Mock.ChainDB

data ServerHandle m blk = ServerHandle
Expand All @@ -100,6 +100,13 @@ addBlock handle blk =
modifyTVar (chainProducerState handle) $
addBlockState blk

rollback :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> Point blk -> STM m ()
rollback handle point =
modifyTVar (chainProducerState handle) $ \st ->
case rollbackState point st of
Nothing -> error $ "point " <> show point <> " not in chain"
Just st' -> st'

stopServer :: ServerHandle m blk -> IO ()
stopServer = cancel . threadHandle

Expand Down Expand Up @@ -144,7 +151,7 @@ runLocalServer iom codecConfig networkMagic localDomainSock chainProducerState =
networkState <- NodeToClient.newNetworkMutableState
_ <- NodeToClient.withServer
localSnocket
debuggingNetworkServerTracers
NodeToClient.nullNetworkServerTracers -- debuggingNetworkServerTracers
networkState
localSocket
(versions chainProducerState)
Expand Down
10 changes: 5 additions & 5 deletions cardano-chain-gen/src/Cardano/Mock/ChainSync/State.hs
Expand Up @@ -58,11 +58,11 @@ addBlockState b (ChainProducerState c cflrst cfid) =
-- | Rollback producer chain. It requires to update follower states, since some
-- @'followerPoint'@s may not be on the new chain; in this case find intersection
-- of the two chains and set @'followerNext'@ to @'FollowerBackTo'@.
rollback :: (HasHeader block, HeaderHash block ~ HeaderHash block')
=> Point block'
-> ChainProducerState block
-> Maybe (ChainProducerState block)
rollback p (ChainProducerState c cflrst cfid) = do
rollbackState :: (HasHeader block, HeaderHash block ~ HeaderHash block')
=> Point block'
-> ChainProducerState block
-> Maybe (ChainProducerState block)
rollbackState p (ChainProducerState c cflrst cfid) = do
c' <- rollbackChainDB c (castPoint p)
return $ ChainProducerState c' (rollbackFollower <$> cflrst) cfid
where
Expand Down
34 changes: 31 additions & 3 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit.hs
Expand Up @@ -19,7 +19,7 @@ import Cardano.Ledger.Slot (BlockNo (..))

import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus

import Ouroboros.Network.Block (blockNo)
import Ouroboros.Network.Block (blockNo, blockPoint)
import Ouroboros.Network.Magic

import Cardano.DbSync (runDbSyncNode)
Expand All @@ -39,7 +39,7 @@ unitTests :: IOManager -> [(Text, Text)] -> TestTree
unitTests iom knownMigrations =
testGroup "unit tests"
[ testCase "Forge some blocks" forgeBlocks
, testCase "Add one Simple block" (addSimpleChain iom knownMigrations)
, testCase "Add one Simple block" (simpleRollback iom knownMigrations)
]

rootTestDir :: FilePath
Expand Down Expand Up @@ -111,7 +111,7 @@ addSimpleChain iom knownMigrations = do

restartDBSync :: IOManager -> [(Text, Text)] -> IO ()
restartDBSync iom knownMigrations = do
let testDir = rootTestDir </> "temp/addSimple"
let testDir = rootTestDir </> "temp/restartDBSync"
recreateDir testDir
-- create all keys, configs and genesis files from a template
-- Normally these will be already hard-coded.
Expand All @@ -134,6 +134,34 @@ restartDBSync iom knownMigrations = do
_dbSync' <- async $ runDbSyncNode emptyMetricsSetters True knownMigrations (syncNodeParams cfg)
assertBlockNoBackoff 0

simpleRollback :: IOManager -> [(Text, Text)] -> IO ()
simpleRollback iom knownMigrations = do
let testDir = rootTestDir </> "temp/simpleRollback"
recreateDir testDir
-- create all keys, configs and genesis files from a template
-- Normally these will be already hard-coded.
when False $
setupTestsDir testDir
-- create the configuration for dbsync and the interpreter
cfg <- mkConfig configDir testDir
-- initiate the interpreter
interpreter <- initInterpreter (protocolInfoForging cfg) nullTracer
-- translate the block to a real Cardano block.
blk0 <- forgeNext interpreter mockBlock0
blk1 <- forgeNext interpreter mockBlock1
blk2 <- forgeNext interpreter mockBlock2
let initSt = Consensus.pInfoInitLedger $ protocolInfo cfg
-- fork the mocked chainsync server
mockServer <- forkServerThread @CardanoBlock iom (topLevelConfig cfg) initSt (NetworkMagic 42) $ unSocketPath (enpSocketPath $ syncNodeParams cfg)
atomically $ addBlock mockServer blk0
-- start db-sync and let it sync
_ <- async $ runDbSyncNode emptyMetricsSetters True knownMigrations (syncNodeParams cfg)
atomically $ addBlock mockServer blk1
atomically $ addBlock mockServer blk2
assertBlockNoBackoff 2
atomically $ rollback mockServer (blockPoint blk1)
assertBlockNoBackoff 1

recreateDir :: FilePath -> IO ()
recreateDir path = do
removePathForcibly path
Expand Down

0 comments on commit 763d6e5

Please sign in to comment.