Skip to content

Commit

Permalink
log: log rewinds in execValidateBlock instead of blocks played
Browse files Browse the repository at this point in the history
Change-Id: Id2d597d6e72b0bbd6825184b9dd122261502d035
  • Loading branch information
edmundnoble committed May 30, 2024
1 parent aeef468 commit 115ef8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased (2024-XX-YY)

Changes:
- Log rewound blocks during catchup instead of played blocks (#xxxx)


Internal Changes:
Expand Down
32 changes: 16 additions & 16 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -841,15 +841,14 @@ execValidateBlock memPoolAccess headerToValidate payloadToValidate = pactLabel "
-- check that we don't exceed the rewind limit. for the purpose
-- of this check, the genesis block and the genesis parent
-- have the same height.
do
let !currHeight = maybe (genesisHeight v cid) _blockHeight currHeader
let !ancestorHeight = maybe (genesisHeight v cid) _blockHeight commonAncestor
let !rewindLimitSatisfied = ancestorHeight + fromIntegral reorgLimit >= currHeight
unless rewindLimitSatisfied $
throwM $ RewindLimitExceeded
(RewindLimit reorgLimit)
currHeader
commonAncestor
let !currHeight = maybe (genesisHeight v cid) _blockHeight currHeader
let !ancestorHeight = maybe (genesisHeight v cid) _blockHeight commonAncestor
let !rewindLimitSatisfied = ancestorHeight + fromIntegral reorgLimit >= currHeight
unless rewindLimitSatisfied $
throwM $ RewindLimitExceeded
(RewindLimit reorgLimit)
currHeader
commonAncestor
-- get all blocks on the fork we're going to play, up to the parent
-- of the block we're validating
let withForkBlockStream kont = case parentOfHeaderToValidate of
Expand All @@ -860,7 +859,7 @@ execValidateBlock memPoolAccess headerToValidate payloadToValidate = pactLabel "
let forkStartHeight = maybe (genesisHeight v cid) (succ . _blockHeight) commonAncestor
in getBranchIncreasing bhdb parentHeaderOfHeaderToValidate (fromIntegral forkStartHeight) kont

((), T2 results (Sum numForkBlocksPlayed)) <-
((), results) <-
withPactState $ \runPact ->
withForkBlockStream $ \forkBlockHeaders -> do

Expand All @@ -874,14 +873,14 @@ execValidateBlock memPoolAccess headerToValidate payloadToValidate = pactLabel "
<> ". Block: " <> encodeToText (ObjectEncoded forkBh)
Just x -> return $ payloadWithOutputsToPayloadData x
void $ execBlock forkBh (CheckablePayload payload)
return (T2 [] (Sum (1 :: Word)), forkBh)
return ([], forkBh)
) forkBlockHeaders

-- run the new block, the one we're validating, and
-- validate its hashes
let runThisBlock = Stream.yield $ do
!output <- execBlock headerToValidate payloadToValidate
return (T2 [output] (Sum (0 :: Word)), headerToValidate)
return ([output], headerToValidate)

-- here we rewind to the common ancestor block, run the
-- transactions in all of its child blocks until the parent
Expand All @@ -890,12 +889,13 @@ execValidateBlock memPoolAccess headerToValidate payloadToValidate = pactLabel "
runPact $ restoreAndSave
(ParentHeader <$> commonAncestor)
(runForkBlockHeaders >> runThisBlock)
let logPlayed =
-- we consider a fork of height 3 or more to be notable.
if numForkBlocksPlayed > 3
let logRewind =
-- we consider a fork of height more than 3 to be notable.
if ancestorHeight + 3 < currHeight
then logWarn
else logDebug
logPlayed $ "execValidateBlock: played " <> sshow numForkBlocksPlayed <> " fork blocks"
logRewind $
"execValidateBlock: rewound " <> sshow (currHeight - ancestorHeight) <> " blocks"
(totalGasUsed, result) <- case results of
[r] -> return r
_ -> throwM $ PactInternalError "execValidateBlock: wrong number of block results returned from _cpRestoreAndSave."
Expand Down

0 comments on commit 115ef8c

Please sign in to comment.