boost::optional<Answer> YacProposalStorage::findRejectProof() {
        auto max_vote = std::max_element(block_storages_.begin(),
                                         block_storages_.end(),
                                         [](auto &left, auto &right) {
                                           return left.getNumberOfVotes()
                                               < right.getNumberOfVotes();
                                         })
                            ->getNumberOfVotes();

        auto all_votes =
            std::accumulate(block_storages_.begin(),
                            block_storages_.end(),
                            0ull,
                            [](auto &acc, auto &storage) {
                              return acc + storage.getNumberOfVotes();
                            });

        auto is_reject = supermajority_checker_->hasReject(
            max_vote, all_votes, peers_in_round_);

        if (is_reject) {
          std::vector<VoteMessage> result;
          result.reserve(all_votes);
          std::for_each(block_storages_.begin(),
                        block_storages_.end(),
                        [&result](auto &storage) {
                          auto votes_from_block_storage = storage.getVotes();
                          std::move(votes_from_block_storage.begin(),
                                    votes_from_block_storage.end(),
                                    std::back_inserter(result));
                        });

          return Answer(RejectMessage(std::move(result)));
        }

        return boost::none;
      }
示例#2
0
// Start sending a file or a JSON response.
void Webserver::SendFile(const char* nameOfFileToSend)
{
	if (StringEquals(nameOfFileToSend, "/"))
	{
		nameOfFileToSend = INDEX_PAGE;
	}
	FileStore *fileToSend = platform->GetFileStore(platform->GetWebDir(), nameOfFileToSend, false);
	if (fileToSend == NULL)
	{
		nameOfFileToSend = FOUR04_FILE;
		fileToSend = platform->GetFileStore(platform->GetWebDir(), nameOfFileToSend, false);
		if (fileToSend == NULL)
		{
			RejectMessage("not found", 404);
			return;
		}
	}

	Network *net = reprap.GetNetwork();
	RequestState *req = net->GetRequest(NULL);
	req->Write("HTTP/1.1 200 OK\n");

	const char* contentType;
	bool zip = false;
	if (StringEndsWith(nameOfFileToSend, ".png"))
	{
		contentType = "image/png";
	}
	else if (StringEndsWith(nameOfFileToSend, ".ico"))
	{
		contentType = "image/x-icon";
	}
	else if (StringEndsWith(nameOfFileToSend, ".js"))
	{
		contentType = "application/javascript";
	}
	else if (StringEndsWith(nameOfFileToSend, ".css"))
	{
		contentType = "text/css";
	}
	else if (StringEndsWith(nameOfFileToSend, ".htm") || StringEndsWith(nameOfFileToSend, ".html"))
	{
		contentType = "text/html";
	}
	else if (StringEndsWith(nameOfFileToSend, ".zip"))
	{
		contentType = "application/zip";
		zip = true;
	}
	else
	{
		contentType = "application/octet-stream";
	}
	req->Printf("Content-Type: %s\n", contentType);

	if (zip && fileToSend != NULL)
	{
		req->Write("Content-Encoding: gzip\n");
		req->Printf("Content-Length: %lu", fileToSend->Length());
	}

	req->Write("Connection: close\n\n");
	net->SendAndClose(fileToSend);
}