Skip to content

Commit

Permalink
Remove support for legacy p2p topology format
Browse files Browse the repository at this point in the history
  • Loading branch information
crocodile-dentist committed Oct 4, 2024
1 parent 1b1122d commit 0969a7e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 115 deletions.
120 changes: 31 additions & 89 deletions cardano-node/src/Cardano/Node/Configuration/TopologyP2P.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ where
import Cardano.Node.Configuration.NodeAddress
import Cardano.Node.Configuration.POM (NodeConfiguration (..))
import Cardano.Node.Configuration.Topology (TopologyError (..))
import Cardano.Node.Startup (StartupTrace (..))
import Cardano.Node.Types
import Cardano.Tracing.OrphanInstances.Network ()
import Ouroboros.Network.NodeToNode (PeerAdvertise (..))
Expand All @@ -41,9 +40,7 @@ import Control.Applicative (Alternative (..))
import Control.Exception (IOException)
import qualified Control.Exception as Exception
import Control.Exception.Base (Exception (..))
import "contra-tracer" Control.Tracer (Tracer, traceWith)
import Data.Aeson
import Data.Bifunctor (Bifunctor (..))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Text (Text)
Expand Down Expand Up @@ -198,101 +195,46 @@ instance ToJSON NetworkTopology where
, "bootstrapPeers" .= ntUseBootstrapPeers
]

--
-- Legacy p2p topology file format
--

-- | A newtype wrapper which provides legacy 'FromJSON' instances.
--
newtype Legacy a = Legacy { getLegacy :: a }

instance FromJSON (Legacy a) => FromJSON (Legacy [a]) where
parseJSON = fmap (Legacy . map getLegacy) . parseJSONList

instance FromJSON (Legacy LocalRootPeersGroup) where
parseJSON = withObject "LocalRootPeersGroup" $ \o -> do
hv@(HotValency v) <- o .: "hotValency"
wv <- o .:? "warmValency" .!= WarmValency v
fmap Legacy $ LocalRootPeersGroup
<$> o .: "localRoots"
<*> pure hv
<*> pure wv
<*> o .: "trustable"

instance FromJSON (Legacy LocalRootPeersGroups) where
parseJSON = withObject "LocalRootPeersGroups" $ \o ->
Legacy . LocalRootPeersGroups . getLegacy
<$> o .: "groups"

instance FromJSON (Legacy PublicRootPeers) where
parseJSON = withObject "PublicRootPeers" $ \o ->
Legacy . PublicRootPeers
<$> o .: "publicRoots"

instance FromJSON (Legacy NetworkTopology) where
parseJSON = fmap Legacy
. withObject "NetworkTopology" (\o ->
RealNodeTopology <$> fmap getLegacy (o .: "LocalRoots")
<*> fmap getLegacy (o .: "PublicRoots")
<*> (o .:? "useLedgerAfterSlot" .!= DontUseLedgerPeers)
<*> pure DontUseBootstrapPeers)

-- | Read the `NetworkTopology` configuration from the specified file.
--
readTopologyFile :: Tracer IO (StartupTrace blk)
-> NodeConfiguration -> IO (Either Text NetworkTopology)
readTopologyFile tr nc = do
readTopologyFile :: NodeConfiguration -> IO (Either Text NetworkTopology)
readTopologyFile nc = do
eBs <- Exception.try $ BS.readFile (unTopology $ ncTopologyFile nc)

case eBs of
Left e -> return . Left $ handler e
Right bs ->
let bs' = LBS.fromStrict bs in
(case eitherDecode bs' of
Left err -> Left (handlerJSON err)
Right t
| isValidTrustedPeerConfiguration t -> Right t
| otherwise -> Left handlerBootstrap
)
`combine`
first handlerJSON (eitherDecode bs')

where
combine :: Either Text NetworkTopology
-> Either Text (Legacy NetworkTopology)
-> IO (Either Text NetworkTopology)
combine a b = case (a, b) of
(Right {}, _) -> return a
(_, Right {}) -> traceWith tr NetworkConfigLegacy
>> return (getLegacy <$> b)
(Left _, Left _) -> -- ignore parsing error of legacy format
return a

handler :: IOException -> Text
handler e = Text.pack $ "Cardano.Node.Configuration.Topology.readTopologyFile: "
++ displayException e
handlerJSON :: String -> Text
handlerJSON err = mconcat
[ "Is your topology file formatted correctly? "
, "Expecting P2P Topology file format. "
, "The port and valency fields should be numerical. "
, "If you specified the correct topology file "
, "make sure that you correctly setup EnableP2P "
, "configuration flag. "
, Text.pack err
]
handlerBootstrap :: Text
handlerBootstrap = mconcat
[ "You seem to have not configured any trustable peers. "
, "This is important in order for the node to make progress "
, "in bootstrap mode. Make sure you provide at least one bootstrap peer "
, "source. "
]
return $ case eitherDecode bs' of
Left err -> Left (handlerJSON err)
Right t
| isValidTrustedPeerConfiguration t -> Right t
| otherwise -> Left handlerBootstrap
where
handler :: IOException -> Text
handler e = Text.pack $ "Cardano.Node.Configuration.Topology.readTopologyFile: "
++ displayException e
handlerJSON :: String -> Text
handlerJSON err = mconcat
[ "Is your topology file formatted correctly? "
, "Expecting P2P Topology file format. "
, "The port and valency fields should be numerical. "
, "If you specified the correct topology file "
, "make sure that you correctly setup EnableP2P "
, "configuration flag. "
, Text.pack err
]
handlerBootstrap :: Text
handlerBootstrap = mconcat
[ "You seem to have not configured any trustable peers. "
, "This is important in order for the node to make progress "
, "in bootstrap mode. Make sure you provide at least one bootstrap peer "
, "source. "
]

readTopologyFileOrError :: Tracer IO (StartupTrace blk)
-> NodeConfiguration -> IO NetworkTopology
readTopologyFileOrError tr nc =
readTopologyFile tr nc
readTopologyFileOrError :: NodeConfiguration -> IO NetworkTopology
readTopologyFileOrError nc =
readTopologyFile nc
>>= either (\err -> error $ "Cardano.Node.Configuration.TopologyP2P.readTopologyFile: "
<> Text.unpack err)
pure
Expand Down
6 changes: 3 additions & 3 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ import GHC.Weak (deRefWeak)
import System.Posix.Files
import qualified System.Posix.Signals as Signals
import System.Posix.Types (FileMode)
#else
#else
import System.Win32.File
#endif
import Paths_cardano_node (version)
Expand Down Expand Up @@ -435,7 +435,7 @@ handleSimpleNode blockType runP p2pMode tracers nc onKernel = do
nt@TopologyP2P.RealNodeTopology
{ ntUseLedgerPeers
, ntUseBootstrapPeers
} <- TopologyP2P.readTopologyFileOrError (startupTracer tracers) nc
} <- TopologyP2P.readTopologyFileOrError nc
let (localRoots, publicRoots) = producerAddresses nt
traceWith (startupTracer tracers)
$ NetworkConfig localRoots
Expand Down Expand Up @@ -764,7 +764,7 @@ updateTopologyConfiguration :: Tracer IO (StartupTrace blk)
updateTopologyConfiguration startupTracer nc localRootsVar publicRootsVar useLedgerVar
useBootsrapPeersVar = do
traceWith startupTracer NetworkConfigUpdate
result <- try $ readTopologyFileOrError startupTracer nc
result <- try $ readTopologyFileOrError nc
case result of
Left (FatalError err) ->
traceWith startupTracer
Expand Down
4 changes: 0 additions & 4 deletions cardano-node/src/Cardano/Node/Startup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ data StartupTrace blk =
--
| NetworkConfigUpdateError Text

-- | Legacy topology file format is used.
--
| NetworkConfigLegacy

-- | Log peer-to-peer network configuration, either on startup or when its
-- updated.
--
Expand Down
19 changes: 0 additions & 19 deletions cardano-node/src/Cardano/Node/Tracing/Tracers/Startup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ instance ( Show (BlockNodeToNodeVersion blk)
, "publicRoots" .= toJSON publicRoots
, "useLedgerAfter" .= useLedgerPeers
]
forMachine _dtal NetworkConfigLegacy =
mconcat [ "kind" .= String "NetworkConfigLegacy"
, "message" .= String p2pNetworkConfigLegacyMessage
]
forMachine _dtal NonP2PWarning =
mconcat [ "kind" .= String "NonP2PWarning"
, "message" .= String nonP2PWarningMessage ]
Expand Down Expand Up @@ -313,8 +309,6 @@ instance MetaTrace (StartupTrace blk) where
Namespace [] ["NetworkConfigUpdateError"]
namespaceFor NetworkConfig {} =
Namespace [] ["NetworkConfig"]
namespaceFor NetworkConfigLegacy {} =
Namespace [] ["NetworkConfigLegacy"]
namespaceFor NonP2PWarning {} =
Namespace [] ["NonP2PWarning"]
namespaceFor WarningDevelopmentNodeToNodeVersions {} =
Expand Down Expand Up @@ -367,8 +361,6 @@ instance MetaTrace (StartupTrace blk) where
""
documentFor (Namespace [] ["NetworkConfig"]) = Just
""
documentFor (Namespace [] ["NetworkConfigLegacy"]) = Just
""
documentFor (Namespace [] ["NonP2PWarning"]) = Just
""
documentFor (Namespace [] ["WarningDevelopmentNodeToNodeVersions"]) = Just
Expand Down Expand Up @@ -427,7 +419,6 @@ instance MetaTrace (StartupTrace blk) where
, Namespace [] ["NetworkConfigUpdateUnsupported"]
, Namespace [] ["NetworkConfigUpdateError"]
, Namespace [] ["NetworkConfig"]
, Namespace [] ["NetworkConfigLegacy"]
, Namespace [] ["NonP2PWarning"]
, Namespace [] ["WarningDevelopmentNodeToNodeVersions"]
, Namespace [] ["WarningDevelopmentNodeToClientVersions"]
Expand Down Expand Up @@ -540,7 +531,6 @@ ppStartupInfoTrace (NetworkConfig localRoots publicRoots useLedgerPeers) =
UseLedgerPeers Always ->
"Use ledger peers in any slot."
]
ppStartupInfoTrace NetworkConfigLegacy = p2pNetworkConfigLegacyMessage

ppStartupInfoTrace NonP2PWarning = nonP2PWarningMessage

Expand Down Expand Up @@ -582,15 +572,6 @@ nonP2PWarningMessage =
"You are using legacy networking stack, "
<> "consider upgrading to the p2p network stack."

p2pNetworkConfigLegacyMessage :: Text
p2pNetworkConfigLegacyMessage =
pack
$ intercalate "\n"
[ "You are using legacy p2p topology file format."
, "See https://github.com/intersectmbo/cardano-node/issues/4559"
, "Note that the legacy p2p format will be removed in `1.37` release."
]

-- | Pretty print 'SocketOrSocketInfo'.
--
ppSocketInfo :: Show sock
Expand Down

0 comments on commit 0969a7e

Please sign in to comment.