Skip to content

Commit f37b381

Browse files
Cmdvkderme
authored andcommitted
fix review comments for offchain data
1 parent 2bef23b commit f37b381

File tree

8 files changed

+43
-47
lines changed

8 files changed

+43
-47
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import Cardano.Db (PoolMetaHash (..), PoolUrl (..))
44
import Cardano.DbSync (
55
OffChainFetchError (..),
66
SimplifiedOffChainPoolData (..),
7-
httpGetOffChainPoolData,
8-
parseOffChainPoolUrl,
97
)
108
import Cardano.DbSync.Error (runOrThrowIO)
9+
import Cardano.DbSync.OffChain.Http (httpGetOffChainPoolData, parseOffChainPoolUrl)
1110
import Cardano.DbSync.Types (OffChainHashType (..), OffChainUrlType (..))
1211
import Control.Monad.IO.Class (liftIO)
1312
import Control.Monad.Trans.Except (ExceptT, runExceptT)

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ module Cardano.DbSync (
2121
-- For testing and debugging
2222
OffChainFetchError (..),
2323
SimplifiedOffChainPoolData (..),
24-
httpGetOffChainPoolData,
25-
parseOffChainPoolUrl,
26-
httpGetOffChainVoteData,
27-
parseOffChainVoteUrl,
2824
) where
2925

3026
import Cardano.BM.Trace (Trace, logError, logInfo, logWarning)
@@ -51,12 +47,6 @@ import Cardano.DbSync.Era
5147
import Cardano.DbSync.Error (SyncNodeError, hasAbortOnPanicEnv, runOrThrowIO)
5248
import Cardano.DbSync.Ledger.State
5349
import Cardano.DbSync.OffChain (runFetchOffChainPoolThread, runFetchOffChainVoteThread)
54-
import Cardano.DbSync.OffChain.Http (
55-
httpGetOffChainPoolData,
56-
httpGetOffChainVoteData,
57-
parseOffChainPoolUrl,
58-
parseOffChainVoteUrl,
59-
)
6050
import Cardano.DbSync.Rollback (unsafeRollback)
6151
import Cardano.DbSync.Sync (runSyncNodeClient)
6252
import Cardano.DbSync.Tracing.ToObjectOrphans ()

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
118118

119119
slid <- lift . DB.insertSlotLeader $ Generic.mkSlotLeader (ioShelley iopts) (Generic.unKeyHashRaw $ Generic.blkSlotLeader blk) (eitherToMaybe mPhid)
120120
blkId <-
121-
lift
122-
. insertBlockAndCache cache
123-
$ DB.Block
121+
lift . insertBlockAndCache cache $
122+
DB.Block
124123
{ DB.blockHash = Generic.blkHash blk
125124
, DB.blockEpochNo = Just $ unEpochNo epochNo
126125
, DB.blockSlotNo = Just $ unSlotNo (Generic.blkSlotNo blk)
@@ -197,7 +196,7 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is
197196

198197
insertStakeSlice syncEnv $ apStakeSlice applyResult
199198

200-
unless (ioGov iopts)
199+
when (ioGov iopts)
201200
. lift
202201
$ insertOffChainVoteResults tracer (envOffChainVoteResultQueue syncEnv)
203202

cardano-db-sync/src/Cardano/DbSync/OffChain.hs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ loadOffChainWorkQueue ::
8484
loadOffChainWorkQueue _trce offChainWorkQueue = do
8585
whenM (liftIO $ atomically (isEmptyTBQueue (lQueue offChainWorkQueue))) $ do
8686
now <- liftIO Time.getPOSIXTime
87-
runnablePools <- filter (isRunnable now offChainWorkQueue) <$> lGetData offChainWorkQueue now 100
88-
liftIO $ mapM_ (queueInsert offChainWorkQueue) runnablePools
87+
runnableOffChainPools <- filter (isRunnable now offChainWorkQueue) <$> lGetData offChainWorkQueue now 100
88+
liftIO $ mapM_ (queueInsert offChainWorkQueue) runnableOffChainPools
8989
where
9090
isRunnable :: POSIXTime -> LoadOffChainWorkQueue a m -> a -> Bool
9191
isRunnable now oCWorkQueue locWq = retryRetryTime (lRetryTime oCWorkQueue locWq) <= now
@@ -103,13 +103,11 @@ insertOffChainPoolResults ::
103103
ReaderT SqlBackend m ()
104104
insertOffChainPoolResults trce resultQueue = do
105105
res <- liftIO . atomically $ flushTBQueue resultQueue
106-
let resLength = length res
107-
resErrorsLength = length $ filter isFetchError res
108-
unless (null res)
109-
$ liftIO
110-
. logInfo trce
111-
$ logInsertOffChainResults "Pool" resLength resErrorsLength
112-
106+
unless (null res) $ do
107+
let resLength = length res
108+
resErrorsLength = length $ filter isFetchError res
109+
liftIO . logInfo trce $
110+
logInsertOffChainResults "Pool" resLength resErrorsLength
113111
mapM_ insert res
114112
where
115113
insert :: (MonadBaseControl IO m, MonadIO m) => OffChainPoolResult -> ReaderT SqlBackend m ()
@@ -129,12 +127,11 @@ insertOffChainVoteResults ::
129127
ReaderT SqlBackend m ()
130128
insertOffChainVoteResults trce resultQueue = do
131129
res <- liftIO . atomically $ flushTBQueue resultQueue
132-
let resLength = length res
133-
resErrorsLength = length $ filter isFetchError res
134-
unless (null res)
135-
$ liftIO
136-
. logInfo trce
137-
$ logInsertOffChainResults "Voting Anchor" resLength resErrorsLength
130+
unless (null res) $ do
131+
let resLength = length res
132+
resErrorsLength = length $ filter isFetchError res
133+
liftIO . logInfo trce $
134+
logInsertOffChainResults "Voting Anchor" resLength resErrorsLength
138135
mapM_ insert res
139136
where
140137
insert :: (MonadBaseControl IO m, MonadIO m) => OffChainVoteResult -> ReaderT SqlBackend m ()
@@ -174,7 +171,7 @@ runFetchOffChainPoolThread syncEnv = do
174171
forever $ do
175172
tDelay
176173
-- load the offChain vote work queue using the db
177-
_ <- runReaderT (loadOffChainVoteWorkQueue trce (envOffChainVoteWorkQueue syncEnv)) (envBackend syncEnv)
174+
_ <- runReaderT (loadOffChainPoolWorkQueue trce (envOffChainPoolWorkQueue syncEnv)) (envBackend syncEnv)
178175
poolq <- blockingFlushTBQueue (envOffChainPoolWorkQueue syncEnv)
179176
manager <- Http.newManager tlsManagerSettings
180177
now <- liftIO Time.getPOSIXTime
@@ -189,12 +186,12 @@ runFetchOffChainPoolThread syncEnv = do
189186
runFetchOffChainVoteThread :: SyncEnv -> IO ()
190187
runFetchOffChainVoteThread syncEnv = do
191188
-- if dissable gov is active then don't run voting anchor thread
192-
unless (ioGov iopts) $ do
189+
when (ioGov iopts) $ do
193190
logInfo trce "Running Offchain Vote Anchor fetch thread"
194191
forever $ do
195192
tDelay
196193
-- load the offChain pool work queue using the db
197-
_ <- runReaderT (loadOffChainPoolWorkQueue trce (envOffChainPoolWorkQueue syncEnv)) (envBackend syncEnv)
194+
_ <- runReaderT (loadOffChainVoteWorkQueue trce (envOffChainVoteWorkQueue syncEnv)) (envBackend syncEnv)
198195
voteq <- blockingFlushTBQueue (envOffChainVoteWorkQueue syncEnv)
199196
manager <- Http.newManager tlsManagerSettings
200197
now <- liftIO Time.getPOSIXTime

cardano-db-sync/src/Cardano/DbSync/OffChain/Http.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,12 @@ httpGetOffChain manager request mHash url =
147147
. left
148148
$ OCFErrHashMismatch url (renderByteArray eMetaHash) (renderByteArray metadataHash)
149149

150-
decodedMetadata <-
151-
case Aeson.eitherDecode' respLBS of
152-
Left err -> left $ OCFErrJsonDecodeFail url (Text.pack err)
153-
Right res -> pure res
154-
155150
case url of
156-
OffChainPoolUrl _ ->
151+
OffChainPoolUrl _ -> do
152+
decodedMetadata <-
153+
case Aeson.eitherDecode' respLBS of
154+
Left err -> left $ OCFErrJsonDecodeFail url (Text.pack err)
155+
Right res -> pure res
157156
pure $
158157
SimplifiedOffChainPoolDataType
159158
SimplifiedOffChainPoolData
@@ -172,10 +171,8 @@ httpGetOffChain manager request mHash url =
172171
SimplifiedOffChainVoteData
173172
{ sovaHash = metadataHash
174173
, sovaBytes = respBS
175-
, -- Instead of inserting the `respBS` here, we encode the JSON and then store that.
176-
-- This is necessary because the PostgreSQL JSON parser can reject some ByteStrings
177-
-- that the Aeson parser accepts.
178-
sovaJson = Text.decodeUtf8 $ LBS.toStrict (Aeson.encode decodedMetadata)
174+
, -- TODO: no json format decided for vote metadata yet so we are leaving it blank for now
175+
sovaJson = ""
179176
, sovaContentType = mContentType
180177
}
181178

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,14 @@ insertCheckOffChainPoolFetchError pofe = do
316316
when (foundPool && foundMeta) . void $ insertCheckUnique "OffChainPoolFetchError" pofe
317317

318318
insertOffChainVoteData :: (MonadBaseControl IO m, MonadIO m) => OffChainVoteData -> ReaderT SqlBackend m ()
319-
insertOffChainVoteData pod = void $ insertCheckUnique "OffChainVoteData" pod
319+
insertOffChainVoteData ocvd = do
320+
foundVotingAnchor <- existsVotingAnchorId (offChainVoteDataVotingAnchorId ocvd)
321+
when foundVotingAnchor . void $ insertCheckUnique "OffChainVoteData" ocvd
320322

321323
insertOffChainVoteFetchError :: (MonadBaseControl IO m, MonadIO m) => OffChainVoteFetchError -> ReaderT SqlBackend m ()
322-
insertOffChainVoteFetchError pofe = void $ insertCheckUnique "OffChainVoteFetchError" pofe
324+
insertOffChainVoteFetchError ocvfe = do
325+
foundVotingAnchor <- existsVotingAnchorId (offChainVoteFetchErrorVotingAnchorId ocvfe)
326+
when foundVotingAnchor . void $ insertCheckUnique "OffChainVoteFetchError" ocvfe
323327

324328
insertReservedPoolTicker :: (MonadBaseControl IO m, MonadIO m) => ReservedPoolTicker -> ReaderT SqlBackend m (Maybe ReservedPoolTickerId)
325329
insertReservedPoolTicker ticker = do

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module Cardano.Db.Query (
5353
queryMaxRefId,
5454
existsPoolHashId,
5555
existsPoolMetadataRefId,
56+
existsVotingAnchorId,
5657
queryAdaPotsId,
5758
queryBlockHeight,
5859
queryAllExtraMigrations,
@@ -741,6 +742,15 @@ existsPoolMetadataRefId pmrid = do
741742
pure (pmr ^. PoolMetadataRefId)
742743
pure $ not (null res)
743744

745+
existsVotingAnchorId :: MonadIO m => VotingAnchorId -> ReaderT SqlBackend m Bool
746+
existsVotingAnchorId vaId = do
747+
res <- select $ do
748+
votingAnchor <- from $ table @VotingAnchor
749+
where_ (votingAnchor ^. VotingAnchorId ==. val vaId)
750+
limit 1
751+
pure (votingAnchor ^. VotingAnchorId)
752+
pure $ not (null res)
753+
744754
queryAdaPotsId :: MonadIO m => BlockId -> ReaderT SqlBackend m (Maybe (Entity AdaPots))
745755
queryAdaPotsId blkId = do
746756
res <- select $ do

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ newtype VoteUrl = VoteUrl {unVoteUrl :: Text}
238238
deriving (Eq, Ord, Generic)
239239
deriving (Show) via (Quiet VoteUrl)
240240

241-
-- | The raw binary hash of a stake vote metadata.
241+
-- | The raw binary hash of a vote metadata.
242242
newtype VoteMetaHash = VoteMetaHash {unVoteMetaHash :: ByteString}
243243
deriving (Eq, Ord, Generic)
244244
deriving (Show) via (Quiet VoteMetaHash)

0 commit comments

Comments
 (0)