Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
coranos committed May 28, 2023
1 parent a1061c2 commit b73acab
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 253 deletions.
4 changes: 3 additions & 1 deletion nano/secure/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ target_link_libraries(
ed25519
crypto_lib
lmdb
Boost::boost
Boost::iostreams
Boost::system
Boost::filesystem)

target_compile_definitions(secure PUBLIC -DQT_NO_KEYWORDS
-DBOOST_ASIO_HAS_STD_ARRAY=1)

include_directories(${CMAKE_SOURCE_DIR}/submodules)
30 changes: 7 additions & 23 deletions nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
#include <nano/lib/numbers.hpp>
#include <nano/lib/rep_weights.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/timer.hpp>
#include <nano/lib/utility.hpp>

#include <boost/iterator/transform_iterator.hpp>
#include <boost/optional/optional.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <boost/variant/variant.hpp>

#include <array>
#include <unordered_map>

namespace boost
Expand Down Expand Up @@ -102,7 +104,7 @@ class account_info final
{
public:
account_info () = default;
account_info (nano::block_hash const &, nano::account const &, nano::block_hash const &, nano::amount const &, uint64_t, uint64_t, epoch);
account_info (nano::block_hash const &, nano::account const &, nano::block_hash const &, nano::amount const &, nano::seconds_t modified, uint64_t, epoch);
bool deserialize (nano::stream &);
bool operator== (nano::account_info const &) const;
bool operator!= (nano::account_info const &) const;
Expand All @@ -113,7 +115,7 @@ class account_info final
nano::block_hash open_block{ 0 };
nano::amount balance{ 0 };
/** Seconds since posix epoch */
uint64_t modified{ 0 };
nano::seconds_t modified{ 0 };
uint64_t block_count{ 0 };
nano::epoch epoch_m{ nano::epoch::epoch_0 };
};
Expand Down Expand Up @@ -192,38 +194,22 @@ class unchecked_key final
nano::block_hash hash{ 0 };
};

/**
* Tag for block signature verification result
*/
enum class signature_verification : uint8_t
{
unknown = 0,
invalid = 1,
valid = 2,
valid_epoch = 3 // Valid for epoch blocks
};

/**
* Information on an unchecked block
*/
class unchecked_info final
{
public:
unchecked_info () = default;
unchecked_info (std::shared_ptr<nano::block> const &, nano::account const &, nano::signature_verification = nano::signature_verification::unknown);
unchecked_info (std::shared_ptr<nano::block> const &);
void serialize (nano::stream &) const;
bool deserialize (nano::stream &);
uint64_t modified () const;
nano::seconds_t modified () const;
std::shared_ptr<nano::block> block;
nano::account account{};

private:
/** Seconds since posix epoch */
uint64_t modified_m{ 0 };

public:
nano::signature_verification verified{ nano::signature_verification::unknown };
};

class block_info final
Expand Down Expand Up @@ -270,7 +256,7 @@ class vote final
vote () = default;
vote (nano::vote const &);
vote (bool &, nano::stream &);
vote (nano::account const &, nano::raw_key const &, uint64_t timestamp, uint8_t duration, std::vector<nano::block_hash> const &);
vote (nano::account const &, nano::raw_key const &, nano::millis_t timestamp, uint8_t duration, std::vector<nano::block_hash> const &);
std::string hashes_string () const;
nano::block_hash hash () const;
nano::block_hash full_hash () const;
Expand All @@ -291,7 +277,7 @@ class vote final
uint8_t duration_bits () const;
std::chrono::milliseconds duration () const;
static uint64_t constexpr timestamp_mask = { 0xffff'ffff'ffff'fff0ULL };
static uint64_t constexpr timestamp_max = { 0xffff'ffff'ffff'fff0ULL };
static nano::seconds_t constexpr timestamp_max = { 0xffff'ffff'ffff'fff0ULL };
static uint64_t constexpr timestamp_min = { 0x0000'0000'0000'0010ULL };
static uint8_t constexpr duration_max = { 0x0fu };

Expand Down Expand Up @@ -361,8 +347,6 @@ class process_return final
{
public:
nano::process_result code;
nano::signature_verification verified;
nano::amount previous_balance;
};
enum class tally_result
{
Expand Down
238 changes: 100 additions & 138 deletions nano/secure/ledger.cpp

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace nano
{
class store;
class stat;
class stats;
class write_transaction;

// map of vote weight per block, ordered greater first
Expand All @@ -27,11 +27,12 @@ class uncemented_info
class ledger final
{
public:
ledger (nano::store &, nano::stat &, nano::ledger_constants & constants, nano::generate_cache const & = nano::generate_cache ());
ledger (nano::store &, nano::stats &, nano::ledger_constants & constants, nano::generate_cache const & = nano::generate_cache ());
/**
* Return account containing hash, expects that block hash exists in ledger
*/
nano::account account (nano::transaction const &, nano::block_hash const &) const;
std::optional<nano::account_info> account_info (nano::transaction const & transaction, nano::account const & account) const;
/**
* For non-prunning nodes same as `ledger::account()`
* For prunning nodes ensures that block hash exists, otherwise returns zero account
Expand All @@ -52,6 +53,7 @@ class ledger final
nano::uint128_t weight (nano::account const &);
std::shared_ptr<nano::block> successor (nano::transaction const &, nano::qualified_root const &);
std::shared_ptr<nano::block> forked_block (nano::transaction const &, nano::block const &);
std::shared_ptr<nano::block> head_block (nano::transaction const &, nano::account const &);
bool block_confirmed (nano::transaction const &, nano::block_hash const &) const;
nano::block_hash latest (nano::transaction const &, nano::account const &);
nano::root latest_root (nano::transaction const &, nano::account const &);
Expand All @@ -66,7 +68,8 @@ class ledger final
nano::account const & block_destination (nano::transaction const &, nano::block const &);
nano::block_hash block_source (nano::transaction const &, nano::block const &);
std::pair<nano::block_hash, nano::block_hash> hash_root_random (nano::transaction const &) const;
nano::process_return process (nano::write_transaction const &, nano::block &, nano::signature_verification = nano::signature_verification::unknown);
std::optional<nano::pending_info> pending_info (nano::transaction const & transaction, nano::pending_key const & key) const;
nano::process_return process (nano::write_transaction const &, nano::block &);
bool rollback (nano::write_transaction const &, nano::block_hash const &, std::vector<std::shared_ptr<nano::block>> &);
bool rollback (nano::write_transaction const &, nano::block_hash const &);
void update_account (nano::write_transaction const &, nano::account const &, nano::account_info const &, nano::account_info const &);
Expand All @@ -86,7 +89,7 @@ class ledger final
nano::ledger_constants & constants;
nano::store & store;
nano::ledger_cache cache;
nano::stat & stats;
nano::stats & stats;
std::unordered_map<nano::account, nano::uint128_t> bootstrap_weights;
uint64_t bootstrap_weight_max_blocks{ 1 };
std::atomic<bool> check_bootstrap_weights;
Expand Down
8 changes: 4 additions & 4 deletions nano/secure/network_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool nano::network_filter::apply (uint8_t const * bytes_a, size_t count_a, nano:
// Get hash before locking
auto digest (hash (bytes_a, count_a));

nano::lock_guard<nano::mutex> lock (mutex);
nano::lock_guard<nano::mutex> lock{ mutex };
auto & element (get_element (digest));
bool existed (element == digest);
if (!existed)
Expand All @@ -32,7 +32,7 @@ bool nano::network_filter::apply (uint8_t const * bytes_a, size_t count_a, nano:

void nano::network_filter::clear (nano::uint128_t const & digest_a)
{
nano::lock_guard<nano::mutex> lock (mutex);
nano::lock_guard<nano::mutex> lock{ mutex };
auto & element (get_element (digest_a));
if (element == digest_a)
{
Expand All @@ -42,7 +42,7 @@ void nano::network_filter::clear (nano::uint128_t const & digest_a)

void nano::network_filter::clear (std::vector<nano::uint128_t> const & digests_a)
{
nano::lock_guard<nano::mutex> lock (mutex);
nano::lock_guard<nano::mutex> lock{ mutex };
for (auto const & digest : digests_a)
{
auto & element (get_element (digest));
Expand All @@ -66,7 +66,7 @@ void nano::network_filter::clear (OBJECT const & object_a)

void nano::network_filter::clear ()
{
nano::lock_guard<nano::mutex> lock (mutex);
nano::lock_guard<nano::mutex> lock{ mutex };
items.assign (items.size (), nano::uint128_t{ 0 });
}

Expand Down
6 changes: 3 additions & 3 deletions nano/secure/network_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include <nano/lib/numbers.hpp>

#include <crypto/cryptopp/seckey.h>
#include <crypto/cryptopp/siphash.h>

#include <mutex>

#include <cryptopp/seckey.h>
#include <cryptopp/siphash.h>

namespace nano
{
/**
Expand Down
16 changes: 0 additions & 16 deletions nano/secure/store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ nano::store::store (
nano::frontier_store & frontier_store_a,
nano::account_store & account_store_a,
nano::pending_store & pending_store_a,
nano::unchecked_store & unchecked_store_a,
nano::online_weight_store & online_weight_store_a,
nano::pruned_store & pruned_store_a,
nano::peer_store & peer_store_a,
Expand All @@ -124,7 +123,6 @@ nano::store::store (
frontier (frontier_store_a),
account (account_store_a),
pending (pending_store_a),
unchecked (unchecked_store_a),
online_weight (online_weight_store_a),
pruned (pruned_store_a),
peer (peer_store_a),
Expand Down Expand Up @@ -155,20 +153,6 @@ void nano::store::initialize (nano::write_transaction const & transaction_a, nan
frontier.put (transaction_a, hash_l, constants.genesis->account ());
}

auto nano::unchecked_store::equal_range (nano::transaction const & transaction, nano::block_hash const & dependency) -> std::pair<iterator, iterator>
{
nano::unchecked_key begin_l{ dependency, 0 };
nano::unchecked_key end_l{ nano::block_hash{ dependency.number () + 1 }, 0 };
// Adjust for edge case where number () + 1 wraps around.
auto end_iter = begin_l.previous < end_l.previous ? lower_bound (transaction, end_l) : end ();
return std::make_pair (lower_bound (transaction, begin_l), std::move (end_iter));
}

auto nano::unchecked_store::full_range (nano::transaction const & transaction) -> std::pair<iterator, iterator>
{
return std::make_pair (begin (transaction), end ());
}

std::optional<nano::account_info> nano::account_store::get (const nano::transaction & transaction, const nano::account & account)
{
nano::account_info info;
Expand Down
66 changes: 2 additions & 64 deletions nano/secure/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ class db_val
static_assert (std::is_standard_layout<nano::pending_key>::value, "Standard layout is required");
}

db_val (nano::unchecked_info const & val_a) :
buffer (std::make_shared<std::vector<uint8_t>> ())
{
{
nano::vectorstream stream (*buffer);
val_a.serialize (stream);
}
convert_buffer_to_value ();
}

db_val (nano::unchecked_key const & val_a) :
db_val (sizeof (val_a), const_cast<nano::unchecked_key *> (&val_a))
{
static_assert (std::is_standard_layout<nano::unchecked_key>::value, "Standard layout is required");
}

db_val (nano::confirmation_height_info const & val_a) :
buffer (std::make_shared<std::vector<uint8_t>> ())
{
Expand Down Expand Up @@ -222,25 +206,6 @@ class db_val
return result;
}

explicit operator nano::unchecked_info () const
{
nano::bufferstream stream (reinterpret_cast<uint8_t const *> (data ()), size ());
nano::unchecked_info result;
bool error (result.deserialize (stream));
(void)error;
debug_assert (!error);
return result;
}

explicit operator nano::unchecked_key () const
{
nano::unchecked_key result;
debug_assert (size () == sizeof (result));
static_assert (sizeof (nano::unchecked_key::previous) + sizeof (nano::pending_key::hash) == sizeof (result), "Packed class");
std::copy (reinterpret_cast<uint8_t const *> (data ()), reinterpret_cast<uint8_t const *> (data ()) + sizeof (result), reinterpret_cast<uint8_t *> (&result));
return result;
}

explicit operator nano::uint128_union () const
{
return convert<nano::uint128_union> ();
Expand Down Expand Up @@ -550,7 +515,6 @@ enum class tables
peers,
pending,
pruned,
unchecked,
vote
};

Expand Down Expand Up @@ -745,26 +709,6 @@ class confirmation_height_store
virtual void for_each_par (std::function<void (nano::read_transaction const &, nano::store_iterator<nano::account, nano::confirmation_height_info>, nano::store_iterator<nano::account, nano::confirmation_height_info>)> const &) const = 0;
};

/**
* Manages unchecked storage and iteration
*/
class unchecked_store
{
public:
using iterator = nano::store_iterator<nano::unchecked_key, nano::unchecked_info>;

virtual void clear (nano::write_transaction const &) = 0;
virtual void put (nano::write_transaction const &, nano::hash_or_account const & dependency, nano::unchecked_info const &) = 0;
std::pair<iterator, iterator> equal_range (nano::transaction const & transaction, nano::block_hash const & dependency);
std::pair<iterator, iterator> full_range (nano::transaction const & transaction);
virtual bool exists (nano::transaction const & transaction_a, nano::unchecked_key const & unchecked_key_a) = 0;
virtual void del (nano::write_transaction const &, nano::unchecked_key const &) = 0;
virtual iterator begin (nano::transaction const &) const = 0;
virtual iterator lower_bound (nano::transaction const &, nano::unchecked_key const &) const = 0;
virtual iterator end () const = 0;
virtual size_t count (nano::transaction const &) = 0;
};

/**
* Manages final vote storage and iteration
*/
Expand Down Expand Up @@ -821,13 +765,13 @@ class block_store
virtual uint64_t account_height (nano::transaction const & transaction_a, nano::block_hash const & hash_a) const = 0;
};

class unchecked_map;
/**
* Store manager
*/
class store
{
friend class rocksdb_block_store_tombstone_count_Test;
friend class mdb_block_store_upgrade_v21_v22_Test;

public:
// clang-format off
Expand All @@ -836,7 +780,6 @@ class store
nano::frontier_store &,
nano::account_store &,
nano::pending_store &,
nano::unchecked_store &,
nano::online_weight_store &,
nano::pruned_store &,
nano::peer_store &,
Expand All @@ -859,10 +802,7 @@ class store
account_store & account;
pending_store & pending;
static int constexpr version_minimum{ 14 };
static int constexpr version_current{ 21 };

private:
unchecked_store & unchecked;
static int constexpr version_current{ 22 };

public:
online_weight_store & online_weight;
Expand Down Expand Up @@ -890,8 +830,6 @@ class store
virtual nano::read_transaction tx_begin_read () const = 0;

virtual std::string vendor_get () const = 0;

friend class unchecked_map;
};

std::unique_ptr<nano::store> make_store (nano::logger_mt & logger, boost::filesystem::path const & path, nano::ledger_constants & constants, bool open_read_only = false, bool add_db_postfix = false, nano::rocksdb_config const & rocksdb_config = nano::rocksdb_config{}, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), nano::lmdb_config const & lmdb_config_a = nano::lmdb_config{}, bool backup_before_upgrade = false);
Expand Down

0 comments on commit b73acab

Please sign in to comment.