Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erikd committed Jul 6, 2020
1 parent 5fb89e8 commit f4ba279
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
Expand Up @@ -193,9 +193,10 @@ insertPoolRegister tracer txId params = do
, " > maxLovelace. See https://github.com/input-output-hk/cardano-ledger-specs/issues/1551"
]

hashId <- lift . DB.insertPoolHash $ DB.PoolHash (Shelley.unKeyHashBS $ Shelley._poolPubKey params)
poolId <- lift . DB.insertPool $
DB.Pool
{ DB.poolHash = Shelley.unKeyHashBS (Shelley._poolPubKey params)
{ DB.poolIdent = hashId
, DB.poolPledge = Shelley.unCoin $ Shelley._poolPledge params
, DB.poolRewardAddrId = rewardId
, DB.poolMeta = mdId
Expand Down
Expand Up @@ -23,7 +23,8 @@ import Data.Either (fromRight)
import Data.Maybe (listToMaybe)
import Data.Word (Word64)

import Database.Esqueleto (Value (..), (^.), (==.), from, select, val, where_)
import Database.Esqueleto (InnerJoin (..), Value (..), (^.), (==.),
from, on, select, val, where_)
import Database.Persist.Sql (SqlBackend)

import qualified Shelley.Spec.Ledger.TxData as Shelley
Expand All @@ -38,8 +39,9 @@ queryStakeAddress addr = do

queryStakePoolKeyHash :: MonadIO m => ShelleyStakePoolKeyHash -> ReaderT SqlBackend m (Either LookupFail PoolId)
queryStakePoolKeyHash kh = do
res <- select . from $ \ pool -> do
where_ (pool ^. PoolHash ==. val (unKeyHashBS kh))
res <- select . from $ \ (pool `InnerJoin` poolHash) -> do
on (pool ^. PoolIdent ==. poolHash ^. PoolHashId)
where_ (poolHash ^. PoolHashHash ==. val (unKeyHashBS kh))
pure (pool ^. PoolId)
pure $ maybeToEither (DbLookupMessage "StakePoolKeyHash") unValue (listToMaybe res)

Expand Down
4 changes: 4 additions & 0 deletions cardano-db/src/Cardano/Db/Insert.hs
Expand Up @@ -7,6 +7,7 @@ module Cardano.Db.Insert
, insertEpoch
, insertMeta
, insertPool
, insertPoolHash
, insertPoolMetaData
, insertPoolOwner
, insertPoolRelay
Expand Down Expand Up @@ -54,6 +55,9 @@ insertMeta = insertByReturnKey "Meta"
insertPool :: (MonadBaseControl IO m, MonadIO m) => Pool -> ReaderT SqlBackend m PoolId
insertPool = insertByReturnKey "Pool"

insertPoolHash :: (MonadBaseControl IO m, MonadIO m) => PoolHash -> ReaderT SqlBackend m PoolHashId
insertPoolHash = insertByReturnKey "PoolHash"

insertPoolMetaData :: (MonadBaseControl IO m, MonadIO m) => PoolMetaData -> ReaderT SqlBackend m PoolMetaDataId
insertPoolMetaData = insertByReturnKey "PoolMetaData"

Expand Down
8 changes: 6 additions & 2 deletions cardano-db/src/Cardano/Db/Schema.hs
Expand Up @@ -154,15 +154,19 @@ share
txId TxId
UniquePoolMetaData url

Pool
PoolHash
hash ByteString sqltype=hash28type
UniquePoolIdent hash

Pool
ident PoolHashId
pledge Word64 -- This really should be sqltype=lovelace See https://github.com/input-output-hk/cardano-ledger-specs/issues/1551
rewardAddrId StakeAddressId
meta PoolMetaDataId Maybe
margin Double -- sqltype=percentage????
fixedCost Word64
registeredTxId TxId -- Slot number in which the pool was registered.
UniquePool hash registeredTxId
UniquePool ident

PoolOwner
hash ByteString sqltype=hash28type
Expand Down
Expand Up @@ -16,8 +16,11 @@ BEGIN
EXECUTE 'CREATe TABLE "pool_meta_data"("id" SERIAL8 PRIMARY KEY UNIQUE,"url" VARCHAR NOT NULL,"hash" hash32type NOT NULL,"tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_meta_data" ADD CONSTRAINT "unique_pool_meta_data" UNIQUE("url")' ;
EXECUTE 'ALTER TABLE "pool_meta_data" ADD CONSTRAINT "pool_meta_data_tx_id_fkey" FOREIGN KEY("tx_id") REFERENCES "tx"("id")' ;
EXECUTE 'CREATe TABLE "pool"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash" hash28type NOT NULL,"pledge" INT8 NOT NULL,"reward_addr_id" INT8 NOT NULL,"meta" INT8 NULL,"margin" DOUBLE PRECISION NOT NULL,"fixed_cost" INT8 NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool" ADD CONSTRAINT "unique_pool" UNIQUE("hash","registered_tx_id")' ;
EXECUTE 'CREATe TABLE "pool_hash"("id" SERIAL8 PRIMARY KEY UNIQUE,"hash" hash28type NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool_hash" ADD CONSTRAINT "unique_pool_ident" UNIQUE("hash")' ;
EXECUTE 'CREATe TABLE "pool"("id" SERIAL8 PRIMARY KEY UNIQUE,"ident" INT8 NOT NULL,"pledge" INT8 NOT NULL,"reward_addr_id" INT8 NOT NULL,"meta" INT8 NULL,"margin" DOUBLE PRECISION NOT NULL,"fixed_cost" INT8 NOT NULL,"registered_tx_id" INT8 NOT NULL)' ;
EXECUTE 'ALTER TABLE "pool" ADD CONSTRAINT "unique_pool" UNIQUE("ident")' ;
EXECUTE 'ALTER TABLE "pool" ADD CONSTRAINT "pool_ident_fkey" FOREIGN KEY("ident") REFERENCES "pool_hash"("id")' ;
EXECUTE 'ALTER TABLE "pool" ADD CONSTRAINT "pool_reward_addr_id_fkey" FOREIGN KEY("reward_addr_id") REFERENCES "stake_address"("id")' ;
EXECUTE 'ALTER TABLE "pool" ADD CONSTRAINT "pool_meta_fkey" FOREIGN KEY("meta") REFERENCES "pool_meta_data"("id")' ;
EXECUTE 'ALTER TABLE "pool" ADD CONSTRAINT "pool_registered_tx_id_fkey" FOREIGN KEY("registered_tx_id") REFERENCES "tx"("id")' ;
Expand Down

0 comments on commit f4ba279

Please sign in to comment.