-
Notifications
You must be signed in to change notification settings - Fork 211
/
Sqlite.hs
904 lines (840 loc) · 31.1 KB
/
Sqlite.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Redundant flip" #-}
-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- An implementation of the DBLayer which uses Persistent and SQLite.
module Cardano.Pool.DB.Sqlite
( newDBLayer
, withDBLayer
, withDecoratedDBLayer
, DBDecorator (..)
, undecoratedDB
, defaultFilePath
, DatabaseView (..)
) where
import Prelude
import Cardano.DB.Sqlite
( DBField (..)
, DBLog (..)
, ManualMigration (..)
, MigrationError (..)
, SqliteContext (..)
, destroyDBLayer
, fieldName
, handleConstraint
, startSqliteBackend
, tableName
)
import Cardano.Pool.DB
( DBLayer (..), ErrPointAlreadyExists (..), determinePoolLifeCycleStatus )
import Cardano.Pool.DB.Log
( ParseFailure (..), PoolDbLog (..) )
import Cardano.Wallet.DB.Sqlite.Types
( BlockId (..) )
import Cardano.Wallet.Logging
( bracketTracer )
import Cardano.Wallet.Primitive.Slotting
( TimeInterpreter, epochOf, firstSlotInEpoch )
import Cardano.Wallet.Primitive.Types
( BlockHeader (..)
, CertificatePublicationTime (..)
, EpochNo (..)
, PoolId
, PoolLifeCycleStatus (..)
, PoolRegistrationCertificate (..)
, PoolRetirementCertificate (..)
, StakePoolMetadata (..)
, StakePoolMetadataHash
)
import Cardano.Wallet.Unsafe
( unsafeMkPercentage )
import Control.Exception
( bracket, throwIO )
import Control.Monad
( forM, forM_ )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Except
( ExceptT (..) )
import Control.Tracer
( Tracer (..), contramap, natTracer, traceWith )
import Data.Either
( partitionEithers, rights )
import Data.Function
( (&) )
import Data.Functor
( (<&>) )
import Data.Generics.Internal.VL.Lens
( view )
import Data.List
( foldl' )
import Data.Map.Strict
( Map )
import Data.Quantity
( Percentage (..), Quantity (..) )
import Data.Ratio
( denominator, numerator, (%) )
import Data.String.QQ
( s )
import Data.Text
( Text )
import Data.Time.Clock
( UTCTime, addUTCTime, getCurrentTime )
import Data.Word
( Word64, Word8 )
import Database.Persist.Sql
( Entity (..)
, Filter
, PersistValue
, RawSql
, SelectOpt (..)
, Single (..)
, deleteWhere
, fromPersistValue
, insertMany_
, insert_
, rawSql
, repsert
, selectFirst
, selectList
, toPersistValue
, (<.)
, (==.)
, (>.)
, (>=.)
)
import Database.Persist.Sqlite
( SqlPersistT )
import System.Directory
( removeFile )
import System.FilePath
( (</>) )
import System.Random
( newStdGen )
import Cardano.Pool.DB.Sqlite.TH
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Database.Sqlite as Sqlite
-- | Return the preferred @FilePath@ for the stake pool .sqlite file, given a
-- parent directory.
defaultFilePath
:: FilePath
-- ^ The directory in which the .sqlite file will be located.
-> FilePath
defaultFilePath = (</> "stake-pools.sqlite")
-- | Runs an action with a connection to the SQLite database.
--
-- Database migrations are run to create tables if necessary.
--
-- If the given file path does not exist, it will be created by the sqlite
-- library.
withDBLayer
:: Tracer IO PoolDbLog
-- ^ Logging object.
-> Maybe FilePath
-- ^ Database file location, or 'Nothing' for in-memory database.
-> TimeInterpreter IO
-- ^ The time interpreter object.
-> (DBLayer IO -> IO a)
-- ^ Action to run.
-> IO a
withDBLayer = withDecoratedDBLayer undecoratedDB
-- | A decorator for the database layer, useful for instrumenting or monitoring
-- calls to database operations.
newtype DBDecorator a =
DBDecorator { decorateDBLayer :: DBLayer a -> DBLayer a }
-- | The identity decorator.
--
-- Equivalent to an undecorated database.
--
undecoratedDB :: DBDecorator a
undecoratedDB = DBDecorator id
-- | Runs an action with a connection to the SQLite database.
--
-- This function has the same behaviour as 'withDBLayer', but provides a way
-- to decorate the created 'DBLayer' object with a 'DBDecorator', useful for
-- instrumenting or monitoring calls to database operations.
--
withDecoratedDBLayer
:: DBDecorator IO
-- ^ The database decorator.
-> Tracer IO PoolDbLog
-- ^ Logging object
-> Maybe FilePath
-- ^ Database file location, or Nothing for in-memory database
-> TimeInterpreter IO
-- ^ The time interpreter object.
-> (DBLayer IO -> IO a)
-- ^ Action to run.
-> IO a
withDecoratedDBLayer dbDecorator trace fp timeInterpreter action = do
traceWith trace (MsgGeneric $ MsgWillOpenDB fp)
bracket before after (action . decorateDBLayer dbDecorator . snd)
where
before = newDBLayer trace fp timeInterpreter
after = destroyDBLayer . fst
-- | Sets up a connection to the SQLite database.
--
-- Database migrations are run to create tables if necessary.
--
-- If the given file path does not exist, it will be created by the sqlite
-- library.
--
-- 'getDBLayer' will provide the actual 'DBLayer' implementation. The database
-- should be closed with 'destroyDBLayer'. If you use 'withDBLayer' then both of
-- these things will be handled for you.
newDBLayer
:: Tracer IO PoolDbLog
-- ^ Logging object
-> Maybe FilePath
-- ^ Database file location, or Nothing for in-memory database
-> TimeInterpreter IO
-> IO (SqliteContext, DBLayer IO)
newDBLayer trace fp timeInterpreter = do
let io = startSqliteBackend
(migrateManually trace)
migrateAll
(contramap MsgGeneric trace)
fp
ctx@SqliteContext{runQuery} <- handlingPersistError trace fp io
pure (ctx, mkDBLayer runQuery)
where
mkDBLayer :: (forall a. SqlPersistT IO a -> IO a) -> DBLayer IO
mkDBLayer runQuery = DBLayer {..}
where
putPoolProduction point pool = ExceptT $
handleConstraint (ErrPointAlreadyExists point) $
insert_ (mkPoolProduction pool point)
readPoolProduction epoch = do
production <- fmap fromPoolProduction
<$> selectPoolProduction timeInterpreter epoch
let toMap :: Ord a => Map a [b] -> (a,b) -> Map a [b]
toMap m (k, v) = Map.alter (alter v) k m
where
alter x = \case
Nothing -> Just [x]
Just xs -> Just (x:xs)
pure (foldl' toMap Map.empty production)
readTotalProduction = Map.fromList <$> runRawQuery trace
(RawQuery "readTotalProduction" query [] parseRow)
where
query = T.unwords
[ "SELECT pool_id, count(pool_id) as block_count"
, "FROM pool_production"
, "GROUP BY pool_id;"
]
parseRow
( Single fieldPoolId
, Single fieldBlockCount
) = (,)
<$> fromPersistValue fieldPoolId
<*> (Quantity <$> fromPersistValue fieldBlockCount)
putStakeDistribution epoch@(EpochNo ep) distribution = do
deleteWhere [StakeDistributionEpoch ==. fromIntegral ep]
insertMany_ (mkStakeDistribution epoch distribution)
readStakeDistribution (EpochNo epoch) = do
fmap (fromStakeDistribution . entityVal) <$> selectList
[ StakeDistributionEpoch ==. fromIntegral epoch ]
[]
readPoolLifeCycleStatus poolId =
determinePoolLifeCycleStatus
<$> readPoolRegistration poolId
<*> readPoolRetirement poolId
putPoolRegistration cpt cert = do
let CertificatePublicationTime {slotNo, slotInternalIndex} = cpt
let poolId = view #poolId cert
deleteWhere
[ PoolOwnerPoolId ==. poolId
, PoolOwnerSlot ==. slotNo
, PoolOwnerSlotInternalIndex ==. slotInternalIndex
]
let poolRegistrationKey = PoolRegistrationKey
poolId slotNo slotInternalIndex
let poolRegistrationRow = PoolRegistration
(poolId)
(slotNo)
(slotInternalIndex)
(fromIntegral $ numerator
$ getPercentage $ poolMargin cert)
(fromIntegral $ denominator
$ getPercentage $ poolMargin cert)
(getQuantity $ poolCost cert)
(getQuantity $ poolPledge cert)
(fst <$> poolMetadata cert)
(snd <$> poolMetadata cert)
_ <- repsert poolRegistrationKey poolRegistrationRow
insertMany_ $
zipWith
(PoolOwner poolId slotNo slotInternalIndex)
(poolOwners cert)
[0..]
putPoolRetirement cpt cert = do
let CertificatePublicationTime {slotNo, slotInternalIndex} = cpt
let PoolRetirementCertificate
poolId (EpochNo retirementEpoch) = cert
repsert (PoolRetirementKey poolId slotNo slotInternalIndex) $
PoolRetirement
poolId
slotNo
slotInternalIndex
(fromIntegral retirementEpoch)
unfetchedPoolMetadataRefs limit = do
let nLimit = T.pack (show limit)
let poolId = fieldName (DBField PoolRegistrationPoolId)
let metadataHash = fieldName (DBField PoolRegistrationMetadataHash)
let metadataUrl = fieldName (DBField PoolRegistrationMetadataUrl)
let retryAfter = fieldName (DBField PoolFetchAttemptsRetryAfter)
let registrations = tableName (DBField PoolRegistrationMetadataHash)
let fetchAttempts = tableName (DBField PoolFetchAttemptsMetadataHash)
let metadata = tableName (DBField PoolMetadataHash)
let query = T.unwords
[ "SELECT"
, "a." <> poolId, ","
, "a." <> metadataUrl, ","
, "a." <> metadataHash
, "FROM", registrations, "AS a"
, "LEFT JOIN", fetchAttempts, "AS b"
, "ON"
, "a." <> metadataUrl, "=", "b." <> metadataUrl, "AND"
, "a." <> metadataHash, "=", "b." <> metadataHash
, "WHERE"
-- Successfully fetched metadata
, "a." <> metadataHash, "NOT", "IN"
, "("
, "SELECT", metadataHash
, "FROM", metadata
, ")"
, "AND"
-- Discard recent failed attempts
, "("
, retryAfter, "<", "datetime('now')"
, "OR"
, retryAfter, "IS NULL"
, ")"
-- Important, since we have a limit, we order all results by
-- earlist "retry_after", so that we are sure that all
-- metadata gets _eventually_ processed.
--
-- Note that `NULL` is smaller than everything.
, "ORDER BY", retryAfter, "ASC"
, "LIMIT", nLimit
, ";"
]
let safeCast (Single a, Single b, Single c) = (,,)
<$> fromPersistValue a
<*> fromPersistValue b
<*> fromPersistValue c
rights . fmap safeCast <$> rawSql query []
putFetchAttempt (url, hash) = do
-- NOTE
-- assuming SQLite has the same notion of "now" that the host system.
now <- liftIO getCurrentTime
let filters =
[ PoolFetchAttemptsMetadataHash ==. hash
, PoolFetchAttemptsMetadataUrl ==. url
]
(fmap entityVal <$> selectFirst filters []) >>= \case
Nothing -> do
let retryAfter = backoff now 0
repsert
(PoolMetadataFetchAttemptsKey hash url)
(PoolMetadataFetchAttempts hash url retryAfter 1)
Just (PoolMetadataFetchAttempts _ _ _ retryCount) -> do
let retryAfter = backoff now retryCount
repsert
(PoolMetadataFetchAttemptsKey hash url)
(PoolMetadataFetchAttempts hash url retryAfter $ retryCount + 1)
putPoolMetadata hash metadata = do
let StakePoolMetadata{ticker,name,description,homepage} = metadata
repsert
(PoolMetadataKey hash)
(PoolMetadata hash name ticker description homepage)
deleteWhere [ PoolFetchAttemptsMetadataHash ==. hash ]
readPoolMetadata = do
Map.fromList . map (fromPoolMeta . entityVal)
<$> selectList [] []
listRegisteredPools = do
fmap (poolRegistrationPoolId . entityVal) <$> selectList [ ]
[ Desc PoolRegistrationSlot
, Desc PoolRegistrationSlotInternalIndex
]
listRetiredPools epochNo = runRawQuery trace $
RawQuery "listRetiredPools" query parameters parseRow
where
query = T.unwords
[ "SELECT *"
, "FROM active_pool_retirements"
, "WHERE retirement_epoch <= ?;"
]
parameters = [ toPersistValue epochNo ]
parseRow (Single poolId, Single retirementEpoch) =
PoolRetirementCertificate
<$> fromPersistValue poolId
<*> fromPersistValue retirementEpoch
listPoolLifeCycleData epochNo = runRawQuery trace $ RawQuery
"listPoolLifeCycleData" query parameters parseRow
where
query = T.unwords
[ "SELECT *"
, "FROM active_pool_lifecycle_data"
, "WHERE retirement_epoch IS NULL OR retirement_epoch > ?;"
]
parameters = [ toPersistValue epochNo ]
parseRow
( Single fieldPoolId
, Single fieldRetirementEpoch
, Single fieldOwners
, Single fieldCost
, Single fieldPledge
, Single fieldMarginNumerator
, Single fieldMarginDenominator
, Single fieldMetadataHash
, Single fieldMetadataUrl
) = do
regCert <- parseRegistrationCertificate
parseRetirementCertificate <&> maybe
(PoolRegistered regCert)
(PoolRegisteredAndRetired regCert)
where
parseRegistrationCertificate = PoolRegistrationCertificate
<$> fromPersistValue fieldPoolId
<*> fromPersistValue fieldOwners
<*> parseMargin
<*> (Quantity <$> fromPersistValue fieldCost)
<*> (Quantity <$> fromPersistValue fieldPledge)
<*> parseMetadata
parseRetirementCertificate = do
poolId <- fromPersistValue fieldPoolId
mRetirementEpoch <- fromPersistValue fieldRetirementEpoch
pure $ PoolRetirementCertificate poolId <$> mRetirementEpoch
parseMargin = mkMargin
<$> fromPersistValue @Word64 fieldMarginNumerator
<*> fromPersistValue @Word64 fieldMarginDenominator
where
mkMargin n d = unsafeMkPercentage $ toRational $ n % d
parseMetadata = do
u <- fromPersistValue fieldMetadataUrl
h <- fromPersistValue fieldMetadataHash
pure $ (,) <$> u <*> h
rollbackTo point = do
-- TODO(ADP-356): What if the conversion blocks or fails?
--
-- Missing a rollback would be bad.
EpochNo epoch <- liftIO $ timeInterpreter (epochOf point)
deleteWhere [ StakeDistributionEpoch >. fromIntegral epoch ]
deleteWhere [ PoolProductionSlot >. point ]
deleteWhere [ PoolRegistrationSlot >. point ]
deleteWhere [ PoolRetirementSlot >. point ]
-- TODO: remove dangling metadata no longer attached to a pool
removePools = mapM_ $ \pool -> do
liftIO $ traceWith trace $ MsgRemovingPool pool
deleteWhere [ PoolProductionPoolId ==. pool ]
deleteWhere [ PoolOwnerPoolId ==. pool ]
deleteWhere [ PoolRegistrationPoolId ==. pool ]
deleteWhere [ PoolRetirementPoolId ==. pool ]
deleteWhere [ StakeDistributionPoolId ==. pool ]
removeRetiredPools epoch =
bracketTracer traceOuter action
where
action = listRetiredPools epoch >>= \retirementCerts -> do
traceInner retirementCerts
removePools (view #poolId <$> retirementCerts)
pure retirementCerts
traceOuter = trace
& natTracer liftIO
& contramap (MsgRemovingRetiredPoolsForEpoch epoch)
traceInner = liftIO
. traceWith trace
. MsgRemovingRetiredPools
readPoolProductionCursor k = do
reverse . map (snd . fromPoolProduction . entityVal) <$> selectList
[]
[Desc PoolProductionSlot, LimitTo k]
readSystemSeed = do
mseed <- selectFirst [] []
case mseed of
Nothing -> do
seed <- liftIO newStdGen
insert_ (ArbitrarySeed seed)
return seed
Just seed ->
return $ seedSeed $ entityVal seed
cleanDB = do
deleteWhere ([] :: [Filter PoolProduction])
deleteWhere ([] :: [Filter PoolOwner])
deleteWhere ([] :: [Filter PoolRegistration])
deleteWhere ([] :: [Filter PoolRetirement])
deleteWhere ([] :: [Filter StakeDistribution])
deleteWhere ([] :: [Filter PoolMetadata])
deleteWhere ([] :: [Filter PoolMetadataFetchAttempts])
atomically :: forall a. (SqlPersistT IO a -> IO a)
atomically = runQuery
readPoolRegistration poolId = do
result <- selectFirst
[ PoolRegistrationPoolId ==. poolId ]
[ Desc PoolRegistrationSlot
, Desc PoolRegistrationSlotInternalIndex
]
forM result $ \meta -> do
let PoolRegistration
_poolId
slotNo
slotInternalIndex
marginNum
marginDen
poolCost_
poolPledge_
poolMetadataUrl
poolMetadataHash = entityVal meta
let poolMargin = unsafeMkPercentage $
toRational $ marginNum % marginDen
let poolCost = Quantity poolCost_
let poolPledge = Quantity poolPledge_
let poolMetadata = (,) <$> poolMetadataUrl <*> poolMetadataHash
poolOwners <- fmap (poolOwnerOwner . entityVal) <$>
selectList
[ PoolOwnerPoolId
==. poolId
, PoolOwnerSlot
==. slotNo
, PoolOwnerSlotInternalIndex
==. slotInternalIndex
]
[ Asc PoolOwnerIndex ]
let cert = PoolRegistrationCertificate
{ poolId
, poolOwners
, poolMargin
, poolCost
, poolPledge
, poolMetadata
}
let cpt = CertificatePublicationTime {slotNo, slotInternalIndex}
pure (cpt, cert)
readPoolRetirement poolId = do
result <- selectFirst
[ PoolRetirementPoolId ==. poolId ]
[ Desc PoolRetirementSlot
, Desc PoolRetirementSlotInternalIndex
]
forM result $ \meta -> do
let PoolRetirement
_poolId
slotNo
slotInternalIndex
retirementEpochNo = entityVal meta
let retirementEpoch = EpochNo (fromIntegral retirementEpochNo)
let cert = PoolRetirementCertificate {poolId, retirementEpoch}
let cpt = CertificatePublicationTime {slotNo, slotInternalIndex}
pure (cpt, cert)
-- | Defines a raw SQL query, runnable with 'runRawQuery'.
--
data RawQuery a b = RawQuery
{ queryName :: Text
-- ^ The name of the query.
, queryDefinition :: Text
-- ^ The SQL definition of the query.
, queryParameters :: [PersistValue]
-- ^ Parameters of the query.
, queryParser :: a -> Either Text b
-- ^ A parser for a row of the result.
}
-- | Runs a raw SQL query, logging any parse failures that occur.
--
runRawQuery
:: forall a b. RawSql a
=> Tracer IO PoolDbLog
-> RawQuery a b
-> SqlPersistT IO [b]
runRawQuery trace q = do
(failures, results) <- partitionEithers . fmap (queryParser q) <$> rawSql
(queryDefinition q)
(queryParameters q)
forM_ failures
$ liftIO
. traceWith trace
. MsgParseFailure
. ParseFailure (queryName q)
pure results
migrateManually
:: Tracer IO PoolDbLog
-> ManualMigration
migrateManually _tr =
ManualMigration $ \conn -> do
createView conn activePoolLifeCycleData
createView conn activePoolOwners
createView conn activePoolRegistrations
createView conn activePoolRetirements
-- | Represents a database view.
--
data DatabaseView = DatabaseView
{ databaseViewName :: Text
-- ^ A name for the view.
, databaseViewDefinition :: Text
-- ^ A select query to generate the view.
}
-- | Creates the specified database view, if it does not already exist.
--
createView :: Sqlite.Connection -> DatabaseView -> IO ()
createView conn (DatabaseView name definition) = do
deleteQuery <- Sqlite.prepare conn deleteQueryString
Sqlite.step deleteQuery *> Sqlite.finalize deleteQuery
createQuery <- Sqlite.prepare conn createQueryString
Sqlite.step createQuery *> Sqlite.finalize createQuery
where
deleteQueryString = T.unlines
[ "DROP VIEW IF EXISTS"
, name
, ";"
]
createQueryString = T.unlines
[ "CREATE VIEW"
, name
, "AS"
, definition
]
-- | Views active lifecycle data for every pool in the set of known pools.
--
-- This view has exactly ONE row for each known pool, where each row
-- corresponds to the most-recently-seen registration certificate,
-- retirement certificate, and set of owners for that pool.
--
-- This view does NOT exclude pools that have retired.
--
activePoolLifeCycleData :: DatabaseView
activePoolLifeCycleData = DatabaseView "active_pool_lifecycle_data" [s|
SELECT
active_pool_registrations.pool_id as pool_id,
active_pool_retirements.retirement_epoch as retirement_epoch,
active_pool_owners.pool_owners as pool_owners,
cost,
pledge,
margin_numerator,
margin_denominator,
metadata_hash,
metadata_url
FROM
active_pool_registrations
LEFT JOIN
active_pool_retirements
ON active_pool_registrations.pool_id = active_pool_retirements.pool_id
LEFT JOIN
active_pool_owners
ON active_pool_registrations.pool_id = active_pool_owners.pool_id;
|]
-- | Views the set of active owners for all pools.
--
-- This view has exactly ONE row for each known pool, where each row
-- corresponds to the most-recently-seen set of owners for that pool.
--
-- This view does NOT exclude pools that have retired.
--
activePoolOwners :: DatabaseView
activePoolOwners = DatabaseView "active_pool_owners" [s|
SELECT pool_id, pool_owners FROM (
SELECT row_number() OVER w AS r, *
FROM (
SELECT
pool_id,
slot,
slot_internal_index,
group_concat(pool_owner, ' ') as pool_owners
FROM (
SELECT * FROM pool_owner ORDER BY pool_owner_index
)
GROUP BY pool_id, slot, slot_internal_index
)
WINDOW w AS (ORDER BY pool_id, slot desc, slot_internal_index desc)
)
GROUP BY pool_id;
|]
-- | Views the set of pool registrations that are currently active.
--
-- This view has exactly ONE row for each known pool, where each row
-- corresponds to the most-recently-seen registration certificate for
-- that pool.
--
-- This view does NOT exclude pools that have retired.
--
activePoolRegistrations :: DatabaseView
activePoolRegistrations = DatabaseView "active_pool_registrations" [s|
SELECT
pool_id,
cost,
pledge,
margin_numerator,
margin_denominator,
metadata_hash,
metadata_url
FROM (
SELECT row_number() OVER w AS r, *
FROM pool_registration
WINDOW w AS (ORDER BY pool_id, slot desc, slot_internal_index desc)
)
GROUP BY pool_id;
|]
-- | Views the set of pool retirements that are currently active.
--
-- This view includes all pools for which there are published retirement
-- certificates that have not been revoked or superseded.
--
-- This view does NOT include:
--
-- - pools for which there are no published retirement certificates.
--
-- - pools that have had their most-recently-published retirement
-- certificates revoked by subsequent re-registration certificates.
--
activePoolRetirements :: DatabaseView
activePoolRetirements = DatabaseView "active_pool_retirements" [s|
SELECT * FROM (
SELECT
pool_id,
retirement_epoch
FROM (
SELECT row_number() OVER w AS r, *
FROM (
SELECT
pool_id, slot, slot_internal_index,
NULL as retirement_epoch
FROM pool_registration
UNION
SELECT
pool_id, slot, slot_internal_index,
epoch as retirement_epoch
FROM pool_retirement
)
WINDOW w AS (ORDER BY pool_id, slot desc, slot_internal_index desc)
)
GROUP BY pool_id
)
WHERE retirement_epoch IS NOT NULL;
|]
-- | 'Temporary', catches migration error from previous versions and if any,
-- _removes_ the database file completely before retrying to start the database.
--
-- This comes in handy to fix database schema in a non-backward compatible way
-- without altering too much the user experience. Indeed, the pools' database
-- can swiftly be re-synced from the chain, so instead of patching our mistakes
-- with ugly work-around we can, at least for now, reset it semi-manually when
-- needed to keep things tidy here.
handlingPersistError
:: Tracer IO PoolDbLog
-- ^ Logging object
-> Maybe FilePath
-- ^ Database file location, or Nothing for in-memory database
-> IO (Either MigrationError ctx)
-- ^ Action to set up the context.
-> IO ctx
handlingPersistError trace fp action = action >>= \case
Right ctx -> pure ctx
Left _ -> do
traceWith trace $ MsgGeneric MsgDatabaseReset
maybe (pure ()) removeFile fp
action >>= either throwIO pure
-- | Compute a new date from a base date, with an increasing delay.
--
-- > backoff t 0
-- t+3s
--
-- > backoff t 1
-- t+9s
--
-- > backoff t 2
-- t+27s
--
-- ...
--
-- > backoff t 9
-- t+16h
--
-- > backoff t 10
-- t+49h
backoff :: UTCTime -> Word8 -> UTCTime
backoff time iter = addUTCTime delay time
where
delay = fromIntegral @Integer $ foldr (*) 3 (replicate (fromIntegral iter) 3)
{-------------------------------------------------------------------------------
Queries
-------------------------------------------------------------------------------}
selectPoolProduction
:: TimeInterpreter IO
-> EpochNo
-> SqlPersistT IO [PoolProduction]
selectPoolProduction timeInterpreter epoch = do
e <- liftIO $ timeInterpreter $ firstSlotInEpoch epoch
eplus1 <- liftIO $ timeInterpreter $ firstSlotInEpoch (epoch + 1)
fmap entityVal <$> selectList
[ PoolProductionSlot >=. e
, PoolProductionSlot <. eplus1 ]
[Asc PoolProductionSlot]
{-------------------------------------------------------------------------------
To / From SQLite
-------------------------------------------------------------------------------}
mkPoolProduction
:: PoolId
-> BlockHeader
-> PoolProduction
mkPoolProduction pool block = PoolProduction
{ poolProductionPoolId = pool
, poolProductionSlot = view #slotNo block
, poolProductionHeaderHash = BlockId (headerHash block)
, poolProductionParentHash = BlockId (parentHeaderHash block)
, poolProductionBlockHeight = getQuantity (blockHeight block)
}
fromPoolProduction
:: PoolProduction
-> (PoolId, BlockHeader)
fromPoolProduction (PoolProduction pool slot headerH parentH height) =
( pool
, BlockHeader
{ slotNo = slot
, blockHeight = Quantity height
, headerHash = getBlockId headerH
, parentHeaderHash = getBlockId parentH
}
)
mkStakeDistribution
:: EpochNo
-> [(PoolId, Quantity "lovelace" Word64)]
-> [StakeDistribution]
mkStakeDistribution (EpochNo epoch) = map $ \(pool, (Quantity stake)) ->
StakeDistribution
{ stakeDistributionPoolId = pool
, stakeDistributionEpoch = fromIntegral epoch
, stakeDistributionStake = stake
}
fromStakeDistribution
:: StakeDistribution
-> (PoolId, Quantity "lovelace" Word64)
fromStakeDistribution distribution =
( stakeDistributionPoolId distribution
, Quantity (stakeDistributionStake distribution)
)
fromPoolMeta
:: PoolMetadata
-> (StakePoolMetadataHash, StakePoolMetadata)
fromPoolMeta meta = (poolMetadataHash meta,) $
StakePoolMetadata
{ ticker = poolMetadataTicker meta
, name = poolMetadataName meta
, description = poolMetadataDescription meta
, homepage = poolMetadataHomepage meta
}