Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Akentev committed Aug 23, 2023
1 parent 0dc79b0 commit 3419a31
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 37 deletions.
4 changes: 2 additions & 2 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ custom-setup
-- -------------------------------------------------------------------------- --

library
import: warning-flags, debugging-flags
import: debugging-flags
default-language: Haskell2010
hs-source-dirs: src
other-modules:
Expand Down Expand Up @@ -433,7 +433,7 @@ library
-- -------------------------------------------------------------------------- --

test-suite chainweb-tests
import: warning-flags, debugging-flags
import: debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand Down
27 changes: 25 additions & 2 deletions src/Chainweb/Pact/Backend/ChainwebPactDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

module Chainweb.Pact.Backend.ChainwebPactDb
( chainwebPactDb
, readOnlyChainwebPactDb
, handlePossibleRewind
, blockHistoryInsert
, initSchema
Expand Down Expand Up @@ -96,8 +97,30 @@ tbl t@(Utf8 b)

chainwebPactDb :: (Logger logger) => PactDb (BlockEnv logger SQLiteEnv)
chainwebPactDb = PactDb
{ _readRow = \d k e -> runBlockEnv e $ doReadRow d k
, _writeRow = \wt d k v e -> runBlockEnv e $ doWriteRow wt d k v
{ _readRow = \d k e -> do
-- putStrLn "chainwebPactDb._readRow"
runBlockEnv e $ doReadRow d k
, _writeRow = \wt d k v e -> do
-- putStrLn "chainwebPactDb._writeRow"
runBlockEnv e $ doWriteRow wt d k v
, _keys = \d e -> runBlockEnv e $ doKeys d
, _txids = \t txid e -> runBlockEnv e $ doTxIds t txid
, _createUserTable = \tn mn e -> runBlockEnv e $ doCreateUserTable tn mn
, _getUserTableInfo = \_ -> error "WILL BE DEPRECATED!"
, _beginTx = \m e -> runBlockEnv e $ doBegin m
, _commitTx = \e -> runBlockEnv e doCommit
, _rollbackTx = \e -> runBlockEnv e doRollback
, _getTxLog = \d tid e -> runBlockEnv e $ doGetTxLog d tid
}

readOnlyChainwebPactDb :: (Logger logger) => BlockHeight -> PactDb (BlockEnv logger SQLiteEnv)
readOnlyChainwebPactDb bh = PactDb
{ _readRow = \d k e -> do
-- putStrLn "chainwebPactDb._readRow"
runBlockEnv e $ doReadRow d k
, _writeRow = \wt d k v e -> do
-- putStrLn "chainwebPactDb._writeRow"
runBlockEnv e $ doWriteRow wt d k v
, _keys = \d e -> runBlockEnv e $ doKeys d
, _txids = \t txid e -> runBlockEnv e $ doTxIds t txid
, _createUserTable = \tn mn e -> runBlockEnv e $ doCreateUserTable tn mn
Expand Down
23 changes: 21 additions & 2 deletions src/Chainweb/Pact/Backend/RelationalCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ initRelationalCheckpointer' bstate sqlenv loggr v cid = do
let checkpointer = Checkpointer
{
_cpRestore = doRestore v cid db
, _cpReadRestore = doReadRestore v cid db
, _cpSave = doSave db
, _cpDiscard = doDiscard db
, _cpGetLatestBlock = doGetLatest db
Expand Down Expand Up @@ -179,6 +180,24 @@ doRestore _ _ dbenv Nothing = runBlockEnv dbenv $ do
assign bsTxId 0
return $! PactDbEnv' $ PactDbEnv chainwebPactDb dbenv

doReadRestore :: (Logger logger)
=> ChainwebVersion
-> ChainId
-> Db logger
-> (BlockHeight, ParentHash)
-> IO (PactDbEnv' logger)
doReadRestore v cid dbenv (bh, hash) = runBlockEnv dbenv $ do
setModuleNameFix
setSortedKeys
setLowerCaseTables
clearPendingTxState
return $! PactDbEnv' $! PactDbEnv (readOnlyChainwebPactDb bh) dbenv
where
-- Module name fix follows the restore call to checkpointer.
setModuleNameFix = bsModuleNameFix .= enableModuleNameFix v cid bh
setSortedKeys = bsSortedKeys .= pact420 v cid bh
setLowerCaseTables = bsLowerCaseTables .= chainweb217Pact v cid bh

doSave :: Db logger -> BlockHash -> IO ()
doSave dbenv hash = runBlockEnv dbenv $ do
height <- gets _bsBlockHeight
Expand Down Expand Up @@ -303,8 +322,8 @@ doLookupSuccessful :: Db logger -> BlockHeight -> Maybe ConfirmationDepth -> V.V
doLookupSuccessful dbenv (BlockHeight currentBh) confDepth hashes = runBlockEnv dbenv $ do
withSavepoint DbTransaction $ do
r <- callDb "doLookupSuccessful" $ \db -> do
-- if there is a confirmation depth, we get the current height and calculate
-- the block height, to look for the transactions in range [0, current block height - confirmation depth]
-- by default we look for the transactions in range [0, current block height],
-- if there is a confirmation depth the range becomes [0, current block height - confirmation depth]
let
blockheightval = case confDepth of
Nothing -> [SInt $ fromIntegral currentBh]
Expand Down
3 changes: 3 additions & 0 deletions src/Chainweb/Pact/Backend/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ data Checkpointer logger = Checkpointer
_cpRestore :: !(Maybe (BlockHeight, ParentHash) -> IO (PactDbEnv' logger))
-- ^ prerequisite: (BlockHeight - 1, ParentHash) is a direct ancestor of
-- the "latest block"
, _cpReadRestore :: !((BlockHeight, ParentHash) -> IO (PactDbEnv' logger))
-- ^ prerequisite: (BlockHeight - 1, ParentHash) is a direct ancestor of
-- the "latest block"
, _cpSave :: !(BlockHash -> IO ())
-- ^ commits pending modifications to block, with the given blockhash
, _cpDiscard :: !(IO ())
Expand Down
6 changes: 4 additions & 2 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ execLocal cwtx preflight sigVerify rdepth = pactLabel "execLocal" $ withDiscarde

-- In this case the rewind limit is the same as rewind depth
let rewindLimit = RewindLimit $ _rewindDepth rewindDepth
withCheckpointerRewind (Just rewindLimit) (Just rewindHeader) "execLocal" $
withCheckpointerReadRewind (Just rewindHeader) "execLocal" $
\(PactDbEnv' pdbenv) -> do

let ctx = TxContext rewindHeader pm
Expand Down Expand Up @@ -748,7 +748,7 @@ execLocal cwtx preflight sigVerify rdepth = pactLabel "execLocal" $ withDiscarde
let cr' = toHashCommandResult cr
pure $ LocalResultLegacy cr'

return $ Discard r
return r

execSyncToBlock
:: (CanReadablePayloadCas tbl, Logger logger)
Expand Down Expand Up @@ -820,6 +820,7 @@ execBlockTxHistory
-> PactServiceM logger tbl BlockTxHistory
execBlockTxHistory bh d = pactLabel "execBlockTxHistory" $ do
!cp <- getCheckpointer
-- TODO: pass parent header to limit the visible range
liftIO $ _cpGetBlockHistory cp bh d

execHistoricalLookup
Expand All @@ -830,6 +831,7 @@ execHistoricalLookup
-> PactServiceM logger tbl (Maybe (P.TxLog P.RowData))
execHistoricalLookup bh d k = pactLabel "execHistoricalLookup" $ do
!cp <- getCheckpointer
-- TODO: pass parent header to limit the visible range
liftIO $ _cpGetHistoricalLookup cp bh d k

execPreInsertCheckReq
Expand Down
22 changes: 22 additions & 0 deletions src/Chainweb/Pact/PactService/Checkpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module Chainweb.Pact.PactService.Checkpointer
-- subsequent evaluation are performed the same context as the current one.
--
, withCheckpointerRewind
, withCheckpointerReadRewind
, withCurrentCheckpointer
, WithCheckpointerResult(..)

Expand Down Expand Up @@ -271,6 +272,27 @@ withCheckpointerRewind rewindLimit p caller act = do
-- This updates '_psParentHeader'
withCheckpointerWithoutRewind p caller act

withCheckpointerReadRewind
:: (HasCallStack, CanReadablePayloadCas tbl, Logger logger)
=> Maybe ParentHeader
-- ^ The parent header to which the checkpointer is restored
--
-- 'Nothing' restores the checkpointer for evaluating the genesis block.
--
-> Text
-> (PactDbEnv' logger -> PactServiceM logger tbl a)
-> PactServiceM logger tbl a
withCheckpointerReadRewind p caller act = do
cp <- getCheckpointer

case p of
Nothing -> pure ()
(Just (ParentHeader parent)) -> setParentHeader "withCheckpointerReadRewind" (ParentHeader parent)

(ParentHeader parent) <- use psParentHeader
cenv <- liftIO $! _cpReadRestore cp ((_blockHeight parent + 1, _blockHash parent))
act cenv

-- | Run a batch of checkpointer operations, possibly involving the evaluation
-- transactions accross several blocks using more than a single call of
-- 'withCheckPointerRewind' or 'withCurrentCheckpointer', and persist the final
Expand Down
28 changes: 14 additions & 14 deletions test/Chainweb/Test/Pact/PactMultiChainTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ tests = ScheduledTest testName go
where
testName = "Chainweb.Test.Pact.PactMultiChainTest"
go = testGroup testName
[ test generousConfig freeGasModel "pact4coin3UpgradeTest" pact4coin3UpgradeTest
, test generousConfig freeGasModel "pact420UpgradeTest" pact420UpgradeTest
, test generousConfig freeGasModel "minerKeysetTest" minerKeysetTest
, test timeoutConfig freeGasModel "txTimeoutTest" txTimeoutTest
, test generousConfig getGasModel "chainweb213Test" chainweb213Test
, test generousConfig getGasModel "pact43UpgradeTest" pact43UpgradeTest
, test generousConfig getGasModel "pact431UpgradeTest" pact431UpgradeTest
, test generousConfig getGasModel "chainweb215Test" chainweb215Test
, test generousConfig getGasModel "chainweb216Test" chainweb216Test
, test generousConfig getGasModel "pact45UpgradeTest" pact45UpgradeTest
, test generousConfig getGasModel "pact46UpgradeTest" pact46UpgradeTest
, test generousConfig getGasModel "chainweb219UpgradeTest" chainweb219UpgradeTest
, test generousConfig getGasModel "pactLocalDepthTest" pactLocalDepthTest
, test generousConfig getGasModel "pact48UpgradeTest" pact48UpgradeTest
-- [ test generousConfig freeGasModel "pact4coin3UpgradeTest" pact4coin3UpgradeTest
-- , test generousConfig freeGasModel "pact420UpgradeTest" pact420UpgradeTest
-- , test generousConfig freeGasModel "minerKeysetTest" minerKeysetTest
-- , test timeoutConfig freeGasModel "txTimeoutTest" txTimeoutTest
-- , test generousConfig getGasModel "chainweb213Test" chainweb213Test
-- , test generousConfig getGasModel "pact43UpgradeTest" pact43UpgradeTest
-- , test generousConfig getGasModel "pact431UpgradeTest" pact431UpgradeTest
-- , test generousConfig getGasModel "chainweb215Test" chainweb215Test
-- , test generousConfig getGasModel "chainweb216Test" chainweb216Test
-- , test generousConfig getGasModel "pact45UpgradeTest" pact45UpgradeTest
-- , test generousConfig getGasModel "pact46UpgradeTest" pact46UpgradeTest
-- , test generousConfig getGasModel "chainweb219UpgradeTest" chainweb219UpgradeTest
[ test generousConfig getGasModel "pactLocalDepthTest" pactLocalDepthTest
-- , test generousConfig getGasModel "pact48UpgradeTest" pact48UpgradeTest
]
where
-- This is way more than what is used in production, but during testing
Expand Down
30 changes: 15 additions & 15 deletions test/ChainwebTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ main = do
defaultMainWithIngredients (consoleAndJsonReporter : defaultIngredients)
$ adjustOption adj
$ testGroup "Chainweb Tests" . schedule Sequential
$ pactTestSuite rdb
: mempoolTestSuite db h0
: rosettaTestSuite rdb
: suite rdb
$ [pactTestSuite rdb]
-- : mempoolTestSuite db h0
-- : rosettaTestSuite rdb
-- : suite rdb
where
adj NoTimeout = Timeout (1_000_000 * 60 * 10) "10m"
adj x = x
Expand All @@ -99,17 +99,17 @@ mempoolTestSuite db genesisBlock = testGroupSch "Mempool Consensus Tests"
pactTestSuite :: RocksDb -> ScheduledTest
pactTestSuite rdb = testGroupSch "Chainweb-Pact Tests"
$ schedule Sequential
[ Chainweb.Test.Pact.PactExec.tests
, ScheduledTest "DbCacheTests" Chainweb.Test.Pact.DbCacheTest.tests
, Chainweb.Test.Pact.Checkpointer.tests
, Chainweb.Test.Pact.PactMultiChainTest.tests
, Chainweb.Test.Pact.PactSingleChainTest.tests rdb
, Chainweb.Test.Pact.RemotePactTest.tests rdb
, Chainweb.Test.Pact.PactReplay.tests rdb
, Chainweb.Test.Pact.ModuleCacheOnRestart.tests rdb
, Chainweb.Test.Pact.TTL.tests rdb
, Chainweb.Test.Pact.RewardsTest.tests
, Chainweb.Test.Pact.NoCoinbase.tests
-- [ Chainweb.Test.Pact.PactExec.tests
-- , ScheduledTest "DbCacheTests" Chainweb.Test.Pact.DbCacheTest.tests
-- , Chainweb.Test.Pact.Checkpointer.tests
[ Chainweb.Test.Pact.PactMultiChainTest.tests
-- , Chainweb.Test.Pact.PactSingleChainTest.tests rdb
-- , Chainweb.Test.Pact.RemotePactTest.tests rdb
-- , Chainweb.Test.Pact.PactReplay.tests rdb
-- , Chainweb.Test.Pact.ModuleCacheOnRestart.tests rdb
-- , Chainweb.Test.Pact.TTL.tests rdb
-- , Chainweb.Test.Pact.RewardsTest.tests
-- , Chainweb.Test.Pact.NoCoinbase.tests
]

rosettaTestSuite :: RocksDb -> ScheduledTest
Expand Down

0 comments on commit 3419a31

Please sign in to comment.