Skip to content

Commit

Permalink
Adding fast exit in synchronization so we don't continue trying to co…
Browse files Browse the repository at this point in the history
…mmit things that will definitely fail.
  • Loading branch information
clemahieu committed Feb 20, 2017
1 parent 8204f1c commit c08ff9f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
58 changes: 34 additions & 24 deletions rai/core_test/block_synchronization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TEST (pull_synchronization, empty)
rai::pull_synchronization sync (test_log, [&blocks] (MDB_txn *, rai::block const & block_a)
{
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
Expand All @@ -37,6 +38,7 @@ TEST (pull_synchronization, one)
rai::pull_synchronization sync (test_log, [&blocks] (MDB_txn *, rai::block const & block_a)
{
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
Expand All @@ -62,10 +64,11 @@ TEST (pull_synchronization, send_dependencies)
store.unchecked_put (transaction, block3.hash (), block3);
}
rai::pull_synchronization sync (test_log, [&blocks, &store] (MDB_txn * transaction_a, rai::block const & block_a)
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
}, store);
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
return false;
}, store);
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_FALSE (sync.synchronize (transaction, block3.hash ()));
ASSERT_EQ (2, blocks.size ());
Expand All @@ -89,10 +92,11 @@ TEST (pull_synchronization, change_dependencies)
store.unchecked_put (transaction, block3.hash (), block3);
}
rai::pull_synchronization sync (test_log, [&blocks, &store] (MDB_txn * transaction_a, rai::block const & block_a)
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
}, store);
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_FALSE (sync.synchronize (transaction, block3.hash ()));
Expand All @@ -118,10 +122,11 @@ TEST (pull_synchronization, open_dependencies)
store.unchecked_put (transaction, block3.hash (), block3);
}
rai::pull_synchronization sync (test_log, [&blocks, &store] (MDB_txn * transaction_a, rai::block const & block_a)
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
}, store);
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_FALSE (sync.synchronize (transaction, block3.hash ()));
Expand Down Expand Up @@ -151,10 +156,11 @@ TEST (pull_synchronization, receive_dependencies)
store.unchecked_put (transaction, block5.hash (), block5);
}
rai::pull_synchronization sync (test_log, [&blocks, &store] (MDB_txn * transaction_a, rai::block const & block_a)
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
}, store);
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_FALSE (sync.synchronize (transaction, block5.hash ()));
Expand Down Expand Up @@ -190,10 +196,11 @@ TEST (pull_synchronization, ladder_dependencies)
store.unchecked_put (transaction, block7.hash (), block7);
}
rai::pull_synchronization sync (test_log, [&blocks, &store] (MDB_txn * transaction_a, rai::block const & block_a)
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
}, store);
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_FALSE (sync.synchronize (transaction, block7.hash ()));
Expand All @@ -216,6 +223,7 @@ TEST (push_synchronization, empty)
rai::push_synchronization sync (test_log, [&blocks] (MDB_txn * transaction_a, rai::block const & block_a)
{
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
Expand All @@ -238,10 +246,11 @@ TEST (push_synchronization, one)
store.block_put (transaction, block2.hash (), block2);
}
rai::push_synchronization sync (test_log, [&blocks, &store] (MDB_txn * transaction_a, rai::block const & block_a)
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
}, store);
{
store.block_put (transaction_a, block_a.hash (), block_a);
blocks.push_back (block_a.clone ());
return false;
}, store);
{
rai::transaction transaction (store.environment, nullptr, true);
store.unsynced_put (transaction, block2.hash ());
Expand Down Expand Up @@ -273,6 +282,7 @@ TEST (pull_synchronization, dependent_fork)
store.unchecked_put (transaction, send3.hash (), send3);
rai::pull_synchronization sync (test_log, [] (MDB_txn *, rai::block const & block_a)
{
return false;
}, store);
ASSERT_FALSE (sync.synchronize (transaction, send3.hash ()));
}
Expand Down
9 changes: 5 additions & 4 deletions rai/node/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <boost/log/trivial.hpp>

rai::block_synchronization::block_synchronization (boost::log::sources::logger_mt & log_a, std::function <void (MDB_txn *, rai::block const &)> const & target_a, rai::block_store & store_a) :
rai::block_synchronization::block_synchronization (boost::log::sources::logger_mt & log_a, std::function <bool (MDB_txn *, rai::block const &)> const & target_a, rai::block_store & store_a) :
log (log_a),
target (target_a),
store (store_a)
Expand Down Expand Up @@ -102,7 +102,7 @@ bool rai::block_synchronization::synchronize_one (MDB_txn * transaction_a)
auto block (retrieve (transaction_a, hash));
if (block != nullptr)
{
target (transaction_a, *block);
result = target (transaction_a, *block);
}
else
{
Expand All @@ -125,7 +125,7 @@ bool rai::block_synchronization::synchronize (MDB_txn * transaction_a, rai::bloc
return result;
}

rai::pull_synchronization::pull_synchronization (boost::log::sources::logger_mt & log_a, std::function <void (MDB_txn *, rai::block const &)> const & target_a, rai::block_store & store_a) :
rai::pull_synchronization::pull_synchronization (boost::log::sources::logger_mt & log_a, std::function <bool (MDB_txn *, rai::block const &)> const & target_a, rai::block_store & store_a) :
block_synchronization (log_a, target_a, store_a)
{
}
Expand All @@ -140,7 +140,7 @@ bool rai::pull_synchronization::synchronized (MDB_txn * transaction_a, rai::bloc
return store.block_exists (transaction_a, hash_a) || attempted.count (hash_a) != 0;
}

rai::push_synchronization::push_synchronization (boost::log::sources::logger_mt & log_a, std::function <void (MDB_txn *, rai::block const &)> const & target_a, rai::block_store & store_a) :
rai::push_synchronization::push_synchronization (boost::log::sources::logger_mt & log_a, std::function <bool (MDB_txn *, rai::block const &)> const & target_a, rai::block_store & store_a) :
block_synchronization (log_a, target_a, store_a)
{
}
Expand Down Expand Up @@ -598,6 +598,7 @@ connection (connection_a),
synchronization (connection->connection->node->log, [this] (MDB_txn * transaction_a, rai::block const & block_a)
{
push_block (block_a);
return false;
}, connection_a->connection->node->store)
{
}
Expand Down
8 changes: 4 additions & 4 deletions rai/node/bootstrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace rai
class block_synchronization
{
public:
block_synchronization (boost::log::sources::logger_mt &, std::function <void (MDB_txn *, rai::block const &)> const &, rai::block_store &);
block_synchronization (boost::log::sources::logger_mt &, std::function <bool (MDB_txn *, rai::block const &)> const &, rai::block_store &);
~block_synchronization ();
// Return true if target already has block
virtual bool synchronized (MDB_txn *, rai::block_hash const &) = 0;
Expand All @@ -26,21 +26,21 @@ class block_synchronization
bool synchronize (MDB_txn *, rai::block_hash const &);
std::unordered_set <rai::block_hash> sent;
boost::log::sources::logger_mt & log;
std::function <void (MDB_txn *, rai::block const &)> target;
std::function <bool (MDB_txn *, rai::block const &)> target;
rai::block_store & store;
std::unordered_set <rai::block_hash> attempted;
};
class pull_synchronization : public rai::block_synchronization
{
public:
pull_synchronization (boost::log::sources::logger_mt &, std::function <void (MDB_txn *, rai::block const &)> const &, rai::block_store &);
pull_synchronization (boost::log::sources::logger_mt &, std::function <bool (MDB_txn *, rai::block const &)> const &, rai::block_store &);
bool synchronized (MDB_txn *, rai::block_hash const &) override;
std::unique_ptr <rai::block> retrieve (MDB_txn *, rai::block_hash const &) override;
};
class push_synchronization : public rai::block_synchronization
{
public:
push_synchronization (boost::log::sources::logger_mt &, std::function <void (MDB_txn *, rai::block const &)> const &, rai::block_store &);
push_synchronization (boost::log::sources::logger_mt &, std::function <bool (MDB_txn *, rai::block const &)> const &, rai::block_store &);
bool synchronized (MDB_txn *, rai::block_hash const &) override;
std::unique_ptr <rai::block> retrieve (MDB_txn *, rai::block_hash const &) override;
};
Expand Down
5 changes: 4 additions & 1 deletion rai/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1758,12 +1758,14 @@ void rai::node::process_unchecked (std::shared_ptr <rai::bootstrap_attempt> atte
rai::block_hash block (1); // 1 is a sentinal initial value
rai::pull_synchronization synchronization (log, [this, &block, attempt_a] (MDB_txn * transaction_a, rai::block const & block_a)
{
process_receive_many (transaction_a, block_a, [this, transaction_a, &block, attempt_a] (rai::process_return result_a, rai::block const & block_a)
auto result (true);
process_receive_many (transaction_a, block_a, [this, transaction_a, &block, attempt_a, &result] (rai::process_return result_a, rai::block const & block_a)
{
switch (result_a.code)
{
case rai::process_result::progress:
case rai::process_result::old:
result = false;
// It definitely doesn't need to be in unchecked because it's in the ledger
store.unchecked_del (transaction_a, block_a.hash ());
break;
Expand Down Expand Up @@ -1797,6 +1799,7 @@ void rai::node::process_unchecked (std::shared_ptr <rai::bootstrap_attempt> atte
break;
}
});
return result;
}, store);
while (!block.is_zero ())
{
Expand Down

0 comments on commit c08ff9f

Please sign in to comment.