From 35ed0f6b543c2248197fe1709e19de9685eba6b5 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Wed, 12 Jan 2022 17:26:53 +1100 Subject: [PATCH] db-sync: Fix handling of StakeRefPtr Previously `insertStakeAddressRefIfMissing` was returning `Nothing` if it received a stake address of type `StakeRefPtr`. Now it returns a proper `StakeAddressId`. Closes: https://github.com/input-output-hk/cardano-db-sync/issues/1016 --- .../src/Cardano/DbSync/Era/Shelley/Insert.hs | 6 +-- .../src/Cardano/DbSync/Era/Shelley/Query.hs | 38 +++++++++---------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs index db63a5d7e..e9c5bb775 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs @@ -462,10 +462,8 @@ insertStakeAddressRefIfMissing txId addr = case sref of Ledger.StakeRefBase cred -> Just <$> insertStakeAddress txId (Shelley.RewardAcnt nw cred) - Ledger.StakeRefPtr {} -> - -- This happens when users pay to payment addresses that refer to a stake addresses - -- by pointer, but where the pointer does not refer to a registered stake address. - pure Nothing + Ledger.StakeRefPtr ptr -> + queryStakeRefPtr ptr Ledger.StakeRefNull -> pure Nothing insertPoolOwner diff --git a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs index 133ee282b..1ec4f6ca3 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Query.hs @@ -8,6 +8,7 @@ module Cardano.DbSync.Era.Shelley.Query ( queryPoolHashId , queryStakeAddress , queryStakePoolKeyHash + , queryStakeRefPtr , queryStakeAddressRef , queryResolveInput , queryResolveInputCredentials @@ -17,7 +18,7 @@ module Cardano.DbSync.Era.Shelley.Query , queryPoolUpdateByBlock ) where -import Cardano.Prelude hiding (from, maybeToEither, on) +import Cardano.Prelude hiding (Ptr, from, maybeToEither, on) import Cardano.Db import qualified Cardano.DbSync.Era.Shelley.Generic as Generic @@ -82,26 +83,8 @@ queryStakeAddressRef addr = StakeRefBase cred -> do eres <- queryStakeAddress $ Ledger.serialiseRewardAcnt (Ledger.RewardAcnt nw cred) pure $ either (const Nothing) Just eres - StakeRefPtr (Ptr slotNo txIx certIx) -> queryStakeDelegation slotNo (fromIntegral txIx) (fromIntegral certIx) + StakeRefPtr ptr -> queryStakeRefPtr ptr StakeRefNull -> pure Nothing - where - queryStakeDelegation - :: MonadIO m - => SlotNo -> Natural -> Natural - -> ReaderT SqlBackend m (Maybe StakeAddressId) - queryStakeDelegation (SlotNo slot) txIx certIx = do - res <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` dlg) -> do - on (tx ^. TxId ==. dlg ^. DelegationTxId) - on (blk ^. BlockId ==. tx ^. TxBlockId) - where_ (blk ^. BlockSlotNo ==. just (val slot)) - where_ (tx ^. TxBlockIndex ==. val (fromIntegral txIx)) - where_ (dlg ^. DelegationCertIndex ==. val (fromIntegral certIx)) - -- Need to order by BlockSlotNo descending for correct behavior when there are two - -- or more delegation certificates in a single epoch. - orderBy [desc (blk ^. BlockSlotNo)] - limit 1 - pure (dlg ^. DelegationAddrId) - pure $ unValue <$> listToMaybe res queryResolveInput :: MonadIO m => Generic.TxIn -> ReaderT SqlBackend m (Either LookupFail (TxId, DbLovelace)) queryResolveInput txIn = @@ -121,6 +104,21 @@ queryStakeAddressIdPair cred@(Generic.StakeCred bs) = do convert :: Value StakeAddressId -> (Generic.StakeCred, StakeAddressId) convert (Value said) = (cred, said) +queryStakeRefPtr :: MonadIO m => Ptr -> ReaderT SqlBackend m (Maybe StakeAddressId) +queryStakeRefPtr (Ptr (SlotNo slot) txIx certIx) = do + res <- select . from $ \ (blk `InnerJoin` tx `InnerJoin` dlg) -> do + on (tx ^. TxId ==. dlg ^. DelegationTxId) + on (blk ^. BlockId ==. tx ^. TxBlockId) + where_ (blk ^. BlockSlotNo ==. just (val slot)) + where_ (tx ^. TxBlockIndex ==. val (fromIntegral txIx)) + where_ (dlg ^. DelegationCertIndex ==. val (fromIntegral certIx)) + -- Need to order by BlockSlotNo descending for correct behavior when there are two + -- or more delegation certificates in a single epoch. + orderBy [desc (blk ^. BlockSlotNo)] + limit 1 + pure (dlg ^. DelegationAddrId) + pure $ unValue <$> listToMaybe res + queryPoolHashIdPair :: MonadIO m => Generic.StakePoolKeyHash