Skip to content

Commit

Permalink
fix compact flag for no grand hash
Browse files Browse the repository at this point in the history
  • Loading branch information
chessai committed Aug 21, 2023
1 parent 0c680cb commit 01f0cb0
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/Chainweb/Pact/Backend/Compaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
Expand Down Expand Up @@ -320,27 +321,39 @@ tableRowCount lbl =
-- and compute+store table grand hash in CompactGrandHash.
collectTableRows :: CompactM ()
collectTableRows = do

tableRowCount "collectTableRows"

logg Info "collectTableRows:insert"
execM'
" INSERT INTO CompactActiveRow \
\ SELECT ?1,rowkey,rowid, \
\ sha3_256('T',?1,'K',rowkey,'I',txid,'D',rowdata) \
\ FROM $VTABLE$ t1 \
\ WHERE txid=(SELECT MAX(txid) FROM $VTABLE$ t2 \
\ WHERE t2.rowkey=t1.rowkey AND t2.txid<?2) \
\ GROUP BY rowkey; "
[vtable,txid]

logg Info "collectTableRows:checksum"
execM'
" INSERT INTO CompactGrandHash \
\ VALUES (?1, \
\ (SELECT sha3a_256(hash) FROM CompactActiveRow \
\ WHERE tablename=?1 ORDER BY rowkey)); "
[vtable]
doGrandHash <- not <$> isFlagSet Flag_NoGrandHash
if | doGrandHash -> do
logg Info "collectTableRows:insert"
execM'
" INSERT INTO CompactActiveRow \
\ SELECT ?1,rowkey,rowid, \
\ sha3_256('T',?1,'K',rowkey,'I',txid,'D',rowdata) \
\ FROM $VTABLE$ t1 \
\ WHERE txid=(SELECT MAX(txid) FROM $VTABLE$ t2 \
\ WHERE t2.rowkey=t1.rowkey AND t2.txid<?2) \
\ GROUP BY rowkey; "
[vtable,txid]

logg Info "collectTableRows:checksum"
execM'
" INSERT INTO CompactGrandHash \
\ VALUES (?1, \
\ (SELECT sha3a_256(hash) FROM CompactActiveRow \
\ WHERE tablename=?1 ORDER BY rowkey)); "
[vtable]
| otherwise -> do
logg Info "collectTableRows:insert"
execM'
" INSERT INTO CompactActiveRow \
\ SELECT ?1,rowkey,rowid, \
\ NULL \
\ FROM $VTABLE$ t1 \
\ WHERE txid=(SELECT MAX(txid) FROM $VTABLE$ t2 \
\ WHERE t2.rowkey=t1.rowkey AND t2.txid<?2) \
\ GROUP BY rowkey; "
[vtable,txid]

-- | Compute global grand hash from all table grand hashes.
computeGlobalHash :: CompactM ByteString
Expand Down Expand Up @@ -436,20 +449,17 @@ compact :: CompactM (Maybe ByteString)
compact = do
doGrandHash <- not <$> isFlagSet Flag_NoGrandHash

when doGrandHash $ do
withTx $ do
createCompactGrandHash
createCompactActiveRow
withTx $ do
createCompactGrandHash
createCompactActiveRow

readTxId $ collectVersionedTables $ do

gh <- if doGrandHash
then do
fmap Just $ withTx $ do
withTables collectTableRows
computeGlobalHash
else do
pure Nothing
gh <- withTx $ do
withTables collectTableRows
if doGrandHash
then Just <$> computeGlobalHash
else pure Nothing

withTx $ do
withTables $ do
Expand Down

0 comments on commit 01f0cb0

Please sign in to comment.