Skip to content

Commit 1e545fc

Browse files
committed
add offchain user header to configuration
1 parent 83aeb07 commit 1e545fc

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
@@ -320,6 +320,7 @@ extractSyncOptions snp aop snc =
320320
, ioPoolStats = isPoolStatsEnabled (sioPoolStats (dncInsertOptions snc))
321321
, ioGov = useGovernance
322322
, ioRemoveJsonbFromSchema = isRemoveJsonbFromSchemaEnabled (sioRemoveJsonbFromSchema (dncInsertOptions snc))
323+
, ioOffChainUserAgent = sioOffchainUserAgent (dncInsertOptions snc)
323324
, ioTxOutVariantType = ioTxOutVariantType'
324325
}
325326

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 (SnapshotIntervalConfig, SyncNodeConfig)
31+
import Cardano.DbSync.Config.Types (OffChainUserAgent, SnapshotIntervalConfig, SyncNodeConfig)
3232
import Cardano.DbSync.Ledger.Types (HasLedgerEnv)
3333
import Cardano.DbSync.LocalStateQuery (NoLedgerEnv)
3434
import Cardano.DbSync.Types (
@@ -88,6 +88,7 @@ data InsertOptions = InsertOptions
8888
, ioPoolStats :: !Bool
8989
, ioGov :: !Bool
9090
, ioRemoveJsonbFromSchema :: !Bool
91+
, ioOffChainUserAgent :: !OffChainUserAgent
9192
, ioTxOutVariantType :: !DB.TxOutVariantType
9293
}
9394
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
SnapshotIntervalConfig (..),
4344
LedgerStateDir (..),
@@ -192,6 +193,7 @@ data SyncInsertOptions = SyncInsertOptions
192193
, sioPoolStats :: PoolStatsConfig
193194
, sioJsonType :: JsonTypeConfig
194195
, sioRemoveJsonbFromSchema :: RemoveJsonbFromSchemaConfig
196+
, sioOffchainUserAgent :: OffChainUserAgent
195197
, sioStopAtBlock :: Maybe Word64
196198
}
197199
deriving (Eq, Show)
@@ -266,6 +268,12 @@ newtype OffchainPoolDataConfig = OffchainPoolDataConfig
266268
}
267269
deriving (Eq, Show)
268270

271+
newtype OffChainUserAgent = OffChainUserAgent
272+
{ unOffChainUserAgent :: Maybe Text
273+
}
274+
deriving (Eq, Show)
275+
deriving newtype (ToJSON, FromJSON)
276+
269277
newtype RemoveJsonbFromSchemaConfig = RemoveJsonbFromSchemaConfig
270278
{ isRemoveJsonbFromSchemaEnabled :: Bool
271279
}
@@ -465,6 +473,7 @@ parseOverrides obj baseOptions = do
465473
<*> obj .:? "pool_stat" .!= sioPoolStats baseOptions
466474
<*> obj .:? "json_type" .!= sioJsonType baseOptions
467475
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
476+
<*> obj .:? "offchain_user_agent" .!= sioOffchainUserAgent baseOptions
468477
<*> obj .:? "stop_at_block" .!= sioStopAtBlock baseOptions
469478

470479
instance ToJSON SyncInsertConfig where
@@ -487,6 +496,7 @@ optionsToList SyncInsertOptions {..} =
487496
, toJsonIfSet "pool_stat" sioPoolStats
488497
, toJsonIfSet "json_type" sioJsonType
489498
, toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema
499+
, toJsonIfSet "offchain_user_agent" sioOffchainUserAgent
490500
, toJsonIfSet "stop_at_block" sioStopAtBlock
491501
]
492502

@@ -509,6 +519,7 @@ instance FromJSON SyncInsertOptions where
509519
<*> obj .:? "pool_stat" .!= sioPoolStats def
510520
<*> obj .:? "json_type" .!= sioJsonType def
511521
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema def
522+
<*> obj .:? "offchain_user_agent" .!= sioOffchainUserAgent def
512523
<*> obj .:? "stop_at_block" .!= sioStopAtBlock def
513524

514525
instance ToJSON SyncInsertOptions where
@@ -526,6 +537,7 @@ instance ToJSON SyncInsertOptions where
526537
, "pool_stat" .= sioPoolStats
527538
, "json_type" .= sioJsonType
528539
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
540+
, "offchain_user_agent" .= sioOffchainUserAgent
529541
, "stop_at_block" .= sioStopAtBlock
530542
]
531543

@@ -773,6 +785,7 @@ instance Default SyncInsertOptions where
773785
, sioPoolStats = PoolStatsConfig False
774786
, sioJsonType = JsonTypeText
775787
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
788+
, sioOffchainUserAgent = OffChainUserAgent Nothing
776789
, sioStopAtBlock = Nothing
777790
}
778791

@@ -792,6 +805,7 @@ fullInsertOptions =
792805
, sioPoolStats = PoolStatsConfig True
793806
, sioJsonType = JsonTypeText
794807
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
808+
, sioOffchainUserAgent = OffChainUserAgent Nothing
795809
, sioStopAtBlock = Nothing
796810
}
797811

@@ -811,6 +825,7 @@ onlyUTxOInsertOptions =
811825
, sioPoolStats = PoolStatsConfig False
812826
, sioJsonType = JsonTypeText
813827
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
828+
, sioOffchainUserAgent = OffChainUserAgent Nothing
814829
, sioStopAtBlock = Nothing
815830
}
816831

@@ -838,6 +853,7 @@ disableAllInsertOptions =
838853
, sioGovernance = GovernanceConfig False
839854
, sioJsonType = JsonTypeText
840855
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
856+
, sioOffchainUserAgent = OffChainUserAgent Nothing
841857
, sioStopAtBlock = Nothing
842858
}
843859

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)