Skip to content

Commit

Permalink
parity replay
Browse files Browse the repository at this point in the history
Change-Id: I7348adc30c17f57a33ce7d87cd2fe974b6aeec2d
  • Loading branch information
chessai committed Oct 8, 2024
1 parent cb3b5a7 commit 1665f3f
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 205 deletions.
2 changes: 2 additions & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ library
, Chainweb.Version.Guards
, Chainweb.Version.Mainnet
, Chainweb.Version.Pact5Development
, Chainweb.Version.Pact5Retro
, Chainweb.Version.RecapDevelopment
, Chainweb.Version.Registry
, Chainweb.Version.Testnet04
Expand Down Expand Up @@ -380,6 +381,7 @@ library
build-depends:
, Decimal >= 0.4.2
, aeson >= 2.2
, aeson-pretty >= 0.8
, asn1-encoding >=0.9
, asn1-types >=0.3
, async >= 2.2
Expand Down
13 changes: 9 additions & 4 deletions node/ChainwebNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import Chainweb.Utils
import Chainweb.Utils.RequestLog
import Chainweb.Version
import Chainweb.Version.Mainnet
import Chainweb.Version.Pact5Retro (pact5Retro)
import Chainweb.Version.Testnet04 (testnet04)
import Chainweb.Version.Registry

Expand Down Expand Up @@ -558,10 +559,14 @@ main = do
installFatalSignalHandlers [ sigHUP, sigTERM, sigXCPU, sigXFSZ ]
checkRLimits
runWithPkgInfoConfiguration mainInfo pkgInfo $ \conf -> do
let v = _configChainwebVersion $ _nodeConfigChainweb conf
let nodeConf = _nodeConfigChainweb conf
let v = if _configPact5Retro nodeConf
then pact5Retro (_configChainwebVersion nodeConf)
else _configChainwebVersion nodeConf
let conf' = conf & nodeConfigChainweb . configChainwebVersion .~ v
registerVersion v
hSetBuffering stderr LineBuffering
withNodeLogger (_nodeConfigLog conf) (_nodeConfigChainweb conf) v $ \logger -> do
withNodeLogger (_nodeConfigLog conf') (_nodeConfigChainweb conf') v $ \logger -> do
logFunctionJson logger Info ProcessStarted
handles
[ Handler $ \(e :: SomeAsyncException) ->
Expand All @@ -570,8 +575,8 @@ main = do
logFunctionJson logger Error (ProcessDied $ show e) >> throwIO e
] $ do
kt <- mapM iso8601ParseM (_versionServiceDate v)
withServiceDate (_configChainwebVersion (_nodeConfigChainweb conf)) (logFunctionText logger) kt $ void $
race (node conf logger) (gcRunner (logFunctionText logger))
withServiceDate (_configChainwebVersion (_nodeConfigChainweb conf')) (logFunctionText logger) kt $ void $
race (node conf' logger) (gcRunner (logFunctionText logger))
where
gcRunner lf = runForever lf "GarbageCollect" $ do
performMajorGC
Expand Down
8 changes: 6 additions & 2 deletions src/Chainweb/BlockHeaderDB/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import Numeric.Additive
data Configuration = Configuration
{ _configRoot :: !BlockHeader
, _configRocksDb :: !RocksDb
, _configReadOnly :: !Bool
}

-- -------------------------------------------------------------------------- --
Expand Down Expand Up @@ -239,7 +240,8 @@ dbAddChecked db e = unlessM (tableMember (_chainDbCas db) ek) dbAddCheckedIntern
--
initBlockHeaderDb :: Configuration -> IO BlockHeaderDb
initBlockHeaderDb config = do
dbAddChecked db rootEntry
unless (_configReadOnly config) $ do
dbAddChecked db rootEntry
return db
where
rootEntry = _configRoot config
Expand Down Expand Up @@ -271,14 +273,16 @@ closeBlockHeaderDb _ = return ()
withBlockHeaderDb
:: RocksDb
-> ChainwebVersion
-> Bool
-> ChainId
-> (BlockHeaderDb -> IO b)
-> IO b
withBlockHeaderDb db v cid = bracket start closeBlockHeaderDb
withBlockHeaderDb db v readOnly cid = bracket start closeBlockHeaderDb
where
start = initBlockHeaderDb Configuration
{ _configRoot = genesisBlockHeader v cid
, _configRocksDb = db
, _configReadOnly = readOnly
}

-- -------------------------------------------------------------------------- --
Expand Down
60 changes: 31 additions & 29 deletions src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ withChainwebInternal
-> (StartedChainweb logger -> IO ())
-> IO ()
withChainwebInternal conf logger peer serviceSock rocksDb pactDbDir backupDir resetDb inner = do

unless (_configOnlySyncPact conf || _configReadOnlyReplay conf) $
let isReadOnly = _configReadOnlyReplay conf
unless (_configOnlySyncPact conf || isReadOnly) $
initializePayloadDb v payloadDb

-- Garbage Collection
Expand Down Expand Up @@ -412,6 +412,7 @@ withChainwebInternal conf logger peer serviceSock rocksDb pactDbDir backupDir re
v
cid
rocksDb
isReadOnly
(chainLogger cid)
mcfg
payloadDb
Expand All @@ -421,33 +422,34 @@ withChainwebInternal conf logger peer serviceSock rocksDb pactDbDir backupDir re
x
)

-- initialize global resources after all chain resources are initialized
(\cs -> do
logg Debug "finished initializing chain resources"
global (HM.fromList $ zip cidsList cs)
)
cidsList
-- initialize global resources after all chain resources are initialized
(\cs -> do
logg Debug "finished initializing chain resources"
global (HM.fromList $ zip cidsList cs)
)

cidsList
where
pactConfig maxGasLimit = PactServiceConfig
{ _pactReorgLimit = _configReorgLimit conf
, _pactPreInsertCheckTimeout = _configPreInsertCheckTimeout conf
, _pactQueueSize = _configPactQueueSize conf
, _pactResetDb = resetDb
, _pactAllowReadsInLocal = _configAllowReadsInLocal conf
, _pactUnlimitedInitialRewind =
isJust (_cutDbParamsInitialHeightLimit cutConfig) ||
isJust (_cutDbParamsInitialCutFile cutConfig)
, _pactBlockGasLimit = maybe id min maxGasLimit (_configBlockGasLimit conf)
, _pactLogGas = _configLogGas conf
, _pactModuleCacheLimit = _configModuleCacheLimit conf
, _pactEnableLocalTimeout = _configEnableLocalTimeout conf
, _pactFullHistoryRequired = _configFullHistoricPactState conf
, _pactPersistIntraBlockWrites =
if _configFullHistoricPactState conf
then PersistIntraBlockWrites
else DoNotPersistIntraBlockWrites
, _pactTxTimeLimit = Nothing
}
{ _pactReorgLimit = _configReorgLimit conf
, _pactPreInsertCheckTimeout = _configPreInsertCheckTimeout conf
, _pactQueueSize = _configPactQueueSize conf
, _pactResetDb = resetDb
, _pactAllowReadsInLocal = _configAllowReadsInLocal conf
, _pactUnlimitedInitialRewind =
isJust (_cutDbParamsInitialHeightLimit cutConfig) ||
isJust (_cutDbParamsInitialCutFile cutConfig)
, _pactBlockGasLimit = maybe id min maxGasLimit (_configBlockGasLimit conf)
, _pactLogGas = _configLogGas conf
, _pactModuleCacheLimit = _configModuleCacheLimit conf
, _pactEnableLocalTimeout = _configEnableLocalTimeout conf
, _pactFullHistoryRequired = _configFullHistoricPactState conf
, _pactPersistIntraBlockWrites =
if _configFullHistoricPactState conf
then PersistIntraBlockWrites
else DoNotPersistIntraBlockWrites
, _pactTxTimeLimit = Nothing
}

pruningLogger :: T.Text -> logger
pruningLogger l = addLabel ("sub-component", l)
Expand Down Expand Up @@ -506,8 +508,8 @@ withChainwebInternal conf logger peer serviceSock rocksDb pactDbDir backupDir re
let
pactSyncChains =
case _configSyncPactChains conf of
Just syncChains | _configOnlySyncPact conf || _configReadOnlyReplay conf -> HM.filterWithKey (\k _ -> elem k syncChains) cs
_ -> cs
Just syncChains | _configOnlySyncPact conf || _configReadOnlyReplay conf -> HM.filterWithKey (\k _ -> elem k syncChains) cs
_ -> cs

if _configReadOnlyReplay conf
then do
Expand Down
39 changes: 20 additions & 19 deletions src/Chainweb/Chainweb/ChainResources.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
Expand Down Expand Up @@ -84,6 +85,8 @@ withChainResources
=> ChainwebVersion
-> ChainId
-> RocksDb
-> Bool
-- ^ are we in readonly mode?
-> logger
-> (MVar PactExecutionService -> Mempool.InMemConfig Pact4.UnparsedTransaction)
-> PayloadDb tbl
Expand All @@ -93,25 +96,23 @@ withChainResources
-> Counter "txFailures"
-> (ChainResources logger -> IO a)
-> IO a
withChainResources
v cid rdb logger mempoolCfg0 payloadDb pactDbDir pactConfig txFailuresCounter inner =
withBlockHeaderDb rdb v cid $ \cdb -> do
pexMv <- newEmptyMVar
let mempoolCfg = mempoolCfg0 pexMv
Mempool.withInMemoryMempool (setComponent "mempool" logger) mempoolCfg v $ \mempool -> do
mpc <- MPCon.mkMempoolConsensus mempool cdb $ Just payloadDb
withPactService v cid logger (Just txFailuresCounter) mpc cdb
payloadDb pactDbDir pactConfig $ \requestQ -> do
let pex = pes requestQ
putMVar pexMv pex

-- run inner
inner $ ChainResources
{ _chainResBlockHeaderDb = cdb
, _chainResLogger = logger
, _chainResMempool = mempool
, _chainResPact = pex
}
withChainResources v cid rdb readOnly logger mempoolCfg0 payloadDb pactDbDir pactConfig txFailuresCounter inner = do
withBlockHeaderDb rdb v readOnly cid $ \cdb -> do
pexMv <- newEmptyMVar
let mempoolCfg = mempoolCfg0 pexMv
Mempool.withInMemoryMempool (setComponent "mempool" logger) mempoolCfg v $ \mempool -> do
mpc <- MPCon.mkMempoolConsensus mempool cdb $ Just payloadDb
withPactService v cid logger (Just txFailuresCounter) mpc cdb payloadDb pactDbDir pactConfig $ \requestQ -> do
let pex = pes requestQ
putMVar pexMv pex

-- run inner
inner $ ChainResources
{ _chainResBlockHeaderDb = cdb
, _chainResLogger = logger
, _chainResMempool = mempool
, _chainResPact = pex
}
where
pes requestQ
| v ^. versionCheats . disablePact = emptyPactExecutionService
Expand Down
16 changes: 12 additions & 4 deletions src/Chainweb/Chainweb/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ import Chainweb.Time

import P2P.Node.Configuration
import Chainweb.Pact.Backend.DbCache (DbCacheLimitBytes)
import Chainweb.Version.Testnet04

-- -------------------------------------------------------------------------- --
-- Throttling Configuration
Expand Down Expand Up @@ -407,7 +406,9 @@ data ChainwebConfiguration = ChainwebConfiguration
, _configReadOnlyReplay :: !Bool
-- ^ do a read-only replay using the cut db params for the block heights
, _configPactParityReplay :: !Bool
-- ^ perform replay in a way that compares pact4 and pact5 outputs.
-- ^ dump transaction outputs for comparison.
, _configPact5Retro :: !Bool
-- ^ Enable pact5 retroactively, as far back as Chainweb 2.17, inclusive
, _configSyncPactChains :: !(Maybe [ChainId])
-- ^ the only chains to be synchronized on startup to the latest cut.
-- if unset, all chains will be synchronized.
Expand Down Expand Up @@ -468,12 +469,12 @@ validateChainwebVersion v = do
, sshow (_versionName v)
]
-- FIXME Pact5: disable
when (v == mainnet || v == testnet04) $
{-when (v == mainnet || v == testnet04) $
throwError $ T.unwords
[ "This node version is a technical preview of Pact 5, and"
, "cannot be used with Pact 4 chainweb versions (testnet04, mainnet)"
, "just yet."
]
]-}
where
isDevelopment = _versionCode v `elem` [_versionCode dv | dv <- [recapDevnet, devnet]]

Expand Down Expand Up @@ -509,6 +510,7 @@ defaultChainwebConfiguration v = ChainwebConfiguration
, _configOnlySyncPact = False
, _configReadOnlyReplay = False
, _configPactParityReplay = False
, _configPact5Retro = False
, _configSyncPactChains = Nothing
, _configBackup = defaultBackupConfig
, _configModuleCacheLimit = defaultModuleCacheLimit
Expand Down Expand Up @@ -538,6 +540,8 @@ instance ToJSON ChainwebConfiguration where
, "serviceApi" .= _configServiceApi o
, "onlySyncPact" .= _configOnlySyncPact o
, "readOnlyReplay" .= _configReadOnlyReplay o
, "pactParityReplay" .= _configPactParityReplay o
, "pact5Retro" .= _configPact5Retro o
, "syncPactChains" .= _configSyncPactChains o
, "backup" .= _configBackup o
, "moduleCacheLimit" .= _configModuleCacheLimit o
Expand Down Expand Up @@ -572,6 +576,7 @@ instance FromJSON (ChainwebConfiguration -> ChainwebConfiguration) where
<*< configOnlySyncPact ..: "onlySyncPact" % o
<*< configReadOnlyReplay ..: "readOnlyReplay" % o
<*< configPactParityReplay ..: "pactParityReplay" % o
<*< configPact5Retro ..: "pact5Retro" % o
<*< configSyncPactChains ..: "syncPactChains" % o
<*< configBackup %.: "backup" % o
<*< configModuleCacheLimit ..: "moduleCacheLimit" % o
Expand Down Expand Up @@ -633,6 +638,9 @@ pChainwebConfiguration = id
<*< configPactParityReplay .:: boolOption_
% long "pact-parity-replay"
<> help "Replay the block history in a way that compares Pact 4 and Pact 5 outputs"
<*< configPact5Retro .:: boolOption_
% long "pact-5-retro"
<> help "Enable pact5 retroactively, as far back as Chainweb 2.17, inclusive"
<*< configSyncPactChains .:: fmap Just % jsonOption
% long "sync-pact-chains"
<> help "The only Pact databases to synchronize. If empty or unset, all chains will be synchronized."
Expand Down
5 changes: 4 additions & 1 deletion src/Chainweb/Chainweb/PeerResources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ getHost
-> [PeerInfo]
-> IO (Either T.Text Hostname)
getHost mgr ver logger peers = do
let vName = case _versionName ver of
ChainwebVersionName n -> ChainwebVersionName (fromMaybe n (T.stripPrefix "pact5-retro-" n))

nis <- forConcurrently peers $ \p ->
tryAllSynchronous (requestRemoteNodeInfo mgr (_versionName ver) (_peerAddr p) Nothing) >>= \case
tryAllSynchronous (requestRemoteNodeInfo mgr vName (_peerAddr p) Nothing) >>= \case
Right x -> Just x <$ do
logFunctionText logger Info
$ "got remote info from " <> toText (_peerAddr p)
Expand Down
4 changes: 2 additions & 2 deletions src/Chainweb/Chainweb/PruneChainDatabase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pruneAllChains
-> IO ()
pruneAllChains logger rdb v checks = do
now <- getCurrentTimeIntegral
wdb <- initWebBlockHeaderDb rdb v
wdb <- initWebBlockHeaderDb rdb v False
forConcurrently_ (toList $ chainIds v) (pruneChain now wdb)
where
diam = diameter $ chainGraphAt v (maxBound @BlockHeight)
Expand Down Expand Up @@ -311,7 +311,7 @@ fullGc logger rdb v = do

-- Prune a single chain and return the sets of marked payloads
--
pruneChain cid = withBlockHeaderDb rdb v cid $ \cdb -> do
pruneChain cid = withBlockHeaderDb rdb v False cid $ \cdb -> do
let chainLogger = addLabel ("chain", toText cid) logger
chainLogg = logFunctionText chainLogger

Expand Down
4 changes: 2 additions & 2 deletions src/Chainweb/Pact/Backend/Compaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ compactRocksDb logger cwVersion cids minBlockHeight srcDb targetDb = do
log LL.Info "Initializing payload db"
initializePayloadDb cwVersion targetPayloads

srcWbhdb <- initWebBlockHeaderDb srcDb cwVersion
targetWbhdb <- initWebBlockHeaderDb targetDb cwVersion
srcWbhdb <- initWebBlockHeaderDb srcDb cwVersion True
targetWbhdb <- initWebBlockHeaderDb targetDb cwVersion False
forM_ cids $ \cid -> do
let log' = logFunctionText (addChainIdLabel cid logger)
log' LL.Info $ "Starting chain " <> chainIdToText cid
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact/Backend/PactState/GrandHash/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ getLatestCutHeaders :: ()
-> RocksDb
-> IO (WebBlockHeaderDb, HashMap ChainId BlockHeader)
getLatestCutHeaders v rocksDb = do
wbhdb <- initWebBlockHeaderDb rocksDb v
wbhdb <- initWebBlockHeaderDb rocksDb v True
let cutHashes = cutHashesTable rocksDb
latestCutHeaders <- readHighestCutHeaders v (\_ _ -> pure ()) wbhdb cutHashes
pure (wbhdb, latestCutHeaders)
Expand Down
Loading

0 comments on commit 1665f3f

Please sign in to comment.