summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmir Taaki <genjix@riseup.net>2014-04-01 01:17:42 (GMT)
committer Amir Taaki <genjix@riseup.net>2014-04-01 01:17:42 (GMT)
commit7184aba78a6e863394dcf16347e4028c8178c3c0 (patch)
tree38784b75b21e26ec06eddeaeefcb596ed7e69028
parent36954bc0355785aedbcc37faa64467e02e39355d (diff)
anticipate a future where a large stealth_bitfield is needed and don't make the 4 bytes hardcoded in the db.
-rw-r--r--include/bitcoin/stealth.hpp4
-rw-r--r--include/bitcoin/utility/serializer.hpp30
-rw-r--r--src/blockchain/leveldb/leveldb_blockchain.cpp8
-rw-r--r--src/blockchain/leveldb/leveldb_common.cpp21
4 files changed, 39 insertions, 24 deletions
diff --git a/include/bitcoin/stealth.hpp b/include/bitcoin/stealth.hpp
index cf51ec9..b466efd 100644
--- a/include/bitcoin/stealth.hpp
+++ b/include/bitcoin/stealth.hpp
@@ -24,10 +24,12 @@
namespace libbitcoin {
+typedef uint32_t stealth_bitfield;
+
struct stealth_prefix
{
uint8_t number_bits;
- uint32_t bitfield;
+ stealth_bitfield bitfield;
};
bool stealth_match(const stealth_prefix& prefix, const uint8_t* raw_bitfield);
diff --git a/include/bitcoin/utility/serializer.hpp b/include/bitcoin/utility/serializer.hpp
index 3855f5d..b471675 100644
--- a/include/bitcoin/utility/serializer.hpp
+++ b/include/bitcoin/utility/serializer.hpp
@@ -55,6 +55,12 @@ public:
serializer(const Iterator begin)
: iter_(begin) {}
+ template <typename T>
+ void write_uint_auto(T value)
+ {
+ write_data(uncast_type(value));
+ }
+
void write_byte(uint8_t value)
{
*iter_ = value;
@@ -62,15 +68,15 @@ public:
}
void write_2_bytes(uint16_t value)
{
- write_uint(value);
+ write_uint_auto(value);
}
void write_4_bytes(uint32_t value)
{
- write_uint(value);
+ write_uint_auto(value);
}
void write_8_bytes(uint64_t value)
{
- write_uint(value);
+ write_uint_auto(value);
}
void write_variable_uint(uint64_t value)
@@ -152,12 +158,6 @@ public:
private:
template <typename T>
- void write_uint(T value)
- {
- write_data(uncast_type(value));
- }
-
- template <typename T>
void write_data_reverse(const T& data)
{
internal_copy(data.rbegin(), data.rend());
@@ -214,6 +214,12 @@ public:
deserializer(const Iterator begin, const Iterator end)
: iter_(begin), end_(end) {}
+ template <typename T>
+ const T read_uint_auto()
+ {
+ return read_data_impl<T>(iter_, end_);
+ }
+
uint8_t read_byte()
{
check_distance(iter_, end_, 1);
@@ -221,15 +227,15 @@ public:
}
uint16_t read_2_bytes()
{
- return read_data_impl<uint16_t>(iter_, end_);
+ return read_uint_auto<uint16_t>();
}
uint32_t read_4_bytes()
{
- return read_data_impl<uint32_t>(iter_, end_);
+ return read_uint_auto<uint32_t>();
}
uint64_t read_8_bytes()
{
- return read_data_impl<uint64_t>(iter_, end_);
+ return read_uint_auto<uint64_t>();
}
uint64_t read_variable_uint()
diff --git a/src/blockchain/leveldb/leveldb_blockchain.cpp b/src/blockchain/leveldb/leveldb_blockchain.cpp
index d1f229b..30460ee 100644
--- a/src/blockchain/leveldb/leveldb_blockchain.cpp
+++ b/src/blockchain/leveldb/leveldb_blockchain.cpp
@@ -720,9 +720,10 @@ bool leveldb_blockchain::do_fetch_stealth(const stealth_prefix& prefix,
{
if (!stealth_match(prefix, it))
return;
- constexpr uint32_t row_size = 4 + 33 + 21 + 32;
+ constexpr size_t bitfield_size = sizeof(stealth_bitfield);
+ constexpr size_t row_size = bitfield_size + 33 + 21 + 32;
// Skip bitfield value since we don't need it.
- auto deserial = make_deserializer(it + 4, it + row_size);
+ auto deserial = make_deserializer(it + bitfield_size, it + row_size);
stealth_row row;
row.ephemkey = deserial.read_data(33);
uint8_t address_version = deserial.read_byte();
@@ -744,4 +745,5 @@ void leveldb_blockchain::subscribe_reorganize(
reorganize_subscriber_->subscribe(handle_reorganize);
}
-} // namespace libbitcoin \ No newline at end of file
+} // namespace libbitcoin
+
diff --git a/src/blockchain/leveldb/leveldb_common.cpp b/src/blockchain/leveldb/leveldb_common.cpp
index 7eac067..af0691e 100644
--- a/src/blockchain/leveldb/leveldb_common.cpp
+++ b/src/blockchain/leveldb/leveldb_common.cpp
@@ -24,6 +24,7 @@
#include <bitcoin/address.hpp>
#include <bitcoin/satoshi_serialize.hpp>
#include <bitcoin/transaction.hpp>
+#include <bitcoin/stealth.hpp>
#include <bitcoin/blockchain/database/stealth_database.hpp>
#include <bitcoin/utility/assert.hpp>
#include <bitcoin/utility/logger.hpp>
@@ -246,7 +247,7 @@ bool add_debit(leveldb::WriteBatch& batch,
bool process_stealth_output_info(const transaction_output_type& output,
data_chunk& stealth_data_store)
{
- // Return false when we want the main loop to skip pass this
+ // Return true when we want the main loop to skip past this
// output and not process it any further.
if (output.script.type() != payment_type::stealth_info)
return false;
@@ -255,12 +256,15 @@ bool process_stealth_output_info(const transaction_output_type& output,
return true;
}
-uint32_t calculate_bitfield(const data_chunk& stealth_data)
+constexpr size_t bitfield_size = sizeof(stealth_bitfield);
+
+stealth_bitfield calculate_bitfield(const data_chunk& stealth_data)
{
// Calculate stealth bitfield
const hash_digest index = generate_sha256_hash(stealth_data);
- auto deserial = make_deserializer(index.begin(), index.begin() + 4);
- uint32_t bitfield = deserial.read_4_bytes();
+ auto deserial = make_deserializer(
+ index.begin(), index.begin() + bitfield_size);
+ stealth_bitfield bitfield = deserial.read_uint_auto<stealth_bitfield>();
return bitfield;
}
data_chunk read_ephemkey(const data_chunk& stealth_data)
@@ -275,17 +279,17 @@ void add_stealth_info(const data_chunk& stealth_data,
const payment_address& address, const hash_digest& tx_hash,
stealth_database& db)
{
- const uint32_t bitfield = calculate_bitfield(stealth_data);
+ const stealth_bitfield bitfield = calculate_bitfield(stealth_data);
const data_chunk ephemkey = read_ephemkey(stealth_data);
auto write_func = [&](uint8_t *it)
{
auto serial = make_serializer(it);
- serial.write_4_bytes(bitfield);
+ serial.write_uint_auto(bitfield);
serial.write_data(ephemkey);
serial.write_byte(address.version());
serial.write_short_hash(address.hash());
serial.write_hash(tx_hash);
- BITCOIN_ASSERT(serial.iterator() == it + 4 + 33 + 21 + 32);
+ BITCOIN_ASSERT(serial.iterator() == it + bitfield_size + 33 + 21 + 32);
};
db.store(write_func);
}
@@ -438,4 +442,5 @@ data_chunk create_address_key(
return result;
}
-} // namespace libbitcoin \ No newline at end of file
+} // namespace libbitcoin
+