diff --git a/bench/Chainweb/Pact/Backend/Bench.hs b/bench/Chainweb/Pact/Backend/Bench.hs index 67f0f101b5..ebd4d4b343 100644 --- a/bench/Chainweb/Pact/Backend/Bench.hs +++ b/bench/Chainweb/Pact/Backend/Bench.hs @@ -405,4 +405,4 @@ cpBenchLookupProcessedTx transactionCount cp = C.env (setup' cp) $ \ ~(ut) -> go CheckpointEnv{..} (NoopNFData _) = do _cpRestore _cpeCheckpointer (Just (BlockHeight 2, hash02)) >>= \case PactDbEnv' _ -> - _cpLookupProcessedTx _cpeCheckpointer Nothing (V.fromList [Pact.TypedHash "" | _ <- [1..transactionCount]]) + _cpLookupProcessedTx _cpeCheckpointer (BlockHeight 2) Nothing (V.fromList [Pact.TypedHash "" | _ <- [1..transactionCount]]) diff --git a/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs b/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs index f7cc85c80e..23ea1195c9 100644 --- a/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs +++ b/src/Chainweb/Pact/Backend/RelationalCheckpointer.hs @@ -296,28 +296,15 @@ doRegisterSuccessful :: Db -> PactHash -> IO () doRegisterSuccessful dbenv (TypedHash hash) = runBlockEnv dbenv (indexPactTransaction $ BS.fromShort hash) -doLookupSuccessful :: Db -> Maybe ConfirmationDepth -> V.Vector PactHash -> IO (HashMap.HashMap PactHash (T2 BlockHeight BlockHash)) -doLookupSuccessful dbenv confDepth hashes = runBlockEnv dbenv $ do +doLookupSuccessful :: Db -> BlockHeight -> Maybe ConfirmationDepth -> V.Vector PactHash -> IO (HashMap.HashMap PactHash (T2 BlockHeight BlockHash)) +doLookupSuccessful dbenv (BlockHeight currentHeight) confDepth hashes = runBlockEnv dbenv $ do withSavepoint DbTransaction $ do r <- callDb "doLookupSuccessful" $ \db -> do let - currentHeightQ = "SELECT blockheight FROM BlockHistory \ - \ ORDER BY blockheight DESC LIMIT 1" - - -- 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] - blockheight <- case confDepth of - Nothing -> pure Nothing - Just (ConfirmationDepth cd) -> do - currentHeight <- qry_ db currentHeightQ [RInt] - case currentHeight of - [[SInt bh]] -> pure $ Just (bh - fromIntegral cd) - _ -> fail "impossible" - - let - blockheightval = maybe [] (\bh -> [SInt bh]) blockheight - qvals = [ SBlob (BS.fromShort hash) | (TypedHash hash) <- V.toList hashes ] ++ blockheightval - + -- if there is a confirmation depth, we calculate the block height, + -- to look for the transactions in range [0, current block height - confirmation depth] + blockheight = maybe [] (\(ConfirmationDepth cd) -> [SInt $ fromIntegral currentHeight - fromIntegral cd]) confDepth + qvals = [ SBlob (BS.fromShort hash) | (TypedHash hash) <- V.toList hashes ] ++ blockheight qry db qtext qvals [RInt, RBlob] >>= mapM go return $ HashMap.fromList (zip (V.toList hashes) r) where diff --git a/src/Chainweb/Pact/Backend/Types.hs b/src/Chainweb/Pact/Backend/Types.hs index 94ed77ef52..73489efcbb 100644 --- a/src/Chainweb/Pact/Backend/Types.hs +++ b/src/Chainweb/Pact/Backend/Types.hs @@ -318,7 +318,7 @@ data Checkpointer = Checkpointer , _cpRegisterProcessedTx :: !(P.PactHash -> IO ()) , _cpLookupProcessedTx :: - !(Maybe ConfirmationDepth -> Vector P.PactHash -> IO (HashMap P.PactHash (T2 BlockHeight BlockHash))) + !(BlockHeight -> Maybe ConfirmationDepth -> Vector P.PactHash -> IO (HashMap P.PactHash (T2 BlockHeight BlockHash))) , _cpGetBlockHistory :: !( forall k v . (FromJSON v) => BlockHeader -> Domain k v -> IO BlockTxHistory) , _cpGetHistoricalLookup :: !( diff --git a/src/Chainweb/Pact/PactService.hs b/src/Chainweb/Pact/PactService.hs index db50582e52..14744022ea 100644 --- a/src/Chainweb/Pact/PactService.hs +++ b/src/Chainweb/Pact/PactService.hs @@ -802,11 +802,9 @@ execLookupPactTxs restorePoint confDepth txs | otherwise = go where go = getCheckpointer >>= \(!cp) -> case restorePoint of - NoRewind _ -> - liftIO $! _cpLookupProcessedTx cp confDepth txs DoRewind parent -> withDiscardedBatch $ do withCheckpointerRewind Nothing (Just $ ParentHeader parent) "lookupPactTxs" $ \_ -> - liftIO $ Discard <$> _cpLookupProcessedTx cp confDepth txs + liftIO $ Discard <$> _cpLookupProcessedTx cp (_blockHeight parent) confDepth txs -- | Modified table gas module with free module loads -- diff --git a/src/Chainweb/Pact/PactService/ExecBlock.hs b/src/Chainweb/Pact/PactService/ExecBlock.hs index 70674c6fb7..d2e5a7e365 100644 --- a/src/Chainweb/Pact/PactService/ExecBlock.hs +++ b/src/Chainweb/Pact/PactService/ExecBlock.hs @@ -226,7 +226,7 @@ validateChainwebTxs logger v cid cp txValidationTime bh txs doBuyGas checkUnique :: ChainwebTransaction -> IO (Either InsertError ChainwebTransaction) checkUnique t = do - found <- HashMap.lookup (P._cmdHash t) <$> _cpLookupProcessedTx cp Nothing (V.singleton $ P._cmdHash t) + found <- HashMap.lookup (P._cmdHash t) <$> _cpLookupProcessedTx cp bh Nothing (V.singleton $ P._cmdHash t) case found of Nothing -> pure $ Right t Just _ -> pure $ Left InsertErrorDuplicate diff --git a/src/Chainweb/Pact/RestAPI/Server.hs b/src/Chainweb/Pact/RestAPI/Server.hs index 831a7933a2..b7d549d3d2 100644 --- a/src/Chainweb/Pact/RestAPI/Server.hs +++ b/src/Chainweb/Pact/RestAPI/Server.hs @@ -408,7 +408,12 @@ spvHandler l cdb cid (SpvRequest rk (Pact.ChainId ptid)) = do liftIO $! logg (sshow ph) - T2 bhe _bha <- liftIO (_pactLookup pe (NoRewind cid) Nothing (pure ph)) >>= \case + -- get current best cut + cut <- liftIO $! CutDB._cut cdb + -- get leaf block header for our chain from current best cut + chainLeaf <- lookupCutM cid cut + + T2 bhe _bha <- liftIO (_pactLookup pe (DoRewind chainLeaf) Nothing (pure ph)) >>= \case Left e -> toErr $ "Internal error: transaction hash lookup failed: " <> sshow e Right v -> case HM.lookup ph v of @@ -487,7 +492,13 @@ spv2Handler l cdb cid r = case _spvSubjectIdType sid of proof f = SomePayloadProof <$> do validateRequestKey rk liftIO $! logg (sshow ph) - T2 bhe bha <- liftIO (_pactLookup pe (NoRewind cid) Nothing (pure ph)) >>= \case + + -- get current best cut + cut <- liftIO $! CutDB._cut cdb + -- get leaf block header for our chain from current best cut + chainLeaf <- lookupCutM cid cut + + T2 bhe bha <- liftIO (_pactLookup pe (DoRewind chainLeaf) Nothing (pure ph)) >>= \case Left e -> toErr $ "Internal error: transaction hash lookup failed: " <> sshow e Right v -> case HM.lookup ph v of diff --git a/src/Chainweb/Pact/Service/Types.hs b/src/Chainweb/Pact/Service/Types.hs index fe43d8eebc..64cfdfc76d 100644 --- a/src/Chainweb/Pact/Service/Types.hs +++ b/src/Chainweb/Pact/Service/Types.hs @@ -334,18 +334,11 @@ newtype TransactionOutputProofB64 = TransactionOutputProofB64 Text deriving stock (Eq, Show, Generic) deriving newtype (ToJSON, FromJSON) --- | This data type marks whether or not a particular header is --- expected to rewind or not. In the case of 'NoRewind', no --- header data is given, and a chain id is given instead for --- routing purposes --- -data Rewind - = DoRewind !BlockHeader - | NoRewind {-# UNPACK #-} !ChainId - deriving (Eq, Show) +-- | This data type marks the particular header for rewinding. +newtype Rewind = DoRewind BlockHeader + deriving (Eq, Show) instance HasChainId Rewind where _chainId = \case DoRewind !bh -> _chainId bh - NoRewind !cid -> cid {-# INLINE _chainId #-}