Skip to content

Commit 34af490

Browse files
committed
add offchain user header to configuration
1 parent 0dab850 commit 34af490

File tree

12 files changed

+84
-23
lines changed

12 files changed

+84
-23
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ insertConfig = do
3434
, sioPoolStats = PoolStatsConfig False
3535
, sioJsonType = JsonTypeDisable
3636
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
37+
, sioOffchainUserAgent = OffChainUserAgent Nothing
3738
, sioStopAtBlock = Nothing
3839
}
3940

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/Parse.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ insertConfig = do
104104
, sioPoolStats = PoolStatsConfig False
105105
, sioJsonType = JsonTypeDisable
106106
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
107+
, sioOffchainUserAgent = OffChainUserAgent Nothing
107108
, sioStopAtBlock = Nothing
108109
}
109110

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import Cardano.Db (PoolMetaHash (..), PoolUrl (..), VoteMetaHash (..), VoteUrl (..))
55
import qualified Cardano.Db as DB
6+
import Cardano.DbSync.Config.Types (OffChainUserAgent (..))
67
import Cardano.DbSync.Error (bsBase16Encode, runOrThrowIO)
78
import Cardano.DbSync.OffChain.Http
89
import Cardano.DbSync.Types
@@ -107,10 +108,12 @@ runHttpGetPool poolUrl mHash =
107108
where
108109
httpGet :: ExceptT OffChainFetchError IO SimplifiedOffChainPoolData
109110
httpGet = do
110-
request <- parseOffChainUrl $ OffChainPoolUrl poolUrl
111+
request <- parseOffChainUrl (OffChainPoolUrl poolUrl) defaultUserAgent
111112
manager <- liftIO $ Http.newManager tlsManagerSettings
112113
httpGetOffChainPoolData manager request poolUrl mHash
113114

115+
defaultUserAgent = OffChainUserAgent Nothing
116+
114117
reportSuccess :: SimplifiedOffChainPoolData -> IO ()
115118
reportSuccess spod = do
116119
case spodContentType spod of
@@ -126,7 +129,9 @@ runHttpGetVote voteUrl mHash vtype =
126129
reportSuccess =<< runOrThrowIO (runExceptT httpGet)
127130
where
128131
httpGet :: ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
129-
httpGet = httpGetOffChainVoteData [] voteUrl mHash vtype
132+
httpGet = httpGetOffChainVoteData [] voteUrl defaultUserAgent mHash vtype
133+
134+
defaultUserAgent = OffChainUserAgent Nothing
130135

131136
reportSuccess :: SimplifiedOffChainVoteData -> IO ()
132137
reportSuccess spod = do

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#endif
88

99
import qualified Cardano.Db as DB
10+
import Cardano.DbSync.Config.Types (OffChainUserAgent (..))
1011
import Cardano.DbSync.OffChain.Http (
1112
httpGetOffChainPoolData,
1213
parseOffChainUrl,
@@ -40,8 +41,9 @@ main = do
4041
testOne manager !accum testPoolOffChain = do
4142
let poolUrl = toUrl testPoolOffChain
4243
mHash = Just $ toHash testPoolOffChain
44+
defaultUserAgent = OffChainUserAgent Nothing
4345
eres <- runExceptT $ do
44-
request <- parseOffChainUrl (OffChainPoolUrl poolUrl)
46+
request <- parseOffChainUrl (OffChainPoolUrl poolUrl) defaultUserAgent
4547
httpGetOffChainPoolData manager request poolUrl mHash
4648
case eres of
4749
Left err -> do

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ extractSyncOptions snp aop snc =
321321
, ioPoolStats = isPoolStatsEnabled (sioPoolStats (dncInsertOptions snc))
322322
, ioGov = useGovernance
323323
, ioRemoveJsonbFromSchema = isRemoveJsonbFromSchemaEnabled (sioRemoveJsonbFromSchema (dncInsertOptions snc))
324+
, ioOffChainUserAgent = sioOffchainUserAgent (dncInsertOptions snc)
324325
, ioTxOutVariantType = ioTxOutVariantType'
325326
}
326327

cardano-db-sync/src/Cardano/DbSync/Api/Types.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Ouroboros.Network.Magic (NetworkMagic (..))
2828

2929
import qualified Cardano.Db as DB
3030
import Cardano.DbSync.Cache.Types (CacheStatistics, CacheStatus)
31-
import Cardano.DbSync.Config.Types (SyncNodeConfig)
31+
import Cardano.DbSync.Config.Types (OffChainUserAgent, SyncNodeConfig)
3232
import Cardano.DbSync.Ledger.Types (HasLedgerEnv)
3333
import Cardano.DbSync.LocalStateQuery (NoLedgerEnv)
3434
import Cardano.DbSync.Types (
@@ -89,6 +89,7 @@ data InsertOptions = InsertOptions
8989
, ioPoolStats :: !Bool
9090
, ioGov :: !Bool
9191
, ioRemoveJsonbFromSchema :: !Bool
92+
, ioOffChainUserAgent :: !OffChainUserAgent
9293
, ioTxOutVariantType :: !DB.TxOutVariantType
9394
}
9495
deriving (Show)

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Cardano.DbSync.Config.Types (
3838
PlutusConfig (..),
3939
GovernanceConfig (..),
4040
OffchainPoolDataConfig (..),
41+
OffChainUserAgent (..),
4142
JsonTypeConfig (..),
4243
LedgerStateDir (..),
4344
LogFileDir (..),
@@ -189,6 +190,7 @@ data SyncInsertOptions = SyncInsertOptions
189190
, sioPoolStats :: PoolStatsConfig
190191
, sioJsonType :: JsonTypeConfig
191192
, sioRemoveJsonbFromSchema :: RemoveJsonbFromSchemaConfig
193+
, sioOffchainUserAgent :: OffChainUserAgent
192194
, sioStopAtBlock :: Maybe Word64
193195
}
194196
deriving (Eq, Show)
@@ -263,6 +265,12 @@ newtype OffchainPoolDataConfig = OffchainPoolDataConfig
263265
}
264266
deriving (Eq, Show)
265267

268+
newtype OffChainUserAgent = OffChainUserAgent
269+
{ unOffChainUserAgent :: Maybe Text
270+
}
271+
deriving (Eq, Show)
272+
deriving newtype (ToJSON, FromJSON)
273+
266274
newtype RemoveJsonbFromSchemaConfig = RemoveJsonbFromSchemaConfig
267275
{ isRemoveJsonbFromSchemaEnabled :: Bool
268276
}
@@ -456,6 +464,7 @@ parseOverrides obj baseOptions = do
456464
<*> obj .:? "pool_stat" .!= sioPoolStats baseOptions
457465
<*> obj .:? "json_type" .!= sioJsonType baseOptions
458466
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
467+
<*> obj .:? "offchain_user_agent" .!= sioOffchainUserAgent baseOptions
459468
<*> obj .:? "stop_at_block" .!= sioStopAtBlock baseOptions
460469

461470
instance ToJSON SyncInsertConfig where
@@ -478,6 +487,7 @@ optionsToList SyncInsertOptions {..} =
478487
, toJsonIfSet "pool_stat" sioPoolStats
479488
, toJsonIfSet "json_type" sioJsonType
480489
, toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema
490+
, toJsonIfSet "offchain_user_agent" sioOffchainUserAgent
481491
, toJsonIfSet "stop_at_block" sioStopAtBlock
482492
]
483493

@@ -500,6 +510,7 @@ instance FromJSON SyncInsertOptions where
500510
<*> obj .:? "pool_stat" .!= sioPoolStats def
501511
<*> obj .:? "json_type" .!= sioJsonType def
502512
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema def
513+
<*> obj .:? "offchain_user_agent" .!= sioOffchainUserAgent def
503514
<*> obj .:? "stop_at_block" .!= sioStopAtBlock def
504515

505516
instance ToJSON SyncInsertOptions where
@@ -517,6 +528,7 @@ instance ToJSON SyncInsertOptions where
517528
, "pool_stat" .= sioPoolStats
518529
, "json_type" .= sioJsonType
519530
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
531+
, "offchain_user_agent" .= sioOffchainUserAgent
520532
, "stop_at_block" .= sioStopAtBlock
521533
]
522534

@@ -747,6 +759,7 @@ instance Default SyncInsertOptions where
747759
, sioPoolStats = PoolStatsConfig False
748760
, sioJsonType = JsonTypeText
749761
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
762+
, sioOffchainUserAgent = OffChainUserAgent Nothing
750763
, sioStopAtBlock = Nothing
751764
}
752765

@@ -766,6 +779,7 @@ fullInsertOptions =
766779
, sioPoolStats = PoolStatsConfig True
767780
, sioJsonType = JsonTypeText
768781
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
782+
, sioOffchainUserAgent = OffChainUserAgent Nothing
769783
, sioStopAtBlock = Nothing
770784
}
771785

@@ -785,6 +799,7 @@ onlyUTxOInsertOptions =
785799
, sioPoolStats = PoolStatsConfig False
786800
, sioJsonType = JsonTypeText
787801
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
802+
, sioOffchainUserAgent = OffChainUserAgent Nothing
788803
, sioStopAtBlock = Nothing
789804
}
790805

@@ -812,6 +827,7 @@ disableAllInsertOptions =
812827
, sioGovernance = GovernanceConfig False
813828
, sioJsonType = JsonTypeText
814829
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
830+
, sioOffchainUserAgent = OffChainUserAgent Nothing
815831
, sioStopAtBlock = Nothing
816832
}
817833

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,12 @@ runFetchOffChainPoolThread syncEnv = do
255255
poolq <- atomically $ flushTBQueue (envOffChainPoolWorkQueue threadSyncEnv)
256256
manager <- Http.newManager tlsManagerSettings
257257
now <- liftIO Time.getPOSIXTime
258-
mapM_ (queuePoolInsert <=< fetchOffChainPoolData trce manager now) poolq
258+
mapM_ (queuePoolInsert <=< fetchOffChainPoolData trce manager now userAgent) poolq
259259
)
260260
where
261261
trce = getTrace syncEnv
262262
iopts = getInsertOptions syncEnv
263+
userAgent = ioOffChainUserAgent iopts
263264

264265
queuePoolInsert :: OffChainPoolResult -> IO ()
265266
queuePoolInsert = atomically . writeTBQueue (envOffChainPoolResultQueue syncEnv)
@@ -290,12 +291,13 @@ runFetchOffChainVoteThread syncEnv = do
290291
loadOffChainVoteWorkQueue trce (envOffChainVoteWorkQueue threadSyncEnv)
291292
voteq <- atomically $ flushTBQueue (envOffChainVoteWorkQueue threadSyncEnv)
292293
now <- liftIO Time.getPOSIXTime
293-
mapM_ (queueVoteInsert <=< fetchOffChainVoteData gateways now) voteq
294+
mapM_ (queueVoteInsert <=< fetchOffChainVoteData gateways userAgent now) voteq
294295
)
295296
where
296297
trce = getTrace syncEnv
297298
iopts = getInsertOptions syncEnv
298299
gateways = dncIpfsGateway $ envSyncNodeConfig syncEnv
300+
userAgent = ioOffChainUserAgent iopts
299301

300302
queueVoteInsert :: OffChainVoteResult -> IO ()
301303
queueVoteInsert = atomically . writeTBQueue (envOffChainVoteResultQueue syncEnv)
@@ -307,12 +309,12 @@ tDelay = threadDelay 300_000_000
307309
---------------------------------------------------------------------------------------------------------------------------------
308310
-- Fetch OffChain data
309311
---------------------------------------------------------------------------------------------------------------------------------
310-
fetchOffChainPoolData :: Trace IO Text -> Http.Manager -> Time.POSIXTime -> OffChainPoolWorkQueue -> IO OffChainPoolResult
311-
fetchOffChainPoolData _tracer manager time oPoolWorkQ =
312+
fetchOffChainPoolData :: Trace IO Text -> Http.Manager -> Time.POSIXTime -> OffChainUserAgent -> OffChainPoolWorkQueue -> IO OffChainPoolResult
313+
fetchOffChainPoolData _tracer manager time ocUserAgent oPoolWorkQ =
312314
convert <<$>> runExceptT $ do
313315
let url = oPoolWqUrl oPoolWorkQ
314316
metaHash = oPoolWqMetaHash oPoolWorkQ
315-
request <- parseOffChainUrl $ OffChainPoolUrl url
317+
request <- parseOffChainUrl (OffChainPoolUrl url) ocUserAgent
316318
httpGetOffChainPoolData manager request url (Just metaHash)
317319
where
318320
convert :: Either OffChainFetchError SimplifiedOffChainPoolData -> OffChainPoolResult
@@ -338,12 +340,12 @@ fetchOffChainPoolData _tracer manager time oPoolWorkQ =
338340
, DB.offChainPoolFetchErrorRetryCount = retryCount (oPoolWqRetry oPoolWorkQ)
339341
}
340342

341-
fetchOffChainVoteData :: [Text] -> Time.POSIXTime -> OffChainVoteWorkQueue -> IO OffChainVoteResult
342-
fetchOffChainVoteData gateways time oVoteWorkQ =
343+
fetchOffChainVoteData :: [Text] -> OffChainUserAgent -> Time.POSIXTime -> OffChainVoteWorkQueue -> IO OffChainVoteResult
344+
fetchOffChainVoteData gateways userAgent time oVoteWorkQ =
343345
convert <<$>> runExceptT $ do
344346
let url = oVoteWqUrl oVoteWorkQ
345347
metaHash = oVoteWqMetaHash oVoteWorkQ
346-
httpGetOffChainVoteData gateways url (Just metaHash) (oVoteWqType oVoteWorkQ)
348+
httpGetOffChainVoteData gateways url userAgent (Just metaHash) (oVoteWqType oVoteWorkQ)
347349
where
348350
convert :: Either OffChainFetchError SimplifiedOffChainVoteData -> OffChainVoteResult
349351
convert eres =

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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 (..))
1515
import qualified Cardano.Db as DB
16+
import Cardano.DbSync.Config.Types (OffChainUserAgent (..))
1617
import Cardano.DbSync.OffChain.Types (
1718
PoolOffChainMetadata (..),
1819
PoolTicker (..),
@@ -81,30 +82,32 @@ httpGetOffChainPoolData manager request purl expectedMetaHash = do
8182
httpGetOffChainVoteData ::
8283
[Text] ->
8384
VoteUrl ->
85+
OffChainUserAgent ->
8486
Maybe VoteMetaHash ->
8587
DB.AnchorType ->
8688
ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
87-
httpGetOffChainVoteData gateways vurl metaHash anchorType = do
89+
httpGetOffChainVoteData gateways vurl userAgent metaHash anchorType = do
8890
case useIpfsGatewayMaybe vurl gateways of
89-
Nothing -> httpGetOffChainVoteDataSingle vurl metaHash anchorType
91+
Nothing -> httpGetOffChainVoteDataSingle vurl userAgent metaHash anchorType
9092
Just [] -> left $ OCFErrNoIpfsGateway (OffChainVoteUrl vurl)
9193
Just urls -> tryAllGatewaysRec urls []
9294
where
9395
tryAllGatewaysRec [] acc = left $ OCFErrIpfsGatewayFailures (OffChainVoteUrl vurl) (reverse acc)
9496
tryAllGatewaysRec (url : rest) acc = do
95-
msocd <- liftIO $ runExceptT $ httpGetOffChainVoteDataSingle url metaHash anchorType
97+
msocd <- liftIO $ runExceptT $ httpGetOffChainVoteDataSingle url userAgent metaHash anchorType
9698
case msocd of
9799
Right socd -> pure socd
98100
Left err -> tryAllGatewaysRec rest (err : acc)
99101

100102
httpGetOffChainVoteDataSingle ::
101103
VoteUrl ->
104+
OffChainUserAgent ->
102105
Maybe VoteMetaHash ->
103106
DB.AnchorType ->
104107
ExceptT OffChainFetchError IO SimplifiedOffChainVoteData
105-
httpGetOffChainVoteDataSingle vurl metaHash anchorType = do
108+
httpGetOffChainVoteDataSingle vurl userAgent metaHash anchorType = do
106109
manager <- liftIO $ Http.newManager tlsManagerSettings
107-
request <- parseOffChainUrl url
110+
request <- parseOffChainUrl url userAgent
108111
let req = httpGetBytes manager request 3000000 3000000 url
109112
httpRes <- handleExceptT (convertHttpException url) req
110113
(respBS, respLBS, mContentType) <- hoistEither httpRes
@@ -205,22 +208,24 @@ isPossiblyJsonObject bs =
205208
-------------------------------------------------------------------------------------
206209
-- Url
207210
-------------------------------------------------------------------------------------
208-
parseOffChainUrl :: OffChainUrlType -> ExceptT OffChainFetchError IO Http.Request
209-
parseOffChainUrl url =
210-
handleExceptT wrapHttpException $ applyHeaders <$> Http.parseRequest (showUrl url)
211+
parseOffChainUrl :: OffChainUrlType -> OffChainUserAgent -> ExceptT OffChainFetchError IO Http.Request
212+
parseOffChainUrl url userAgent =
213+
handleExceptT wrapHttpException $ applyHeaders userAgent <$> Http.parseRequest (showUrl url)
211214
where
212215
wrapHttpException :: HttpException -> OffChainFetchError
213216
wrapHttpException err = OCFErrHttpException url (textShow err)
214217

215-
applyHeaders :: Http.Request -> Http.Request
216-
applyHeaders req =
218+
applyHeaders :: OffChainUserAgent -> Http.Request -> Http.Request
219+
applyHeaders (OffChainUserAgent mUserAgent) req =
217220
req
218221
{ Http.requestHeaders =
219222
Http.requestHeaders req
220223
++ [ (CI.mk "content-type", "application/json")
221-
, (CI.mk "user-agent", "cardano-db-sync")
224+
, (CI.mk "user-agent", Text.encodeUtf8 userAgent)
222225
]
223226
}
227+
where
228+
userAgent = fromMaybe "cardano-db-sync" mUserAgent
224229

225230
-------------------------------------------------------------------------------------
226231
-- Exceptions to Error

cardano-db-sync/test/Cardano/DbSync/Config/TypesTest.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,18 @@ genDefaultJson =
140140
},
141141
"governance": "enable",
142142
"offchain_pool_data": "enable",
143+
"offchain_user_agent": null,
143144
"json_type": "text"
144145
}
145146
|]
146147
, [aesonQQ|
147148
{ }
148149
|]
150+
, [aesonQQ|
151+
{
152+
"offchain_user_agent": "test-agent-v1.0"
153+
}
154+
|]
149155
, [aesonQQ|
150156
{
151157
"tx_out": {

0 commit comments

Comments
 (0)