Skip to content

Commit 06e4bbe

Browse files
committed
Add a warning and populate the json field of Vote metadata
1 parent 7eddf39 commit 06e4bbe

File tree

5 files changed

+58
-22
lines changed

5 files changed

+58
-22
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,22 @@ loadOffChainVoteWorkQueue trce workQueue =
7878
}
7979

8080
loadOffChainWorkQueue ::
81+
forall a m.
8182
(MonadBaseControl IO m, MonadIO m) =>
8283
Trace IO Text ->
8384
LoadOffChainWorkQueue a m ->
8485
ReaderT SqlBackend m ()
8586
loadOffChainWorkQueue _trce offChainWorkQueue = do
8687
whenM (liftIO $ atomically (isEmptyTBQueue (lQueue offChainWorkQueue))) $ do
8788
now <- liftIO Time.getPOSIXTime
88-
runnableOffChainData <- filter (isRunnable now offChainWorkQueue) <$> lGetData offChainWorkQueue now 100
89-
liftIO $ mapM_ (queueInsert offChainWorkQueue) runnableOffChainData
89+
runnableOffChainData <- filter (isRunnable now) <$> lGetData offChainWorkQueue now 100
90+
liftIO $ mapM_ queueInsert runnableOffChainData
9091
where
91-
isRunnable :: POSIXTime -> LoadOffChainWorkQueue a m -> a -> Bool
92-
isRunnable now oCWorkQueue locWq = retryRetryTime (lRetryTime oCWorkQueue locWq) <= now
92+
isRunnable :: POSIXTime -> a -> Bool
93+
isRunnable now locWq = retryRetryTime (lRetryTime offChainWorkQueue locWq) <= now
9394

94-
queueInsert :: LoadOffChainWorkQueue a m -> a -> IO ()
95-
queueInsert oCWorkQueue locWq = atomically $ writeTBQueue (lQueue oCWorkQueue) locWq
95+
queueInsert :: a -> IO ()
96+
queueInsert locWq = atomically $ writeTBQueue (lQueue offChainWorkQueue) locWq
9697

9798
---------------------------------------------------------------------------------------------------------------------------------
9899
-- Insert OffChain
@@ -266,6 +267,7 @@ fetchOffChainVoteData _tracer manager time oVoteWorkQ =
266267
, DB.offChainVoteDataHash = sovaHash sVoteData
267268
, DB.offChainVoteDataJson = sovaJson sVoteData
268269
, DB.offChainVoteDataVotingAnchorId = oVoteWqReferenceId oVoteWorkQ
270+
, DB.offChainVoteDataWarning = sovaWarning sVoteData
269271
}
270272
Left err ->
271273
OffChainVoteResultError $

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE LambdaCase #-}
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE NoImplicitPrelude #-}
4+
{-# LANGUAGE TypeApplications #-}
45

56
module Cardano.DbSync.OffChain.Http (
67
httpGetOffChainPoolData,
@@ -26,7 +27,6 @@ import Cardano.DbSync.Types (
2627
)
2728
import Cardano.DbSync.Util (renderByteArray)
2829
import Cardano.Prelude hiding (show)
29-
import Control.Monad.Extra (whenJust)
3030
import Control.Monad.Trans.Except.Extra (handleExceptT, hoistEither, left)
3131
import qualified Data.Aeson as Aeson
3232
import qualified Data.ByteString.Char8 as BS
@@ -100,8 +100,11 @@ httpGetOffChain manager request mHash url =
100100
. left
101101
$ OCFErrHttpResponse url (Http.statusCode status) (Text.decodeLatin1 $ Http.statusMessage status)
102102

103-
-- Read a maxiumm of 600 bytes and then later check if the length exceeds 512 bytes.
104-
respLBS <- liftIO $ Http.brReadSome (Http.responseBody responseBR) 600
103+
let (bytesToRead, maxBytes) =
104+
case url of
105+
OffChainPoolUrl _ -> (600, 512)
106+
OffChainVoteUrl _ -> (3000, 3000)
107+
respLBS <- liftIO $ Http.brReadSome (Http.responseBody responseBR) bytesToRead
105108
let respBS = LBS.toStrict respLBS
106109

107110
let mContentType = List.lookup Http.hContentType (Http.responseHeaders responseBR)
@@ -132,20 +135,24 @@ httpGetOffChain manager request mHash url =
132135
. left
133136
$ OCFErrBadContentType url (Text.decodeLatin1 ct)
134137

135-
unless (BS.length respBS <= 512)
138+
unless (BS.length respBS <= maxBytes)
136139
. left
137140
$ OCFErrDataTooLong url
138141

139142
let metadataHash = Crypto.digest (Proxy :: Proxy Crypto.Blake2b_256) respBS
140143

141-
whenJust mHash $ \expectedMetaHash -> do
142-
let eMetaHash =
143-
case expectedMetaHash of
144-
OffChainPoolHash (PoolMetaHash m) -> m
145-
OffChainVoteHash (VoteMetaHash m) -> m
146-
when (metadataHash /= eMetaHash)
147-
. left
148-
$ OCFErrHashMismatch url (renderByteArray eMetaHash) (renderByteArray metadataHash)
144+
mWarning <- case mHash of
145+
Nothing -> pure Nothing
146+
Just expectedMetaHash -> do
147+
case expectedMetaHash of
148+
OffChainPoolHash (PoolMetaHash m) ->
149+
if metadataHash /= m
150+
then left $ OCFErrHashMismatch url (renderByteArray m) (renderByteArray metadataHash)
151+
else pure Nothing
152+
OffChainVoteHash (VoteMetaHash m) ->
153+
if metadataHash /= m
154+
then pure $ Just $ textShow $ OCFErrHashMismatch url (renderByteArray m) (renderByteArray metadataHash)
155+
else pure Nothing
149156

150157
case url of
151158
OffChainPoolUrl _ -> do
@@ -165,15 +172,19 @@ httpGetOffChain manager request mHash url =
165172
spodJson = Text.decodeUtf8 $ LBS.toStrict (Aeson.encode decodedMetadata)
166173
, spodContentType = mContentType
167174
}
168-
OffChainVoteUrl _ ->
175+
OffChainVoteUrl _ -> do
176+
decodedMetadata <-
177+
case Aeson.eitherDecode' @Aeson.Value respLBS of
178+
Left err -> left $ OCFErrJsonDecodeFail url (Text.pack err)
179+
Right res -> pure res
169180
pure $
170181
SimplifiedOffChainVoteDataType
171182
SimplifiedOffChainVoteData
172183
{ sovaHash = metadataHash
173184
, sovaBytes = respBS
174-
, -- TODO: no json format decided for vote metadata yet so we are leaving it blank for now
175-
sovaJson = "{}"
185+
, sovaJson = Text.decodeUtf8 $ LBS.toStrict (Aeson.encode decodedMetadata)
176186
, sovaContentType = mContentType
187+
, sovaWarning = mWarning
177188
}
178189

179190
-- | Is the provided ByteSring possibly JSON object?

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ data SimplifiedOffChainVoteData = SimplifiedOffChainVoteData
183183
, sovaBytes :: !ByteString
184184
, sovaJson :: !Text
185185
, sovaContentType :: !(Maybe ByteString)
186+
, sovaWarning :: !(Maybe Text)
186187
}
187188

188189
data Retry = Retry

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ share
660660
hash ByteString
661661
json Text sqltype=jsonb
662662
bytes ByteString sqltype=bytea
663+
warning Text Maybe
663664
UniqueOffChainVoteData votingAnchorId hash
664665
deriving Show
665666

@@ -1250,11 +1251,13 @@ schemaDocs =
12501251
VotingProcedureVotingAnchorId # "The VotingAnchor table index associated with this VotingProcedure."
12511252

12521253
OffChainVoteData --^ do
1253-
"The table with the offchain metadata related to Vote Anchors. New in 13.2-Conway."
1254+
"The table with the offchain metadata related to Vote Anchors. It accepts metadata in a more lenient way than what's\
1255+
\ decribed in CIP-100. New in 13.2-Conway."
12541256
OffChainVoteDataVotingAnchorId # "The VotingAnchor table index this offchain data refers."
12551257
OffChainVoteDataHash # "The hash of the offchain data."
12561258
OffChainVoteDataJson # "The payload as JSON."
12571259
OffChainVoteDataBytes # "The raw bytes of the payload."
1260+
OffChainVoteDataWarning # "A warning that occured while validating the metadata."
12581261

12591262
OffChainVoteFetchError --^ do
12601263
"Errors while fetching or validating offchain Voting Anchor metadata. New in 13.2-Conway."

schema/migration-2-0035-20231218.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Persistent generated migration.
2+
3+
CREATE FUNCTION migrate() RETURNS void AS $$
4+
DECLARE
5+
next_version int ;
6+
BEGIN
7+
SELECT stage_two + 1 INTO next_version FROM schema_version ;
8+
IF next_version = 35 THEN
9+
EXECUTE 'ALTER TABLE "off_chain_vote_data" ADD COLUMN "warning" VARCHAR NULL' ;
10+
-- Hand written SQL statements can be added here.
11+
UPDATE schema_version SET stage_two = next_version ;
12+
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;
13+
END IF ;
14+
END ;
15+
$$ LANGUAGE plpgsql ;
16+
17+
SELECT migrate() ;
18+
19+
DROP FUNCTION migrate() ;

0 commit comments

Comments
 (0)