Skip to content

Commit

Permalink
PoolOwner now references PoolUpdate, instead of PoolHash and Tx
Browse files Browse the repository at this point in the history
Fixes #986
  • Loading branch information
kderme committed Jan 26, 2022
1 parent 1b08087 commit dcc1d2b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit.hs
Expand Up @@ -1220,7 +1220,7 @@ poolDeRegMany =

assertBlockNoBackoff dbSync 2
-- TODO fix PoolOwner and PoolRelay unique key
assertPoolCounters dbSync (addPoolCounters (1,1,5,7,3,5) initCounter)
assertPoolCounters dbSync (addPoolCounters (1,1,5,10,3,5) initCounter)

st <- getAlonzoLedgerState interpreter
-- Not retired yet, because epoch has not changed
Expand All @@ -1231,7 +1231,7 @@ poolDeRegMany =

assertBlockNoBackoff dbSync (fromIntegral $ length a + 2)
-- these counters are the same
assertPoolCounters dbSync (addPoolCounters (1,1,5,7,3,5) initCounter)
assertPoolCounters dbSync (addPoolCounters (1,1,5,10,3,5) initCounter)

-- from all these certificates only the latest matters. So it will retire
-- on epoch 0
Expand Down
9 changes: 4 additions & 5 deletions cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs
Expand Up @@ -373,7 +373,7 @@ insertPoolRegister _tracer mlStateSnap network (EpochNo epoch) blkId txId idx pa
, DB.poolUpdateRegisteredTxId = txId
}

mapM_ (insertPoolOwner network poolHashId txId) $ toList (Shelley._poolOwners params)
mapM_ (insertPoolOwner network poolUpdateId txId) $ toList (Shelley._poolOwners params)
mapM_ (insertPoolRelay poolUpdateId) $ toList (Shelley._poolRelays params)

where
Expand Down Expand Up @@ -476,15 +476,14 @@ insertStakeAddressRefIfMissing trce txId addr =

insertPoolOwner
:: (MonadBaseControl IO m, MonadIO m)
=> Ledger.Network -> DB.PoolHashId -> DB.TxId -> Ledger.KeyHash 'Ledger.Staking StandardCrypto
=> Ledger.Network -> DB.PoolUpdateId -> DB.TxId -> Ledger.KeyHash 'Ledger.Staking StandardCrypto
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertPoolOwner network poolHashId txId skh = do
insertPoolOwner network poolUpdateId txId skh = do
saId <- lift $ insertStakeAddress txId (Shelley.RewardAcnt network (Ledger.KeyHashObj skh))
void . lift . DB.insertPoolOwner $
DB.PoolOwner
{ DB.poolOwnerAddrId = saId
, DB.poolOwnerPoolHashId = poolHashId
, DB.poolOwnerRegisteredTxId = txId
, DB.poolOwnerPoolUpdateId = poolUpdateId
}

insertStakeRegistration
Expand Down
4 changes: 2 additions & 2 deletions cardano-db-tool/src/Cardano/DbTool/Validate/PoolOwner.hs
Expand Up @@ -30,8 +30,8 @@ validateAllPoolsHaveOwners = do

queryPoolsWithoutOwners :: MonadIO m => ReaderT SqlBackend m Int
queryPoolsWithoutOwners = do
res <- select . from $ \ phash -> do
res <- select . from $ \ pupd -> do
where_ . notExists . from $ \ powner -> do
where_ (phash ^. PoolHashId ==. powner ^. PoolOwnerPoolHashId)
where_ (pupd ^. PoolUpdateId ==. powner ^. PoolOwnerPoolUpdateId)
pure countRows
pure $ maybe 0 unValue (listToMaybe res)
10 changes: 4 additions & 6 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -197,7 +197,6 @@ share
deriving Eq

-- -----------------------------------------------------------------------------------------------
-- A Pool can have more than one owner, so we have a PoolOwner table that references this one.

PoolMetadataRef
poolId PoolHashId
Expand All @@ -219,11 +218,11 @@ share
registeredTxId TxId OnDeleteCascade -- Slot number in which the pool was registered.
UniquePoolUpdate registeredTxId certIndex

-- A Pool can have more than one owner, so we have a PoolOwner table.
PoolOwner
addrId StakeAddressId OnDeleteCascade
poolHashId PoolHashId OnDeleteCascade
registeredTxId TxId OnDeleteCascade -- Slot number in which the owner was registered.
UniquePoolOwner addrId poolHashId registeredTxId
poolUpdateId PoolUpdateId OnDeleteCascade
UniquePoolOwner addrId poolUpdateId

PoolRetire
hashId PoolHashId OnDeleteCascade
Expand Down Expand Up @@ -673,8 +672,7 @@ schemaDocs =
PoolOwner --^ do
"A table containing pool owners."
PoolOwnerAddrId # "The StakeAddress table index for the pool owner's stake address."
PoolOwnerPoolHashId # "The PoolHash table index for the pool."
PoolOwnerRegisteredTxId # "The Tx table index of the transaction where this pool owner was registered."
PoolOwnerPoolUpdateId # "The PoolUpdate table index for the pool."

PoolRetire --^ do
"A table containing information about pools retiring."
Expand Down
1 change: 0 additions & 1 deletion schema/migration-2-0001-20211003.sql
Expand Up @@ -59,7 +59,6 @@ BEGIN
EXECUTE 'ALTER TABLE "pool_update" ADD CONSTRAINT "pool_update_meta_id_fkey" FOREIGN KEY("meta_id") REFERENCES "pool_metadata_ref"("id") ON DELETE CASCADE ON UPDATE RESTRICT' ;
EXECUTE 'ALTER TABLE "pool_update" ADD CONSTRAINT "pool_update_registered_tx_id_fkey" FOREIGN KEY("registered_tx_id") REFERENCES "tx"("id") ON DELETE CASCADE ON UPDATE RESTRICT' ;
EXECUTE 'CREATe TABLE "pool_owner"("id" SERIAL8 PRIMARY KEY UNIQUE,"addr_id" INT8 NOT NULL,"pool_hash_id" INT8 NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_owner" ADD CONSTRAINT "unique_pool_owner" UNIQUE("addr_id","pool_hash_id","registered_tx_id")' ;
EXECUTE 'ALTER TABLE "pool_owner" ADD CONSTRAINT "pool_owner_addr_id_fkey" FOREIGN KEY("addr_id") REFERENCES "stake_address"("id") ON DELETE CASCADE ON UPDATE RESTRICT' ;
EXECUTE 'ALTER TABLE "pool_owner" ADD CONSTRAINT "pool_owner_pool_hash_id_fkey" FOREIGN KEY("pool_hash_id") REFERENCES "pool_hash"("id") ON DELETE CASCADE ON UPDATE RESTRICT' ;
EXECUTE 'ALTER TABLE "pool_owner" ADD CONSTRAINT "pool_owner_registered_tx_id_fkey" FOREIGN KEY("registered_tx_id") REFERENCES "tx"("id") ON DELETE CASCADE ON UPDATE RESTRICT' ;
Expand Down
2 changes: 0 additions & 2 deletions schema/migration-3-0005-20210116.sql
Expand Up @@ -10,7 +10,6 @@ BEGIN
-- Some of them are intentional commented out, because they already exist.
CREATE INDEX idx_delegation_pool_hash_id ON delegation(pool_hash_id) ;
CREATE INDEX idx_epoch_stake_pool_id ON epoch_stake(pool_id) ;
CREATE INDEX idx_pool_owner_pool_hash_id ON pool_owner(pool_hash_id) ;
CREATE INDEX idx_pool_retire_hash_id ON pool_retire(hash_id) ;
-- CREATE INDEX idx_pool_update_hash_id ON pool_update(hash_id) ;
CREATE INDEX idx_reward_pool_id ON reward(pool_id) ;
Expand All @@ -25,7 +24,6 @@ BEGIN
CREATE INDEX idx_ma_tx_mint_tx_id ON ma_tx_mint(tx_id) ;
CREATE INDEX idx_param_proposal_registered_tx_id ON param_proposal(registered_tx_id) ;
CREATE INDEX idx_pool_metadata_ref_registered_tx_id ON pool_metadata_ref(registered_tx_id) ;
CREATE INDEX idx_pool_owner_registered_tx_id ON pool_owner(registered_tx_id) ;
CREATE INDEX idx_pool_retire_announced_tx_id ON pool_retire(announced_tx_id) ;
CREATE INDEX idx_pool_update_registered_tx_id ON pool_update(registered_tx_id) ;
CREATE INDEX idx_reserve_tx_id ON reserve(tx_id) ;
Expand Down

0 comments on commit dcc1d2b

Please sign in to comment.