diff --git a/bench/Chainweb/Pact/Backend/ForkingBench.hs b/bench/Chainweb/Pact/Backend/ForkingBench.hs index cea15553a9..ec69315343 100644 --- a/bench/Chainweb/Pact/Backend/ForkingBench.hs +++ b/bench/Chainweb/Pact/Backend/ForkingBench.hs @@ -340,7 +340,7 @@ withResources trunkLength logLevel f = C.envWithCleanup create destroy unwrap coinAccounts <- newMVar mempty nonceCounter <- newIORef 1 txPerBlock <- newIORef 10 - sqlEnv <- startSqliteDb testVer cid logger (Just tempDir) Nothing False + sqlEnv <- startSqliteDb cid logger tempDir False pactService <- startPact testVer logger blockHeaderDb payloadDb (testMemPoolAccess txPerBlock coinAccounts) sqlEnv mainTrunkBlocks <- diff --git a/chainweb.cabal b/chainweb.cabal index 32ba87c35c..85d8100635 100644 --- a/chainweb.cabal +++ b/chainweb.cabal @@ -193,7 +193,6 @@ library , Chainweb.Miner.RestAPI , Chainweb.Miner.RestAPI.Client , Chainweb.Miner.RestAPI.Server - , Chainweb.NodeId , Chainweb.NodeVersion , Chainweb.Payload , Chainweb.Payload.PayloadStore diff --git a/node/ChainwebNode.hs b/node/ChainwebNode.hs index ab4cde0324..e0aafbfe73 100644 --- a/node/ChainwebNode.hs +++ b/node/ChainwebNode.hs @@ -173,6 +173,20 @@ pChainwebNodeConfiguration = id % long "reset-chain-databases" <> help "Reset the chain databases for all chains on startup" +getRocksDbDir :: ChainwebNodeConfiguration -> IO FilePath +getRocksDbDir conf = (<> "/rocksDb") <$> getDbBaseDir conf + +getPactDbDir :: ChainwebNodeConfiguration -> IO FilePath +getPactDbDir conf = (<> "/sqlite") <$> getDbBaseDir conf + +getDbBaseDir :: ChainwebNodeConfiguration -> IO FilePath +getDbBaseDir conf = case _nodeConfigDatabaseDirectory conf of + Nothing -> getXdgDirectory XdgData + $ "chainweb-node/" <> sshow v <> "/0" + Just d -> return d + where + v = _configChainwebVersion $ _nodeConfigChainweb conf + -- -------------------------------------------------------------------------- -- -- Monitors @@ -300,11 +314,14 @@ runQueueMonitor logger cutDb = L.withLoggerLabel ("component", "queue-monitor") node :: Logger logger => ChainwebNodeConfiguration -> logger -> IO () node conf logger = do - rocksDbDir <- getRocksDbDir - when (_nodeConfigResetChainDbs conf) $ destroyRocksDb rocksDbDir + migrateDbDirectory logger conf + dbBaseDir <- getDbBaseDir conf + when (_nodeConfigResetChainDbs conf) $ removeDirectoryRecursive dbBaseDir + rocksDbDir <- getRocksDbDir conf + pactDbDir <- getPactDbDir conf withRocksDb rocksDbDir $ \rocksDb -> do logFunctionText logger Info $ "opened rocksdb in directory " <> sshow rocksDbDir - withChainweb cwConf logger rocksDb (_nodeConfigDatabaseDirectory conf) (_nodeConfigResetChainDbs conf) $ \cw -> mapConcurrently_ id + withChainweb cwConf logger rocksDb pactDbDir (_nodeConfigResetChainDbs conf) $ \cw -> mapConcurrently_ id [ runChainweb cw -- we should probably push 'onReady' deeper here but this should be ok , runCutMonitor (_chainwebLogger cw) (_cutResCutDb $ _chainwebCutResources cw) @@ -315,14 +332,10 @@ node conf logger = do ] where cwConf = _nodeConfigChainweb conf - nodeText = T.unpack (toText (_configNodeId cwConf)) - v = _configChainwebVersion cwConf - getRocksDbDir = case _nodeConfigDatabaseDirectory conf of - Nothing -> getXdgDirectory XdgData - $ "chainweb-node/" <> sshow v <> "/" <> nodeText <> "/rocksDb" - Just d -> return d amberdataConfig = _logConfigAmberdataBackend . _nodeConfigLog + + withNodeLogger :: LogConfig -> ChainwebVersion @@ -501,3 +514,70 @@ main = do node conf logger where timeFormat = iso8601DateFormat (Just "%H:%M:%SZ") + +-- -------------------------------------------------------------------------- -- +-- Database Director Migration +-- +-- Legacy default locations: +-- +-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/$NODEID/rocksDb` +-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/$NODEID/sqlite` +-- +-- New default locations: +-- +-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/0/rocksDb` +-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/0/sqlite` +-- +-- Legacy custom locations: +-- +-- `$CUSTOM_PATH/rocksDb` +-- `$CUSTOM_PATH/rocksDbsqlite` +-- +-- New custom locations: +-- +-- `$CUSTOM_PATH/rocksDb` +-- `$CUSTOM_PATH/sqlite` +-- +-- Migration scenarios: +-- +-- 1. Custom location configured (and directory exists): +-- +-- * Log warning +-- * `mv "$CUSTOM_PATH/rocksDbslqite "$CUSTOM_PATH/sqlite"` +-- * fail if target directory already exists +-- +-- 2. No custom location configured: +-- +-- * Log warning if `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/[1-9]*` exists +-- +-- +-- Migration code can be removed in the next version. We don't need to support +-- longer backwards compatibility, because in those situations it will be faster +-- and more convenient to start over with a fresh db. +-- + +migrateDbDirectory :: Logger logger => logger -> ChainwebNodeConfiguration -> IO () +migrateDbDirectory logger config = case _nodeConfigDatabaseDirectory config of + Just custom -> do + let legacyCustomPact = custom <> "sqlite" + newCustomPact <- getPactDbDir config + whenM (doesDirectoryExist legacyCustomPact) $ do + logg Warn + $ "Updated database directory layout for new chainweb version" + <> ". Old pact db location: " <> T.pack legacyCustomPact + <> ". New pact db location: " <> T.pack newCustomPact + targetExists <- doesDirectoryExist newCustomPact + if targetExists + then logg Error + $ "Can't move old pact database to new location because the database already exists" + <> ". The database at the new location will be used." + else renameDirectory legacyCustomPact newCustomPact + Nothing -> do + defDir <- getXdgDirectory XdgData $ "chainweb-node/" <> sshow v + dirs <- getDirectoryContents defDir + forM_ (filter (/= defDir <> "/0") dirs) $ \i -> + logg Warn $ "ignoring existing database directory " <> T.pack i + where + logg = logFunctionText logger + v = _configChainwebVersion $ _nodeConfigChainweb config + diff --git a/src/Chainweb/Chainweb.hs b/src/Chainweb/Chainweb.hs index d7e8a0562f..4de5032aa9 100644 --- a/src/Chainweb/Chainweb.hs +++ b/src/Chainweb/Chainweb.hs @@ -50,7 +50,6 @@ module Chainweb.Chainweb -- * Chainweb Configuration , ChainwebConfiguration(..) -, configNodeId , configChainwebVersion , configMining , configHeaderStream @@ -127,6 +126,7 @@ import Control.Error.Util (note) import Control.Lens hiding ((.=), (<.>)) import Control.Monad import Control.Monad.Catch (throwM) +import Control.Monad.Writer import Data.Bifunctor (second) import Data.CAS (casLookupM) @@ -135,7 +135,6 @@ import Data.Function (on) import qualified Data.HashMap.Strict as HM import Data.List (isPrefixOf, sortBy) import Data.Maybe -import Data.Monoid import qualified Data.Text as T import Data.These (These(..)) import Data.Tuple.Strict (T2(..)) @@ -178,7 +177,6 @@ import qualified Chainweb.Mempool.InMemTypes as Mempool import qualified Chainweb.Mempool.Mempool as Mempool import Chainweb.Mempool.P2pConfig import Chainweb.Miner.Config -import Chainweb.NodeId import Chainweb.Pact.RestAPI.Server (PactServerData) import Chainweb.Pact.Service.Types (PactServiceConfig(..)) import Chainweb.Pact.Types (defaultReorgLimit) @@ -304,7 +302,8 @@ defaultCutConfig = CutConfig data ChainwebConfiguration = ChainwebConfiguration { _configChainwebVersion :: !ChainwebVersion - , _configNodeId :: !NodeId + , _configNodeIdDeprecated :: !Int + -- ^ Deprecated, won't show up in --print-config , _configCuts :: !CutConfig , _configMining :: !MiningConfig , _configHeaderStream :: !Bool @@ -328,14 +327,18 @@ instance HasChainwebVersion ChainwebConfiguration where _chainwebVersion = _configChainwebVersion {-# INLINE _chainwebVersion #-} -validateChainwebConfiguration :: ConfigValidation ChainwebConfiguration l +validateChainwebConfiguration :: ConfigValidation ChainwebConfiguration [] validateChainwebConfiguration c = do validateMinerConfig (_configMining c) + unless (_configNodeIdDeprecated c == (-1)) $ tell + [ "Usage NodeId is deprecated. This option will be removed in a future version of chainweb-node" + , "The value of NodeId is ignored by chainweb-node. In particular the database path will not depend on it" + ] defaultChainwebConfiguration :: ChainwebVersion -> ChainwebConfiguration defaultChainwebConfiguration v = ChainwebConfiguration { _configChainwebVersion = v - , _configNodeId = NodeId 0 -- FIXME + , _configNodeIdDeprecated = (-1) , _configCuts = defaultCutConfig , _configMining = defaultMining , _configHeaderStream = False @@ -355,7 +358,6 @@ defaultChainwebConfiguration v = ChainwebConfiguration instance ToJSON ChainwebConfiguration where toJSON o = object [ "chainwebVersion" .= _configChainwebVersion o - , "nodeId" .= _configNodeId o , "cuts" .= _configCuts o , "mining" .= _configMining o , "headerStream" .= _configHeaderStream o @@ -375,7 +377,7 @@ instance ToJSON ChainwebConfiguration where instance FromJSON (ChainwebConfiguration -> ChainwebConfiguration) where parseJSON = withObject "ChainwebConfig" $ \o -> id <$< configChainwebVersion ..: "chainwebVersion" % o - <*< configNodeId ..: "nodeId" % o + <*< configNodeIdDeprecated ..: "nodeId" % o <*< configCuts %.: "cuts" % o <*< configMining %.: "mining" % o <*< configHeaderStream ..: "headerStream" % o @@ -397,10 +399,10 @@ pChainwebConfiguration = id % long "chainweb-version" <> short 'v' <> help "the chainweb version that this node is using" - <*< configNodeId .:: textOption + <*< configNodeIdDeprecated .:: option auto % long "node-id" <> short 'i' - <> help "unique id of the node that is used as miner id in new blocks" + <> help "DEPRECATED. The value is ignored" <*< configHeaderStream .:: boolOption_ % long "header-stream" <> help "whether to enable an endpoint for streaming block updates" @@ -470,19 +472,19 @@ withChainweb => ChainwebConfiguration -> logger -> RocksDb - -> Maybe FilePath + -> FilePath + -- ^ Pact database directory -> Bool -> (forall cas' . PayloadCasLookup cas' => Chainweb logger cas' -> IO a) -> IO a -withChainweb c logger rocksDb dbDir resetDb inner = +withChainweb c logger rocksDb pactDbDir resetDb inner = withPeerResources v (view configP2p conf) logger $ \logger' peer -> withChainwebInternal (set configP2p (_peerResConfig peer) conf) logger' peer rocksDb - dbDir - (Just (_configNodeId c)) + pactDbDir resetDb inner where @@ -565,20 +567,19 @@ withChainwebInternal -> logger -> PeerResources logger -> RocksDb - -> Maybe FilePath - -> Maybe NodeId + -> FilePath -> Bool -> (forall cas' . PayloadCasLookup cas' => Chainweb logger cas' -> IO a) -> IO a -withChainwebInternal conf logger peer rocksDb dbDir nodeid resetDb inner = do +withChainwebInternal conf logger peer rocksDb pactDbDir resetDb inner = do initializePayloadDb v payloadDb concurrentWith -- initialize chains concurrently (\cid -> do let mcfg = validatingMempoolConfig cid v (_configBlockGasLimit conf) withChainResources v cid rocksDb peer (chainLogger cid) - mcfg payloadDb prune dbDir nodeid - pactConfig) + mcfg payloadDb prune pactDbDir pactConfig + ) -- initialize global resources after all chain resources are initialized (\cs -> global (HM.fromList $ zip cidsList cs)) diff --git a/src/Chainweb/Chainweb/ChainResources.hs b/src/Chainweb/Chainweb/ChainResources.hs index 35f20264e3..4599948981 100644 --- a/src/Chainweb/Chainweb/ChainResources.hs +++ b/src/Chainweb/Chainweb/ChainResources.hs @@ -64,7 +64,6 @@ import Chainweb.Mempool.Mempool (MempoolBackend) import qualified Chainweb.Mempool.Mempool as Mempool import Chainweb.Mempool.P2pConfig import qualified Chainweb.Mempool.RestAPI.Client as MPC -import Chainweb.NodeId import Chainweb.Pact.Service.PactInProcApi import Chainweb.Pact.Service.Types import Chainweb.Payload.PayloadStore @@ -117,21 +116,20 @@ withChainResources -> PayloadDb cas -> Bool -- ^ whether to prune the chain database - -> Maybe FilePath + -> FilePath -- ^ database directory for checkpointer - -> Maybe NodeId -> PactServiceConfig -> (ChainResources logger -> IO a) -> IO a withChainResources - v cid rdb peer logger mempoolCfg0 payloadDb prune dbDir nodeid pactConfig inner = + v cid rdb peer logger mempoolCfg0 payloadDb prune pactDbDir pactConfig 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 (setComponent "pact" logger) mpc cdb - payloadDb dbDir nodeid pactConfig $ \requestQ -> do + payloadDb pactDbDir pactConfig $ \requestQ -> do -- prune block header db when prune $ do logg Info "start pruning block header database" diff --git a/src/Chainweb/NodeId.hs b/src/Chainweb/NodeId.hs deleted file mode 100644 index 7d158409bd..0000000000 --- a/src/Chainweb/NodeId.hs +++ /dev/null @@ -1,83 +0,0 @@ -{-# LANGUAGE DeriveAnyClass #-} -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE DerivingStrategies #-} -{-# LANGUAGE GADTs #-} -{-# LANGUAGE GeneralizedNewtypeDeriving #-} -{-# LANGUAGE MultiParamTypeClasses #-} -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TemplateHaskell #-} -{-# LANGUAGE TypeApplications #-} -{-# LANGUAGE TypeFamilies #-} -{-# LANGUAGE TypeInType #-} - --- | --- Module: Chainweb.NodeId --- Copyright: Copyright © 2018 Kadena LLC. --- License: MIT --- Maintainer: Lars Kuhtz --- Stability: experimental --- --- Node Id of a P2P node. This module is deprecated and will be removed --- in a future version of chainweb. --- -module Chainweb.NodeId -( --- * Chainweb Node Id - NodeId(..) -, nodeIdId -, encodeNodeId -, decodeNodeId -, nodeIdToText -, nodeIdFromText -) where - -import Control.DeepSeq -import Control.Lens -import Control.Monad ((<$!>)) -import Control.Monad.Catch - -import Data.Aeson -import Data.Bytes.Get -import Data.Bytes.Put -import Data.Hashable -import qualified Data.Text as T -import Data.Word - -import GHC.Generics - --- internal imports - -import Chainweb.Utils - --- -------------------------------------------------------------------------- -- --- Chainweb NodeId - -newtype NodeId = NodeId - { _nodeIdId :: Word64 - } - deriving stock (Show, Read, Eq, Ord, Generic) - deriving anyclass (Hashable, NFData, FromJSON, ToJSON) - -makeLenses ''NodeId - -encodeNodeId :: MonadPut m => NodeId -> m () -encodeNodeId (NodeId i) = putWord64le i -{-# INLINE encodeNodeId #-} - -decodeNodeId :: MonadGet m => m NodeId -decodeNodeId = NodeId <$!> getWord64le -{-# INLINE decodeNodeId #-} - -nodeIdToText :: NodeId -> T.Text -nodeIdToText (NodeId i) = sshow i -{-# INLINE nodeIdToText #-} - -nodeIdFromText :: MonadThrow m => T.Text -> m NodeId -nodeIdFromText = fmap NodeId . treadM - -instance HasTextRepresentation NodeId where - toText = nodeIdToText - {-# INLINE toText #-} - fromText = nodeIdFromText - {-# INLINE fromText #-} diff --git a/src/Chainweb/Pact/Backend/Utils.hs b/src/Chainweb/Pact/Backend/Utils.hs index ca0e6128a7..99f2dc105b 100644 --- a/src/Chainweb/Pact/Backend/Utils.hs +++ b/src/Chainweb/Pact/Backend/Utils.hs @@ -82,7 +82,6 @@ import Pact.Types.Util (AsString(..)) -- chainweb import Chainweb.Logger -import Chainweb.NodeId import Chainweb.Pact.Backend.SQLite.DirectV2 import Chainweb.Pact.Backend.Types import Chainweb.Pact.Service.Types @@ -239,63 +238,44 @@ execMulti db q rows = do withSqliteDb :: Logger logger - => ChainwebVersion - -> ChainId + => ChainId -> logger - -> Maybe FilePath - -> Maybe NodeId + -> FilePath -> Bool -> (SQLiteEnv -> IO a) -> IO a -withSqliteDb ver cid logger dbDir nodeid resetDb = bracket - (startSqliteDb ver cid logger dbDir nodeid resetDb) +withSqliteDb cid logger dbDir resetDb = bracket + (startSqliteDb cid logger dbDir resetDb) stopSqliteDb startSqliteDb :: Logger logger - => ChainwebVersion - -> ChainId + => ChainId -> logger - -> Maybe FilePath - -> Maybe NodeId + -> FilePath -> Bool -> IO SQLiteEnv -startSqliteDb ver cid logger dbDir nodeid doResetDb = do - sqlitedir <- getSqliteDir - when doResetDb $ resetDb sqlitedir - createDirectoryIfMissing True sqlitedir +startSqliteDb cid logger dbDir doResetDb = do + when doResetDb $ resetDb + createDirectoryIfMissing True dbDir textLog Info $ mconcat [ "opened sqlitedb for " , sshow cid , " in directory " - , sshow sqlitedir + , sshow dbDir ] - let sqlitefile = getSqliteFile sqlitedir - textLog Info $ "opening sqlitedb named " <> pack sqlitefile - openSQLiteConnection sqlitefile chainwebPragmas + textLog Info $ "opening sqlitedb named " <> pack sqliteFile + openSQLiteConnection sqliteFile chainwebPragmas where textLog = logFunctionText logger - - resetDb sqlitedir = do - exist <- doesDirectoryExist sqlitedir - when exist $ removeDirectoryRecursive sqlitedir - - getSqliteFile dir = mconcat - [ dir + resetDb = removeDirectoryRecursive dbDir + sqliteFile = mconcat + [ dbDir , "/pact-v1-chain-" , unpack (chainIdToText cid) , ".sqlite" ] - getSqliteDir = case dbDir of - Nothing -> getXdgDirectory XdgData $ mconcat - [ "chainweb-node/" - , show ver - , maybe mempty (("/" <>) . unpack . toText) nodeid - , "/sqlite" - ] - Just d -> return (d <> "sqlite") - stopSqliteDb :: SQLiteEnv -> IO () stopSqliteDb = closeSQLiteConnection diff --git a/src/Chainweb/Pact/Service/PactInProcApi.hs b/src/Chainweb/Pact/Service/PactInProcApi.hs index 81e84037f5..d2094a6ce6 100644 --- a/src/Chainweb/Pact/Service/PactInProcApi.hs +++ b/src/Chainweb/Pact/Service/PactInProcApi.hs @@ -42,7 +42,6 @@ import Chainweb.ChainId import Chainweb.Logger import Chainweb.Mempool.Consensus import Chainweb.Mempool.Mempool -import Chainweb.NodeId import Chainweb.Pact.Backend.Types import Chainweb.Pact.Backend.Utils import Chainweb.Pact.Service.Types @@ -65,13 +64,12 @@ withPactService -> MempoolConsensus -> BlockHeaderDb -> PayloadDb cas - -> Maybe FilePath - -> Maybe NodeId + -> FilePath -> PactServiceConfig -> (PactQueue -> IO a) -> IO a -withPactService ver cid logger mpc bhdb pdb dbDir nodeid config action = - withSqliteDb ver cid logger dbDir nodeid (_pactResetDb config) $ \sqlenv -> +withPactService ver cid logger mpc bhdb pdb pactDbDir config action = + withSqliteDb cid logger pactDbDir (_pactResetDb config) $ \sqlenv -> withPactService' ver cid logger mpa bhdb pdb sqlenv config action where mpa = pactMemPoolAccess mpc logger diff --git a/src/Chainweb/Pact/Service/Types.hs b/src/Chainweb/Pact/Service/Types.hs index de3565829a..2ee35553f6 100644 --- a/src/Chainweb/Pact/Service/Types.hs +++ b/src/Chainweb/Pact/Service/Types.hs @@ -55,16 +55,16 @@ import Chainweb.Version -- | Externally-injected PactService properties. data PactServiceConfig = PactServiceConfig - { _pactReorgLimit :: Natural + { _pactReorgLimit :: !Natural -- ^ Maximum allowed reorg depth, implemented as a rewind limit in validate. New block -- hardcodes this to 8 currently. - , _pactRevalidate :: Bool + , _pactRevalidate :: !Bool -- ^ Re-validate payload hashes during transaction replay - , _pactAllowReadsInLocal :: Bool + , _pactAllowReadsInLocal :: !Bool -- ^ Allow direct database reads in local mode - , _pactQueueSize :: Natural + , _pactQueueSize :: !Natural -- ^ max size of pact internal queue. - , _pactResetDb :: Bool + , _pactResetDb :: !Bool -- ^ blow away pact dbs } deriving (Eq,Show) diff --git a/test/Chainweb/Test/MultiNode.hs b/test/Chainweb/Test/MultiNode.hs index d5a16b9669..1a8cff6126 100644 --- a/test/Chainweb/Test/MultiNode.hs +++ b/test/Chainweb/Test/MultiNode.hs @@ -62,6 +62,7 @@ import Numeric.Natural import qualified Streaming.Prelude as S +import System.IO.Temp import System.LogLevel import System.Timeout @@ -80,7 +81,6 @@ import Chainweb.Cut import Chainweb.CutDB import Chainweb.Graph import Chainweb.Logger -import Chainweb.NodeId import Chainweb.Test.Utils import Chainweb.Time (Seconds(..)) import Chainweb.Utils @@ -113,10 +113,8 @@ multiConfig :: ChainwebVersion -> Natural -- ^ number of nodes - -> NodeId - -- ^ NodeId -> ChainwebConfiguration -multiConfig v n nid = config v n nid +multiConfig v n = config v n & set (configP2p . p2pConfigSessionTimeout) 20 -- Use short sessions to cover session timeouts and setup logic in the -- test. @@ -140,22 +138,23 @@ multiNode -> MVar PeerInfo -> ChainwebConfiguration -> RocksDb + -> Int + -- ^ Unique node id. Node id 0 is used for the bootstrap node -> IO () -multiNode loglevel write stateVar bootstrapPeerInfoVar conf rdb = do - withChainweb conf logger nodeRocksDb Nothing False $ \cw -> do - - -- If this is the bootstrap node we extract the port number and - -- publish via an MVar. - when (nid == NodeId 0) $ putMVar bootstrapPeerInfoVar - $ view (chainwebPeer . peerResPeer . peerInfo) cw - - runChainweb cw `finally` do - logFunctionText logger Info "write sample data" - sample cw - logFunctionText logger Info "shutdown node" +multiNode loglevel write stateVar bootstrapPeerInfoVar conf rdb nid = do + withSystemTempDirectory "multiNode-pact-db" $ \pactDbDir -> + withChainweb conf logger nodeRocksDb pactDbDir False $ \cw -> do + + -- If this is the bootstrap node we extract the port number and + -- publish via an MVar. + when (nid == 0) $ putMVar bootstrapPeerInfoVar + $ view (chainwebPeer . peerResPeer . peerInfo) cw + + runChainweb cw `finally` do + logFunctionText logger Info "write sample data" + sample cw + logFunctionText logger Info "shutdown node" where - nid = _configNodeId conf - logger :: GenericLogger logger = addLabel ("node", toText nid) $ genericLogger loglevel write @@ -197,14 +196,14 @@ runNodes loglevel write stateVar v n = forConcurrently_ [0 .. int n - 1] $ \i -> do threadDelay (500_000 * int i) - let baseConf = multiConfig v n (NodeId i) + let baseConf = multiConfig v n conf <- if | i == 0 -> return $ bootstrapConfig baseConf | otherwise -> setBootstrapPeerInfo <$> readMVar bootstrapPortVar <*> pure baseConf - multiNode loglevel write stateVar bootstrapPortVar conf rdb + multiNode loglevel write stateVar bootstrapPortVar conf rdb i runNodesForSeconds :: LogLevel @@ -291,7 +290,8 @@ data ConsensusState = ConsensusState -- ^ for short tests this is fine. For larger test runs we should -- use HyperLogLog+ - , _stateCutMap :: !(HM.HashMap NodeId Cut) + , _stateCutMap :: !(HM.HashMap Int Cut) + -- ^ Node Id map , _stateChainwebVersion :: !ChainwebVersion } deriving (Show, Generic, NFData) @@ -304,7 +304,8 @@ emptyConsensusState :: ChainwebVersion -> ConsensusState emptyConsensusState v = ConsensusState mempty mempty v sampleConsensusState - :: NodeId + :: Int + -- ^ node Id -> WebBlockHeaderDb -> CutDb cas -> ConsensusState diff --git a/test/Chainweb/Test/Orphans/Internal.hs b/test/Chainweb/Test/Orphans/Internal.hs index d5030bcd8b..dae28bda0f 100644 --- a/test/Chainweb/Test/Orphans/Internal.hs +++ b/test/Chainweb/Test/Orphans/Internal.hs @@ -52,7 +52,6 @@ import Chainweb.Cut.Create import Chainweb.Difficulty import Chainweb.Graph import Chainweb.MerkleLogHash -import Chainweb.NodeId import Chainweb.Payload import Chainweb.PowHash import Chainweb.RestAPI.NetworkID @@ -105,11 +104,6 @@ instance Arbitrary MerkleLogHash where arbitrary = unsafeMerkleLogHash . B.pack <$> vector (int merkleLogHashBytesCount) --- Deprecated --- -instance Arbitrary NodeId where - arbitrary = NodeId <$> arbitrary - -- -------------------------------------------------------------------------- -- -- POW diff --git a/test/Chainweb/Test/Pact/ModuleCacheOnRestart.hs b/test/Chainweb/Test/Pact/ModuleCacheOnRestart.hs index 0498ab2d71..c71e1e6495 100644 --- a/test/Chainweb/Test/Pact/ModuleCacheOnRestart.hs +++ b/test/Chainweb/Test/Pact/ModuleCacheOnRestart.hs @@ -142,7 +142,7 @@ withPact' bdbio iodir r ctest toTestTree = bhdb <- getWebBlockHeaderDb (_bdbWebBlockHeaderDb bdb) testChainId let pdb = _bdbPayloadDb bdb dir <- iodir - sqlEnv <- startSqliteDb testVer testChainId logger (Just dir) Nothing False + sqlEnv <- startSqliteDb testChainId logger dir False return $ (sqlEnv,) $ \(ps,cacheTest) -> do T2 _ pstate <- initPactService' testVer testChainId logger bhdb pdb sqlEnv defaultPactServiceConfig ps diff --git a/test/Chainweb/Test/Pact/Utils.hs b/test/Chainweb/Test/Pact/Utils.hs index 25663e6f24..753de5be87 100644 --- a/test/Chainweb/Test/Pact/Utils.hs +++ b/test/Chainweb/Test/Pact/Utils.hs @@ -613,7 +613,7 @@ withPactTestBlockDb version cid logLevel mempoolIO pactConfig f = mempool <- mempoolIO bhdb <- getWebBlockHeaderDb (_bdbWebBlockHeaderDb bdb) cid let pdb = _bdbPayloadDb bdb - sqlEnv <- startSqliteDb version cid logger (Just dir) Nothing False + sqlEnv <- startSqliteDb cid logger dir False a <- async $ runForever (\_ _ -> return ()) "Chainweb.Test.Pact.Utils.withPactTestBlockDb" $ initPactService version cid logger reqQ mempool bhdb pdb sqlEnv pactConfig return (a, sqlEnv, (reqQ,bdb)) diff --git a/test/Chainweb/Test/Roundtrips.hs b/test/Chainweb/Test/Roundtrips.hs index 1256298b09..71bfb01439 100644 --- a/test/Chainweb/Test/Roundtrips.hs +++ b/test/Chainweb/Test/Roundtrips.hs @@ -36,7 +36,6 @@ import Chainweb.Cut.Create import Chainweb.Difficulty import Chainweb.HostAddress import Chainweb.MerkleLogHash -import Chainweb.NodeId import Chainweb.Payload import Chainweb.PowHash import Chainweb.RestAPI.NetworkID @@ -73,8 +72,6 @@ encodeDecodeTests = testGroup "Encode-Decode roundtrips" $ prop_encodeDecodeRoundtrip decodeChainwebVersion encodeChainwebVersion , testProperty "ChainId" $ prop_encodeDecodeRoundtrip decodeChainId encodeChainId - , testProperty "NodeId" - $ prop_encodeDecodeRoundtrip decodeNodeId encodeNodeId , testProperty "MerkleLogHash" $ prop_encodeDecodeRoundtrip decodeMerkleLogHash encodeMerkleLogHash , testProperty "BlockHash" @@ -137,7 +134,6 @@ jsonTestCases f = , testProperty "TimeSpan Micros" $ f @(TimeSpan Micros) , testProperty "Seconds" $ f @Seconds , testProperty "ChainId" $ f @ChainId - , testProperty "NodeId" $ f @NodeId , testProperty "ChainwebVersion" $ f @ChainwebVersion , testProperty "Nonce" $ f @Nonce , testProperty "HashDifficulty" $ f @HashDifficulty @@ -215,7 +211,6 @@ showReadTestCases f = , testProperty "Either String Int" $ f @(Either String Int) , testProperty "Text" $ f @T.Text , testProperty "ChainId" $ f @ChainId - , testProperty "NodeId" $ f @NodeId ] showReadTests :: TestTree @@ -249,7 +244,6 @@ hasTextRepresentationTests = testGroup "HasTextRepresentation roundtrips" [ testProperty "ChainwebVersion" $ prop_iso' @_ @ChainwebVersion fromText toText , testProperty "ChainwebVersion" $ prop_iso' @_ @ChainwebVersion eitherFromText toText , testProperty "ChainId" $ prop_iso' @_ @ChainId fromText toText - , testProperty "NodeId" $ prop_iso' @_ @NodeId fromText toText , testProperty "BlockHash" $ prop_iso' @_ @BlockHash fromText toText , testProperty "Seconds" $ prop_iso' @_ @Seconds fromText toText , testProperty "Hostname" $ prop_iso' @_ @Hostname fromText toText diff --git a/test/Chainweb/Test/Utils.hs b/test/Chainweb/Test/Utils.hs index b5f4781f6a..39bc0f356a 100644 --- a/test/Chainweb/Test/Utils.hs +++ b/test/Chainweb/Test/Utils.hs @@ -197,7 +197,6 @@ import Chainweb.Logger import Chainweb.Mempool.Mempool (MempoolBackend(..), TransactionHash(..)) import Chainweb.Miner.Config import Chainweb.Miner.Pact -import Chainweb.NodeId import Chainweb.Payload.PayloadStore import Chainweb.RestAPI import Chainweb.RestAPI.NetworkID @@ -925,13 +924,13 @@ runTestNodes runTestNodes label rdb loglevel ver n portMVar = forConcurrently_ [0 .. int n - 1] $ \i -> do threadDelay (1000 * int i) - let baseConf = config ver n (NodeId i) + let baseConf = config ver n conf <- if | i == 0 -> return $ bootstrapConfig baseConf | otherwise -> setBootstrapPeerInfo <$> readMVar portMVar <*> pure baseConf - node label rdb loglevel portMVar conf + node label rdb loglevel portMVar conf i node :: B.ByteString @@ -939,15 +938,16 @@ node -> LogLevel -> MVar PeerInfo -> ChainwebConfiguration + -> Int + -- ^ Unique Node Id. The node id 0 is used for the bootstrap node -> IO () -node label rdb loglevel peerInfoVar conf = do +node label rdb loglevel peerInfoVar conf nid = do rocksDb <- testRocksDb (label <> T.encodeUtf8 (toText nid)) rdb - Extra.withTempDir $ \dir -> withChainweb conf logger rocksDb (Just dir) False $ \cw -> do + Extra.withTempDir $ \dir -> withChainweb conf logger rocksDb dir False $ \cw -> do -- If this is the bootstrap node we extract the port number and publish via an MVar. - when (nid == NodeId 0) $ do - let bootStrapInfo = view (chainwebPeer . peerResPeer . peerInfo) cw - putMVar peerInfoVar bootStrapInfo + when (nid == 0) $ + putMVar peerInfoVar $! view (chainwebPeer . peerResPeer . peerInfo) cw poisonDeadBeef cw runChainweb cw `finally` do @@ -955,9 +955,8 @@ node label rdb loglevel peerInfoVar conf = do logFunctionText logger Info "shutdown node" return () where - nid = _configNodeId conf logger :: GenericLogger - logger = addLabel ("node", toText nid) $ genericLogger loglevel print + logger = addLabel ("node", sshow nid) $ genericLogger loglevel print poisonDeadBeef cw = mapM_ poison crs where @@ -970,10 +969,8 @@ deadbeef = TransactionHash "deadbeefdeadbeefdeadbeefdeadbeef" config :: ChainwebVersion -> Natural - -> NodeId -> ChainwebConfiguration -config ver n nid = defaultChainwebConfiguration ver - & set configNodeId nid +config ver n = defaultChainwebConfiguration ver & set (configP2p . p2pConfigPeer . peerConfigHost) host & set (configP2p . p2pConfigPeer . peerConfigInterface) interface & set (configP2p . p2pConfigKnownPeers) mempty diff --git a/tools/ea/Ea.hs b/tools/ea/Ea.hs index 60d9724bda..70cc464171 100644 --- a/tools/ea/Ea.hs +++ b/tools/ea/Ea.hs @@ -42,6 +42,7 @@ import qualified Data.Yaml as Yaml import Ea.Genesis +import System.IO.Temp import System.LogLevel (LogLevel(..)) import Text.Printf @@ -163,15 +164,16 @@ genPayloadModule' v tag cwTxs = withBlockHeaderDb rocks v cid $ \bhdb -> do let logger = genericLogger Warn TIO.putStrLn pdb <- newPayloadDb - T2 payloadWO _ <- withSqliteDb v cid logger Nothing Nothing False $ \env -> - initPactService' v cid logger bhdb pdb env defaultPactServiceConfig $ - execNewGenesisBlock noMiner (V.fromList cwTxs) + withSystemTempDirectory "ea-pact-db" $ \pactDbDir -> do + T2 payloadWO _ <- withSqliteDb cid logger pactDbDir False $ \env -> + initPactService' v cid logger bhdb pdb env defaultPactServiceConfig $ + execNewGenesisBlock noMiner (V.fromList cwTxs) - let payloadYaml = TE.decodeUtf8 $ Yaml.encode payloadWO - modl = T.unlines $ startModule tag <> [payloadYaml] <> endModule - fileName = "src/Chainweb/BlockHeader/Genesis/" <> tag <> "Payload.hs" + let payloadYaml = TE.decodeUtf8 $ Yaml.encode payloadWO + modl = T.unlines $ startModule tag <> [payloadYaml] <> endModule + fileName = "src/Chainweb/BlockHeader/Genesis/" <> tag <> "Payload.hs" - TIO.writeFile (T.unpack fileName) modl + TIO.writeFile (T.unpack fileName) modl where cid = someChainId v