Skip to content

Commit cb0cd5c

Browse files
committed
feat: add l2-certification
1 parent 15d51ac commit cb0cd5c

30 files changed

+1225
-274
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Makefile
1414
*.sqlite
1515
react-web/node_modules/
1616
*.ignore.*
17+
todo.txt

dapps-certification-persistence/dapps-certification-persistence.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ library
99
other-modules: IOHK.Certification.Persistence.Structure
1010
, IOHK.Certification.Persistence.API
1111
, IOHK.Certification.Persistence.Structure.Subscription
12+
, IOHK.Certification.Persistence.Structure.Certification
13+
, IOHK.Certification.Persistence.Structure.Run
1214
, IOHK.Certification.Persistence.Structure.Profile
1315
build-depends: base
1416
, selda

dapps-certification-persistence/src/IOHK/Certification/Persistence.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
module IOHK.Certification.Persistence (module X) where
2-
import IOHK.Certification.Persistence.Structure as X
2+
import IOHK.Certification.Persistence.Structure.Run as X
33
( Run(..)
44
, Status(..)
5-
, DApp(..)
6-
, Certification(..)
5+
)
6+
import IOHK.Certification.Persistence.Structure.Certification as X
7+
( Certification(..)
78
, L1Certification(..)
9+
, CertificationLevel(..)
810
, L1CertificationDTO(..)
11+
)
12+
import IOHK.Certification.Persistence.Structure as X
13+
( DApp(..)
914
, ProfileDTO(..)
10-
, runs
1115
, createTables
1216
, IpfsCid(..)
1317
, TxId(..)

dapps-certification-persistence/src/IOHK/Certification/Persistence/API.hs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{-# LANGUAGE OverloadedLabels #-}
2-
{-# LANGUAGE OverloadedStrings #-}
3-
{-# LANGUAGE RecordWildCards #-}
4-
{-# LANGUAGE ScopedTypeVariables #-}
5-
{-# LANGUAGE TypeOperators #-}
6-
{-# LANGUAGE OverloadedRecordDot #-}
7-
{-# LANGUAGE NamedFieldPuns #-}
1+
{-# LANGUAGE OverloadedLabels #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE TypeOperators #-}
6+
{-# LANGUAGE OverloadedRecordDot #-}
7+
{-# LANGUAGE DuplicateRecordFields #-}
88

99
module IOHK.Certification.Persistence.API where
1010

@@ -15,6 +15,8 @@ import Database.Selda
1515
import Database.Selda.SQLite
1616
import IOHK.Certification.Persistence.Structure.Profile
1717
import IOHK.Certification.Persistence.Structure.Subscription as Subscription
18+
import IOHK.Certification.Persistence.Structure.Run
19+
import IOHK.Certification.Persistence.Structure.Certification
1820
import IOHK.Certification.Persistence.Structure
1921
import Data.Time.Clock
2022
import Data.Int
@@ -314,7 +316,7 @@ createL1Certificate runId TxId{..} time = transaction $ do
314316
-- and now add a l1Certification
315317
let l1Cert = L1Certification runId certId
316318
_ <- insert l1Certifications [l1Cert]
317-
pure $ Just (L1CertificationDTO l1Cert (cert { certId }))
319+
pure $ Just (L1CertificationDTO l1Cert (#certId certId cert))
318320
_ -> pure Nothing
319321

320322
getL1CertificationQuery :: UUID -> Query t (Row t Certification :*: Row t L1Certification)

dapps-certification-persistence/src/IOHK/Certification/Persistence/Structure.hs

Lines changed: 3 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{-# LANGUAGE DuplicateRecordFields #-}
44
{-# LANGUAGE FlexibleInstances #-}
55
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6-
{-# LANGUAGE InstanceSigs #-}
76
{-# LANGUAGE MultiParamTypeClasses #-}
87
{-# LANGUAGE OverloadedLabels #-}
98
{-# LANGUAGE OverloadedLists #-}
@@ -22,12 +21,12 @@ import Data.Proxy
2221
--import Control.Exception ( throw)
2322
import Data.Swagger hiding (Contact)
2423
import Database.Selda
25-
--import Database.Selda.SqlType as Selda
26-
import GHC.OverloadedLabels
2724
import Data.Int
2825

2926
import IOHK.Certification.Persistence.Structure.Profile
3027
import IOHK.Certification.Persistence.Structure.Subscription
28+
import IOHK.Certification.Persistence.Structure.Certification
29+
import IOHK.Certification.Persistence.Structure.Run
3130

3231
import qualified Data.Text as Text
3332
import qualified Data.Aeson.KeyMap as KM
@@ -118,118 +117,6 @@ instance ToSchema ProfileDTO where
118117
return $ NamedSchema (Just "ProfileDTO") $ profileSchema
119118
& properties %~ (`mappend` [ ("dapp", dappSchema) ])
120119

121-
--------------------------------------------------------------------------------
122-
-- | Certification
123-
{-
124-
data CertificationLevel = L0 | L1 | L2 | L3
125-
deriving (Generic,Show,Read,Eq,Ord,Enum)
126-
127-
instance SqlType CertificationLevel where
128-
mkLit n = LCustom TInt64 (LInt64 (toInt64 n))
129-
where
130-
toInt64 = \case
131-
L0 -> 0
132-
L1 -> 1
133-
L2 -> 2
134-
L3 -> 3
135-
sqlType _ = TInt64
136-
fromSql (SqlInt64 0) = L0
137-
fromSql (SqlInt64 1) = L1
138-
fromSql (SqlInt64 2) = L2
139-
fromSql (SqlInt64 3) = L3
140-
fromSql v = throw $ userError $ "fromSql: expected SqlInt64, got " ++ show v
141-
defaultValue = mkLit L1
142-
143-
instance ToJSON CertificationLevel where
144-
toJSON = toJSON . \case
145-
L0 -> "l0" :: String
146-
L1 -> "l1"
147-
L2 -> "l2"
148-
L3 -> "l3"
149-
150-
instance FromJSON CertificationLevel where
151-
parseJSON = withText "CertificationLevel" $ \case
152-
"l0" -> pure L0
153-
"l1" -> pure L1
154-
"l2" -> pure L2
155-
"l3" -> pure L3
156-
_ -> fail "CertificationLevel"
157-
158-
instance ToSchema CertificationLevel where
159-
declareNamedSchema _ = do
160-
let values = [ "l0", "l1", "l2", "l3" ] :: [Value]
161-
return $ NamedSchema (Just "CertificationLevel") $ mempty
162-
& type_ ?~ SwaggerString
163-
& enum_ ?~ values
164-
-}
165-
166-
data Certification = Certification
167-
{ certId :: ID Certification
168-
, certTransactionId :: Text
169-
, certCreatedAt :: UTCTime
170-
} deriving (Generic,Show)
171-
172-
instance FromJSON Certification where
173-
parseJSON = withObject "Certification" $ \v -> Certification def
174-
<$> v .: "transactionId"
175-
<*> v .: "createdAt"
176-
177-
instance ToJSON Certification where
178-
toJSON (Certification{..}) = object
179-
[ "transactionId" .= certTransactionId
180-
, "createdAt" .= certCreatedAt
181-
]
182-
183-
instance ToSchema Certification where
184-
declareNamedSchema _ = do
185-
textSchema <- declareSchemaRef (Proxy :: Proxy Text)
186-
utcSchema <- declareSchemaRef (Proxy :: Proxy UTCTime)
187-
return $ NamedSchema (Just "Certification") $ mempty
188-
& type_ ?~ SwaggerObject
189-
& properties .~
190-
[ ("transactionId", textSchema)
191-
, ("createdAt", utcSchema)
192-
]
193-
& required .~ [ "createdAt" ]
194-
195-
instance SqlRow Certification
196-
197-
-- one to one mapping with Run
198-
data L1Certification = L1Certification
199-
{ l1CertRunId :: UUID
200-
, l1CertId :: ID Certification
201-
} deriving (Generic,Show)
202-
203-
instance SqlRow L1Certification
204-
205-
data L1CertificationDTO = L1CertificationDTO
206-
{ l1Certification :: L1Certification
207-
, certification :: Certification
208-
}
209-
210-
instance ToJSON L1CertificationDTO where
211-
toJSON L1CertificationDTO{..} = Object (x <> y)
212-
where
213-
x = case toJSON certification of
214-
Object obj -> obj
215-
_ -> KM.empty
216-
y = KM.fromList [ "runId" .= l1CertRunId l1Certification ]
217-
218-
instance FromJSON L1CertificationDTO where
219-
parseJSON = withObject "L1CertificationDTO" $ \v -> do
220-
l1Certification <- L1Certification
221-
<$> v .: "runId"
222-
<*> pure def
223-
L1CertificationDTO l1Certification <$> v .: "runId"
224-
225-
instance ToSchema L1CertificationDTO where
226-
declareNamedSchema _ = do
227-
certificationSchema <- declareSchema (Proxy :: Proxy Certification)
228-
uuidSchema <- declareSchemaRef (Proxy :: Proxy UUID)
229-
return $ NamedSchema (Just "TierDTO") $ certificationSchema
230-
& properties %~ (`mappend` [ ("certRunId", uuidSchema) ])
231-
& required %~ (<> [ "certRunId" ])
232-
233120
--------------------------------------------------------------------------------
234121
-- | Dapp
235122

@@ -275,115 +162,6 @@ instance ToJSON DApp where
275162

276163
instance SqlRow DApp
277164

278-
--------------------------------------------------------------------------------
279-
-- | Run
280-
281-
data Status = Queued | Failed | Succeeded | ReadyForCertification | Certified | Aborted
282-
deriving (Show, Read, Bounded, Enum, Eq, Generic)
283-
284-
instance ToJSON Status where
285-
toJSON :: Status -> Value
286-
toJSON Queued = toJSON ("queued" :: Text)
287-
toJSON Failed = toJSON ("failed" :: Text)
288-
toJSON Succeeded = toJSON ("succeeded" :: Text)
289-
toJSON ReadyForCertification = toJSON ("ready-for-certification" :: Text)
290-
toJSON Certified = toJSON ("certified" :: Text)
291-
toJSON Aborted = toJSON ("aborted" :: Text)
292-
293-
instance FromJSON Status where
294-
parseJSON =
295-
withText "Status" handle
296-
where
297-
handle "queued" = pure Queued
298-
handle "failed" = pure Failed
299-
handle "succeeded" = pure Succeeded
300-
handle "certified" = pure Succeeded
301-
handle "ready-for-certification" = pure ReadyForCertification
302-
handle "aborted" = pure Aborted
303-
handle t = fail $ "provided text (" ++ show t ++ ") is not a Status"
304-
305-
instance SqlType Status
306-
307-
type CommitHash = Text
308-
type CertificationPrice = Int64
309-
data Run = Run
310-
{ runId :: UUID
311-
, created :: UTCTime
312-
, finishedAt :: Maybe UTCTime
313-
, syncedAt :: UTCTime
314-
, repoUrl :: Text
315-
, commitDate :: UTCTime
316-
, commitHash :: CommitHash
317-
, runStatus :: Status
318-
, profileId :: ID Profile
319-
, certificationPrice :: CertificationPrice
320-
, reportContentId :: Maybe Text
321-
} deriving (Generic,Show)
322-
323-
instance ToSchema Status where
324-
declareNamedSchema _ = do
325-
let values = ["queued", "failed", "succeeded", "certified", "ready-for-certification","aborted"] :: [Value]
326-
return $ NamedSchema (Just "RunStatus") $ mempty
327-
& type_ ?~ SwaggerString
328-
& enum_ ?~ values
329-
330-
instance ToSchema Run where
331-
declareNamedSchema _ = do
332-
utcSchema <- declareSchemaRef (Proxy :: Proxy UTCTime)
333-
utcSchemaM <- declareSchemaRef (Proxy :: Proxy (Maybe UTCTime))
334-
textSchema <- declareSchemaRef (Proxy :: Proxy Text)
335-
statusSchema <- declareSchemaRef (Proxy :: Proxy Status)
336-
uuidSchema <- declareSchemaRef (Proxy :: Proxy UUID)
337-
intSchema <- declareSchemaRef (Proxy :: Proxy Int)
338-
return $ NamedSchema (Just "Run") $ mempty
339-
& type_ ?~ SwaggerObject
340-
& properties .~
341-
[ ("created", utcSchema)
342-
, ("runId", uuidSchema)
343-
, ("finishedAt", utcSchemaM)
344-
, ("syncedAt", utcSchema)
345-
, ("repoUrl", textSchema)
346-
, ("commitDate", utcSchema)
347-
, ("commitHash", textSchema)
348-
, ("runStatus", statusSchema)
349-
, ("certificationPrice", intSchema)
350-
, ("reportContentId", textSchema)
351-
]
352-
& required .~ [ "runId", "created", "utcSchema", "repoUrl"
353-
, "commitDate","commitHash", "runStatus", "certificationPrice"]
354-
355-
instance ToJSON Run where
356-
toJSON (Run{..}) = object
357-
[ "runId" .= runId
358-
, "created" .= created
359-
, "finishedAt" .= finishedAt
360-
, "syncedAt" .= syncedAt
361-
, "repoUrl" .= repoUrl
362-
, "commitDate" .= commitDate
363-
, "commitHash" .= commitHash
364-
, "runStatus" .= runStatus
365-
, "certificationPrice" .= certificationPrice
366-
, "reportContentId" .= reportContentId
367-
]
368-
369-
instance FromJSON Run where
370-
parseJSON = withObject "Run" $ \v -> Run
371-
<$> v .: "runId"
372-
<*> v .: "created"
373-
<*> v .:? "finishedAt" .!= Nothing
374-
<*> v .: "syncedAt"
375-
<*> v .: "repoUrl"
376-
<*> v .: "commitDate"
377-
<*> v .: "commitHash"
378-
<*> v .: "runStatus"
379-
<*> pure def
380-
<*> v .: "certificationPrice"
381-
<*> v .:? "reportContentId" .!= Nothing
382-
383-
instance SqlRow Run
384-
instance IsLabel "profileId" (ID Profile -> Profile -> Profile) where
385-
fromLabel v p = p { profileId = v}
386-
387165
--------------------------------------------------------------------------------
388166
-- | Wallet transactions
389167

@@ -433,25 +211,6 @@ transactionEntries = table "entry"
433211
[ #txEntryId :- autoPrimary
434212
, #txEntryTxId :- foreignKey transactions #wtxId
435213
]
436-
runs :: Table Run
437-
runs = table "run"
438-
[ #runId :- primary
439-
, #profileId :- foreignKey profiles #profileId
440-
, #created :- index
441-
]
442-
443-
certifications :: Table Certification
444-
certifications = table "certification"
445-
[ #certId :- primary
446-
]
447-
448-
l1Certifications :: Table L1Certification
449-
l1Certifications = table "certification"
450-
[ #l1CertRunId :- primary
451-
, #l1CertRunId :- foreignKey runs #runId
452-
, #l1CertId :- foreignKey certifications #certId
453-
]
454-
455214
dapps :: Table DApp
456215
dapps = table "dapp"
457216
[ #dappId :- unique
@@ -461,6 +220,7 @@ dapps = table "dapp"
461220
createTables :: MonadSelda m => m ()
462221
createTables = do
463222
createTable certifications
223+
createTable onChainCertifications
464224
createTable profiles
465225
createTable dapps
466226
createTable runs

0 commit comments

Comments
 (0)