Skip to content

Commit

Permalink
db: Update the schema wrt to multi-assets
Browse files Browse the repository at this point in the history
* Add a new table `multi_asset` which includes the asset fingerprint.
* Add a reference from `ma_tx_mint` and `ma_tx_out` to `multi_asset`.
* Drop the `policy` and `name` fields of `ma_tx_mint` and `ma_tx_out`.

Closes: #868
  • Loading branch information
erikd committed Oct 7, 2021
1 parent 03e5491 commit def3db8
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 20 deletions.
20 changes: 10 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 @@ -248,7 +248,7 @@ resolveTxInputs txIn = do
insertTxOut
:: (MonadBaseControl IO m, MonadIO m)
=> Trace IO Text -> DB.TxId -> Generic.TxOut
-> ExceptT e (ReaderT SqlBackend m) ()
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertTxOut tracer txId (Generic.TxOut index addr addrRaw value maMap dataHash) = do
mSaId <- lift $ insertStakeAddressRefIfMissing txId addr
txOutId <- lift . DB.insertTxOut $
Expand Down Expand Up @@ -834,38 +834,38 @@ insertMaTxMint _tracer txId (Value _adaShouldAlwaysBeZeroButWeDoNotCheck mintMap
:: (MonadBaseControl IO m, MonadIO m)
=> PolicyID StandardCrypto -> (AssetName, Integer)
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertInner policy (aname, amount) =
insertInner policy (aname, amount) = do
maId <- liftLookupFail "insertMaTxMint" $ DB.queryMultiAssetId (Generic.unScriptHash $ policyID policy) (assetName aname)
void . lift . DB.insertMaTxMint $
DB.MaTxMint
{ DB.maTxMintPolicy = Generic.unScriptHash (policyID policy)
, DB.maTxMintName = assetName aname
{ DB.maTxMintIdent = maId
, DB.maTxMintQuantity = DB.integerToDbInt65 amount
, DB.maTxMintTxId = txId
}

insertMaTxOut
:: (MonadBaseControl IO m, MonadIO m)
=> Trace IO Text -> DB.TxOutId -> Map (PolicyID StandardCrypto) (Map AssetName Integer)
-> ExceptT e (ReaderT SqlBackend m) ()
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertMaTxOut _tracer txOutId maMap =
mapM_ insertOuter $ Map.toList maMap
where
insertOuter
:: (MonadBaseControl IO m, MonadIO m)
=> (PolicyID StandardCrypto, Map AssetName Integer)
-> ExceptT e (ReaderT SqlBackend m) ()
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertOuter (policy, aMap) =
mapM_ (insertInner policy) $ Map.toList aMap

insertInner
:: (MonadBaseControl IO m, MonadIO m)
=> PolicyID StandardCrypto -> (AssetName, Integer)
-> ExceptT e (ReaderT SqlBackend m) ()
insertInner policy (aname, amount) =
-> ExceptT SyncNodeError (ReaderT SqlBackend m) ()
insertInner policy (aname, amount) = do
maId <- liftLookupFail "insertMaTxOut" $ DB.queryMultiAssetId (Generic.unScriptHash $ policyID policy) (assetName aname)
void . lift . DB.insertMaTxOut $
DB.MaTxOut
{ DB.maTxOutPolicy = Generic.unScriptHash (policyID policy)
, DB.maTxOutName = assetName aname
{ DB.maTxOutIdent = maId
, DB.maTxOutQuantity = DbWord64 (fromIntegral amount)
, DB.maTxOutTxOutId = txOutId
}
Expand Down
4 changes: 3 additions & 1 deletion cardano-db/cardano-db.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ library
Cardano.Db.Text
Cardano.Db.Types


build-depends: aeson
, base >= 4.14 && < 4.16
, bech32
, base16-bytestring
, bytestring
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-ledger-shelley-ma
, cardano-slotting
, containers
, conduit
Expand All @@ -73,6 +74,7 @@ library
, memory
, monad-control
, monad-logger
, ouroboros-consensus-shelley
, ouroboros-network
, persistent
, persistent-documentation
Expand Down
4 changes: 4 additions & 0 deletions cardano-db/src/Cardano/Db/Insert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Cardano.Db.Insert
, insertMaTxMint
, insertMaTxOut
, insertMeta
, insertMultiAsset
, insertParamProposal
, insertPotTransfer
, insertPoolHash
Expand Down Expand Up @@ -143,6 +144,9 @@ insertMaTxOut = insertCheckUnique "insertMaTxOut"
insertMeta :: (MonadBaseControl IO m, MonadIO m) => Meta -> ReaderT SqlBackend m MetaId
insertMeta = insertCheckUnique "Meta"

insertMultiAsset :: (MonadBaseControl IO m, MonadIO m) => MultiAsset -> ReaderT SqlBackend m MultiAssetId
insertMultiAsset = insertCheckUnique "MultiAsset"

insertParamProposal :: (MonadBaseControl IO m, MonadIO m) => ParamProposal -> ReaderT SqlBackend m ParamProposalId
insertParamProposal = insertUnchecked "ParamProposal"

Expand Down
8 changes: 8 additions & 0 deletions cardano-db/src/Cardano/Db/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Cardano.Db.Query
, queryLatestBlockNo
, queryLatestSlotNo
, queryMeta
, queryMultiAssetId
, queryNetworkName
, queryPreviousSlotNo
, querySchemaVersion
Expand Down Expand Up @@ -455,6 +456,13 @@ queryMeta = do
[m] -> Right $ entityVal m
_ -> Left DbMetaMultipleRows

queryMultiAssetId :: MonadIO m => ByteString -> ByteString -> ReaderT SqlBackend m (Either LookupFail MultiAssetId)
queryMultiAssetId policy assetName = do
res <- select . from $ \ ma -> do
where_ (ma ^. MultiAssetPolicy ==. val policy &&. ma ^. MultiAssetName ==. val assetName)
pure (ma ^. MultiAssetId)
pure $ maybe (Left $ DbLookupMessage "MultiAssetId") (Right . unValue) (listToMaybe res)

-- | Get the network name from the Meta table.
queryNetworkName :: MonadIO m => ReaderT SqlBackend m (Maybe Text)
queryNetworkName = do
Expand Down
26 changes: 17 additions & 9 deletions cardano-db/src/Cardano/Db/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,23 @@ share
-- -----------------------------------------------------------------------------------------------
-- Multi Asset related tables.

MaTxMint
MultiAsset
policy ByteString sqltype=hash28type
name ByteString sqltype=asset32type
fingerprint Text
UniqueMultiAsset policy name

MaTxMint
ident MultiAssetId
quantity DbInt65 sqltype=int65type
txId TxId OnDeleteCascade
UniqueMaTxMint policy name txId
UniqueMaTxMint ident txId

MaTxOut
policy ByteString sqltype=hash28type
name ByteString sqltype=asset32type
ident MultiAssetId
quantity DbWord64 sqltype=word64type
txOutId TxOutId OnDeleteCascade
UniqueMaTxOut policy name txOutId
UniqueMaTxOut ident txOutId

-- -----------------------------------------------------------------------------------------------
-- Scripts related tables.
Expand Down Expand Up @@ -767,17 +771,21 @@ schemaDocs =
\ that was already partially synced when `db-sync` was started)."
EpochSyncTimeState # "The sync state when the sync time is recorded (either 'lagging' or 'following')."

MultiAsset --^ do
"A table containing all the unique policy/name pairs along with a CIP14 asset fingerprint"
MultiAssetPolicy # "The MultiAsset policy hash."
MultiAssetName # "The MultiAsset name."
MultiAssetFingerprint # "The CIP14 fingerprint for the MultiAsset."

MaTxMint --^ do
"A table containing Multi-Asset mint events."
MaTxMintPolicy # "The MultiAsset policy hash."
MaTxMintName # "The MultiAsset name."
MaTxMintIdent # "The MultiAsset table index specifying the asset."
MaTxMintQuantity # "The amount of the Multi Asset to mint (can be negative to \"burn\" assets)."
MaTxMintTxId # "The Tx table index for the transaction that contains this minting event."

MaTxOut --^ do
"A table containing Multi-Asset transaction outputs."
MaTxOutPolicy # "The MultiAsset policy hash."
MaTxOutName # "The MultiAsset name."
MaTxOutIdent # "The MultiAsset table index specifying the asset."
MaTxOutQuantity # "The Multi Asset transaction output amount (denominated in the Multi Asset)."
MaTxOutTxOutId # "The TxOut table index for the transaction that this Multi Asset transaction output."

Expand Down
30 changes: 30 additions & 0 deletions schema/migration-2-0002-20211007.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Persistent generated migration.

CREATE FUNCTION migrate() RETURNS void AS $$
DECLARE
next_version int ;
BEGIN
SELECT stage_two + 1 INTO next_version FROM schema_version ;
IF next_version = 2 THEN
EXECUTE 'CREATe TABLE "multi_asset"("id" SERIAL8 PRIMARY KEY UNIQUE,"policy" hash28type NOT NULL,"name" asset32type NOT NULL,"fingerprint" VARCHAR NOT NULL)' ;
EXECUTE 'ALTER TABLE "multi_asset" ADD CONSTRAINT "unique_multi_asset" UNIQUE("policy","name")' ;
EXECUTE 'ALTER TABLE "ma_tx_mint" ADD COLUMN "ident" INT8 NOT NULL' ;
EXECUTE 'ALTER TABLE "ma_tx_mint" DROP COLUMN "policy"' ;
EXECUTE 'ALTER TABLE "ma_tx_mint" DROP COLUMN "name"' ;
EXECUTE 'ALTER TABLE "ma_tx_mint" DROP CONSTRAINT IF EXISTS "unique_ma_tx_mint"' ;
EXECUTE 'ALTER TABLE "ma_tx_mint" ADD CONSTRAINT "unique_ma_tx_mint" UNIQUE("ident","tx_id")' ;
EXECUTE 'ALTER TABLE "ma_tx_out" ADD COLUMN "ident" INT8 NOT NULL' ;
EXECUTE 'ALTER TABLE "ma_tx_out" DROP COLUMN "policy"' ;
EXECUTE 'ALTER TABLE "ma_tx_out" DROP COLUMN "name"' ;
EXECUTE 'ALTER TABLE "ma_tx_out" DROP CONSTRAINT IF EXISTS "unique_ma_tx_out"' ;
EXECUTE 'ALTER TABLE "ma_tx_out" ADD CONSTRAINT "unique_ma_tx_out" UNIQUE("ident","tx_out_id")' ;
-- Hand written SQL statements can be added here.
UPDATE schema_version SET stage_two = next_version ;
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;
END IF ;
END ;
$$ LANGUAGE plpgsql ;

SELECT migrate() ;

DROP FUNCTION migrate() ;

0 comments on commit def3db8

Please sign in to comment.