Skip to content

Commit

Permalink
implement X-Min-Block-Height http header
Browse files Browse the repository at this point in the history
Change-Id: If4b86fedafcbe1962f5a59807fe66b2c420b2260
  • Loading branch information
chessai committed Dec 9, 2024
1 parent afb8810 commit efdd7ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NumericUnderscores #-}
Expand Down Expand Up @@ -133,6 +134,7 @@ import Network.Wai.Middleware.Throttle

import Prelude hiding (log)

import Streaming.Prelude qualified as S
import System.Clock
import System.LogLevel

Expand All @@ -141,6 +143,7 @@ import System.LogLevel
import Chainweb.Backup
import Chainweb.BlockHeader
import Chainweb.BlockHeaderDB (BlockHeaderDb)
import Chainweb.BlockHeight (BlockHeight)
import Chainweb.ChainId
import Chainweb.Chainweb.ChainResources
import Chainweb.Chainweb.Configuration
Expand Down Expand Up @@ -168,6 +171,7 @@ import Chainweb.Payload.PayloadStore.RocksDB
import Chainweb.RestAPI
import Chainweb.RestAPI.NetworkID
import Chainweb.Transaction
import Chainweb.TreeDB (getBranchIncreasing)
import Chainweb.Utils
import Chainweb.Utils.RequestLog
import Chainweb.Version
Expand Down Expand Up @@ -721,14 +725,24 @@ runChainweb cw nowServing = do
mkValidationMiddleware
else return id

concurrentlies_
-- Get the minimum blockheight across all chains starting from the cut that the node starts with
cutHeaders <- readHighestCutHeaders (_chainwebVersion cw) (logFunctionText $ _chainwebLogger cw) (cutDb ^. cutDbWebBlockHeaderDb) (cutDb ^. cutDbStore)
minBlockHeight <- fmap (minimum . fmap (view blockHeight) . catMaybes) $ forM (HM.toList (cutDb ^. cutDbWebBlockHeaderDb ^. webBlockHeaderDb)) $ \(cid, blockHeaderDb) -> do
case cutHeaders ^? ix cid of
Nothing -> do
return Nothing
Just latestHeader -> do
getBranchIncreasing blockHeaderDb latestHeader 0 $ \branch -> do
S.head_ branch

concurrentlies_
-- 1. Start serving Rest API
[ (if tls then serve else servePlain)
$ httpLog
. throttle (_chainwebPutPeerThrottler cw)
. throttle (_chainwebMempoolThrottler cw)
. throttle (_chainwebThrottler cw)
. minBlockHeightMiddleware minBlockHeight
. p2pRequestSizeLimit
. p2pValidationMiddleware

Expand All @@ -740,6 +754,7 @@ runChainweb cw nowServing = do
serveServiceApi
$ serviceHttpLog
. serviceRequestSizeLimit
. minBlockHeightMiddleware minBlockHeight
. serviceApiValidationMiddleware
]

Expand Down Expand Up @@ -966,3 +981,7 @@ runChainweb cw nowServing = do
enabled conf = do
logg Info "Mempool p2p sync enabled"
return $ map (runMempoolSyncClient mgr conf (_chainwebPeer cw)) chainVals

minBlockHeightMiddleware :: BlockHeight -> Middleware
minBlockHeightMiddleware minBlockHeight app req respond = app req $ \response ->
respond (mapResponseHeaders (("X-Min-Block-Height", sshow minBlockHeight) :) response)
2 changes: 1 addition & 1 deletion test/unit/Chainweb/Test/RestAPI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ simpleSessionTests rdb tls =

httpHeaderTests :: IO TestClientEnv_ -> ChainId -> TestTree
httpHeaderTests envIO cid =
testGroup ("http header tests for chain " <> sshow cid)
testGroup ("http header tests for chain " <> T.unpack (chainIdToText cid))
[ testCase "headerClient" $ go $ \v h -> headerClient' v cid (key h)
, testCase "headersClient" $ go $ \v _ -> headersClient' v cid Nothing Nothing Nothing Nothing
, testCase "blocksClient" $ go $ \v _ -> blocksClient' v cid Nothing Nothing Nothing Nothing
Expand Down

0 comments on commit efdd7ec

Please sign in to comment.