Skip to content

Commit

Permalink
add support for 'GetGenesisConfig' state query
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Mar 4, 2021
1 parent 69abe6d commit 03c1110
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 5 deletions.
74 changes: 74 additions & 0 deletions docs/static/ogmios.wsp.json
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@
}
}
}
, { "type": "string"
, "enum": ["genesisConfig"]
}
]
}
}
Expand Down Expand Up @@ -790,6 +793,39 @@
}
}

, "QueryResponse[genesisConfig]":
{ "type": "object"
, "required": [ "type", "version", "servicename", "methodname", "result" ]
, "properties":
{ "type":
{ "type": "string"
, "enum": [ "jsonwsp/response" ]
}
, "version":
{ "type": "string"
, "enum": [ "1.0" ]
}
, "servicename":
{ "type": "string"
, "enum": [ "ogmios" ]
}
, "methodname":
{ "type": "string"
, "enum": [ "Query" ]
}
, "result":
{ "oneOf":
[ { "$ref": "#/definitions/CompactGenesis" }
, { "$ref": "#/definitions/EraMismatch" }
, { "$ref": "#/definitions/QueryUnavailableInCurrentEra" }
]
}
, "reflection":
{ "description": "Any value that was set by a client request in the 'mirror' field."
}
}
}

}

, "definitions":
Expand Down Expand Up @@ -1463,6 +1499,39 @@
]
}

, "CompactGenesis":
{ "type": "object"
, "additionalProperties": false
, "required":
[ "systemStart"
, "networkMagic"
, "network"
, "activeSlotsCoefficient"
, "securityParameter"
, "epochLength"
, "slotsPerKesPeriod"
, "maxKesEvolutions"
, "slotLength"
, "updateQuorum"
, "maxLovelaceSupply"
, "protocolParameters"
]
, "properties":
{ "systemStart": { "$ref": "#/definitions/UtcTime" }
, "networkMagic": { "type": "integer" }
, "network": { "$ref": "#/definitions/Network" }
, "activeSlotsCoefficient": { "$ref": "#/definitions/Ratio" }
, "securityParameter": { "type": "integer" }
, "epochLength": { "$ref": "#/definitions/Epoch" }
, "slotsPerKesPeriod": { "type": "integer" }
, "maxKesEvolutions": { "type": "integer" }
, "slotLength": { "type": "integer" }
, "updateQuorum": { "type": "integer" }
, "maxLovelaceSupply": { "type": "integer" }
, "protocolParameters": { "$ref": "#/definitions/ProtocolParameters[Shelley]" }
}
}

, "DlgCertificate":
{ "type": "object"
, "additionalProperties": false
Expand Down Expand Up @@ -2563,6 +2632,11 @@
]
}

, "UtcTime":
{ "type": "string"
, "pattern": "[0-9]{1,4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$"
}

, "Utxo":
{ "type": "array"
, "additionalItems": false
Expand Down
1 change: 1 addition & 0 deletions ogmios-server/src/Ogmios/Data/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ instance Crypto crypto => FromJSON (QueryInEra Proxy (CardanoBlock crypto)) wher
, Query.parseGetStakeDistribution id
, Query.parseGetUTxO (const id)
, Query.parseGetFilteredUTxO (const id)
, Query.parseGetGenesisConfig (const id)
]

instance Crypto crypto => FromJSON (Tip (CardanoBlock crypto)) where
Expand Down
24 changes: 23 additions & 1 deletion ogmios-server/src/Ogmios/Data/Json/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ module Ogmios.Data.Json.Prelude
, encodeDnsName
, encodeDouble
, encodeEpochNo
, encodeEpochSize
, encodeIPv4
, encodeIPv6
, encodeInteger
, encodeNatural
, encodeNominalDiffTime
, encodeNull
, encodePort
, encodeRational
Expand All @@ -45,6 +47,7 @@ module Ogmios.Data.Json.Prelude
, encodeText
, encodeUnitInterval
, encodeUrl
, encodeUtcTime
, encodeWord
, encodeWord16
, encodeWord32
Expand Down Expand Up @@ -72,7 +75,7 @@ import Cardano.Binary
import Cardano.Slotting.Block
( BlockNo (..) )
import Cardano.Slotting.Slot
( EpochNo (..), SlotNo (..) )
( EpochNo (..), EpochSize (..), SlotNo (..) )
import Codec.Binary.Bech32
( HumanReadablePart )
import Codec.Binary.Bech32.TH
Expand All @@ -93,6 +96,10 @@ import Data.Scientific
( Scientific )
import Data.Sequence.Strict
( StrictSeq )
import Data.Time.Clock
( NominalDiffTime, UTCTime )
import Data.Time.Format
( defaultTimeLocale, formatTime, iso8601DateFormat )
import Data.Vector
( Vector )
import Ouroboros.Consensus.BlockchainTime.WallClock.Types
Expand Down Expand Up @@ -186,6 +193,11 @@ encodeEpochNo =
encodeWord64 . unEpochNo
{-# INLINABLE encodeEpochNo #-}

encodeEpochSize :: EpochSize -> Json
encodeEpochSize =
encodeWord64 . unEpochSize
{-# INLINABLE encodeEpochSize #-}

encodeIPv4 :: IPv4 -> Json
encodeIPv4 =
encodeString . show
Expand All @@ -206,6 +218,11 @@ encodeNatural =
encodeInteger . toInteger
{-# INLINABLE encodeNatural #-}

encodeNominalDiffTime :: NominalDiffTime -> Json
encodeNominalDiffTime =
encodeInteger . round
{-# INLINABLE encodeNominalDiffTime #-}

encodeNull :: Json
encodeNull =
Json.Null
Expand Down Expand Up @@ -261,6 +278,11 @@ encodeUrl =
encodeText . urlToText
{-# INLINABLE encodeUrl #-}

encodeUtcTime :: UTCTime -> Json
encodeUtcTime =
encodeString . formatTime defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%S"))
{-# INLINABLE encodeUtcTime #-}

encodeWord :: Word -> Json
encodeWord =
Json.toJSON
Expand Down
45 changes: 45 additions & 0 deletions ogmios-server/src/Ogmios/Data/Json/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Ogmios.Data.Json.Query
, parseGetStakeDistribution
, parseGetUTxO
, parseGetFilteredUTxO
, parseGetGenesisConfig
) where

import Ogmios.Data.Json.Prelude
Expand Down Expand Up @@ -59,6 +60,8 @@ import Ouroboros.Consensus.Shelley.Eras
( AllegraEra, MaryEra, ShelleyEra )
import Ouroboros.Consensus.Shelley.Ledger.Block
( ShelleyBlock (..), ShelleyHash (..) )
import Ouroboros.Consensus.Shelley.Ledger.Config
( CompactGenesis, getCompactGenesis )
import Ouroboros.Consensus.Shelley.Ledger.Query
( NonMyopicMemberRewards (..), Query (..) )
import Ouroboros.Network.Block
Expand Down Expand Up @@ -450,6 +453,48 @@ parseGetFilteredUTxO genResultInEra = Json.withObject "SomeQuery" $ \obj -> do
parseAddresses obj = fmap fromList $
obj .: "utxo" >>= traverse parseAddress

parseGetGenesisConfig
:: forall f crypto. (Crypto crypto)
=> (forall era. Typeable era => Proxy era -> GenResult crypto f (CompactGenesis era))
-> Json
-> Json.Parser (QueryInEra f (CardanoBlock crypto))
parseGetGenesisConfig genResultInEra = do
Json.withText "SomeQuery" $ \text -> do
guard (text == "genesisConfig") $> \case
SomeShelleyEra ShelleyBasedEraShelley ->
Just $ SomeQuery
{ query =
QueryIfCurrentShelley GetGenesisConfig
, encodeResult =
let encodeGenesis =
Shelley.encodeCompactGenesis . getCompactGenesis
in either encodeMismatchEraInfo encodeGenesis
, genResult =
genResultInEra (Proxy @(ShelleyEra crypto))
}
SomeShelleyEra ShelleyBasedEraAllegra ->
Just $ SomeQuery
{ query =
QueryIfCurrentAllegra GetGenesisConfig
, encodeResult =
let encodeGenesis =
Shelley.encodeCompactGenesis . getCompactGenesis
in either encodeMismatchEraInfo encodeGenesis
, genResult =
genResultInEra (Proxy @(AllegraEra crypto))
}
SomeShelleyEra ShelleyBasedEraMary ->
Just $ SomeQuery
{ query =
QueryIfCurrentMary GetGenesisConfig
, encodeResult =
let encodeGenesis =
Shelley.encodeCompactGenesis . getCompactGenesis
in either encodeMismatchEraInfo encodeGenesis
, genResult =
genResultInEra (Proxy @(MaryEra crypto))
}

--
-- Internal
--
Expand Down
43 changes: 43 additions & 0 deletions ogmios-server/src/Ogmios/Data/Json/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import qualified Shelley.Spec.Ledger.BlockChain as Sh
import qualified Shelley.Spec.Ledger.Coin as Sh
import qualified Shelley.Spec.Ledger.Credential as Sh
import qualified Shelley.Spec.Ledger.Delegation.Certificates as Sh
import qualified Shelley.Spec.Ledger.Genesis as Sh
import qualified Shelley.Spec.Ledger.Keys as Sh
import qualified Shelley.Spec.Ledger.LedgerState as Sh
import qualified Shelley.Spec.Ledger.Metadata as Sh
Expand Down Expand Up @@ -155,6 +156,48 @@ encodeBootstrapWitness (Sh.BootstrapWitness key sig cc attr) = encodeObject
)
]

encodeCompactGenesis
:: Sh.ShelleyGenesis era
-> Json
encodeCompactGenesis x = encodeObject
[ ( "systemStart"
, encodeUtcTime (Sh.sgSystemStart x)
)
, ( "networkMagic"
, encodeWord32 (Sh.sgNetworkMagic x)
)
, ( "network"
, encodeNetwork (Sh.sgNetworkId x)
)
, ( "activeSlotsCoefficient"
, encodeRational (Sh.sgActiveSlotsCoeff x)
)
, ( "securityParameter"
, encodeWord64 (Sh.sgSecurityParam x)
)
, ( "epochLength"
, encodeEpochSize (Sh.sgEpochLength x)
)
, ( "slotsPerKesPeriod"
, encodeWord64 (Sh.sgSlotsPerKESPeriod x)
)
, ( "maxKesEvolutions"
, encodeWord64 (Sh.sgMaxKESEvolutions x)
)
, ( "slotLength"
, encodeNominalDiffTime (Sh.sgSlotLength x)
)
, ( "updateQuorum"
, encodeWord64 (Sh.sgUpdateQuorum x)
)
, ( "maxLovelaceSupply"
, encodeWord64 (Sh.sgMaxLovelaceSupply x)
)
, ( "protocolParameters"
, encodePParams' id (Sh.sgProtocolParams x)
)
]

encodeCertVRF
:: CC.VRFAlgorithm alg
=> CC.CertVRF alg
Expand Down
1 change: 1 addition & 0 deletions ogmios-server/static/tests/state-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe("StateQuery", () => {
}
, { "timeout": 5000 }
]
, [ "genesisConfig", { timeout: 2000 } ]
]

const gibberish =
Expand Down
Loading

0 comments on commit 03c1110

Please sign in to comment.