Moving work_verify outside work_pool since it isn't needed.

This commit is contained in:
clemahieu 2017-12-21 13:31:15 -06:00
commit afc6ff322c
13 changed files with 58 additions and 58 deletions

View file

@ -661,7 +661,7 @@ TEST (node, block_replace)
auto block3 (system.wallet (0)->send_action (rai::test_genesis_key.pub, 0, rai::Gxrb_ratio));
ASSERT_NE (nullptr, block1);
auto initial_work (block1->block_work ());
while (system.work.work_value (block1->root (), block1->block_work ()) <= system.work.work_value (block1->root (), initial_work))
while (rai::work_value (block1->root (), block1->block_work ()) <= rai::work_value (block1->root (), initial_work))
{
system.nodes [1]->generate_work (*block1);
}
@ -694,7 +694,7 @@ TEST (node, block_replace)
}
ASSERT_NE (initial_work, block1->block_work ());
ASSERT_EQ (block1->block_work (), block2->block_work ());
ASSERT_GT (system.work.work_value (block2->root (), block2->block_work ()), system.work.work_value (block1->root (), initial_work));
ASSERT_GT (rai::work_value (block2->root (), block2->block_work ()), rai::work_value (block1->root (), initial_work));
}
TEST (node, fork_publish)

View file

@ -1028,8 +1028,8 @@ TEST (rpc, payment_begin_end)
auto root1 (system.nodes [0]->ledger.latest_root (rai::transaction (wallet->store.environment, nullptr, false), account));
uint64_t work (0);
auto iteration (0);
ASSERT_TRUE (system.work.work_validate (root1, work));
while (system.work.work_validate (root1, work))
ASSERT_TRUE (rai::work_validate (root1, work));
while (rai::work_validate (root1, work))
{
system.poll ();
ASSERT_FALSE (wallet->store.work_get (rai::transaction (wallet->store.environment, nullptr, false), account, work));
@ -1413,7 +1413,7 @@ TEST (rpc, work_generate)
auto work1 (response1.json.get <std::string> ("work"));
uint64_t work2;
ASSERT_FALSE (rai::from_string_hex (work1, work2));
ASSERT_FALSE (system.work.work_validate (hash1, work2));
ASSERT_FALSE (rai::work_validate (hash1, work2));
}
TEST (rpc, work_cancel)
@ -1462,7 +1462,7 @@ TEST (rpc, work_peer_bad)
{
work = work_a;
});
while (system.work.work_validate (hash1, work))
while (rai::work_validate (hash1, work))
{
system.poll ();
}
@ -1486,7 +1486,7 @@ TEST (rpc, work_peer_one)
{
work = work_a;
});
while (system.work.work_validate (key1.pub, work))
while (rai::work_validate (key1.pub, work))
{
system.poll ();
}
@ -1527,7 +1527,7 @@ TEST (rpc, work_peer_many)
{
work = work_a;
});
while (system1.work.work_validate (key1.pub, work))
while (rai::work_validate (key1.pub, work))
{
system1.poll ();
system2.poll ();

View file

@ -603,7 +603,7 @@ TEST (wallet, empty_work)
rai::system system (24000, 1);
auto wallet (system.wallet (0));
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
ASSERT_FALSE (system.work.work_validate (1, wallet->work_fetch (transaction, 0, 1)));
ASSERT_FALSE (rai::work_validate (1, wallet->work_fetch (transaction, 0, 1)));
}
// Test work is precached when a key is inserted
@ -621,10 +621,10 @@ TEST (wallet, work)
root1 = system.nodes [0]->ledger.latest_root (transaction, account1);
work4 = wallet->work_fetch (transaction, account1, root1);
}
ASSERT_FALSE (system.work.work_validate (root1, work4));
ASSERT_FALSE (rai::work_validate (root1, work4));
uint64_t work3 (0);
auto iteration (0);
while (system.work.work_validate (root1, work3))
while (rai::work_validate (root1, work3))
{
system.poll ();
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
@ -664,7 +664,7 @@ TEST (wallet, work_generate)
++iterations2;
ASSERT_LT (iterations2, 200);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
again = wallet->store.work_get (transaction, account1, work1) || system.work.work_validate (system.nodes [0]->ledger.latest_root (transaction, account1), work1);
again = wallet->store.work_get (transaction, account1, work1) || rai::work_validate (system.nodes [0]->ledger.latest_root (transaction, account1), work1);
}
}
@ -675,7 +675,7 @@ TEST (wallet, unsynced_work)
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
wallet->store.work_put (transaction, 0, 0);
auto work1 (wallet->work_fetch (transaction, 0, 1));
ASSERT_FALSE (system.work.work_validate (1, work1));
ASSERT_FALSE (rai::work_validate (1, work1));
}
TEST (wallet, insert_locked)

View file

@ -9,16 +9,16 @@ TEST (work, one)
rai::work_pool pool (std::numeric_limits <unsigned>::max (), nullptr);
rai::change_block block (1, 1, rai::keypair ().prv, 3, 4);
block.block_work_set (pool.generate (block.root ()));
ASSERT_FALSE (pool.work_validate (block));
ASSERT_FALSE (rai::work_validate (block));
}
TEST (work, validate)
{
rai::work_pool pool (std::numeric_limits <unsigned>::max (), nullptr);
rai::send_block send_block (1, 1, 2, rai::keypair ().prv, 4, 6);
ASSERT_TRUE (pool.work_validate (send_block));
ASSERT_TRUE (rai::work_validate (send_block));
send_block.block_work_set (pool.generate (send_block.root ()));
ASSERT_FALSE (pool.work_validate (send_block));
ASSERT_FALSE (rai::work_validate (send_block));
}
TEST (work, cancel)
@ -66,7 +66,7 @@ TEST (work, DISABLED_opencl)
{
rai::random_pool.GenerateBlock (root.bytes.data (), root.bytes.size ());
auto result (pool.generate (root));
ASSERT_FALSE (pool.work_validate (root, result));
ASSERT_FALSE (rai::work_validate (root, result));
}
}
}

View file

@ -164,7 +164,7 @@ void rai::message_parser::deserialize_publish (uint8_t const * buffer_a, size_t
auto error_l (incoming.deserialize (stream));
if (!error_l && at_end (stream))
{
if (!pool.work_validate (*incoming.block))
if (!rai::work_validate (*incoming.block))
{
visitor.publish (incoming);
}
@ -186,7 +186,7 @@ void rai::message_parser::deserialize_confirm_req (uint8_t const * buffer_a, siz
auto error_l (incoming.deserialize (stream));
if (!error_l && at_end (stream))
{
if (!pool.work_validate (*incoming.block))
if (!rai::work_validate (*incoming.block))
{
visitor.confirm_req (incoming);
}
@ -208,7 +208,7 @@ void rai::message_parser::deserialize_confirm_ack (uint8_t const * buffer_a, siz
rai::confirm_ack incoming (error_l, stream);
if (!error_l && at_end (stream))
{
if (!pool.work_validate (*incoming.vote->block))
if (!rai::work_validate (*incoming.vote->block))
{
visitor.confirm_ack (incoming);
}

View file

@ -1219,7 +1219,7 @@ rai::process_return rai::block_processor::process_receive_one (MDB_txn * transac
if (existing != nullptr)
{
// Replace block with one that has higher work value
if (node.work.work_value (root, block_a->block_work ()) > node.work.work_value (root, existing->block_work ()))
if (rai::work_value (root, block_a->block_work ()) > rai::work_value (root, existing->block_work ()))
{
node.store.block_put (transaction_a, hash, *block_a, node.store.block_successor (transaction_a, hash));
}
@ -2096,7 +2096,7 @@ void success (std::string const & body_a, boost::asio::ip::address const & addre
uint64_t work;
if (!rai::from_string_hex (work_text, work))
{
if (!node->work.work_validate (root, work))
if (!rai::work_validate (root, work))
{
set_once (work);
stop ();

View file

@ -713,7 +713,7 @@ boost::optional <uint64_t> rai::opencl_work::generate_work (rai::work_pool & poo
uint64_t result (0);
unsigned thread_count (config.threads);
size_t work_size [] = { thread_count, 0, 0 };
while (pool_a.work_validate (root_a, result) && !error)
while (rai::work_validate (root_a, result) && !error)
{
result = rand.next ();
cl_int write_error1 = clEnqueueWriteBuffer (queue, attempt_buffer, false, 0, sizeof (uint64_t), &result, 0, nullptr, nullptr);

View file

@ -578,7 +578,7 @@ void rai::rpc_handler::account_representative_set ()
rai::account_info info;
if (!node.store.account_get (transaction, account, info))
{
if (!node.work.work_validate (info.head, work))
if (!rai::work_validate (info.head, work))
{
existing->second->store.work_put (transaction, account, work);
}
@ -2363,7 +2363,7 @@ void rai::rpc_handler::process ()
auto block (rai::deserialize_block_json (block_l));
if (block != nullptr)
{
if (!node.work.work_validate (*block))
if (!rai::work_validate (*block))
{
auto hash (block->hash ());
node.block_arrival.add (hash);
@ -2540,7 +2540,7 @@ void rai::rpc_handler::receive ()
{
head = account;
}
if (!node.work.work_validate (head, work))
if (!rai::work_validate (head, work))
{
rai::transaction transaction_a (node.store.environment, nullptr, true);
existing->second->store.work_put (transaction_a, account, work);
@ -2912,7 +2912,7 @@ void rai::rpc_handler::send ()
}
if (work)
{
if (!node.work.work_validate (info.head, work))
if (!rai::work_validate (info.head, work))
{
existing->second->store.work_put (transaction, source, work);
}
@ -3997,7 +3997,7 @@ void rai::rpc_handler::work_validate ()
auto work_error (rai::from_string_hex (work_text, work));
if (!work_error)
{
auto validate (node.work.work_validate (hash, work));
auto validate (rai::work_validate (hash, work));
boost::property_tree::ptree response_l;
response_l.put ("valid", validate ? "0" : "1");
response (response_l);

View file

@ -1028,7 +1028,7 @@ void rai::wallet::send_async (rai::account const & source_a, rai::account const
// Update work for account if latest root is root_a
void rai::wallet::work_update (MDB_txn * transaction_a, rai::account const & account_a, rai::block_hash const & root_a, uint64_t work_a)
{
assert (!node.work.work_validate (root_a, work_a));
assert (!rai::work_validate (root_a, work_a));
assert (store.exists (transaction_a, account_a));
auto latest (node.ledger.latest_root (transaction_a, account_a));
if (latest == root_a)
@ -1052,7 +1052,7 @@ uint64_t rai::wallet::work_fetch (MDB_txn * transaction_a, rai::account const &
}
else
{
if (node.work.work_validate (root_a, result))
if (rai::work_validate (root_a, result))
{
BOOST_LOG (node.log) << "Cached work invalid, regenerating";
result = node.generate_work (root_a);
@ -1068,7 +1068,7 @@ void rai::wallet::work_ensure (MDB_txn * transaction_a, rai::account const & acc
uint64_t work;
auto error (store.work_get (transaction_a, account_a, work));
assert (!error);
if (node.work.work_validate (root, work))
if (rai::work_validate (root, work))
{
auto this_l (shared_from_this ());
node.background ([this_l, account_a, root] () {

View file

@ -5,6 +5,28 @@
#include <future>
bool rai::work_validate (rai::block_hash const & root_a, uint64_t work_a)
{
auto result (rai::work_value (root_a, work_a) < rai::work_pool::publish_threshold);
return result;
}
bool rai::work_validate (rai::block const & block_a)
{
return work_validate (block_a.root (), block_a.block_work ());
}
uint64_t rai::work_value (rai::block_hash const & root_a, uint64_t work_a)
{
uint64_t result;
blake2b_state hash;
blake2b_init (&hash, sizeof (result));
blake2b_update (&hash, reinterpret_cast <uint8_t *> (&work_a), sizeof (work_a));
blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ());
blake2b_final (&hash, reinterpret_cast <uint8_t *> (&result), sizeof (result));
return result;
}
rai::work_pool::work_pool (unsigned max_threads_a, std::unique_ptr <rai::opencl_work> opencl_a) :
ticket (0),
done (false),
@ -32,17 +54,6 @@ rai::work_pool::~work_pool ()
}
}
uint64_t rai::work_pool::work_value (rai::block_hash const & root_a, uint64_t work_a)
{
uint64_t result;
blake2b_state hash;
blake2b_init (&hash, sizeof (result));
blake2b_update (&hash, reinterpret_cast <uint8_t *> (&work_a), sizeof (work_a));
blake2b_update (&hash, root_a.bytes.data (), root_a.bytes.size ());
blake2b_final (&hash, reinterpret_cast <uint8_t *> (&result), sizeof (result));
return result;
}
void rai::work_pool::loop (uint64_t thread)
{
// Quick RNG for work attempts.
@ -134,17 +145,6 @@ void rai::work_pool::cancel (rai::uint256_union const & root_a)
});
}
bool rai::work_pool::work_validate (rai::block_hash const & root_a, uint64_t work_a)
{
auto result (work_value (root_a, work_a) < rai::work_pool::publish_threshold);
return result;
}
bool rai::work_pool::work_validate (rai::block & block_a)
{
return work_validate (block_a.root (), block_a.block_work ());
}
void rai::work_pool::stop ()
{
std::lock_guard <std::mutex> lock (mutex);

View file

@ -10,6 +10,9 @@
namespace rai
{
bool work_validate (rai::block_hash const &, uint64_t);
bool work_validate (rai::block const &);
uint64_t work_value (rai::block_hash const &, uint64_t);
class block;
class opencl_work;
class work_pool
@ -22,9 +25,6 @@ public:
void cancel (rai::uint256_union const &);
void generate (rai::uint256_union const &, std::function <void (boost::optional <uint64_t> const &)>);
uint64_t generate (rai::uint256_union const &);
uint64_t work_value (rai::block_hash const &, uint64_t);
bool work_validate (rai::block &);
bool work_validate (rai::block_hash const &, uint64_t);
std::atomic <int> ticket;
bool done;
std::vector <std::thread> threads;

View file

@ -661,7 +661,7 @@ TEST (wallet, seed_work_generation)
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_FALSE (system.work.work_validate (system.nodes [0]->ledger.latest_root (rai::transaction (system.wallet (0)->store.environment, nullptr, false), key1), work));
ASSERT_FALSE (rai::work_validate (system.nodes [0]->ledger.latest_root (rai::transaction (system.wallet (0)->store.environment, nullptr, false), key1), work));
}
TEST (wallet, backup_seed)

View file

@ -362,7 +362,7 @@ int main (int argc, char * const * argv)
{
block.hashables.previous.qwords [0] += 1;
block.block_work_set (t);
work.work_validate (block);
rai::work_validate (block);
}
auto end1 (std::chrono::high_resolution_clock::now ());
std::cerr << boost::str (boost::format ("%|1$ 12d|\n") % std::chrono::duration_cast <std::chrono::microseconds> (end1 - begin1).count ());