Skip to content
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

add minimum blockheight header #2062

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
5 changes: 5 additions & 0 deletions src/Chainweb/CutDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Chainweb.CutDB
, pruneCuts
, cutDbWebBlockHeaderDb
, cutDbBlockHeaderDb
, cutDbStore
, cutDbPayloadDb
, cutDbPactService
, cut
Expand Down Expand Up @@ -280,6 +281,10 @@ cutDbPactService :: Getter (CutDb tbl) WebPactExecutionService
cutDbPactService = to $ _webBlockPayloadStorePact . _cutDbPayloadStore
{-# INLINE cutDbPactService #-}

cutDbStore :: Getter (CutDb tbl) (Casify RocksDbTable CutHashes)
cutDbStore = to _cutDbCutStore
{-# INLINE cutDbStore #-}

cutDbPayloadStore :: Getter (CutDb tbl) (WebBlockPayloadStore tbl)
cutDbPayloadStore = to _cutDbPayloadStore
{-# INLINE cutDbPayloadStore #-}
Expand Down
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
Loading