From 39168813e9cfc0990f68fcbf2bfcd92c9f8c2bf1 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Thu, 4 Jan 2024 12:45:05 +0200 Subject: [PATCH 01/17] Use the first which is always the minimum --- .../src/Cardano/DbSync/Era/Shelley/Insert/Grouped.hs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Grouped.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Grouped.hs index ec3c81057..bdc04d203 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Grouped.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Grouped.hs @@ -102,7 +102,7 @@ insertBlockGroupedData syncEnv grouped = do lift $ DB.updateListTxOutConsumedByTxId $ catMaybes updateTuples void . lift . DB.insertManyTxMetadata $ groupedTxMetadata grouped void . lift . DB.insertManyTxMint $ groupedTxMint grouped - pure $ DB.MinIds (minimumMaybe txInIds) (minimumMaybe txOutIds) (minimumMaybe maTxOutIds) + pure $ DB.MinIds (listToMaybe txInIds) (listToMaybe txOutIds) (listToMaybe maTxOutIds) where tracer = getTrace syncEnv @@ -214,8 +214,3 @@ matches :: Generic.TxIn -> ExtendedTxOut -> Bool matches txIn eutxo = Generic.txInHash txIn == etoTxHash eutxo && Generic.txInIndex txIn == DB.txOutIndex (etoTxOut eutxo) - -minimumMaybe :: (Ord a, Foldable f) => f a -> Maybe a -minimumMaybe xs - | null xs = Nothing - | otherwise = Just $ minimum xs From 2e79ee479d04eed605c459fdc9bb393e2884c5f8 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Mon, 8 Jan 2024 20:18:11 +0200 Subject: [PATCH 02/17] Insert offchain data rarely when syncing --- cardano-db-sync/src/Cardano/DbSync/Api.hs | 8 ++++---- cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/Api.hs b/cardano-db-sync/src/Cardano/DbSync/Api.hs index 650bc9970..23345537c 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Api.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Api.hs @@ -376,10 +376,10 @@ mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemS bts <- getBootstrapInProgress trce (enpBootstrap syncNP) backend bootstrapVar <- newTVarIO bts -- Offline Pool + Anchor queues - opwq <- newTBQueueIO 100 - oprq <- newTBQueueIO 100 - oawq <- newTBQueueIO 100 - oarq <- newTBQueueIO 100 + opwq <- newTBQueueIO 1000 + oprq <- newTBQueueIO 1000 + oawq <- newTBQueueIO 1000 + oarq <- newTBQueueIO 1000 epochVar <- newTVarIO initEpochState epochSyncTime <- newTVarIO =<< getCurrentTime ledgerEnvType <- diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs index d69c02da3..9e919c549 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -197,11 +197,11 @@ insertShelleyBlock syncEnv shouldLog withinTwoMins withinHalfHour blk details is insertStakeSlice syncEnv $ apStakeSlice applyResult - when (ioGov iopts) + when (ioGov iopts && (withinHalfHour || unBlockNo (Generic.blkBlockNo blk) `mod` 10000 == 0)) . lift $ insertOffChainVoteResults tracer (envOffChainVoteResultQueue syncEnv) - when (ioOffChainPoolData iopts) + when (ioOffChainPoolData iopts && (withinHalfHour || unBlockNo (Generic.blkBlockNo blk) `mod` 10000 == 0)) . lift $ insertOffChainPoolResults tracer (envOffChainPoolResultQueue syncEnv) where From fd98ed6032a737f628f309fb7be70e80ed4e2ff2 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Mon, 8 Jan 2024 22:07:32 +0200 Subject: [PATCH 03/17] Drop uneeded checks from domains Fixes https://github.com/IntersectMBO/cardano-db-sync/issues/1584 --- schema/migration-2-0035-20240108.sql | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 schema/migration-2-0035-20240108.sql diff --git a/schema/migration-2-0035-20240108.sql b/schema/migration-2-0035-20240108.sql new file mode 100644 index 000000000..a92584547 --- /dev/null +++ b/schema/migration-2-0035-20240108.sql @@ -0,0 +1,29 @@ +-- Persistent generated migration. + +CREATE FUNCTION migrate() RETURNS void AS $$ +DECLARE + next_version int ; +BEGIN + SELECT stage_two + 1 INTO next_version FROM schema_version ; + IF next_version = 35 THEN + EXECUTE 'alter domain addr29type drop constraint addr29type_check' ; + EXECUTE 'alter domain asset32type drop constraint asset32type_check' ; + EXECUTE 'alter domain hash28type drop constraint hash28type_check' ; + EXECUTE 'alter domain hash32type drop constraint hash32type_check' ; + EXECUTE 'alter domain int65type drop constraint int65type_check' ; + EXECUTE 'alter domain lovelace drop constraint lovelace_check' ; + EXECUTE 'alter domain txindex drop constraint txindex_check' ; + EXECUTE 'alter domain word128type drop constraint word128type_check' ; + EXECUTE 'alter domain word31type drop constraint word31type_check' ; + EXECUTE 'alter domain word63type drop constraint word63type_check' ; + EXECUTE 'alter domain word64type drop constraint word64type_check' ; + -- Hand written SQL statements can be added here. + UPDATE schema_version SET stage_two = next_version ; + RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; + END IF ; +END ; +$$ LANGUAGE plpgsql ; + +SELECT migrate() ; + +DROP FUNCTION migrate() ; From c86a2efb0ca1a567d787d3693616ae64a5c689a3 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 9 Jan 2024 12:59:54 +0200 Subject: [PATCH 04/17] Remove redundant tx_out fields --- cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs | 5 +++-- cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs | 2 -- cardano-db-sync/src/Cardano/DbSync/Era/Byron/Insert.hs | 6 ------ .../src/Cardano/DbSync/Era/Shelley/Generic/Tx/Alonzo.hs | 2 -- .../src/Cardano/DbSync/Era/Shelley/Generic/Tx/Babbage.hs | 4 ---- .../src/Cardano/DbSync/Era/Shelley/Generic/Tx/Mary.hs | 4 ---- .../src/Cardano/DbSync/Era/Shelley/Generic/Tx/Shelley.hs | 5 ----- .../src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs | 1 - cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs | 3 --- cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs | 6 ++---- .../src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs | 4 ---- cardano-db/src/Cardano/Db/Multiplex.hs | 1 - cardano-db/src/Cardano/Db/Query.hs | 4 ++-- cardano-db/src/Cardano/Db/Schema.hs | 4 ---- cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs | 3 +-- cardano-db/test/Test/IO/Cardano/Db/Util.hs | 2 +- 16 files changed, 9 insertions(+), 47 deletions(-) diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs index fb1d9dfee..176bdbd4c 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs @@ -42,6 +42,7 @@ module Test.Cardano.Db.Mock.Validate ( import Cardano.Db import qualified Cardano.Db as DB +import qualified Cardano.DbSync.Era.Shelley.Generic as Generic import Cardano.DbSync.Era.Shelley.Generic.Util import qualified Cardano.Ledger.Address as Ledger import Cardano.Ledger.BaseTypes @@ -197,8 +198,8 @@ assertAddrValues :: IO () assertAddrValues env ix expected sta = do addr <- assertRight $ resolveAddress ix sta - let addrBs = Ledger.serialiseAddr addr - q = queryAddressOutputs addrBs + let address = Generic.renderAddress addr + q = queryAddressOutputs address assertEqBackoff env q expected defaultDelays "Unexpected Balance" assertRight :: Show err => Either err a -> IO a diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs index be9258e9e..0af89a365 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Genesis.hs @@ -11,7 +11,6 @@ module Cardano.DbSync.Era.Byron.Genesis ( ) where import Cardano.BM.Trace (Trace, logInfo) -import qualified Cardano.Binary as Binary import qualified Cardano.Chain.Common as Byron import qualified Cardano.Chain.Genesis as Byron import qualified Cardano.Chain.UTxO as Byron @@ -209,7 +208,6 @@ insertTxOuts hasConsumed disInOut blkId (address, value) = do { DB.txOutTxId = txId , DB.txOutIndex = 0 , DB.txOutAddress = Text.decodeUtf8 $ Byron.addrToBase58 address - , DB.txOutAddressRaw = Binary.serialize' address , DB.txOutAddressHasScript = False , DB.txOutPaymentCred = Nothing , DB.txOutStakeAddressId = Nothing diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Insert.hs index 192af940d..efb316ff7 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Byron/Insert.hs @@ -12,11 +12,6 @@ module Cardano.DbSync.Era.Byron.Insert ( import Cardano.BM.Trace (Trace, logDebug, logInfo) import Cardano.Binary (serialize') -import qualified Cardano.Binary as Binary - --- Import all 'cardano-ledger' functions and data types qualified so they do not --- clash with the Cardano.Db functions and data types which are also imported --- qualified. import qualified Cardano.Chain.Block as Byron hiding (blockHash) import qualified Cardano.Chain.Common as Byron import qualified Cardano.Chain.UTxO as Byron @@ -327,7 +322,6 @@ insertTxOut _tracer hasConsumed bootStrap txId index txout = { DB.txOutTxId = txId , DB.txOutIndex = fromIntegral index , DB.txOutAddress = Text.decodeUtf8 $ Byron.addrToBase58 (Byron.txOutAddress txout) - , DB.txOutAddressRaw = Binary.serialize' (Byron.txOutAddress txout) , DB.txOutAddressHasScript = False , DB.txOutPaymentCred = Nothing -- Byron does not have a payment credential. , DB.txOutStakeAddressId = Nothing -- Byron does not have a stake address. diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Alonzo.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Alonzo.hs index f0d810b3b..be17923c0 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Alonzo.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Alonzo.hs @@ -119,14 +119,12 @@ fromAlonzoTx ioExtraPlutus mprices (blkIndex, tx) = TxOut { txOutIndex = index , txOutAddress = txOut ^. Core.addrTxOutL - , txOutAddressRaw = SBS.fromShort bs , txOutAdaValue = ada , txOutMaValue = maMap , txOutScript = Nothing , txOutDatum = getMaybeDatumHash $ strictMaybeToMaybe mDataHash } where - bs = Ledger.unCompactAddr $ txOut ^. Core.compactAddrTxOutL MaryValue ada (MultiAsset maMap) = txOut ^. Core.valueTxOutL mDataHash = txOut ^. Alonzo.dataHashTxOutL diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Babbage.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Babbage.hs index daea9a4e3..03115436e 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Babbage.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Babbage.hs @@ -19,7 +19,6 @@ import Cardano.DbSync.Era.Shelley.Generic.Tx.Alonzo import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley import Cardano.DbSync.Era.Shelley.Generic.Tx.Types import Cardano.DbSync.Era.Shelley.Generic.Witness -import qualified Cardano.Ledger.Address as Ledger import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo import qualified Cardano.Ledger.Alonzo.Tx as Alonzo import Cardano.Ledger.Babbage.Core as Core hiding (Tx, TxOut) @@ -31,7 +30,6 @@ import qualified Cardano.Ledger.Era as Ledger import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..)) import qualified Cardano.Ledger.Plutus.Data as Alonzo import Cardano.Prelude -import qualified Data.ByteString.Short as SBS import qualified Data.Map.Strict as Map import Lens.Micro import Ouroboros.Consensus.Shelley.Eras (StandardBabbage, StandardCrypto) @@ -123,14 +121,12 @@ fromTxOut index txOut = TxOut { txOutIndex = index , txOutAddress = txOut ^. Core.addrTxOutL - , txOutAddressRaw = SBS.fromShort bs , txOutAdaValue = ada , txOutMaValue = maMap , txOutScript = fromScript <$> strictMaybeToMaybe mScript , txOutDatum = fromDatum datum } where - bs = Ledger.unCompactAddr $ txOut ^. Core.compactAddrTxOutL MaryValue ada (MultiAsset maMap) = txOut ^. Core.valueTxOutL datum = txOut ^. Core.datumTxOutL mScript = txOut ^. Core.referenceScriptTxOutL diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Mary.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Mary.hs index 4ef7f0b3c..b92c09ee5 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Mary.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Mary.hs @@ -11,13 +11,11 @@ import Cardano.DbSync.Era.Shelley.Generic.Tx.Allegra (getInterval, getScripts) import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley import Cardano.DbSync.Era.Shelley.Generic.Tx.Types import Cardano.DbSync.Era.Shelley.Generic.Witness -import qualified Cardano.Ledger.Address as Ledger import qualified Cardano.Ledger.Core as Core import Cardano.Ledger.Mary.TxBody import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..)) import qualified Cardano.Ledger.Shelley.Tx as ShelleyTx import Cardano.Prelude -import qualified Data.ByteString.Short as SBS import Lens.Micro ((^.)) import Ouroboros.Consensus.Cardano.Block (StandardMary) @@ -63,14 +61,12 @@ fromMaryTx (blkIndex, tx) = TxOut { txOutIndex = index , txOutAddress = txOut ^. Core.addrTxOutL - , txOutAddressRaw = SBS.fromShort bs , txOutAdaValue = ada , txOutMaValue = maMap , txOutScript = Nothing , txOutDatum = NoDatum -- Mary does not support plutus data } where - bs = Ledger.unCompactAddr $ txOut ^. Core.compactAddrTxOutL MaryValue ada (MultiAsset maMap) = txOut ^. Core.valueTxOutL (invBefore, invAfter) = getInterval txBody diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Shelley.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Shelley.hs index 46803d1fd..7cd69fc56 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Shelley.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Shelley.hs @@ -32,7 +32,6 @@ import Cardano.DbSync.Era.Shelley.Generic.Script (fromMultiSig) import Cardano.DbSync.Era.Shelley.Generic.Tx.Types import Cardano.DbSync.Era.Shelley.Generic.Util import Cardano.DbSync.Era.Shelley.Generic.Witness -import qualified Cardano.Ledger.Address as Ledger import Cardano.Ledger.BaseTypes (TxIx (..), strictMaybeToMaybe) import Cardano.Ledger.Coin (Coin (..)) import qualified Cardano.Ledger.Core as Core @@ -45,7 +44,6 @@ import Cardano.Ledger.Shelley.TxCert import Cardano.Prelude import qualified Data.Aeson as Aeson import qualified Data.ByteString.Lazy.Char8 as LBS -import qualified Data.ByteString.Short as SBS import qualified Data.Map.Strict as Map import Lens.Micro ((^.)) import Ouroboros.Consensus.Cardano.Block (StandardCrypto, StandardShelley) @@ -112,14 +110,11 @@ mkTxOut txBody = zipWith fromTxOut [0 ..] $ toList (txBody ^. Core.outputsTxBody TxOut { txOutIndex = index , txOutAddress = txOut ^. Core.addrTxOutL - , txOutAddressRaw = SBS.fromShort bs , txOutAdaValue = txOut ^. Core.valueTxOutL , txOutMaValue = mempty -- Shelley does not support multi-assets , txOutScript = Nothing , txOutDatum = NoDatum -- Shelley does not support plutus data } - where - bs = Ledger.unCompactAddr $ txOut ^. Core.compactAddrTxOutL fromTxIn :: ShelleyTx.TxIn StandardCrypto -> TxIn fromTxIn (ShelleyTx.TxIn (ShelleyTx.TxId txid) (TxIx w64)) = diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs index d247c0d2c..499fcb1fd 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Types.hs @@ -91,7 +91,6 @@ data TxIn = TxIn data TxOut = TxOut { txOutIndex :: !Word64 , txOutAddress :: !(Ledger.Addr StandardCrypto) - , txOutAddressRaw :: !ByteString , txOutAdaValue :: !Coin , txOutMaValue :: !(Map (PolicyID StandardCrypto) (Map AssetName Integer)) , txOutScript :: Maybe TxScript diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs index d4c7b9857..9d8d2ae8c 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Genesis.hs @@ -21,7 +21,6 @@ import Cardano.DbSync.Era.Shelley.Insert import Cardano.DbSync.Era.Util (liftLookupFail) import Cardano.DbSync.Error import Cardano.DbSync.Util -import qualified Cardano.Ledger.Address as Ledger import qualified Cardano.Ledger.Coin as Ledger import qualified Cardano.Ledger.Core as Core import Cardano.Ledger.Credential (Credential (KeyHashObj)) @@ -247,7 +246,6 @@ insertTxOuts trce hasConsumed disInOut blkId (ShelleyTx.TxIn txInId _, txOut) = { DB.txOutTxId = txId , DB.txOutIndex = 0 , DB.txOutAddress = Generic.renderAddress addr - , DB.txOutAddressRaw = Ledger.serialiseAddr addr , DB.txOutAddressHasScript = hasScript , DB.txOutPaymentCred = Generic.maybePaymentCred addr , DB.txOutStakeAddressId = Nothing -- No stake addresses in Shelley Genesis @@ -258,7 +256,6 @@ insertTxOuts trce hasConsumed disInOut blkId (ShelleyTx.TxIn txInId _, txOut) = } where addr = txOut ^. Core.addrTxOutL - hasScript = maybe False Generic.hasCredScript (Generic.getPaymentCred addr) -- Insert pools and delegations coming from Genesis. diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs index 9e919c549..11650955b 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -395,7 +395,7 @@ prepareTxOut :: (DB.TxId, ByteString) -> Generic.TxOut -> ExceptT SyncNodeError (ReaderT SqlBackend m) (ExtendedTxOut, [MissingMaTxOut]) -prepareTxOut tracer cache iopts (txId, txHash) (Generic.TxOut index addr addrRaw value maMap mScript dt) = do +prepareTxOut tracer cache iopts (txId, txHash) (Generic.TxOut index addr value maMap mScript dt) = do mSaId <- lift $ insertStakeAddressRefIfMissing tracer cache addr mDatumId <- whenFalseEmpty (ioPlutusExtra iopts) Nothing $ @@ -410,7 +410,6 @@ prepareTxOut tracer cache iopts (txId, txHash) (Generic.TxOut index addr addrRaw { DB.txOutTxId = txId , DB.txOutIndex = index , DB.txOutAddress = Generic.renderAddress addr - , DB.txOutAddressRaw = addrRaw , DB.txOutAddressHasScript = hasScript , DB.txOutPaymentCred = Generic.maybePaymentCred addr , DB.txOutStakeAddressId = mSaId @@ -434,7 +433,7 @@ insertCollateralTxOut :: (DB.TxId, ByteString) -> Generic.TxOut -> ExceptT SyncNodeError (ReaderT SqlBackend m) () -insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index addr addrRaw value maMap mScript dt) = do +insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index addr value maMap mScript dt) = do mSaId <- lift $ insertStakeAddressRefIfMissing tracer cache addr mDatumId <- whenFalseEmpty (ioPlutusExtra iopts) Nothing $ @@ -451,7 +450,6 @@ insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index ad { DB.collateralTxOutTxId = txId , DB.collateralTxOutIndex = index , DB.collateralTxOutAddress = Generic.renderAddress addr - , DB.collateralTxOutAddressRaw = addrRaw , DB.collateralTxOutAddressHasScript = hasScript , DB.collateralTxOutPaymentCred = Generic.maybePaymentCred addr , DB.collateralTxOutStakeAddressId = mSaId diff --git a/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs b/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs index d5ebe9c37..7417bb44d 100644 --- a/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs +++ b/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs @@ -140,7 +140,6 @@ share txId TxId noreference index Word64 sqltype=txindex address Text - addressRaw ByteString addressHasScript Bool paymentCred ByteString Maybe sqltype=hash28type stakeAddressId StakeAddressId Maybe noreference @@ -155,7 +154,6 @@ share txId TxId noreference -- This type is the primary key for the 'tx' table. index Word64 sqltype=txindex address Text - addressRaw ByteString addressHasScript Bool paymentCred ByteString Maybe sqltype=hash28type stakeAddressId StakeAddressId Maybe noreference @@ -600,7 +598,6 @@ schemaDocs = TxOutTxId # "The Tx table index of the transaction that contains this transaction output." TxOutIndex # "The index of this transaction output with the transaction." TxOutAddress # "The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era." - TxOutAddressRaw # "The raw binary address." TxOutAddressHasScript # "Flag which shows if this address is locked by a script." TxOutPaymentCred # "The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash." TxOutStakeAddressId # "The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses)." @@ -614,7 +611,6 @@ schemaDocs = CollateralTxOutTxId # "The Tx table index of the transaction that contains this transaction output." CollateralTxOutIndex # "The index of this transaction output with the transaction." CollateralTxOutAddress # "The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era." - CollateralTxOutAddressRaw # "The raw binary address." CollateralTxOutAddressHasScript # "Flag which shows if this address is locked by a script." CollateralTxOutPaymentCred # "The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash." CollateralTxOutStakeAddressId # "The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses)." diff --git a/cardano-db/src/Cardano/Db/Multiplex.hs b/cardano-db/src/Cardano/Db/Multiplex.hs index 668a18d36..ebb207e6d 100644 --- a/cardano-db/src/Cardano/Db/Multiplex.hs +++ b/cardano-db/src/Cardano/Db/Multiplex.hs @@ -66,7 +66,6 @@ toExtraTxOut txOut = { ExtraCons.txOutTxId = changeKey $ txOutTxId txOut , ExtraCons.txOutIndex = txOutIndex txOut , ExtraCons.txOutAddress = txOutAddress txOut - , ExtraCons.txOutAddressRaw = txOutAddressRaw txOut , ExtraCons.txOutAddressHasScript = txOutAddressHasScript txOut , ExtraCons.txOutPaymentCred = txOutPaymentCred txOut , ExtraCons.txOutStakeAddressId = changeKey <$> txOutStakeAddressId txOut diff --git a/cardano-db/src/Cardano/Db/Query.hs b/cardano-db/src/Cardano/Db/Query.hs index ebfc5969a..9e4fcff23 100644 --- a/cardano-db/src/Cardano/Db/Query.hs +++ b/cardano-db/src/Cardano/Db/Query.hs @@ -1192,11 +1192,11 @@ queryAddressBalanceAtSlot addr slotNo = do Queries use in tests ------------------------} -queryAddressOutputs :: MonadIO m => ByteString -> ReaderT SqlBackend m DbLovelace +queryAddressOutputs :: MonadIO m => Text -> ReaderT SqlBackend m DbLovelace queryAddressOutputs addr = do res <- select $ do txout <- from $ table @TxOut - where_ (txout ^. TxOutAddressRaw ==. val addr) + where_ (txout ^. TxOutAddress ==. val addr) pure $ sum_ (txout ^. TxOutValue) pure $ convert (listToMaybe res) where diff --git a/cardano-db/src/Cardano/Db/Schema.hs b/cardano-db/src/Cardano/Db/Schema.hs index 56effd467..23ad9d6e8 100644 --- a/cardano-db/src/Cardano/Db/Schema.hs +++ b/cardano-db/src/Cardano/Db/Schema.hs @@ -144,7 +144,6 @@ share txId TxId noreference index Word64 sqltype=txindex address Text - addressRaw ByteString addressHasScript Bool paymentCred ByteString Maybe sqltype=hash28type stakeAddressId StakeAddressId Maybe noreference @@ -158,7 +157,6 @@ share txId TxId noreference -- This type is the primary key for the 'tx' table. index Word64 sqltype=txindex address Text - addressRaw ByteString addressHasScript Bool paymentCred ByteString Maybe sqltype=hash28type stakeAddressId StakeAddressId Maybe noreference @@ -762,7 +760,6 @@ schemaDocs = TxOutTxId # "The Tx table index of the transaction that contains this transaction output." TxOutIndex # "The index of this transaction output with the transaction." TxOutAddress # "The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era." - TxOutAddressRaw # "The raw binary address." TxOutAddressHasScript # "Flag which shows if this address is locked by a script." TxOutPaymentCred # "The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash." TxOutStakeAddressId # "The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses)." @@ -776,7 +773,6 @@ schemaDocs = CollateralTxOutTxId # "The Tx table index of the transaction that contains this transaction output." CollateralTxOutIndex # "The index of this transaction output with the transaction." CollateralTxOutAddress # "The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era." - CollateralTxOutAddressRaw # "The raw binary address." CollateralTxOutAddressHasScript # "Flag which shows if this address is locked by a script." CollateralTxOutPaymentCred # "The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash." CollateralTxOutStakeAddressId # "The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses)." diff --git a/cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs b/cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs index fd55f17fd..4c8ac1f0b 100644 --- a/cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs +++ b/cardano-db/test/Test/IO/Cardano/Db/TotalSupply.hs @@ -5,7 +5,6 @@ module Test.IO.Cardano.Db.TotalSupply ( ) where import Cardano.Db -import qualified Data.ByteString.Char8 as BS import qualified Data.Text as Text import Test.IO.Cardano.Db.Util import Test.Tasty (TestTree, testGroup) @@ -53,6 +52,6 @@ initialSupplyTest = } _ <- insertTxIn (TxIn tx1Id (head tx0Ids) 0 Nothing) let addr = mkAddressHash bid1 tx1Id - _ <- insertTxOut $ TxOut tx1Id 0 (Text.pack addr) (BS.pack addr) False Nothing Nothing (DbLovelace 500000000) Nothing Nothing Nothing + _ <- insertTxOut $ TxOut tx1Id 0 (Text.pack addr) False Nothing Nothing (DbLovelace 500000000) Nothing Nothing Nothing supply1 <- queryTotalSupply assertBool ("Total supply should be < " ++ show supply0) (supply1 < supply0) diff --git a/cardano-db/test/Test/IO/Cardano/Db/Util.hs b/cardano-db/test/Test/IO/Cardano/Db/Util.hs index 1a29a3cb8..2fce1c651 100644 --- a/cardano-db/test/Test/IO/Cardano/Db/Util.hs +++ b/cardano-db/test/Test/IO/Cardano/Db/Util.hs @@ -100,4 +100,4 @@ testSlotLeader = mkTxOut :: BlockId -> TxId -> TxOut mkTxOut blkId txId = let addr = mkAddressHash blkId txId - in TxOut txId 0 (Text.pack addr) (BS.pack addr) False Nothing Nothing (DbLovelace 1000000000) Nothing Nothing Nothing + in TxOut txId 0 (Text.pack addr) False Nothing Nothing (DbLovelace 1000000000) Nothing Nothing Nothing From 00c2ff37b4767bd9b80d5833c62afe232c322e04 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Thu, 18 Jan 2024 11:13:13 +0200 Subject: [PATCH 05/17] Add migration to remove redundant tx_out fields --- schema/migration-2-0036-20240117.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 schema/migration-2-0036-20240117.sql diff --git a/schema/migration-2-0036-20240117.sql b/schema/migration-2-0036-20240117.sql new file mode 100644 index 000000000..90c0aeb23 --- /dev/null +++ b/schema/migration-2-0036-20240117.sql @@ -0,0 +1,21 @@ +-- Persistent generated migration. + +CREATE FUNCTION migrate() RETURNS void AS $$ +DECLARE + next_version int ; +BEGIN + SELECT stage_two + 1 INTO next_version FROM schema_version ; + IF next_version = 36 THEN + EXECUTE 'ALTER TABLE "tx_out" DROP COLUMN "address_raw"' ; + + EXECUTE 'ALTER TABLE "collateral_tx_out" DROP COLUMN "address_raw"' ; + -- Hand written SQL statements can be added here. + UPDATE schema_version SET stage_two = next_version ; + RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; + END IF ; +END ; +$$ LANGUAGE plpgsql ; + +SELECT migrate() ; + +DROP FUNCTION migrate() ; From 1a2b326875244ba9c888c89404cb65ee035fc54e Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Thu, 18 Jan 2024 15:05:52 +0200 Subject: [PATCH 06/17] Add instant_reward and adjust reward table Move MIR to a separate table Use "generated always as" for earned_epoch Remove primary keys --- cardano-db-sync/src/Cardano/DbSync/Default.hs | 4 +- .../src/Cardano/DbSync/Era/Shelley/Adjust.hs | 7 +- .../DbSync/Era/Shelley/Generic/Rewards.hs | 19 +++-- .../DbSync/Era/Shelley/Insert/Epoch.hs | 79 ++++++++++++------- .../src/Cardano/DbSync/Era/Util.hs | 2 +- .../src/Cardano/DbSync/Ledger/Event.hs | 22 +++--- cardano-db/src/Cardano/Db/Delete.hs | 2 +- cardano-db/src/Cardano/Db/Insert.hs | 7 ++ .../Migration/Extra/CosnumedTxOut/Schema.hs | 34 -------- cardano-db/src/Cardano/Db/Query.hs | 12 --- cardano-db/src/Cardano/Db/Schema.hs | 40 ++++++++-- schema/migration-2-0037-20240117.sql | 23 ++++++ schema/migration-2-0038-20240117.sql | 35 ++++++++ 13 files changed, 180 insertions(+), 106 deletions(-) create mode 100644 schema/migration-2-0037-20240117.sql create mode 100644 schema/migration-2-0038-20240117.sql diff --git a/cardano-db-sync/src/Cardano/DbSync/Default.hs b/cardano-db-sync/src/Cardano/DbSync/Default.hs index 1e48d53a6..4f1833a2f 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Default.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Default.hs @@ -23,7 +23,7 @@ import Cardano.DbSync.Era.Cardano.Insert (insertEpochSyncTime) import Cardano.DbSync.Era.Shelley.Adjust (adjustEpochRewards) import qualified Cardano.DbSync.Era.Shelley.Generic as Generic import Cardano.DbSync.Era.Shelley.Insert (insertShelleyBlock, mkAdaPots) -import Cardano.DbSync.Era.Shelley.Insert.Epoch (insertPoolDepositRefunds, insertRewards) +import Cardano.DbSync.Era.Shelley.Insert.Epoch (insertInstantRewards, insertPoolDepositRefunds, insertRewards) import Cardano.DbSync.Era.Shelley.Validate (validateEpochRewards) import Cardano.DbSync.Error import Cardano.DbSync.Fix.EpochStake @@ -293,7 +293,7 @@ insertLedgerEvents syncEnv currentEpochNo@(EpochNo curEpoch) = LedgerMirDist rwd -> do unless (Map.null rwd) $ do let rewards = Map.toList rwd - insertRewards syncEnv ntw (subFromCurrentEpoch 1) currentEpochNo cache rewards + insertInstantRewards ntw (subFromCurrentEpoch 1) currentEpochNo cache rewards liftIO . logInfo tracer $ "Inserted " <> show (length rewards) <> " Mir rewards" LedgerPoolReap en drs -> unless (Map.null $ Generic.unRewards drs) $ do diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Adjust.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Adjust.hs index cfcab9cf2..eb9f616ba 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Adjust.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Adjust.hs @@ -22,7 +22,6 @@ import Cardano.Slotting.Slot (EpochNo (..)) import Control.Monad.Trans.Control (MonadBaseControl) import qualified Data.Map.Strict as Map import qualified Data.Set as Set -import qualified Data.Strict.Maybe as Strict import Database.Esqueleto.Experimental ( SqlBackend, delete, @@ -80,9 +79,7 @@ deleteReward :: ReaderT SqlBackend m () deleteReward nw cache epochNo (cred, rwd) = do mAddrId <- queryStakeAddrWithCache cache DontCacheNew nw cred - eiPoolId <- case Generic.rewardPool rwd of - Strict.Nothing -> pure $ Left $ Db.DbLookupMessage "deleteReward.queryPoolKeyWithCache" - Strict.Just poolHash -> queryPoolKeyWithCache cache DontCacheNew poolHash + eiPoolId <- queryPoolKeyWithCache cache DontCacheNew (Generic.rewardPool rwd) case (mAddrId, eiPoolId) of (Right addrId, Right poolId) -> do delete $ do @@ -90,7 +87,7 @@ deleteReward nw cache epochNo (cred, rwd) = do where_ (rwdDb ^. Db.RewardAddrId ==. val addrId) where_ (rwdDb ^. Db.RewardType ==. val (Generic.rewardSource rwd)) where_ (rwdDb ^. Db.RewardSpendableEpoch ==. val (unEpochNo epochNo)) - where_ (rwdDb ^. Db.RewardPoolId ==. val (Just poolId)) + where_ (rwdDb ^. Db.RewardPoolId ==. val poolId) _ -> pure () deleteOrphanedRewards :: MonadIO m => EpochNo -> [Db.StakeAddressId] -> ReaderT SqlBackend m () diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Rewards.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Rewards.hs index a3f79ab83..6604bffef 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Rewards.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Rewards.hs @@ -6,6 +6,8 @@ module Cardano.DbSync.Era.Shelley.Generic.Rewards ( Reward (..), Rewards (..), + InstantReward (..), + InstantRewards (..), rewardsCount, rewardsTotalAda, ) where @@ -16,24 +18,31 @@ import Cardano.Ledger.Coin (Coin (..)) import Cardano.Prelude import qualified Data.Map.Strict as Map import qualified Data.Set as Set -import qualified Data.Strict.Maybe as Strict import Ouroboros.Consensus.Cardano.CanHardFork () data Reward = Reward { rewardSource :: !RewardSource - , rewardPool :: !(Strict.Maybe PoolKeyHash) + , rewardPool :: !PoolKeyHash , rewardAmount :: !Coin } deriving (Eq, Ord, Show) --- The `ledger-specs` code defines a `RewardUpdate` type that is parameterised over --- Shelley/Allegra/Mary. This is a huge pain in the neck for `db-sync` so we define a --- generic one instead. newtype Rewards = Rewards { unRewards :: Map StakeCred (Set Reward) } deriving (Eq, Show) +data InstantReward = InstantReward + { irSource :: !RewardSource + , irAmount :: !Coin + } + deriving (Eq, Ord, Show) + +newtype InstantRewards = InstantRewards + { unIRewards :: Map StakeCred (Set Reward) + } + deriving (Eq, Show) + rewardsCount :: Rewards -> Int rewardsCount = sum . map Set.size . Map.elems . unRewards diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Epoch.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Epoch.hs index b7bdf06de..fb09ff7f5 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Epoch.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert/Epoch.hs @@ -11,6 +11,7 @@ module Cardano.DbSync.Era.Shelley.Insert.Epoch ( insertRewards, + insertInstantRewards, insertPoolDepositRefunds, insertStakeSlice, sumRewardTotal, @@ -31,11 +32,9 @@ import qualified Cardano.Ledger.Coin as Shelley import Cardano.Prelude import Cardano.Slotting.Slot (EpochNo (..)) import Control.Concurrent.Class.MonadSTM.Strict (readTVarIO) -import Control.Monad.Extra (mapMaybeM) import Control.Monad.Trans.Control (MonadBaseControl) import qualified Data.Map.Strict as Map import qualified Data.Set as Set -import qualified Data.Strict.Maybe as Strict import Database.Persist.Sql (SqlBackend) {- HLINT ignore "Use readTVarIO" -} @@ -117,44 +116,70 @@ insertRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk = do ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.Reward] mkRewards (saddr, rset) = do saId <- lift $ queryOrInsertStakeAddress cache CacheNew nw saddr - mapMaybeM (prepareReward saId) (Set.toList rset) + mapM (prepareReward saId) (Set.toList rset) - -- For rewards with a null pool, the reward unique key doesn't work. - -- So we need to manually check that it's not already in the db. - -- This can happen on rollbacks. prepareReward :: (MonadBaseControl IO m, MonadIO m) => DB.StakeAddressId -> Generic.Reward -> - ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.Reward) + ExceptT SyncNodeError (ReaderT SqlBackend m) DB.Reward prepareReward saId rwd = do - mPool <- queryPool (Generic.rewardPool rwd) - let rwdDb = - DB.Reward - { DB.rewardAddrId = saId - , DB.rewardType = Generic.rewardSource rwd - , DB.rewardAmount = Generic.coinToDbLovelace (Generic.rewardAmount rwd) - , DB.rewardEarnedEpoch = unEpochNo earnedEpoch - , DB.rewardSpendableEpoch = unEpochNo spendableEpoch - , DB.rewardPoolId = mPool - } - case DB.rewardPoolId rwdDb of - Just _ -> pure $ Just rwdDb - Nothing -> do - exists <- lift $ DB.queryNullPoolRewardExists rwdDb - if exists then pure Nothing else pure (Just rwdDb) + poolId <- queryPool (Generic.rewardPool rwd) + pure $ + DB.Reward + { DB.rewardAddrId = saId + , DB.rewardType = Generic.rewardSource rwd + , DB.rewardAmount = Generic.coinToDbLovelace (Generic.rewardAmount rwd) + , DB.rewardEarnedEpoch = unEpochNo earnedEpoch + , DB.rewardSpendableEpoch = unEpochNo spendableEpoch + , DB.rewardPoolId = poolId + } queryPool :: (MonadBaseControl IO m, MonadIO m) => - Strict.Maybe PoolKeyHash -> - ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.PoolHashId) - queryPool Strict.Nothing = pure Nothing - queryPool (Strict.Just poolHash) = - Just <$> lift (queryPoolKeyOrInsert "insertRewards" trce cache CacheNew (ioShelley iopts) poolHash) + PoolKeyHash -> + ExceptT SyncNodeError (ReaderT SqlBackend m) DB.PoolHashId + queryPool poolHash = + lift (queryPoolKeyOrInsert "insertRewards" trce cache CacheNew (ioShelley iopts) poolHash) trce = getTrace syncEnv iopts = getInsertOptions syncEnv +insertInstantRewards :: + (MonadBaseControl IO m, MonadIO m) => + Network -> + EpochNo -> + EpochNo -> + Cache -> + [(StakeCred, Set Generic.InstantReward)] -> + ExceptT SyncNodeError (ReaderT SqlBackend m) () +insertInstantRewards nw earnedEpoch spendableEpoch cache rewardsChunk = do + dbRewards <- concatMapM mkRewards rewardsChunk + let chunckDbRewards = splittRecordsEvery 100000 dbRewards + -- minimising the bulk inserts into hundred thousand chunks to improve performance + forM_ chunckDbRewards $ \rws -> lift $ DB.insertManyInstantRewards rws + where + mkRewards :: + (MonadBaseControl IO m, MonadIO m) => + (StakeCred, Set Generic.InstantReward) -> + ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.InstantReward] + mkRewards (saddr, rset) = do + saId <- lift $ queryOrInsertStakeAddress cache CacheNew nw saddr + pure $ map (prepareReward saId) (Set.toList rset) + + prepareReward :: + DB.StakeAddressId -> + Generic.InstantReward -> + DB.InstantReward + prepareReward saId rwd = + DB.InstantReward + { DB.instantRewardAddrId = saId + , DB.instantRewardType = Generic.irSource rwd + , DB.instantRewardAmount = Generic.coinToDbLovelace (Generic.irAmount rwd) + , DB.instantRewardEarnedEpoch = unEpochNo earnedEpoch + , DB.instantRewardSpendableEpoch = unEpochNo spendableEpoch + } + splittRecordsEvery :: Int -> [a] -> [[a]] splittRecordsEvery val = go where diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Util.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Util.hs index 8c4548fe3..ba794eff9 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Util.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Util.hs @@ -47,6 +47,6 @@ safeDecodeToJson tracer tracePrefix x = do -- See https://github.com/IntersectMBO/cardano-db-sync/issues/297 if containsUnicodeNul json then do - liftIO $ logWarning tracer $ tracePrefix <> ": dropped due to a Unicode NUL character." + liftIO $ logWarning tracer $ tracePrefix <> ": dropped due to a Unicode NUL character. " <> json pure Nothing else pure $ Just json diff --git a/cardano-db-sync/src/Cardano/DbSync/Ledger/Event.hs b/cardano-db-sync/src/Cardano/DbSync/Ledger/Event.hs index 335985118..b4567561c 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Ledger/Event.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Ledger/Event.hs @@ -52,7 +52,6 @@ import Data.SOP.BasicFunctors import Data.SOP.Constraint import Data.SOP.Strict (hcmap, hcollapse) import qualified Data.Set as Set -import qualified Data.Strict.Maybe as Strict import Ouroboros.Consensus.Byron.Ledger.Block (ByronBlock) import Ouroboros.Consensus.Cardano.Block import Ouroboros.Consensus.HardFork.Combinator.AcrossEras ( @@ -64,7 +63,7 @@ import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock, ShelleyLedgerEvent (..) import Ouroboros.Consensus.TypeFamilyWrappers data LedgerEvent - = LedgerMirDist !(Map StakeCred (Set Generic.Reward)) + = LedgerMirDist !(Map StakeCred (Set Generic.InstantReward)) | LedgerPoolReap !EpochNo !Generic.Rewards | LedgerIncrementalRewards !EpochNo !Generic.Rewards | LedgerDeltaRewards !EpochNo !Generic.Rewards @@ -260,30 +259,29 @@ convertPoolDepositRefunds rwds = convert (kh, coin) = Generic.Reward { Generic.rewardSource = RwdDepositRefund - , Generic.rewardPool = Strict.Just kh + , Generic.rewardPool = kh , Generic.rewardAmount = coin } convertMirRewards :: Map StakeCred Coin -> Map StakeCred Coin -> - Map StakeCred (Set Generic.Reward) + Map StakeCred (Set Generic.InstantReward) convertMirRewards resPay trePay = Map.unionWith Set.union (convertResPay resPay) (convertTrePay trePay) where - convertResPay :: Map StakeCred Coin -> Map StakeCred (Set Generic.Reward) + convertResPay :: Map StakeCred Coin -> Map StakeCred (Set Generic.InstantReward) convertResPay = Map.map (mkPayment RwdReserves) - convertTrePay :: Map StakeCred Coin -> Map StakeCred (Set Generic.Reward) + convertTrePay :: Map StakeCred Coin -> Map StakeCred (Set Generic.InstantReward) convertTrePay = Map.map (mkPayment RwdTreasury) - mkPayment :: RewardSource -> Coin -> Set Generic.Reward + mkPayment :: RewardSource -> Coin -> Set Generic.InstantReward mkPayment src coin = Set.singleton $ - Generic.Reward - { Generic.rewardSource = src - , Generic.rewardPool = Strict.Nothing - , Generic.rewardAmount = coin + Generic.InstantReward + { Generic.irSource = src + , Generic.irAmount = coin } convertPoolRewards :: @@ -298,7 +296,7 @@ convertPoolRewards rmap = Generic.Reward { Generic.rewardSource = rewardTypeToSource $ Ledger.rewardType sr , Generic.rewardAmount = Ledger.rewardAmount sr - , Generic.rewardPool = Strict.Just $ Ledger.rewardPool sr + , Generic.rewardPool = Ledger.rewardPool sr } -------------------------------------------------------------------------------- diff --git a/cardano-db/src/Cardano/Db/Delete.hs b/cardano-db/src/Cardano/Db/Delete.hs index 3b2ec5240..be541fd5f 100644 --- a/cardano-db/src/Cardano/Db/Delete.hs +++ b/cardano-db/src/Cardano/Db/Delete.hs @@ -82,7 +82,7 @@ deleteBlocksBlockId trce blockId = do liftIO $ logWarning trce - "Failed to find ReverseInex. Deletion may take longer." + "Failed to find ReverseIndex. Deletion may take longer." pure (minIds, False) Just minIdDB -> do let minIds' = minIds <> minIdDB diff --git a/cardano-db/src/Cardano/Db/Insert.hs b/cardano-db/src/Cardano/Db/Insert.hs index 14645af46..c6d8e9607 100644 --- a/cardano-db/src/Cardano/Db/Insert.hs +++ b/cardano-db/src/Cardano/Db/Insert.hs @@ -18,6 +18,7 @@ module Cardano.Db.Insert ( insertExtraKeyWitness, insertManyEpochStakes, insertManyRewards, + insertManyInstantRewards, insertManyDrepDistr, insertManyTxIn, insertMaTxMint, @@ -199,6 +200,12 @@ insertManyRewards :: ReaderT SqlBackend m () insertManyRewards = insertManyWithManualUnique "Many Rewards" +insertManyInstantRewards :: + (MonadBaseControl IO m, MonadIO m) => + [InstantReward] -> + ReaderT SqlBackend m () +insertManyInstantRewards = insertManyCheckUnique "Many Instant Rewards" + insertManyDrepDistr :: (MonadBaseControl IO m, MonadIO m) => [DrepDistr] -> diff --git a/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs b/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs index 7417bb44d..bbc453186 100644 --- a/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs +++ b/cardano-db/src/Cardano/Db/Migration/Extra/CosnumedTxOut/Schema.hs @@ -26,7 +26,6 @@ import Cardano.Db.Types ( DbInt65, DbLovelace, DbWord64, - RewardSource, ScriptPurpose, ScriptType, SyncState, @@ -289,24 +288,6 @@ share txId TxId noreference -- ----------------------------------------------------------------------------------------------- - -- Reward, Stake and Treasury need to be obtained from the ledger state. - - -- The reward for each stake address and. This is not a balance, but a reward amount and the - -- epoch in which the reward was earned. - -- This table should never get rolled back. - Reward - addrId StakeAddressId noreference - type RewardSource sqltype=rewardtype - amount DbLovelace sqltype=lovelace - earnedEpoch Word64 - spendableEpoch Word64 - poolId PoolHashId Maybe noreference - -- Usually NULLables are not allowed in a uniqueness constraint. The semantics of how NULL - -- interacts with those constraints is non-trivial: two NULL values are not considered equal - -- for the purposes of an uniqueness constraint. - -- Use of "!force" attribute on the end of the line disables this check. - UniqueReward addrId type earnedEpoch poolId !force - deriving Show Withdrawal addrId StakeAddressId noreference @@ -740,21 +721,6 @@ schemaDocs = TxMetadataBytes # "The raw bytes of the payload." TxMetadataTxId # "The Tx table index of the transaction where this metadata was included." - Reward --^ do - "A table for earned rewards. It includes 5 types of rewards. The rewards are inserted incrementally and\ - \ this procedure is finalised when the spendable epoch comes. Before the epoch comes, some entries\ - \ may be missing." - RewardAddrId # "The StakeAddress table index for the stake address that earned the reward." - RewardType # "The source of the rewards; pool `member`, pool `leader`, `treasury` or `reserves` payment and pool deposits `refunds`" - RewardAmount # "The reward amount (in Lovelace)." - RewardEarnedEpoch - # "The epoch in which the reward was earned. For `pool` and `leader` rewards spendable in epoch `N`, this will be\ - \ `N - 2`, for `treasury` and `reserves` `N - 1` and for `refund` N." - RewardSpendableEpoch # "The epoch in which the reward is actually distributed and can be spent." - RewardPoolId - # "The PoolHash table index for the pool the stake address was delegated to when\ - \ the reward is earned or for the pool that there is a deposit refund. Will be NULL for payments from the treasury or the reserves." - Withdrawal --^ do "A table for withdrawals from a reward account." WithdrawalAddrId # "The StakeAddress table index for the stake address for which the withdrawal is for." diff --git a/cardano-db/src/Cardano/Db/Query.hs b/cardano-db/src/Cardano/Db/Query.hs index 9e4fcff23..deb617e9c 100644 --- a/cardano-db/src/Cardano/Db/Query.hs +++ b/cardano-db/src/Cardano/Db/Query.hs @@ -21,7 +21,6 @@ module Cardano.Db.Query ( queryCalcEpochEntry, queryCurrentEpochNo, queryNormalEpochRewardCount, - queryNullPoolRewardExists, queryGenesis, queryGenesisSupply, queryShelleyGenesisSupply, @@ -429,17 +428,6 @@ queryNormalEpochRewardCount epochNum = do pure countRows pure $ maybe 0 unValue (listToMaybe res) -queryNullPoolRewardExists :: MonadIO m => Reward -> ReaderT SqlBackend m Bool -queryNullPoolRewardExists newRwd = do - res <- select $ do - rwd <- from $ table @Reward - where_ (rwd ^. RewardAddrId ==. val (rewardAddrId newRwd)) - where_ (rwd ^. RewardType ==. val (rewardType newRwd)) - where_ (rwd ^. RewardEarnedEpoch ==. val (rewardEarnedEpoch newRwd)) - limit 1 - pure (rwd ^. RewardId) - pure $ not (null res) - queryGenesis :: MonadIO m => ReaderT SqlBackend m (Either LookupFail BlockId) queryGenesis = do res <- select $ do diff --git a/cardano-db/src/Cardano/Db/Schema.hs b/cardano-db/src/Cardano/Db/Schema.hs index 23ad9d6e8..515aac7d0 100644 --- a/cardano-db/src/Cardano/Db/Schema.hs +++ b/cardano-db/src/Cardano/Db/Schema.hs @@ -302,12 +302,24 @@ share addrId StakeAddressId noreference type RewardSource sqltype=rewardtype amount DbLovelace sqltype=lovelace - earnedEpoch Word64 + earnedEpoch Word64 generated="((CASE WHEN (type='refund') then spendable_epoch else (CASE WHEN spendable_epoch >= 2 then spendable_epoch-2 else 0 end) end) STORED)" spendableEpoch Word64 - poolId PoolHashId Maybe noreference - -- Here used to lie a uniqye constraint which would slow down inserts when in syncing mode + poolId PoolHashId noreference + -- Here used to lie a unique constraint which would slow down inserts when in syncing mode + -- Now the constraint is set manually inside of `applyAndInsertBlockMaybe` once the tip of + -- the chain has been reached. + deriving Show + + InstantReward + addrId StakeAddressId noreference + type RewardSource sqltype=rewardtype + amount DbLovelace sqltype=lovelace + earnedEpoch Word64 generated="(CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)" + spendableEpoch Word64 + -- Here used to lie a unique constraint which would slow down inserts when in syncing mode -- Now the constraint is set manually inside of `applyAndInsertBlockMaybe` once the tip of -- the chain has been reached. + UniqueInstantReward addrId earnedEpoch type deriving Show Withdrawal @@ -903,19 +915,33 @@ schemaDocs = TxMetadataTxId # "The Tx table index of the transaction where this metadata was included." Reward --^ do - "A table for earned rewards. It includes 5 types of rewards. The rewards are inserted incrementally and\ + "A table for earned staking rewards. After 13.2 release it includes only 3 types of rewards: member, leader and refund, \ + \ since the other 2 types have moved to a separate table instant_reward.\ + \ The rewards are inserted incrementally and\ \ this procedure is finalised when the spendable epoch comes. Before the epoch comes, some entries\ \ may be missing." RewardAddrId # "The StakeAddress table index for the stake address that earned the reward." - RewardType # "The source of the rewards; pool `member`, pool `leader`, `treasury` or `reserves` payment and pool deposits `refunds`" + RewardType # "The type of the rewards" RewardAmount # "The reward amount (in Lovelace)." RewardEarnedEpoch # "The epoch in which the reward was earned. For `pool` and `leader` rewards spendable in epoch `N`, this will be\ - \ `N - 2`, for `treasury` and `reserves` `N - 1` and for `refund` N." + \ `N - 2`, `refund` N." RewardSpendableEpoch # "The epoch in which the reward is actually distributed and can be spent." RewardPoolId # "The PoolHash table index for the pool the stake address was delegated to when\ - \ the reward is earned or for the pool that there is a deposit refund. Will be NULL for payments from the treasury or the reserves." + \ the reward is earned or for the pool that there is a deposit refund." + + InstantReward --^ do + "A table for earned instant rewards. It includes only 2 types of rewards: reserves and treasury.\ + \ This table only exists for historic reasons, since instant rewards are depredated after Conway.\ + \ New in 13.2" + InstantRewardAddrId # "The StakeAddress table index for the stake address that earned the reward." + InstantRewardType # "The type of the rewards." + InstantRewardAmount # "The reward amount (in Lovelace)." + InstantRewardEarnedEpoch + # "The epoch in which the reward was earned. For rewards spendable in epoch `N`, this will be\ + \ `N - 1`." + InstantRewardSpendableEpoch # "The epoch in which the reward is actually distributed and can be spent." Withdrawal --^ do "A table for withdrawals from a reward account." diff --git a/schema/migration-2-0037-20240117.sql b/schema/migration-2-0037-20240117.sql new file mode 100644 index 000000000..755bc70ce --- /dev/null +++ b/schema/migration-2-0037-20240117.sql @@ -0,0 +1,23 @@ +-- Persistent generated migration. + +CREATE FUNCTION migrate() RETURNS void AS $$ +DECLARE + next_version int ; +BEGIN + SELECT stage_two + 1 INTO next_version FROM schema_version ; + IF next_version = 37 THEN + EXECUTE 'CREATe TABLE instant_reward AS TABLE reward WITH NO DATA'; + EXECUTE 'INSERT INTO instant_reward (SELECT * FROM reward WHERE pool_id IS NULL)'; +-- EXECUTE 'ALTER TABLE "instant_reward"("id" SERIAL8 PRIMARY KEY UNIQUE,"addr_id" INT8 NOT NULL,"type" rewardtype NOT NULL,"amount" lovelace NOT NULL,"earned_epoch" INT8 NOT NULL GENERATED ALWAYS AS ((CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)) STORED,"spendable_epoch" INT8 NOT NULL)' ; +-- EXECUTE 'ALTER TABLE "instant_reward" ADD CONSTRAINT "unique_instant_reward" UNIQUE("addr_id","earned_epoch","type")' ; + -- EXECUTE 'ALTER TABLE "reward" ALTER COLUMN "pool_id" SET NOT NULL' ; + -- Hand written SQL statements can be added here. + UPDATE schema_version SET stage_two = next_version ; + RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; + END IF ; +END ; +$$ LANGUAGE plpgsql ; + +SELECT migrate() ; + +DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0038-20240117.sql b/schema/migration-2-0038-20240117.sql new file mode 100644 index 000000000..0da91eec9 --- /dev/null +++ b/schema/migration-2-0038-20240117.sql @@ -0,0 +1,35 @@ +-- Persistent generated migration. + +CREATE FUNCTION migrate() RETURNS void AS $$ +DECLARE + next_version int ; +BEGIN + SELECT stage_two + 1 INTO next_version FROM schema_version ; + IF next_version = 38 THEN + EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "id" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "instant_reward" DROP COLUMN id'; + EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "addr_id" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "type" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "amount" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "spendable_epoch" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "instant_reward" DROP COLUMN "earned_epoch"' ; + EXECUTE 'ALTER TABLE "instant_reward" ADD COLUMN "earned_epoch" bigint NOT NULL GENERATED ALWAYS AS ((CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)) STORED' ; + EXECUTE 'ALTER TABLE "instant_reward" DROP COLUMN "pool_id"' ; + EXECUTE 'ALTER TABLE "instant_reward" ADD CONSTRAINT "unique_instant_reward" UNIQUE("addr_id","earned_epoch","type")' ; + + EXECUTE 'DELETE FROM "reward" WHERE "pool_id" IS NULL' ; + EXECUTE 'ALTER TABLE "reward" ALTER COLUMN "pool_id" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "reward" DROP COLUMN id'; + EXECUTE 'ALTER TABLE "reward" DROP COLUMN "earned_epoch"' ; + ALTER TABLE "reward" ADD COLUMN "earned_epoch" bigint NOT NULL GENERATED ALWAYS AS + (CASE WHEN (type='refund') then spendable_epoch else (CASE WHEN spendable_epoch >= 2 then spendable_epoch-2 else 0 end) end) STORED; + -- Hand written SQL statements can be added here. + UPDATE schema_version SET stage_two = next_version ; + RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; + END IF ; +END ; +$$ LANGUAGE plpgsql ; + +SELECT migrate() ; + +DROP FUNCTION migrate() ; From 5473d2e97c114a0a1fb84bc68ebf0e67e90211fe Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Thu, 18 Jan 2024 15:11:13 +0200 Subject: [PATCH 07/17] Remove reward constraint hack Since instant rewards no longer cause a uniqueness issue --- cardano-db-sync/src/Cardano/DbSync.hs | 2 -- 1 file changed, 2 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync.hs b/cardano-db-sync/src/Cardano/DbSync.hs index 273a13c3e..3235497c9 100644 --- a/cardano-db-sync/src/Cardano/DbSync.hs +++ b/cardano-db-sync/src/Cardano/DbSync.hs @@ -51,7 +51,6 @@ import Cardano.DbSync.Rollback (unsafeRollback) import Cardano.DbSync.Sync (runSyncNodeClient) import Cardano.DbSync.Tracing.ToObjectOrphans () import Cardano.DbSync.Types -import Cardano.DbSync.Util.Constraint (addRewardConstraintsIfNotExist) import Cardano.Prelude hiding (Nat, (%)) import Cardano.Slotting.Slot (EpochNo (..)) import Control.Concurrent.Async @@ -180,7 +179,6 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc syncNodeParams ranMigrations runMigrationFnc - liftIO $ runReaderT (addRewardConstraintsIfNotExist syncEnv trce) (envBackend syncEnv) liftIO $ runExtraMigrationsMaybe syncEnv unless (enpShouldUseLedger syncNodeParams) $ liftIO $ do logInfo trce "Migrating to a no ledger schema" From d5c422166b24d52e6fb8af23d6538bbdee6514dc Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Thu, 18 Jan 2024 21:38:20 +0200 Subject: [PATCH 08/17] Filter generated values correctly --- cardano-db/src/Cardano/Db/Insert.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cardano-db/src/Cardano/Db/Insert.hs b/cardano-db/src/Cardano/Db/Insert.hs index c6d8e9607..b1224cb2b 100644 --- a/cardano-db/src/Cardano/Db/Insert.hs +++ b/cardano-db/src/Cardano/Db/Insert.hs @@ -499,7 +499,7 @@ insertManyUnique vtype constraintExists constraintName records = do ] values :: [PersistValue] - values = concatMap (map toPersistValue . toPersistFields) records + values = concatMap Util.mkInsertValues records conflictQuery :: Text conflictQuery = From 09616bee0d91dae4785ff1b50637e1119cad54b5 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Fri, 19 Jan 2024 01:47:02 +0200 Subject: [PATCH 09/17] Fix reward tests --- .../Cardano/Db/Mock/Unit/Alonzo/Reward.hs | 4 +-- .../Cardano/Db/Mock/Unit/Babbage/Reward.hs | 3 +- .../test/Test/Cardano/Db/Mock/Validate.hs | 36 ++++++++++++------- cardano-db/src/Cardano/Db/Query.hs | 8 +++++ cardano-db/src/Cardano/Db/Schema.hs | 3 -- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Reward.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Reward.hs index 736088db7..f09943b11 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Reward.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Reward.hs @@ -52,7 +52,7 @@ import Test.Cardano.Db.Mock.UnifiedApi ( withAlonzoFindLeaderAndSubmit, withAlonzoFindLeaderAndSubmitTx, ) -import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertRewardCount, assertRewardCounts) +import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertInstantRewardCount, assertRewardCount, assertRewardCounts) import Test.Tasty.HUnit (Assertion) simpleRewards :: IOManager -> [(Text, Text)] -> Assertion @@ -481,6 +481,6 @@ singleMIRCertMultiOut = b <- fillUntilNextEpoch interpreter mockServer assertBlockNoBackoff dbSync (2 + length a + length b) - assertRewardCount dbSync 4 + assertInstantRewardCount dbSync 4 where testLabel = "singleMIRCertMultiOut-alonzo" diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Reward.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Reward.hs index d77ad1170..8d8ace347 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Reward.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/Reward.hs @@ -51,6 +51,7 @@ import Test.Cardano.Db.Mock.UnifiedApi ( import Test.Cardano.Db.Mock.Validate ( assertBlockNoBackoff, assertCurrentEpoch, + assertInstantRewardCount, assertRewardCount, assertRewardCounts, ) @@ -566,6 +567,6 @@ singleMIRCertMultiOut = b <- fillUntilNextEpoch interpreter mockServer assertBlockNoBackoff dbSync (2 + length a + length b) - assertRewardCount dbSync 4 + assertInstantRewardCount dbSync 4 where testLabel = "singleMIRCertMultiOut" diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs index 176bdbd4c..024755f31 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs @@ -14,6 +14,7 @@ module Test.Cardano.Db.Mock.Validate ( assertTxInCount, assertUnspentTx, assertRewardCount, + assertInstantRewardCount, assertBlockNoBackoff, assertBlockNoBackoffTimes, assertEqQuery, @@ -79,7 +80,7 @@ import Database.Esqueleto.Legacy ( (==.), (^.), ) -import Database.Persist.Sql (Entity, SqlBackend, entityVal) +import Database.Persist.Sql (Entity, SqlBackend) import Database.PostgreSQL.Simple (SqlError (..)) import Ouroboros.Consensus.Cardano.Block import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock) @@ -112,6 +113,10 @@ assertRewardCount :: DBSyncEnv -> Word64 -> IO () assertRewardCount env n = assertEqBackoff env queryRewardCount n defaultDelays "Unexpected rewards count" +assertInstantRewardCount :: DBSyncEnv -> Word64 -> IO () +assertInstantRewardCount env n = + assertEqBackoff env queryInstantRewardCount n defaultDelays "Unexpected instant rewards count" + assertBlockNoBackoff :: DBSyncEnv -> Int -> IO () assertBlockNoBackoff = assertBlockNoBackoffTimes defaultDelays @@ -242,7 +247,7 @@ assertRewardCounts env st filterAddr mEpoch expected = do expectedMap :: Map ByteString (Word64, Word64, Word64, Word64, Word64) expectedMap = Map.fromList $ fmap (first mkDBStakeAddress) expected - groupByAddress :: [(Reward, ByteString)] -> Map ByteString (Word64, Word64, Word64, Word64, Word64) + groupByAddress :: [(RewardSource, ByteString)] -> Map ByteString (Word64, Word64, Word64, Word64, Word64) groupByAddress rewards = let res = foldr updateMap Map.empty rewards in if filterAddr @@ -255,17 +260,17 @@ assertRewardCounts env st filterAddr mEpoch expected = do Right cred -> Ledger.serialiseRewardAcnt $ Ledger.RewardAcnt Testnet cred updateAddrCounters :: - Reward -> + RewardSource -> Maybe (Word64, Word64, Word64, Word64, Word64) -> (Word64, Word64, Word64, Word64, Word64) - updateAddrCounters reward Nothing = updateCounters reward (0, 0, 0, 0, 0) - updateAddrCounters reward (Just cs) = updateCounters reward cs + updateAddrCounters rs Nothing = updateCounters rs (0, 0, 0, 0, 0) + updateAddrCounters rs (Just cs) = updateCounters rs cs updateCounters :: - Reward -> + RewardSource -> (Word64, Word64, Word64, Word64, Word64) -> (Word64, Word64, Word64, Word64, Word64) - updateCounters reward (a, b, c, d, e) = case rewardType reward of + updateCounters rs (a, b, c, d, e) = case rs of RwdLeader -> (a + 1, b, c, d, e) RwdMember -> (a, b + 1, c, d, e) RwdReserves -> (a, b, c + 1, d, e) @@ -273,21 +278,28 @@ assertRewardCounts env st filterAddr mEpoch expected = do RwdDepositRefund -> (a, b, c, d, e + 1) updateMap :: - (Reward, ByteString) -> + (RewardSource, ByteString) -> Map ByteString (Word64, Word64, Word64, Word64, Word64) -> Map ByteString (Word64, Word64, Word64, Word64, Word64) - updateMap (rew, addr) = Map.alter (Just . updateAddrCounters rew) addr + updateMap (rs, addr) = Map.alter (Just . updateAddrCounters rs) addr filterEpoch rw = case mEpoch of Nothing -> val True Just e -> rw ^. RewardSpendableEpoch ==. val e + filterEpoch' rw = case mEpoch of + Nothing -> val True + Just e -> rw ^. InstantRewardSpendableEpoch ==. val e q = do - res <- select . from $ \(reward `InnerJoin` stake_addr) -> do + res1 <- select . from $ \(reward `InnerJoin` stake_addr) -> do on (reward ^. RewardAddrId ==. stake_addr ^. StakeAddressId) where_ (filterEpoch reward) - pure (reward, stake_addr ^. StakeAddressHashRaw) - pure $ fmap (bimap entityVal unValue) res + pure (reward ^. RewardType, stake_addr ^. StakeAddressHashRaw) + res2 <- select . from $ \(ireward `InnerJoin` stake_addr) -> do + on (ireward ^. InstantRewardAddrId ==. stake_addr ^. StakeAddressId) + where_ (filterEpoch' ireward) + pure (ireward ^. InstantRewardType, stake_addr ^. StakeAddressHashRaw) + pure $ fmap (bimap unValue unValue) (res1 <> res2) assertEpochStake :: DBSyncEnv -> Word64 -> IO () assertEpochStake env expected = diff --git a/cardano-db/src/Cardano/Db/Query.hs b/cardano-db/src/Cardano/Db/Query.hs index deb617e9c..57ca2d2c5 100644 --- a/cardano-db/src/Cardano/Db/Query.hs +++ b/cardano-db/src/Cardano/Db/Query.hs @@ -89,6 +89,7 @@ module Cardano.Db.Query ( -- queries used only in tests queryAddressOutputs, queryRewardCount, + queryInstantRewardCount, queryTxInCount, queryEpochCount, queryCostModel, @@ -1199,6 +1200,13 @@ queryRewardCount = do pure countRows pure $ maybe 0 unValue (listToMaybe res) +queryInstantRewardCount :: MonadIO m => ReaderT SqlBackend m Word64 +queryInstantRewardCount = do + res <- select $ do + _ <- from $ table @InstantReward + pure countRows + pure $ maybe 0 unValue (listToMaybe res) + -- | Count the number of transactions in the Tx table. queryTxInCount :: MonadIO m => ReaderT SqlBackend m Word queryTxInCount = do diff --git a/cardano-db/src/Cardano/Db/Schema.hs b/cardano-db/src/Cardano/Db/Schema.hs index 515aac7d0..f0f47cfce 100644 --- a/cardano-db/src/Cardano/Db/Schema.hs +++ b/cardano-db/src/Cardano/Db/Schema.hs @@ -316,9 +316,6 @@ share amount DbLovelace sqltype=lovelace earnedEpoch Word64 generated="(CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)" spendableEpoch Word64 - -- Here used to lie a unique constraint which would slow down inserts when in syncing mode - -- Now the constraint is set manually inside of `applyAndInsertBlockMaybe` once the tip of - -- the chain has been reached. UniqueInstantReward addrId earnedEpoch type deriving Show From 6f0ee96a32f1c11a2b14b08d315e85b3cf4023bd Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Fri, 19 Jan 2024 01:48:00 +0200 Subject: [PATCH 10/17] Fix indexes tests --- .../Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs | 4 ++-- .../Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs | 4 ++-- schema/migration-4-0002-20200810.sql | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs index b7d0a91c8..877e6a045 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs @@ -19,7 +19,7 @@ checkForceIndexesArg = withCustomConfig commandLineForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do startDBSync dbSyncEnv threadDelay 3_000_000 - assertEqQuery dbSyncEnv DB.queryPgIndexesCount 165 "there wasn't the correct number of indexes" + assertEqQuery dbSyncEnv DB.queryPgIndexesCount 161 "there wasn't the correct number of indexes" where testLabel = "CLAcheckForceIndexesArg" commandLineForceIndexArgs = @@ -32,7 +32,7 @@ checkNoForceIndexesArg = withCustomConfigAndDropDB commandLineNoForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do startDBSync dbSyncEnv threadDelay 3_000_000 - assertEqQuery dbSyncEnv DB.queryPgIndexesCount 100 "there wasn't the correct number of indexes" + assertEqQuery dbSyncEnv DB.queryPgIndexesCount 97 "there wasn't the correct number of indexes" where testLabel = "CLAcheckNoForceIndexesArg" commandLineNoForceIndexArgs = diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs index 6ea0d991a..1ca3d3632 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs @@ -17,7 +17,7 @@ checkForceIndexesArg = startDBSync dbSync -- Verify number of DB indexes - assertEqQuery dbSync DB.queryPgIndexesCount 165 "unexpected number of indexes" + assertEqQuery dbSync DB.queryPgIndexesCount 161 "unexpected number of indexes" where cliArgs = initCommandLineArgs {claForceIndexes = True} testLabel = "conwayCLACheckForceIndexesArg" @@ -28,7 +28,7 @@ checkNoForceIndexesArg = startDBSync dbSync -- Verify number of DB indexes - assertEqQuery dbSync DB.queryPgIndexesCount 100 "unexpected number of indexes" + assertEqQuery dbSync DB.queryPgIndexesCount 97 "unexpected number of indexes" where cliArgs = initCommandLineArgs {claForceIndexes = False} testLabel = "conwayCLACheckNoForceIndexesArg" diff --git a/schema/migration-4-0002-20200810.sql b/schema/migration-4-0002-20200810.sql index 4805612ca..e14a44bad 100644 --- a/schema/migration-4-0002-20200810.sql +++ b/schema/migration-4-0002-20200810.sql @@ -1,4 +1,3 @@ CREATE INDEX IF NOT EXISTS idx_block_time ON block(time); -CREATE INDEX IF NOT EXISTS idx_tx_out_payment_cred ON tx_out(payment_cred); CREATE INDEX IF NOT EXISTS idx_pool_update_hash_id ON pool_update(hash_id); From d798db3eb74534ced5eec651751bd75618cf1952 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Fri, 19 Jan 2024 01:51:22 +0200 Subject: [PATCH 11/17] Add changelog --- cardano-db-sync/CHANGELOG.md | 74 ++++-------------------------------- 1 file changed, 7 insertions(+), 67 deletions(-) diff --git a/cardano-db-sync/CHANGELOG.md b/cardano-db-sync/CHANGELOG.md index fa0900a22..2648e871a 100644 --- a/cardano-db-sync/CHANGELOG.md +++ b/cardano-db-sync/CHANGELOG.md @@ -1,71 +1,6 @@ # Revision history for cardano-db-sync -## Sancho changelog -`sancho-a-b-c` tags in db-sync are a series of incremental integration of conway feautures. These tags can follow the -sanchonet https://book.world.dev.cardano.org/environments.html#sanchonet-testnet. -Their enumeration has the following meaning:
-a: schema changes
-b: compatible with a respinned sanchonet or node
-c: minor changes or fixes
- -It is advised to resync from genesis when using a new version a or b. - -Some useful links (adjust the numbers to the correct tag): - -- Changelog: https://github.com/IntersectMBO/cardano-db-sync/blob/sancho-2-0-0/cardano-db-sync/CHANGELOG.md#13200 -- Schema docs https://github.com/IntersectMBO/cardano-db-sync/blob/sancho-2-0-0/doc/schema.md -- Postgres migrations https://github.com/IntersectMBO/cardano-db-sync/tree/sancho-2-0-0/schema - -In the schema docs, you can search for `13.2` or `Conway` for schema changes from the previous official release. - -### sancho-3.0.0 -- Renamed table `governance_action` to `gov_action_proposal` -- Replaced `new_committee.quorum` to `new_committee.quorum_nominator` and `new_committee.quorum_denominator` -- `always_abstain` and `always_no_confidence` `drep_hash` entries now have a null `raw` value and the unique key changed -- `governance_action.description` now uses the json format -- Added new offchain capabilites for voting anchor: the `off_chain_vote_data` and `off_chain_vote_fetch_error` are now populated -- Added a constitution table - -### sancho-2.3.0 -- is compatible with node 8.7-pre -- introduces flag `--only-gov` which allows db-sync to sync only the governance related data. -- disable swagger in smash server temporarily - -### sancho-2.2.0 -- is compatible with node 8.6-pre -- `governance_action.ratified_epoch` is now populated -- `drep_distr.active_until` is now populated -- Fixed typo `voting_procedure.comittee_voter` to `voting_procedure.committee_voter` -- Added `committee_de_registration.voting_anchor_id` field - -### sancho-2.1.0 -- Fixed an issue with the size of Committee hash - -### sancho-2.0.1 -- Workaround an issue where delegation could target unregistered pools - -### sancho-2.0.0 -- is compatible with node 8.5-pre -- adds the governance epoch param group in table `epoch_param` and their updates through governance proposal in -`param_proposal` -- adds a new field `governance_action.expiration` for the expected expiration of an action. -- populates the `drep_distr` table and `governance.enacted_epoch` that used to remain empty. -- fixes an issue with duplicated entries in `drep_hash` and `voting_anchor`. -- merges drep registration, re-registration and de-registration on the same table `drep_registration` for easy use -- the new field `drep_distr.active_until` is not populated yet. -- change `new_committee` gov actions represantation. - -### sancho-1.1.0 -- is compatible with node-8.3-pre. There are no schema changes over sancho-1-0-0. - -### sancho-1.0.0 -The schema is quite close to the [initial design](https://github.com/IntersectMBO/cardano-db-sync/blob/conway-schema-design-13.2/doc/schema.md). You may find some differences: ie the `param_proposals` and `epoch_param` are not extended yet, -Some tables/fields are created but are not populated yet: `anchor_offline_data`, `anchor_offline_fetch_error` , `drep_distr` , `governance_action.x_epoch` , `delegation_vote.redeemer_id` - -### sancho-0.0.0 -It's very early stage and is missing all Conway specific feautures and some Shelley features related to Rewards. Incremental tags will follow. - -## 13.2.0.0 - Unreleased +## 13.2.0.0 * Uses the cache for the computation of `epoch` table when following * `epoch_stake` is now computed earlier, before the epoch is reached * Exception handling and concurrency improved. @@ -78,7 +13,12 @@ It's very early stage and is missing all Conway specific feautures and some Shel * References to `offline` for pool metadata are replaced by `offchain` * Added a new table `extra_migrations` * DBSync no longer does any query to the `tx_out` table during syncing -* Added flags `--disable-in-out`, `--disable-shelley`, `--disable-gov`, `--only-gov` +* Added flags `--disable-in-out`, `--disable-shelley`, `--disable-gov`, `--only-gov`, `--keep-tx-metadata` +* Move MIR from reward to instant_reward. Done with a migration. +* Remove redundant field `address_raw` of `tx_out` and `collateral_tx_out` +* Partial Conway integration. You can read more in `doc/schema.md` (tagged as Conway). Also in files +`schema/cardano-chain-gen/schema/migration-2-0032-20230815.sql` and `schema/cardano-chain-gen/schema/migration-2-0032-20230815.sql` +for the raw sql migrations. ## 13.1.1.3 * Adds compatibility with node-8.0.0 [#1403] and node-8.1.1 [#1455] From 9801165cd010c514b21b5ab3fe0e8dcec50ef398 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 23 Jan 2024 00:45:05 +0200 Subject: [PATCH 12/17] Tidy schema files --- cardano-db/src/Cardano/Db/Schema.hs | 6 +-- ...1017.sql => migration-2-0026-20231017.sql} | 2 +- ...0713.sql => migration-2-0027-20230713.sql} | 2 +- schema/migration-2-0027-20230815.sql | 35 ------------- ...1102.sql => migration-2-0028-20240117.sql} | 5 +- schema/migration-2-0029-20231009.sql | 21 -------- ...0117.sql => migration-2-0029-20240117.sql} | 2 +- ...0108.sql => migration-2-0030-20240108.sql} | 2 +- ...0117.sql => migration-2-0031-20240117.sql} | 2 +- schema/migration-2-0032-20230815.sql | 50 +++++++++++++++++++ schema/migration-2-0032-20231107.sql | 19 ------- ...1009.sql => migration-2-0033-20231009.sql} | 16 +----- schema/migration-2-0033-20231207.sql | 32 ------------ schema/migration-2-0034-20231220.sql | 24 --------- schema/migration-2-0037-20240117.sql | 23 --------- schema/migration-3-0002-20200521.sql | 7 --- schema/migration-3-0003-20240121.sql | 7 +++ 17 files changed, 70 insertions(+), 185 deletions(-) rename schema/{migration-2-0030-20231017.sql => migration-2-0026-20231017.sql} (98%) rename schema/{migration-2-0026-20230713.sql => migration-2-0027-20230713.sql} (97%) delete mode 100644 schema/migration-2-0027-20230815.sql rename schema/{migration-2-0031-20231102.sql => migration-2-0028-20240117.sql} (70%) delete mode 100644 schema/migration-2-0029-20231009.sql rename schema/{migration-2-0038-20240117.sql => migration-2-0029-20240117.sql} (98%) rename schema/{migration-2-0035-20240108.sql => migration-2-0030-20240108.sql} (97%) rename schema/{migration-2-0036-20240117.sql => migration-2-0031-20240117.sql} (95%) create mode 100644 schema/migration-2-0032-20230815.sql delete mode 100644 schema/migration-2-0032-20231107.sql rename schema/{migration-2-0028-20231009.sql => migration-2-0033-20231009.sql} (79%) delete mode 100644 schema/migration-2-0033-20231207.sql delete mode 100644 schema/migration-2-0034-20231220.sql delete mode 100644 schema/migration-2-0037-20240117.sql create mode 100644 schema/migration-3-0003-20240121.sql diff --git a/cardano-db/src/Cardano/Db/Schema.hs b/cardano-db/src/Cardano/Db/Schema.hs index f0f47cfce..b8f0e6f30 100644 --- a/cardano-db/src/Cardano/Db/Schema.hs +++ b/cardano-db/src/Cardano/Db/Schema.hs @@ -564,13 +564,13 @@ share CommitteeRegistration txId TxId noreference certIndex Word16 - coldKey ByteString sqltype=bytea - hotKey ByteString sqltype=bytea + coldKey ByteString sqltype=hash28type + hotKey ByteString sqltype=hash28type CommitteeDeRegistration txId TxId noreference certIndex Word16 - coldKey ByteString sqltype=bytea + coldKey ByteString sqltype=hash28type votingAnchorId VotingAnchorId Maybe noreference DrepRegistration diff --git a/schema/migration-2-0030-20231017.sql b/schema/migration-2-0026-20231017.sql similarity index 98% rename from schema/migration-2-0030-20231017.sql rename to schema/migration-2-0026-20231017.sql index 3b6a1a63b..00f95837f 100644 --- a/schema/migration-2-0030-20231017.sql +++ b/schema/migration-2-0026-20231017.sql @@ -5,7 +5,7 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 30 THEN + IF next_version = 26 THEN EXECUTE 'ALTER TABLE "pool_offline_data" RENAME TO "off_chain_pool_data"' ; EXECUTE 'ALTER SEQUENCE "pool_offline_data_id_seq" RENAME TO "off_chain_pool_data_id_seq"' ; EXECUTE 'ALTER TABLE "off_chain_pool_data" RENAME CONSTRAINT "unique_pool_offline_data" TO "unique_off_chain_pool_data"' ; diff --git a/schema/migration-2-0026-20230713.sql b/schema/migration-2-0027-20230713.sql similarity index 97% rename from schema/migration-2-0026-20230713.sql rename to schema/migration-2-0027-20230713.sql index 9399b80aa..ae8c0f685 100644 --- a/schema/migration-2-0026-20230713.sql +++ b/schema/migration-2-0027-20230713.sql @@ -5,7 +5,7 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 26 THEN + IF next_version = 27 THEN EXECUTE 'CREATe TABLE "epoch_stake_progress"("id" SERIAL8 PRIMARY KEY UNIQUE,"epoch_no" word31type NOT NULL,"completed" BOOLEAN NOT NULL)' ; EXECUTE 'ALTER TABLE "epoch_stake_progress" ADD CONSTRAINT "unique_epoch_stake_progress" UNIQUE("epoch_no")' ; EXECUTE 'CREATe TABLE "extra_migrations"("id" SERIAL8 PRIMARY KEY UNIQUE,"token" VARCHAR NOT NULL,"description" VARCHAR NULL)' ; diff --git a/schema/migration-2-0027-20230815.sql b/schema/migration-2-0027-20230815.sql deleted file mode 100644 index 89f508602..000000000 --- a/schema/migration-2-0027-20230815.sql +++ /dev/null @@ -1,35 +0,0 @@ --- Persistent generated migration. - -CREATE FUNCTION migrate() RETURNS void AS $$ -DECLARE - next_version int ; -BEGIN - SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 27 THEN - EXECUTE 'ALTER TABLE "reverse_index" ALTER COLUMN "min_ids" SET NOT NULL' ; - EXECUTE 'ALTER TABLE "param_proposal" ALTER COLUMN "epoch_no" DROP NOT NULL' ; - EXECUTE 'ALTER TABLE "param_proposal" ALTER COLUMN "key" DROP NOT NULL' ; - EXECUTE 'CREATe TABLE "drep_hash"("id" SERIAL8 PRIMARY KEY UNIQUE,"raw" hash28type NULL,"view" VARCHAR NULL,"has_script" BOOLEAN NOT NULL)' ; - EXECUTE 'ALTER TABLE "drep_hash" ADD CONSTRAINT "unique_drep_hash" UNIQUE("raw","view")' ; - EXECUTE 'CREATe TABLE "delegation_vote"("id" SERIAL8 PRIMARY KEY UNIQUE,"addr_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"drep_hash_id" INT8 NOT NULL,"active_epoch_no" INT8 NOT NULL,"tx_id" INT8 NOT NULL,"redeemer_id" INT8 NULL)' ; - EXECUTE 'CREATe TABLE "committee_registration"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"cold_key" addr29type NOT NULL,"hot_key" addr29type NOT NULL)' ; - EXECUTE 'CREATe TABLE "committee_de_registration"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"hot_key" addr29type NOT NULL)' ; - EXECUTE 'CREATe TABLE "drep_registration"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"deposit" lovelace NOT NULL,"drep_hash_id" INT8 NOT NULL)' ; - EXECUTE 'CREATe TABLE "voting_anchor"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"url" varchar NOT NULL,"data_hash" BYTEA NOT NULL)' ; - EXECUTE 'CREATe TABLE "governance_action"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"index" INT8 NOT NULL,"deposit" lovelace NOT NULL,"return_address" INT8 NOT NULL,"voting_anchor_id" INT8 NULL,"type" govactiontype NOT NULL,"description" VARCHAR NOT NULL,"param_proposal" INT8 NULL,"ratified_epoch" word31type NULL,"enacted_epoch" word31type NULL,"dropped_epoch" word31type NULL,"expired_epoch" word31type NULL)' ; - EXECUTE 'CREATe TABLE "treasury_withdrawal"("id" SERIAL8 PRIMARY KEY UNIQUE,"governance_action_id" INT8 NOT NULL,"stake_address_id" INT8 NOT NULL,"amount" lovelace NOT NULL)' ; - EXECUTE 'CREATe TABLE "new_committee"("id" SERIAL8 PRIMARY KEY UNIQUE,"governance_action_id" INT8 NOT NULL,"quorum" DOUBLE PRECISION NOT NULL,"members" VARCHAR NOT NULL)' ; - EXECUTE 'CREATe TABLE "voting_procedure"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"index" INT4 NOT NULL,"governance_action_id" INT8 NOT NULL,"voter_role" voterrole NOT NULL,"committee_voter" BYTEA NULL,"drep_voter" INT8 NULL,"pool_voter" INT8 NULL,"vote" vote NOT NULL,"voting_anchor_id" INT8 NULL)' ; - EXECUTE 'CREATe TABLE "anchor_offline_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"voting_anchor_id" INT8 NOT NULL,"hash" BYTEA NOT NULL,"json" jsonb NOT NULL,"bytes" bytea NOT NULL)' ; - EXECUTE 'CREATe TABLE "anchor_offline_fetch_error"("id" SERIAL8 PRIMARY KEY UNIQUE,"voting_anchor_id" INT8 NOT NULL,"fetch_error" VARCHAR NOT NULL,"retry_count" word31type NOT NULL)' ; - EXECUTE 'CREATe TABLE "drep_distr"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash_id" INT8 NOT NULL,"amount" INT8 NOT NULL,"epoch_no" word31type NOT NULL)' ; - -- Hand written SQL statements can be added here. - UPDATE schema_version SET stage_two = next_version ; - RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; - END IF ; -END ; -$$ LANGUAGE plpgsql ; - -SELECT migrate() ; - -DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0031-20231102.sql b/schema/migration-2-0028-20240117.sql similarity index 70% rename from schema/migration-2-0031-20231102.sql rename to schema/migration-2-0028-20240117.sql index e41391a4a..8f9de651e 100644 --- a/schema/migration-2-0031-20231102.sql +++ b/schema/migration-2-0028-20240117.sql @@ -5,8 +5,9 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 31 THEN - EXECUTE 'ALTER TABLE "committee_de_registration" ADD COLUMN "voting_anchor_id" INT8 NULL' ; + IF next_version = 28 THEN + EXECUTE 'CREATe TABLE instant_reward AS TABLE reward WITH NO DATA'; + EXECUTE 'INSERT INTO instant_reward (SELECT * FROM reward WHERE pool_id IS NULL)'; -- Hand written SQL statements can be added here. UPDATE schema_version SET stage_two = next_version ; RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; diff --git a/schema/migration-2-0029-20231009.sql b/schema/migration-2-0029-20231009.sql deleted file mode 100644 index f4a546ba2..000000000 --- a/schema/migration-2-0029-20231009.sql +++ /dev/null @@ -1,21 +0,0 @@ --- Persistent generated migration. - -CREATE FUNCTION migrate() RETURNS void AS $$ -DECLARE - next_version int ; -BEGIN - SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 29 THEN - ALTER TABLE committee_registration ALTER COLUMN cold_key SET DATA TYPE bytea; - ALTER TABLE committee_registration ALTER COLUMN hot_key SET DATA TYPE bytea; - ALTER TABLE committee_de_registration ALTER COLUMN hot_key SET DATA TYPE bytea; - -- Hand written SQL statements can be added here. - UPDATE schema_version SET stage_two = next_version ; - RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; - END IF ; -END ; -$$ LANGUAGE plpgsql ; - -SELECT migrate() ; - -DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0038-20240117.sql b/schema/migration-2-0029-20240117.sql similarity index 98% rename from schema/migration-2-0038-20240117.sql rename to schema/migration-2-0029-20240117.sql index 0da91eec9..7d3766705 100644 --- a/schema/migration-2-0038-20240117.sql +++ b/schema/migration-2-0029-20240117.sql @@ -5,7 +5,7 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 38 THEN + IF next_version = 29 THEN EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "id" SET NOT NULL' ; EXECUTE 'ALTER TABLE "instant_reward" DROP COLUMN id'; EXECUTE 'ALTER TABLE "instant_reward" ALTER COLUMN "addr_id" SET NOT NULL' ; diff --git a/schema/migration-2-0035-20240108.sql b/schema/migration-2-0030-20240108.sql similarity index 97% rename from schema/migration-2-0035-20240108.sql rename to schema/migration-2-0030-20240108.sql index a92584547..30a9bafd9 100644 --- a/schema/migration-2-0035-20240108.sql +++ b/schema/migration-2-0030-20240108.sql @@ -5,7 +5,7 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 35 THEN + IF next_version = 30 THEN EXECUTE 'alter domain addr29type drop constraint addr29type_check' ; EXECUTE 'alter domain asset32type drop constraint asset32type_check' ; EXECUTE 'alter domain hash28type drop constraint hash28type_check' ; diff --git a/schema/migration-2-0036-20240117.sql b/schema/migration-2-0031-20240117.sql similarity index 95% rename from schema/migration-2-0036-20240117.sql rename to schema/migration-2-0031-20240117.sql index 90c0aeb23..8bb9f2d10 100644 --- a/schema/migration-2-0036-20240117.sql +++ b/schema/migration-2-0031-20240117.sql @@ -5,7 +5,7 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 36 THEN + IF next_version = 31 THEN EXECUTE 'ALTER TABLE "tx_out" DROP COLUMN "address_raw"' ; EXECUTE 'ALTER TABLE "collateral_tx_out" DROP COLUMN "address_raw"' ; diff --git a/schema/migration-2-0032-20230815.sql b/schema/migration-2-0032-20230815.sql new file mode 100644 index 000000000..b20232fad --- /dev/null +++ b/schema/migration-2-0032-20230815.sql @@ -0,0 +1,50 @@ +-- Persistent generated migration. + +CREATE FUNCTION migrate() RETURNS void AS $$ +DECLARE + next_version int ; +BEGIN + SELECT stage_two + 1 INTO next_version FROM schema_version ; + IF next_version = 32 THEN + EXECUTE 'ALTER TABLE "reverse_index" ALTER COLUMN "min_ids" SET NOT NULL' ; + EXECUTE 'ALTER TABLE "param_proposal" ALTER COLUMN "epoch_no" DROP NOT NULL' ; + EXECUTE 'ALTER TABLE "param_proposal" ALTER COLUMN "key" DROP NOT NULL' ; + EXECUTE 'CREATe TABLE "drep_hash"("id" SERIAL8 PRIMARY KEY UNIQUE,"raw" hash28type NULL,"view" VARCHAR NOT NULL,"has_script" BOOLEAN NOT NULL)' ; + EXECUTE 'ALTER TABLE "drep_hash" ADD CONSTRAINT "unique_drep_hash" UNIQUE("raw")' ; + EXECUTE 'CREATe TABLE "delegation_vote"("id" SERIAL8 PRIMARY KEY UNIQUE,"addr_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"drep_hash_id" INT8 NOT NULL,"tx_id" INT8 NOT NULL,"redeemer_id" INT8 NULL)' ; + EXECUTE 'CREATe TABLE "committee_registration"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"cold_key" hash28type NOT NULL,"hot_key" hash28type NOT NULL)' ; + EXECUTE 'CREATe TABLE "committee_de_registration"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"cold_key" hash28type NOT NULL)' ; + EXECUTE 'ALTER TABLE "committee_de_registration" ADD COLUMN "voting_anchor_id" INT8 NULL' ; + EXECUTE 'CREATe TABLE "drep_registration"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"deposit" INT8 NULL,"drep_hash_id" INT8 NOT NULL)' ; + EXECUTE 'ALTER TABLE "drep_registration" ADD COLUMN "voting_anchor_id" INT8 NULL' ; + EXECUTE 'CREATe TABLE "voting_anchor"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"url" varchar NOT NULL,"data_hash" BYTEA NOT NULL)' ; + EXECUTE 'ALTER TABLE "voting_anchor" ADD CONSTRAINT "unique_voting_anchor" UNIQUE("data_hash","url")' ; + EXECUTE 'CREATe TABLE "gov_action_proposal"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"index" INT8 NOT NULL,"prev_gov_action_proposal" INT8 NULL,"deposit" lovelace NOT NULL,"return_address" INT8 NOT NULL,"expiration" word31type NULL,"voting_anchor_id" INT8 NULL,"type" govactiontype NOT NULL,"description" VARCHAR NOT NULL,"param_proposal" INT8 NULL,"ratified_epoch" word31type NULL,"enacted_epoch" word31type NULL,"dropped_epoch" word31type NULL,"expired_epoch" word31type NULL)' ; + EXECUTE 'CREATe TABLE "treasury_withdrawal"("id" SERIAL8 PRIMARY KEY UNIQUE,"gov_action_proposal_id" INT8 NOT NULL,"stake_address_id" INT8 NOT NULL,"amount" lovelace NOT NULL)' ; + EXECUTE 'CREATe TABLE "new_committee"("id" SERIAL8 PRIMARY KEY UNIQUE,"gov_action_proposal_id" INT8 NOT NULL,"quorum" DOUBLE PRECISION NOT NULL,"members" VARCHAR NOT NULL)' ; + EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "deleted_members" VARCHAR NOT NULL' ; + EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "added_members" VARCHAR NOT NULL' ; + EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "members"' ; + EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_nominator" INT8 NOT NULL' ; + EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_denominator" INT8 NOT NULL' ; + EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "quorum"' ; + EXECUTE 'CREATe TABLE "voting_procedure"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"index" INT4 NOT NULL,"gov_action_proposal_id" INT8 NOT NULL,"voter_role" voterrole NOT NULL,"committee_voter" BYTEA NULL,"drep_voter" INT8 NULL,"pool_voter" INT8 NULL,"vote" vote NOT NULL,"voting_anchor_id" INT8 NULL)' ; + EXECUTE 'CREATe TABLE "drep_distr"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash_id" INT8 NOT NULL,"amount" INT8 NOT NULL,"epoch_no" word31type NOT NULL)' ; + EXECUTE 'ALTER TABLE "drep_distr" ADD COLUMN "active_until" word31type NULL' ; + EXECUTE 'ALTER TABLE "drep_distr" ADD CONSTRAINT "unique_drep_distr" UNIQUE("hash_id","epoch_no")' ; + EXECUTE 'CREATe TABLE "off_chain_vote_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"voting_anchor_id" INT8 NOT NULL,"hash" BYTEA NOT NULL,"json" jsonb NOT NULL,"bytes" bytea NOT NULL)' ; + EXECUTE 'ALTER TABLE "off_chain_vote_data" ADD CONSTRAINT "unique_off_chain_vote_data" UNIQUE("voting_anchor_id","hash")' ; + EXECUTE 'ALTER TABLE "off_chain_vote_data" ADD COLUMN "warning" VARCHAR NULL' ; + EXECUTE 'CREATe TABLE "off_chain_vote_fetch_error"("id" SERIAL8 PRIMARY KEY UNIQUE,"voting_anchor_id" INT8 NOT NULL,"fetch_error" VARCHAR NOT NULL,"fetch_time" timestamp NOT NULL,"retry_count" word31type NOT NULL)' ; + EXECUTE 'ALTER TABLE "off_chain_vote_fetch_error" ADD CONSTRAINT "unique_off_chain_vote_fetch_error" UNIQUE("voting_anchor_id","retry_count")' ; + EXECUTE 'CREATe TABLE "constitution"("id" SERIAL8 PRIMARY KEY UNIQUE,"gov_action_proposal_id" INT8 NOT NULL,"voting_anchor_id" INT8 NOT NULL,"script_hash" hash28type NULL)' ; + -- Hand written SQL statements can be added here. + UPDATE schema_version SET stage_two = next_version ; + RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; + END IF ; +END ; +$$ LANGUAGE plpgsql ; + +SELECT migrate() ; + +DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0032-20231107.sql b/schema/migration-2-0032-20231107.sql deleted file mode 100644 index b3b4698ba..000000000 --- a/schema/migration-2-0032-20231107.sql +++ /dev/null @@ -1,19 +0,0 @@ --- Persistent generated migration. - -CREATE FUNCTION migrate() RETURNS void AS $$ -DECLARE - next_version int ; -BEGIN - SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 32 THEN - EXECUTE 'ALTER TABLE "committee_de_registration" RENAME COLUMN "hot_key" TO "cold_key"' ; - -- Hand written SQL statements can be added here. - UPDATE schema_version SET stage_two = next_version ; - RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; - END IF ; -END ; -$$ LANGUAGE plpgsql ; - -SELECT migrate() ; - -DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0028-20231009.sql b/schema/migration-2-0033-20231009.sql similarity index 79% rename from schema/migration-2-0028-20231009.sql rename to schema/migration-2-0033-20231009.sql index 3487ea60f..809b30c6a 100644 --- a/schema/migration-2-0028-20231009.sql +++ b/schema/migration-2-0033-20231009.sql @@ -5,7 +5,7 @@ DECLARE next_version int ; BEGIN SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 28 THEN + IF next_version = 33 THEN EXECUTE 'ALTER TABLE "param_proposal" ADD COLUMN "pvt_motion_no_confidence" DOUBLE PRECISION NULL' ; EXECUTE 'ALTER TABLE "param_proposal" ADD COLUMN "pvt_committee_normal" DOUBLE PRECISION NULL' ; EXECUTE 'ALTER TABLE "param_proposal" ADD COLUMN "pvt_committee_no_confidence" DOUBLE PRECISION NULL' ; @@ -46,19 +46,7 @@ BEGIN EXECUTE 'ALTER TABLE "epoch_param" ADD COLUMN "gov_action_deposit" word64type NULL' ; EXECUTE 'ALTER TABLE "epoch_param" ADD COLUMN "drep_deposit" word64type NULL' ; EXECUTE 'ALTER TABLE "epoch_param" ADD COLUMN "drep_activity" word64type NULL' ; - EXECUTE 'ALTER TABLE "drep_hash" ALTER COLUMN "raw" SET NOT NULL' ; - EXECUTE 'ALTER TABLE "drep_hash" ALTER COLUMN "view" SET NOT NULL' ; - EXECUTE 'ALTER TABLE "delegation_vote" DROP COLUMN "active_epoch_no"' ; - EXECUTE 'ALTER TABLE "drep_registration" ALTER COLUMN "deposit" DROP NOT NULL' ; - EXECUTE 'ALTER TABLE "drep_registration" ALTER COLUMN "deposit" TYPE INT8' ; - EXECUTE 'ALTER TABLE "drep_registration" ADD COLUMN "voting_anchor_id" INT8 NULL' ; - EXECUTE 'ALTER TABLE "voting_anchor" ADD CONSTRAINT "unique_voting_anchor" UNIQUE("data_hash","url")' ; - EXECUTE 'ALTER TABLE "governance_action" ADD COLUMN "expiration" word31type NULL' ; - EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "deleted_members" VARCHAR NOT NULL' ; - EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "added_members" VARCHAR NOT NULL' ; - EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "members"' ; - EXECUTE 'ALTER TABLE "drep_distr" ADD COLUMN "active_until" word31type NULL' ; - EXECUTE 'ALTER TABLE "drep_distr" ADD CONSTRAINT "unique_drep_distr" UNIQUE("hash_id","epoch_no")' ; + -- Hand written SQL statements can be added here. UPDATE schema_version SET stage_two = next_version ; RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; diff --git a/schema/migration-2-0033-20231207.sql b/schema/migration-2-0033-20231207.sql deleted file mode 100644 index 8b596d39d..000000000 --- a/schema/migration-2-0033-20231207.sql +++ /dev/null @@ -1,32 +0,0 @@ --- Persistent generated migration. - -CREATE FUNCTION migrate() RETURNS void AS $$ -DECLARE - next_version int ; -BEGIN - SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 33 THEN - EXECUTE 'ALTER TABLE "drep_hash" ALTER COLUMN "raw" DROP NOT NULL' ; - EXECUTE 'ALTER TABLE "drep_hash" DROP CONSTRAINT "unique_drep_hash"' ; - EXECUTE 'ALTER TABLE "drep_hash" ADD CONSTRAINT "unique_drep_hash" UNIQUE("raw")' ; - EXECUTE 'CREATe TABLE "gov_action_proposal"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"index" INT8 NOT NULL,"prev_gov_action_proposal" INT8 NULL,"deposit" lovelace NOT NULL,"return_address" INT8 NOT NULL,"expiration" word31type NULL,"voting_anchor_id" INT8 NULL,"type" govactiontype NOT NULL,"description" VARCHAR NOT NULL,"param_proposal" INT8 NULL,"ratified_epoch" word31type NULL,"enacted_epoch" word31type NULL,"dropped_epoch" word31type NULL,"expired_epoch" word31type NULL)' ; - EXECUTE 'ALTER TABLE "treasury_withdrawal" ADD COLUMN "gov_action_proposal_id" INT8 NOT NULL' ; - EXECUTE 'ALTER TABLE "treasury_withdrawal" DROP COLUMN "governance_action_id"' ; - EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "gov_action_proposal_id" INT8 NOT NULL' ; - EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_nominator" INT8 NOT NULL' ; - EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_denominator" INT8 NOT NULL' ; - EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "governance_action_id"' ; - EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "quorum"' ; - EXECUTE 'ALTER TABLE "voting_procedure" ADD COLUMN "gov_action_proposal_id" INT8 NOT NULL' ; - EXECUTE 'ALTER TABLE "voting_procedure" DROP COLUMN "governance_action_id"' ; - EXECUTE 'DROP TABLE "governance_action"'; - -- Hand written SQL statements can be added here. - UPDATE schema_version SET stage_two = next_version ; - RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; - END IF ; -END ; -$$ LANGUAGE plpgsql ; - -SELECT migrate() ; - -DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0034-20231220.sql b/schema/migration-2-0034-20231220.sql deleted file mode 100644 index d340270c7..000000000 --- a/schema/migration-2-0034-20231220.sql +++ /dev/null @@ -1,24 +0,0 @@ --- Persistent generated migration. - -CREATE FUNCTION migrate() RETURNS void AS $$ -DECLARE - next_version int ; -BEGIN - SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 34 THEN - EXECUTE 'CREATe TABLE "off_chain_vote_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"voting_anchor_id" INT8 NOT NULL,"hash" BYTEA NOT NULL,"json" jsonb NOT NULL,"bytes" bytea NOT NULL)' ; - EXECUTE 'ALTER TABLE "off_chain_vote_data" ADD CONSTRAINT "unique_off_chain_vote_data" UNIQUE("voting_anchor_id","hash")' ; - EXECUTE 'ALTER TABLE "off_chain_vote_data" ADD COLUMN "warning" VARCHAR NULL' ; - EXECUTE 'CREATe TABLE "off_chain_vote_fetch_error"("id" SERIAL8 PRIMARY KEY UNIQUE,"voting_anchor_id" INT8 NOT NULL,"fetch_error" VARCHAR NOT NULL,"fetch_time" timestamp NOT NULL,"retry_count" word31type NOT NULL)' ; - EXECUTE 'ALTER TABLE "off_chain_vote_fetch_error" ADD CONSTRAINT "unique_off_chain_vote_fetch_error" UNIQUE("voting_anchor_id","retry_count")' ; - EXECUTE 'CREATe TABLE "constitution"("id" SERIAL8 PRIMARY KEY UNIQUE,"gov_action_proposal_id" INT8 NOT NULL,"voting_anchor_id" INT8 NOT NULL,"script_hash" hash28type NULL)' ; - -- Hand written SQL statements can be added here. - UPDATE schema_version SET stage_two = next_version ; - RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; - END IF ; -END ; -$$ LANGUAGE plpgsql ; - -SELECT migrate() ; - -DROP FUNCTION migrate() ; diff --git a/schema/migration-2-0037-20240117.sql b/schema/migration-2-0037-20240117.sql deleted file mode 100644 index 755bc70ce..000000000 --- a/schema/migration-2-0037-20240117.sql +++ /dev/null @@ -1,23 +0,0 @@ --- Persistent generated migration. - -CREATE FUNCTION migrate() RETURNS void AS $$ -DECLARE - next_version int ; -BEGIN - SELECT stage_two + 1 INTO next_version FROM schema_version ; - IF next_version = 37 THEN - EXECUTE 'CREATe TABLE instant_reward AS TABLE reward WITH NO DATA'; - EXECUTE 'INSERT INTO instant_reward (SELECT * FROM reward WHERE pool_id IS NULL)'; --- EXECUTE 'ALTER TABLE "instant_reward"("id" SERIAL8 PRIMARY KEY UNIQUE,"addr_id" INT8 NOT NULL,"type" rewardtype NOT NULL,"amount" lovelace NOT NULL,"earned_epoch" INT8 NOT NULL GENERATED ALWAYS AS ((CASE WHEN spendable_epoch >= 1 then spendable_epoch-1 else 0 end)) STORED,"spendable_epoch" INT8 NOT NULL)' ; --- EXECUTE 'ALTER TABLE "instant_reward" ADD CONSTRAINT "unique_instant_reward" UNIQUE("addr_id","earned_epoch","type")' ; - -- EXECUTE 'ALTER TABLE "reward" ALTER COLUMN "pool_id" SET NOT NULL' ; - -- Hand written SQL statements can be added here. - UPDATE schema_version SET stage_two = next_version ; - RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ; - END IF ; -END ; -$$ LANGUAGE plpgsql ; - -SELECT migrate() ; - -DROP FUNCTION migrate() ; diff --git a/schema/migration-3-0002-20200521.sql b/schema/migration-3-0002-20200521.sql index 44776d86f..5c80d425b 100644 --- a/schema/migration-3-0002-20200521.sql +++ b/schema/migration-3-0002-20200521.sql @@ -9,10 +9,3 @@ CREATE INDEX IF NOT EXISTS idx_tx_block_id ON tx(block_id); CREATE INDEX IF NOT EXISTS idx_reward_spendable_epoch ON reward(spendable_epoch); CREATE INDEX IF NOT EXISTS idx_epoch_stake_epoch_no ON epoch_stake(epoch_no) ; CREATE INDEX IF NOT EXISTS idx_pool_metadata_ref_pool_id ON pool_metadata_ref(pool_id); - --- as of 13.2.0.0 offline was renamed to off_chain so we rename previous indexes if they exists -ALTER INDEX IF EXISTS idx_pool_offline_data_pmr_id RENAME TO idx_off_chain_pool_data_pmr_id; -ALTER INDEX IF EXISTS idx_pool_offline_fetch_error_pmr_id RENAME TO idx_off_chain_pool_fetch_error_pmr_id; - -CREATE INDEX IF NOT EXISTS idx_off_chain_pool_fetch_error_pmr_id ON off_chain_pool_fetch_error (pmr_id); -CREATE INDEX IF NOT EXISTS idx_off_chain_pool_data_pmr_id ON off_chain_pool_data (pmr_id); diff --git a/schema/migration-3-0003-20240121.sql b/schema/migration-3-0003-20240121.sql new file mode 100644 index 000000000..0a48f2f93 --- /dev/null +++ b/schema/migration-3-0003-20240121.sql @@ -0,0 +1,7 @@ + +-- as of 13.2.0.0 offline was renamed to off_chain so we rename previous indexes if they exists +ALTER INDEX IF EXISTS idx_pool_offline_data_pmr_id RENAME TO idx_off_chain_pool_data_pmr_id; +ALTER INDEX IF EXISTS idx_pool_offline_fetch_error_pmr_id RENAME TO idx_off_chain_pool_fetch_error_pmr_id; + +CREATE INDEX IF NOT EXISTS idx_off_chain_pool_fetch_error_pmr_id ON off_chain_pool_fetch_error (pmr_id); +CREATE INDEX IF NOT EXISTS idx_off_chain_pool_data_pmr_id ON off_chain_pool_data (pmr_id); From 419feeef20836e65da42694b80253173c28392b2 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 23 Jan 2024 01:52:29 +0200 Subject: [PATCH 13/17] Fix typo for numerator --- cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs | 2 +- cardano-db/src/Cardano/Db/Schema.hs | 4 ++-- schema/migration-2-0032-20230815.sql | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs index 11650955b..2a92b7e35 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -1541,7 +1541,7 @@ insertGovActionProposal cache blkId txId govExpiresAt (index, pp) = do void . DB.insertNewCommittee $ DB.NewCommittee { DB.newCommitteeGovActionProposalId = gaId - , DB.newCommitteeQuorumNominator = fromIntegral $ numerator r + , DB.newCommitteeQuorumNumerator = fromIntegral $ numerator r , DB.newCommitteeQuorumDenominator = fromIntegral $ denominator r , DB.newCommitteeDeletedMembers = textShow removed , DB.newCommitteeAddedMembers = textShow added diff --git a/cardano-db/src/Cardano/Db/Schema.hs b/cardano-db/src/Cardano/Db/Schema.hs index b8f0e6f30..095953705 100644 --- a/cardano-db/src/Cardano/Db/Schema.hs +++ b/cardano-db/src/Cardano/Db/Schema.hs @@ -609,7 +609,7 @@ share NewCommittee govActionProposalId GovActionProposalId noreference - quorumNominator Word64 + quorumNumerator Word64 quorumDenominator Word64 deletedMembers Text addedMembers Text @@ -1248,7 +1248,7 @@ schemaDocs = NewCommittee --^ do "A table for new committee proposed on a GovActionProposal. New in 13.2-Conway." NewCommitteeGovActionProposalId # "The GovActionProposal table index for this new committee." - NewCommitteeQuorumNominator # "The proposed quorum nominator." + NewCommitteeQuorumNumerator # "The proposed quorum nominator." NewCommitteeQuorumDenominator # "The proposed quorum denominator." NewCommitteeDeletedMembers # "The removed members of the committee. This is now given in a text as a description, but may change. TODO: Conway." NewCommitteeAddedMembers # "The new members of the committee. This is now given in a text as a description, but may change. TODO: Conway." diff --git a/schema/migration-2-0032-20230815.sql b/schema/migration-2-0032-20230815.sql index b20232fad..7cea6fca1 100644 --- a/schema/migration-2-0032-20230815.sql +++ b/schema/migration-2-0032-20230815.sql @@ -25,7 +25,7 @@ BEGIN EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "deleted_members" VARCHAR NOT NULL' ; EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "added_members" VARCHAR NOT NULL' ; EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "members"' ; - EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_nominator" INT8 NOT NULL' ; + EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_numerator" INT8 NOT NULL' ; EXECUTE 'ALTER TABLE "new_committee" ADD COLUMN "quorum_denominator" INT8 NOT NULL' ; EXECUTE 'ALTER TABLE "new_committee" DROP COLUMN "quorum"' ; EXECUTE 'CREATe TABLE "voting_procedure"("id" SERIAL8 PRIMARY KEY UNIQUE,"tx_id" INT8 NOT NULL,"index" INT4 NOT NULL,"gov_action_proposal_id" INT8 NOT NULL,"voter_role" voterrole NOT NULL,"committee_voter" BYTEA NULL,"drep_voter" INT8 NULL,"pool_voter" INT8 NULL,"vote" vote NOT NULL,"voting_anchor_id" INT8 NULL)' ; From 2da504b8acc6e42d5bd7b6a1df35ebed35756c97 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 23 Jan 2024 01:53:00 +0200 Subject: [PATCH 14/17] Fix gov flag Fixes https://github.com/IntersectMBO/cardano-db-sync/issues/1606 --- .../src/Cardano/DbSync/Era/Shelley/Insert.hs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs index 2a92b7e35..3b82ee3cd 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -543,7 +543,7 @@ insertCertificate syncEnv isMember blkId txId epochNo slotNo redeemers (Generic. liftIO $ logWarning tracer "insertCertificate: Unhandled DCertGenesis certificate" Right (ConwayTxCertDeleg deleg) -> - when (ioShelley iopts) $ insertConwayDelegCert syncEnv txId idx mRedeemerId epochNo slotNo deleg + insertConwayDelegCert syncEnv txId idx mRedeemerId epochNo slotNo deleg Right (ConwayTxCertPool pool) -> when (ioShelley iopts) $ insertPoolCert tracer cache isMember network epochNo blkId txId idx pool Right (ConwayTxCertGov c) -> @@ -687,20 +687,28 @@ insertConwayDelegCert :: ExceptT SyncNodeError (ReaderT SqlBackend m) () insertConwayDelegCert syncEnv txId idx mRedeemerId epochNo slotNo dCert = case dCert of - ConwayRegCert cred _dep -> insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred - ConwayUnRegCert cred _dep -> insertStakeDeregistration cache network epochNo txId idx mRedeemerId cred + ConwayRegCert cred _dep -> + when (ioShelley iopts) $ + insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred + ConwayUnRegCert cred _dep -> + when (ioShelley iopts) $ + insertStakeDeregistration cache network epochNo txId idx mRedeemerId cred ConwayDelegCert cred delegatee -> insertDeleg cred delegatee ConwayRegDelegCert cred delegatee _dep -> do - insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred + when (ioShelley iopts) $ + insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred insertDeleg cred delegatee where insertDeleg cred = \case - DelegStake poolkh -> insertDelegation trce cache network epochNo slotNo txId idx mRedeemerId cred poolkh + DelegStake poolkh -> + when (ioShelley iopts) $ + insertDelegation trce cache network epochNo slotNo txId idx mRedeemerId cred poolkh DelegVote drep -> when (ioGov iopts) $ insertDelegationVote cache network txId idx cred drep DelegStakeVote poolkh drep -> do - insertDelegation trce cache network epochNo slotNo txId idx mRedeemerId cred poolkh + when (ioShelley iopts) $ + insertDelegation trce cache network epochNo slotNo txId idx mRedeemerId cred poolkh when (ioGov iopts) $ insertDelegationVote cache network txId idx cred drep From 8639b558e45d37be964f534ff682c075f9da0470 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 30 Jan 2024 19:56:45 +0200 Subject: [PATCH 15/17] Fourmolise --- cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs index 3b82ee3cd..01e99c265 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -689,14 +689,16 @@ insertConwayDelegCert syncEnv txId idx mRedeemerId epochNo slotNo dCert = case dCert of ConwayRegCert cred _dep -> when (ioShelley iopts) $ - insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred + insertStakeRegistration epochNo txId idx $ + Generic.annotateStakingCred network cred ConwayUnRegCert cred _dep -> when (ioShelley iopts) $ insertStakeDeregistration cache network epochNo txId idx mRedeemerId cred ConwayDelegCert cred delegatee -> insertDeleg cred delegatee ConwayRegDelegCert cred delegatee _dep -> do when (ioShelley iopts) $ - insertStakeRegistration epochNo txId idx $ Generic.annotateStakingCred network cred + insertStakeRegistration epochNo txId idx $ + Generic.annotateStakingCred network cred insertDeleg cred delegatee where insertDeleg cred = \case From ed617140347649c514590bac07ba639fa9ef0eca Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Fri, 2 Feb 2024 04:32:40 +0200 Subject: [PATCH 16/17] Prepare Release 13.2.0.0 --- config/mainnet-config.yaml | 2 +- doc/migrations.md | 17 +++++++++++++++++ doc/schema.md | 37 ++++++++++++++++++++++++++----------- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/config/mainnet-config.yaml b/config/mainnet-config.yaml index e8a7f566a..d52439d44 100644 --- a/config/mainnet-config.yaml +++ b/config/mainnet-config.yaml @@ -12,7 +12,7 @@ EnableLogging: True # config, it will likely lead to db-sync throwing up weird error messages from # the consensus layer. # The path to the node config file is relative to this config file. -NodeConfigFile: /nix/store/33r8vqnrlhbpk6vifd1hphphg8v6fc89-config-0-0.json +NodeConfigFile: /nix/store/8lnh50vcc13zq5av67sakyj23ybgssnh-config-0-0.json # ------------------------------------------------------------------------------ # Logging configuration follows. diff --git a/doc/migrations.md b/doc/migrations.md index ec19e8ff8..e400fe6bd 100644 --- a/doc/migrations.md +++ b/doc/migrations.md @@ -8,6 +8,23 @@ This is `a.b.c.d`. where different values. - `d`: no semantic change to the db. +## Upgrading to 13.2.0.0 + +In order to upgrade from 13.1.x.x to 13.2.0.x resyncing is not necessary and no special +flags are required from the user. DBSync will automatically perform the new migrations under `schema` +directory. Details about these migrations can be found in the release `Changelog.md`, in the `schema.md` +annotated as `13.2` or in the schema files directly. New stage 1 files starts from 1-10 and stage 2 starts from 2-26. +These migrations takes a couple minutes, mosty because the instant rewards are moved from the `reward` table to a +new `instant_reward` table. After that there is a ledger replay (read below), which takes a few hours. + +Upgrading from 13.0.x to 13.2.0.x should be possible but hasn't been tested. + +### Ledger replay + +Release 13.2.0.0 drops the ledger snaphot serialisation compatibility. This means it's not able to +parse older ledger snapshots. DBSync will delete any existing snapshot and will replay the ledger +rules from genesis. This doesn't mean a rollback to genesis. No db data are deleted. + ## Upgrading to 13.1.1.x In order to upgrade from 13.0.x or 13.1.0.x to 13.1.1.x resyncing is not necessary and no special diff --git a/doc/schema.md b/doc/schema.md index 4ad6cc9e7..25ddf2518 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -1,6 +1,8 @@ +Resolving dependencies... +Up to date # Schema Documentation for cardano-db-sync -Schema version: 13.2.0.0 (from branch **kderme/sancho-3-0-0** which may not accurately reflect the version number) +Schema version: 13.2.0.0 (from branch **kderme/optimizations** which may not accurately reflect the version number) **Note:** This file is auto-generated from the documentation in cardano-db/src/Cardano/Db/Schema.hs by the command `cabal run -- gen-schema-docs doc/schema.md`. This document should only be updated during the release process and updated on the release branch. ### `schema_version` @@ -124,7 +126,6 @@ A table for transaction outputs. | `tx_id` | integer (64) | The Tx table index of the transaction that contains this transaction output. | | `index` | txindex | The index of this transaction output with the transaction. | | `address` | string | The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era. | -| `address_raw` | blob | The raw binary address. | | `address_has_script` | boolean | Flag which shows if this address is locked by a script. | | `payment_cred` | hash28type | The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash. | | `stake_address_id` | integer (64) | The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses). | @@ -145,7 +146,6 @@ A table for transaction collateral outputs. New in v13. | `tx_id` | integer (64) | The Tx table index of the transaction that contains this transaction output. | | `index` | txindex | The index of this transaction output with the transaction. | | `address` | string | The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era. | -| `address_raw` | blob | The raw binary address. | | `address_has_script` | boolean | Flag which shows if this address is locked by a script. | | `payment_cred` | hash28type | The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash. | | `stake_address_id` | integer (64) | The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses). | @@ -383,7 +383,7 @@ A table for metadata attached to a transaction. ### `reward` -A table for earned rewards. It includes 5 types of rewards. The rewards are inserted incrementally and this procedure is finalised when the spendable epoch comes. Before the epoch comes, some entries may be missing. +A table for earned staking rewards. After 13.2 release it includes only 3 types of rewards: member, leader and refund, since the other 2 types have moved to a separate table instant_reward. The rewards are inserted incrementally and this procedure is finalised when the spendable epoch comes. Before the epoch comes, some entries may be missing. * Primary Id: `id` @@ -391,11 +391,26 @@ A table for earned rewards. It includes 5 types of rewards. The rewards are inse |-|-|-| | `id` | integer (64) | | | `addr_id` | integer (64) | The StakeAddress table index for the stake address that earned the reward. | -| `type` | rewardtype | The source of the rewards; pool `member`, pool `leader`, `treasury` or `reserves` payment and pool deposits `refunds` | +| `type` | rewardtype | The type of the rewards | | `amount` | lovelace | The reward amount (in Lovelace). | -| `earned_epoch` | integer (64) | The epoch in which the reward was earned. For `pool` and `leader` rewards spendable in epoch `N`, this will be `N - 2`, for `treasury` and `reserves` `N - 1` and for `refund` N. | +| `earned_epoch` | integer (64) | The epoch in which the reward was earned. For `pool` and `leader` rewards spendable in epoch `N`, this will be `N - 2`, `refund` N. | +| `spendable_epoch` | integer (64) | The epoch in which the reward is actually distributed and can be spent. | +| `pool_id` | integer (64) | The PoolHash table index for the pool the stake address was delegated to when the reward is earned or for the pool that there is a deposit refund. | + +### `instant_reward` + +A table for earned instant rewards. It includes only 2 types of rewards: reserves and treasury. This table only exists for historic reasons, since instant rewards are depredated after Conway. New in 13.2 + +* Primary Id: `id` + +| Column name | Type | Description | +|-|-|-| +| `id` | integer (64) | | +| `addr_id` | integer (64) | The StakeAddress table index for the stake address that earned the reward. | +| `type` | rewardtype | The type of the rewards. | +| `amount` | lovelace | The reward amount (in Lovelace). | +| `earned_epoch` | integer (64) | The epoch in which the reward was earned. For rewards spendable in epoch `N`, this will be `N - 1`. | | `spendable_epoch` | integer (64) | The epoch in which the reward is actually distributed and can be spent. | -| `pool_id` | integer (64) | The PoolHash table index for the pool the stake address was delegated to when the reward is earned or for the pool that there is a deposit refund. Will be NULL for payments from the treasury or the reserves. | ### `withdrawal` @@ -790,8 +805,8 @@ A table for every committee hot key registration. New in 13.2-Conway. | `id` | integer (64) | | | `tx_id` | integer (64) | The Tx table index of the tx that includes this certificate. | | `cert_index` | integer (32) | The index of this registration within the certificates of this transaction. | -| `cold_key` | bytea | The registered cold hey hash. TODO: should this reference DrepHashId or some separate hash table? | -| `hot_key` | bytea | The registered hot hey hash | +| `cold_key` | hash28type | The registered cold hey hash. TODO: should this reference DrepHashId or some separate hash table? | +| `hot_key` | hash28type | The registered hot hey hash | ### `committee_de_registration` @@ -804,7 +819,7 @@ A table for every committee key de-registration. New in 13.2-Conway. | `id` | integer (64) | | | `tx_id` | integer (64) | The Tx table index of the tx that includes this certificate. | | `cert_index` | integer (32) | The index of this deregistration within the certificates of this transaction. | -| `cold_key` | bytea | The deregistered cold key hash | +| `cold_key` | hash28type | The deregistered cold key hash | | `voting_anchor_id` | integer (64) | The Voting anchor reference id | ### `drep_registration` @@ -882,7 +897,7 @@ A table for new committee proposed on a GovActionProposal. New in 13.2-Conway. |-|-|-| | `id` | integer (64) | | | `gov_action_proposal_id` | integer (64) | The GovActionProposal table index for this new committee. | -| `quorum_nominator` | integer (64) | The proposed quorum nominator. | +| `quorum_numerator` | integer (64) | The proposed quorum nominator. | | `quorum_denominator` | integer (64) | The proposed quorum denominator. | | `deleted_members` | string | The removed members of the committee. This is now given in a text as a description, but may change. TODO: Conway. | | `added_members` | string | The new members of the committee. This is now given in a text as a description, but may change. TODO: Conway. | From c5d6b8a1da25fbc7f283a34b07ed75dc747131c6 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Fri, 2 Feb 2024 14:18:34 +0200 Subject: [PATCH 17/17] Restore Index --- .../Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs | 2 +- .../Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs | 2 +- schema/migration-4-0002-20200810.sql | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs index 877e6a045..720c94bfe 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs @@ -19,7 +19,7 @@ checkForceIndexesArg = withCustomConfig commandLineForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do startDBSync dbSyncEnv threadDelay 3_000_000 - assertEqQuery dbSyncEnv DB.queryPgIndexesCount 161 "there wasn't the correct number of indexes" + assertEqQuery dbSyncEnv DB.queryPgIndexesCount 162 "there wasn't the correct number of indexes" where testLabel = "CLAcheckForceIndexesArg" commandLineForceIndexArgs = diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs index 1ca3d3632..4a26169bd 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs @@ -17,7 +17,7 @@ checkForceIndexesArg = startDBSync dbSync -- Verify number of DB indexes - assertEqQuery dbSync DB.queryPgIndexesCount 161 "unexpected number of indexes" + assertEqQuery dbSync DB.queryPgIndexesCount 162 "unexpected number of indexes" where cliArgs = initCommandLineArgs {claForceIndexes = True} testLabel = "conwayCLACheckForceIndexesArg" diff --git a/schema/migration-4-0002-20200810.sql b/schema/migration-4-0002-20200810.sql index e14a44bad..4805612ca 100644 --- a/schema/migration-4-0002-20200810.sql +++ b/schema/migration-4-0002-20200810.sql @@ -1,3 +1,4 @@ CREATE INDEX IF NOT EXISTS idx_block_time ON block(time); +CREATE INDEX IF NOT EXISTS idx_tx_out_payment_cred ON tx_out(payment_cred); CREATE INDEX IF NOT EXISTS idx_pool_update_hash_id ON pool_update(hash_id);