Skip to content

Commit

Permalink
Chain Index: separate scripts and redeemers tables (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
kk-hainq authored Nov 15, 2021
1 parent c30527e commit 7f7aca8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
20 changes: 17 additions & 3 deletions plutus-chain-index-core/src/Plutus/ChainIndex/DbSchema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ instance Table ScriptRowT where
data PrimaryKey ScriptRowT f = ScriptRowId (Columnar f ByteString) deriving (Generic, Beamable)
primaryKey = ScriptRowId . _scriptRowHash

data RedeemerRowT f = RedeemerRow
{ _redeemerRowHash :: Columnar f ByteString
, _redeemerRowRedeemer :: Columnar f ByteString
} deriving (Generic, Beamable)

type RedeemerRow = RedeemerRowT Identity

instance Table RedeemerRowT where
data PrimaryKey RedeemerRowT f = RedeemerRowId (Columnar f ByteString) deriving (Generic, Beamable)
primaryKey = RedeemerRowId . _redeemerRowHash

data TxRowT f = TxRow
{ _txRowTxId :: Columnar f ByteString
, _txRowTx :: Columnar f ByteString
Expand Down Expand Up @@ -152,6 +163,7 @@ instance Table UnmatchedInputRowT where
data Db f = Db
{ datumRows :: f (TableEntity DatumRowT)
, scriptRows :: f (TableEntity ScriptRowT)
, redeemerRows :: f (TableEntity RedeemerRowT)
, txRows :: f (TableEntity TxRowT)
, addressRows :: f (TableEntity AddressRowT)
, assetClassRows :: f (TableEntity AssetClassRowT)
Expand All @@ -163,6 +175,7 @@ data Db f = Db
type AllTables (c :: * -> Constraint) f =
( c (f (TableEntity DatumRowT))
, c (f (TableEntity ScriptRowT))
, c (f (TableEntity RedeemerRowT))
, c (f (TableEntity TxRowT))
, c (f (TableEntity AddressRowT))
, c (f (TableEntity AssetClassRowT))
Expand All @@ -181,6 +194,7 @@ checkedSqliteDb = defaultMigratableDbSettings
`withDbModification` dbModification
{ datumRows = renameCheckedEntity (const "datums")
, scriptRows = renameCheckedEntity (const "scripts")
, redeemerRows = renameCheckedEntity (const "redeemers")
, txRows = renameCheckedEntity (const "txs")
, addressRows = renameCheckedEntity (const "addresses")
, assetClassRows = renameCheckedEntity (const "asset_classes")
Expand Down Expand Up @@ -259,9 +273,9 @@ instance HasDbType (ScriptHash, Script) where
fromDbValue (ScriptRow hash script) = (fromDbValue hash, fromDbValue script)

instance HasDbType (RedeemerHash, Redeemer) where
type DbType (RedeemerHash, Redeemer) = ScriptRow
toDbValue (hash, script) = ScriptRow (toDbValue hash) (toDbValue script)
fromDbValue (ScriptRow hash script) = (fromDbValue hash, fromDbValue script)
type DbType (RedeemerHash, Redeemer) = RedeemerRow
toDbValue (hash, redeemer) = RedeemerRow (toDbValue hash) (toDbValue redeemer)
fromDbValue (RedeemerRow hash redeemer) = (fromDbValue hash, fromDbValue redeemer)

instance HasDbType (TxId, ChainIndexTx) where
type DbType (TxId, ChainIndexTx) = TxRow
Expand Down
16 changes: 14 additions & 2 deletions plutus-chain-index-core/src/Plutus/ChainIndex/Handlers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ handleQuery = \case
DatumFromHash dh -> getDatumFromHash dh
ValidatorFromHash hash -> getScriptFromHash hash
MintingPolicyFromHash hash -> getScriptFromHash hash
RedeemerFromHash hash -> getScriptFromHash hash
RedeemerFromHash hash -> getRedeemerFromHash hash
StakeValidatorFromHash hash -> getScriptFromHash hash
TxFromTxId txId -> getTxFromTxId txId
TxOutFromRef tor -> getTxOutFromRef tor
Expand Down Expand Up @@ -112,6 +112,16 @@ getScriptFromHash ::
-> Eff effs (Maybe o)
getScriptFromHash = queryOne . queryKeyValue scriptRows _scriptRowHash _scriptRowScript

getRedeemerFromHash ::
( Member BeamEffect effs
, HasDbType i
, DbType i ~ ByteString
, HasDbType o
, DbType o ~ ByteString
) => i
-> Eff effs (Maybe o)
getRedeemerFromHash = queryOne . queryKeyValue redeemerRows _redeemerRowHash _redeemerRowRedeemer

queryKeyValue ::
( HasDbType key
, HasSqlEqualityCheck Sqlite (DbType key)
Expand Down Expand Up @@ -283,6 +293,7 @@ handleControl = \case
combined $
[ DeleteRows $ truncateTable (datumRows db)
, DeleteRows $ truncateTable (scriptRows db)
, DeleteRows $ truncateTable (redeemerRows db)
, DeleteRows $ truncateTable (txRows db)
, DeleteRows $ truncateTable (addressRows db)
, DeleteRows $ truncateTable (assetClassRows db)
Expand Down Expand Up @@ -385,7 +396,8 @@ insert = getAp . getConst . zipTables Proxy (\tbl (InsertRows rows) -> Const $ A
fromTx :: ChainIndexTx -> Db InsertRows
fromTx tx = mempty
{ datumRows = fromMap citxData
, scriptRows = fromMap citxScripts <> fromMap citxRedeemers
, scriptRows = fromMap citxScripts
, redeemerRows = fromMap citxRedeemers
, txRows = InsertRows [toDbValue (_citxTxId tx, tx)]
, addressRows = fromPairs (fmap credential . txOutsWithRef)
, assetClassRows = fromPairs (concatMap assetClasses . txOutsWithRef)
Expand Down

0 comments on commit 7f7aca8

Please sign in to comment.