Skip to content

Commit 7eddf39

Browse files
committed
Add a new constitution table
1 parent 27c76b1 commit 7eddf39

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

cardano-db-sync/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ In the schema docs, you can search for `13.2` or `Conway` for schema changes fro
2424
- always_abstain and always_no_confidence drep_hash entries now have a null `raw` value and the unique key changed
2525
- governance_action.description now uses the json format
2626
- new offchain capabilites for voting anchor
27+
- add a constitution table
2728

2829
### sancho-2.3.0
2930
- is compatible with node 8.7-pre

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
367367
Generic.txMint tx
368368

369369
when (ioPlutusExtra iopts) $
370-
mapM_ (insertScript tracer txId) $
370+
mapM_ (lift . insertScript tracer txId) $
371371
Generic.txScripts tx
372372

373373
when (ioPlutusExtra iopts) $
@@ -402,7 +402,7 @@ prepareTxOut tracer cache iopts (txId, txHash) (Generic.TxOut index addr addrRaw
402402
mScriptId <-
403403
whenFalseEmpty (ioPlutusExtra iopts) Nothing $
404404
whenMaybe mScript $
405-
insertScript tracer txId
405+
lift . insertScript tracer txId
406406
let !txOut =
407407
DB.TxOut
408408
{ DB.txOutTxId = txId
@@ -441,7 +441,7 @@ insertCollateralTxOut tracer cache iopts (txId, _txHash) (Generic.TxOut index ad
441441
mScriptId <-
442442
whenFalseEmpty (ioPlutusExtra iopts) Nothing $
443443
whenMaybe mScript $
444-
insertScript tracer txId
444+
lift . insertScript tracer txId
445445
_ <-
446446
lift
447447
. DB.insertCollateralTxOut
@@ -1390,16 +1390,15 @@ insertScript ::
13901390
Trace IO Text ->
13911391
DB.TxId ->
13921392
Generic.TxScript ->
1393-
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.ScriptId
1393+
ReaderT SqlBackend m DB.ScriptId
13941394
insertScript tracer txId script = do
1395-
mScriptId <- lift $ DB.queryScript $ Generic.txScriptHash script
1395+
mScriptId <- DB.queryScript $ Generic.txScriptHash script
13961396
case mScriptId of
13971397
Just scriptId -> pure scriptId
13981398
Nothing -> do
13991399
json <- scriptConvert script
1400-
lift
1401-
. DB.insertScript
1402-
$ DB.Script
1400+
DB.insertScript $
1401+
DB.Script
14031402
{ DB.scriptTxId = txId
14041403
, DB.scriptHash = Generic.txScriptHash script
14051404
, DB.scriptType = Generic.txScriptType script
@@ -1479,7 +1478,7 @@ insertGovActionProposal cache blkId txId govExpiresAt (index, pp) = do
14791478
prevGovActionDBId <- case mprevGovAction of
14801479
Nothing -> pure Nothing
14811480
Just prevGovActionId -> Just <$> resolveGovActionProposal prevGovActionId
1482-
govActionProposal <-
1481+
govActionProposalId <-
14831482
lift $
14841483
DB.insertGovActionProposal $
14851484
DB.GovActionProposal
@@ -1499,15 +1498,17 @@ insertGovActionProposal cache blkId txId govExpiresAt (index, pp) = do
14991498
, DB.govActionProposalExpiredEpoch = Nothing
15001499
}
15011500
case pProcGovAction pp of
1502-
TreasuryWithdrawals mp -> lift $ mapM_ (insertTreasuryWithdrawal govActionProposal) (Map.toList mp)
1503-
UpdateCommittee _ removed added q -> lift $ insertNewCommittee govActionProposal removed added q
1501+
TreasuryWithdrawals mp -> lift $ mapM_ (insertTreasuryWithdrawal govActionProposalId) (Map.toList mp)
1502+
UpdateCommittee _ removed added q -> lift $ insertNewCommittee govActionProposalId removed added q
1503+
NewConstitution _ constitution -> lift $ insertConstitution txId govActionProposalId constitution
15041504
_ -> pure ()
15051505
where
15061506
mprevGovAction :: Maybe (GovActionId StandardCrypto) = case pProcGovAction pp of
15071507
ParameterChange prv _ -> unPrevGovActionId <$> strictMaybeToMaybe prv
15081508
HardForkInitiation prv _ -> unPrevGovActionId <$> strictMaybeToMaybe prv
15091509
NoConfidence prv -> unPrevGovActionId <$> strictMaybeToMaybe prv
15101510
UpdateCommittee prv _ _ _ -> unPrevGovActionId <$> strictMaybeToMaybe prv
1511+
NewConstitution prv _ -> unPrevGovActionId <$> strictMaybeToMaybe prv
15111512
_ -> Nothing
15121513

15131514
insertTreasuryWithdrawal gaId (rwdAcc, coin) = do
@@ -1541,6 +1542,16 @@ insertAnchor txId anchor =
15411542
, DB.votingAnchorDataHash = Generic.safeHashToByteString $ anchorDataHash anchor
15421543
}
15431544

1545+
insertConstitution :: (MonadIO m, MonadBaseControl IO m) => DB.TxId -> DB.GovActionProposalId -> Constitution StandardConway -> ReaderT SqlBackend m ()
1546+
insertConstitution txId gapId constitution = do
1547+
votingAnchorId <- insertAnchor txId $ constitutionAnchor constitution
1548+
void . DB.insertConstitution $
1549+
DB.Constitution
1550+
{ DB.constitutionGovActionProposalId = gapId
1551+
, DB.constitutionVotingAnchorId = votingAnchorId
1552+
, DB.constitutionScriptHash = Generic.unScriptHash <$> strictMaybeToMaybe (constitutionScript constitution)
1553+
}
1554+
15441555
insertVotingProcedures ::
15451556
(MonadIO m, MonadBaseControl IO m) =>
15461557
Trace IO Text ->

cardano-db/src/Cardano/Db/Delete.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ deleteTablesAfterTxId mtxId mtxInId mtxOutId mmaTxOutId = do
148148
whenJust mgaId $ \gaId -> do
149149
queryFirstAndDeleteAfter TreasuryWithdrawalGovActionProposalId gaId
150150
queryFirstAndDeleteAfter NewCommitteeGovActionProposalId gaId
151+
queryFirstAndDeleteAfter ConstitutionGovActionProposalId gaId
151152
deleteWhere [GovActionProposalId >=. gaId]
152153
mvaId <- queryMinRefId VotingAnchorTxId txId
153154
whenJust mvaId $ \vaId -> do

cardano-db/src/Cardano/Db/Insert.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ module Cardano.Db.Insert (
6767
setNullRatified,
6868
replaceAdaPots,
6969
insertAnchor,
70+
insertConstitution,
7071
insertGovActionProposal,
7172
insertTreasuryWithdrawal,
7273
insertNewCommittee,
@@ -377,6 +378,9 @@ replaceAdaPots blockId adapots = do
377378
insertAnchor :: (MonadBaseControl IO m, MonadIO m) => VotingAnchor -> ReaderT SqlBackend m VotingAnchorId
378379
insertAnchor = insertCheckUnique "VotingAnchor"
379380

381+
insertConstitution :: (MonadBaseControl IO m, MonadIO m) => Constitution -> ReaderT SqlBackend m ConstitutionId
382+
insertConstitution = insertUnchecked "Constitution"
383+
380384
insertGovActionProposal :: (MonadBaseControl IO m, MonadIO m) => GovActionProposal -> ReaderT SqlBackend m GovActionProposalId
381385
insertGovActionProposal = insertUnchecked "GovActionProposal"
382386

cardano-db/src/Cardano/Db/Schema.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,11 @@ share
607607
deletedMembers Text
608608
addedMembers Text
609609

610+
Constitution
611+
govActionProposalId GovActionProposalId noreference
612+
votingAnchorId VotingAnchorId noreference
613+
scriptHash ByteString Maybe sqltype=hash28type
614+
610615
VotingProcedure -- GovVote
611616
txId TxId noreference
612617
index Word16
@@ -1228,6 +1233,12 @@ schemaDocs =
12281233
NewCommitteeDeletedMembers # "The removed members of the committee. This is now given in a text as a description, but may change. TODO: Conway."
12291234
NewCommitteeAddedMembers # "The new members of the committee. This is now given in a text as a description, but may change. TODO: Conway."
12301235

1236+
Constitution --^ do
1237+
"A table for constitutiona attached to a GovActionProposal. New in 13.2-Conway."
1238+
ConstitutionGovActionProposalId # "The GovActionProposal table index for this constitution."
1239+
ConstitutionVotingAnchorId # "The ConstitutionVotingAnchor table index for this constitution."
1240+
ConstitutionScriptHash # "The Script Hash. It's associated script may not be already inserted in the script table."
1241+
12311242
VotingProcedure --^ do
12321243
"A table for voting procedures, aka GovVote. A Vote can be Yes No or Abstain. New in 13.2-Conway."
12331244
VotingProcedureTxId # "The Tx table index of the tx that includes this VotingProcedure."

schema/migration-2-0034-20231218.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Persistent generated migration.
2+
3+
CREATE FUNCTION migrate() RETURNS void AS $$
4+
DECLARE
5+
next_version int ;
6+
BEGIN
7+
SELECT stage_two + 1 INTO next_version FROM schema_version ;
8+
IF next_version = 34 THEN
9+
EXECUTE 'CREATe TABLE "constitution"("id" SERIAL8 PRIMARY KEY UNIQUE,"gov_action_proposal_id" INT8 NOT NULL,"voting_anchor_id" INT8 NOT NULL,"script_hash" hash28type NULL)' ;
10+
-- Hand written SQL statements can be added here.
11+
UPDATE schema_version SET stage_two = next_version ;
12+
RAISE NOTICE 'DB has been migrated to stage_two version %', next_version ;
13+
END IF ;
14+
END ;
15+
$$ LANGUAGE plpgsql ;
16+
17+
SELECT migrate() ;
18+
19+
DROP FUNCTION migrate() ;

0 commit comments

Comments
 (0)