Skip to content

Commit f74e8d8

Browse files
committed
Implement parsing for drep registration metadata
Supports partsing CIP-119 metadata
1 parent 14345b2 commit f74e8d8

File tree

7 files changed

+170
-50
lines changed

7 files changed

+170
-50
lines changed

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
{-# LANGUAGE LambdaCase #-}
12
{-# LANGUAGE OverloadedStrings #-}
23

34
import Cardano.Db (PoolMetaHash (..), PoolUrl (..), VoteMetaHash (..), VoteUrl (..))
5+
import qualified Cardano.Db as DB
46
import Cardano.DbSync.Error (bsBase16Encode, runOrThrowIO)
57
import Cardano.DbSync.OffChain.Http
68
import Cardano.DbSync.Types
@@ -30,9 +32,10 @@ import System.Exit (exitFailure)
3032
main :: IO ()
3133
main = do
3234
xs <- getArgs
35+
let voteType = getVoteType xs
3336
case cleanOpt xs of
34-
[url] -> runGet (isItVote xs) (isItGa xs) (isItLink xs) (Text.pack url) Nothing
35-
[url, hash] -> runGet (isItVote xs) (isItGa xs) (isItLink xs) (Text.pack url) (Just $ parseHash hash)
37+
[url] -> runGet voteType (isItLink xs) (Text.pack url) Nothing
38+
[url, hash] -> runGet voteType (isItLink xs) (Text.pack url) (Just $ parseHash hash)
3639
_otherwise -> usageExit
3740
where
3841
parseHash :: String -> BS.ByteString
@@ -44,17 +47,30 @@ main = do
4447
cleanOpt :: [String] -> [String]
4548
cleanOpt ls = List.delete "ga" $ List.delete "vote" $ List.delete "pool" ls
4649

47-
isItVote ls = List.elem "vote" ls || isItGa ls
48-
isItGa = List.elem "ga"
50+
getVoteType :: [String] -> Maybe OffChainVoteType
51+
getVoteType ls
52+
| "ga" `List.elem` ls = Just GovAction
53+
| "drep" `List.elem` ls = Just DrepReg
54+
| "vote" `List.elem` ls = Just Other
55+
| otherwise = Nothing
4956
isItLink = List.elem "url"
5057

51-
runGet isVote isGa isLink url mhsh
52-
| isVote && isLink =
53-
runHttpGetVote (VoteUrl url) (VoteMetaHash <$> mhsh) isGa
54-
| isVote && not isLink =
55-
runGetVote url (VoteMetaHash <$> mhsh) isGa
56-
| otherwise =
57-
runHttpGetPool (PoolUrl url) (PoolMetaHash <$> mhsh)
58+
runGet mvtype isLink url mhsh = case mvtype of
59+
Just vtype
60+
| isLink ->
61+
runHttpGetVote (VoteUrl url) (VoteMetaHash <$> mhsh) (toDBOffChainVoteType vtype)
62+
Just vtype ->
63+
runGetVote url (VoteMetaHash <$> mhsh) (toDBOffChainVoteType vtype)
64+
_ ->
65+
runHttpGetPool (PoolUrl url) (PoolMetaHash <$> mhsh)
66+
67+
data OffChainVoteType = GovAction | DrepReg | Other
68+
69+
toDBOffChainVoteType :: OffChainVoteType -> DB.AnchorType
70+
toDBOffChainVoteType = \case
71+
GovAction -> DB.GovActionAnchor
72+
DrepReg -> DB.DrepAnchor
73+
Other -> DB.OtherAnchor
5874

5975
-- -------------------------------------------------------------------------------------------------
6076

@@ -93,15 +109,15 @@ runHttpGetPool poolUrl mHash =
93109
else putStrLn $ orangeText ("Warning: This should be 'application/json'\nContent-type: " ++ BSC.unpack ct)
94110
Text.putStrLn $ spodJson spod
95111

96-
runHttpGetVote :: VoteUrl -> Maybe VoteMetaHash -> Bool -> IO ()
97-
runHttpGetVote voteUrl mHash isGa =
112+
runHttpGetVote :: VoteUrl -> Maybe VoteMetaHash -> DB.AnchorType -> IO ()
113+
runHttpGetVote voteUrl mHash vtype =
98114
reportSuccess =<< runOrThrowIO (runExceptT httpGet)
99115
where
100116
httpGet :: ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
101117
httpGet = do
102118
request <- parseOffChainUrl $ OffChainVoteUrl voteUrl
103119
manager <- liftIO $ Http.newManager tlsManagerSettings
104-
httpGetOffChainVoteData manager request voteUrl mHash isGa
120+
httpGetOffChainVoteData manager request voteUrl mHash vtype
105121

106122
reportSuccess :: SimplifiedOffChainVoteData -> IO ()
107123
reportSuccess spod = do
@@ -113,11 +129,11 @@ runHttpGetVote voteUrl mHash isGa =
113129
else putStrLn $ orangeText ("Warning: This should be 'application/json'\nContent-type: " ++ BSC.unpack ct)
114130
Text.putStrLn $ sovaJson spod
115131

116-
runGetVote :: Text.Text -> Maybe VoteMetaHash -> Bool -> IO ()
117-
runGetVote file mExpectedHash isGa = do
132+
runGetVote :: Text.Text -> Maybe VoteMetaHash -> DB.AnchorType -> IO ()
133+
runGetVote file mExpectedHash vtype = do
118134
respBs <- BS.readFile (Text.unpack file)
119135
let respLBs = fromStrict respBs
120-
(ocvd, val, hsh, mWarning) <- runOrThrowIO $ runExceptT $ parseAndValidateVoteData respBs respLBs mExpectedHash isGa Nothing
136+
(ocvd, val, hsh, mWarning) <- runOrThrowIO $ runExceptT $ parseAndValidateVoteData respBs respLBs mExpectedHash vtype Nothing
121137
print ocvd
122138
print val
123139
print $ bsBase16Encode hsh

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ insertDrepRegistration ::
245245
ReaderT SqlBackend m ()
246246
insertDrepRegistration blkId txId idx cred mcoin mAnchor = do
247247
drepId <- insertCredDrepHash cred
248-
votingAnchorId <- whenMaybe mAnchor $ insertVotingAnchor blkId DB.OtherAnchor
248+
votingAnchorId <- whenMaybe mAnchor $ insertVotingAnchor blkId DB.DrepAnchor
249249
void
250250
. DB.insertDrepRegistration
251251
$ DB.DrepRegistration

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fetchOffChainVoteData _tracer manager time oVoteWorkQ =
262262
let url = oVoteWqUrl oVoteWorkQ
263263
metaHash = oVoteWqMetaHash oVoteWorkQ
264264
request <- parseOffChainUrl $ OffChainVoteUrl url
265-
httpGetOffChainVoteData manager request url (Just metaHash) (oVoteWqType oVoteWorkQ == DB.GovActionAnchor)
265+
httpGetOffChainVoteData manager request url (Just metaHash) (oVoteWqType oVoteWorkQ)
266266
where
267267
convert :: Either OffChainFetchError SimplifiedOffChainVoteData -> OffChainVoteResult
268268
convert eres =

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Cardano.DbSync.OffChain.Http (
1212
import qualified Cardano.Crypto.Hash.Blake2b as Crypto
1313
import qualified Cardano.Crypto.Hash.Class as Crypto
1414
import Cardano.Db (PoolMetaHash (..), PoolUrl (..), VoteMetaHash (..), VoteUrl (..), textShow)
15+
import qualified Cardano.Db as DB
1516
import Cardano.DbSync.OffChain.Types (
1617
PoolOffChainMetadata (..),
1718
PoolTicker (..),
@@ -81,12 +82,12 @@ httpGetOffChainVoteData ::
8182
Http.Request ->
8283
VoteUrl ->
8384
Maybe VoteMetaHash ->
84-
Bool ->
85+
DB.AnchorType ->
8586
ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
86-
httpGetOffChainVoteData manager request vurl metaHash isGovAction = do
87+
httpGetOffChainVoteData manager request vurl metaHash anchorType = do
8788
httpRes <- handleExceptT (convertHttpException url) req
8889
(respBS, respLBS, mContentType) <- hoistEither httpRes
89-
(ocvd, decodedValue, metadataHash, mWarning) <- parseAndValidateVoteData respBS respLBS metaHash isGovAction (Just $ OffChainVoteUrl vurl)
90+
(ocvd, decodedValue, metadataHash, mWarning) <- parseAndValidateVoteData respBS respLBS metaHash anchorType (Just $ OffChainVoteUrl vurl)
9091
pure $
9192
SimplifiedOffChainVoteData
9293
{ sovaHash = metadataHash
@@ -100,8 +101,8 @@ httpGetOffChainVoteData manager request vurl metaHash isGovAction = do
100101
req = httpGetBytes manager request 10000 30000 url
101102
url = OffChainVoteUrl vurl
102103

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
104+
parseAndValidateVoteData :: ByteString -> LBS.ByteString -> Maybe VoteMetaHash -> DB.AnchorType -> Maybe OffChainUrlType -> ExceptT OffChainFetchError IO (Vote.OffChainVoteData, Aeson.Value, ByteString, Maybe Text)
105+
parseAndValidateVoteData bs lbs metaHash anchorType murl = do
105106
let metadataHash = Crypto.digest (Proxy :: Proxy Crypto.Blake2b_256) bs
106107
(hsh, mWarning) <- case unVoteMetaHash <$> metaHash of
107108
Nothing -> pure (metadataHash, Nothing)
@@ -111,7 +112,7 @@ parseAndValidateVoteData bs lbs metaHash isGa murl = do
111112
Left err -> left $ OCFErrJsonDecodeFail murl (Text.pack err)
112113
Right res -> pure res
113114
ocvd <-
114-
case Vote.eitherDecodeOffChainVoteData lbs isGa of
115+
case Vote.eitherDecodeOffChainVoteData lbs anchorType of
115116
Left err -> left $ OCFErrJsonDecodeFail murl (Text.pack err)
116117
Right res -> pure res
117118
pure (ocvd, decodedValue, hsh, mWarning)

0 commit comments

Comments
 (0)