Skip to content

Commit 917156e

Browse files
authored
Merge pull request #919 from IntersectMBO/mgalazyn/feature/add-get-pparams-json
cardano-rpc | Add getProtocolParamsJson gRPC endpoint
2 parents 847bc76 + e21b26d commit 917156e

5 files changed

Lines changed: 70 additions & 19 deletions

File tree

cardano-rpc/cardano-rpc.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ library
6161
Cardano.Rpc.Server.Internal.Env
6262
Cardano.Rpc.Server.Internal.Error
6363
Cardano.Rpc.Server.Internal.Monad
64+
Cardano.Rpc.Server.Internal.Node
6465
Cardano.Rpc.Server.Internal.UtxoRpc.Query
6566
Cardano.Rpc.Server.Internal.UtxoRpc.Submit
6667
Cardano.Rpc.Server.Internal.UtxoRpc.Type
@@ -89,6 +90,7 @@ library
8990
Proto.Utxorpc.V1alpha.Submit.Submit_Fields
9091

9192
build-depends:
93+
aeson,
9294
base,
9395
bytestring,
9496
cardano-api >=10.17,

cardano-rpc/proto/cardano/rpc/node.proto

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ package cardano.rpc;
55
import "google/protobuf/empty.proto";
66

77
service Node {
8-
98
rpc GetEra(google.protobuf.Empty) returns (CurrentEra) {}
109

10+
rpc GetProtocolParamsJson(google.protobuf.Empty) returns (ProtocolParamsJson) {}
1111
}
1212

1313
enum Era {
@@ -22,5 +22,9 @@ enum Era {
2222

2323

2424
message CurrentEra {
25-
Era era = 1;
25+
Era era = 1;
26+
}
27+
28+
message ProtocolParamsJson {
29+
bytes json = 1; // JSON representation of the protocol parameters
2630
}

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
{-# LANGUAGE QuantifiedConstraints #-}
88
{-# LANGUAGE RankNTypes #-}
99
{-# LANGUAGE ScopedTypeVariables #-}
10-
{-# LANGUAGE TypeApplications #-}
1110

1211
module Cardano.Rpc.Server
1312
( runRpcServer
@@ -21,38 +20,28 @@ import Cardano.Rpc.Proto.Api.UtxoRpc.Submit qualified as UtxoRpc
2120
import Cardano.Rpc.Server.Config
2221
import Cardano.Rpc.Server.Internal.Env
2322
import Cardano.Rpc.Server.Internal.Monad
23+
import Cardano.Rpc.Server.Internal.Node
2424
import Cardano.Rpc.Server.Internal.Orphans ()
2525
import Cardano.Rpc.Server.Internal.UtxoRpc.Query
2626
import Cardano.Rpc.Server.Internal.UtxoRpc.Submit
2727

2828
import RIO
2929

3030
import Control.Tracer
31-
import Data.ProtoLens (defMessage)
32-
import Data.ProtoLens.Field (field)
3331
import Network.GRPC.Common
3432
import Network.GRPC.Server
3533
import Network.GRPC.Server.Protobuf
3634
import Network.GRPC.Server.Run
3735
import Network.GRPC.Server.StreamType
38-
import Network.GRPC.Spec hiding (Identity)
39-
40-
import Proto.Google.Protobuf.Empty
41-
42-
-- Individual handlers
43-
44-
getEraMethod :: MonadRpc e m => Proto Empty -> m (Proto Rpc.CurrentEra)
45-
getEraMethod _ = pure mockNodeResponse
46-
47-
-- Mock node response
48-
mockNodeResponse :: Proto Rpc.CurrentEra
49-
mockNodeResponse = Proto $ defMessage & field @"era" .~ Rpc.Conway
5036

5137
-- Server top level
5238
methodsNodeRpc
5339
:: MonadRpc e m
5440
=> Methods m (ProtobufMethodsOf Rpc.Node)
55-
methodsNodeRpc = Method (mkNonStreaming getEraMethod) NoMoreMethods
41+
methodsNodeRpc =
42+
Method (mkNonStreaming getEraMethod)
43+
. Method (mkNonStreaming getProtocolParamsJsonMethod)
44+
$ NoMoreMethods
5645

5746
methodsUtxoRpc
5847
:: MonadRpc e m
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{-# LANGUAGE ConstraintKinds #-}
2+
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE DerivingVia #-}
4+
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE GADTs #-}
6+
{-# LANGUAGE OverloadedLabels #-}
7+
{-# LANGUAGE QuantifiedConstraints #-}
8+
{-# LANGUAGE RankNTypes #-}
9+
{-# LANGUAGE ScopedTypeVariables #-}
10+
{-# LANGUAGE TypeApplications #-}
11+
12+
module Cardano.Rpc.Server.Internal.Node
13+
( getEraMethod
14+
, getProtocolParamsJsonMethod
15+
)
16+
where
17+
18+
import Cardano.Api
19+
import Cardano.Api.Experimental.Era
20+
import Cardano.Rpc.Proto.Api.Node qualified as Rpc
21+
import Cardano.Rpc.Server.Internal.Error
22+
import Cardano.Rpc.Server.Internal.Monad
23+
import Cardano.Rpc.Server.Internal.Orphans ()
24+
25+
import RIO hiding (toList)
26+
27+
import Data.Aeson qualified as A
28+
import Data.ByteString.Lazy qualified as BL
29+
import Data.Default
30+
import Data.ProtoLens (defMessage)
31+
import Network.GRPC.Spec
32+
33+
import Proto.Google.Protobuf.Empty
34+
35+
getEraMethod :: MonadRpc e m => Proto Empty -> m (Proto Rpc.CurrentEra)
36+
getEraMethod _ = pure . Proto $ defMessage & #era .~ Rpc.Conway
37+
38+
getProtocolParamsJsonMethod :: MonadRpc e m => Proto Empty -> m (Proto Rpc.ProtocolParamsJson)
39+
getProtocolParamsJsonMethod _ = do
40+
nodeConnInfo <- grab
41+
AnyCardanoEra era <- liftIO . throwExceptT $ determineEra nodeConnInfo
42+
eon <- forEraInEon @Era era (error "getProtocolParamsJsonMethod: Minimum Conway era required") pure
43+
let sbe = convert eon
44+
45+
let target = VolatileTip
46+
pparams <-
47+
liftIO . (throwEither =<<) $
48+
executeLocalStateQueryExpr nodeConnInfo target $
49+
throwEither =<< throwEither =<< queryProtocolParameters sbe
50+
51+
let pparamsJson = obtainCommonConstraints eon $ A.encode pparams
52+
53+
pure $
54+
def
55+
& #json .~ BL.toStrict pparamsJson

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Cardano.Rpc.Server.Internal.UtxoRpc.Type
2525

2626
import RIO hiding (toList)
2727

28+
import Data.Default
2829
import Data.ProtoLens (defMessage)
2930
import Data.Text.Encoding qualified as T
3031
import GHC.IsList
@@ -51,7 +52,7 @@ readParamsMethod _req = do
5152
pure (pparams, chainPoint, blockNo)
5253

5354
pure $
54-
defMessage
55+
def
5556
& #ledgerTip .~ mkChainPointMsg chainPoint blockNo
5657
& #values . #cardano .~ conwayEraOnwardsConstraints eon (inject pparams)
5758

0 commit comments

Comments
 (0)