Skip to content

Commit

Permalink
Drop NoRewind and pass current blockheight directly to lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgenii Akentev committed Jul 5, 2023
1 parent 2669712 commit 2a20114
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 37 deletions.
2 changes: 1 addition & 1 deletion bench/Chainweb/Pact/Backend/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
25 changes: 6 additions & 19 deletions src/Chainweb/Pact/Backend/RelationalCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact/Backend/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 :: !(
Expand Down
4 changes: 1 addition & 3 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Pact/PactService/ExecBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/Chainweb/Pact/RestAPI/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions src/Chainweb/Pact/Service/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 #-}

0 comments on commit 2a20114

Please sign in to comment.