Example #1
0
void
consensus::create(
  const string_ref cached_consensus_path,
  bool force_download
  )
{
  string consensus_content;
  bool have_valid_consensus = false;

  //
  // if no path to the cached consensus file
  // was provided, we have to download it.
  //
  if (cached_consensus_path.is_empty() || !io::file::exists(cached_consensus_path))
  {
    force_download = true;
  }

  while (!have_valid_consensus)
  {
    consensus_content = force_download
      ? download_from_random_authority("/tor/status-vote/current/consensus")
      : io::file::read_to_string(cached_consensus_path);

    parse_consensus(consensus_content);

    have_valid_consensus = _valid_until >= time::now();

    //
    // if the consensus is invalid, we have to download it anyway.
    //
    if (!have_valid_consensus)
    {
      force_download = true;
    }
  }

  //
  // save the consensus content, if the path was provided.
  //
  if (force_download && !cached_consensus_path.is_empty())
  {
    io::file::write_from_string(cached_consensus_path, consensus_content);
  }
}