You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// During normal polling DB does not have any blocks after currentBlockNumber, so no reorg is possible. We can skip extra checks and just return currentBlock.
1036
-
returncurrentBlock, nil
1030
+
if!isReplay {
1031
+
// During normal polling DB does not have any blocks after currentBlockNumber, so no reorg is possible. We can skip extra checks and just return currentBlock.
1032
+
returncurrentBlock, nil
1033
+
}
1034
+
} else {
1035
+
// previous block is not present, it could only in case of initial poll or replay.
1036
+
// If that's not a replay, let's double-check that it's an initial poll by verifying that the DB is empty.
1037
+
if!isReplay {
1038
+
_, err=lp.orm.SelectLatestBlock(ctx)
1039
+
iferr==nil {
1040
+
err=fmt.Errorf("unexpected state: no previous block found for block number %d, but db is not empty", currentBlockNumber)
1041
+
lp.lggr.Criticalw("Invariant violation: expected to always have previous block except replay and first poll for a new chain", "currentBlockNumber", currentBlockNumber, "err", err)
1042
+
returnnil, err
1043
+
}
1044
+
1045
+
if!errors.Is(err, sql.ErrNoRows) {
1046
+
// Real DB error
1047
+
returnnil, fmt.Errorf("failed to check for first poll ever on new chain: %w", err)
1048
+
}
1049
+
1050
+
// This is expected, we just don't have any blocks in the DB yet.
1051
+
lp.lggr.Infow("Do not have previous block, first poll ever on new chain", "currentBlockNumber", currentBlockNumber)
1052
+
returncurrentBlock, nil
1053
+
}
1037
1054
}
1038
1055
1056
+
// In case of replay currentBlock may be in the middle of DB range, lets do additional checks to handle possible reorgs.
1039
1057
// Ensure that if DB contains current block it matches the current block from RPC.
// No reorg for current block, but during replay it's possible that current block is older than the latest block, let's check it too to avoid false positives on finality violation.
lp.lggr.Criticalw("Unexpected state. Expected at least one block to be present in the db when checking for reorg during replay, but got no rows", "currentBlockNumber", currentBlockNumber, "err", err1)
1068
+
latestBlockDB, err:=lp.orm.SelectLatestBlock(ctx)
1069
+
iferr!=nil {
1070
+
ifpkgerrors.Is(err, sql.ErrNoRows) {
1071
+
lp.lggr.Criticalw("Unexpected state. Expected at least one block to be present in the db when checking for reorg during replay, but got no rows", "currentBlockNumber", currentBlockNumber, "err", err)
1054
1072
}
1055
-
returnnil, pkgerrors.Wrap(err1, "unable to get latest block")
1073
+
returnnil, pkgerrors.Wrap(err, "unable to get latest block")
0 commit comments