Skip to content

Store Bech32 encodings of two more fields #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
15 changes: 9 additions & 6 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ module Cardano.DbSync.Era.Shelley.Util
, txWithdrawalSum
, unHeaderHash
, unitIntervalToDouble
, unKeyHash
, unKeyHashRaw
, unKeyHashView
, unTxHash
) where

Expand All @@ -50,7 +51,6 @@ import qualified Cardano.Api.Typed as Api
import qualified Cardano.Crypto.Hash as Crypto
import qualified Cardano.Crypto.DSIGN as DSIGN
import qualified Cardano.Crypto.KES.Class as KES
import qualified Cardano.Crypto.VRF.Class as VRF

import qualified Cardano.Db as Db
import Cardano.DbSync.Config
Expand Down Expand Up @@ -124,8 +124,8 @@ blockTxs =
blockOpCert :: Shelley.BHBody StandardShelley -> ByteString
blockOpCert = KES.rawSerialiseVerKeyKES . Shelley.ocertVkHot . Shelley.bheaderOCert

blockVrfKey :: Shelley.BHBody StandardShelley -> ByteString
blockVrfKey = VRF.rawSerialiseVerKeyVRF . Shelley.bheaderVrfVk
blockVrfKey :: Shelley.BHBody StandardShelley -> Text
blockVrfKey = Api.serialiseToBech32 . Api.VrfVerificationKey . Shelley.bheaderVrfVk

blockVrfKeyToPoolHash :: ShelleyBlock -> ByteString
blockVrfKeyToPoolHash =
Expand Down Expand Up @@ -233,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
7 changes: 4 additions & 3 deletions cardano-db/src/Cardano/Db/Schema.hs
Original file line number Diff line number Diff line change
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 Expand Up @@ -88,7 +89,7 @@ share
time UTCTime sqltype=timestamp
txCount Word64
-- Shelley specific
vrfKey ByteString Maybe sqltype=hash32type
vrfKey Text Maybe
opCert ByteString Maybe sqltype=hash32type
protoVersion Text Maybe
UniqueBlock hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ 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' ;
EXECUTE 'ALTER TABLE "block" ALTER COLUMN "tx_count" TYPE INT8' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "vrf_key" hash32type NULL' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "vrf_key" VARCHAR NULL' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "op_cert" hash32type NULL' ;
EXECUTE 'ALTER TABLE "block" ADD COLUMN "proto_version" VARCHAR NULL' ;
EXECUTE 'ALTER TABLE "tx" ADD COLUMN "deposit" INT8 NOT NULL' ;
Expand Down