-
Notifications
You must be signed in to change notification settings - Fork 108
Make ChainBackend a monad #2503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
locallycompact
wants to merge
2
commits into
master
Choose a base branch
from
lc/chain-backend-monad
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ import Hydra.Chain.Direct (DirectBackend (..)) | |
| import Hydra.Cluster.Faucet (delayBF) | ||
| import Hydra.Cluster.Fixture (KnownNetwork (..), toNetworkId) | ||
| import Hydra.Cluster.Util (readConfigFile) | ||
| import Hydra.Options (BlockfrostOptions (..), DirectOptions (..), defaultBlockfrostOptions) | ||
| import Hydra.Options (BlockfrostOptions (..), ChainBackendOptions (..), DirectOptions (..), defaultBlockfrostOptions) | ||
| import Network.HTTP.Simple (getResponseBody, httpBS, parseRequestThrow) | ||
| import System.Directory ( | ||
| createDirectoryIfMissing, | ||
|
|
@@ -140,7 +140,7 @@ getCardanoNodeVersion = | |
| -- | Tries to find an communicate with an existing cardano-node running in given | ||
| -- work directory. NOTE: This is using the default node socket name as defined | ||
| -- by 'defaultCardanoNodeArgs'. | ||
| findRunningCardanoNode :: Tracer IO NodeLog -> FilePath -> KnownNetwork -> IO (Maybe (NominalDiffTime, DirectBackend)) | ||
| findRunningCardanoNode :: Tracer IO NodeLog -> FilePath -> KnownNetwork -> IO (Maybe (NominalDiffTime, ChainBackendOptions)) | ||
| findRunningCardanoNode tracer workDir knownNetwork = do | ||
| findRunningCardanoNode' tracer knownNetworkId socketPath | ||
| where | ||
|
|
@@ -152,14 +152,15 @@ findRunningCardanoNode tracer workDir knownNetwork = do | |
|
|
||
| -- | Tries to find an communicate with an existing cardano-node running in given | ||
| -- network id and socket path. | ||
| findRunningCardanoNode' :: Tracer IO NodeLog -> NetworkId -> SocketPath -> IO (Maybe (NominalDiffTime, DirectBackend)) | ||
| findRunningCardanoNode' :: Tracer IO NodeLog -> NetworkId -> SocketPath -> IO (Maybe (NominalDiffTime, ChainBackendOptions)) | ||
| findRunningCardanoNode' tracer networkId nodeSocket = do | ||
| let backend = DirectBackend $ DirectOptions{networkId, nodeSocket} | ||
| try (Backend.getBlockTime backend) >>= \case | ||
| let connectInfo = Api.LocalNodeConnectInfo{localConsensusModeParams = Api.CardanoModeParams (Api.EpochSlots 21600), localNodeNetworkId = networkId, localNodeSocketPath = nodeSocket} | ||
| let runDirect = flip runReaderT connectInfo . runDirectBackend | ||
| try (runDirect Backend.getBlockTime) >>= \case | ||
| Left (e :: SomeException) -> | ||
| traceWith tracer MsgQueryGenesisParametersFailed{err = show e} $> Nothing | ||
| Right blockTime -> | ||
| pure $ Just (blockTime, backend) | ||
| pure $ Just (blockTime, Direct DirectOptions{networkId, nodeSocket}) | ||
|
|
||
| -- | Start a single cardano-node devnet using the config from config/ and | ||
| -- credentials from config/credentials/. Only the 'Faucet' actor will receive | ||
|
|
@@ -168,7 +169,7 @@ withCardanoNodeDevnet :: | |
| Tracer IO NodeLog -> | ||
| -- | State directory in which credentials, db & logs are persisted. | ||
| FilePath -> | ||
| (NominalDiffTime -> DirectBackend -> IO a) -> | ||
| (NominalDiffTime -> ChainBackendOptions -> IO a) -> | ||
| IO a | ||
| withCardanoNodeDevnet tracer stateDirectory action = do | ||
| args <- setupCardanoDevnet stateDirectory | ||
|
|
@@ -178,20 +179,20 @@ withBlockfrostBackend :: | |
| Tracer IO NodeLog -> | ||
| -- | State directory in which credentials, db & logs are persisted. | ||
| FilePath -> | ||
| (NominalDiffTime -> BlockfrostBackend -> IO a) -> | ||
| (NominalDiffTime -> ChainBackendOptions -> IO a) -> | ||
| IO a | ||
| withBlockfrostBackend _tracer stateDirectory action = do | ||
| args <- setupCardanoDevnet stateDirectory | ||
| shelleyGenesis <- readFileBS >=> unsafeDecodeJson $ stateDirectory </> nodeShelleyGenesisFile args | ||
| bfProjectPath <- findFileStartingAtDirectory 3 Backend.blockfrostProjectPath | ||
| let backend = BlockfrostBackend $ defaultBlockfrostOptions{projectPath = bfProjectPath} | ||
| let backendOptions = Blockfrost defaultBlockfrostOptions{projectPath = bfProjectPath} | ||
| -- We need to make sure somehow that, before we start our blockfrost tests, | ||
| -- doing queries will give us updated information on some UTxO. There is no | ||
| -- way to definitely know if this information is correct since it might be | ||
| -- outdated. We just try to wait for sufficient amount of time before | ||
| -- starting another BF related test. | ||
| delayBF backend | ||
| action (getShelleyGenesisBlockTime shelleyGenesis) backend | ||
| -- TODO: Implement delay using the backend | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a leftover or we actually need to implement it? |
||
| action (getShelleyGenesisBlockTime shelleyGenesis) backendOptions | ||
|
|
||
| -- | Find the given file in the current directory or its parents. | ||
| -- | ||
|
|
@@ -221,7 +222,7 @@ withBackend :: | |
| forall a. | ||
| Tracer IO NodeLog -> | ||
| FilePath -> | ||
| (forall backend. ChainBackend backend => NominalDiffTime -> backend -> IO a) -> | ||
| (NominalDiffTime -> ChainBackendOptions -> IO a) -> | ||
| IO a | ||
| withBackend tracer stateDirectory action = do | ||
| getHydraBackend >>= \case | ||
|
|
@@ -235,7 +236,7 @@ withCardanoNodeOnKnownNetwork :: | |
| FilePath -> | ||
| -- | A well-known Cardano network to connect to. | ||
| KnownNetwork -> | ||
| (NominalDiffTime -> DirectBackend -> IO a) -> | ||
| (NominalDiffTime -> ChainBackendOptions -> IO a) -> | ||
| IO a | ||
| withCardanoNodeOnKnownNetwork tracer stateDirectory knownNetwork action = do | ||
| copyKnownNetworkFiles | ||
|
|
@@ -360,7 +361,7 @@ withCardanoNode :: | |
| Tracer IO NodeLog -> | ||
| FilePath -> | ||
| CardanoNodeArgs -> | ||
| (NominalDiffTime -> DirectBackend -> IO a) -> | ||
| (NominalDiffTime -> ChainBackendOptions -> IO a) -> | ||
| IO a | ||
| withCardanoNode tr stateDirectory args action = do | ||
| traceWith tr $ MsgNodeCmdSpec (show $ cmdspec process) | ||
|
|
@@ -388,7 +389,7 @@ withCardanoNode tr stateDirectory args action = do | |
| waitForSocket nodeSocketPath | ||
| traceWith tr $ MsgSocketIsReady nodeSocketPath | ||
| shelleyGenesis <- readShelleyGenesisJSON $ stateDirectory </> nodeShelleyGenesisFile args | ||
| action (getShelleyGenesisBlockTime shelleyGenesis) (DirectBackend $ DirectOptions{networkId = getShelleyGenesisNetworkId shelleyGenesis, nodeSocket = File (stateDirectory </> nodeSocket)}) | ||
| action (getShelleyGenesisBlockTime shelleyGenesis) (Direct $ DirectOptions{networkId = getShelleyGenesisNetworkId shelleyGenesis, nodeSocket = File (stateDirectory </> nodeSocket)}) | ||
|
|
||
| cleanupSocketFile = | ||
| whenM (doesFileExist socketPath) $ | ||
|
|
@@ -421,26 +422,25 @@ computeBlockTime slotLength activeSlotsCoeff = | |
| -- | Wait until the node is fully caught up with the network. This can take a | ||
| -- while! | ||
| waitForFullySynchronized :: | ||
| ChainBackend backend => | ||
| (ChainBackend m, MonadIO m) => | ||
| Tracer IO NodeLog -> | ||
| backend -> | ||
| IO () | ||
| waitForFullySynchronized tracer backend = do | ||
| systemStart <- Backend.querySystemStart backend QueryTip | ||
| m () | ||
| waitForFullySynchronized tracer = do | ||
| systemStart <- Backend.querySystemStart QueryTip | ||
| check systemStart | ||
| where | ||
| check systemStart = do | ||
| targetTime <- toRelativeTime systemStart <$> getCurrentTime | ||
| eraHistory <- Backend.queryEraHistory backend QueryTip | ||
| tipSlotNo <- fromMaybe 0 . Api.chainPointToSlotNo <$> Backend.queryTip backend | ||
| (tipTime, _slotLength) <- either throwIO pure $ getProgress tipSlotNo eraHistory | ||
| targetTime <- toRelativeTime systemStart <$> liftIO getCurrentTime | ||
| eraHistory <- Backend.queryEraHistory QueryTip | ||
| tipSlotNo <- fromMaybe 0 . Api.chainPointToSlotNo <$> Backend.queryTip | ||
| (tipTime, _slotLength) <- either (liftIO . throwIO) pure $ getProgress tipSlotNo eraHistory | ||
| let timeDifference = diffRelativeTime targetTime tipTime | ||
| let percentDone = realToFrac (100.0 * getRelativeTime tipTime / getRelativeTime targetTime) | ||
| blockTime <- Backend.getBlockTime backend | ||
| traceWith tracer $ MsgSynchronizing{percentDone, blockTime, tipTime = getRelativeTime tipTime, targetTime = getRelativeTime targetTime, timeDifference} | ||
| blockTime <- Backend.getBlockTime | ||
| liftIO $ traceWith tracer $ MsgSynchronizing{percentDone, blockTime, tipTime = getRelativeTime tipTime, targetTime = getRelativeTime targetTime, timeDifference} | ||
| if timeDifference < 20 * blockTime | ||
| then pure () | ||
| else threadDelay 3 >> check systemStart | ||
| else liftIO (threadDelay 3) >> check systemStart | ||
|
|
||
| -- | Wait for the node socket file to become available. | ||
| waitForSocket :: SocketPath -> IO () | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these extra constraints needed?