ErrorCode ThreadTransferHistory::validateCheckpoint()

in util/ThreadTransferHistory.cpp [136:179]


ErrorCode ThreadTransferHistory::validateCheckpoint(
    const Checkpoint &checkpoint, bool globalCheckpoint) {
  if (lastCheckpoint_ == nullptr) {
    return OK;
  }
  if (checkpoint.numBlocks < lastCheckpoint_->numBlocks) {
    WLOG(ERROR)
        << "Current checkpoint must be higher than previous checkpoint, "
           "Last checkpoint: "
        << *lastCheckpoint_ << ", Current checkpoint: " << checkpoint;
    return INVALID_CHECKPOINT;
  }
  if (checkpoint.numBlocks > lastCheckpoint_->numBlocks) {
    return OK;
  }
  bool noProgress = false;
  // numBlocks same
  if (checkpoint.lastBlockSeqId == lastCheckpoint_->lastBlockSeqId &&
      checkpoint.lastBlockOffset == lastCheckpoint_->lastBlockOffset) {
    // same block
    if (checkpoint.lastBlockReceivedBytes !=
        lastCheckpoint_->lastBlockReceivedBytes) {
      WLOG(ERROR) << "Current checkpoint has different received bytes, but all "
                     "other fields are same, Last checkpoint "
                  << *lastCheckpoint_ << ", Current checkpoint: " << checkpoint;
      return INVALID_CHECKPOINT;
    }
    noProgress = true;
  } else {
    // different block
    WDT_CHECK(checkpoint.lastBlockReceivedBytes >= 0);
    if (checkpoint.lastBlockReceivedBytes == 0) {
      noProgress = true;
    }
  }
  if (noProgress && !globalCheckpoint) {
    // we can get same global checkpoint multiple times, so no need to check for
    // progress
    WLOG(WARNING) << "No progress since last checkpoint, Last checkpoint: "
                  << *lastCheckpoint_ << ", Current checkpoint: " << checkpoint;
    return NO_PROGRESS;
  }
  return OK;
}