Skip to content

Commit

Permalink
db-sync: Render stake address hashes as Bech32
Browse files Browse the repository at this point in the history
Closes: #224
  • Loading branch information
erikd committed Aug 10, 2020
1 parent c0b9aed commit b4edfeb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
21 changes: 11 additions & 10 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ insertDelegCert
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
insertDelegCert tracer env txId idx dCert =
case dCert of
Shelley.RegKey cred -> insertStakeRegistration tracer env txId idx cred
Shelley.RegKey cred -> insertStakeRegistration tracer txId idx $ Shelley.annotateStakingCred env cred
Shelley.DeRegKey cred -> insertStakeDeregistration tracer env txId idx cred
Shelley.Delegate (Shelley.Delegation cred poolkh) -> insertDelegation tracer env txId idx cred poolkh

Expand All @@ -225,7 +225,7 @@ insertPoolRegister tracer txId idx params = do
mdId <- case strictMaybeToMaybe $ Shelley._poolMD params of
Just md -> Just <$> insertMetaData txId md
Nothing -> pure Nothing
rewardId <- insertStakeAddress txId $ Shelley.serialiseRewardAcnt (Shelley._poolRAcnt params)
rewardId <- insertStakeAddress txId $ Shelley._poolRAcnt params

when (fromIntegral (Shelley.unCoin $ Shelley._poolPledge params) > maxLovelace) $
liftIO . logError tracer $
Expand Down Expand Up @@ -283,12 +283,13 @@ insertMetaData txId md =

insertStakeAddress
:: (MonadBaseControl IO m, MonadIO m)
=> DB.TxId -> ByteString
=> DB.TxId -> Shelley.RewardAcnt TPraosStandardCrypto
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) DB.StakeAddressId
insertStakeAddress txId stakeAddr =
insertStakeAddress txId rewardAddr =
lift . DB.insertStakeAddress $
DB.StakeAddress
{ DB.stakeAddressHash = stakeAddr
{ DB.stakeAddressHashRaw = Shelley.serialiseRewardAcnt rewardAddr
, DB.stakeAddressView = Shelley.renderRewardAcnt rewardAddr
, DB.stakeAddressRegisteredTxId = txId
}

Expand All @@ -305,10 +306,10 @@ insertPoolOwner poolId skh =

insertStakeRegistration
:: (MonadBaseControl IO m, MonadIO m)
=> Trace IO Text -> DbSyncEnv -> DB.TxId -> Word16 -> ShelleyStakingCred
=> Trace IO Text -> DB.TxId -> Word16 -> Shelley.RewardAcnt TPraosStandardCrypto
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
insertStakeRegistration _tracer env txId idx cred = do
scId <- insertStakeAddress txId $ Shelley.stakingCredHash env cred
insertStakeRegistration _tracer txId idx rewardAccount = do
scId <- insertStakeAddress txId rewardAccount
void . lift . DB.insertStakeRegistration $
DB.StakeRegistration
{ DB.stakeRegistrationAddrId = scId
Expand Down Expand Up @@ -366,7 +367,7 @@ insertMirCert _tracer env txId idx mcert = do
=> (ShelleyStakingCred, Shelley.Coin)
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
insertMirReserves (cred, coin) = do
addrId <- insertStakeAddress txId $ Shelley.stakingCredHash env cred
addrId <- insertStakeAddress txId $ Shelley.annotateStakingCred env cred
void . lift . DB.insertReserve $
DB.Reserve
{ DB.reserveAddrId = addrId
Expand All @@ -380,7 +381,7 @@ insertMirCert _tracer env txId idx mcert = do
=> (ShelleyStakingCred, Shelley.Coin)
-> ExceptT DbSyncNodeError (ReaderT SqlBackend m) ()
insertMirTreasury (cred, coin) = do
addrId <- insertStakeAddress txId $ Shelley.stakingCredHash env cred
addrId <- insertStakeAddress txId $ Shelley.annotateStakingCred env cred
void . lift . DB.insertTreasury $
DB.Treasury
{ DB.treasuryAddrId = addrId
Expand Down
2 changes: 1 addition & 1 deletion cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ queryPoolHashId hash = do
queryStakeAddress :: MonadIO m => ByteString -> ReaderT SqlBackend m (Either LookupFail StakeAddressId)
queryStakeAddress addr = do
res <- select . from $ \ saddr -> do
where_ (saddr ^. StakeAddressHash ==. val addr)
where_ (saddr ^. StakeAddressHashRaw ==. val addr)
pure (saddr ^. StakeAddressId)
pure $ maybeToEither (DbLookupMessage "StakeAddress") unValue (listToMaybe res)

Expand Down
25 changes: 16 additions & 9 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{-# LANGUAGE RankNTypes #-}

module Cardano.DbSync.Era.Shelley.Util
( blockBody
( annotateStakingCred
, blockBody
, blockHash
, blockNumber
, blockPrevHash
Expand All @@ -21,6 +22,7 @@ module Cardano.DbSync.Era.Shelley.Util
, nonceToBytes
, renderAddress
, renderHash
, renderRewardAcnt
, slotLeaderHash
, slotNumber
, stakingCredHash
Expand Down Expand Up @@ -79,6 +81,15 @@ import qualified Shelley.Spec.Ledger.PParams as Shelley
import qualified Shelley.Spec.Ledger.Tx as Shelley
import qualified Shelley.Spec.Ledger.TxData as Shelley

annotateStakingCred :: DbSyncEnv -> ShelleyStakingCred -> Shelley.RewardAcnt TPraosStandardCrypto
annotateStakingCred env cred =
let network =
case envProtocol env of
DbSyncProtocolByron -> Shelley.Mainnet -- Should not happen
DbSyncProtocolShelley -> envNetwork env
DbSyncProtocolCardano -> envNetwork env
in Shelley.RewardAcnt network cred

blockBody :: Shelley.ShelleyBlock TPraosStandardCrypto -> Shelley.BHBody TPraosStandardCrypto
blockBody = Shelley.bhbody . Shelley.bheader . Shelley.shelleyBlockRaw

Expand Down Expand Up @@ -163,6 +174,9 @@ renderAddress addr =
renderHash :: ShelleyHash -> Text
renderHash = Text.decodeUtf8 . Base16.encode . unHeaderHash

renderRewardAcnt :: Shelley.RewardAcnt TPraosStandardCrypto -> Text
renderRewardAcnt (Shelley.RewardAcnt nw cred) = Api.serialiseAddress (Api.StakeAddress nw cred)

slotLeaderHash :: Shelley.ShelleyBlock TPraosStandardCrypto -> ByteString
slotLeaderHash =
DSIGN.rawSerialiseVerKeyDSIGN . unVKey . Shelley.bheaderVk . blockBody
Expand All @@ -171,14 +185,7 @@ slotNumber :: Shelley.ShelleyBlock TPraosStandardCrypto -> Word64
slotNumber = unSlotNo . Shelley.bheaderSlotNo . blockBody

stakingCredHash :: DbSyncEnv -> ShelleyStakingCred -> ByteString
stakingCredHash env cred =
let network =
case envProtocol env of
DbSyncProtocolByron -> Shelley.Mainnet -- Should not happen
DbSyncProtocolShelley -> envNetwork env
DbSyncProtocolCardano -> envNetwork env
in Shelley.serialiseRewardAcnt $ Shelley.RewardAcnt network cred

stakingCredHash env = Shelley.serialiseRewardAcnt . annotateStakingCred env

txCertificates :: ShelleyTx -> [(Word16, ShelleyDCert)]
txCertificates tx =
Expand Down
5 changes: 3 additions & 2 deletions cardano-db/src/Cardano/Db/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ share
-- Shelley bits

StakeAddress -- Can be an address of a script hash
hash ByteString sqltype=addr29type
hashRaw ByteString sqltype=addr29type
view Text
registeredTxId TxId -- Only used for rollback.
UniqueStakeAddress hash
UniqueStakeAddress hashRaw

-- -----------------------------------------------------------------------------------------------
-- A Pool can have more than one owner, so we have a PoolOwner table that references this one.
Expand Down
4 changes: 2 additions & 2 deletions schema/migration-2-0003-20200810.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ BEGIN
EXECUTE 'ALTER TABLE "meta" DROP COLUMN "slot_duration"' ;
EXECUTE 'ALTER TABLE "meta" DROP COLUMN "slots_per_epoch"' ;
EXECUTE 'ALTER TABLE "epoch" ALTER COLUMN "out_sum" TYPE word128type' ;
EXECUTE 'CREATe TABLE "stake_address"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash" addr29type NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "stake_address" ADD CONSTRAINT "unique_stake_address" UNIQUE("hash")' ;
EXECUTE 'CREATe TABLE "stake_address"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash_raw" addr29type NOT NULL,"view" VARCHAR NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "stake_address" ADD CONSTRAINT "unique_stake_address" UNIQUE("hash_raw")' ;
EXECUTE 'ALTER TABLE "stake_address" ADD CONSTRAINT "stake_address_registered_tx_id_fkey" FOREIGN KEY("registered_tx_id") REFERENCES "tx"("id")' ;
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")' ;
Expand Down

0 comments on commit b4edfeb

Please sign in to comment.