Skip to content

Commit 153dece

Browse files
committed
phase 6
1 parent 0c54d20 commit 153dece

12 files changed

Lines changed: 176 additions & 79 deletions

File tree

cardano-api/src/Cardano/Api/Consensus.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,25 @@ module Cardano.Api.Consensus
4747
, ProtocolClientInfoArgs (..)
4848

4949
-- * Reexports from @ouroboros-consensus@
50+
, BlockComponent (..)
5051
, ByronBlock
52+
, CardanoBlock
53+
, ChainDB.ChainDB
54+
, ChainDB.getBlockComponent
5155
, ChainDepState
5256
, GenTx (..)
5357
, EraMismatch (..)
58+
, HasHeader
59+
, HeaderHash
60+
, NodeKernel (..)
61+
, OneEraHash (..)
5462
, PastHorizonException
5563
, PraosProtocolSupportsNode
5664
, PraosProtocolSupportsNodeCrypto
65+
, RealPoint (..)
5766
, ShelleyGenesisStaking (..)
67+
, StandardCrypto
68+
, blockNo
5869
, byronIdTx
5970
, condense
6071
, getOpCertCounters
@@ -68,3 +79,5 @@ import Cardano.Api.Consensus.Internal.InMode
6879
import Cardano.Api.Consensus.Internal.Mode
6980
import Cardano.Api.Consensus.Internal.Protocol
7081
import Cardano.Api.Consensus.Internal.Reexport
82+
83+
import Ouroboros.Consensus.Storage.ChainDB qualified as ChainDB
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
module Cardano.Api.Consensus.Internal.Reexport
2-
( ByronBlock
2+
( BlockComponent (..)
3+
, ByronBlock
4+
, CardanoBlock
35
, ChainDepState
46
, GenTx (..)
7+
, HasHeader
8+
, HeaderHash
59
, EraMismatch (..)
10+
, NodeKernel (..)
11+
, OneEraHash (..)
612
, PastHorizonException
713
, PraosProtocolSupportsNode
814
, PraosProtocolSupportsNodeCrypto
15+
, RealPoint (..)
916
, ShelleyGenesisStaking (..)
17+
, StandardCrypto
18+
, blockNo
1019
, byronIdTx
1120
, condense
1221
, getOpCertCounters
@@ -16,19 +25,24 @@ module Cardano.Api.Consensus.Internal.Reexport
1625
)
1726
where
1827

28+
import Cardano.Protocol.Crypto (StandardCrypto)
29+
import Ouroboros.Consensus.Block (HasHeader, HeaderHash, RealPoint (..), blockNo)
1930
import Ouroboros.Consensus.Byron.Ledger (ByronBlock, GenTx (..), byronIdTx)
20-
import Ouroboros.Consensus.Cardano.Block (EraMismatch (..))
31+
import Ouroboros.Consensus.Cardano.Block (CardanoBlock, EraMismatch (..))
32+
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras (OneEraHash (..))
2133
import Ouroboros.Consensus.HardFork.History.EpochInfo (interpreterToEpochInfo)
2234
import Ouroboros.Consensus.HardFork.History.Qry
2335
( PastHorizonException
2436
, unsafeExtendSafeZone
2537
)
2638
import Ouroboros.Consensus.Ledger.SupportsMempool (txId)
39+
import Ouroboros.Consensus.Node (NodeKernel (..))
2740
import Ouroboros.Consensus.Protocol.Abstract (ChainDepState)
2841
import Ouroboros.Consensus.Protocol.Praos.Common
2942
( PraosProtocolSupportsNode
3043
, PraosProtocolSupportsNodeCrypto
3144
, getOpCertCounters
3245
)
3346
import Ouroboros.Consensus.Shelley.Node (ShelleyGenesisStaking (..))
47+
import Ouroboros.Consensus.Storage.Common (BlockComponent (..))
3448
import Ouroboros.Consensus.Util.Condense (condense)

cardano-rpc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ It implements [UTxO RPC](https://utxorpc.org/introduction) protobuf communicatio
3333

3434
| Method | Status |
3535
|--------|--------|
36-
| [FetchBlock](https://utxorpc.org/sync/spec/#fetchblockrequest) | ⬜ Not supported |
36+
| [FetchBlock](https://utxorpc.org/sync/spec/#fetchblockrequest) | ✅ Supported |
3737
| [DumpHistory](https://utxorpc.org/sync/spec/#dumphistoryrequest) | ⬜ Not supported |
3838
| [FollowTip](https://utxorpc.org/sync/spec/#followtiprequest) | ⬜ Not supported |
3939
| [ReadTip](https://utxorpc.org/sync/spec/#readtiprequest) | ⬜ Not supported |

cardano-rpc/cardano-rpc.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ library
5858
Cardano.Rpc.Server.Internal.Error
5959
Cardano.Rpc.Server.Internal.Monad
6060
Cardano.Rpc.Server.Internal.Node
61-
Cardano.Rpc.Server.Internal.NodeKernelAccess
6261
Cardano.Rpc.Server.Internal.Tracing
6362
Cardano.Rpc.Server.Internal.UtxoRpc.Eval
6463
Cardano.Rpc.Server.Internal.UtxoRpc.Predicate
6564
Cardano.Rpc.Server.Internal.UtxoRpc.Query
6665
Cardano.Rpc.Server.Internal.UtxoRpc.Submit
6766
Cardano.Rpc.Server.Internal.UtxoRpc.Sync
6867
Cardano.Rpc.Server.Internal.UtxoRpc.Type
68+
Cardano.Rpc.Server.NodeKernelAccess
69+
Cardano.Rpc.Server.NodeKernelAccess.Internal
6970

7071
other-modules:
7172
Cardano.Rpc.Server.Internal.Orphans

cardano-rpc/src/Cardano/Rpc/Proto/Api/UtxoRpc/Sync.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
module Cardano.Rpc.Proto.Api.UtxoRpc.Sync
55
( module Proto.Utxorpc.V1beta.Sync.Sync
66
, module Proto.Utxorpc.V1beta.Sync.Sync_Fields
7+
, module Proto.Utxorpc.V1beta.Cardano.Cardano
8+
, header
79
)
810
where
911

1012
import Network.GRPC.Common
1113
import Network.GRPC.Common.Protobuf
1214

15+
import Proto.Utxorpc.V1beta.Cardano.Cardano
16+
import Proto.Utxorpc.V1beta.Cardano.Cardano_Fields (header)
1317
import Proto.Utxorpc.V1beta.Sync.Sync
1418
import Proto.Utxorpc.V1beta.Sync.Sync_Fields
1519

cardano-rpc/src/Cardano/Rpc/Server.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ module Cardano.Rpc.Server
1212
( runRpcServer
1313

1414
-- * Node kernel access
15-
, NodeKernelAccess (..)
15+
, NodeKernelAccess
16+
, NodeKernelAccessF (..)
1617
, LedgerSnapshot (..)
18+
, mkNodeKernelAccess
1719

1820
-- * Traces
1921
, TraceRpc (..)
@@ -33,13 +35,14 @@ import Cardano.Rpc.Server.Config
3335
import Cardano.Rpc.Server.Internal.Env
3436
import Cardano.Rpc.Server.Internal.Monad
3537
import Cardano.Rpc.Server.Internal.Node
36-
import Cardano.Rpc.Server.Internal.NodeKernelAccess
3738
import Cardano.Rpc.Server.Internal.Orphans ()
3839
import Cardano.Rpc.Server.Internal.Tracing
3940
import Cardano.Rpc.Server.Internal.UtxoRpc.Eval
4041
import Cardano.Rpc.Server.Internal.UtxoRpc.Query
4142
import Cardano.Rpc.Server.Internal.UtxoRpc.Submit
4243
import Cardano.Rpc.Server.Internal.UtxoRpc.Sync
44+
import Cardano.Rpc.Server.NodeKernelAccess
45+
import Cardano.Rpc.Server.NodeKernelAccess.Internal
4346

4447
import RIO
4548

@@ -108,7 +111,7 @@ runRpcServer
108111
-- ^ Server configuration
109112
-> NetworkMagic
110113
-- ^ Network discriminant
111-
-> IORef (Maybe (NodeKernelAccess IO))
114+
-> IORef (Maybe NodeKernelAccess)
112115
-- ^ Node kernel access, populated when the kernel is ready
113116
-> IO ()
114117
runRpcServer tracer rpcConfig networkMagic nodeKernelAccessRef = handleFatalExceptions $ do

cardano-rpc/src/Cardano/Rpc/Server/Internal/Env.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22
{-# LANGUAGE NoFieldSelectors #-}
33

44
module Cardano.Rpc.Server.Internal.Env
5-
( RpcEnv (..)
5+
( NodeKernelAccess
6+
, RpcEnv (..)
67
, mkLocalNodeConnectInfo
78
)
89
where
910

1011
import Cardano.Api
1112
import Cardano.Rpc.Server.Config
12-
import Cardano.Rpc.Server.Internal.NodeKernelAccess
1313
import Cardano.Rpc.Server.Internal.Tracing
14+
import Cardano.Rpc.Server.NodeKernelAccess (NodeKernelAccessF)
15+
16+
import RIO (RIO)
1417

1518
import Control.Tracer (Tracer)
1619
import Data.IORef
1720

21+
type NodeKernelAccess = NodeKernelAccessF (RIO RpcEnv)
22+
1823
data RpcEnv = RpcEnv
1924
{ config :: !RpcConfig
2025
, tracer :: forall m. MonadIO m => Tracer m TraceRpc
2126
, -- TODO replace with better connection management than one connection per rpc request
2227
rpcLocalNodeConnectInfo :: !LocalNodeConnectInfo
23-
, rpcNodeKernelAccess :: !(IORef (Maybe (NodeKernelAccess IO)))
28+
, rpcNodeKernelAccess :: !(IORef (Maybe NodeKernelAccess))
2429
}
2530

2631
mkLocalNodeConnectInfo :: SocketPath -> NetworkMagic -> LocalNodeConnectInfo

cardano-rpc/src/Cardano/Rpc/Server/Internal/Monad.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ where
2323

2424
import Cardano.Api
2525
import Cardano.Rpc.Server.Internal.Env
26-
import Cardano.Rpc.Server.Internal.NodeKernelAccess
2726
import Cardano.Rpc.Server.Internal.Tracing
27+
import Cardano.Rpc.Server.NodeKernelAccess (NodeKernelAccessF)
2828

2929
import RIO
3030

@@ -42,7 +42,7 @@ instance Has a a where
4242
instance Has LocalNodeConnectInfo RpcEnv where
4343
obtain RpcEnv{rpcLocalNodeConnectInfo} = rpcLocalNodeConnectInfo
4444

45-
instance Has (IORef (Maybe (NodeKernelAccess IO))) RpcEnv where
45+
instance m ~ RIO RpcEnv => Has (IORef (Maybe (NodeKernelAccessF m))) RpcEnv where
4646
obtain RpcEnv{rpcNodeKernelAccess} = rpcNodeKernelAccess
4747

4848
instance MonadIO m => Has (Tracer m TraceRpc) RpcEnv where
@@ -96,7 +96,7 @@ wrapInSpan spanConstructor act = do
9696
type MonadRpc e m =
9797
( Has (Tracer m TraceRpc) e
9898
, Has LocalNodeConnectInfo e
99-
, Has (IORef (Maybe (NodeKernelAccess m))) e
99+
, Has (IORef (Maybe (NodeKernelAccessF m))) e
100100
, HasCallStack
101101
, MonadReader e m
102102
, MonadUnliftIO m

cardano-rpc/src/Cardano/Rpc/Server/Internal/NodeKernelAccess.hs

Lines changed: 0 additions & 57 deletions
This file was deleted.

cardano-rpc/src/Cardano/Rpc/Server/Internal/UtxoRpc/Sync.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import Cardano.Api
1111
import Cardano.Rpc.Proto.Api.UtxoRpc.Sync qualified as U5c
1212
import Cardano.Rpc.Server.Internal.Error
1313
import Cardano.Rpc.Server.Internal.Monad
14-
import Cardano.Rpc.Server.Internal.NodeKernelAccess
1514
import Cardano.Rpc.Server.Internal.Tracing ()
15+
import Cardano.Rpc.Server.NodeKernelAccess
16+
import Cardano.Rpc.Server.NodeKernelAccess.Internal
1617

1718
import RIO
1819

@@ -25,17 +26,12 @@ fetchBlockMethod
2526
=> Proto U5c.FetchBlockRequest
2627
-> m (Proto U5c.FetchBlockResponse)
2728
fetchBlockMethod request = do
28-
nodeKernelAccessRef <- grab -- @(IORef (Maybe (NodeKernelAccess m)))
29+
nodeKernelAccessRef <- grab
2930
let blockRefs = request ^. U5c.ref
30-
liftIO $ withNodeKernelAccess nodeKernelAccessRef $ \nodeKernelAccess -> do
31+
withNodeKernelAccess nodeKernelAccessRef $ \nodeKernelAccess -> do
3132
blocks <- mapM (fetchOne nodeKernelAccess) blockRefs
3233
pure $ defMessage & U5c.block .~ blocks
3334
where
34-
fetchOne
35-
:: MonadRpc e m
36-
=> NodeKernelAccess m
37-
-> Proto U5c.BlockRef
38-
-> m (Proto U5c.AnyChainBlock)
3935
fetchOne nodeKernelAccess blockRef = do
4036
let slot = SlotNo $ blockRef ^. U5c.slot
4137
hashBytes = blockRef ^. U5c.hash
@@ -50,4 +46,10 @@ fetchBlockMethod request = do
5046
Nothing ->
5147
throwGrpcErrorWithMessage GrpcNotFound $
5248
"block not found at slot " <> tshow (unSlotNo slot)
53-
Just rawBytes -> pure $ defMessage & U5c.nativeBytes .~ rawBytes
49+
Just (rawBytes, BlockNo height) ->
50+
pure $
51+
defMessage
52+
& U5c.nativeBytes .~ rawBytes
53+
& U5c.cardano . U5c.header . U5c.slot .~ unSlotNo slot
54+
& U5c.cardano . U5c.header . U5c.hash .~ hashBytes
55+
& U5c.cardano . U5c.header . U5c.height .~ height

0 commit comments

Comments
 (0)