|
| 1 | +{-# LANGUAGE ApplicativeDo #-} |
| 2 | +{-# LANGUAGE BangPatterns #-} |
| 3 | +{-# LANGUAGE FlexibleContexts #-} |
| 4 | +{-# LANGUAGE ImportQualifiedPost #-} |
| 5 | +{-# LANGUAGE LambdaCase #-} |
| 6 | +{-# LANGUAGE NumericUnderscores #-} |
| 7 | +{-# LANGUAGE OverloadedStrings #-} |
| 8 | +{-# LANGUAGE PartialTypeSignatures #-} |
| 9 | +{-# LANGUAGE RankNTypes #-} |
| 10 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 11 | +{-# LANGUAGE TupleSections #-} |
| 12 | +{-# LANGUAGE TypeApplications #-} |
| 13 | + |
| 14 | +module Main(main) where |
| 15 | + |
| 16 | +import Chainweb.BlockHeader |
| 17 | +import Chainweb.BlockHeaderDB |
| 18 | +import Chainweb.BlockHeight |
| 19 | +import Chainweb.Core.Brief |
| 20 | +import Chainweb.Cut (cutHeaders, unsafeMkCut) |
| 21 | +import Chainweb.Cut.Create (limitCut) |
| 22 | +import Chainweb.CutDB (cutHashesTable, readHighestCutHeaders) |
| 23 | +import Chainweb.Logger |
| 24 | +import Chainweb.Pact.PactService qualified as PactService |
| 25 | +import Chainweb.Pact.Payload.PayloadStore.RocksDB qualified as Pact.Payload.PayloadStore.RocksDB |
| 26 | +import Chainweb.Pact.Types |
| 27 | +import Chainweb.Parent |
| 28 | +import Chainweb.PayloadProvider (blockHeaderToEvaluationCtx) |
| 29 | +import Chainweb.PayloadProvider.Pact |
| 30 | +import Chainweb.PayloadProvider.Pact.Genesis (genesisPayload) |
| 31 | +import Chainweb.Storage.Table.RocksDB (modernDefaultOptions, withRocksDb) |
| 32 | +import Chainweb.Time |
| 33 | +import Chainweb.TreeDB qualified as TreeDB |
| 34 | +import Chainweb.Utils |
| 35 | +import Chainweb.Version |
| 36 | +import Chainweb.Version.Registry |
| 37 | +import Chainweb.WebBlockHeaderDB |
| 38 | +import Control.Concurrent (threadDelay) |
| 39 | +import Control.Concurrent.Async (forConcurrently) |
| 40 | +import Control.Exception.Safe |
| 41 | +import Control.Lens |
| 42 | +import Control.Monad |
| 43 | +import Control.Monad.IO.Class |
| 44 | +import Control.Monad.Trans.Resource |
| 45 | +import Data.Constraint |
| 46 | +import Data.HashSet qualified as HS |
| 47 | +import Data.HashMap.Strict qualified as HM |
| 48 | +import Data.IORef |
| 49 | +import Data.List qualified as List |
| 50 | +import Data.LogMessage |
| 51 | +import Data.Proxy |
| 52 | +import Data.Text qualified as T |
| 53 | +import GHC.Stack |
| 54 | +import Options.Applicative |
| 55 | +import Streaming qualified as S |
| 56 | +import Streaming.Prelude qualified as S |
| 57 | +import System.FilePath ((</>)) |
| 58 | +import System.Logger qualified as L |
| 59 | +import System.LogLevel |
| 60 | +import Text.Printf |
| 61 | +import Utils.Logging |
| 62 | +import Utils.Logging.Trace (Trace) |
| 63 | + |
| 64 | +main :: IO () |
| 65 | +main = join $ |
| 66 | + execParser $ info |
| 67 | + (parser <**> helper) |
| 68 | + (fullDesc |
| 69 | + <> progDesc "Replay Pact blocks checking that we get the correct outputs" |
| 70 | + <> header "pact-replay") |
| 71 | + |
| 72 | +getRocksDbDir :: HasCallStack => FilePath -> FilePath |
| 73 | +getRocksDbDir base = base </> "0" </> "rocksDb" |
| 74 | + |
| 75 | +getPactDbDir :: HasCallStack => FilePath -> FilePath |
| 76 | +getPactDbDir base = base </> "0" </> "sqlite" |
| 77 | + |
| 78 | +isPactChain :: HasVersion => ChainId -> Bool |
| 79 | +isPactChain cid = payloadProviderTypeForChain cid == PactProvider |
| 80 | + |
| 81 | +parser :: Parser (IO ()) |
| 82 | +parser = do |
| 83 | + version <- option (findKnownVersion =<< textReader) |
| 84 | + (long "chainweb-version" |
| 85 | + <> short 'v' |
| 86 | + <> help "network ID" |
| 87 | + <> metavar (T.unpack $ |
| 88 | + "[" <> T.intercalate "," (getChainwebVersionName . _versionName <$> knownVersions) <> "]") |
| 89 | + ) |
| 90 | + dbDir <- textOption (long "database-directory" <> help "chainweb database directory") |
| 91 | + maybeStart <- optional $ BlockHeight <$> textOption (long "start" <> help "lower block height bound for the replay") |
| 92 | + maybeEnd <- optional $ BlockHeight <$> textOption (long "end" <> help "upper block height bound for the replay") |
| 93 | + chains :: Dict HasVersion -> [ChainId] <- |
| 94 | + fmap const (jsonOption (long "chains")) |
| 95 | + <|> pure (\Dict -> filter isPactChain (HS.toList chainIds)) |
| 96 | + let defaultLogConfig = |
| 97 | + -- default the threshold to "info" because the progress log is "info". |
| 98 | + L.defaultLogConfig & L.logConfigLogger . L.loggerConfigThreshold .~ L.Info |
| 99 | + logConfig <- parserOptionGroup "Logging" (($ defaultLogConfig) <$> L.pLogConfig) |
| 100 | + return $ withVersion version $ do |
| 101 | + -- we'd use a read-only db, but unfortunately we write to rocksdb even |
| 102 | + -- when it's not necessary sometimes during initialization, c.f. |
| 103 | + -- initBlockHeaderDb. |
| 104 | + withRocksDb (getRocksDbDir dbDir) modernDefaultOptions $ \rdb -> do |
| 105 | + L.withHandleBackend_ logText (logConfig ^. L.logConfigBackend) $ \backend -> do |
| 106 | + -- do not log tx failures and performance telemetry. |
| 107 | + let replayLogHandles = logHandles |
| 108 | + [ dropLogHandler (Proxy @PactTxFailureLog) |
| 109 | + , dropLogHandler (Proxy @(JsonLog Trace)) |
| 110 | + ] backend |
| 111 | + L.withLogger (logConfig ^. L.logConfigLogger) replayLogHandles $ \logger -> do |
| 112 | + let cutTable = cutHashesTable rdb |
| 113 | + let pdb = Pact.Payload.PayloadStore.RocksDB.newPayloadDb rdb |
| 114 | + let wbhdb = mkWebBlockHeaderDb rdb (tabulateChains (mkBlockHeaderDb rdb)) |
| 115 | + |
| 116 | + -- start by finding our upper bound |
| 117 | + highestCutInDb <- unsafeMkCut <$> readHighestCutHeaders (logFunctionText logger) wbhdb cutTable |
| 118 | + upperBoundCut <- case maybeEnd of |
| 119 | + Nothing -> return highestCutInDb |
| 120 | + Just upperBound -> limitCut wbhdb upperBound highestCutInDb |
| 121 | + |
| 122 | + -- replay all chains concurrently |
| 123 | + failuresByChain <- forConcurrently (chains Dict `List.intersect` HM.keys (view cutHeaders upperBoundCut)) $ \cid -> runResourceT $ do |
| 124 | + let chainLogger = addLabel ("chain", brief cid) logger |
| 125 | + bhdb <- getWebBlockHeaderDb wbhdb cid |
| 126 | + |
| 127 | + PactPayloadProvider _ serviceEnv <- withPactPayloadProvider |
| 128 | + cid rdb Nothing chainLogger Nothing mempty pdb |
| 129 | + (getPactDbDir dbDir) |
| 130 | + defaultPactServiceConfig |
| 131 | + (genesisPayload cid) |
| 132 | + |
| 133 | + |
| 134 | + let upperBoundBlock = upperBoundCut ^?! cutHeaders . ix cid |
| 135 | + let upper = HS.singleton (TreeDB.UpperBound $ view blockHash upperBoundBlock) |
| 136 | + |
| 137 | + failureCountRef <- liftIO $ newIORef (0 :: Word) |
| 138 | + heightRef <- liftIO $ newIORef (view blockHeight upperBoundBlock) |
| 139 | + rateRef <- liftIO $ newIORef (0 :: Double) |
| 140 | + _ <- withAsyncR (logProgress chainLogger cid heightRef rateRef) |
| 141 | + |
| 142 | + -- replay all blocks below our upper bound, in descending order. |
| 143 | + -- This works well for us because replay failures are |
| 144 | + -- most likely to happen nearest the tip of the chains. |
| 145 | + liftIO $ TreeDB.branchEntries bhdb Nothing Nothing Nothing Nothing mempty upper $ \blockStream -> do |
| 146 | + blockStream |
| 147 | + & S.takeWhile (\blk -> maybe True (\start -> view blockHeight blk >= start) maybeStart) |
| 148 | + & withParent |
| 149 | + |
| 150 | + & S.mapM (\(h, ph) -> do |
| 151 | + |
| 152 | + -- replay the block |
| 153 | + writeIORef heightRef (view blockHeight h) |
| 154 | + fmap (h,) $ |
| 155 | + try @_ @SomeException $ |
| 156 | + PactService.execReadOnlyReplay chainLogger serviceEnv |
| 157 | + (view blockPayloadHash h <$ blockHeaderToEvaluationCtx ph) |
| 158 | + ) |
| 159 | + |
| 160 | + -- calculate the rate we're replaying and send |
| 161 | + -- it to the progress logger thread, updating |
| 162 | + -- the rate every 500 blocks. |
| 163 | + -- The replay still functionally proceeds on a |
| 164 | + -- single block at a time, because it happens |
| 165 | + -- *before* the chunking in the pipeline. |
| 166 | + & S.chunksOf 500 |
| 167 | + & mapsM_ (\blkChunk -> do |
| 168 | + startTime <- getCurrentTimeIntegral |
| 169 | + |
| 170 | + count S.:> _ S.:> res <- blkChunk |
| 171 | + & S.mapM (\case |
| 172 | + (h, Left err) -> do |
| 173 | + atomicModifyIORef' failureCountRef (\n -> (succ n, ())) |
| 174 | + logFunctionText chainLogger Error $ "Error block: " <> brief h <> ": " <> sshow err |
| 175 | + return h |
| 176 | + (h, Right (Just err)) -> do |
| 177 | + atomicModifyIORef' failureCountRef (\n -> (succ n, ())) |
| 178 | + logFunctionText chainLogger Error $ "Invalid block " <> brief h <> ": " <> sshow err |
| 179 | + return h |
| 180 | + (h, Right Nothing) -> return h |
| 181 | + ) |
| 182 | + & S.copy |
| 183 | + & S.last |
| 184 | + & S.length |
| 185 | + |
| 186 | + endTime <- getCurrentTimeIntegral |
| 187 | + let !(TimeSpan (timeTaken :: Micros)) = (endTime `diff` startTime) |
| 188 | + let !rate :: Double = int count * 1_000_000 / int timeTaken |
| 189 | + |
| 190 | + writeIORef rateRef rate |
| 191 | + |
| 192 | + return res |
| 193 | + ) |
| 194 | + |
| 195 | + liftIO $ logFunctionText chainLogger Info $ "finished replaying chain " <> brief cid |
| 196 | + |
| 197 | + liftIO $ readIORef failureCountRef |
| 198 | + let failureCount = sum failuresByChain |
| 199 | + when (failureCount > 0) $ |
| 200 | + error $ sshow failureCount <> " blocks failed" |
| 201 | + where |
| 202 | + logProgress logger cid heightRef rateRef = forever $ do |
| 203 | + threadDelay 20_000_000 |
| 204 | + height <- readIORef heightRef |
| 205 | + rate <- readIORef rateRef |
| 206 | + logFunctionText logger Info $ |
| 207 | + "Chain " <> brief cid <> |
| 208 | + " rate " <> T.pack (printf "%.2f" rate) <> "/s" |
| 209 | + <> " at " <> brief height <> " (desc.)" |
| 210 | + |
| 211 | +-- requires that the input is descending |
| 212 | +withParent :: Monad m => S.Stream (S.Of h) m r -> S.Stream (S.Of (h, Parent h)) m r |
| 213 | +withParent = \strm -> do |
| 214 | + S.lift (S.next strm) >>= \case |
| 215 | + Left r -> return r |
| 216 | + Right (bh, strm') -> go bh strm' |
| 217 | + where |
| 218 | + go bh strm = do |
| 219 | + S.lift (S.next strm) >>= \case |
| 220 | + Left r -> return r |
| 221 | + Right (bh', strm') -> do |
| 222 | + S.yield (bh, Parent bh') |
| 223 | + go bh' strm' |
| 224 | + |
| 225 | +mapsM_ :: Monad m => (forall x. f x -> m x) -> S.Stream f m r -> m r |
| 226 | +mapsM_ f = go |
| 227 | + where |
| 228 | + go strm = |
| 229 | + S.inspect strm >>= \case |
| 230 | + Left r -> return r |
| 231 | + Right fstrm -> do |
| 232 | + strm' <- f fstrm |
| 233 | + go strm' |
0 commit comments