Skip to content

Commit eaf60dd

Browse files
committed
Fix IPV6 parser
1 parent b02718b commit eaf60dd

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/Internal/QueryM/Ogmios.purs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,25 +724,37 @@ parseIpv6String str = do
724724
padded = String.replaceAll (Pattern " ") (Replacement "0") $ fold $
725725
partsFixed
726726
<#> StringUtils.padStart 4
727-
decodeCbor <<< wrap =<< hexToByteArray padded
727+
decodeCbor <<< wrap =<< hexToByteArray ("50" <> padded)
728728

729729
decodeRelay :: Aeson -> Either JsonDecodeError Relay
730730
decodeRelay aeson = do
731731
obj <- decodeAeson aeson
732732
let
733+
guardType :: String -> String -> Either JsonDecodeError Unit
734+
guardType fname expectedType = do
735+
otype <- obj .: "type"
736+
when (otype /= expectedType) $ do
737+
Left $ TypeMismatch $ fname <> ": expected '" <> expectedType
738+
<> "', got: '"
739+
<> otype
740+
<> "' for "
741+
<> Aeson.stringifyAeson aeson
733742
decodeSingleHostAddr = do
743+
guardType "SingleHostAddr" "ipAddress"
734744
port <- obj .:? "port"
735745
ipv4 <- obj .:? "ipv4" >>= traverse decodeIpv4
736746
ipv6 <- obj .:? "ipv6" >>= traverse decodeIpv6
737747
pure $ SingleHostAddr { port, ipv4, ipv6 }
738748
decodeSingleHostName = do
749+
guardType "SingleHostName" "hostname"
739750
port <- obj .: "port"
740751
dnsName <- obj .: "hostname"
741752
pure $ SingleHostName { port, dnsName }
742753
decodeMultiHostName = do
754+
guardType "MultiHostName" "hostname"
743755
dnsName <- obj .: "hostname"
744756
pure $ MultiHostName { dnsName }
745-
decodeSingleHostName <|> decodeSingleHostAddr <|> decodeMultiHostName
757+
decodeSingleHostName <|> decodeMultiHostName <|> decodeSingleHostAddr
746758

747759
decodePoolMetadata :: Aeson -> Either JsonDecodeError PoolMetadata
748760
decodePoolMetadata aeson = do

test/Types/Ipv6.purs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@ import Prelude
77
import Cardano.AsCbor (decodeCbor)
88
import Ctl.Internal.QueryM.Ogmios (parseIpv6String)
99
import Data.ByteArray (hexToByteArrayUnsafe)
10+
import Data.Maybe (Maybe(Nothing))
1011
import Data.Newtype (wrap)
1112
import Effect.Aff (Aff)
1213
import Mote (group, test)
1314
import Mote.TestPlanM (TestPlanM)
14-
import Test.Spec.Assertions (shouldEqual)
15+
import Test.Spec.Assertions (shouldEqual, shouldNotEqual)
1516

1617
suite :: TestPlanM (Aff Unit) Unit
1718
suite = do
1819
group "Ipv6 type (parsing)" do
1920
testIpv6 "2345:425:2CA1:0000:0000:567:5673:23b5"
20-
"234504252CA1000000000567567323b5"
21+
"50234504252CA1000000000567567323b5"
2122
testIpv6 "2345:0425:2CA1:0:0:0567:5673:23b5"
22-
"234504252CA1000000000567567323b5"
23-
testIpv6 "2345:0425:2CA1::0567:5673:23b5" "234504252CA1000000000567567323b5"
24-
testIpv6 "2345:0425:2CA1::5673:23b5" "234504252CA1000000000000567323b5"
25-
testIpv6 "2345:0425:2CA1::23b5" "234504252CA1000000000000000023b5"
23+
"50234504252CA1000000000567567323b5"
24+
testIpv6 "2345:0425:2CA1::0567:5673:23b5"
25+
"50234504252CA1000000000567567323b5"
26+
testIpv6 "2345:0425:2CA1::5673:23b5" "50234504252CA1000000000000567323b5"
27+
testIpv6 "2345:0425:2CA1::23b5" "50234504252CA1000000000000000023b5"
2628

2729
testIpv6 :: String -> String -> TestPlanM (Aff Unit) Unit
2830
testIpv6 str expected =
2931
test str do
30-
parseIpv6String str `shouldEqual`
31-
(decodeCbor (wrap $ hexToByteArrayUnsafe expected))
32+
let ipv6 = parseIpv6String str
33+
ipv6 `shouldNotEqual` Nothing
34+
ipv6 `shouldEqual` (decodeCbor (wrap $ hexToByteArrayUnsafe expected))

0 commit comments

Comments
 (0)