-
Notifications
You must be signed in to change notification settings - Fork 93
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
branch bound limits #1621
Merged
Merged
branch bound limits #1621
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d810dd6
limit number of bounds in branch requests
larskuhtz 7a0dc09
tighten request size limit for p2p endpoints
larskuhtz 1337fae
Merge branch 'master' into lars/bound-limits
edmundnoble 10df3c3
Address review
edmundnoble 402f172
Merge branch 'master' into lars/bound-limits
larskuhtz e2b445e
Merge branch 'master' into lars/bound-limits
chessai 57a45be
Merge branch 'master' into lars/bound-limits
edmundnoble 4f7ade4
add tests for bounds limits
chessai 480b20f
point to other openapi spec
chessai 53b21a5
openapi spec: switch back to main
chessai 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 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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
@@ -43,6 +45,7 @@ import Data.Functor.Of | |
import Data.IORef | ||
import Data.Proxy | ||
import Data.Text.Encoding (decodeUtf8) | ||
import Numeric.Natural(Natural) | ||
|
||
import Network.Wai.EventSource (ServerEvent(..), eventSourceAppIO) | ||
|
||
|
@@ -106,6 +109,14 @@ checkBounds | |
checkBounds db b = b | ||
<$ traverse_ (checkKey db . _getUpperBound) (_branchBoundsUpper b) | ||
|
||
err400Msg :: ToJSON msg => msg -> ServerError | ||
err400Msg msg = ServerError | ||
{ errHTTPCode = 400 | ||
, errReasonPhrase = "Bad request" | ||
, errBody = encode msg | ||
, errHeaders = [] | ||
} | ||
|
||
-- -------------------------------------------------------------------------- -- | ||
-- Handlers | ||
|
||
|
@@ -118,6 +129,21 @@ defaultEntryLimit = 360 | |
p2pEntryLimit :: Num a => a | ||
p2pEntryLimit = 20 | ||
|
||
newtype BranchBoundsLimit | ||
= BranchBoundsLimit { getBranchBoundsLimit :: Natural } | ||
deriving newtype (Show, Eq, Ord) | ||
|
||
-- | Default limit for the number of bounds in the request of a branch query | ||
-- | ||
defaultBoundsLimit :: BranchBoundsLimit | ||
defaultBoundsLimit = BranchBoundsLimit 32 | ||
|
||
-- | Limit for the number of bounds in the request of a branch query on the P2P | ||
-- API. | ||
-- | ||
p2pBoundsLimit :: BranchBoundsLimit | ||
p2pBoundsLimit = BranchBoundsLimit 4 | ||
|
||
-- | Query Branch Hashes of the database. | ||
-- | ||
-- Cf. "Chainweb.BlockHeaderDB.RestAPI" for more details | ||
|
@@ -132,14 +158,19 @@ branchHashesHandler | |
-> Maybe MaxRank | ||
-> BranchBounds db | ||
-> Handler (Page (NextItem (DbKey db)) (DbKey db)) | ||
branchHashesHandler db limit next minr maxr bounds = do | ||
nextChecked <- traverse (traverse $ checkKey db) next | ||
checkedBounds <- checkBounds db bounds | ||
liftIO | ||
$ branchKeys db nextChecked (succ <$> effectiveLimit) minr maxr | ||
(_branchBoundsLower checkedBounds) | ||
(_branchBoundsUpper checkedBounds) | ||
$ finiteStreamToPage id effectiveLimit . void | ||
branchHashesHandler db limit next minr maxr bounds | ||
| fromIntegral (length (_branchBoundsUpper bounds)) > getBranchBoundsLimit defaultBoundsLimit = throwError $ err400Msg $ | ||
"upper branch bound limit exceeded. Only " <> show defaultBoundsLimit <> " values are supported." | ||
| fromIntegral (length (_branchBoundsLower bounds)) > getBranchBoundsLimit defaultBoundsLimit = throwError $ err400Msg $ | ||
"lower branch bound limit exceeded. Only " <> show defaultBoundsLimit <> " values are supported." | ||
| otherwise = do | ||
nextChecked <- traverse (traverse $ checkKey db) next | ||
checkedBounds <- checkBounds db bounds | ||
liftIO | ||
$ branchKeys db nextChecked (succ <$> effectiveLimit) minr maxr | ||
(_branchBoundsLower checkedBounds) | ||
(_branchBoundsUpper checkedBounds) | ||
$ finiteStreamToPage id effectiveLimit . void | ||
where | ||
effectiveLimit = min defaultKeyLimit <$> (limit <|> Just defaultKeyLimit) | ||
|
||
|
@@ -151,6 +182,7 @@ branchHeadersHandler | |
:: TreeDb db | ||
=> ToJSON (DbKey db) | ||
=> db | ||
-> BranchBoundsLimit | ||
-> Limit | ||
-- ^ max limit | ||
-> Maybe Limit | ||
|
@@ -159,14 +191,19 @@ branchHeadersHandler | |
-> Maybe MaxRank | ||
-> BranchBounds db | ||
-> Handler (Page (NextItem (DbKey db)) (DbEntry db)) | ||
branchHeadersHandler db maxLimit limit next minr maxr bounds = do | ||
nextChecked <- traverse (traverse $ checkKey db) next | ||
checkedBounds <- checkBounds db bounds | ||
liftIO | ||
$ branchEntries db nextChecked (succ <$> effectiveLimit) minr maxr | ||
(_branchBoundsLower checkedBounds) | ||
(_branchBoundsUpper checkedBounds) | ||
$ finiteStreamToPage key effectiveLimit . void | ||
branchHeadersHandler db (BranchBoundsLimit boundsLimit) maxLimit limit next minr maxr bounds | ||
| fromIntegral (length (_branchBoundsUpper bounds)) > boundsLimit = throwError $ err400Msg $ | ||
"upper branch bound limit exceeded. Only " <> show boundsLimit <> " values are supported." | ||
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. Same question as above here. |
||
| fromIntegral (length (_branchBoundsLower bounds)) > boundsLimit = throwError $ err400Msg $ | ||
"lower branch bound limit exceeded. Only " <> show boundsLimit <> " values are supported." | ||
| otherwise = do | ||
nextChecked <- traverse (traverse $ checkKey db) next | ||
checkedBounds <- checkBounds db bounds | ||
liftIO | ||
$ branchEntries db nextChecked (succ <$> effectiveLimit) minr maxr | ||
(_branchBoundsLower checkedBounds) | ||
(_branchBoundsUpper checkedBounds) | ||
$ finiteStreamToPage key effectiveLimit . void | ||
where | ||
effectiveLimit = min maxLimit <$> (limit <|> Just maxLimit) | ||
|
||
|
@@ -243,15 +280,15 @@ blockHeaderDbServer (BlockHeaderDb_ db) | |
:<|> headersHandler db defaultEntryLimit | ||
:<|> headerHandler db | ||
:<|> branchHashesHandler db | ||
:<|> branchHeadersHandler db defaultEntryLimit | ||
:<|> branchHeadersHandler db defaultBoundsLimit defaultEntryLimit | ||
|
||
-- Restricted P2P BlockHeader DB API | ||
-- | ||
p2pBlockHeaderDbServer :: BlockHeaderDb_ v c -> Server (P2pBlockHeaderDbApi v c) | ||
p2pBlockHeaderDbServer (BlockHeaderDb_ db) | ||
= headersHandler db p2pEntryLimit | ||
:<|> headerHandler db | ||
:<|> branchHeadersHandler db p2pEntryLimit | ||
:<|> branchHeadersHandler db p2pBoundsLimit p2pEntryLimit | ||
|
||
-- -------------------------------------------------------------------------- -- | ||
-- Multichain Server | ||
|
@@ -284,7 +321,7 @@ headerStreamServer | |
. CanReadablePayloadCas tbl | ||
=> CutDb tbl | ||
-> Server (HeaderStreamApi v) | ||
headerStreamServer cdb = headerStreamHandler cdb | ||
headerStreamServer = headerStreamHandler | ||
|
||
headerStreamHandler :: forall tbl. CanReadablePayloadCas tbl => CutDb tbl -> Tagged Handler Application | ||
headerStreamHandler db = Tagged $ \req resp -> do | ||
|
This file contains 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 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 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
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.
Should this say "lower branch bound limit exceeded"?