Skip to content

Commit

Permalink
db: Add a Bech32 view of the pool hash to the PoolHash table
Browse files Browse the repository at this point in the history
In this case we store both the raw hex and Bech32 encoded version.

Closes: #295
  • Loading branch information
erikd committed Oct 11, 2020
1 parent a503bb9 commit aa42fe1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
20 changes: 16 additions & 4 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Expand Up @@ -257,7 +257,7 @@ insertPoolRegister tracer (EpochNo epoch) txId idx params = do
, " > maxLovelace. See https://github.com/input-output-hk/cardano-ledger-specs/issues/1551"
]

poolHashId <- lift . DB.insertPoolHash $ DB.PoolHash (Shelley.unKeyHash $ Shelley._poolPubKey params)
poolHashId <- insertPoolHash (Shelley._poolPubKey params)
poolUpdateId <- lift . DB.insertPoolUpdate $
DB.PoolUpdate
{ DB.poolUpdateHashId = poolHashId
Expand All @@ -278,6 +278,18 @@ insertPoolRegister tracer (EpochNo epoch) txId idx params = do
maxLovelace :: Word64
maxLovelace = 45000000000000000

insertPoolHash
:: (MonadBaseControl IO m, MonadIO m)
=> Shelley.KeyHash 'Shelley.StakePool StandardShelley
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) DB.PoolHashId
insertPoolHash kh =
lift . DB.insertPoolHash $
DB.PoolHash
{ DB.poolHashHashRaw = Shelley.unKeyHashRaw kh
, DB.poolHashView = Shelley.unKeyHashView kh
}


insertPoolRetire
:: (MonadBaseControl IO m, MonadIO m)
=> DB.TxId -> EpochNo -> Word16 -> ShelleyStakePoolKeyHash
Expand Down Expand Up @@ -324,8 +336,8 @@ insertPoolOwner
insertPoolOwner poolHashId txId skh =
void . lift . DB.insertPoolOwner $
DB.PoolOwner
{ DB.poolOwnerHash = Shelley.unKeyHash skh
, DB.poolOwnerPoolHashId = poolHashId
{ DB.poolOwnerPoolHashId = poolHashId
, DB.poolOwnerHash = Shelley.unKeyHashRaw skh
, DB.poolOwnerRegisteredTxId = txId
}

Expand Down Expand Up @@ -482,7 +494,7 @@ insertParamProposal _tracer txId (Shelley.Update (Shelley.ProposedPPUpdates umap
void . lift . DB.insertParamProposal $
DB.ParamProposal
{ DB.paramProposalEpochNo = epoch
, DB.paramProposalKey = Shelley.unKeyHash key
, DB.paramProposalKey = Shelley.unKeyHashRaw key
, DB.paramProposalMinFeeA = fromIntegral <$> strictMaybeToMaybe (Shelley._minfeeA pmap)
, DB.paramProposalMinFeeB = fromIntegral <$> strictMaybeToMaybe (Shelley._minfeeB pmap)
, DB.paramProposalMaxBlockSize = fromIntegral <$> strictMaybeToMaybe (Shelley._maxBBSize pmap)
Expand Down
6 changes: 3 additions & 3 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs
Expand Up @@ -14,7 +14,7 @@ module Cardano.DbSync.Era.Shelley.Query
import qualified Cardano.Crypto.Hash as Crypto
import Cardano.Db
import Cardano.DbSync.Types
import Cardano.DbSync.Era.Shelley.Util (unKeyHash)
import Cardano.DbSync.Era.Shelley.Util (unKeyHashRaw)
import Cardano.DbSync.Util

import Control.Monad.IO.Class (MonadIO)
Expand All @@ -34,7 +34,7 @@ import qualified Shelley.Spec.Ledger.TxBody as Shelley
queryPoolHashId :: MonadIO m => ByteString -> ReaderT SqlBackend m (Maybe PoolHashId)
queryPoolHashId hash = do
res <- select . from $ \ phash -> do
where_ (phash ^. PoolHashHash ==. val hash)
where_ (phash ^. PoolHashHashRaw ==. val hash)
pure (phash ^. PoolHashId)
pure $ unValue <$> listToMaybe res

Expand Down Expand Up @@ -76,7 +76,7 @@ queryStakePoolKeyHash kh = do
on (blk ^. BlockId ==. tx ^. TxBlock)
on (tx ^. TxId ==. poolUpdate ^. PoolUpdateRegisteredTxId)
on (poolUpdate ^. PoolUpdateHashId ==. poolHash ^. PoolHashId)
where_ (poolHash ^. PoolHashHash ==. val (unKeyHash kh))
where_ (poolHash ^. PoolHashHashRaw ==. val (unKeyHashRaw kh))
orderBy [desc (blk ^. BlockSlotNo)]
pure (poolHash ^. PoolHashId)
pure $ maybeToEither (DbLookupMessage "StakePoolKeyHash") unValue (listToMaybe res)
Expand Down
10 changes: 7 additions & 3 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Util.hs
Expand Up @@ -39,7 +39,8 @@ module Cardano.DbSync.Era.Shelley.Util
, txWithdrawalSum
, unHeaderHash
, unitIntervalToDouble
, unKeyHash
, unKeyHashRaw
, unKeyHashView
, unTxHash
) where

Expand Down Expand Up @@ -232,8 +233,11 @@ unHeaderHash = Crypto.hashToBytes . Shelley.unHashHeader . Consensus.unShelleyHa
unitIntervalToDouble :: Shelley.UnitInterval -> Double
unitIntervalToDouble = fromRational . Shelley.unitIntervalToRational

unKeyHash :: Shelley.KeyHash d era -> ByteString
unKeyHash (Shelley.KeyHash kh) = Crypto.hashToBytes kh
unKeyHashRaw :: Shelley.KeyHash d era -> ByteString
unKeyHashRaw (Shelley.KeyHash kh) = Crypto.hashToBytes kh

unKeyHashView :: Shelley.KeyHash 'Shelley.StakePool StandardShelley -> Text
unKeyHashView = Api.serialiseToBech32 . Api.StakePoolKeyHash

unTxHash :: ShelleyTxId -> ByteString
unTxHash (Shelley.TxId txid) = Crypto.hashToBytes txid
Expand Down
5 changes: 3 additions & 2 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -58,8 +58,9 @@ share
stageThree Int

PoolHash
hash ByteString sqltype=hash28type
UniquePoolHash hash
hashRaw ByteString sqltype=hash28type
view Text
UniquePoolHash hashRaw

SlotLeader
hash ByteString sqltype=hash32type
Expand Down
4 changes: 2 additions & 2 deletions schema/migration-2-0003-20201011.sql
Expand Up @@ -6,8 +6,8 @@ DECLARE
BEGIN
SELECT stage_two + 1 INTO next_version FROM schema_version ;
IF next_version = 3 THEN
EXECUTE 'CREATe TABLE "pool_hash"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash" hash28type NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_hash" ADD CONSTRAINT "unique_pool_hash" UNIQUE("hash")' ;
EXECUTE 'CREATe TABLE "pool_hash"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash_raw" hash28type NOT NULL,"view" VARCHAR NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_hash" ADD CONSTRAINT "unique_pool_hash" UNIQUE("hash_raw")' ;
EXECUTE 'ALTER TABLE "slot_leader" ALTER COLUMN "hash" TYPE hash32type' ;
EXECUTE 'ALTER TABLE "slot_leader" ADD COLUMN "pool_hash_id" INT8 NULL' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "epoch_slot_no" uinteger NULL' ;
Expand Down

0 comments on commit aa42fe1

Please sign in to comment.