Skip to content

Commit

Permalink
fixup: combine
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Jun 18, 2020
1 parent 9042f06 commit a6bcca7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
3 changes: 3 additions & 0 deletions lib/core/src/Cardano/Pool/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ data DBLayer m = forall stm. (MonadFail stm, MonadIO stm) => DBLayer
-> stm ()
-- ^ Store metadata fetched from a remote server.

, readPoolMetadata
:: stm (Map StakePoolMetadataHash StakePoolMetadata)

, readSystemSeed
:: stm StdGen
-- ^ Read the seed assigned to this particular database. The seed is
Expand Down
2 changes: 2 additions & 0 deletions lib/core/src/Cardano/Pool/DB/MVar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ newDBLayer = do

, putPoolMetadata = error "todo"

, readPoolMetadata = error "todo"

, atomically = id
}

Expand Down
17 changes: 17 additions & 0 deletions lib/core/src/Cardano/Pool/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

Expand Down Expand Up @@ -50,6 +51,7 @@ import Cardano.Wallet.Primitive.Types
, PoolRegistrationCertificate (..)
, SlotId (..)
, StakePoolMetadata (..)
, StakePoolMetadataHash
)
import Cardano.Wallet.Unsafe
( unsafeMkPercentage )
Expand Down Expand Up @@ -273,6 +275,10 @@ newDBLayer trace fp = do
let StakePoolMetadata{ticker,name,description,homepage} = metadata
putMany [PoolMetadata hash poolId name ticker description homepage]

, readPoolMetadata = do
Map.fromList . map (fromPoolMeta . entityVal)
<$> selectList [] []

, listRegisteredPools = do
fmap (poolRegistrationPoolId . entityVal) <$> selectList [ ]
[ Desc PoolRegistrationSlot ]
Expand Down Expand Up @@ -390,3 +396,14 @@ 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
}
38 changes: 27 additions & 11 deletions lib/shelley/src/Cardano/Wallet/Shelley/Pools.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ data PoolLsqMetrics = PoolLsqMetrics
-- | Top level combine-function that merges chain and LSQ data.
combineChainAndLsqData
:: Map PoolId PoolLsqMetrics
-> Map PoolId (PoolRegistrationCertificate, Quantity "block" Word64)
-> Map PoolId (PoolRegistrationCertificate, Quantity "block" Word64, Maybe StakePoolMetadata)
-> Map PoolId Api.ApiStakePool
combineChainAndLsqData =
Map.merge lsqButNoChain chainButNoLsq bothPresent
Expand All @@ -158,7 +158,7 @@ combineChainAndLsqData =
mkApiPool
:: PoolId
-> PoolLsqMetrics
-> Maybe (PoolRegistrationCertificate, Quantity "block" Word64)
-> Maybe (PoolRegistrationCertificate, Quantity "block" Word64, Maybe StakePoolMetadata)
-> Api.ApiStakePool
mkApiPool
pid
Expand All @@ -171,15 +171,20 @@ combineChainAndLsqData =
, Api.relativeStake = Quantity pstk
, Api.saturation = psat
, Api.producedBlocks = maybe (Quantity 0)
(mapQ fromIntegral . snd) dbData
(mapQ fromIntegral . second) dbData
}
, Api.metadata = Nothing -- TODO: Implement
, Api.cost = mapQ fromIntegral . poolCost . fst <$> dbData
, Api.margin = Quantity . poolMargin . fst <$> dbData
, Api.metadata = dbData >>= third >>= (return . ApiT)
, Api.cost = mapQ fromIntegral . poolCost . first <$> dbData
, Api.margin = Quantity . poolMargin . first <$> dbData
}

mapQ f (Quantity x) = Quantity $ f x

-- TODO: Add proper record
first (x,_,_) = x
second (_,x,_) = x
third (_,_,x) = x


-- | Combines all the LSQ data into a single map.
--
Expand Down Expand Up @@ -263,7 +268,6 @@ newStakePoolLayer gp nl db = StakePoolLayer
-> ExceptT ErrNetworkUnavailable IO [Api.ApiStakePool]
_listPools DBLayer{..} s = do
pt <- liftIO getTip
-- TODO: We need to make sure th
rawLsqData <- stakeDistribution nl pt s
let lsqData = combineLsqData rawLsqData
chainData <- liftIO readChainPoolData
Expand All @@ -277,14 +281,26 @@ newStakePoolLayer gp nl db = StakePoolLayer
-- certificates at once from the DB?
readChainPoolData
:: IO (Map PoolId
(PoolRegistrationCertificate, Quantity "block" Word64))
(PoolRegistrationCertificate, Quantity "block" Word64, Maybe StakePoolMetadata))
readChainPoolData = do
(certs, prod) <- atomically $ do
(certs, prod, m) <- atomically $ do
pools <- listRegisteredPools
x <- mapM (\p -> ((fmap (p,)) <$> readPoolRegistration p) ) pools
prod <- readTotalProduction
return (Map.fromList $ catMaybes x, prod)
return $ combineDBData certs prod
m <- readPoolMetadata
return (Map.fromList $ catMaybes x, prod, m)
return $ Map.map (lookupMetaIn m) (combineDBData certs prod)
where
lookupMetaIn
:: Map StakePoolMetadataHash StakePoolMetadata
-> (PoolRegistrationCertificate, Quantity "block" Word64)
-> (PoolRegistrationCertificate, Quantity "block" Word64, Maybe StakePoolMetadata)
lookupMetaIn m (cert, n) =
let
metaHash = snd <$> poolMetadata cert
meta = flip Map.lookup m =<< metaHash
in
(cert, n, meta)

--
-- Monitoring stake pool
Expand Down

0 comments on commit a6bcca7

Please sign in to comment.