From 77d5626e892fc8ab394350899ad08415df1b4717 Mon Sep 17 00:00:00 2001 From: Kostas Dermentzis Date: Tue, 24 May 2022 07:03:03 +0300 Subject: [PATCH] Change unique constraint of CostModel to a hash Using the whole CostModel caused errors because the unique key can't be that big --- .../test/Test/Cardano/Db/Mock/Unit/Alonzo.hs | 4 +-- .../test/Test/Cardano/Db/Mock/Unit/Babbage.hs | 4 +-- cardano-db-sync/cardano-db-sync.cabal | 2 +- .../src/Cardano/DbSync/Era/Shelley/Insert.hs | 29 ++++++++++--------- cardano-db-tool/cardano-db-tool.cabal | 1 - cardano-db/src/Cardano/Db/Schema.hs | 4 ++- schema/migration-2-0016-20220524.sql | 21 ++++++++++++++ 7 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 schema/migration-2-0016-20220524.sql diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo.hs index e39796d16..3e78a8140 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo.hs @@ -261,11 +261,11 @@ rollbackFurther = -- because a checkpoint was found. let blockHash1 = hfBlockHash (blks !! 33) Right bid1 <- queryDBSync dbSync $ DB.queryBlockId blockHash1 - cm1 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "{\"1\" : 1}" bid1 + cm1 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "1" "{\"1\" : 1}" bid1 let blockHash2 = hfBlockHash (blks !! 34) Right bid2 <- queryDBSync dbSync $ DB.queryBlockId blockHash2 - cm2 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "{\"2\" : 2}" bid2 + cm2 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "2" "{\"2\" : 2}" bid2 -- Note that there is no epoch change, which would add a new entry, since we have -- 80 blocks and not 100, which is the expected blocks/epoch. This also means there diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs index 876d84922..cc3545d97 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs @@ -292,11 +292,11 @@ rollbackFurther = -- because a checkpoint was found. let blockHash1 = hfBlockHash (blks !! 33) Right bid1 <- queryDBSync dbSync $ DB.queryBlockId blockHash1 - cm1 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "{\"1\" : 1}" bid1 + cm1 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "1" "{\"1\" : 1}" bid1 let blockHash2 = hfBlockHash (blks !! 34) Right bid2 <- queryDBSync dbSync $ DB.queryBlockId blockHash2 - cm2 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "{\"2\" : 2}" bid2 + cm2 <- queryDBSync dbSync $ DB.insertCostModel $ DB.CostModel "2" "{\"2\" : 2}" bid2 -- Note that there is no epoch change, which would add a new entry, since we have -- 80 blocks and not 100, which is the expected blocks/epoch. This also means there diff --git a/cardano-db-sync/cardano-db-sync.cabal b/cardano-db-sync/cardano-db-sync.cabal index 2af9e1935..f03e2c0a0 100644 --- a/cardano-db-sync/cardano-db-sync.cabal +++ b/cardano-db-sync/cardano-db-sync.cabal @@ -208,7 +208,7 @@ executable cardano-db-sync -Wno-unsafe -threaded -rtsopts - "-with-rtsopts=-T -A16m -N3 --disable-delayed-os-memory-return" + "-with-rtsopts=-A16m -N3 --disable-delayed-os-memory-return" autogen-modules: Paths_cardano_db_sync MigrationValidations 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 f0075af17..438852ef6 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -6,6 +6,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} @@ -27,7 +28,8 @@ import Cardano.Api.Shelley (TxMetadataValue (..), makeTransactionMetad import Cardano.BM.Trace (Trace, logDebug, logInfo, logWarning) -import qualified Cardano.Crypto.Hash as Crypto +import Cardano.Crypto.Hash (hashToBytes) +import qualified Cardano.Crypto.Hashing as Crypto import Cardano.Db (DbLovelace (..), DbWord64 (..)) import qualified Cardano.Db as DB @@ -420,7 +422,7 @@ insertPoolRegister _tracer cache isMember network (EpochNo epoch) blkId txId idx DB.PoolUpdate { DB.poolUpdateHashId = poolHashId , DB.poolUpdateCertIndex = idx - , DB.poolUpdateVrfKeyHash = Crypto.hashToBytes (Shelley._poolVrf params) + , DB.poolUpdateVrfKeyHash = hashToBytes (Shelley._poolVrf params) , DB.poolUpdatePledge = Generic.coinToDbLovelace (Shelley._poolPledge params) , DB.poolUpdateRewardAddrId = saId , DB.poolUpdateActiveEpochNo = epoch + epochActivationDelay @@ -700,8 +702,8 @@ insertParamProposal :: (MonadBaseControl IO m, MonadIO m) => Trace IO Text -> DB.BlockId -> DB.TxId -> ParamProposal -> ExceptT SyncNodeError (ReaderT SqlBackend m) () -insertParamProposal tracer blkId txId pp = do - cmId <- maybe (pure Nothing) (fmap Just . insertCostModel tracer blkId) (pppCostmdls pp) +insertParamProposal _tracer blkId txId pp = do + cmId <- maybe (pure Nothing) (fmap Just . insertCostModel blkId) (pppCostmdls pp) void . lift . DB.insertParamProposal $ DB.ParamProposal { DB.paramProposalRegisteredTxId = txId @@ -829,21 +831,22 @@ insertTxMetadata tracer txId metadata = insertCostModel :: (MonadBaseControl IO m, MonadIO m) - => Trace IO Text -> DB.BlockId -> Map Language Ledger.CostModel + => DB.BlockId -> Map Language Ledger.CostModel -> ExceptT SyncNodeError (ReaderT SqlBackend m) DB.CostModelId -insertCostModel _tracer blkId cms = - lift . DB.insertCostModel $ - DB.CostModel - { DB.costModelCosts = Text.decodeUtf8 $ LBS.toStrict $ Aeson.encode cms - , DB.costModelBlockId = blkId - } +insertCostModel blkId cms = + lift . DB.insertCostModel $ + DB.CostModel + { DB.costModelHash = Crypto.abstractHashToBytes $ Crypto.serializeCborHash cms + , DB.costModelCosts = Text.decodeUtf8 $ LBS.toStrict $ Aeson.encode cms + , DB.costModelBlockId = blkId + } insertEpochParam :: (MonadBaseControl IO m, MonadIO m) => Trace IO Text -> DB.BlockId -> EpochNo -> Generic.ProtoParams -> Ledger.Nonce -> ExceptT SyncNodeError (ReaderT SqlBackend m) () -insertEpochParam tracer blkId (EpochNo epoch) params nonce = do - cmId <- maybe (pure Nothing) (fmap Just . insertCostModel tracer blkId) (Generic.ppCostmdls params) +insertEpochParam _tracer blkId (EpochNo epoch) params nonce = do + cmId <- maybe (pure Nothing) (fmap Just . insertCostModel blkId) (Generic.ppCostmdls params) void . lift . DB.insertEpochParam $ DB.EpochParam { DB.epochParamEpochNo = epoch diff --git a/cardano-db-tool/cardano-db-tool.cabal b/cardano-db-tool/cardano-db-tool.cabal index b0743866f..52c0602ce 100644 --- a/cardano-db-tool/cardano-db-tool.cabal +++ b/cardano-db-tool/cardano-db-tool.cabal @@ -69,7 +69,6 @@ library , cardano-ledger-shelley , cardano-prelude , cardano-slotting - , vector-map , containers , contra-tracer , esqueleto diff --git a/cardano-db/src/Cardano/Db/Schema.hs b/cardano-db/src/Cardano/Db/Schema.hs index 8eae1be8d..a6e8a6d07 100644 --- a/cardano-db/src/Cardano/Db/Schema.hs +++ b/cardano-db/src/Cardano/Db/Schema.hs @@ -508,9 +508,10 @@ share UniqueEpochParam epochNo blockId CostModel + hash ByteString sqltype=hash32type costs Text sqltype=jsonb blockId BlockId OnDeleteCascade - UniqueCostModel costs + UniqueCostModel hash -- ----------------------------------------------------------------------------------------------- -- Pool offline (ie not on the blockchain) data. @@ -962,6 +963,7 @@ schemaDocs = CostModel --^ do "CostModel for EpochParam and ParamProposal." + CostModelHash # "The hash of cost model. It ensures uniqueness of entries." CostModelCosts # "The actual costs formatted as json." CostModelBlockId # "The first block where these costs were introduced. This is only used for rollbacks." diff --git a/schema/migration-2-0016-20220524.sql b/schema/migration-2-0016-20220524.sql new file mode 100644 index 000000000..052f21bb6 --- /dev/null +++ b/schema/migration-2-0016-20220524.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 = 16 THEN + EXECUTE 'ALTER TABLE "cost_model" ADD COLUMN "hash" hash32type NOT NULL' ; + EXECUTE 'ALTER TABLE "cost_model" DROP CONSTRAINT "unique_cost_model"' ; + EXECUTE 'ALTER TABLE "cost_model" ADD CONSTRAINT "unique_cost_model" UNIQUE("hash")' ; + -- 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() ;