Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 8976420

Browse files
Merge #3957
3957: Implement Update endpoints for the Node API r=KtorZ a=parsonsmatt ## Description This PR implements the node update endpoints. ## Linked issue There's a change to the implementation of the API. The decision is covered here: cardano-foundation/cardano-wallet#151 Co-authored-by: parsonsmatt <[email protected]>
2 parents 2b95d61 + f5c1894 commit 8976420

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

lib/src/Pos/Node/API.hs

+7-8
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,10 @@ type API =
613613
:<|>
614614
InfoAPI
615615
:<|>
616-
"update"
617-
:> ( "apply"
618-
:> Summary "Apply the next available update"
619-
:> Post '[ValidJSON] NoContent
620-
:<|> "postpone"
621-
:> Summary "Discard and postpone the next available update"
622-
:> Post '[ValidJSON] NoContent
623-
)
616+
Summary "Version of the next update (404 if none)"
617+
:> "next-update"
618+
:> Get '[ValidJSON] (APIResponse (V1 Core.SoftwareVersion))
619+
:<|>
620+
Summary "Restart the underlying node software."
621+
:> "restart-node"
622+
:> Post '[ValidJSON] NoContent

node/src/Cardano/Node/API.hs

+48-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import Ntp.Client (NtpConfiguration, NtpStatus (..),
2525
import Ntp.Packet (NtpOffset)
2626
import Pos.Chain.Block (LastKnownHeader, LastKnownHeaderTag)
2727
import Pos.Chain.Ssc (SscContext)
28-
import Pos.Chain.Update (UpdateConfiguration, curSoftwareVersion)
28+
import Pos.Chain.Update (ConfirmedProposalState (..), SoftwareVersion,
29+
UpdateConfiguration, UpdateProposal (..),
30+
curSoftwareVersion)
2931
import Pos.Client.CLI.NodeOptions (NodeApiArgs (..))
3032
import Pos.Context (HasPrimaryKey (..), HasSscContext (..),
3133
NodeContext (..))
@@ -38,6 +40,7 @@ import Pos.DB.GState.Lock (Priority (..), StateLock,
3840
withStateLockNoMetrics)
3941
import qualified Pos.DB.Rocks as DB
4042
import Pos.DB.Txp.MemState (GenericTxpLocalData, TxpHolderTag)
43+
import Pos.DB.Update (UpdateContext (..))
4144
import Pos.Infra.Diffusion.Subscription.Status (ssMap)
4245
import Pos.Infra.Diffusion.Types (Diffusion (..))
4346
import Pos.Infra.InjectFail (FInject (..), testLogFInject)
@@ -64,6 +67,10 @@ type NodeV1Api
6467
nodeV1Api :: Proxy NodeV1Api
6568
nodeV1Api = Proxy
6669

70+
--------------------------------------------------------------------------------
71+
-- Legacy Handlers
72+
--------------------------------------------------------------------------------
73+
6774
data LegacyCtx = LegacyCtx
6875
{ legacyCtxTxpLocalData
6976
:: !(GenericTxpLocalData ())
@@ -101,13 +108,22 @@ instance {-# OVERLAPPING #-} DB.MonadDBRead (ReaderT LegacyCtx IO) where
101108
dbGetSerUndo = DB.dbGetSerUndoRealDefault
102109
dbGetSerBlund = DB.dbGetSerBlundRealDefault
103110

111+
-- | Prepare a 'Server' for the 'Legacy.NodeApi'. We expose a 'Server' so that
112+
-- it can be embedded in other APIs, instead of an 'Application', which can only
113+
-- be run on a given port.
104114
legacyNodeApi :: LegacyCtx -> Server Legacy.NodeApi
105115
legacyNodeApi r =
106116
hoistServer
107117
(Proxy :: Proxy Legacy.NodeApi)
108118
(Handler . ExceptT . try . flip runReaderT r)
109119
Legacy.nodeServantHandlers
110120

121+
--------------------------------------------------------------------------------
122+
-- Entry point
123+
--------------------------------------------------------------------------------
124+
125+
-- | This function launches a node API server, which serves the new V1 API, the
126+
-- legacy node API, and the documentation server for both.
111127
launchNodeServer
112128
:: NodeApiArgs
113129
-> NtpConfiguration
@@ -149,6 +165,7 @@ launchNodeServer
149165
updateConfiguration
150166
compileTimeInfo
151167
shutdownCtx
168+
(ncUpdateContext nodeCtx)
152169
:<|> legacyApi
153170

154171
concurrently_
@@ -187,6 +204,7 @@ launchNodeServer
187204
(ipAddress, portNumber) = nodeBackendAddress params
188205
(docAddress, docPort) = nodeBackendDocAddress params
189206

207+
-- | Assembles the handlers for the new node API.
190208
handlers
191209
:: Diffusion IO
192210
-> TVar NtpStatus
@@ -198,12 +216,17 @@ handlers
198216
-> UpdateConfiguration
199217
-> CompileTimeInfo
200218
-> ShutdownContext
219+
-> UpdateContext
201220
-> ServerT Node.API Handler
202-
handlers d t s n l ts sv uc ci sc =
221+
handlers d t s n l ts sv uc ci sc uCtx =
203222
getNodeSettings ci uc ts sv
204223
:<|> getNodeInfo d t s n l
205-
:<|> applyUpdate sc
206-
:<|> postponeUpdate
224+
:<|> getNextUpdate uCtx
225+
:<|> restartNode sc
226+
227+
--------------------------------------------------------------------------------
228+
-- Node Settings
229+
--------------------------------------------------------------------------------
207230

208231
getNodeSettings
209232
:: CompileTimeInfo
@@ -239,20 +262,31 @@ instance Core.HasSlottingVar SettingsCtx where
239262
slottingVar =
240263
lens settingsCtxSlottingVar (\s t -> s { settingsCtxSlottingVar = t })
241264

242-
applyUpdate :: ShutdownContext -> Handler NoContent
243-
applyUpdate shutdownCtx = liftIO $ do
265+
--------------------------------------------------------------------------------
266+
-- Updates
267+
--------------------------------------------------------------------------------
268+
269+
-- | Handler
270+
restartNode :: ShutdownContext -> Handler NoContent
271+
restartNode shutdownCtx = liftIO $ do
244272
doFail <- testLogFInject (_shdnFInjects shutdownCtx) FInjApplyUpdateNoExit
245273
unless doFail (runReaderT triggerShutdown shutdownCtx)
246274
pure NoContent
247275

248-
-- | In the old implementation, we would delete the new update from the
249-
-- acid-stae database. We no longer persist this information, so postponing an
250-
-- update is simply a noop.
251-
--
252-
-- TODO: verify this is a real thought and not, in fact, bad
253-
postponeUpdate :: Handler NoContent
254-
postponeUpdate = do
255-
pure NoContent
276+
-- | This endpoint does a 404 unless there is an update available. If an update
277+
-- is available, it returns the 'SoftwareVersion' for that update.
278+
getNextUpdate :: UpdateContext -> Handler (APIResponse (V1 SoftwareVersion))
279+
getNextUpdate uc = do
280+
mproposalState <- tryReadMVar (ucDownloadedUpdate uc)
281+
single <$> case mproposalState of
282+
Just proposalState ->
283+
pure (V1 (upSoftwareVersion (cpsUpdateProposal proposalState)))
284+
Nothing ->
285+
throwError err404
286+
287+
--------------------------------------------------------------------------------
288+
-- Node Info
289+
--------------------------------------------------------------------------------
256290

257291
getNodeInfo
258292
:: Diffusion IO
@@ -319,7 +353,6 @@ instance DB.MonadDBRead (ReaderT InfoCtx IO) where
319353
dbGetSerUndo = DB.dbGetSerUndoRealDefault
320354
dbGetSerBlund = DB.dbGetSerBlundRealDefault
321355

322-
323356
getNodeSyncProgress
324357
::
325358
( MonadIO m

0 commit comments

Comments
 (0)