Skip to content

Commit 97930bd

Browse files
committed
1333 - add new AddressDetail table for TxOut
1 parent 6883a93 commit 97930bd

File tree

26 files changed

+380
-127
lines changed

26 files changed

+380
-127
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+
, sioAddressDetail = AddressDetailConfig False
3738
}
3839

3940
dncInsertOptions cfg @?= expected

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ simpleScript =
9595
testLabel = "simpleScript-alonzo"
9696
getOutFields txOut = (DB.txOutAddress txOut, DB.txOutAddressHasScript txOut, DB.txOutValue txOut, DB.txOutDataHash txOut)
9797
expectedFields =
98-
( renderAddress alwaysSucceedsScriptAddr
98+
( Just $ renderAddress alwaysSucceedsScriptAddr
9999
, True
100100
, DB.DbLovelace 20000
101101
, Just $ Crypto.hashToBytes (extractHash $ hashData @StandardAlonzo plutusDataList)

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Config/Parse.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+
, sioAddressDetail = AddressDetailConfig False
3738
}
3839

3940
dncInsertOptions cfg @?= expected

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Plutus.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ simpleScript =
102102
testLabel = "simpleScript"
103103
getOutFields txOut = (DB.txOutAddress txOut, DB.txOutAddressHasScript txOut, DB.txOutValue txOut, DB.txOutDataHash txOut)
104104
expectedFields =
105-
( renderAddress alwaysSucceedsScriptAddr
105+
( Just $ renderAddress alwaysSucceedsScriptAddr
106106
, True
107107
, DB.DbLovelace 20000
108108
, Just $ Crypto.hashToBytes (extractHash $ hashData @StandardBabbage plutusDataList)

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+
, sioAddressDetail = AddressDetailConfig False
107108
}
108109

109110
dncInsertOptions cfg @?= expected

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ simpleScript =
9090
, DB.txOutDataHash txOut
9191
)
9292
expectedFields =
93-
( renderAddress Examples.alwaysSucceedsScriptAddr
93+
( Just $ renderAddress Examples.alwaysSucceedsScriptAddr
9494
, True
9595
, DB.DbLovelace 20_000
9696
, Just $

cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ module Test.Cardano.Db.Mock.Validate (
4444

4545
import Cardano.Db
4646
import qualified Cardano.Db as DB
47-
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
4847
import Cardano.DbSync.Era.Shelley.Generic.Util
4948
import qualified Cardano.Ledger.Address as Ledger
5049
import Cardano.Ledger.BaseTypes
@@ -212,7 +211,7 @@ assertAddrValues ::
212211
IO ()
213212
assertAddrValues env ix expected sta = do
214213
addr <- assertRight $ resolveAddress ix sta
215-
let address = Generic.renderAddress addr
214+
let address = Ledger.serialiseAddr addr
216215
q = queryAddressOutputs address
217216
assertEqBackoff env q expected defaultDelays "Unexpected Balance"
218217

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ extractSyncOptions snp aop snc =
278278
, ioPoolStats = isPoolStatsEnabled (sioPoolStats (dncInsertOptions snc))
279279
, ioGov = useGovernance
280280
, ioRemoveJsonbFromSchema = isRemoveJsonbFromSchemaEnabled (sioRemoveJsonbFromSchema (dncInsertOptions snc))
281+
, ioAddressDetail = useAddressDetailTable (sioAddressDetail (dncInsertOptions snc))
281282
}
282283

283284
useLedger = sioLedger (dncInsertOptions snc) == LedgerEnable

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ data InsertOptions = InsertOptions
8787
, ioPoolStats :: !Bool
8888
, ioGov :: !Bool
8989
, ioRemoveJsonbFromSchema :: !Bool
90+
, ioAddressDetail :: !Bool
9091
}
9192
deriving (Show)
9293

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Cardano.DbSync.Config.Types (
1919
GenesisHashAlonzo (..),
2020
GenesisHashConway (..),
2121
RemoveJsonbFromSchemaConfig (..),
22+
AddressDetailConfig (..),
2223
SyncNodeConfig (..),
2324
SyncPreConfig (..),
2425
SyncInsertConfig (..),
@@ -183,6 +184,7 @@ data SyncInsertOptions = SyncInsertOptions
183184
, sioPoolStats :: PoolStatsConfig
184185
, sioJsonType :: JsonTypeConfig
185186
, sioRemoveJsonbFromSchema :: RemoveJsonbFromSchemaConfig
187+
, sioAddressDetail :: AddressDetailConfig
186188
}
187189
deriving (Eq, Show)
188190

@@ -257,6 +259,11 @@ newtype RemoveJsonbFromSchemaConfig = RemoveJsonbFromSchemaConfig
257259
}
258260
deriving (Eq, Show)
259261

262+
newtype AddressDetailConfig = AddressDetailConfig
263+
{ useAddressDetailTable :: Bool
264+
}
265+
deriving (Eq, Show)
266+
260267
data JsonTypeConfig
261268
= JsonTypeText
262269
| JsonTypeJsonb
@@ -439,6 +446,7 @@ parseOverrides obj baseOptions = do
439446
<*> obj .:? "pool_stats" .!= sioPoolStats baseOptions
440447
<*> obj .:? "json_type" .!= sioJsonType baseOptions
441448
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
449+
<*> obj .:? "use_address_table" .!= sioAddressDetail baseOptions
442450

443451
instance ToJSON SyncInsertConfig where
444452
toJSON (SyncInsertConfig preset options) =
@@ -481,6 +489,7 @@ instance FromJSON SyncInsertOptions where
481489
<*> obj .:? "pool_stat" .!= sioPoolStats def
482490
<*> obj .:? "json_type" .!= sioJsonType def
483491
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema def
492+
<*> obj .:? "use_address_table" .!= sioAddressDetail def
484493

485494
instance ToJSON SyncInsertOptions where
486495
toJSON SyncInsertOptions {..} =
@@ -671,6 +680,15 @@ instance FromJSON RemoveJsonbFromSchemaConfig where
671680
instance ToJSON RemoveJsonbFromSchemaConfig where
672681
toJSON = boolToEnableDisable . isRemoveJsonbFromSchemaEnabled
673682

683+
instance FromJSON AddressDetailConfig where
684+
parseJSON = Aeson.withText "use_address_table" $ \v ->
685+
case enableDisableToBool v of
686+
Just g -> pure (AddressDetailConfig g)
687+
Nothing -> fail $ "unexpected use_address_table: " <> show v
688+
689+
instance ToJSON AddressDetailConfig where
690+
toJSON = boolToEnableDisable . useAddressDetailTable
691+
674692
instance FromJSON OffchainPoolDataConfig where
675693
parseJSON = Aeson.withText "offchain_pool_data" $ \v ->
676694
case enableDisableToBool v of
@@ -708,6 +726,7 @@ instance Default SyncInsertOptions where
708726
, sioPoolStats = PoolStatsConfig False
709727
, sioJsonType = JsonTypeText
710728
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
729+
, sioAddressDetail = AddressDetailConfig False
711730
}
712731

713732
fullInsertOptions :: SyncInsertOptions
@@ -726,6 +745,7 @@ fullInsertOptions =
726745
, sioPoolStats = PoolStatsConfig True
727746
, sioJsonType = JsonTypeText
728747
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
748+
, sioAddressDetail = AddressDetailConfig False
729749
}
730750

731751
onlyUTxOInsertOptions :: SyncInsertOptions
@@ -744,6 +764,7 @@ onlyUTxOInsertOptions =
744764
, sioPoolStats = PoolStatsConfig False
745765
, sioJsonType = JsonTypeText
746766
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
767+
, sioAddressDetail = AddressDetailConfig False
747768
}
748769

749770
onlyGovInsertOptions :: SyncInsertOptions
@@ -770,6 +791,7 @@ disableAllInsertOptions =
770791
, sioGovernance = GovernanceConfig False
771792
, sioJsonType = JsonTypeText
772793
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
794+
, sioAddressDetail = AddressDetailConfig False
773795
}
774796

775797
boolToEnableDisable :: IsString s => Bool -> s

cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ module Cardano.DbSync.Era.Byron.Genesis (
1111
) where
1212

1313
import Cardano.BM.Trace (Trace, logInfo)
14+
import Cardano.Binary (serialize')
1415
import qualified Cardano.Chain.Common as Byron
1516
import qualified Cardano.Chain.Genesis as Byron
1617
import qualified Cardano.Chain.UTxO as Byron
1718
import qualified Cardano.Crypto as Crypto
1819
import qualified Cardano.Db as DB
1920
import Cardano.DbSync.Api
20-
import Cardano.DbSync.Api.Types (SyncEnv (envBackend))
21+
import Cardano.DbSync.Api.Types (SyncEnv (..), SyncOptions (..), ioAddressDetail)
2122
import Cardano.DbSync.Config.Types
2223
import qualified Cardano.DbSync.Era.Byron.Util as Byron
2324
import Cardano.DbSync.Era.Util (liftLookupFail)
@@ -104,7 +105,7 @@ insertValidateGenesisDist syncEnv (NetworkName networkName) cfg = do
104105
, DB.blockOpCert = Nothing
105106
, DB.blockOpCertCounter = Nothing
106107
}
107-
mapM_ (insertTxOuts hasConsumed disInOut bid) $ genesisTxos cfg
108+
mapM_ (insertTxOuts syncEnv hasConsumed disInOut bid) $ genesisTxos cfg
108109
liftIO . logInfo tracer $
109110
"Initial genesis distribution populated. Hash "
110111
<> renderByteArray (configGenesisHash cfg)
@@ -175,48 +176,86 @@ validateGenesisDistribution prunes disInOut tracer networkName cfg bid =
175176

176177
insertTxOuts ::
177178
(MonadBaseControl IO m, MonadIO m) =>
179+
SyncEnv ->
178180
Bool ->
179181
Bool ->
180182
DB.BlockId ->
181183
(Byron.Address, Byron.Lovelace) ->
182184
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
183-
insertTxOuts hasConsumed disInOut blkId (address, value) = do
185+
insertTxOuts syncEnv hasConsumed disInOut blkId (address, value) = do
184186
case txHashOfAddress address of
185187
Left err -> throwError err
186-
Right val -> do
188+
Right val -> lift $ do
187189
-- Each address/value pair of the initial coin distribution comes from an artifical transaction
188190
-- with a hash generated by hashing the address.
189-
txId <-
190-
lift $
191-
DB.insertTx $
192-
DB.Tx
193-
{ DB.txHash = Byron.unTxHash val
194-
, DB.txBlockId = blkId
195-
, DB.txBlockIndex = 0
196-
, DB.txOutSum = DB.DbLovelace (Byron.unsafeGetLovelace value)
197-
, DB.txFee = DB.DbLovelace 0
198-
, DB.txDeposit = Just 0
199-
, DB.txSize = 0 -- Genesis distribution address to not have a size.
200-
, DB.txInvalidHereafter = Nothing
201-
, DB.txInvalidBefore = Nothing
202-
, DB.txValidContract = True
203-
, DB.txScriptSize = 0
204-
, DB.txTreasuryDonation = DB.DbLovelace 0
205-
}
206-
lift $
207-
DB.insertTxOutPlex hasConsumed disInOut $
208-
DB.TxOut
209-
{ DB.txOutTxId = txId
210-
, DB.txOutIndex = 0
211-
, DB.txOutAddress = Text.decodeUtf8 $ Byron.addrToBase58 address
212-
, DB.txOutAddressHasScript = False
213-
, DB.txOutPaymentCred = Nothing
214-
, DB.txOutStakeAddressId = Nothing
215-
, DB.txOutValue = DB.DbLovelace (Byron.unsafeGetLovelace value)
216-
, DB.txOutDataHash = Nothing
217-
, DB.txOutInlineDatumId = Nothing
218-
, DB.txOutReferenceScriptId = Nothing
191+
txId <- do
192+
DB.insertTx $
193+
DB.Tx
194+
{ DB.txHash = Byron.unTxHash val
195+
, DB.txBlockId = blkId
196+
, DB.txBlockIndex = 0
197+
, DB.txOutSum = DB.DbLovelace (Byron.unsafeGetLovelace value)
198+
, DB.txFee = DB.DbLovelace 0
199+
, DB.txDeposit = Just 0
200+
, DB.txSize = 0 -- Genesis distribution address to not have a size.
201+
, DB.txInvalidHereafter = Nothing
202+
, DB.txInvalidBefore = Nothing
203+
, DB.txValidContract = True
204+
, DB.txScriptSize = 0
205+
, DB.txTreasuryDonation = DB.DbLovelace 0
219206
}
207+
-- Insert the address detail config is active
208+
if ioAddressDetail . soptInsertOptions $ envOptions syncEnv
209+
then do
210+
addrDetailId <- insertAddressDetail
211+
DB.insertTxOutPlex hasConsumed disInOut $
212+
DB.TxOut
213+
{ DB.txOutTxId = txId
214+
, DB.txOutIndex = 0
215+
, DB.txOutAddress = Nothing
216+
, DB.txOutAddressHasScript = False
217+
, DB.txOutPaymentCred = Nothing
218+
, DB.txOutStakeAddressId = Nothing
219+
, DB.txOutValue = DB.DbLovelace (Byron.unsafeGetLovelace value)
220+
, DB.txOutDataHash = Nothing
221+
, DB.txOutInlineDatumId = Nothing
222+
, DB.txOutReferenceScriptId = Nothing
223+
, DB.txOutAddressDetailId = Just addrDetailId
224+
}
225+
else
226+
DB.insertTxOutPlex hasConsumed disInOut $
227+
DB.TxOut
228+
{ DB.txOutTxId = txId
229+
, DB.txOutIndex = 0
230+
, DB.txOutAddress = Just $ Text.decodeUtf8 $ Byron.addrToBase58 address
231+
, DB.txOutAddressHasScript = False
232+
, DB.txOutPaymentCred = Nothing
233+
, DB.txOutStakeAddressId = Nothing
234+
, DB.txOutValue = DB.DbLovelace (Byron.unsafeGetLovelace value)
235+
, DB.txOutDataHash = Nothing
236+
, DB.txOutInlineDatumId = Nothing
237+
, DB.txOutReferenceScriptId = Nothing
238+
, DB.txOutAddressDetailId = Nothing
239+
}
240+
where
241+
insertAddressDetail ::
242+
(MonadBaseControl IO m, MonadIO m) =>
243+
ReaderT SqlBackend m DB.AddressDetailId
244+
insertAddressDetail = do
245+
let addrRaw = serialize' address
246+
mAddrId <- DB.queryAddressDetailId addrRaw
247+
case mAddrId of
248+
Nothing ->
249+
DB.insertAddressDetail
250+
DB.AddressDetail
251+
{ DB.addressDetailAddress = Text.decodeUtf8 $ Byron.addrToBase58 address
252+
, DB.addressDetailAddressRaw = addrRaw
253+
, DB.addressDetailHasScript = False
254+
, DB.addressDetailPaymentCred = Nothing -- Byron does not have a payment credential.
255+
, DB.addressDetailStakeAddressId = Nothing -- Byron does not have a stake address.
256+
}
257+
-- this address is already in the database, so we can just return the id to be linked to the txOut.
258+
Just addrId -> pure addrId
220259

221260
-- -----------------------------------------------------------------------------
222261

0 commit comments

Comments
 (0)