Skip to content

Commit c7cb138

Browse files
committed
Extend schema for offchain vote data
1 parent c69a37a commit c7cb138

File tree

6 files changed

+159
-24
lines changed

6 files changed

+159
-24
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ runGetVote :: Text.Text -> Maybe VoteMetaHash -> Bool -> IO ()
117117
runGetVote file mExpectedHash isGa = do
118118
respBs <- BS.readFile (Text.unpack file)
119119
let respLBs = fromStrict respBs
120-
(val, hsh, mWarning) <- runOrThrowIO $ runExceptT $ parseAndValidateVoteData respBs respLBs mExpectedHash isGa Nothing
120+
(ocvd, val, hsh, mWarning) <- runOrThrowIO $ runExceptT $ parseAndValidateVoteData respBs respLBs mExpectedHash isGa Nothing
121+
print ocvd
121122
print val
122123
print $ bsBase16Encode hsh
123124
print mWarning

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

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import Cardano.DbSync.Api
2424
import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..))
2525
import Cardano.DbSync.OffChain.Http
2626
import Cardano.DbSync.OffChain.Query
27+
import qualified Cardano.DbSync.OffChain.Vote.Types as Vote
2728
import Cardano.DbSync.Types
2829
import Cardano.Prelude
2930
import Control.Concurrent.Class.MonadSTM.Strict (
@@ -138,7 +139,9 @@ insertOffChainVoteResults trce resultQueue = do
138139
where
139140
insert :: (MonadBaseControl IO m, MonadIO m) => OffChainVoteResult -> ReaderT SqlBackend m ()
140141
insert = \case
141-
OffChainVoteResultMetadata md -> void $ DB.insertOffChainVoteData md
142+
OffChainVoteResultMetadata md _ _ _ -> do
143+
_ocvdId <- DB.insertOffChainVoteData md
144+
pure ()
142145
OffChainVoteResultError fe -> void $ DB.insertOffChainVoteFetchError fe
143146

144147
isFetchError :: OffChainVoteResult -> Bool
@@ -261,14 +264,28 @@ fetchOffChainVoteData _tracer manager time oVoteWorkQ =
261264
convert eres =
262265
case eres of
263266
Right sVoteData ->
264-
OffChainVoteResultMetadata $
265-
DB.OffChainVoteData
266-
{ DB.offChainVoteDataBytes = sovaBytes sVoteData
267-
, DB.offChainVoteDataHash = sovaHash sVoteData
268-
, DB.offChainVoteDataJson = sovaJson sVoteData
269-
, DB.offChainVoteDataVotingAnchorId = oVoteWqReferenceId oVoteWorkQ
270-
, DB.offChainVoteDataWarning = sovaWarning sVoteData
271-
}
267+
let
268+
offChainData = sovaOffChainVoteData sVoteData
269+
minimalBody = Vote.getMinimalBody offChainData
270+
vdt =
271+
DB.OffChainVoteData
272+
{ DB.offChainVoteDataLanguage = Vote.getLanguage offChainData
273+
, DB.offChainVoteDataComment = Vote.comment minimalBody
274+
, DB.offChainVoteDataTitle = Vote.getTitle offChainData
275+
, DB.offChainVoteDataAbstract = Vote.getAbstract offChainData
276+
, DB.offChainVoteDataMotivation = Vote.getMotivation offChainData
277+
, DB.offChainVoteDataRationale = Vote.getRationale offChainData
278+
, DB.offChainVoteDataBytes = sovaBytes sVoteData
279+
, DB.offChainVoteDataHash = sovaHash sVoteData
280+
, DB.offChainVoteDataJson = sovaJson sVoteData
281+
, DB.offChainVoteDataVotingAnchorId = oVoteWqReferenceId oVoteWorkQ
282+
, DB.offChainVoteDataWarning = sovaWarning sVoteData
283+
}
284+
authorsF ocvdId = map (mkAuthor ocvdId) $ Vote.getAuthors offChainData
285+
referencesF ocvdId = map (mkReference ocvdId) $ mListToList $ Vote.references minimalBody
286+
externalUpdatesF ocvdId = map (mkexternalUpdates ocvdId) $ mListToList $ Vote.externalUpdates minimalBody
287+
in
288+
OffChainVoteResultMetadata vdt authorsF referencesF externalUpdatesF
272289
Left err ->
273290
OffChainVoteResultError $
274291
DB.OffChainVoteFetchError
@@ -277,3 +294,32 @@ fetchOffChainVoteData _tracer manager time oVoteWorkQ =
277294
, DB.offChainVoteFetchErrorFetchTime = Time.posixSecondsToUTCTime time
278295
, DB.offChainVoteFetchErrorRetryCount = retryCount (oVoteWqRetry oVoteWorkQ)
279296
}
297+
mkAuthor ocvdId au =
298+
DB.OffChainVoteAuthor
299+
{ DB.offChainVoteAuthorOffChainVoteDataId = ocvdId
300+
, DB.offChainVoteAuthorName = Vote.name au
301+
, DB.offChainVoteAuthorWitnessAlgorithm = Vote.witnessAlgorithm $ Vote.witness au
302+
, DB.offChainVoteAuthorPublicKey = Vote.publicKey $ Vote.witness au
303+
, DB.offChainVoteAuthorSignature = Vote.signature $ Vote.witness au
304+
}
305+
306+
mkReference ocvdId ref =
307+
DB.OffChainVoteReference
308+
{ DB.offChainVoteReferenceOffChainVoteDataId = ocvdId
309+
, DB.offChainVoteReferenceLabel = Vote.label ref
310+
, DB.offChainVoteReferenceUri = Vote.uri ref
311+
, DB.offChainVoteReferenceHashDigest = Vote.hashDigest <$> Vote.referenceHash ref
312+
, DB.offChainVoteReferenceHashAlgorithm = Vote.rhHashAlgorithm <$> Vote.referenceHash ref
313+
}
314+
315+
mkexternalUpdates ocvdId eupd =
316+
DB.OffChainVoteExternalUpdate
317+
{ DB.offChainVoteExternalUpdateOffChainVoteDataId = ocvdId
318+
, DB.offChainVoteExternalUpdateTitle = Vote.euTitle eupd
319+
, DB.offChainVoteExternalUpdateUri = Vote.euUri eupd
320+
}
321+
322+
mListToList :: Maybe [a] -> [a]
323+
mListToList = \case
324+
Nothing -> []
325+
Just ls -> ls

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,22 @@ httpGetOffChainVoteData ::
8686
httpGetOffChainVoteData manager request vurl metaHash isGovAction = do
8787
httpRes <- handleExceptT (convertHttpException url) req
8888
(respBS, respLBS, mContentType) <- hoistEither httpRes
89-
(decodedValue, metadataHash, mWarning) <- parseAndValidateVoteData respBS respLBS metaHash isGovAction (Just $ OffChainVoteUrl vurl)
89+
(ocvd, decodedValue, metadataHash, mWarning) <- parseAndValidateVoteData respBS respLBS metaHash isGovAction (Just $ OffChainVoteUrl vurl)
9090
pure $
9191
SimplifiedOffChainVoteData
9292
{ sovaHash = metadataHash
9393
, sovaBytes = respBS
9494
, sovaJson = Text.decodeUtf8 $ LBS.toStrict (Aeson.encode decodedValue)
9595
, sovaContentType = mContentType
96+
, sovaOffChainVoteData = ocvd
9697
, sovaWarning = mWarning
9798
}
9899
where
99100
req = httpGetBytes manager request 10000 30000 url
100101
url = OffChainVoteUrl vurl
101102

102-
parseAndValidateVoteData :: ByteString -> LBS.ByteString -> Maybe VoteMetaHash -> Bool -> Maybe OffChainUrlType -> ExceptT OffChainFetchError IO (Aeson.Value, ByteString, Maybe Text)
103-
parseAndValidateVoteData bs lbs metaHash _isGa murl = do
103+
parseAndValidateVoteData :: ByteString -> LBS.ByteString -> Maybe VoteMetaHash -> Bool -> Maybe OffChainUrlType -> ExceptT OffChainFetchError IO (Vote.OffChainVoteData, Aeson.Value, ByteString, Maybe Text)
104+
parseAndValidateVoteData bs lbs metaHash isGa murl = do
104105
mWarning <- case unVoteMetaHash <$> metaHash of
105106
Nothing -> pure Nothing
106107
Just _expectedMetaHash -> pure Nothing
@@ -109,11 +110,11 @@ parseAndValidateVoteData bs lbs metaHash _isGa murl = do
109110
case Aeson.eitherDecode' @Aeson.Value lbs of
110111
Left err -> left $ OCFErrJsonDecodeFail murl (Text.pack err)
111112
Right res -> pure res
112-
_cdcCip <-
113-
case Aeson.eitherDecode' @(Vote.OffChainVoteData Vote.OtherOffChainData) lbs of
113+
ocvd <-
114+
case Vote.eitherDecodeOffChainVoteData lbs isGa of
114115
Left err -> left $ OCFErrJsonDecodeFail murl (Text.pack err)
115-
Right res -> liftIO $ print res
116-
pure (decodedValue, metadataHash, mWarning)
116+
Right res -> pure res
117+
pure (ocvd, decodedValue, metadataHash, mWarning)
117118

118119
httpGetBytes ::
119120
Http.Manager ->

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

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DeriveAnyClass #-}
33
{-# LANGUAGE DeriveGeneric #-}
44
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE LambdaCase #-}
56
{-# LANGUAGE OverloadedStrings #-}
67
{-# LANGUAGE ScopedTypeVariables #-}
78
{-# LANGUAGE StandaloneDeriving #-}
@@ -12,19 +13,70 @@ module Cardano.DbSync.OffChain.Vote.Types where
1213

1314
import Data.Aeson
1415
import Data.Aeson.Types
16+
import qualified Data.ByteString.Lazy as LBS
1517
import Data.Text (Text)
1618
import qualified Data.Text as Text
1719
import GHC.Generics
1820

19-
data OffChainVoteData tp = OffChainVoteData
21+
data OffChainVoteData
22+
= OffChainVoteDataOther (OffChainVoteDataTp OtherOffChainData)
23+
| OffChainVoteDataGa (OffChainVoteDataTp GovernanceOffChainData)
24+
deriving (Show)
25+
26+
getMinimalBody :: OffChainVoteData -> MinimalBody
27+
getMinimalBody = \case
28+
OffChainVoteDataOther dt -> body dt
29+
OffChainVoteDataGa dt -> minimalBody $ body dt
30+
31+
getTitle :: OffChainVoteData -> Maybe Text
32+
getTitle = \case
33+
OffChainVoteDataOther _ -> Nothing
34+
OffChainVoteDataGa ga -> Just $ title $ body ga
35+
36+
getAbstract :: OffChainVoteData -> Maybe Text
37+
getAbstract = \case
38+
OffChainVoteDataOther _ -> Nothing
39+
OffChainVoteDataGa ga -> Just $ abstract $ body ga
40+
41+
getMotivation :: OffChainVoteData -> Maybe Text
42+
getMotivation = \case
43+
OffChainVoteDataOther _ -> Nothing
44+
OffChainVoteDataGa ga -> Just $ motivation $ body ga
45+
46+
getRationale :: OffChainVoteData -> Maybe Text
47+
getRationale = \case
48+
OffChainVoteDataOther _ -> Nothing
49+
OffChainVoteDataGa ga -> Just $ rationale $ body ga
50+
51+
eitherDecodeOffChainVoteData :: LBS.ByteString -> Bool -> Either String OffChainVoteData
52+
eitherDecodeOffChainVoteData lbs isGa
53+
| isGa = OffChainVoteDataGa <$> eitherDecode' lbs
54+
| otherwise = OffChainVoteDataOther <$> eitherDecode' lbs
55+
56+
getAuthors :: OffChainVoteData -> [Author]
57+
getAuthors = \case
58+
OffChainVoteDataOther dt -> authors dt
59+
OffChainVoteDataGa dt -> authors dt
60+
61+
getHashAlgorithm :: OffChainVoteData -> Text
62+
getHashAlgorithm = \case
63+
OffChainVoteDataOther dt -> hashAlgorithm dt
64+
OffChainVoteDataGa dt -> hashAlgorithm dt
65+
66+
getLanguage :: OffChainVoteData -> Text
67+
getLanguage = \case
68+
OffChainVoteDataOther dt -> language $ context dt
69+
OffChainVoteDataGa dt -> language $ context dt
70+
71+
data OffChainVoteDataTp tp = OffChainVoteDataTp
2072
{ hashAlgorithm :: Text
2173
, authors :: [Author]
2274
, body :: Body tp
2375
, context :: Context
2476
}
2577

26-
deriving instance (Show (Body tp)) => Show (OffChainVoteData tp)
27-
deriving instance Generic (OffChainVoteData tp)
78+
deriving instance (Show (Body tp)) => Show (OffChainVoteDataTp tp)
79+
deriving instance Generic (OffChainVoteDataTp tp)
2880

2981
data Author = Author
3082
{ name :: Maybe Text
@@ -53,6 +105,7 @@ data GABody = GABody
53105
, motivation :: Text
54106
, rationale :: Text
55107
}
108+
deriving (Show)
56109

57110
data Reference = Reference
58111
{ rtype :: Text -- key is @type. It can be "GovernanceMetadata" or "Other" or ?? "other" ??
@@ -89,10 +142,10 @@ instance HasBody GovernanceOffChainData where
89142
type Body GovernanceOffChainData = GABody
90143
toMinimal = minimalBody
91144

92-
instance FromJSON (Body tp) => FromJSON (OffChainVoteData tp) where
145+
instance FromJSON (Body tp) => FromJSON (OffChainVoteDataTp tp) where
93146
parseJSON =
94-
withObject "offChainVoteData" $ \o ->
95-
OffChainVoteData
147+
withObject "offChainVoteDataTp" $ \o ->
148+
OffChainVoteDataTp
96149
<$> o .: "hashAlgorithm"
97150
<*> o .: "authors"
98151
<*> o .: "body"

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ module Cardano.DbSync.Types (
3333
import Cardano.Db (
3434
OffChainPoolData,
3535
OffChainPoolFetchError,
36+
OffChainVoteAuthor,
3637
OffChainVoteData,
38+
OffChainVoteExternalUpdate,
3739
OffChainVoteFetchError,
40+
OffChainVoteReference,
3841
PoolHashId,
3942
PoolMetaHash,
4043
PoolMetadataRefId,
@@ -44,6 +47,7 @@ import Cardano.Db (
4447
VotingAnchorId,
4548
)
4649
import qualified Cardano.Db as DB
50+
import qualified Cardano.DbSync.OffChain.Vote.Types as Vote
4751
import qualified Cardano.Ledger.Credential as Ledger
4852
import Cardano.Ledger.Crypto (StandardCrypto)
4953
import qualified Cardano.Ledger.Hashes as Ledger
@@ -140,7 +144,11 @@ data OffChainPoolResult
140144
| OffChainPoolResultError !OffChainPoolFetchError
141145

142146
data OffChainVoteResult
143-
= OffChainVoteResultMetadata !OffChainVoteData
147+
= OffChainVoteResultMetadata
148+
!OffChainVoteData
149+
(DB.OffChainVoteDataId -> [OffChainVoteAuthor])
150+
(DB.OffChainVoteDataId -> [OffChainVoteReference])
151+
(DB.OffChainVoteDataId -> [OffChainVoteExternalUpdate])
144152
| OffChainVoteResultError !OffChainVoteFetchError
145153

146154
data OffChainPoolWorkQueue = OffChainPoolWorkQueue
@@ -174,6 +182,7 @@ data SimplifiedOffChainVoteData = SimplifiedOffChainVoteData
174182
, sovaBytes :: !ByteString
175183
, sovaJson :: !Text
176184
, sovaContentType :: !(Maybe ByteString)
185+
, sovaOffChainVoteData :: !Vote.OffChainVoteData
177186
, sovaWarning :: !(Maybe Text)
178187
}
179188

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,37 @@ share
677677
OffChainVoteData
678678
votingAnchorId VotingAnchorId noreference
679679
hash ByteString
680+
language Text
681+
comment Text Maybe
682+
title Text Maybe
683+
abstract Text Maybe
684+
motivation Text Maybe
685+
rationale Text Maybe
680686
json Text sqltype=jsonb
681687
bytes ByteString sqltype=bytea
682688
warning Text Maybe
683689
UniqueOffChainVoteData votingAnchorId hash
684690
deriving Show
685691

692+
OffChainVoteAuthor
693+
offChainVoteDataId OffChainVoteDataId noreference
694+
name Text Maybe
695+
witnessAlgorithm Text
696+
publicKey Text
697+
signature Text
698+
699+
OffChainVoteReference
700+
offChainVoteDataId OffChainVoteDataId noreference
701+
label Text
702+
uri Text
703+
hashDigest Text Maybe
704+
hashAlgorithm Text Maybe
705+
706+
OffChainVoteExternalUpdate
707+
offChainVoteDataId OffChainVoteDataId noreference
708+
title Text
709+
uri Text
710+
686711
OffChainVoteFetchError
687712
votingAnchorId VotingAnchorId noreference
688713
fetchError Text

0 commit comments

Comments
 (0)