Skip to content

Commit 71208af

Browse files
authored
Merge pull request #1614 from input-output-hk/KtorZ/1596/mock-metadata-aggregation-server
Metadata aggregation client & mock server
2 parents bb202df + 0c54579 commit 71208af

File tree

8 files changed

+362
-8
lines changed

8 files changed

+362
-8
lines changed

lib/core/cardano-wallet-core.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ library
5757
, foldl
5858
, generic-lens
5959
, http-api-data
60+
, http-client
6061
, http-media
6162
, http-types
6263
, iohk-monitoring
@@ -104,6 +105,7 @@ library
104105
Cardano.DB.Sqlite
105106
Cardano.DB.Sqlite.Delete
106107
Cardano.Pool
108+
Cardano.Pool.Metadata
107109
Cardano.Pool.DB
108110
Cardano.Pool.DB.MVar
109111
Cardano.Pool.DB.Model
@@ -214,6 +216,7 @@ test-suite unit
214216
, safe
215217
, scrypt
216218
, servant
219+
, servant-client
217220
, servant-server
218221
, servant-swagger
219222
, stm
@@ -244,6 +247,7 @@ test-suite unit
244247
other-modules:
245248
Cardano.Byron.Codec.CborSpec
246249
Cardano.DB.Sqlite.DeleteSpec
250+
Cardano.Pool.MetadataSpec
247251
Cardano.Pool.DB.Arbitrary
248252
Cardano.Pool.DB.Properties
249253
Cardano.Pool.DB.SqliteSpec
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE TypeApplications #-}
3+
{-# LANGUAGE TypeOperators #-}
4+
5+
-- |
6+
-- Copyright: © 2018-2020 IOHK
7+
-- License: Apache-2.0
8+
--
9+
-- API and corresponding client for dealing with a metadata aggregation server
10+
-- in Shelley. Such servers follow an OpenAPI specification, and on existing
11+
-- implementation written in Haskell is available at:
12+
--
13+
-- - https://github.com/input-output-hk/smash
14+
--
15+
module Cardano.Pool.Metadata
16+
( Api
17+
, Client(..)
18+
, mkClient
19+
20+
-- * Re-export
21+
, BaseUrl (..)
22+
, Scheme (..)
23+
, Manager
24+
, defaultManagerSettings
25+
, newManager
26+
) where
27+
28+
import Prelude
29+
30+
import Cardano.Wallet.Api.Types
31+
( ApiT (..) )
32+
import Cardano.Wallet.Primitive.Types
33+
( PoolId, StakePoolOffChainMetadata (..) )
34+
import Data.Proxy
35+
( Proxy (..) )
36+
import Network.HTTP.Client
37+
( Manager, defaultManagerSettings, newManager )
38+
import Servant
39+
( (:>), Capture, Get, JSON )
40+
import Servant.Client
41+
( BaseUrl (..)
42+
, ClientError (..)
43+
, ClientM
44+
, Scheme (..)
45+
, client
46+
, mkClientEnv
47+
, runClientM
48+
)
49+
50+
--
51+
-- Api
52+
--
53+
54+
type Api
55+
= "api"
56+
:> "v1"
57+
:> "metadata"
58+
:> Capture "hash" (ApiT PoolId)
59+
:> Get '[JSON] (ApiT StakePoolOffChainMetadata)
60+
61+
--
62+
-- Client
63+
--
64+
65+
-- | A client for fetching metadata from an Aggregation server.
66+
--
67+
-- See also 'newClient' to construct a client.
68+
newtype Client api = Client
69+
{ getStakePoolMetadata
70+
:: PoolId
71+
-> IO (Either ClientError StakePoolOffChainMetadata)
72+
}
73+
74+
mkClient
75+
:: Manager
76+
-> BaseUrl
77+
-> Client Api
78+
mkClient mgr baseUrl = Client
79+
{ getStakePoolMetadata = \pid ->
80+
fmap getApiT <$> run (getMetadata (ApiT pid))
81+
}
82+
where
83+
run :: ClientM a -> IO (Either ClientError a)
84+
run query = runClientM query (mkClientEnv mgr baseUrl)
85+
86+
getMetadata =
87+
client (Proxy @Api)

lib/core/src/Cardano/Wallet/Api/Types.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ import Cardano.Wallet.Primitive.Types
161161
, SlotLength (..)
162162
, SlotNo (..)
163163
, StakePoolMetadata
164+
, StakePoolOffChainMetadata
164165
, StartTime (..)
165166
, SyncProgress (..)
166167
, TxIn (..)
@@ -1215,10 +1216,14 @@ instance ToJSON ApiNetworkClock where
12151216

12161217
instance FromJSON (ApiT StakePoolMetadata) where
12171218
parseJSON = fmap ApiT . genericParseJSON defaultRecordTypeOptions
1218-
12191219
instance ToJSON (ApiT StakePoolMetadata) where
12201220
toJSON = genericToJSON defaultRecordTypeOptions . getApiT
12211221

1222+
instance FromJSON (ApiT StakePoolOffChainMetadata) where
1223+
parseJSON = fmap ApiT . genericParseJSON defaultRecordTypeOptions
1224+
instance ToJSON (ApiT StakePoolOffChainMetadata) where
1225+
toJSON = genericToJSON defaultRecordTypeOptions . getApiT
1226+
12221227
instance FromJSON (ApiT StartTime) where
12231228
parseJSON = fmap (ApiT . StartTime) . parseJSON
12241229
instance ToJSON (ApiT StartTime) where

lib/core/src/Cardano/Wallet/Primitive/Types.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ module Cardano.Wallet.Primitive.Types
139139
, StakeDistribution (..)
140140
, poolIdBytesLength
141141
, StakePoolMetadata (..)
142+
, StakePoolOffChainMetadata (..)
142143
, StakePoolTicker (..)
143144
, sameStakePoolMetadata
144145

@@ -600,6 +601,20 @@ data StakePoolMetadata = StakePoolMetadata
600601
-- ^ Bech32-encoded address.
601602
} deriving (Eq, Show, Generic)
602603

604+
-- | A subset of the 'StakePoolMetadata' but with the information that is
605+
-- available off-chain. The 'pledgeAddress' and 'owner' are actually part of the
606+
-- pool registration certificates published on-chain.
607+
data StakePoolOffChainMetadata = StakePoolOffChainMetadata
608+
{ ticker :: StakePoolTicker
609+
-- ^ Very short human-readable ID for the stake pool.
610+
, name :: Text
611+
-- ^ Name of the stake pool.
612+
, description :: Text
613+
-- ^ Short description of the stake pool.
614+
, homepage :: Text
615+
-- ^ Absolute URL for the stake pool's homepage link.
616+
} deriving (Eq, Show, Generic)
617+
603618
-- | Returns 'True' iff metadata is exactly equal, modulo 'PoolOwner'.
604619
sameStakePoolMetadata :: StakePoolMetadata -> StakePoolMetadata -> Bool
605620
sameStakePoolMetadata a b = a { owner = same } == b { owner = same }

lib/core/src/Data/Vector/Shuffle.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Data.Vector.Mutable
2929
import Data.Word
3030
( Word8 )
3131
import System.Random
32-
( RandomGen, StdGen, mkStdGen, newStdGen, randomR )
32+
( RandomGen, newStdGen, randomR )
3333

3434
import qualified Data.ByteArray as BA
3535
import qualified Data.ByteString as BS
@@ -39,8 +39,8 @@ import qualified Data.Vector.Mutable as MV
3939

4040

4141
-- | Generate a random generator seed from a text string
42-
mkSeed :: Text -> StdGen
43-
mkSeed = mkStdGen . toInt . quickHash . T.encodeUtf16LE
42+
mkSeed :: Text -> Int
43+
mkSeed = toInt . quickHash . T.encodeUtf16LE
4444
where
4545
quickHash = BA.convert . hash @_ @MD5
4646
toInt = snd . BS.foldl' exponentiation (0,0)

0 commit comments

Comments
 (0)