Skip to content

Commit

Permalink
Change unique constraint of CostModel to a hash
Browse files Browse the repository at this point in the history
Using the whole CostModel caused errors because the unique key can't be that big
  • Loading branch information
kderme committed May 24, 2022
1 parent 81cea00 commit 77d5626
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
4 changes: 2 additions & 2 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cardano-db-sync/cardano-db-sync.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion cardano-db-tool/cardano-db-tool.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ library
, cardano-ledger-shelley
, cardano-prelude
, cardano-slotting
, vector-map
, containers
, contra-tracer
, esqueleto
Expand Down
4 changes: 3 additions & 1 deletion cardano-db/src/Cardano/Db/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."

Expand Down
21 changes: 21 additions & 0 deletions schema/migration-2-0016-20220524.sql
Original file line number Diff line number Diff line change
@@ -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() ;

0 comments on commit 77d5626

Please sign in to comment.