Skip to content

Commit

Permalink
db-sync: Accept potentially nonsensical pool_update values
Browse files Browse the repository at this point in the history
It seems the ledger accepts nonsensical values for 'pool_update.fixed_cost'
like a value greater than max Lovelace. That means we need to handle it
too 😢.

Closes: #351
  • Loading branch information
erikd committed Oct 20, 2020
1 parent 1c61b97 commit baa79f8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Expand Up @@ -15,7 +15,7 @@ module Cardano.DbSync.Era.Shelley.Insert

import Cardano.Prelude

import Cardano.BM.Trace (Trace, logDebug, logError, logInfo, logWarning)
import Cardano.BM.Trace (Trace, logDebug, logInfo, logWarning)

import Cardano.Db (DbWord64 (..))

Expand Down Expand Up @@ -198,7 +198,7 @@ insertCertificate tracer env txId (idx, cert) =
Shelley.DCertMir mir -> insertMirCert tracer env txId idx mir
Shelley.DCertGenesis _gen -> do
-- TODO : Low priority
liftIO $ logError tracer "insertCertificate: Unhandled DCertGenesis certificate"
liftIO $ logWarning tracer "insertCertificate: Unhandled DCertGenesis certificate"
pure ()


Expand Down Expand Up @@ -233,12 +233,19 @@ insertPoolRegister tracer txId idx params = do
rewardId <- insertStakeAddress txId $ Shelley._poolRAcnt params

when (fromIntegral (Shelley.unCoin $ Shelley._poolPledge params) > maxLovelace) $
liftIO . logError tracer $
liftIO . logWarning tracer $
mconcat
[ "Bad pledge amount: ", textShow (Shelley.unCoin $ Shelley._poolPledge params)
, " > maxLovelace. See https://github.com/input-output-hk/cardano-ledger-specs/issues/1551"
]

when (fromIntegral (Shelley.unCoin $ Shelley._poolCost params) > maxLovelace) $
liftIO . logWarning tracer $
mconcat
[ "Bad fixed cost amount: ", textShow (Shelley.unCoin $ Shelley._poolCost params)
, " > maxLovelace. See https://github.com/input-output-hk/cardano-db-sync/issues/351"
]

poolHashId <- lift . DB.insertPoolHash $ DB.PoolHash (Shelley.unKeyHashBS $ Shelley._poolPubKey params)
poolUpdateId <- lift . DB.insertPoolUpdate $
DB.PoolUpdate
Expand All @@ -249,7 +256,7 @@ insertPoolRegister tracer txId idx params = do
, DB.poolUpdateRewardAddrId = rewardId
, DB.poolUpdateMeta = mdId
, DB.poolUpdateMargin = realToFrac $ Shelley.intervalValue (Shelley._poolMargin params)
, DB.poolUpdateFixedCost = fromIntegral $ Shelley.unCoin (Shelley._poolCost params)
, DB.poolUpdateFixedCost = DbWord64 $ fromIntegral (Shelley.unCoin $ Shelley._poolCost params)
, DB.poolUpdateRegisteredTxId = txId
}

Expand Down
2 changes: 1 addition & 1 deletion cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -172,7 +172,7 @@ share
rewardAddrId StakeAddressId
meta PoolMetaDataId Maybe
margin Double -- sqltype=percentage????
fixedCost Word64 sqltype=lovelace
fixedCost DbWord64 sqltype=word64type
registeredTxId TxId -- Slot number in which the pool was registered.
UniquePoolUpdate hashId registeredTxId

Expand Down
Expand Up @@ -30,7 +30,7 @@ BEGIN
EXECUTE 'CREATe TABLE "pool_meta_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"url" VARCHAR NOT NULL,"hash" hash32type NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_meta_data" ADD CONSTRAINT "unique_pool_meta_data" UNIQUE("url","hash")' ;
EXECUTE 'ALTER TABLE "pool_meta_data" ADD CONSTRAINT "pool_meta_data_registered_tx_id_fkey" FOREIGN KEY("registered_tx_id") REFERENCES "tx"("id")' ;
EXECUTE 'CREATe TABLE "pool_update"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"vrf_key" hash32type NOT NULL,"pledge" word64type NOT NULL,"reward_addr_id" INT8 NOT NULL,"meta" INT8 NULL,"margin" DOUBLE PRECISION NOT NULL,"fixed_cost" lovelace NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'CREATe TABLE "pool_update"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash_id" INT8 NOT NULL,"cert_index" INT4 NOT NULL,"vrf_key" hash32type NOT NULL,"pledge" word64type NOT NULL,"reward_addr_id" INT8 NOT NULL,"meta" INT8 NULL,"margin" DOUBLE PRECISION NOT NULL,"fixed_cost" word64type NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_update" ADD CONSTRAINT "unique_pool_update" UNIQUE("hash_id","registered_tx_id")' ;
EXECUTE 'ALTER TABLE "pool_update" ADD CONSTRAINT "pool_update_hash_id_fkey" FOREIGN KEY("hash_id") REFERENCES "pool_hash"("id")' ;
EXECUTE 'ALTER TABLE "pool_update" ADD CONSTRAINT "pool_update_reward_addr_id_fkey" FOREIGN KEY("reward_addr_id") REFERENCES "stake_address"("id")' ;
Expand Down

0 comments on commit baa79f8

Please sign in to comment.