Skip to content

Commit 8625330

Browse files
committed
Fix queryPoolFetchRetry
This query was incorrectly putting the Bech32 pool indentifier into the pool URL field of the PoolFetchRetry struct (both were of type `Text` in the database schema). Instead of just fixing this in a simple/naive manner, the PoolUrl type was pushed into the schema (no migration needed) to make querying that field type safe. Closes: #1347
1 parent 07d3587 commit 8625330

File tree

7 files changed

+27
-14
lines changed

7 files changed

+27
-14
lines changed

cardano-db-sync/app/test-http-get-json-metadata.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ queryTestOfflineData = do
147147
)
148148
pure . organise $ map (convert . unValue4) res
149149
where
150-
convert :: (Text, Text, ByteString, PoolHashId) -> (PoolHashId, TestOffline)
150+
convert :: (Text, PoolUrl, ByteString, PoolHashId) -> (PoolHashId, TestOffline)
151151
convert (tname, url, hash, poolId) =
152152
( poolId
153153
, TestOffline
154154
{ toTicker = tname
155-
, toUrl = PoolUrl url
155+
, toUrl = url
156156
, toHash = PoolMetaHash hash
157157
}
158158
)

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Cardano.Api.Shelley (
2626
import Cardano.BM.Trace (Trace, logDebug, logInfo, logWarning)
2727
import Cardano.Crypto.Hash (hashToBytes)
2828
import qualified Cardano.Crypto.Hashing as Crypto
29-
import Cardano.Db (DbLovelace (..), DbWord64 (..))
29+
import Cardano.Db (DbLovelace (..), DbWord64 (..), PoolUrl (..))
3030
import qualified Cardano.Db as DB
3131
import Cardano.DbSync.Api
3232
import Cardano.DbSync.Cache
@@ -540,7 +540,7 @@ insertMetaDataRef poolId txId md =
540540
lift . DB.insertPoolMetadataRef $
541541
DB.PoolMetadataRef
542542
{ DB.poolMetadataRefPoolId = poolId
543-
, DB.poolMetadataRefUrl = Ledger.urlToText (Shelley._poolMDUrl md)
543+
, DB.poolMetadataRefUrl = PoolUrl $ Ledger.urlToText (Shelley._poolMDUrl md)
544544
, DB.poolMetadataRefHash = Shelley._poolMDHash md
545545
, DB.poolMetadataRefRegisteredTxId = txId
546546
}

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Offline/Http.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import Network.HTTP.Client (HttpException (..))
3232
import qualified Network.HTTP.Client as Http
3333
import qualified Network.HTTP.Types as Http
3434

35-
-- |Fetch error for the HTTP client fetching the pool offline metadata.
35+
-- | Fetch error for the HTTP client fetching the pool offline metadata.
3636
data FetchError
3737
= FEHashMismatch !PoolUrl !Text !Text
3838
| FEDataTooLong !PoolUrl

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Offline/Query.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Cardano.DbSync.Era.Shelley.Offline.Query (
66
) where
77

88
import Cardano.Db (
9-
EntityField (PoolHashId, PoolHashView, PoolMetadataRefHash, PoolMetadataRefId, PoolMetadataRefPoolId, PoolMetadataRefUrl, PoolOfflineDataPmrId, PoolOfflineFetchErrorFetchTime, PoolOfflineFetchErrorId, PoolOfflineFetchErrorPmrId, PoolOfflineFetchErrorPoolId, PoolOfflineFetchErrorRetryCount),
9+
EntityField (PoolHashId, PoolMetadataRefHash, PoolMetadataRefId, PoolMetadataRefPoolId, PoolMetadataRefUrl, PoolOfflineDataPmrId, PoolOfflineFetchErrorFetchTime, PoolOfflineFetchErrorId, PoolOfflineFetchErrorPmrId, PoolOfflineFetchErrorPoolId, PoolOfflineFetchErrorRetryCount),
1010
PoolHash,
1111
PoolHashId,
1212
PoolMetaHash (PoolMetaHash),
@@ -15,7 +15,7 @@ import Cardano.Db (
1515
PoolOfflineData,
1616
PoolOfflineFetchError,
1717
PoolOfflineFetchErrorId,
18-
PoolUrl (PoolUrl),
18+
PoolUrl,
1919
)
2020
import Cardano.DbSync.Era.Shelley.Offline.FetchQueue (newRetry, retryAgain)
2121
import Cardano.DbSync.Types (PoolFetchRetry (..))
@@ -92,13 +92,13 @@ queryNewPoolFetch now = do
9292
pure $ max_ (pmr ^. PoolMetadataRefId)
9393

9494
convert ::
95-
(Value PoolHashId, Value PoolMetadataRefId, Value Text, Value ByteString) ->
95+
(Value PoolHashId, Value PoolMetadataRefId, Value PoolUrl, Value ByteString) ->
9696
PoolFetchRetry
9797
convert (Value phId, Value pmrId, Value url, Value pmh) =
9898
PoolFetchRetry
9999
{ pfrPoolHashId = phId
100100
, pfrReferenceId = pmrId
101-
, pfrPoolUrl = PoolUrl url
101+
, pfrPoolUrl = url
102102
, pfrPoolMDHash = Just $ PoolMetaHash pmh
103103
, pfrRetry = newRetry now
104104
}
@@ -120,7 +120,7 @@ queryPoolFetchRetry _now = do
120120
pure
121121
( pofe ^. PoolOfflineFetchErrorFetchTime
122122
, pofe ^. PoolOfflineFetchErrorPmrId
123-
, ph ^. PoolHashView
123+
, pmr ^. PoolMetadataRefUrl
124124
, pmr ^. PoolMetadataRefHash
125125
, ph ^. PoolHashId
126126
, pofe ^. PoolOfflineFetchErrorRetryCount
@@ -138,13 +138,13 @@ queryPoolFetchRetry _now = do
138138
pure $ max_ (pofe ^. PoolOfflineFetchErrorId)
139139

140140
convert ::
141-
(Value UTCTime, Value PoolMetadataRefId, Value Text, Value ByteString, Value PoolHashId, Value Word) ->
141+
(Value UTCTime, Value PoolMetadataRefId, Value PoolUrl, Value ByteString, Value PoolHashId, Value Word) ->
142142
PoolFetchRetry
143143
convert (Value time, Value pmrId, Value url, Value pmh, Value phId, Value rCount) =
144144
PoolFetchRetry
145145
{ pfrPoolHashId = phId
146146
, pfrReferenceId = pmrId
147-
, pfrPoolUrl = PoolUrl url
147+
, pfrPoolUrl = url
148148
, pfrPoolMDHash = Just $ PoolMetaHash pmh
149149
, pfrRetry = retryAgain (Time.utcTimeToPOSIXSeconds time) rCount
150150
}

cardano-db/src/Cardano/Db/Schema.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
module Cardano.Db.Schema where
1919

2020
import Cardano.Db.Schema.Orphans ()
21+
import Cardano.Db.Schema.Types (
22+
PoolUrl,
23+
)
2124
import Cardano.Db.Types (
2225
DbInt65,
2326
DbLovelace,
@@ -220,7 +223,7 @@ share
220223

221224
PoolMetadataRef
222225
poolId PoolHashId noreference
223-
url Text
226+
url PoolUrl sqltype=varchar
224227
hash ByteString sqltype=hash32type
225228
registeredTxId TxId noreference -- Only used for rollback.
226229
UniquePoolMetadataRef poolId url hash

cardano-db/src/Cardano/Db/Schema/Orphans.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
module Cardano.Db.Schema.Orphans () where
55

6+
import Cardano.Db.Schema.Types (
7+
PoolUrl (..),
8+
)
69
import Cardano.Db.Types (
710
DbInt65 (..),
811
DbLovelace (..),
@@ -73,6 +76,13 @@ instance PersistField DbWord64 where
7376
fromPersistValue x =
7477
Left $ mconcat ["Failed to parse Haskell type DbWord64: ", Text.pack (show x)]
7578

79+
instance PersistField PoolUrl where
80+
toPersistValue = PersistText . unPoolUrl
81+
fromPersistValue (PersistText txt) = Right $ PoolUrl txt
82+
fromPersistValue (PersistByteString bs) = Right $ PoolUrl (Text.decodeLatin1 bs)
83+
fromPersistValue x =
84+
Left $ mconcat ["Failed to parse Haskell type PoolUrl: ", Text.pack (show x)]
85+
7686
instance PersistField RewardSource where
7787
toPersistValue = PersistText . showRewardSource
7888
fromPersistValue (PersistLiteral bs) = Right $ readRewardSource (Text.decodeLatin1 bs)

cardano-db/test/Test/IO/Cardano/Db/Insert.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ poolMetadataRef :: TxId -> PoolHashId -> PoolMetadataRef
181181
poolMetadataRef txid phid =
182182
PoolMetadataRef
183183
{ poolMetadataRefPoolId = phid
184-
, poolMetadataRefUrl = "best.pool.com"
184+
, poolMetadataRefUrl = PoolUrl "best.pool.com"
185185
, poolMetadataRefHash = mkHash 32 '4'
186186
, poolMetadataRefRegisteredTxId = txid
187187
}

0 commit comments

Comments
 (0)