diff --git a/bench/Chainweb/MempoolBench.hs b/bench/Chainweb/MempoolBench.hs index 585c694b24..9935f5426e 100644 --- a/bench/Chainweb/MempoolBench.hs +++ b/bench/Chainweb/MempoolBench.hs @@ -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] diff --git a/src/Chainweb/BlockHeader/Internal.hs b/src/Chainweb/BlockHeader/Internal.hs index 771e6e6ffa..17b044809f 100644 --- a/src/Chainweb/BlockHeader/Internal.hs +++ b/src/Chainweb/BlockHeader/Internal.hs @@ -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. diff --git a/src/Chainweb/Pact/Backend/ChainwebPactDb.hs b/src/Chainweb/Pact/Backend/ChainwebPactDb.hs index c09bb66e36..be905427e4 100644 --- a/src/Chainweb/Pact/Backend/ChainwebPactDb.hs +++ b/src/Chainweb/Pact/Backend/ChainwebPactDb.hs @@ -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 @@ -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) @@ -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 @@ -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 @@ -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)) @@ -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 @@ -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. diff --git a/src/Chainweb/Pact/Backend/Compaction.hs b/src/Chainweb/Pact/Backend/Compaction.hs index a21fdae878..40e77b56f8 100644 --- a/src/Chainweb/Pact/Backend/Compaction.hs +++ b/src/Chainweb/Pact/Backend/Compaction.hs @@ -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 diff --git a/src/Chainweb/Pact/Backend/Utils.hs b/src/Chainweb/Pact/Backend/Utils.hs index 5c0231d65b..65a9599209 100644 --- a/src/Chainweb/Pact/Backend/Utils.hs +++ b/src/Chainweb/Pact/Backend/Utils.hs @@ -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 @@ -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 @@ -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 @@ -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) = diff --git a/src/Chainweb/Pact/PactService.hs b/src/Chainweb/Pact/PactService.hs index 682cf0dcc3..ad31ef4ee5 100644 --- a/src/Chainweb/Pact/PactService.hs +++ b/src/Chainweb/Pact/PactService.hs @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 $ @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/Chainweb/Pact/PactService/Checkpointer.hs b/src/Chainweb/Pact/PactService/Checkpointer.hs index 96d3e95a0b..707c461f38 100644 --- a/src/Chainweb/Pact/PactService/Checkpointer.hs +++ b/src/Chainweb/Pact/PactService/Checkpointer.hs @@ -33,8 +33,7 @@ -- Checkpointer interaction for Pact service. -- module Chainweb.Pact.PactService.Checkpointer - ( mkFakeParentCreationTime - , readFromLatest + ( readFromLatest , readFromNthParent , readFrom , restoreAndSave @@ -68,7 +67,8 @@ import Chainweb.BlockCreationTime import Chainweb.BlockHash import Chainweb.BlockHeader import Chainweb.BlockHeight -import Chainweb.ForkState (pact4ForkNumber) +import Chainweb.ForkState (ForkNumber) +import Control.Monad.Trans.Maybe import Chainweb.Logger import Chainweb.MinerReward import Chainweb.Pact.Backend.ChainwebPactDb qualified as ChainwebPactDb @@ -78,10 +78,9 @@ import Chainweb.Pact.Types import Chainweb.Parent import Chainweb.PayloadProvider import Chainweb.Ranked -import Chainweb.Time import Chainweb.Utils hiding (check) import Chainweb.Version -import Chainweb.Version.Guards (pact5) +import Chainweb.Version.Guards (pact5, minimumBlockHeaderHistory) import Control.Exception.Safe (MonadMask) import Data.List.NonEmpty (NonEmpty) import Chainweb.Pact.Backend.ChainwebPactDb (lookupRankedBlockHash) @@ -99,12 +98,39 @@ import Pact.Types.Command qualified as Pact4 import Pact.Types.Hash qualified as Pact4 import Data.Foldable1 (foldlMapM1) --- readFrom demands to know this context in case it's mining a block. --- But it's not really used by /local, or polling, so we fabricate it --- for those users. -mkFakeParentCreationTime :: IO (Parent BlockCreationTime) -mkFakeParentCreationTime = do - Parent . BlockCreationTime <$> getCurrentTimeIntegral + +-- We try to recover the clock context from the history with a rewind of n. +-- +-- We read both the nth block (target) and its parent: +-- - parentBlockCreationTime and ParentForkNumber are extracted from the stored context associated to target +-- - parentHash is extracted from its parent (referenced by Height) +-- +-- n=0 is a special case. n=0 means that the context is related to the next mined block +-- The parent block is the tip.. But we still ignore what is his FOrNumber and BlockCreationTime +-- Even if the block is already mined, Consensus layer didn't send us. +-- In this case, we take the ForkNumber and BlockCreationTime from the GrandParent +-- +blockCtxFromHistory:: (HasCallStack, HasVersion) => ChainId -> SQLiteEnv -> Word -> MaybeT IO BlockCtx +blockCtxFromHistory cid sql n = do + latest@(latestH, _latestFn, _latestCt) <- MaybeT $ latestBlocEnv sql + (parentH, _parentFn, _parentCt) <- if n==0 + then return latest + else MaybeT $ lookupBlocEnvkWithHeight sql $ (_rankedHeight latestH - fromIntegral n) + + (_targetH, targetFn, targetCt) <- if n==0 || n==1 + then return latest + else MaybeT $ lookupBlocEnvkWithHeight sql $ (_rankedHeight latestH - fromIntegral n + 1) + + return BlockCtx {_bctxParentCreationTime = targetCt -- TODO Add 30 s in case of n=0, as an estimator ?? There are pros and cons... + -- Pros: Give a more accurate estimate of the next blockTime to the local Caller. + -- Cons: Would break the assumption of "blocTime during local" <= "really mined blocktime" + -- IMHO Cons > Pros + , _bctxParentHash = Parent $ _ranked parentH + , _bctxParentHeight = Parent $ _rankedHeight parentH + , _bctxParentForkNumber = targetFn + , _bctxChainId = cid + , _bctxMinerReward = blockMinerReward (_rankedHeight latestH - fromIntegral n +1) + } -- read-only rewind to the latest block. -- note: because there is a race between getting the latest header @@ -117,11 +143,10 @@ readFromLatest => logger -> ChainId -> SQLiteEnv - -> Parent BlockCreationTime -> PactRead a -> IO a -readFromLatest logger cid sql parentCreationTime doRead = - readFromNthParent logger cid sql parentCreationTime 0 doRead >>= \case +readFromLatest logger cid sql doRead = + readFromNthParent logger cid sql 0 doRead >>= \case NoHistory -> error "readFromLatest: failed to grab the latest header, this is a bug in chainweb" Historical a -> return a @@ -132,58 +157,43 @@ readFromNthParent => logger -> ChainId -> SQLiteEnv - -> Parent BlockCreationTime -> Word -> PactRead a -> IO (Historical a) -readFromNthParent logger cid sql parentCreationTime n doRead = do - latest <- - fmap (fromMaybe (error "readFromNthParent is illegal to call before genesis")) - $ ChainwebPactDb.throwOnDbError - $ ChainwebPactDb.getLatestBlock sql - if genesisHeight cid + fromIntegral @Word @BlockHeight n > _syncStateHeight latest +readFromNthParent logger cid sql n doRead = do + latest <- (_syncStateHeight . fromMaybe (error "readFromNthParent is illegal to call before genesis")) + <$> (ChainwebPactDb.throwOnDbError $ ChainwebPactDb.getLatestBlock sql) + + if genesisHeight cid + fromIntegral @Word @BlockHeight n > latest --TODO check if a +1 condition is required here then do logFunctionText logger Warn $ "readFromNthParent asked to rewind beyond genesis, to " - <> sshow (int (_syncStateHeight latest) - int n :: Integer) + <> sshow (int latest - int n :: Integer) return NoHistory + else do - let targetHeight = _syncStateHeight latest - fromIntegral @Word @BlockHeight n - lookupBlockWithHeight sql targetHeight >>= \case - -- this case for shallow nodes without enough history - Nothing -> do - logFunctionText logger Warn "readFromNthParent asked to rewind beyond known blocks" - return NoHistory - Just nthBlock -> - readFrom logger cid sql parentCreationTime (Parent nthBlock) doRead - --- read-only rewind to a target block. --- if that target block is missing, return Nothing. + mbctx <- runMaybeT $ blockCtxFromHistory cid sql n + case mbctx of + Nothing -> logFunctionText logger Warn "readFromNthParent asked to rewind beyond known blocks" >> return NoHistory + Just bctx -> readFrom logger cid sql bctx doRead + + readFrom :: (HasCallStack, HasVersion, Logger logger) => logger -> ChainId -> SQLiteEnv - -> Parent BlockCreationTime - -- ^ you can fake this if you're not making a new block - -> Parent RankedBlockHash + -> BlockCtx -> PactRead a -> IO (Historical a) -readFrom logger cid sql parentCreationTime parent pactRead = do - let blockCtx = BlockCtx - { _bctxParentCreationTime = parentCreationTime - , _bctxParentHash = _rankedBlockHashHash <$> parent - , _bctxParentHeight = _rankedBlockHashHeight <$> parent - , _bctxParentForkNumber = pact4ForkNumber - , _bctxMinerReward = blockMinerReward (childBlockHeight cid parent) - , _bctxChainId = cid - } +readFrom logger cid sql blockCtx pactRead = do !latestHeader <- ChainwebPactDb.throwOnDbError $ fmap (maybe (genesisRankedParentBlockHash cid) (Parent . _syncStateRankedBlockHash)) $ ChainwebPactDb.getLatestBlock sql -- is the parent the latest header, i.e., can we get away without rewinding? - let parentIsLatestHeader = latestHeader == parent + let parentIsLatestHeader = latestHeader == _bctxParentRankedBlockHash blockCtx let currentHeight = _bctxCurrentBlockHeight blockCtx - Backend.Utils.getEndTxId cid sql parent >>= traverse \startTxId -> + + Backend.Utils.getEndTxId cid sql (_bctxParentRankedBlockHash blockCtx) >>= traverse \startTxId -> if pact5 cid currentHeight then do let blockHandlerEnv = ChainwebPactDb.BlockHandlerEnv @@ -195,7 +205,8 @@ readFrom logger cid sql parentCreationTime parent pactRead = do , ChainwebPactDb._blockHandlerUpperBoundTxId = startTxId , ChainwebPactDb._blockHandlerAtTip = parentIsLatestHeader } - let pactDb = ChainwebPactDb.chainwebPactBlockDb blockHandlerEnv + let minOracleHistory = minimumBlockHeaderHistory (blockCtx ^. bctxParentForkNumber . _Parent) currentHeight + let pactDb = ChainwebPactDb.chainwebPactBlockDb blockHandlerEnv minOracleHistory let blockEnv = BlockEnv blockCtx pactDb pact5Read pactRead blockEnv (emptyBlockHandle startTxId) else do @@ -240,14 +251,16 @@ data PactRead a readPact5 :: String -> (BlockEnv -> BlockHandle -> IO a) -> PactRead a readPact5 msg act = PactRead - { pact4Read = \_ -> error msg + { pact4Read = \_ -> error msg -- TODO: Not good => possible DoS for a node that is catching up ? , pact5Read = act } data RunnableBlock m q = Pact4RunnableBlock + BlockCtx -- <- FOR API consistency, in fact Pact4 doesn't need it (forall logger. Logger logger => Pact4.CurrentBlockDbEnv logger -> m (q, (BlockHash, BlockPayloadHash))) | Pact5RunnableBlock + BlockCtx (ChainwebPactDb -> StateT BlockHandle m (q, (BlockHash, BlockPayloadHash))) foldState1 @@ -293,7 +306,7 @@ restoreAndSave logger cid sql parent blocks = do executeBlock :: RunnableBlock m q -> T2 BlockHeight Pact.TxId -> m (q, T2 BlockHeight Pact.TxId) executeBlock blockAction (T2 currentBlockHeight startTxId) = do case blockAction of - Pact4RunnableBlock block | not (pact5 cid currentBlockHeight) -> do + Pact4RunnableBlock _ block | not (pact5 cid currentBlockHeight) -> do let pact4TxId = Pact4.TxId (coerce startTxId) let blockHandlerEnv = Pact4.mkBlockHandlerEnv cid currentBlockHeight sql logger newBlockDbEnv <- liftIO $ newMVar $ Pact4.BlockDbEnv @@ -327,8 +340,10 @@ restoreAndSave logger cid sql parent blocks = do , T2 (childBlockHeight cid (Parent currentRbh)) (Pact.TxId $ coerce $ Pact4._bsTxId finalBlockState)) - Pact5RunnableBlock block | pact5 cid currentBlockHeight -> do + Pact5RunnableBlock ctx block | pact5 cid currentBlockHeight -> do let + minOracleHistory = (minimumBlockHeaderHistory (ctx ^. bctxParentForkNumber. _Parent) + currentBlockHeight) blockEnv = ChainwebPactDb.BlockHandlerEnv { ChainwebPactDb._blockHandlerDb = sql , ChainwebPactDb._blockHandlerLogger = logger @@ -338,7 +353,8 @@ restoreAndSave logger cid sql parent blocks = do , ChainwebPactDb._blockHandlerUpperBoundTxId = startTxId , ChainwebPactDb._blockHandlerAtTip = True } - pactDb = ChainwebPactDb.chainwebPactBlockDb blockEnv + pactDb = ChainwebPactDb.chainwebPactBlockDb blockEnv minOracleHistory + -- run the block !((!m', !blockInfo), !blockHandle) <- runStateT (block pactDb) (emptyBlockHandle startTxId) @@ -356,6 +372,7 @@ restoreAndSave logger cid sql parent blocks = do sql (Ranked currentBlockHeight blockInfo) blockHandle + ctx let currentRbh = RankedBlockHash currentBlockHeight (fst blockInfo) return (m' @@ -386,6 +403,14 @@ lookupBlockWithHeight :: HasCallStack => SQLiteEnv -> BlockHeight -> IO (Maybe ( lookupBlockWithHeight sql bh = do ChainwebPactDb.throwOnDbError $ Backend.Utils.lookupBlockWithHeight sql bh +latestBlocEnv :: HasCallStack => SQLiteEnv -> IO (Maybe (Ranked BlockHash, Parent ForkNumber, Parent BlockCreationTime)) +latestBlocEnv sql = do + ChainwebPactDb.throwOnDbError $ Backend.Utils.latestBlocEnv sql + +lookupBlocEnvkWithHeight :: HasCallStack => SQLiteEnv -> BlockHeight -> IO (Maybe (Ranked BlockHash, Parent ForkNumber, Parent BlockCreationTime)) +lookupBlocEnvkWithHeight sql bh = do + ChainwebPactDb.throwOnDbError $ Backend.Utils.lookupBlocEnvkWithHeight sql bh + lookupBlockHash :: HasCallStack => SQLiteEnv -> BlockHash -> IO (Maybe BlockHeight) lookupBlockHash sql pbh = do ChainwebPactDb.throwOnDbError $ Backend.Utils.lookupBlockHash sql pbh diff --git a/src/Chainweb/Pact/PactService/ExecBlock.hs b/src/Chainweb/Pact/PactService/ExecBlock.hs index ff1d5f8179..cefbe2e545 100644 --- a/src/Chainweb/Pact/PactService/ExecBlock.hs +++ b/src/Chainweb/Pact/PactService/ExecBlock.hs @@ -320,7 +320,7 @@ applyCmdInBlock logger serviceEnv blockEnv miner txIdxInBlock tx = StateT $ \(bl resultOrGasError <- liftIO $ unsafeApplyCmdInBlock blockHandle (initialGasOf implicitVersion (_bctxChainId blockCtx) - (_bctxCurrentBlockHeight blockCtx) (_bctxParentForkNumber blockCtx) tx) + (_bctxCurrentBlockHeight blockCtx) (blockCtx ^. bctxParentForkNumber . _Parent) tx) alteredTx case resultOrGasError of Left err -> throwError err @@ -541,7 +541,7 @@ execExistingBlock logger serviceEnv blockEnv payload = do let blockGasLimit = Pact.GasLimit . Pact.Gas . fromIntegral <$> - maxBlockGasLimit (_bctxParentForkNumber blockCtx) (_bctxCurrentBlockHeight blockCtx) + maxBlockGasLimit (blockCtx ^. bctxParentForkNumber . _Parent) (_bctxCurrentBlockHeight blockCtx) (V.fromList -> results, _finalBlockGasLimit) <- flip weaveStatesFst blockGasLimit $ -- flip runStateT (postCoinbaseBlockHandle, blockGasLimit) $ diff --git a/src/Chainweb/Pact/TransactionExec.hs b/src/Chainweb/Pact/TransactionExec.hs index 78f3d89583..898d4d7765 100644 --- a/src/Chainweb/Pact/TransactionExec.hs +++ b/src/Chainweb/Pact/TransactionExec.hs @@ -182,7 +182,7 @@ runVerifiers txCtx cmd = do gasUsed <- liftIO . readIORef . _geGasRef . _txEnvGasEnv =<< ask let initGasRemaining = MilliGas $ case (gasToMilliGas (gasLimit ^. _GasLimit), gasUsed) of (MilliGas gasLimitMilliGasWord, MilliGas gasUsedMilliGasWord) -> gasLimitMilliGasWord - gasUsedMilliGasWord - let allVerifiers = verifiersAt (_chainId txCtx) (_bctxParentForkNumber txCtx) (_bctxCurrentBlockHeight txCtx) + let allVerifiers = verifiersAt (_chainId txCtx) (unwrapParent $ _bctxParentForkNumber txCtx) (_bctxCurrentBlockHeight txCtx) let txVerifiers = fromMaybe [] $ cmd ^. cmdPayload . pVerifiers verifierResult <- liftIO $ runVerifierPlugins (_chainId txCtx, _bctxCurrentBlockHeight txCtx) logger @@ -1008,5 +1008,5 @@ flagsFor txCtx = Set.unions guardDisablePact54FixFlags :: HasVersion => BlockCtx -> Set ExecutionFlag guardDisablePact54FixFlags txCtx - | chainweb32 (_chainId txCtx) (_bctxParentForkNumber txCtx) = Set.empty + | chainweb32 (_chainId txCtx) (unwrapParent $ _bctxParentForkNumber txCtx) = Set.empty | otherwise = Set.singleton FlagDisablePact54Fix diff --git a/src/Chainweb/Pact/Types.hs b/src/Chainweb/Pact/Types.hs index 247389a94a..2d3363e4fb 100644 --- a/src/Chainweb/Pact/Types.hs +++ b/src/Chainweb/Pact/Types.hs @@ -44,16 +44,19 @@ module Chainweb.Pact.Types , BlockCtx(..) , blockCtxOfEvaluationCtx , evaluationCtxOfBlockCtx + , blockCtxOfNewBlockCtx , guardCtx , _bctxParentRankedBlockHash , _bctxIsGenesis , _bctxCurrentBlockHeight , genesisEvaluationCtx + , genesisEvaluationCtx' , bctxParentCreationTime , bctxParentHash , bctxParentHeight , bctxChainId , bctxMinerReward + , bctxParentForkNumber , PactServiceConfig(..) , defaultPactServiceConfig @@ -151,7 +154,6 @@ import Data.Text.Encoding qualified as T import Data.Vector (Vector) import Data.Word import GHC.Generics (Generic) -import Language.Haskell.TH (mkName, nameBase) import Pact.Core.Builtin qualified as Pact import Pact.Core.Capabilities qualified as Pact import Pact.Core.ChainData qualified as Pact @@ -168,11 +170,11 @@ import System.LogLevel import Chainweb.BlockHeader import Chainweb.BlockPayloadHash import Chainweb.Counter -import Chainweb.ForkState (ForkNumber, pact4ForkNumber) +import Chainweb.ForkState (ForkNumber(..)) import Chainweb.Logger import Chainweb.Pact.Mempool.Mempool import Chainweb.Miner.Pact (Miner, toMinerData, noMiner) -import Chainweb.Pact.Backend.ChainwebPactDb +--import Chainweb.Pact.Backend.ChainwebPactDb import Chainweb.Pact.Backend.Types import Chainweb.Pact.Transaction qualified as Pact import Chainweb.Pact4.ModuleCache @@ -193,6 +195,7 @@ import Control.Concurrent.STM import Chainweb.BlockHeight import Chainweb.BlockHash import Chainweb.Parent +import Chainweb.Ranked import Chainweb.MinerReward import Chainweb.BlockCreationTime import Control.Concurrent.Async @@ -358,19 +361,12 @@ data BlockCtx = BlockCtx { _bctxParentCreationTime :: !(Parent BlockCreationTime) , _bctxParentHash :: !(Parent BlockHash) , _bctxParentHeight :: !(Parent BlockHeight) - , _bctxParentForkNumber :: !ForkNumber + , _bctxParentForkNumber :: !(Parent ForkNumber) , _bctxChainId :: !ChainId , _bctxMinerReward :: !MinerReward } deriving stock (Eq, Generic, Show) --- The lens for '_bctxParentForkNumber' is not generated because it is unused. --- -makeLensesWith - (lensRules & lensField .~ \_ _ f -> - if nameBase f == "_bctxParentForkNumber" - then [] - else [TopName $ mkName $ dropWhile (== '_') $ nameBase f]) - ''BlockCtx +makeLenses ''BlockCtx instance ToJSON BlockCtx where toJSON BlockCtx{..} = object @@ -392,6 +388,16 @@ blockCtxOfEvaluationCtx cid ec = BlockCtx , _bctxMinerReward = _evaluationCtxMinerReward ec } +blockCtxOfNewBlockCtx :: ChainId -> Parent RankedBlockHash -> NewBlockCtx -> BlockCtx +blockCtxOfNewBlockCtx cid bh nbc = BlockCtx + { _bctxParentCreationTime = _newBlockCtxParentCreationTime nbc + , _bctxParentHash = _ranked <$> bh + , _bctxParentHeight = _rankedHeight <$> bh + , _bctxParentForkNumber = _newBlockCtxParentForkNumber nbc + , _bctxChainId = cid + , _bctxMinerReward = _newBlockCtxMinerReward nbc + } + evaluationCtxOfBlockCtx :: BlockCtx -> EvaluationCtx () evaluationCtxOfBlockCtx bctx = EvaluationCtx { _evaluationCtxParentCreationTime = _bctxParentCreationTime bctx @@ -569,22 +575,23 @@ instance HasChainId BlockEnv where -- the evaluation context for the genesis block; note that the payload is filled in genesisEvaluationCtx :: HasVersion => ServiceEnv tbl -> EvaluationCtx ConsensusPayload -genesisEvaluationCtx serviceEnv = EvaluationCtx +genesisEvaluationCtx env = genesisEvaluationCtx' (_chainId env) (_psGenesisPayload env) + +genesisEvaluationCtx' :: HasVersion => ChainId -> Maybe Chainweb.PayloadWithOutputs -> EvaluationCtx ConsensusPayload +genesisEvaluationCtx' cid plo = EvaluationCtx { _evaluationCtxParentCreationTime = Parent $ implicitVersion ^?! versionGenesis . genesisTime . atChain cid , _evaluationCtxParentHash = genesisParentBlockHash cid - , _evaluationCtxParentHeight = Parent $ genesisHeight cid - , _evaluationCtxParentForkNumber = pact4ForkNumber + , _evaluationCtxParentHeight = Parent $ genesisHeight cid -- As an exception we assume that the (ghost) parent of Genesis has the same blockheight than Genesis + , _evaluationCtxParentForkNumber = Parent $ ForkNumber 0 -- should not be used , _evaluationCtxMinerReward = MinerReward 0 , _evaluationCtxPayload = ConsensusPayload { _consensusPayloadHash = genesisBlockPayloadHash cid , _consensusPayloadData = EncodedPayloadData . Chainweb.encodePayloadData . Chainweb.payloadWithOutputsToPayloadData - <$> _psGenesisPayload serviceEnv + <$> plo } } - where - cid = _chainId serviceEnv -- State from a block in progress, which is used to extend blocks after -- running their payloads. diff --git a/src/Chainweb/Pact/Validations.hs b/src/Chainweb/Pact/Validations.hs index 772be6236c..687ab7d838 100644 --- a/src/Chainweb/Pact/Validations.hs +++ b/src/Chainweb/Pact/Validations.hs @@ -72,9 +72,8 @@ assertPreflightMetadata env cmd@(Pact.Command pay sigs hsh) blockCtx sigVerify = let cid = view chainId env -- note that we use the maximum legal block gas limit here. if no miner is -- willing to accept this tx, it still may not make it on the chain. - let mbgl = maxBlockGasLimit - (_bctxParentForkNumber blockCtx) - (_bctxCurrentBlockHeight blockCtx) + let mbgl = maxBlockGasLimit (blockCtx ^. bctxParentForkNumber. _Parent) + (_bctxCurrentBlockHeight blockCtx) let Pact.PublicMeta pcid _ gl gp _ _ = Pact._pMeta pay nid = Pact._pNetworkId pay diff --git a/src/Chainweb/PayloadProvider.hs b/src/Chainweb/PayloadProvider.hs index e3cff16f34..28e3d1c2af 100644 --- a/src/Chainweb/PayloadProvider.hs +++ b/src/Chainweb/PayloadProvider.hs @@ -291,7 +291,7 @@ data EvaluationCtx p = EvaluationCtx -- ^ Block hash of the parent block. , _evaluationCtxParentHeight :: !(Parent BlockHeight) -- ^ Block height of the parent block. - , _evaluationCtxParentForkNumber :: !ForkNumber + , _evaluationCtxParentForkNumber :: !(Parent ForkNumber) -- ^ Fork number of the parent block. , _evaluationCtxMinerReward :: !MinerReward -- ^ The miner reward that is assigned to the miner of the block. Miner @@ -376,6 +376,8 @@ data NewBlockCtx = NewBlockCtx -- ^ the miner reward for the new block. , _newBlockCtxParentCreationTime :: !(Parent BlockCreationTime) -- ^ the creation time of the block on which the new is created. + , _newBlockCtxParentForkNumber:: !(Parent ForkNumber) + -- ^ The fork number of the parent } deriving (Show, Eq, Ord) @@ -389,7 +391,7 @@ blockHeaderToEvaluationCtx (Parent ph) = EvaluationCtx { _evaluationCtxParentCreationTime = Parent $ view blockCreationTime ph , _evaluationCtxParentHash = Parent $ view blockHash ph , _evaluationCtxParentHeight = parentHeight - , _evaluationCtxParentForkNumber = view blockForkNumber ph + , _evaluationCtxParentForkNumber = Parent $ view blockForkNumber ph , _evaluationCtxMinerReward = blockMinerReward height , _evaluationCtxPayload = () } diff --git a/src/Chainweb/PayloadProvider/Pact/BlockHistoryMigration.hs b/src/Chainweb/PayloadProvider/Pact/BlockHistoryMigration.hs index 2dd6a07aac..4889c92b3f 100644 --- a/src/Chainweb/PayloadProvider/Pact/BlockHistoryMigration.hs +++ b/src/Chainweb/PayloadProvider/Pact/BlockHistoryMigration.hs @@ -16,7 +16,7 @@ import Chainweb.ChainId import Chainweb.Logger import Chainweb.Pact.Backend.Types (SQLiteEnv) import Chainweb.TreeDB -import Chainweb.Version (HasVersion) +import Chainweb.Version (HasVersion, genesisTime, implicitVersion, versionGenesis) import Control.Lens import Control.Monad.IO.Class import System.Logger qualified as L @@ -88,25 +88,24 @@ migrateBlockHistoryTable logger cid sdb bhdb cleanup rr@[SInt bh, SInt _, SBlob h] -> do let rowBlockHeight = fromIntegral bh rowBlockHash <- runGetS decodeBlockHash h - lookupRanked bhdb rowBlockHeight rowBlockHash >>= \case - Nothing -> do - -- TODO: stop when there's a missing header? - -- error if there's a gap? - logFunctionText logger Warn - $ "BlockHeader Entry missing for " - <> "blockHeight=" - <> sshow rowBlockHeight - <> ", blockHash=" - <> sshow rowBlockHash - return Nothing - Just blockHeader -> do - let bph = view blockPayloadHash blockHeader - enc = runPutS $ encodeBlockPayloadHash bph - - throwOnDbError $ exec' sdb - "INSERT INTO BlockHistory2 (blockheight, endingtxid, hash, payloadhash) VALUES (?, ?, ?, ?)" - $ rr ++ [SBlob enc] - return (Just blockHeader) + + withLookupRanked rowBlockHeight rowBlockHash \blockHeader -> do + let bph = view blockPayloadHash blockHeader + enc = runPutS $ encodeBlockPayloadHash bph + + maybeEnvData <- if isGenesisBlockHeader blockHeader + then pure $ Just $ runPutS $ encodeHistoryCtx (Parent $ blockHeader ^. blockForkNumber) + (Parent $ implicitVersion ^?! versionGenesis . genesisTime . atChain cid) + + else withLookupRanked (rowBlockHeight - 1) (unwrapParent $ blockHeader ^. blockParent) $ \parentHeader -> + pure $ Just $ runPutS $ encodeHistoryCtx (Parent $ parentHeader ^. blockForkNumber) + (Parent $ parentHeader ^. blockCreationTime) + + case maybeEnvData of + Nothing -> return Nothing + Just envData -> (throwOnDbError $ exec' sdb "INSERT INTO BlockHistory2 (blockheight, endingtxid, hash, payloadhash, ctxData) VALUES (?, ?, ?, ?, ?)" + $ rr ++ [SBlob enc, SBlob envData]) >> return (Just blockHeader) + _ -> error "unexpected row shape" ) & S.last @@ -144,6 +143,20 @@ migrateBlockHistoryTable logger cid sdb bhdb cleanup [[SInt n]] -> pure n _ -> error "unexpected row shape" + withLookupRanked bHeight bHash f = lookupRanked bhdb bHeight bHash >>= \case + Nothing -> do + -- TODO: stop when there's a missing header? + -- error if there's a gap? + logFunctionText logger Warn + $ "BlockHeader Entry missing for " + <> "blockHeight=" + <> sshow bHeight + <> ", blockHash=" + <> sshow bHash + return Nothing + Just bh -> f bh + + -- | Checks whether the @BlockHistory@ table is (still) existing -- and therefore requires a data migration step. diff --git a/src/Chainweb/Sync/WebBlockHeaderStore.hs b/src/Chainweb/Sync/WebBlockHeaderStore.hs index c093e7a94a..122e7df8e3 100644 --- a/src/Chainweb/Sync/WebBlockHeaderStore.hs +++ b/src/Chainweb/Sync/WebBlockHeaderStore.hs @@ -293,6 +293,7 @@ forkInfoForHeader wdb hdr pldData parentHdr createPayload | createPayload = Just NewBlockCtx { _newBlockCtxMinerReward = blockMinerReward (height + 1) , _newBlockCtxParentCreationTime = Parent $ view blockCreationTime hdr + , _newBlockCtxParentForkNumber = Parent $ view blockForkNumber hdr } | otherwise = Nothing height = view blockHeight hdr diff --git a/test/unit/Chainweb/Test/Mempool.hs b/test/unit/Chainweb/Test/Mempool.hs index ce078b880a..5afa449388 100644 --- a/test/unit/Chainweb/Test/Mempool.hs +++ b/test/unit/Chainweb/Test/Mempool.hs @@ -205,7 +205,7 @@ propBadlistPreblock (txs, badTxs) _ mempool = runExceptT $ do -- once we call mempoolGetBlock, the bad txs should be badlisted liftIO $ void $ mempoolGetBlock mempool mockBlockFill preblockCheck - (EvaluationCtx (Parent $ BlockCreationTime Time.epoch) (Parent nullBlockHash) (Parent 1) 0 (MinerReward 0) ()) + (EvaluationCtx (Parent $ BlockCreationTime Time.epoch) (Parent nullBlockHash) (Parent 1) (Parent 0) (MinerReward 0) ()) liftIO (lookup txs) >>= V.mapM_ lookupIsPending liftIO (lookup badTxs) >>= V.mapM_ lookupIsMissing liftIO $ insert badTxs @@ -250,7 +250,7 @@ propAddToBadList tx _ mempool = runExceptT $ do lookup = mempoolLookup mempool . V.fromList . map hash getBlock = liftIO $ V.toList <$> mempoolGetBlock mempool mockBlockFill noopMempoolPreBlockCheck - (EvaluationCtx (Parent $ BlockCreationTime Time.epoch) (Parent nullBlockHash) (Parent 1) 0 (MinerReward 0) ()) + (EvaluationCtx (Parent $ BlockCreationTime Time.epoch) (Parent nullBlockHash) (Parent 1) (Parent 0) (MinerReward 0) ()) -- TODO Does this need to be updated? propPreInsert @@ -308,7 +308,7 @@ propTrivial txs _ mempool = runExceptT $ do lookup = mempoolLookup mempool . V.fromList . map hash getBlock = mempoolGetBlock mempool mockBlockFill noopMempoolPreBlockCheck - (EvaluationCtx (Parent $ BlockCreationTime Time.epoch) (Parent nullBlockHash) (Parent 0) 0 (MinerReward 0) ()) + (EvaluationCtx (Parent $ BlockCreationTime Time.epoch) (Parent nullBlockHash) (Parent 0) (Parent 0) (MinerReward 0) ()) onFees x = (Down (mockGasPrice x)) diff --git a/test/unit/Chainweb/Test/Pact/CheckpointerTest.hs b/test/unit/Chainweb/Test/Pact/CheckpointerTest.hs index 24e84fabee..ef0ac4d940 100644 --- a/test/unit/Chainweb/Test/Pact/CheckpointerTest.hs +++ b/test/unit/Chainweb/Test/Pact/CheckpointerTest.hs @@ -45,6 +45,10 @@ import Chainweb.BlockHash import Chainweb.BlockHeader import Chainweb.ForkState (pact4ForkNumber) import Chainweb.Graph (singletonChainGraph) +import Chainweb.BlockCreationTime +import Chainweb.Time +import Chainweb.Ranked +import Chainweb.Utils import Chainweb.MerkleLogHash import Chainweb.MerkleUniverse (ChainwebMerkleHashAlgorithm) import Chainweb.Pact.Backend.ChainwebPactDb qualified as ChainwebPactDb @@ -56,14 +60,18 @@ import Chainweb.PayloadProvider import Chainweb.Test.Pact.Utils import Chainweb.Test.TestVersions import Chainweb.Test.Utils -import Chainweb.Utils import Chainweb.Utils.Serialization (runGetS, runPutS) import Chainweb.Version import Chainweb.MinerReward import Control.Monad.State.Strict -import Chainweb.Pact.Payload qualified as Chainweb import Chainweb.Pact.Utils (emptyPayload) +fakeTime :: Time Micros +fakeTime = fromJuste $ parseTimeMicros "2026-11-11T08:03:55" + +fakeParentCreationTime:: Parent BlockCreationTime +fakeParentCreationTime = Parent $ BlockCreationTime $ fakeTime + -- | A @DbAction f@ is a description of some action on the database together with an f-full of results for it. type DbValue = Integer data DbAction f @@ -201,37 +209,31 @@ runBlocks sql rootBlockCtx blks = where loop parent (block:blocks) = withVersion testVer $ do logger <- getTestLogger - fakeParentCreationTime <- Checkpointer.mkFakeParentCreationTime + + let blockCtx = BlockCtx { + _bctxParentCreationTime = fakeParentCreationTime + , _bctxParentHash = _ranked <$> parent + , _bctxParentHeight = _rankedHeight <$> parent + , _bctxParentForkNumber = Parent pact4ForkNumber + , _bctxChainId = cid + , _bctxMinerReward = blockMinerReward (childBlockHeight cid parent) + } + (fakeBlockInfo, block', _finalBlockHandle) <- (throwIfNoHistory =<<) $ - Checkpointer.readFrom logger cid sql fakeParentCreationTime parent $ + Checkpointer.readFrom logger cid sql blockCtx $ Checkpointer.readPact5 "unexpected pact 5" $ executeBlockTransaction parent block - let childBlockCtx = BlockCtx - { _bctxParentCreationTime = fakeParentCreationTime - , _bctxParentHash = Parent $ fst fakeBlockInfo - , _bctxParentHeight = Parent $ childBlockHeight cid parent - , _bctxParentForkNumber = pact4ForkNumber - , _bctxChainId = cid - , _bctxMinerReward = blockMinerReward (childBlockHeight cid parent) - } - let parentBlockCtx = BlockCtx - { _bctxParentCreationTime = fakeParentCreationTime - , _bctxParentHash = _rankedBlockHashHash <$> parent - , _bctxParentHeight = _rankedBlockHashHeight <$> parent - , _bctxParentForkNumber = pact4ForkNumber - , _bctxChainId = cid - , _bctxMinerReward = blockMinerReward (unwrapParent $ _rankedBlockHashHeight <$> parent) - } - _ <- Checkpointer.restoreAndSave logger cid sql (parentBlockCtx ^. to _bctxParentRankedBlockHash) - (NE.singleton $ Checkpointer.Pact5RunnableBlock $ \chainwebPactDb -> do + + _ <- Checkpointer.restoreAndSave logger cid sql parent + (NE.singleton $ Checkpointer.Pact5RunnableBlock blockCtx $ \chainwebPactDb -> do blockHandle <- get (fakeBlockInfo', _blk, finalBlockHandle) <- - liftIO $ executeBlockTransaction parent block (BlockEnv parentBlockCtx chainwebPactDb) blockHandle + liftIO $ executeBlockTransaction parent block (BlockEnv blockCtx chainwebPactDb) blockHandle put finalBlockHandle liftIO $ fakeBlockInfo' & P.equals fakeBlockInfo return ((), fakeBlockInfo) ) - ((parentBlockCtx, fakeBlockInfo, block') :) <$> loop (_bctxParentRankedBlockHash childBlockCtx) blocks + ((blockCtx, fakeBlockInfo, block') :) <$> loop (Parent $ Ranked (childBlockHeight cid parent) (fst fakeBlockInfo)) blocks loop _ [] = return [] executeBlockTransaction :: Traversable t @@ -263,9 +265,8 @@ runBlk txdb ph blk = do -- is consistent with us executing that block with `readFrom` assertBlock :: SQLiteEnv -> BlockCtx -> (BlockHash, BlockPayloadHash) -> DbBlock Identity -> IO () assertBlock sql blockCtx expectedBlockInfo blk = withVersion testVer $ do - fakeNewBlockCtx <- Checkpointer.mkFakeParentCreationTime logger <- getTestLogger - hist <- Checkpointer.readFrom logger cid sql fakeNewBlockCtx (_bctxParentRankedBlockHash blockCtx) $ + hist <- Checkpointer.readFrom logger cid sql blockCtx $ Checkpointer.readPact5 "unexpected Pact 4" $ \blockEnv startHandle -> do () <- flip evalStateT startHandle $ doChainwebPactDbTransaction (_psBlockDbEnv blockEnv) Nothing $ \txdb _spv -> do _ <- ignoreGas noInfo $ _pdbBeginTx txdb Transactional @@ -297,9 +298,8 @@ tests = testGroup "Pact5 Checkpointer tests" ChainwebPactDb.initSchema sql Checkpointer.setConsensusState sql $ genesisConsensusState cid logger <- getTestLogger - fakeNewBlockCtx <- Checkpointer.mkFakeParentCreationTime () <- (throwIfNoHistory =<<) $ - Checkpointer.readFrom logger cid sql fakeNewBlockCtx genesisParentRanked + Checkpointer.readFrom logger cid sql genesisBlockCtx $ Checkpointer.readPact5 "unexpected Pact 4" $ \db blockHandle -> do flip evalStateT blockHandle $ doChainwebPactDbTransaction (_psBlockDbEnv db) Nothing $ \txdb _spv -> @@ -314,9 +314,9 @@ tests = testGroup "Pact5 Checkpointer tests" Checkpointer.setConsensusState sql $ genesisConsensusState cid logger <- getTestLogger -- extend this empty chain with the genesis block - _ <- Checkpointer.restoreAndSave logger cid sql (_evaluationCtxRankedParentHash (genesisEvalCtx cid)) + _ <- Checkpointer.restoreAndSave logger cid sql (_evaluationCtxRankedParentHash genesisEvalCtx) $ NE.singleton - $ Checkpointer.Pact5RunnableBlock $ \_ -> + $ Checkpointer.Pact5RunnableBlock genesisBlockCtx $ \_ -> return ((), (view blockHash (genesisBlockHeader cid), genesisBlockPayloadHash cid)) handle @_ @SomeException (\ex -> putStrLn (displayException ex) >> throw ex) @@ -334,13 +334,13 @@ tests = testGroup "Pact5 Checkpointer tests" ChainwebPactDb.initSchema sql _ <- Checkpointer.restoreAndSave logger cid sql (genesisRankedParentBlockHash cid) $ NE.singleton - $ Checkpointer.Pact5RunnableBlock $ \_ -> + $ Checkpointer.Pact5RunnableBlock genesisBlockCtx $ \_ -> return ((), (view blockHash gh, view blockPayloadHash gh)) let coinTable = TableName "coin-table" (ModuleName "coin" Nothing) let domain = DUserTables coinTable blockInfo NE.:| [] <- Checkpointer.restoreAndSave logger cid sql (Parent $ view rankedBlockHash gh) $ NE.singleton - $ Checkpointer.Pact5RunnableBlock $ \chainwebPactDb -> + $ Checkpointer.Pact5RunnableBlock genesisBlockCtx $ \chainwebPactDb -> doChainwebPactDbTransaction chainwebPactDb Nothing $ \txdb _ -> do ((), blockInfo) <- runBlk txdb (Parent (view rankedBlockHash gh)) $ do ignoreGas noInfo $ _pdbCreateUserTable txdb @@ -354,7 +354,7 @@ tests = testGroup "Pact5 Checkpointer tests" _ <- Checkpointer.restoreAndSave logger cid sql (Parent rbh) $ NE.singleton - $ Checkpointer.Pact5RunnableBlock $ \db -> + $ Checkpointer.Pact5RunnableBlock genesisBlockCtx $ \db -> -- TODO Check again here doChainwebPactDbTransaction db Nothing $ \txdb _ -> runBlk txdb (Parent rbh) $ do _ <- ignoreGas noInfo $ _pdbRead txdb @@ -365,22 +365,6 @@ tests = testGroup "Pact5 Checkpointer tests" return () ] - where - -- note that this the evaluation ctx for the genesis header, *not* for the header *after* - genesisEvalCtx c = withVersion testVer $ EvaluationCtx - { _evaluationCtxParentCreationTime = Parent $ implicitVersion ^?! versionGenesis . genesisTime . atChain c - , _evaluationCtxParentHash = genesisParentBlockHash c - , _evaluationCtxParentHeight = Parent $ genesisHeight c - , _evaluationCtxParentForkNumber = pact4ForkNumber - -- should not be used - , _evaluationCtxMinerReward = MinerReward 0 - , _evaluationCtxPayload = ConsensusPayload - { _consensusPayloadHash = genesisBlockPayloadHash c - , _consensusPayloadData = Just $ EncodedPayloadData $ Chainweb.encodePayloadData $ - Chainweb.payloadWithOutputsToPayloadData emptyPayload - } - } - testVer :: ChainwebVersion testVer = checkpointerTestVersion singletonChainGraph @@ -391,11 +375,12 @@ cid = unsafeChainId 0 gh :: BlockHeader gh = withVersion testVer $ genesisBlockHeader cid -genesisParentRanked :: Parent RankedBlockHash -genesisParentRanked = withVersion testVer $ - Parent $ RankedBlockHash - (genesisHeight cid) - (unwrapParent $ genesisParentBlockHash cid) +genesisEvalCtx :: EvaluationCtx ConsensusPayload +genesisEvalCtx = withVersion testVer $ genesisEvaluationCtx' cid (Just emptyPayload) + +genesisBlockCtx :: BlockCtx +genesisBlockCtx = withVersion testVer $ blockCtxOfEvaluationCtx cid genesisEvalCtx + instance (forall a. Show a => Show (f a)) => Show (DbAction f) where showsPrec n (DbRead tn k v) = showParen (n > 10) $ diff --git a/test/unit/Chainweb/Test/Pact/PactServiceTest.hs b/test/unit/Chainweb/Test/Pact/PactServiceTest.hs index 55615da1c2..2c1e16fc54 100644 --- a/test/unit/Chainweb/Test/Pact/PactServiceTest.hs +++ b/test/unit/Chainweb/Test/Pact/PactServiceTest.hs @@ -117,6 +117,12 @@ mkFixtureWith pactServiceConfig baseRdb = withVersion v $ do -- So by running an empty block with the creationTime set to the current time, we get these goals to align -- for future blocks we run. _ <- liftIO $ advanceAllChains fixture $ onChains [] + + -- Pre-Insert-Check is not aware of the Parent Creation Time, so pre-insert-check checks transaction time + -- relative to the 'future Grand-Paent" + -- Genesis being mined at Epoch, we have here to mine a second block here to not allow the mempool to do + -- its checks relative to a valid CreationTime (ie Block 1) + _ <- liftIO $ advanceAllChains fixture $ onChains [] return fixture genesisPayload :: ChainId -> PayloadWithOutputs @@ -603,23 +609,22 @@ makeEmptyBlock :: HasVersion => Fixture -> Parent BlockHeader -> IO BlockInProgr makeEmptyBlock Fixture{..} ph = do Pool.withResource (_psReadSqlPool serviceEnv) $ \roSql -> do (throwIfNoHistory =<<) $ - Checkpointer.readFrom _fixtureLogger cid roSql (view blockCreationTime <$> ph) (view rankedBlockHash <$> ph) $ + Checkpointer.readFrom _fixtureLogger cid roSql blockCtx $ Checkpointer.readPact5 "unexpected Pact 4" $ \blockEnv initialBlockHandle -> PactService.makeEmptyBlock _fixtureLogger serviceEnv blockEnv initialBlockHandle where cid = _chainId ph serviceEnv = _fixturePacts ^?! atChain cid + blockCtx = blockCtxOfEvaluationCtx cid $ blockHeaderToEvaluationCtx ph continueBlock :: HasVersion => Fixture -> BlockInProgress -> IO BlockInProgress continueBlock Fixture{..} bip = do Pool.withResource (_psReadSqlPool serviceEnv) $ \roSql -> do (throwIfNoHistory =<<) $ - Checkpointer.readFrom _fixtureLogger cid roSql parentCreationTime parentRankedHash $ + Checkpointer.readFrom _fixtureLogger cid roSql (bip ^. blockInProgressBlockCtx) $ Checkpointer.readPact5 "unexpected Pact 4" $ \blockEnv _initialBlockHandle -> PactService.continueBlock _fixtureLogger serviceEnv (_psBlockDbEnv blockEnv) bip where - parentCreationTime = (_bctxParentCreationTime $ _blockInProgressBlockCtx bip) - parentRankedHash = (_bctxParentRankedBlockHash $ _blockInProgressBlockCtx bip) cid = _chainId bip serviceEnv = _fixturePacts ^?! atChain cid diff --git a/test/unit/Chainweb/Test/Pact/RemotePactTest.hs b/test/unit/Chainweb/Test/Pact/RemotePactTest.hs index e011953516..3c0f1380da 100644 --- a/test/unit/Chainweb/Test/Pact/RemotePactTest.hs +++ b/test/unit/Chainweb/Test/Pact/RemotePactTest.hs @@ -54,7 +54,7 @@ import Chainweb.Pact.Types import Chainweb.RestAPI.Utils (someServerApplication) import Chainweb.Storage.Table.RocksDB import Chainweb.Test.Pact.CmdBuilder -import Chainweb.Test.Pact.CutFixture (advanceAllChains, advanceAllChains_, advanceToForkHeight) +import Chainweb.Test.Pact.CutFixture (advanceAllChains, advanceAllChains_) import Chainweb.Test.Pact.CutFixture qualified as CutFixture import Chainweb.Test.Pact.Utils import Chainweb.Test.TestVersions @@ -911,7 +911,7 @@ localTests baseRdb = let expectation = P.checkAll [ P.fun _crResult ? P.match _PactResultOk ? P.equals (PInteger 1) , P.fun _crMetaData ? P.match _Just ? P.match A._Object ? P.alignExact ? A.KeyMap.fromList - [ ("blockHeight", P.equals ? A.Number 2) + [ ("blockHeight", P.equals ? A.Number 3) , ("blockTime", P.match A._Number P.succeed) , ("prevBlockHash", P.match A._String P.succeed) , ("publicMeta", P.match A._Object ? P.alignExact ? A.KeyMap.fromList @@ -1078,7 +1078,7 @@ localTests baseRdb = >>= P.match _LocalResultLegacy ? P.checkAll [ hasBalance ? P.lt startBalance - , hasBlockHeight (P.equals 3) + , hasBlockHeight (P.equals 4) ] buildTextCmd (defaultCmd cid & set cbRPC (mkExec' "(coin.details 'sender00)")) @@ -1086,7 +1086,7 @@ localTests baseRdb = >>= P.match _LocalResultLegacy ? P.checkAll [ hasBalance ? P.lt startBalance - , hasBlockHeight (P.equals 3) + , hasBlockHeight (P.equals 4) ] buildTextCmd (defaultCmd cid & set cbRPC (mkExec' "(coin.details 'sender00)")) @@ -1094,7 +1094,7 @@ localTests baseRdb = >>= P.match _LocalResultLegacy ? P.checkAll [ hasBalance ? P.equals startBalance - , hasBlockHeight (P.equals 2) + , hasBlockHeight (P.equals 3) ] buildTextCmd (defaultCmd cid & set cbRPC (mkExec' "(coin.details 'sender00)")) @@ -1102,11 +1102,11 @@ localTests baseRdb = >>= P.match _LocalResultLegacy ? P.checkAll [ hasBalance ? P.equals startBalance - , hasBlockHeight (P.equals 1) + , hasBlockHeight (P.equals 2) ] buildTextCmd (defaultCmd cid & set cbRPC (mkExec' "(coin.details 'sender00)")) - >>= local fx cid Nothing Nothing (Just (RewindDepth 3)) + >>= local fx cid Nothing Nothing (Just (RewindDepth 4)) & P.throws ? P.match _FailureResponse ? P.fun responseBody @@ -1157,7 +1157,7 @@ pollingMetadataTest baseRdb _step = withVersion v $ runResourceT $ do P.alignExact [ P.match _Just ? P.fun _crMetaData ? P.match _Just ? P.match A._Object ? P.alignExact ? A.KeyMap.fromList [ ("blockHash", P.match (A._String . b64UrlNoPaddingPrism) P.succeed) - , ("blockHeight", P.equals (A.Number 2)) + , ("blockHeight", P.equals (A.Number 3)) , ("blockPayloadHash", P.match (A._String . b64UrlNoPaddingPrism) P.succeed) ] ] @@ -1286,6 +1286,9 @@ mkFixture baseRdb = do , baseUrlPath = "" } + -- Mine at least one block to be sure to work on an up-to-date chain, for Tx TLL and Creation times verifications. + liftIO $ advanceAllChains_ fx + return $ Fixture { _cutFixture = fx , _serviceClientEnv = serviceClientEnv diff --git a/test/unit/Chainweb/Test/Pact/TransactionExecTest.hs b/test/unit/Chainweb/Test/Pact/TransactionExecTest.hs index 09682fd775..ecab776461 100644 --- a/test/unit/Chainweb/Test/Pact/TransactionExecTest.hs +++ b/test/unit/Chainweb/Test/Pact/TransactionExecTest.hs @@ -24,12 +24,14 @@ import Chainweb.Miner.Pact (Miner(..), MinerId(..), MinerGuard(..), noMiner) import Chainweb.Pact.Backend.InMemDb qualified as InMemDb import Chainweb.Pact.Backend.Types import Chainweb.Pact.PactService (initialPayloadState, withPactService) -import Chainweb.Pact.PactService.Checkpointer (readFrom, mkFakeParentCreationTime) +import Chainweb.Pact.PactService.Checkpointer (readFrom) import Chainweb.Pact.PactService.Checkpointer qualified as Checkpointer import Chainweb.Pact.Transaction import Chainweb.Pact.TransactionExec import Chainweb.Pact.Types import Chainweb.Parent +import Chainweb.PayloadProvider +import Chainweb.Utils import Chainweb.Storage.Table.RocksDB import Chainweb.Test.Cut.TestBlockDb (TestBlockDb (_bdbPayloadDb), mkTestBlockDb) import Chainweb.Test.Pact.CmdBuilder @@ -112,10 +114,8 @@ readFromAfterGenesis rdb act = runResourceT $ do serviceEnv <- withPactService cid Nothing mempty logger Nothing (_bdbPayloadDb tdb) readPool writeSql defaultPactServiceConfig (GenesisPayload PIN0.payloadBlock) liftIO $ do initialPayloadState logger serviceEnv - fakeParentCreationTime <- mkFakeParentCreationTime throwIfNoHistory =<< - readFrom logger cid writeSql fakeParentCreationTime - (Parent (gh cid ^. rankedBlockHash)) + readFrom logger cid writeSql (blockCtxOfEvaluationCtx cid $ blockHeaderToEvaluationCtx $ Parent $ gh cid) (Checkpointer.readPact5 "unexpected Pact 4" $ act) buyGasShouldTakeGasTokensFromTheTransactionSender :: RocksDb -> IO ()