Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 3ab2bd4

Browse files
committed
[CO-410] Modify ProtocolMagic's FromJSON instance to support legacy encoding
1 parent 0260569 commit 3ab2bd4

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

crypto/Pos/Crypto/Configuration.hs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
13
module Pos.Crypto.Configuration
24
( ProtocolMagic (..)
35
, ProtocolMagicId (..)
@@ -11,6 +13,7 @@ import Control.Monad.Except (MonadError)
1113
import Data.Aeson ((.:), (.=))
1214
import qualified Data.Aeson as A
1315
import Data.Aeson.Types (typeMismatch)
16+
import qualified Data.HashMap.Strict as HM
1417
import Data.List (lookup)
1518
import Data.SafeCopy (base, deriveSafeCopySimple)
1619
import Text.JSON.Canonical (FromJSON (..), JSValue (..), ToJSON (..),
@@ -102,10 +105,19 @@ instance A.FromJSON ProtocolMagic where
102105
parseJSON v@(A.Number _) = ProtocolMagic
103106
<$> (ProtocolMagicId <$> A.parseJSON v)
104107
<*> pure RequiresMagic
105-
parseJSON (A.Object o) = ProtocolMagic
106-
<$> (ProtocolMagicId <$> o .: "pm")
107-
<*> o .: "requiresNetworkMagic"
108-
parseJSON invalid = typeMismatch "Coord" invalid
108+
parseJSON (A.Object o) =
109+
case HM.lookup "requiresNetworkMagic" o of
110+
-- legacy codebase JSON encoding
111+
Just (A.String "NMMustBeJust")
112+
-> ProtocolMagic <$> (ProtocolMagicId <$> o .: "pm")
113+
<*> pure RequiresMagic
114+
Just (A.String "NMMustBeNothing")
115+
-> ProtocolMagic <$> (ProtocolMagicId <$> o .: "pm")
116+
<*> pure RequiresNoMagic
117+
-- current codebase JSON encoding
118+
_ -> ProtocolMagic <$> (ProtocolMagicId <$> o .: "pm")
119+
<*> o .: "requiresNetworkMagic"
120+
parseJSON invalid = typeMismatch "ProtocolMagic" invalid
109121

110122
-- Canonical JSON instances
111123
instance Monad m => ToJSON m ProtocolMagic where

0 commit comments

Comments
 (0)