From 8121673c7f266550e7b4f3f66c6e3938a6b78899 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 11 Aug 2022 11:43:26 +0200 Subject: [PATCH] Enforce backward-compatibility when encoding NominalDiffTime. Keep encoding as integer when the value is a whole number of seconds. Otherwise, this may break clients down the line. --- clients/TypeScript/packages/schema/src/index.ts | 2 +- docs/static/ogmios.wsp.json | 10 ++++------ server/src/Ogmios/Data/Json/Prelude.hs | 11 +++++++++-- server/test/unit/Ogmios/Data/JsonSpec.hs | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/clients/TypeScript/packages/schema/src/index.ts b/clients/TypeScript/packages/schema/src/index.ts index 279c8e58e1..374cada53d 100644 --- a/clients/TypeScript/packages/schema/src/index.ts +++ b/clients/TypeScript/packages/schema/src/index.ts @@ -330,7 +330,7 @@ export type RelativeTime = number; */ export type QueryUnavailableInCurrentEra = "QueryUnavailableInCurrentEra"; /** - * A slot length, in seconds. + * A slot length, in seconds. Starting from v5.5.4, this can be a floating number. Before v5.5.4, the floating value would be rounded to the nearest second. */ export type SlotLength = number; /** diff --git a/docs/static/ogmios.wsp.json b/docs/static/ogmios.wsp.json index 0b75decf53..9186fc1056 100644 --- a/docs/static/ogmios.wsp.json +++ b/docs/static/ogmios.wsp.json @@ -3641,10 +3641,9 @@ } , "RelativeTime": - { "type": "integer" - , "description": "A time in seconds relative to another one (typically, system start or era start)." + { "type": "number" + , "description": "A time in seconds relative to another one (typically, system start or era start). Starting from v5.5.4, this can be a floating number. Before v5.5.4, the floating value would be rounded to the nearest second." , "minimum": 0 - , "maximum": 18446744073709552999 } , "Relay": @@ -4169,9 +4168,8 @@ } , "SlotLength": - { "type": "integer" - , "description": "A slot length, in seconds." - , "maximum": 18446744073709552000 + { "type": "number" + , "description": "A slot length, in seconds. Starting from v5.5.4, this can be a floating number. Before v5.5.4, the floating value would be rounded to the nearest second." } , "SoftwareVersion": diff --git a/server/src/Ogmios/Data/Json/Prelude.hs b/server/src/Ogmios/Data/Json/Prelude.hs index e699da226a..96e9f8266f 100644 --- a/server/src/Ogmios/Data/Json/Prelude.hs +++ b/server/src/Ogmios/Data/Json/Prelude.hs @@ -125,6 +125,8 @@ import Data.ByteString.Bech32 ( HumanReadablePart, encodeBech32 ) import Data.IP ( IPv4, IPv6 ) +import Data.Ratio + ( (%) ) import Data.Scientific ( Scientific ) import Data.Sequence.Strict @@ -295,8 +297,13 @@ encodeNatural = {-# INLINABLE encodeNatural #-} encodeNominalDiffTime :: NominalDiffTime -> Json -encodeNominalDiffTime = - Json.double . fromRational . toRational +encodeNominalDiffTime t = + -- TODO / NOTE: Backward-compatibility prior to v5.5.4. Should encode only + -- as Double in v6+ + if i % 1 == r then Json.integer i else Json.double (fromRational r) + where + r = toRational t + i = round t {-# INLINABLE encodeNominalDiffTime #-} encodeNonNegativeInterval :: NonNegativeInterval -> Json diff --git a/server/test/unit/Ogmios/Data/JsonSpec.hs b/server/test/unit/Ogmios/Data/JsonSpec.hs index fd857b2ddb..3bf41b195b 100644 --- a/server/test/unit/Ogmios/Data/JsonSpec.hs +++ b/server/test/unit/Ogmios/Data/JsonSpec.hs @@ -385,7 +385,7 @@ spec = do \7A80D87A801A002625A0D87983D879801A000F4240D879811A000FA92E" context "SlotLength" $ do - let matrix = [ ( mkSlotLength 1, Json.double 1 ) + let matrix = [ ( mkSlotLength 1, Json.integer 1 ) , ( mkSlotLength 0.1, Json.double 0.1 ) ] forM_ matrix $ \(slotLength, json) ->