Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ldns dependency #160

Merged
merged 3 commits into from
Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/common/dns_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "common/dns_utils.h"
#include <cstring>
#include <sstream>
#include <ldns/rr.h> // for RR type and class defs
#include <unbound.h>

namespace tools
Expand Down Expand Up @@ -126,7 +125,7 @@ std::vector<std::string> DNSResolver::get_ipv4(const std::string& url)

strncpy(urlC, url.c_str(), 999);
urlC[999] = '\0';
if (!check_address_syntax(url))
if (!check_address_syntax(urlC))
{
return addresses;
}
Expand All @@ -135,7 +134,7 @@ std::vector<std::string> DNSResolver::get_ipv4(const std::string& url)
ub_result_ptr result;

// call DNS resolver, blocking. if return value not zero, something went wrong
if (!ub_resolve(m_data->m_ub_context, urlC, LDNS_RR_TYPE_A, LDNS_RR_CLASS_IN, &(result.ptr)))
if (!ub_resolve(m_data->m_ub_context, urlC, DNS_TYPE_A, DNS_CLASS_IN, &(result.ptr)))
{
if (result.ptr->havedata)
{
Expand All @@ -157,15 +156,15 @@ std::vector<std::string> DNSResolver::get_ipv6(const std::string& url)
strncpy(urlC, url.c_str(), 999);
urlC[999] = '\0';

if (!check_address_syntax(url))
if (!check_address_syntax(urlC))
{
return addresses;
}

ub_result_ptr result;

// call DNS resolver, blocking. if return value not zero, something went wrong
if (!ub_resolve(m_data->m_ub_context, urlC, LDNS_RR_TYPE_AAAA, LDNS_RR_CLASS_IN, &(result.ptr)))
if (!ub_resolve(m_data->m_ub_context, urlC, DNS_TYPE_AAAA, DNS_CLASS_IN, &(result.ptr)))
{
if (result.ptr->havedata)
{
Expand All @@ -187,15 +186,15 @@ std::vector<std::string> DNSResolver::get_txt_record(const std::string& url)
strncpy(urlC, url.c_str(), 999);
urlC[999] = '\0';

if (!check_address_syntax(url))
if (!check_address_syntax(urlC))
{
return records;
}

ub_result_ptr result;

// call DNS resolver, blocking. if return value not zero, something went wrong
if (!ub_resolve(m_data->m_ub_context, urlC, LDNS_RR_TYPE_TXT, LDNS_RR_CLASS_IN, &(result.ptr)))
if (!ub_resolve(m_data->m_ub_context, urlC, DNS_TYPE_TXT, DNS_CLASS_IN, &(result.ptr)))
{
if (result.ptr->havedata)
{
Expand Down
6 changes: 6 additions & 0 deletions src/common/dns_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
namespace tools
{

// RFC defines for record types and classes for DNS, gleaned from ldns source
const static int DNS_CLASS_IN = 1;
const static int DNS_TYPE_A = 1;
const static int DNS_TYPE_TXT = 16;
const static int DNS_TYPE_AAAA = 8;

struct DNSResolverData;

/**
Expand Down
25 changes: 24 additions & 1 deletion src/common/varint.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <sstream>
#include <string>
/*! \file varint.h
* \breif provides the implementation of varint's
* \brief provides the implementation of varint's
*
* The representation of varints is rather odd. The first bit of each
* octet is significant, it represents wheter there is another part
Expand All @@ -52,6 +52,29 @@

namespace tools {

template<typename T>
size_t get_varint_packed_size(T v)
{
if(v <= 127)
return 1;
else if(v <= 16383)
return 2;
else if(v <= 2097151)
return 3;
else if(v <= 268435455)
return 4;
else if(v <= 34359738367)
return 5;
else if(v <= 4398046511103)
return 6;
else if(v <= 4398046511103)
return 6;
else if(v <= 562949953421311)
return 7;
else
return 8;
}

/*! \brief Error codes for varint
*/
enum {
Expand Down
1 change: 0 additions & 1 deletion src/cryptonote_core/cryptonote_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ namespace cryptonote
ar.end_array();
END_SERIALIZE()

private:
static size_t get_signature_size(const txin_v& tx_in);
};

Expand Down
8 changes: 4 additions & 4 deletions src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace cryptonote
return false;
}

bool r = add_new_tx(tx, tx_hash, tx_prefixt_hash, tx_blob.size(), tvc, keeped_by_block);
bool r = add_new_tx(tx, tx_hash, tx_prefixt_hash, tvc, keeped_by_block);
if(tvc.m_verifivation_failed)
{LOG_PRINT_RED_L1("Transaction verification failed: " << tx_hash);}
else if(tvc.m_verifivation_impossible)
Expand Down Expand Up @@ -284,7 +284,7 @@ namespace cryptonote
crypto::hash tx_prefix_hash = get_transaction_prefix_hash(tx);
blobdata bl;
t_serializable_object_to_blob(tx, bl);
return add_new_tx(tx, tx_hash, tx_prefix_hash, bl.size(), tvc, keeped_by_block);
return add_new_tx(tx, tx_hash, tx_prefix_hash, tvc, keeped_by_block);
}
//-----------------------------------------------------------------------------------------------
size_t core::get_blockchain_total_transactions()
Expand All @@ -297,7 +297,7 @@ namespace cryptonote
// return m_blockchain_storage.get_outs(amount, pkeys);
//}
//-----------------------------------------------------------------------------------------------
bool core::add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block)
bool core::add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, tx_verification_context& tvc, bool keeped_by_block)
{
if(m_mempool.have_tx(tx_hash))
{
Expand All @@ -311,7 +311,7 @@ namespace cryptonote
return true;
}

return m_mempool.add_tx(tx, tx_hash, blob_size, tvc, keeped_by_block);
return m_mempool.add_tx(tx, tx_hash, tvc, keeped_by_block);
}
//-----------------------------------------------------------------------------------------------
bool core::get_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, const blobdata& ex_nonce)
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/cryptonote_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace cryptonote
uint64_t get_target_blockchain_height() const;

private:
bool add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block);
bool add_new_tx(const transaction& tx, const crypto::hash& tx_hash, const crypto::hash& tx_prefix_hash, tx_verification_context& tvc, bool keeped_by_block);
bool add_new_tx(const transaction& tx, tx_verification_context& tvc, bool keeped_by_block);
bool add_new_block(const block& b, block_verification_context& bvc);
bool load_state_data();
Expand Down
17 changes: 17 additions & 0 deletions src/cryptonote_core/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,23 @@ namespace cryptonote
return true;
}
//---------------------------------------------------------------
size_t get_object_blobsize(const transaction& t)
{
size_t prefix_blob = get_object_blobsize(static_cast<const transaction_prefix&>(t));

if(is_coinbase(t))
return prefix_blob;

for(const auto& in: t.vin)
{
size_t sig_count = transaction::get_signature_size(in);
prefix_blob += 64*sig_count;
prefix_blob += tools::get_varint_packed_size(sig_count);
}
prefix_blob += tools::get_varint_packed_size(t.vin.size());
return prefix_blob;
}
//---------------------------------------------------------------
blobdata block_to_blob(const block& b)
{
return t_serializable_object_to_blob(b);
Expand Down
2 changes: 2 additions & 0 deletions src/cryptonote_core/cryptonote_format_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ namespace cryptonote
return b.size();
}
//---------------------------------------------------------------
size_t get_object_blobsize(const transaction& t);
//---------------------------------------------------------------
template<class t_object>
bool get_object_hash(const t_object& o, crypto::hash& res, size_t& blob_size)
{
Expand Down
9 changes: 4 additions & 5 deletions src/cryptonote_core/tx_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ namespace cryptonote

}
//---------------------------------------------------------------------------------
bool tx_memory_pool::add_tx(const transaction &tx, /*const crypto::hash& tx_prefix_hash,*/ const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool kept_by_block)
bool tx_memory_pool::add_tx(const transaction &tx, const crypto::hash &id, tx_verification_context& tvc, bool kept_by_block)
{

size_t blob_size = get_object_blobsize(tx);

if(!check_inputs_types_supported(tx))
{
Expand Down Expand Up @@ -179,9 +179,8 @@ namespace cryptonote
bool tx_memory_pool::add_tx(const transaction &tx, tx_verification_context& tvc, bool keeped_by_block)
{
crypto::hash h = null_hash;
size_t blob_size = 0;
get_transaction_hash(tx, h, blob_size);
return add_tx(tx, h, blob_size, tvc, keeped_by_block);
get_transaction_hash(tx, h);
return add_tx(tx, h, tvc, keeped_by_block);
}
//---------------------------------------------------------------------------------
bool tx_memory_pool::remove_transaction_keyimages(const transaction& tx)
Expand Down
4 changes: 2 additions & 2 deletions src/cryptonote_core/tx_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace cryptonote
{
public:
tx_memory_pool(blockchain_storage& bchs);
bool add_tx(const transaction &tx, const crypto::hash &id, size_t blob_size, tx_verification_context& tvc, bool keeped_by_block);
bool add_tx(const transaction &tx, const crypto::hash &id, tx_verification_context& tvc, bool keeped_by_block);
bool add_tx(const transaction &tx, tx_verification_context& tvc, bool keeped_by_block);
//gets tx and remove it from pool
bool take_tx(const crypto::hash &id, transaction &tx, size_t& blob_size, uint64_t& fee);
Expand All @@ -81,7 +81,7 @@ namespace cryptonote
/*bool flush_pool(const std::strig& folder);
bool inflate_pool(const std::strig& folder);*/

#define CURRENT_MEMPOOL_ARCHIVE_VER 8
#define CURRENT_MEMPOOL_ARCHIVE_VER 9

template<class archive_t>
void serialize(archive_t & a, const unsigned int version)
Expand Down
31 changes: 31 additions & 0 deletions tests/unit_tests/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,37 @@ namespace
}
}

bool test_get_varint_packed_size_for_num(uint64_t n)
{
std::stringstream ss;
typedef std::ostreambuf_iterator<char> it;
tools::write_varint(it(ss), n);
uint64_t sz = ss.str().size();
if(sz != tools::get_varint_packed_size(n))
return false;
else
return true;
}
TEST(Serialization, validate_get_varint_packed_size)
{
ASSERT_TRUE(test_get_varint_packed_size_for_num(127));
ASSERT_TRUE(test_get_varint_packed_size_for_num(128));
ASSERT_TRUE(test_get_varint_packed_size_for_num(16383));
ASSERT_TRUE(test_get_varint_packed_size_for_num(16383+1));
ASSERT_TRUE(test_get_varint_packed_size_for_num(2097151));
ASSERT_TRUE(test_get_varint_packed_size_for_num(2097151+1));
ASSERT_TRUE(test_get_varint_packed_size_for_num(268435455));
ASSERT_TRUE(test_get_varint_packed_size_for_num(268435455+1));
ASSERT_TRUE(test_get_varint_packed_size_for_num(34359738367));
ASSERT_TRUE(test_get_varint_packed_size_for_num(34359738367+1));
ASSERT_TRUE(test_get_varint_packed_size_for_num(4398046511103));
ASSERT_TRUE(test_get_varint_packed_size_for_num(4398046511103+1));
ASSERT_TRUE(test_get_varint_packed_size_for_num(4398046511103));
ASSERT_TRUE(test_get_varint_packed_size_for_num(4398046511103+1));
ASSERT_TRUE(test_get_varint_packed_size_for_num(562949953421311));
ASSERT_TRUE(test_get_varint_packed_size_for_num(562949953421311+1));
}

TEST(Serialization, serializes_transacion_signatures_correctly)
{
using namespace cryptonote;
Expand Down