Skip to content

Commit

Permalink
Merge pull request #211 Avoid Dispatcher when using httplib to send txs
Browse files Browse the repository at this point in the history
Avoid Dispatcher when using httplib to send txs
  • Loading branch information
aivve authored Feb 9, 2023
2 parents e38b660 + 41a881d commit 8733b51
Show file tree
Hide file tree
Showing 13 changed files with 1,211 additions and 1,048 deletions.
147 changes: 73 additions & 74 deletions src/Common/DnsTools.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2018, Karbo developers
// Copyright (c) 2017-2023, Karbo developers
//
// This file is part of Karbo.
//
Expand Down Expand Up @@ -49,89 +49,88 @@ namespace Common {

#ifndef __ANDROID__

bool fetch_dns_txt(const std::string domain, std::vector<std::string>&records) {
bool fetch_dns_txt(const std::string domain, std::vector<std::string>&records) {
using namespace std;

#ifdef _WIN32
using namespace std;

#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Dnsapi.lib")

PDNS_RECORD pDnsRecord; //Pointer to DNS_RECORD structure.

{
WORD type = DNS_TYPE_TEXT;

if (0 != DnsQuery_A(domain.c_str(), type, DNS_QUERY_BYPASS_CACHE, NULL, &pDnsRecord, NULL))
{
cerr << "Error querying: '" << domain << "'" << endl;
return false;
}
}

PDNS_RECORD it;
map<WORD, function<void(void)>> callbacks;

callbacks[DNS_TYPE_TEXT] = [&it, &records](void) -> void {
std::stringstream stream;
for (DWORD i = 0; i < it->Data.TXT.dwStringCount; i++) {
stream << RPC_CSTR(it->Data.TXT.pStringArray[i]) << endl;;
}
records.push_back(stream.str());
};

for (it = pDnsRecord; it != NULL; it = it->pNext) {
if (callbacks.count(it->wType)) {
callbacks[it->wType]();
}
}
DnsRecordListFree(pDnsRecord, DnsFreeRecordListDeep);
PDNS_RECORD pDnsRecord; //Pointer to DNS_RECORD structure.

{
WORD type = DNS_TYPE_TEXT;

if (0 != DnsQuery_A(domain.c_str(), type, DNS_QUERY_BYPASS_CACHE, NULL, &pDnsRecord, NULL))
{
cerr << "Error querying: '" << domain << "'" << endl;
return false;
}
}

PDNS_RECORD it;
map<WORD, function<void(void)>> callbacks;

callbacks[DNS_TYPE_TEXT] = [&it, &records](void) -> void {
std::stringstream stream;
for (DWORD i = 0; i < it->Data.TXT.dwStringCount; i++) {
stream << RPC_CSTR(it->Data.TXT.pStringArray[i]) << endl;;
}
records.push_back(stream.str());
};

for (it = pDnsRecord; it != NULL; it = it->pNext) {
if (callbacks.count(it->wType)) {
callbacks[it->wType]();
}
}
DnsRecordListFree(pDnsRecord, DnsFreeRecordListDeep);
#else
using namespace std;

res_init();
ns_msg nsMsg;
int response;
unsigned char query_buffer[4096];
{
ns_type type = ns_t_txt;

const char * c_domain = (domain).c_str();
response = res_query(c_domain, 1, type, query_buffer, sizeof(query_buffer));

if (response < 0)
return false;
}

ns_initparse(query_buffer, response, &nsMsg);

map<ns_type, function<void(const ns_rr &rr)>> callbacks;

callbacks[ns_t_txt] = [&nsMsg, &records](const ns_rr &rr) -> void {
int txt_len = *(unsigned char *) ns_rr_rdata(rr);
char txt[256];
memset(txt, 0, 256);
if (txt_len <= 255){
memcpy(txt, ns_rr_rdata(rr) + 1, txt_len);
records.push_back(txt);
}
};

for (int x = 0; x < ns_msg_count(nsMsg, ns_s_an); x++) {
ns_rr rr;
ns_parserr(&nsMsg, ns_s_an, x, &rr);
ns_type type = ns_rr_type(rr);
if (callbacks.count(type)) {
callbacks[type](rr);
}
}

res_init();
ns_msg nsMsg;
int response;
unsigned char query_buffer[4096];
{
ns_type type = ns_t_txt;

const char * c_domain = (domain).c_str();
response = res_query(c_domain, 1, type, query_buffer, sizeof(query_buffer));

if (response < 0)
return false;
}

ns_initparse(query_buffer, response, &nsMsg);

map<ns_type, function<void(const ns_rr &rr)>> callbacks;

callbacks[ns_t_txt] = [&nsMsg, &records](const ns_rr &rr) -> void {
int txt_len = *(unsigned char *) ns_rr_rdata(rr);
char txt[256];
memset(txt, 0, 256);
if (txt_len <= 255){
memcpy(txt, ns_rr_rdata(rr) + 1, txt_len);
records.push_back(txt);
}
};

for (int x = 0; x < ns_msg_count(nsMsg, ns_s_an); x++) {
ns_rr rr;
ns_parserr(&nsMsg, ns_s_an, x, &rr);
ns_type type = ns_rr_type(rr);
if (callbacks.count(type)) {
callbacks[type](rr);
}
}

#endif
if (records.empty())
return false;
if (records.empty())
return false;

return true;
}
return true;
}

#endif

Expand Down
4 changes: 2 additions & 2 deletions src/CryptoNoteProtocol/CryptoNoteProtocolHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,9 @@ void CryptoNoteProtocolHandler::relay_transactions(NOTIFY_NEW_TRANSACTIONS::requ
if (!m_stemPool.hasTransaction(transactionHash)) {
logger(Logging::DEBUGGING) << "Adding relayed transaction " << transactionHash << " to stempool";
auto txblob = *tx_blob_it;
m_dispatcher.remoteSpawn([this, transactionHash, txblob] {
//m_dispatcher.remoteSpawn([this, transactionHash, txblob] {
m_stemPool.addTransaction(transactionHash, txblob);
});
//});
txHashes.push_back(transactionHash);
}
}
Expand Down
46 changes: 0 additions & 46 deletions src/Daemon/Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,6 @@ namespace
return loggerConfiguration;
}

// https://stackoverflow.com/questions/2941491/compare-versions-as-strings
void Parse(int result[4], const std::string& input)
{
std::istringstream parser(input);
parser >> result[0];
for (int idx = 1; idx < 4; idx++)
{
parser.get(); //Skip period
parser >> result[idx];
}
}

bool LessThanVersion(const std::string& a, const std::string& b)
{
int parsedA[4], parsedB[4];
Parse(parsedA, a);
Parse(parsedB, b);
return std::lexicographical_compare(parsedA, parsedA + 4, parsedB, parsedB + 4);
}

} // end anonymous namespace

int main(int argc, char* argv[])
Expand Down Expand Up @@ -340,32 +320,6 @@ int main(int argc, char* argv[])
}
}

// check for update
try {
httplib::Client cli("https://api.github.com");
auto rsp = cli.Get(CHECK_FOR_UPDATE_ENDPOINT);
if (rsp) {
if (rsp->status == 200) {
Common::JsonValue psResp = Common::JsonValue::fromString(rsp->body);
Common::JsonValue::Array a = psResp.getArray();
const auto& o = a.at(0);
if (o.contains("name")) {
const JsonValue& vo = o("name");
std::string remote_v = vo.toString();
remote_v.erase(remove(remote_v.begin(), remote_v.end(), '"'), remote_v.end());
remote_v.erase(0, 2);
std::string ours_v = PROJECT_VERSION;
if (LessThanVersion(ours_v, remote_v)) {
logger(INFO, BRIGHT_RED) << "NEW VERSION IS AVAILABLE. PLEASE UPDATE!\n";
}
}
}
}
}
catch (std::exception& e) {
logger(ERROR, BRIGHT_RED) << "Failed to check for update: " << e.what();
}

CryptoNote::CryptoNoteProtocolHandler cprotocol(currency, dispatcher, m_core, nullptr, logManager);
CryptoNote::NodeServer p2psrv(dispatcher, cprotocol, logManager);
CryptoNote::RpcServer rpcServer(rpcConfig, dispatcher, logManager, m_core, p2psrv, cprotocol);
Expand Down
121 changes: 0 additions & 121 deletions src/HTTP/TaskQueue.h

This file was deleted.

Loading

0 comments on commit 8733b51

Please sign in to comment.