Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/Chainweb/MempoolBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mempoolGetBlockBench :: _ => _
mempoolGetBlockBench name p bf n = C.bench name
$ C.perRunEnv (setup (V.take n cmds)) $ \(NoopNFData mp) -> do
Mempool.mempoolGetBlock mp bf Mempool.noopMempoolPreBlockCheck
(EvaluationCtx (Parent $ BlockCreationTime epoch) (Parent nullBlockHash) (Parent $ BlockHeight 0) (ForkNumber 0) (MinerReward 0) ())
(EvaluationCtx (Parent $ BlockCreationTime epoch) (Parent nullBlockHash) (Parent $ BlockHeight 0) (Parent $ ForkNumber 0) (MinerReward 0) ())
>>= p

allPendingTxHashes :: Mempool.MempoolBackend t -> IO [Mempool.TransactionHash]
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/BlockHeader/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ genesisParentBlockHash p = Parent $ BlockHash $ MerkleLogHash

genesisRankedParentBlockHash :: (HasVersion, HasChainId p) => p -> Parent RankedBlockHash
genesisRankedParentBlockHash p = Parent $ RankedBlockHash
(genesisHeight (_chainId p))
(genesisHeight (_chainId p)) -- As an exception we assume that the (ghost) parent of Genesis has the same blockheight than Genesis
(unwrapParent $ genesisParentBlockHash p)

-- | A block chain is globally uniquely identified by its genesis hash.
Expand Down
17 changes: 11 additions & 6 deletions src/Chainweb/Pact/Backend/ChainwebPactDb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import Data.HashMap.Strict qualified as HM
import Data.HashMap.Strict qualified as HashMap
import Data.HashSet qualified as HashSet
import Data.Int
import Data.Word
import Data.List(sort)
import Data.Map.Strict qualified as M
import Data.Maybe
Expand Down Expand Up @@ -135,6 +136,7 @@ import Chainweb.BlockHeader (encodeBlockPayloadHash, BlockPayloadHash)
import Chainweb.BlockHeight
import Chainweb.Logger
import Chainweb.Pact.Backend.InMemDb qualified as InMemDb
import Chainweb.Pact.Types
import Chainweb.Pact.Backend.Types
import Chainweb.Pact.Backend.Utils
import Chainweb.Pact.SPV (pactSPV)
Expand Down Expand Up @@ -233,8 +235,8 @@ runOnBlockGassed env stateVar act = do
return (newState, fmap fst r)
liftEither r

chainwebPactBlockDb :: (Logger logger) => HasVersion => BlockHandlerEnv logger -> ChainwebPactDb
chainwebPactBlockDb env = ChainwebPactDb
chainwebPactBlockDb :: (Logger logger) => HasVersion => BlockHandlerEnv logger -> Maybe Word64 -> ChainwebPactDb
chainwebPactBlockDb env minBlockHistory = ChainwebPactDb
{ doChainwebPactDbTransaction = \maybeRequestKey kont -> do
blockHandle <- get
stateVar <- liftIO $ newMVar $ BlockState blockHandle (_blockHandlePending blockHandle) Nothing
Expand All @@ -259,9 +261,10 @@ chainwebPactBlockDb env = ChainwebPactDb
let headerOracle = HeaderOracle
{ chain = _chainId env
, consult = \(Parent hsh) -> do
let minHeight = maybe (BlockHeight 0) (\delta -> (currentHeight - fromIntegral delta)) minBlockHistory
throwOnDbError (lookupBlockHash (_blockHandlerDb env) hsh) <&> \case
Nothing -> False
Just rootHeight -> rootHeight < currentHeight
Just rootHeight -> rootHeight < currentHeight && rootHeight >= minHeight
}
let spv = pactSPV headerOracle currentHeight
r <- liftIO $ kont pactDb spv
Expand Down Expand Up @@ -644,8 +647,8 @@ toTxLog cid bh d key value = do
Nothing -> error $ "toTxLog: Unexpected value, unable to deserialize log: " <> sshow value
Just v -> return $! Pact.TxLog d (fromUtf8 key) v

commitBlockStateToDatabase :: SQLiteEnv -> Ranked (BlockHash, BlockPayloadHash) -> BlockHandle -> IO ()
commitBlockStateToDatabase db blockInfo blockHandle = throwOnDbError $ do
commitBlockStateToDatabase :: SQLiteEnv -> Ranked (BlockHash, BlockPayloadHash) -> BlockHandle -> BlockCtx -> IO ()
commitBlockStateToDatabase db blockInfo blockHandle ctx = throwOnDbError $ do
let newTables = _pendingTableCreation $ _blockHandlePending blockHandle
mapM_ (\tn -> createUserTable (toUtf8 tn)) newTables
backendWriteUpdateBatch (_pendingWrites (_blockHandlePending blockHandle))
Expand Down Expand Up @@ -716,9 +719,10 @@ commitBlockStateToDatabase db blockInfo blockHandle = throwOnDbError $ do
, SBlob (runPutS $ encodeBlockHash $ fst $ _ranked blockInfo)
, SBlob (runPutS $ encodeBlockPayloadHash $ snd $ _ranked blockInfo)
, SInt (fromIntegral t)
, SBlob (runPutS $ encodeHistoryCtx (ctx ^. bctxParentForkNumber) (ctx ^. bctxParentCreationTime))
]
where
stmt = "INSERT INTO BlockHistory2 ('blockheight', 'hash', 'payloadhash', 'endingtxid') VALUES (?,?,?,?);"
stmt = "INSERT INTO BlockHistory2 ('blockheight', 'hash', 'payloadhash', 'endingtxid', 'ctxData') VALUES (?,?,?,?, ?);"

createUserTable :: SQ3.Utf8 -> ExceptT LocatedSQ3Error IO ()
createUserTable tablename = do
Expand Down Expand Up @@ -798,6 +802,7 @@ initSchema sql = throwOnDbError $ do
\ endingtxid UNSIGNED BIGINT NOT NULL, \
\ hash BLOB NOT NULL, \
\ payloadhash BLOB NOT NULL, \
\ ctxData BLOBL NOT NULL, \
\ CONSTRAINT blockHeightConstraint UNIQUE (blockheight), \
\ CONSTRAINT hashConstraint UNIQUE (hash));"
-- TODO PP: payload hash should really be NOT NULL but there may exist old databases without it.
Expand Down
2 changes: 2 additions & 0 deletions src/Chainweb/Pact/Backend/Compaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ createCheckpointerIndexes db logger = do
logfun "Creating BlockHistory index"
inTx db $ throwOnDbError $ exec_ db
"CREATE UNIQUE INDEX IF NOT EXISTS BlockHistory_blockheight_unique_ix ON BlockHistory2 (blockheight)"
inTx db $ throwOnDbError $ exec_ db
"CREATE UNIQUE INDEX IF NOT EXISTS BlockHistory_hash_unique_ix ON BlockHistory2 (hash)"

logfun "Creating VersionedTableCreation index"
inTx db $ throwOnDbError $ exec_ db
Expand Down
44 changes: 44 additions & 0 deletions src/Chainweb/Pact/Backend/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ module Chainweb.Pact.Backend.Utils
, getLatestBlock
, getEarliestBlock
, lookupBlockWithHeight
, lookupBlocEnvkWithHeight
, lookupBlockHash
, lookupRankedBlockHash
, latestBlocEnv
-- * Encoders for Pact State DB
, encodeHistoryCtx
, decodeHistoryCtx
-- * SQLite conversions and assertions
, toUtf8
, fromUtf8
Expand Down Expand Up @@ -115,6 +120,8 @@ import Chainweb.Utils
import Chainweb.BlockHash
import Chainweb.BlockHeader
import Chainweb.BlockHeight
import Chainweb.ForkState
import Chainweb.BlockCreationTime
import Database.SQLite3.Direct hiding (open2)
import GHC.Stack
import qualified Data.ByteString.Short as SB
Expand Down Expand Up @@ -559,6 +566,34 @@ lookupBlockWithHeight db bheight = do
where
qtext = "SELECT hash FROM BlockHistory2 WHERE blockheight = ?;"


lookupBlocEnvkWithHeight :: HasCallStack => SQ3.Database -> BlockHeight -> ExceptT LocatedSQ3Error IO (Maybe (Ranked BlockHash, Parent ForkNumber, Parent BlockCreationTime))
lookupBlocEnvkWithHeight db bheight = do
qry db qtext [SInt $ fromIntegral bheight] [RBlob, RBlob] >>= \case
[[SBlob hash, SBlob ctxData]] -> let h = Ranked bheight (either error id $ runGetEitherS decodeBlockHash hash)
(fn, ct) = (either error id $ runGetEitherS decodeHistoryCtx ctxData)
in return $! Just $! (h, fn, ct)

[] -> return Nothing
res -> error $ "Invalid result, " <> sshow res
where
qtext = "SELECT hash, ctxData FROM BlockHistory2 WHERE blockheight = ?;"

latestBlocEnv :: HasCallStack => SQ3.Database -> ExceptT LocatedSQ3Error IO (Maybe (Ranked BlockHash, Parent ForkNumber, Parent BlockCreationTime))
latestBlocEnv db = do
qry db qtext [] [RInt, RBlob, RBlob] >>= \case
[[SInt bheight, SBlob hash, SBlob ctxData]] -> let h = Ranked (BlockHeight $ fromIntegral bheight) (either error id $ runGetEitherS decodeBlockHash hash)
(fn, ct) = (either error id $ runGetEitherS decodeHistoryCtx ctxData)
in return $! Just $! (h, fn, ct)

[] -> return Nothing
res -> error $ "Invalid result, " <> sshow res
where
qtext = "SELECT blockheight, hash, ctxData FROM BlockHistory2 ORDER BY blockheight DESC LIMIT 1;"




lookupBlockHash :: HasCallStack => SQ3.Database -> BlockHash -> ExceptT LocatedSQ3Error IO (Maybe BlockHeight)
lookupBlockHash db hash = do
qry db qtext [SBlob (runPutS (encodeBlockHash hash))] [RInt] >>= \case
Expand All @@ -579,6 +614,15 @@ lookupRankedBlockHash db rankedBHash = throwOnDbError $ do
where
qtext = "SELECT COUNT(*) FROM BlockHistory2 WHERE blockheight = ? AND hash = ?;"

encodeHistoryCtx :: Parent ForkNumber -> Parent BlockCreationTime -> Put
encodeHistoryCtx fn ct = putWord32le (_getForkNumber $ unwrapParent fn) >> encodeBlockCreationTime (unwrapParent ct)

decodeHistoryCtx :: Get (Parent ForkNumber, Parent BlockCreationTime)
decodeHistoryCtx = do
fn <- Parent . ForkNumber <$> getWord32le
bct <- Parent <$> decodeBlockCreationTime
return (fn, bct)

data LocatedSQ3Error = LocatedSQ3Error !CallStack !SQ3.Error
instance Show LocatedSQ3Error where
show (LocatedSQ3Error cs e) =
Expand Down
54 changes: 24 additions & 30 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ runGenesisIfNeeded logger serviceEnv = do
logFunctionText logger Debug "running genesis"
let genesisBlockHash = genesisBlockHeader cid ^. blockHash
let genesisPayloadHash = genesisBlockPayloadHash cid
let gTime = implicitVersion ^?! versionGenesis . genesisTime . atChain cid
let targetSyncState = genesisConsensusState cid
let genesisRankedBlockHash = RankedBlockHash (genesisHeight cid) genesisBlockHash
let evalCtx = genesisEvaluationCtx serviceEnv
let blockCtx = blockCtxOfEvaluationCtx cid evalCtx
let !genesisPayload = case _psGenesisPayload serviceEnv of
Expand All @@ -237,12 +235,12 @@ runGenesisIfNeeded logger serviceEnv = do
$ NEL.singleton
$ (
if pact5 cid (genesisHeight cid)
then Checkpointer.Pact5RunnableBlock $ \chainwebPactDb -> do
then Checkpointer.Pact5RunnableBlock blockCtx $ \chainwebPactDb -> do
_ <- Pact.execExistingBlock logger serviceEnv
(BlockEnv blockCtx chainwebPactDb)
(CheckablePayloadWithOutputs genesisPayload)
return ((), (genesisBlockHash, genesisPayloadHash))
else Checkpointer.Pact4RunnableBlock $ \blockDbEnv -> do
else Checkpointer.Pact4RunnableBlock blockCtx $ \blockDbEnv -> do
_ <- Pact4.execBlock logger serviceEnv
(Pact4.BlockEnv blockCtx blockDbEnv)
(CheckablePayloadWithOutputs genesisPayload)
Expand All @@ -263,8 +261,7 @@ runGenesisIfNeeded logger serviceEnv = do
emptyBlock <- (throwIfNoHistory =<<) $
Checkpointer.readFrom logger cid
(_psReadWriteSql serviceEnv)
(Parent gTime)
(Parent genesisRankedBlockHash)
blockCtx
Checkpointer.PactRead
{ pact5Read = \blockEnv blockHandle ->
makeEmptyBlock logger serviceEnv blockEnv blockHandle
Expand All @@ -289,9 +286,10 @@ execNewGenesisBlock
-> IO PayloadWithOutputs
execNewGenesisBlock logger serviceEnv newTrans = withTransaction (_psReadWriteSql serviceEnv) $ do
let cid = _chainId serviceEnv
let parentCreationTime = Parent (implicitVersion ^?! versionGenesis . genesisTime . atChain cid)
historicalBlock <- Checkpointer.readFrom logger cid (_psReadWriteSql serviceEnv) parentCreationTime
(genesisRankedParentBlockHash cid)
let evalCtx = genesisEvaluationCtx serviceEnv
let blockCtx = blockCtxOfEvaluationCtx cid evalCtx

historicalBlock <- Checkpointer.readFrom logger cid (_psReadWriteSql serviceEnv) blockCtx
Checkpointer.PactRead
{ pact5Read = \blockEnv startHandle -> do

Expand All @@ -310,10 +308,10 @@ execNewGenesisBlock logger serviceEnv newTrans = withTransaction (_psReadWriteSq

let fakeMempoolServiceEnv = serviceEnv
& psMempoolAccess .~ mempty
{ mpaGetBlock = \bf pbc evalCtx -> do
{ mpaGetBlock = \bf pbc _evalCtx -> do
if _bfCount bf == 0
then do
maybeInvalidTxs <- pbc evalCtx newTrans
maybeInvalidTxs <- pbc _evalCtx newTrans
validTxs <- case partitionEithers (V.toList maybeInvalidTxs) of
([], validTxs) -> return validTxs
(errs, _) -> error $ "Pact 5 genesis commands invalid: " <> sshow errs
Expand Down Expand Up @@ -356,8 +354,7 @@ execReadOnlyReplay logger serviceEnv evalCtx = do
logger
cid
sql
(_evaluationCtxParentCreationTime evalCtx)
(_evaluationCtxRankedParentHash evalCtx)
(blockCtxOfEvaluationCtx cid evalCtx)
Checkpointer.PactRead
{ pact5Read = \blockEnv blockHandle ->
runExceptT $ flip evalStateT blockHandle $
Expand Down Expand Up @@ -394,8 +391,7 @@ execLocal logger serviceEnv cwtx preflight sigVerify rdepth = do
where

doLocal = Pool.withResource (view psReadSqlPool serviceEnv) $ \sql -> withTransaction sql $ do
fakeNewBlockCtx <- liftIO Checkpointer.mkFakeParentCreationTime
Checkpointer.readFromNthParent logger cid sql fakeNewBlockCtx (fromIntegral rewindDepth)
Checkpointer.readFromNthParent logger cid sql (fromIntegral rewindDepth)
$ Checkpointer.readPact5 "Pact 4 cannot execute local calls" $ \blockEnv blockHandle -> do
let blockCtx = view psBlockCtx blockEnv
let requestKey = Pact.cmdToRequestKey cwtx
Expand Down Expand Up @@ -427,7 +423,7 @@ execLocal logger serviceEnv cwtx preflight sigVerify rdepth = do
implicitVersion
(_bctxChainId blockCtx)
(_bctxCurrentBlockHeight blockCtx)
(_bctxParentForkNumber blockCtx)
(blockCtx ^. bctxParentForkNumber . _Parent)
cwtx
applyCmdResult <- liftIO $ flip evalStateT blockHandle $ doChainwebPactDbTransaction (blockEnv ^. psBlockDbEnv) Nothing (\dbEnv spvSupport ->
Pact.applyCmd
Expand Down Expand Up @@ -604,7 +600,7 @@ syncToFork logger serviceEnv hints forkInfo = do
if guardCtx pact5 blockCtx
then
return $
Checkpointer.Pact5RunnableBlock $ \chainwebPactDb -> do
Checkpointer.Pact5RunnableBlock blockCtx $ \chainwebPactDb -> do
(_, pwo, validatedTxs) <-
Pact.execExistingBlock logger serviceEnv
(BlockEnv blockCtx chainwebPactDb)
Expand All @@ -617,7 +613,7 @@ syncToFork logger serviceEnv hints forkInfo = do
return $ (DList.singleton validatedTxs, (_ranked rankedBHash, expectedPayloadHash))
else
return $
Checkpointer.Pact4RunnableBlock $ \blockDbEnv -> do
Checkpointer.Pact4RunnableBlock blockCtx $ \blockDbEnv -> do
(_, pwo) <-
Pact4.execBlock logger serviceEnv
(Pact4.BlockEnv blockCtx blockDbEnv)
Expand Down Expand Up @@ -654,10 +650,11 @@ syncToFork logger serviceEnv hints forkInfo = do
-- immediately. then we set up a separate thread
-- to add new transactions to the block.
logFunctionText logger Debug "producing new block"
emptyBlock <-
Checkpointer.readFromLatest logger cid sql (_newBlockCtxParentCreationTime newBlockCtx)
$ Checkpointer.readPact5 "Pact 4 cannot make new blocks" $ \blockEnv blockHandle ->
makeEmptyBlock logger serviceEnv blockEnv blockHandle
let blockCtx = (blockCtxOfNewBlockCtx cid (Parent $ _latestRankedBlockHash newConsensusState) newBlockCtx)
emptyBlock <- throwIfNoHistory =<< (Checkpointer.readFrom logger cid sql blockCtx
$ Checkpointer.readPact5 "Pact 4 cannot make new blocks"
$ \blockEnv blockHandle -> makeEmptyBlock logger serviceEnv blockEnv blockHandle)

let payloadVar = view psMiningPayloadVar serviceEnv

-- cancel payload refresher thread
Expand Down Expand Up @@ -756,9 +753,7 @@ refreshPayloads logger serviceEnv = do
maybeRefreshedBlockInProgress <-
Pool.withResource (view psReadSqlPool serviceEnv) $ \sql ->
withTransaction sql $
Checkpointer.readFrom logger cid sql
(_bctxParentCreationTime $ _blockInProgressBlockCtx blockInProgress)
(_bctxParentRankedBlockHash $ _blockInProgressBlockCtx blockInProgress)
Checkpointer.readFrom logger cid sql (_blockInProgressBlockCtx blockInProgress)
$ Checkpointer.readPact5 "Pact 4 cannot make new blocks" $ \blockEnv _bh -> do
let dbEnv = view psBlockDbEnv blockEnv
continueBlock logger serviceEnv dbEnv blockInProgress
Expand Down Expand Up @@ -856,9 +851,8 @@ execPreInsertCheckReq
execPreInsertCheckReq logger serviceEnv txs = do
let requestKeys = V.map Pact.cmdToRequestKey txs
logFunctionText logger Info $ "(pre-insert check " <> sshow requestKeys <> ")"
fakeParentCreationTime <- Checkpointer.mkFakeParentCreationTime
let act sql = withTransaction sql $
Checkpointer.readFromLatest logger cid sql fakeParentCreationTime $ Checkpointer.PactRead
Checkpointer.readFromLatest logger cid sql $ Checkpointer.PactRead
{ pact5Read = \blockEnv bh -> do
forM txs $ \tx ->
fmap (either Just (\_ -> Nothing)) $ runExceptT $ do
Expand Down Expand Up @@ -931,12 +925,12 @@ execLookupPactTxs logger serviceEnv confDepth txs = do
if V.null txs
then return (Historical mempty)
else do
go =<< liftIO Checkpointer.mkFakeParentCreationTime
go
where
depth = maybe 0 (fromIntegral . _confirmationDepth) confDepth
cid = _chainId serviceEnv
go ctx = Pool.withResource (_psReadSqlPool serviceEnv) $ \sql -> withTransaction sql $
Checkpointer.readFromNthParent logger cid sql ctx depth
go = Pool.withResource (_psReadSqlPool serviceEnv) $ \sql -> withTransaction sql $
Checkpointer.readFromNthParent logger cid sql depth
-- not sure about this, disallows looking up pact txs if we haven't
-- caught up to pact 5
$ Checkpointer.readPact5 "Pact 4 cannot look up transactions" $ \blockEnv _ -> do
Expand Down
Loading
Loading