Skip to content

Commit

Permalink
payload: refactor APIs that return lists to return newtypes
Browse files Browse the repository at this point in the history
The newtypes are effectively just pure wrappers so that we can
write instances for them later. Used in a follow up diff.

Change-Id: Ice2ec720247bb7b7cdaa5e72f9b3953b11b65300
  • Loading branch information
thoughtpolice committed May 31, 2024
1 parent a8c075e commit ac4771c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
22 changes: 21 additions & 1 deletion src/Chainweb/Payload.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ module Chainweb.Payload
, verifyBlockPayload

-- * Binary encodings

, PayloadDataList(..)
, PayloadWithOutputsList(..)
, encodeBlockPayloads
, decodeBlockPayloads
, encodeBlockTransactions
Expand Down Expand Up @@ -494,6 +495,25 @@ data BlockTransactions_ a = BlockTransactions
}
deriving (Show, Eq, Ord, Generic)

-- -------------------------------------------------------------------------- --
-- Type-wrappers for some REST API endpoints

-- We want to use application/octet-stream as the content type for types
-- like [PayloadData], but doing that requires encoding the list specifically
-- with a specific binary instance. write some newtype wrappers to do this
-- with a specific encoding function, so we can then later write MimeRender
-- and MimeUnrender instances

newtype PayloadDataList = PayloadDataList { _payloadDataList :: [PayloadData] }
deriving (Show, Eq, Generic)
deriving anyclass (NFData)
deriving newtype (ToJSON, FromJSON)

newtype PayloadWithOutputsList = PayloadWithOutputsList { _payloadWithOutputsList :: [PayloadWithOutputs] }
deriving (Show, Eq, Generic)
deriving anyclass (NFData)
deriving newtype (ToJSON, FromJSON)

-- -------------------------------------------------------------------------- --

encodeBlockPayloads :: BlockPayload_ a -> B.ByteString
Expand Down
4 changes: 2 additions & 2 deletions src/Chainweb/Payload/RestAPI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type PayloadPostApi_
= "payload"
:> "batch"
:> ReqBody '[JSON] BatchBody
:> Post '[JSON] [PayloadData]
:> Post '[JSON] PayloadDataList

type PayloadPostApi (v :: ChainwebVersionT) (c :: ChainIdT)
= 'ChainwebEndpoint v :> ChainEndpoint c :> PayloadPostApi_
Expand Down Expand Up @@ -205,7 +205,7 @@ type OutputsPostApi_
:> "outputs"
:> "batch"
:> ReqBody '[JSON] BatchBody
:> Post '[JSON] [PayloadWithOutputs]
:> Post '[JSON] PayloadWithOutputsList

type OutputsPostApi (v :: ChainwebVersionT) (c :: ChainIdT)
= 'ChainwebEndpoint v :> ChainEndpoint c :> OutputsPostApi_
Expand Down
8 changes: 4 additions & 4 deletions src/Chainweb/Payload/RestAPI/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ payloadBatchClient_
. KnownChainwebVersionSymbol v
=> KnownChainIdSymbol c
=> BatchBody
-> ClientM [PayloadData]
-> ClientM PayloadDataList
payloadBatchClient_ = client (payloadPostApi @v @c)

-- The query may return any number (including none) of the requested payload
Expand All @@ -77,7 +77,7 @@ payloadBatchClient
:: ChainwebVersion
-> ChainId
-> BatchBody
-> ClientM [PayloadData]
-> ClientM PayloadDataList
payloadBatchClient v c k = runIdentity $ do
SomeChainwebVersionT (_ :: Proxy v) <- return $ someChainwebVersionVal v
SomeChainIdT (_ :: Proxy c) <- return $ someChainIdVal c
Expand Down Expand Up @@ -114,14 +114,14 @@ outputsBatchClient_
. KnownChainwebVersionSymbol v
=> KnownChainIdSymbol c
=> BatchBody
-> ClientM [PayloadWithOutputs]
-> ClientM PayloadWithOutputsList
outputsBatchClient_ = client (outputsPostApi @v @c)

outputsBatchClient
:: ChainwebVersion
-> ChainId
-> BatchBody
-> ClientM [PayloadWithOutputs]
-> ClientM PayloadWithOutputsList
outputsBatchClient v c k = runIdentity $ do
SomeChainwebVersionT (_ :: Proxy v) <- return $ someChainwebVersionVal v
SomeChainIdT (_ :: Proxy c) <- return $ someChainIdVal c
Expand Down
8 changes: 4 additions & 4 deletions src/Chainweb/Payload/RestAPI/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ payloadBatchHandler
=> PayloadBatchLimit
-> PayloadDb tbl
-> BatchBody
-> Handler [PayloadData]
-> Handler PayloadDataList
payloadBatchHandler batchLimit db ks
= liftIO (catMaybes <$> lookupPayloadDataWithHeightBatch db ks')
= liftIO (PayloadDataList . catMaybes <$> lookupPayloadDataWithHeightBatch db ks')
where
limit = take (int batchLimit)
ks' | WithoutHeights xs <- ks = limit (fmap (Nothing,) xs)
Expand Down Expand Up @@ -122,9 +122,9 @@ outputsBatchHandler
=> PayloadBatchLimit
-> PayloadDb tbl
-> BatchBody
-> Handler [PayloadWithOutputs]
-> Handler PayloadWithOutputsList
outputsBatchHandler batchLimit db ks
= liftIO (catMaybes <$> lookupPayloadWithHeightBatch db ks')
= liftIO (PayloadWithOutputsList . catMaybes <$> lookupPayloadWithHeightBatch db ks')
where
limit = take (int batchLimit)
ks' | WithoutHeights xs <- ks = limit (fmap (Nothing,) xs)
Expand Down
2 changes: 1 addition & 1 deletion tools/cwtool/TxSimulator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fetchOutputs sc cenv bhs = do
outputsBatchClient (scVersion sc) (scChain sc) (WithHeights $ map (\bh -> (_blockHeight bh, _blockPayloadHash bh)) bhs)
case r of
Left e -> throwM e
Right ps -> return ps
Right ps -> return (_payloadWithOutputsList ps)

simulateMain :: IO ()
simulateMain = do
Expand Down

0 comments on commit ac4771c

Please sign in to comment.