Apply clang-format config

This commit is contained in:
androm3da 2018-01-05 22:37:20 -06:00
commit e9592e5ad4
66 changed files with 4348 additions and 4562 deletions

View file

@ -1,7 +1,7 @@
#pragma once
#include <cstddef>
#include <chrono>
#include <cstddef>
namespace rai
{

View file

@ -20,7 +20,7 @@ TEST (ed25519, signing)
ed25519_sign (message.bytes.data (), sizeof (message.bytes), prv.bytes.data (), pub.bytes.data (), signature.bytes.data ());
auto valid1 (ed25519_sign_open (message.bytes.data (), sizeof (message.bytes), pub.bytes.data (), signature.bytes.data ()));
ASSERT_EQ (0, valid1);
signature.bytes [32] ^= 0x1;
signature.bytes[32] ^= 0x1;
auto valid2 (ed25519_sign_open (message.bytes.data (), sizeof (message.bytes), pub.bytes.data (), signature.bytes.data ()));
ASSERT_NE (0, valid2);
}
@ -31,14 +31,14 @@ TEST (transaction_block, empty)
rai::send_block block (0, 1, 13, key1.prv, key1.pub, 2);
rai::uint256_union hash (block.hash ());
ASSERT_FALSE (rai::validate_message (key1.pub, hash, block.signature));
block.signature.bytes [32] ^= 0x1;
block.signature.bytes[32] ^= 0x1;
ASSERT_TRUE (rai::validate_message (key1.pub, hash, block.signature));
}
TEST (block, send_serialize)
{
rai::send_block block1 (0, 1, 2, rai::keypair ().prv, 4, 5);
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream1 (bytes);
block1.serialize (stream1);
@ -73,7 +73,7 @@ TEST (block, receive_serialize)
{
rai::receive_block block1 (0, 1, rai::keypair ().prv, 3, 4);
rai::keypair key1;
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream1 (bytes);
block1.serialize (stream1);
@ -168,7 +168,7 @@ TEST (uint512_union, parse_error_symbol)
rai::uint512_union input (rai::uint512_t (1000));
std::string text;
input.encode_hex (text);
text [5] = '!';
text[5] = '!';
rai::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
@ -176,7 +176,7 @@ TEST (uint512_union, parse_error_symbol)
TEST (uint512_union, max)
{
rai::uint512_union input (std::numeric_limits <rai::uint512_t>::max ());
rai::uint512_union input (std::numeric_limits<rai::uint512_t>::max ());
std::string text;
input.encode_hex (text);
rai::uint512_union output;
@ -188,7 +188,7 @@ TEST (uint512_union, max)
TEST (uint512_union, parse_error_overflow)
{
rai::uint512_union input (std::numeric_limits <rai::uint512_t>::max ());
rai::uint512_union input (std::numeric_limits<rai::uint512_t>::max ());
std::string text;
input.encode_hex (text);
text.push_back (0);
@ -201,7 +201,7 @@ TEST (send_block, deserialize)
{
rai::send_block block1 (0, 1, 2, rai::keypair ().prv, 4, 5);
ASSERT_EQ (block1.hash (), block1.hash ());
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream1 (bytes);
block1.serialize (stream1);
@ -220,7 +220,7 @@ TEST (receive_block, deserialize)
ASSERT_EQ (block1.hash (), block1.hash ());
block1.hashables.previous = 2;
block1.hashables.source = 4;
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream1 (bytes);
block1.serialize (stream1);
@ -237,7 +237,7 @@ TEST (open_block, deserialize)
{
rai::open_block block1 (0, 1, 0, rai::keypair ().prv, 0, 0);
ASSERT_EQ (block1.hash (), block1.hash ());
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
block1.serialize (stream);
@ -254,7 +254,7 @@ TEST (change_block, deserialize)
{
rai::change_block block1 (1, 2, rai::keypair ().prv, 4, 5);
ASSERT_EQ (block1.hash (), block1.hash ());
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream1 (bytes);
block1.serialize (stream1);
@ -277,7 +277,7 @@ TEST (frontier_req, serialization)
request1.start = 1;
request1.age = 2;
request1.count = 3;
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
request1.serialize (stream);
@ -292,9 +292,9 @@ TEST (block, publish_req_serialization)
{
rai::keypair key1;
rai::keypair key2;
auto block (std::unique_ptr <rai::send_block> (new rai::send_block (0, key2.pub, 200, rai::keypair ().prv, 2, 3)));
auto block (std::unique_ptr<rai::send_block> (new rai::send_block (0, key2.pub, 200, rai::keypair ().prv, 2, 3)));
rai::publish req (std::move (block));
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
req.serialize (stream);
@ -311,9 +311,9 @@ TEST (block, confirm_req_serialization)
{
rai::keypair key1;
rai::keypair key2;
auto block (std::unique_ptr <rai::send_block> (new rai::send_block (0, key2.pub, 200, rai::keypair ().prv, 2, 3)));
auto block (std::unique_ptr<rai::send_block> (new rai::send_block (0, key2.pub, 200, rai::keypair ().prv, 2, 3)));
rai::confirm_req req (std::move (block));
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
req.serialize (stream);

View file

@ -127,7 +127,7 @@ TEST (block_store, pending_iterator)
ASSERT_TRUE (!init);
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_EQ (store.pending_end (), store.pending_begin (transaction));
store.pending_put (transaction, rai::pending_key (1, 2), {2, 3});
store.pending_put (transaction, rai::pending_key (1, 2), { 2, 3 });
auto current (store.pending_begin (transaction));
ASSERT_NE (store.pending_end (), current);
rai::pending_key key1 (current->first);
@ -152,7 +152,7 @@ TEST (block_store, genesis)
ASSERT_EQ (hash, info.head);
auto block1 (store.block_get (transaction, info.head));
ASSERT_NE (nullptr, block1);
auto receive1 (dynamic_cast <rai::open_block *> (block1.get ()));
auto receive1 (dynamic_cast<rai::open_block *> (block1.get ()));
ASSERT_NE (nullptr, receive1);
ASSERT_LE (info.modified, store.now ());
auto test_pub_text (rai::test_genesis_key.pub.to_string ());
@ -180,14 +180,14 @@ TEST (bootstrap, simple)
bool init (false);
rai::block_store store (init, rai::unique_path ());
ASSERT_TRUE (!init);
auto block1 (std::make_shared <rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
auto block1 (std::make_shared<rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (store.environment, nullptr, true);
auto block2 (store.unchecked_get (transaction, block1->previous ()));
ASSERT_TRUE (block2.empty ());
store.unchecked_put (transaction, block1->previous (), block1);
auto block3 (store.unchecked_get (transaction, block1->previous ()));
ASSERT_FALSE (block3.empty ());
ASSERT_EQ (*block1, *block3 [0]);
ASSERT_EQ (*block1, *block3[0]);
store.unchecked_del (transaction, block1->previous (), *block1);
auto block4 (store.unchecked_get (transaction, block1->previous ()));
ASSERT_TRUE (block4.empty ());
@ -198,7 +198,7 @@ TEST (unchecked, multiple)
bool init (false);
rai::block_store store (init, rai::unique_path ());
ASSERT_TRUE (!init);
auto block1 (std::make_shared <rai::send_block> (4, 1, 2, rai::keypair ().prv, 4, 5));
auto block1 (std::make_shared<rai::send_block> (4, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (store.environment, nullptr, true);
auto block2 (store.unchecked_get (transaction, block1->previous ()));
ASSERT_TRUE (block2.empty ());
@ -266,7 +266,7 @@ TEST (block_store, one_bootstrap)
bool init (false);
rai::block_store store (init, rai::unique_path ());
ASSERT_TRUE (!init);
auto block1 (std::make_shared <rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
auto block1 (std::make_shared<rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (store.environment, nullptr, true);
store.unchecked_put (transaction, block1->hash (), block1);
store.flush (transaction);
@ -313,7 +313,7 @@ TEST (block_store, one_account)
rai::account account (0);
rai::block_hash hash (0);
rai::transaction transaction (store.environment, nullptr, true);
store.account_put (transaction, account, {hash, account, hash, 42, 100, 200});
store.account_put (transaction, account, { hash, account, hash, 42, 100, 200 });
auto begin (store.latest_begin (transaction));
auto end (store.latest_end ());
ASSERT_NE (end, begin);
@ -334,16 +334,16 @@ TEST (block_store, two_block)
ASSERT_TRUE (!init);
rai::open_block block1 (0, 1, 1, rai::keypair ().prv, 0, 0);
block1.hashables.account = 1;
std::vector <rai::block_hash> hashes;
std::vector <rai::open_block> blocks;
std::vector<rai::block_hash> hashes;
std::vector<rai::open_block> blocks;
hashes.push_back (block1.hash ());
blocks.push_back (block1);
rai::transaction transaction (store.environment, nullptr, true);
store.block_put (transaction, hashes [0], block1);
store.block_put (transaction, hashes[0], block1);
rai::open_block block2 (0, 1, 2, rai::keypair ().prv, 0, 0);
hashes.push_back (block2.hash ());
blocks.push_back (block2);
store.block_put (transaction, hashes [1], block2);
store.block_put (transaction, hashes[1], block2);
ASSERT_TRUE (store.block_exists (transaction, block1.hash ()));
ASSERT_TRUE (store.block_exists (transaction, block2.hash ()));
}
@ -358,8 +358,8 @@ TEST (block_store, two_account)
rai::account account2 (3);
rai::block_hash hash2 (4);
rai::transaction transaction (store.environment, nullptr, true);
store.account_put (transaction, account1, {hash1, account1, hash1, 42, 100, 300});
store.account_put (transaction, account2, {hash2, account2, hash2, 84, 200, 400});
store.account_put (transaction, account1, { hash1, account1, hash1, 42, 100, 300 });
store.account_put (transaction, account2, { hash2, account2, hash2, 84, 200, 400 });
auto begin (store.latest_begin (transaction));
auto end (store.latest_end ());
ASSERT_NE (end, begin);
@ -391,8 +391,8 @@ TEST (block_store, latest_find)
rai::account account2 (3);
rai::block_hash hash2 (4);
rai::transaction transaction (store.environment, nullptr, true);
store.account_put (transaction, account1, {hash1, account1, hash1, 100, 0, 300});
store.account_put (transaction, account2, {hash2, account2, hash2, 200, 0, 400});
store.account_put (transaction, account1, { hash1, account1, hash1, 100, 0, 300 });
store.account_put (transaction, account2, { hash2, account2, hash2, 200, 0, 400 });
auto first (store.latest_begin (transaction));
auto second (store.latest_begin (transaction));
++second;
@ -493,7 +493,7 @@ TEST (block_store, unsynced_iteration)
store.unsynced_put (transaction, hash1);
rai::block_hash hash2 (2);
store.unsynced_put (transaction, hash2);
std::unordered_set <rai::block_hash> hashes;
std::unordered_set<rai::block_hash> hashes;
for (auto i (store.unsynced_begin (transaction)), n (store.unsynced_end ()); i != n; ++i)
{
hashes.insert (rai::uint256_union (i->first.uint256 ()));
@ -508,7 +508,7 @@ TEST (block_store, large_iteration)
bool init (false);
rai::block_store store (init, rai::unique_path ());
ASSERT_TRUE (!init);
std::unordered_set <rai::account> accounts1;
std::unordered_set<rai::account> accounts1;
for (auto i (0); i < 1000; ++i)
{
rai::transaction transaction (store.environment, nullptr, true);
@ -517,7 +517,7 @@ TEST (block_store, large_iteration)
accounts1.insert (account);
store.account_put (transaction, account, rai::account_info ());
}
std::unordered_set <rai::account> accounts2;
std::unordered_set<rai::account> accounts2;
rai::account previous (0);
rai::transaction transaction (store.environment, nullptr, false);
for (auto i (store.latest_begin (transaction, 0)), n (store.latest_end ()); i != n; ++i)
@ -591,7 +591,7 @@ TEST (block_store, sequence_increment)
ASSERT_TRUE (!init);
rai::keypair key1;
rai::keypair key2;
auto block1 (std::make_shared <rai::open_block> (0, 1, 0, rai::keypair ().prv, 0, 0));
auto block1 (std::make_shared<rai::open_block> (0, 1, 0, rai::keypair ().prv, 0, 0));
rai::transaction transaction (store.environment, nullptr, true);
auto vote1 (store.vote_generate (transaction, key1.pub, key1.prv, block1));
ASSERT_EQ (1, vote1->sequence);
@ -631,7 +631,7 @@ TEST (block_store, upgrade_v2_v3)
rai::change_block change (hash, key1.pub, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
change_hash = change.hash ();
ASSERT_EQ (rai::process_result::progress, ledger.process (transaction, change).code);
ASSERT_EQ (0, ledger.weight(transaction, rai::test_genesis_key.pub));
ASSERT_EQ (0, ledger.weight (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (rai::genesis_amount, ledger.weight (transaction, key1.pub));
store.version_put (transaction, 2);
store.representation_put (transaction, key1.pub, 7);
@ -745,18 +745,18 @@ TEST (vote, validate)
rai::block_store store (init, rai::unique_path ());
ASSERT_TRUE (!init);
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (0, key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote1 (std::make_shared <rai::vote> (key1.pub, key1.prv, 2, send1));
auto send1 (std::make_shared<rai::send_block> (0, key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote1 (std::make_shared<rai::vote> (key1.pub, key1.prv, 2, send1));
rai::transaction transaction (store.environment, nullptr, true);
auto vote_result1 (store.vote_validate (transaction, vote1));
ASSERT_EQ (rai::vote_code::vote, vote_result1.code);
ASSERT_EQ (*vote1, *vote_result1.vote);
vote1->signature.bytes [8] ^= 1;
vote1->signature.bytes[8] ^= 1;
auto vote_result2 (store.vote_validate (transaction, vote1));
ASSERT_EQ (rai::vote_code::invalid, vote_result2.code);
// If the signature is invalid, we don't need to take the overhead of checking the current sequence value
ASSERT_EQ (nullptr, vote_result2.vote);
auto vote2 (std::make_shared <rai::vote> (key1.pub, key1.prv, 1, send1));
auto vote2 (std::make_shared<rai::vote> (key1.pub, key1.prv, 1, send1));
auto vote_result3 (store.vote_validate (transaction, vote2));
ASSERT_EQ (rai::vote_code::replay, vote_result3.code);
ASSERT_EQ (*vote1, *vote_result3.vote);
@ -799,7 +799,7 @@ TEST (block_store, upgrade_v6_v7)
rai::genesis genesis;
genesis.initialize (transaction, store);
store.version_put (transaction, 6);
auto send1 (std::make_shared <rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
store.unchecked_put (transaction, send1->hash (), send1);
store.flush (transaction);
ASSERT_NE (store.unchecked_end (), store.unchecked_begin (transaction));
@ -820,8 +820,8 @@ TEST (block_store, change_dupsort)
rai::transaction transaction (store.environment, nullptr, true);
ASSERT_EQ (0, mdb_drop (transaction, store.unchecked, 1));
ASSERT_EQ (0, mdb_dbi_open (transaction, "unchecked", MDB_CREATE, &store.unchecked));
auto send1 (std::make_shared <rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared <rai::send_block> (1, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared<rai::send_block> (1, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_NE (send1->hash (), send2->hash ());
store.unchecked_put (transaction, send1->hash (), send1);
store.unchecked_put (transaction, send1->hash (), send2);
@ -871,8 +871,8 @@ TEST (block_store, upgrade_v7_v8)
rai::block_store store (init, path);
ASSERT_FALSE (init);
rai::transaction transaction (store.environment, nullptr, true);
auto send1 (std::make_shared <rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared <rai::send_block> (1, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared<rai::send_block> (1, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
store.unchecked_put (transaction, send1->hash (), send1);
store.unchecked_put (transaction, send1->hash (), send2);
store.flush (transaction);
@ -893,7 +893,7 @@ TEST (block_store, sequence_flush)
ASSERT_FALSE (init);
rai::transaction transaction (store.environment, nullptr, true);
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote1 (store.vote_generate (transaction, key1.pub, key1.prv, send1));
auto seq2 (store.vote_get (transaction, vote1->account));
ASSERT_EQ (nullptr, seq2);

View file

@ -4,13 +4,13 @@
TEST (conflicts, start_stop)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_EQ (rai::process_result::progress, node1.process (*send1).code);
ASSERT_EQ (0, node1.active.roots.size ());
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
@ -27,24 +27,24 @@ TEST (conflicts, start_stop)
TEST (conflicts, add_existing)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_EQ (rai::process_result::progress, node1.process (*send1).code);
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
}
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send2);
}
ASSERT_EQ (1, node1.active.roots.size ());
auto vote1 (std::make_shared <rai::vote> (key2.pub, key2.prv, 0, send2));
auto vote1 (std::make_shared<rai::vote> (key2.pub, key2.prv, 0, send2));
node1.active.vote (vote1);
ASSERT_EQ (1, node1.active.roots.size ());
auto votes1 (node1.active.roots.find (send2->root ())->election);
@ -56,18 +56,18 @@ TEST (conflicts, add_existing)
TEST (conflicts, add_two)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_EQ (rai::process_result::progress, node1.process (*send1).code);
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
}
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (send1->hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared<rai::send_block> (send1->hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_EQ (rai::process_result::progress, node1.process (*send2).code);
{
rai::transaction transaction (node1.store.environment, nullptr, true);

View file

@ -2,5 +2,4 @@
TEST (daemon, fork)
{
}

View file

@ -7,6 +7,6 @@ TEST (basic, basic)
TEST (asan, DISABLED_memory)
{
uint8_t array [1];
auto value (array [-0x800000]);
uint8_t array[1];
auto value (array[-0x800000]);
}

View file

@ -4,69 +4,71 @@
TEST (gap_cache, add_new)
{
rai::system system (24000, 1);
rai::gap_cache cache (*system.nodes [0]);
auto block1 (std::make_shared <rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
rai::gap_cache cache (*system.nodes[0]);
auto block1 (std::make_shared<rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
cache.add (transaction, block1);
}
TEST (gap_cache, add_existing)
{
rai::system system (24000, 1);
rai::gap_cache cache (*system.nodes [0]);
auto block1 (std::make_shared <rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
rai::gap_cache cache (*system.nodes[0]);
auto block1 (std::make_shared<rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
cache.add (transaction, block1);
auto existing1 (cache.blocks.get <1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get <1> ().end (), existing1);
auto existing1 (cache.blocks.get<1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get<1> ().end (), existing1);
auto arrival (existing1->arrival);
while (arrival == std::chrono::system_clock::now ());
while (arrival == std::chrono::system_clock::now ())
;
cache.add (transaction, block1);
ASSERT_EQ (1, cache.blocks.size ());
auto existing2 (cache.blocks.get <1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get <1> ().end (), existing2);
auto existing2 (cache.blocks.get<1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get<1> ().end (), existing2);
ASSERT_GT (existing2->arrival, arrival);
}
TEST (gap_cache, comparison)
{
rai::system system (24000, 1);
rai::gap_cache cache (*system.nodes [0]);
auto block1 (std::make_shared <rai::send_block> (1, 0, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
rai::gap_cache cache (*system.nodes[0]);
auto block1 (std::make_shared<rai::send_block> (1, 0, 2, rai::keypair ().prv, 4, 5));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
cache.add (transaction, block1);
auto existing1 (cache.blocks.get <1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get <1> ().end (), existing1);
auto existing1 (cache.blocks.get<1> ().find (block1->hash ()));
ASSERT_NE (cache.blocks.get<1> ().end (), existing1);
auto arrival (existing1->arrival);
while (std::chrono::system_clock::now () == arrival);
auto block3 (std::make_shared <rai::send_block> (0, 42, 1, rai::keypair ().prv, 3, 4));
while (std::chrono::system_clock::now () == arrival)
;
auto block3 (std::make_shared<rai::send_block> (0, 42, 1, rai::keypair ().prv, 3, 4));
cache.add (transaction, block3);
ASSERT_EQ (2, cache.blocks.size ());
auto existing2 (cache.blocks.get <1> ().find (block3->hash ()));
ASSERT_NE (cache.blocks.get <1> ().end (), existing2);
auto existing2 (cache.blocks.get<1> ().find (block3->hash ()));
ASSERT_NE (cache.blocks.get<1> ().end (), existing2);
ASSERT_GT (existing2->arrival, arrival);
ASSERT_EQ (arrival, cache.blocks.get <1> ().begin ()->arrival);
ASSERT_EQ (arrival, cache.blocks.get<1> ().begin ()->arrival);
}
TEST (gap_cache, gap_bootstrap)
{
rai::system system (24000, 2);
rai::block_hash latest (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest (system.nodes[0]->latest (rai::test_genesis_key.pub));
rai::keypair key;
auto send (std::make_shared <rai::send_block> (latest, key.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (latest)));
auto send (std::make_shared<rai::send_block> (latest, key.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (latest)));
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->block_processor.process_receive_one (transaction, send).code);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->block_processor.process_receive_one (transaction, send).code);
}
ASSERT_EQ (rai::genesis_amount - 100, system.nodes [0]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount, system.nodes [1]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount - 100, system.nodes[0]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount, system.nodes[1]->balance (rai::genesis_account));
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key.prv);
system.wallet (0)->send_action (rai::test_genesis_key.pub, key.pub, 100);
ASSERT_EQ (rai::genesis_amount - 200, system.nodes [0]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount, system.nodes [1]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount - 200, system.nodes[0]->balance (rai::genesis_account));
ASSERT_EQ (rai::genesis_amount, system.nodes[1]->balance (rai::genesis_account));
auto iterations2 (0);
while (system.nodes [1]->balance (rai::genesis_account) != rai::genesis_amount - 200)
while (system.nodes[1]->balance (rai::genesis_account) != rai::genesis_amount - 200)
{
system.poll ();
++iterations2;
@ -79,18 +81,18 @@ TEST (gap_cache, two_dependencies)
rai::system system (24000, 1);
rai::keypair key;
rai::genesis genesis;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key.pub, 1, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
auto send2 (std::make_shared <rai::send_block> (send1->hash (), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
auto open (std::make_shared <rai::open_block> (send1->hash (), key.pub, key.pub, key.prv, key.pub, system.work.generate (key.pub)));
ASSERT_EQ (0, system.nodes [0]->gap_cache.blocks.size ());
system.nodes [0]->block_processor.process_receive_many (rai::block_processor_item (send2));
ASSERT_EQ (1, system.nodes [0]->gap_cache.blocks.size ());
system.nodes [0]->block_processor.process_receive_many (rai::block_processor_item (open));
ASSERT_EQ (2, system.nodes [0]->gap_cache.blocks.size ());
system.nodes [0]->block_processor.process_receive_many (rai::block_processor_item (send1));
ASSERT_EQ (0, system.nodes [0]->gap_cache.blocks.size ());
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_TRUE (system.nodes [0]->store.block_exists (transaction, send1->hash ()));
ASSERT_TRUE (system.nodes [0]->store.block_exists (transaction, send2->hash ()));
ASSERT_TRUE (system.nodes [0]->store.block_exists (transaction, open->hash ()));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key.pub, 1, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
auto send2 (std::make_shared<rai::send_block> (send1->hash (), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
auto open (std::make_shared<rai::open_block> (send1->hash (), key.pub, key.pub, key.prv, key.pub, system.work.generate (key.pub)));
ASSERT_EQ (0, system.nodes[0]->gap_cache.blocks.size ());
system.nodes[0]->block_processor.process_receive_many (rai::block_processor_item (send2));
ASSERT_EQ (1, system.nodes[0]->gap_cache.blocks.size ());
system.nodes[0]->block_processor.process_receive_many (rai::block_processor_item (open));
ASSERT_EQ (2, system.nodes[0]->gap_cache.blocks.size ());
system.nodes[0]->block_processor.process_receive_many (rai::block_processor_item (send1));
ASSERT_EQ (0, system.nodes[0]->gap_cache.blocks.size ());
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_TRUE (system.nodes[0]->store.block_exists (transaction, send1->hash ()));
ASSERT_TRUE (system.nodes[0]->store.block_exists (transaction, send2->hash ()));
ASSERT_TRUE (system.nodes[0]->store.block_exists (transaction, open->hash ()));
}

View file

@ -3,14 +3,14 @@
#include <memory>
#include <rai/lib/blocks.hpp>
#include <rai/lib/numbers.hpp>
#include <rai/lib/interface.h>
#include <rai/lib/numbers.hpp>
#include <rai/lib/work.hpp>
TEST (interface, xrb_uint256_to_string)
{
rai::uint256_union zero (0);
char text [65] = { 0 };
char text[65] = { 0 };
xrb_uint256_to_string (zero.bytes.data (), text);
ASSERT_STREQ ("0000000000000000000000000000000000000000000000000000000000000000", text);
}
@ -18,7 +18,7 @@ TEST (interface, xrb_uint256_to_string)
TEST (interface, xrb_uint256_to_address)
{
rai::uint256_union zero (0);
char text [65] = { 0 };
char text[65] = { 0 };
xrb_uint256_to_address (zero.bytes.data (), text);
ASSERT_STREQ ("xrb_1111111111111111111111111111111111111111111111111111hifc8npp", text);
}
@ -26,7 +26,7 @@ TEST (interface, xrb_uint256_to_address)
TEST (interface, xrb_uint512_to_string)
{
rai::uint512_union zero (0);
char text [129] = { 0 };
char text[129] = { 0 };
xrb_uint512_to_string (zero.bytes.data (), text);
ASSERT_STREQ ("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", text);
}
@ -85,7 +85,7 @@ TEST (interface, sign_transaction)
xrb_key_account (key.data.bytes.data (), pub.bytes.data ());
rai::send_block send (0, 0, 0, key, pub, 0);
ASSERT_FALSE (rai::validate_message (pub, send.hash (), send.signature));
send.signature.bytes [0] ^= 1;
send.signature.bytes[0] ^= 1;
ASSERT_TRUE (rai::validate_message (pub, send.hash (), send.signature));
auto transaction (xrb_sign_transaction (send.to_json ().c_str (), key.data.bytes.data ()));
boost::property_tree::ptree block_l;
@ -94,7 +94,7 @@ TEST (interface, sign_transaction)
boost::property_tree::read_json (block_stream, block_l);
auto block (rai::deserialize_block_json (block_l));
ASSERT_NE (nullptr, block);
auto send1 (dynamic_cast <rai::send_block *> (block.get ()));
auto send1 (dynamic_cast<rai::send_block *> (block.get ()));
ASSERT_NE (nullptr, send1);
ASSERT_FALSE (rai::validate_message (pub, send.hash (), send1->signature));
free (transaction);

View file

@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <rai/node/testing.hpp>
#include <cryptopp/filters.h>
#include <cryptopp/randpool.h>
#include <gtest/gtest.h>
#include <rai/node/testing.hpp>
// Init returns an error if it can't open files at the path
TEST (ledger, store_error)
@ -54,14 +54,14 @@ TEST (ledger, checksum_persistence)
ASSERT_TRUE (!init);
rai::uint256_union checksum1;
rai::uint256_union max;
max.qwords [0] = 0;
max.qwords [0] = ~max.qwords [0];
max.qwords [1] = 0;
max.qwords [1] = ~max.qwords [1];
max.qwords [2] = 0;
max.qwords [2] = ~max.qwords [2];
max.qwords [3] = 0;
max.qwords [3] = ~max.qwords [3];
max.qwords[0] = 0;
max.qwords[0] = ~max.qwords[0];
max.qwords[1] = 0;
max.qwords[1] = ~max.qwords[1];
max.qwords[2] = 0;
max.qwords[2] = ~max.qwords[2];
max.qwords[3] = 0;
max.qwords[3] = ~max.qwords[3];
rai::transaction transaction (store.environment, nullptr, true);
{
rai::ledger ledger (store);
@ -77,7 +77,7 @@ TEST (ledger, checksum_persistence)
TEST (system, system_genesis)
{
rai::system system (24000, 2);
for (auto & i: system.nodes)
for (auto & i : system.nodes)
{
rai::transaction transaction (i->store.environment, nullptr, false);
ASSERT_EQ (rai::genesis_amount, i->ledger.account_balance (transaction, rai::genesis_account));
@ -116,7 +116,7 @@ TEST (ledger, process_send)
ASSERT_EQ (2, info2.block_count);
auto latest6 (store.block_get (transaction, info2.head));
ASSERT_NE (nullptr, latest6);
auto latest7 (dynamic_cast <rai::send_block *> (latest6.get ()));
auto latest7 (dynamic_cast<rai::send_block *> (latest6.get ()));
ASSERT_NE (nullptr, latest7);
ASSERT_EQ (send, *latest7);
// Create an open block opening an account accepting the send we just created
@ -137,14 +137,14 @@ TEST (ledger, process_send)
ASSERT_FALSE (store.account_get (transaction, rai::test_genesis_key.pub, info3));
auto latest2 (store.block_get (transaction, info3.head));
ASSERT_NE (nullptr, latest2);
auto latest3 (dynamic_cast <rai::send_block *> (latest2.get ()));
auto latest3 (dynamic_cast<rai::send_block *> (latest2.get ()));
ASSERT_NE (nullptr, latest3);
ASSERT_EQ (send, *latest3);
rai::account_info info4;
ASSERT_FALSE (store.account_get (transaction, key2.pub, info4));
auto latest4 (store.block_get (transaction, info4.head));
ASSERT_NE (nullptr, latest4);
auto latest5 (dynamic_cast <rai::open_block *> (latest4.get ()));
auto latest5 (dynamic_cast<rai::open_block *> (latest4.get ()));
ASSERT_NE (nullptr, latest5);
ASSERT_EQ (open, *latest5);
ledger.rollback (transaction, hash2);
@ -219,7 +219,7 @@ TEST (ledger, process_receive)
ASSERT_EQ (rai::genesis_amount - 25, ledger.account_balance (transaction, key2.pub));
ASSERT_EQ (rai::genesis_amount - 25, ledger.weight (transaction, key3.pub));
ledger.rollback (transaction, hash4);
ASSERT_TRUE (store.block_successor(transaction, hash2).is_zero ());
ASSERT_TRUE (store.block_successor (transaction, hash2).is_zero ());
ASSERT_EQ (key2.pub, store.frontier_get (transaction, hash2));
ASSERT_TRUE (store.frontier_get (transaction, hash4).is_zero ());
ASSERT_EQ (25, ledger.account_balance (transaction, rai::test_genesis_key.pub));
@ -481,12 +481,12 @@ TEST (ledger, checksum_single)
genesis.initialize (transaction, store);
rai::ledger ledger (store);
store.checksum_put (transaction, 0, 0, genesis.hash ());
ASSERT_EQ (genesis.hash (), ledger.checksum (transaction, 0, std::numeric_limits <rai::uint256_t>::max ()));
ASSERT_EQ (genesis.hash (), ledger.checksum (transaction, 0, std::numeric_limits<rai::uint256_t>::max ()));
rai::change_block block1 (ledger.latest (transaction, rai::test_genesis_key.pub), rai::account (1), rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::checksum check1 (ledger.checksum (transaction, 0, std::numeric_limits <rai::uint256_t>::max ()));
rai::checksum check1 (ledger.checksum (transaction, 0, std::numeric_limits<rai::uint256_t>::max ()));
ASSERT_EQ (genesis.hash (), check1);
ASSERT_EQ (rai::process_result::progress, ledger.process (transaction, block1).code);
rai::checksum check2 (ledger.checksum (transaction, 0, std::numeric_limits <rai::uint256_t>::max ()));
rai::checksum check2 (ledger.checksum (transaction, 0, std::numeric_limits<rai::uint256_t>::max ()));
ASSERT_EQ (block1.hash (), check2);
}
@ -503,10 +503,10 @@ TEST (ledger, checksum_two)
rai::keypair key2;
rai::send_block block1 (ledger.latest (transaction, rai::test_genesis_key.pub), key2.pub, 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
ASSERT_EQ (rai::process_result::progress, ledger.process (transaction, block1).code);
rai::checksum check1 (ledger.checksum (transaction, 0, std::numeric_limits <rai::uint256_t>::max ()));
rai::checksum check1 (ledger.checksum (transaction, 0, std::numeric_limits<rai::uint256_t>::max ()));
rai::open_block block2 (block1.hash (), 1, key2.pub, key2.prv, key2.pub, 0);
ASSERT_EQ (rai::process_result::progress, ledger.process (transaction, block2).code);
rai::checksum check2 (ledger.checksum (transaction, 0, std::numeric_limits <rai::uint256_t>::max ()));
rai::checksum check2 (ledger.checksum (transaction, 0, std::numeric_limits<rai::uint256_t>::max ()));
ASSERT_EQ (check1, check2 ^ block2.hash ());
}
@ -517,43 +517,43 @@ TEST (ledger, DISABLED_checksum_range)
ASSERT_TRUE (!init);
rai::ledger ledger (store);
rai::transaction transaction (store.environment, nullptr, false);
rai::checksum check1 (ledger.checksum (transaction, 0, std::numeric_limits <rai::uint256_t>::max ()));
rai::checksum check1 (ledger.checksum (transaction, 0, std::numeric_limits<rai::uint256_t>::max ()));
ASSERT_TRUE (check1.is_zero ());
rai::block_hash hash1 (42);
rai::checksum check2 (ledger.checksum (transaction, 0, 42));
ASSERT_TRUE (check2.is_zero ());
rai::checksum check3 (ledger.checksum (transaction, 42, std::numeric_limits <rai::uint256_t>::max ()));
rai::checksum check3 (ledger.checksum (transaction, 42, std::numeric_limits<rai::uint256_t>::max ()));
ASSERT_EQ (hash1, check3);
}
TEST (system, generate_send_existing)
{
rai::system system (24000, 1);
rai::thread_runner runner (system.service, system.nodes [0]->config.io_threads);
rai::thread_runner runner (system.service, system.nodes[0]->config.io_threads);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::account_info info1;
{
rai::transaction transaction (system.wallet (0)->store.environment, nullptr, false);
ASSERT_FALSE (system.nodes [0]->store.account_get (transaction, rai::test_genesis_key.pub, info1));
ASSERT_FALSE (system.nodes[0]->store.account_get (transaction, rai::test_genesis_key.pub, info1));
}
std::vector <rai::account> accounts;
std::vector<rai::account> accounts;
accounts.push_back (rai::test_genesis_key.pub);
system.generate_send_existing (*system.nodes [0], accounts);
system.generate_send_existing (*system.nodes[0], accounts);
rai::account_info info2;
{
rai::transaction transaction (system.wallet (0)->store.environment, nullptr, false);
ASSERT_FALSE (system.nodes [0]->store.account_get (transaction, rai::test_genesis_key.pub, info2));
ASSERT_FALSE (system.nodes[0]->store.account_get (transaction, rai::test_genesis_key.pub, info2));
}
ASSERT_NE (info1.head, info2.head);
auto iterations1 (0);
while (system.nodes [0]->balance (rai::test_genesis_key.pub) == rai::genesis_amount)
while (system.nodes[0]->balance (rai::test_genesis_key.pub) == rai::genesis_amount)
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 20);
}
auto iterations2 (0);
while (system.nodes [0]->balance (rai::test_genesis_key.pub) != rai::genesis_amount)
while (system.nodes[0]->balance (rai::test_genesis_key.pub) != rai::genesis_amount)
{
system.poll ();
++iterations2;
@ -566,21 +566,21 @@ TEST (system, generate_send_existing)
TEST (system, generate_send_new)
{
rai::system system (24000, 1);
rai::thread_runner runner (system.service, system.nodes [0]->config.io_threads);
rai::thread_runner runner (system.service, system.nodes[0]->config.io_threads);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
auto iterator1 (system.nodes [0]->store.latest_begin (transaction));
ASSERT_NE (system.nodes [0]->store.latest_end (), iterator1);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto iterator1 (system.nodes[0]->store.latest_begin (transaction));
ASSERT_NE (system.nodes[0]->store.latest_end (), iterator1);
++iterator1;
ASSERT_EQ (system.nodes [0]->store.latest_end (), iterator1);
ASSERT_EQ (system.nodes[0]->store.latest_end (), iterator1);
}
std::vector <rai::account> accounts;
std::vector<rai::account> accounts;
accounts.push_back (rai::test_genesis_key.pub);
system.generate_send_new (*system.nodes [0], accounts);
system.generate_send_new (*system.nodes[0], accounts);
rai::account new_account (0);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto iterator2 (system.wallet (0)->store.begin (transaction));
if (iterator2->first.uint256 () != rai::test_genesis_key.pub)
{
@ -597,7 +597,7 @@ TEST (system, generate_send_new)
ASSERT_FALSE (new_account.is_zero ());
}
auto iterations (0);
while (system.nodes [0]->balance (new_account) == 0)
while (system.nodes[0]->balance (new_account) == 0)
{
system.poll ();
++iterations;
@ -718,30 +718,30 @@ TEST (ledegr, double_receive)
TEST (votes, add_one)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
{
rai::transaction transaction (node1.store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, node1.ledger.process (transaction, *send1).code);
}
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
}
auto votes1 (node1.active.roots.find (send1->root ())->election);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
auto vote1 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
auto vote2 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send1));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send1));
votes1->vote (vote1);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
auto existing1 (votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_NE (votes1->votes.rep_votes.end (), existing1);
ASSERT_EQ (*send1, *existing1->second);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*send1, *winner.second);
ASSERT_EQ (rai::genesis_amount - 100, winner.first);
@ -750,32 +750,32 @@ TEST (votes, add_one)
TEST (votes, add_two)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
{
rai::transaction transaction (node1.store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, node1.ledger.process (transaction, *send1).code);
}
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
}
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto vote1 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared <rai::vote> (key2.pub, key2.prv, 1, send2));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (key2.pub, key2.prv, 1, send2));
votes1->vote (vote2);
ASSERT_EQ (3, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send1, *votes1->votes.rep_votes [rai::test_genesis_key.pub]);
ASSERT_EQ (*send1, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (key2.pub));
ASSERT_EQ (*send2, *votes1->votes.rep_votes [key2.pub]);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (*send2, *votes1->votes.rep_votes[key2.pub]);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*send1, *winner.second);
}
@ -784,30 +784,30 @@ TEST (votes, add_two)
TEST (votes, add_existing)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
{
rai::transaction transaction (node1.store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, node1.ledger.process (transaction, *send1).code);
}
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
}
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto vote1 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send1));
votes1->vote (vote1);
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send2));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send2));
votes1->vote (vote2);
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send2, *votes1->votes.rep_votes [rai::test_genesis_key.pub]);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (*send2, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*send2, *winner.second);
}
@ -816,30 +816,30 @@ TEST (votes, add_existing)
TEST (votes, add_old)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
{
rai::transaction transaction (node1.store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, node1.ledger.process (transaction, *send1).code);
}
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, send1);
}
auto votes1 (node1.active.roots.find (send1->root ())->election);
auto vote1 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send1));
auto vote1 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 2, send1));
node1.vote_processor.vote (vote1, rai::endpoint ());
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send2));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto vote2 (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 1, send2));
node1.vote_processor.vote (vote2, rai::endpoint ());
ASSERT_EQ (2, votes1->votes.rep_votes.size ());
ASSERT_NE (votes1->votes.rep_votes.end (), votes1->votes.rep_votes.find (rai::test_genesis_key.pub));
ASSERT_EQ (*send1, *votes1->votes.rep_votes [rai::test_genesis_key.pub]);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (*send1, *votes1->votes.rep_votes[rai::test_genesis_key.pub]);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*send1, *winner.second);
}
@ -851,10 +851,10 @@ TEST (ledger, successor)
rai::keypair key1;
rai::genesis genesis;
rai::send_block send1 (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (transaction, send1).code);
ASSERT_EQ (send1, *system.nodes [0]->ledger.successor (transaction, genesis.hash ()));
ASSERT_EQ (*genesis.open, *system.nodes [0]->ledger.successor (transaction, genesis.open->root ()));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->ledger.process (transaction, send1).code);
ASSERT_EQ (send1, *system.nodes[0]->ledger.successor (transaction, genesis.hash ()));
ASSERT_EQ (*genesis.open, *system.nodes[0]->ledger.successor (transaction, genesis.open->root ()));
}
TEST (ledger, fail_change_old)

View file

@ -5,7 +5,7 @@
TEST (message, keepalive_serialization)
{
rai::keepalive request1;
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
request1.serialize (stream);
@ -19,8 +19,8 @@ TEST (message, keepalive_serialization)
TEST (message, keepalive_deserialize)
{
rai::keepalive message1;
message1.peers [0] = rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000);
std::vector <uint8_t> bytes;
message1.peers[0] = rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000);
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
message1.serialize (stream);
@ -29,7 +29,7 @@ TEST (message, keepalive_deserialize)
uint8_t version_using;
uint8_t version_min;
rai::message_type type;
std::bitset <16> extensions;
std::bitset<16> extensions;
rai::bufferstream header_stream (bytes.data (), bytes.size ());
ASSERT_FALSE (rai::message::read_header (header_stream, version_max, version_using, version_min, type, extensions));
ASSERT_EQ (rai::message_type::keepalive, type);
@ -41,31 +41,31 @@ TEST (message, keepalive_deserialize)
TEST (message, publish_serialization)
{
rai::publish publish (std::unique_ptr <rai::block> (new rai::send_block (0, 1, 2, rai::keypair ().prv, 4, 5)));
rai::publish publish (std::unique_ptr<rai::block> (new rai::send_block (0, 1, 2, rai::keypair ().prv, 4, 5)));
ASSERT_EQ (rai::block_type::send, publish.block_type ());
ASSERT_FALSE (publish.ipv4_only ());
publish.ipv4_only_set (true);
ASSERT_TRUE (publish.ipv4_only ());
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
publish.write_header (stream);
}
ASSERT_EQ (8, bytes.size ());
ASSERT_EQ (0x52, bytes [0]);
ASSERT_EQ (0x41, bytes [1]);
ASSERT_EQ (0x05, bytes [2]);
ASSERT_EQ (0x05, bytes [3]);
ASSERT_EQ (0x01, bytes [4]);
ASSERT_EQ (static_cast <uint8_t> (rai::message_type::publish), bytes [5]);
ASSERT_EQ (0x02, bytes [6]);
ASSERT_EQ (static_cast <uint8_t> (rai::block_type::send), bytes [7]);
ASSERT_EQ (0x52, bytes[0]);
ASSERT_EQ (0x41, bytes[1]);
ASSERT_EQ (0x05, bytes[2]);
ASSERT_EQ (0x05, bytes[3]);
ASSERT_EQ (0x01, bytes[4]);
ASSERT_EQ (static_cast<uint8_t> (rai::message_type::publish), bytes[5]);
ASSERT_EQ (0x02, bytes[6]);
ASSERT_EQ (static_cast<uint8_t> (rai::block_type::send), bytes[7]);
rai::bufferstream stream (bytes.data (), bytes.size ());
uint8_t version_max;
uint8_t version_using;
uint8_t version_min;
rai::message_type type;
std::bitset <16> extensions;
std::bitset<16> extensions;
ASSERT_FALSE (rai::message::read_header (stream, version_max, version_using, version_min, type, extensions));
ASSERT_EQ (0x01, version_min);
ASSERT_EQ (0x05, version_using);
@ -76,9 +76,9 @@ TEST (message, publish_serialization)
TEST (message, confirm_ack_serialization)
{
rai::keypair key1;
auto vote (std::make_shared <rai::vote> (key1.pub, key1.prv, 0, std::unique_ptr <rai::block> (new rai::send_block (0, 1, 2, key1.prv, 4, 5))));
auto vote (std::make_shared<rai::vote> (key1.pub, key1.prv, 0, std::unique_ptr<rai::block> (new rai::send_block (0, 1, 2, key1.prv, 4, 5))));
rai::confirm_ack con1 (vote);
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream1 (bytes);
con1.serialize (stream1);

View file

@ -1,7 +1,8 @@
#include <gtest/gtest.h>
#include <rai/node/testing.hpp>
namespace {
namespace
{
class test_visitor : public rai::message_visitor
{
public:
@ -58,10 +59,10 @@ TEST (message_parser, exact_confirm_ack_size)
rai::system system (24000, 1);
test_visitor visitor;
rai::message_parser parser (visitor, system.work);
auto block (std::unique_ptr <rai::send_block> (new rai::send_block (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1))));
auto vote (std::make_shared <rai::vote> (0, rai::keypair ().prv, 0, std::move (block)));
auto block (std::unique_ptr<rai::send_block> (new rai::send_block (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1))));
auto vote (std::make_shared<rai::vote> (0, rai::keypair ().prv, 0, std::move (block)));
rai::confirm_ack message (vote);
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
message.serialize (stream);
@ -82,9 +83,9 @@ TEST (message_parser, exact_confirm_req_size)
rai::system system (24000, 1);
test_visitor visitor;
rai::message_parser parser (visitor, system.work);
auto block (std::unique_ptr <rai::send_block> (new rai::send_block (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1))));
auto block (std::unique_ptr<rai::send_block> (new rai::send_block (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1))));
rai::confirm_req message (std::move (block));
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
message.serialize (stream);
@ -105,9 +106,9 @@ TEST (message_parser, exact_publish_size)
rai::system system (24000, 1);
test_visitor visitor;
rai::message_parser parser (visitor, system.work);
auto block (std::unique_ptr <rai::send_block> (new rai::send_block (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1))));
auto block (std::unique_ptr<rai::send_block> (new rai::send_block (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1))));
rai::publish message (std::move (block));
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
message.serialize (stream);
@ -129,7 +130,7 @@ TEST (message_parser, exact_keepalive_size)
test_visitor visitor;
rai::message_parser parser (visitor, system.work);
rai::keepalive message;
std::vector <uint8_t> bytes;
std::vector<uint8_t> bytes;
{
rai::vectorstream stream (bytes);
message.serialize (stream);

View file

@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include <boost/thread.hpp>
#include <gtest/gtest.h>
#include <rai/node/testing.hpp>
TEST (network, tcp_connection)
@ -14,29 +14,26 @@ TEST (network, tcp_connection)
boost::asio::ip::tcp::socket incoming (service);
auto done1 (false);
std::string message1;
acceptor.async_accept (incoming,
[&done1, &message1] (boost::system::error_code const & ec_a)
{
acceptor.async_accept (incoming,
[&done1, &message1](boost::system::error_code const & ec_a) {
if (ec_a)
{
message1 = ec_a.message ();
std::cerr << message1;
}
done1 = true;}
);
done1 = true; });
boost::asio::ip::tcp::socket connector (service);
auto done2 (false);
std::string message2;
connector.async_connect (boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v4::loopback (), 24000),
[&done2, &message2] (boost::system::error_code const & ec_a)
connector.async_connect (boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v4::loopback (), 24000),
[&done2, &message2](boost::system::error_code const & ec_a) {
if (ec_a)
{
if (ec_a)
{
message2 = ec_a.message ();
std::cerr << message2;
}
done2 = true;
});
message2 = ec_a.message ();
std::cerr << message2;
}
done2 = true;
});
while (!done1 || !done2)
{
service.poll ();
@ -49,58 +46,58 @@ TEST (network, construction)
{
rai::system system (24000, 1);
ASSERT_EQ (1, system.nodes.size ());
ASSERT_EQ (24000, system.nodes [0]->network.socket.local_endpoint ().port ());
ASSERT_EQ (24000, system.nodes[0]->network.socket.local_endpoint ().port ());
}
TEST (network, self_discard)
{
rai::system system (24000, 1);
system.nodes [0]->network.remote = system.nodes [0]->network.endpoint ();
ASSERT_EQ (0, system.nodes [0]->network.bad_sender_count);
system.nodes [0]->network.receive_action (boost::system::error_code {}, 0);
ASSERT_EQ (1, system.nodes [0]->network.bad_sender_count);
system.nodes[0]->network.remote = system.nodes[0]->network.endpoint ();
ASSERT_EQ (0, system.nodes[0]->network.bad_sender_count);
system.nodes[0]->network.receive_action (boost::system::error_code{}, 0);
ASSERT_EQ (1, system.nodes[0]->network.bad_sender_count);
}
TEST (network, send_keepalive)
{
rai::system system (24000, 1);
auto list1 (system.nodes [0]->peers.list ());
auto list1 (system.nodes[0]->peers.list ());
ASSERT_EQ (0, list1.size ());
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
system.nodes [0]->network.send_keepalive (node1->network.endpoint ());
auto initial (system.nodes [0]->network.incoming.keepalive.load ());
ASSERT_EQ (0, system.nodes [0]->peers.list ().size ());
system.nodes[0]->network.send_keepalive (node1->network.endpoint ());
auto initial (system.nodes[0]->network.incoming.keepalive.load ());
ASSERT_EQ (0, system.nodes[0]->peers.list ().size ());
ASSERT_EQ (0, node1->peers.list ().size ());
auto iterations (0);
while (system.nodes [0]->network.incoming.keepalive == initial)
while (system.nodes[0]->network.incoming.keepalive == initial)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
auto peers1 (system.nodes [0]->peers.list ());
auto peers1 (system.nodes[0]->peers.list ());
auto peers2 (node1->peers.list ());
ASSERT_EQ (1, peers1.size ());
ASSERT_EQ (1, peers2.size ());
ASSERT_NE (peers1.end (), std::find_if (peers1.begin (), peers1.end (), [&node1] (rai::endpoint const & information_a) {return information_a == node1->network.endpoint ();}));
ASSERT_NE (peers2.end (), std::find_if (peers2.begin (), peers2.end (), [&system] (rai::endpoint const & information_a) {return information_a == system.nodes [0]->network.endpoint ();}));
ASSERT_NE (peers1.end (), std::find_if (peers1.begin (), peers1.end (), [&node1](rai::endpoint const & information_a) { return information_a == node1->network.endpoint (); }));
ASSERT_NE (peers2.end (), std::find_if (peers2.begin (), peers2.end (), [&system](rai::endpoint const & information_a) { return information_a == system.nodes[0]->network.endpoint (); }));
node1->stop ();
}
TEST (network, keepalive_ipv4)
{
rai::system system (24000, 1);
auto list1 (system.nodes [0]->peers.list ());
auto list1 (system.nodes[0]->peers.list ());
ASSERT_EQ (0, list1.size ());
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
node1->start ();
node1->send_keepalive (rai::endpoint (boost::asio::ip::address_v4::loopback (), 24000));
auto initial (system.nodes [0]->network.incoming.keepalive.load ());
auto initial (system.nodes[0]->network.incoming.keepalive.load ());
auto iterations (0);
while (system.nodes [0]->network.incoming.keepalive == initial)
while (system.nodes[0]->network.incoming.keepalive == initial)
{
system.poll ();
++iterations;
@ -112,30 +109,30 @@ TEST (network, keepalive_ipv4)
TEST (network, multi_keepalive)
{
rai::system system (24000, 1);
auto list1 (system.nodes [0]->peers.list ());
auto list1 (system.nodes[0]->peers.list ());
ASSERT_EQ (0, list1.size ());
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->start ();
ASSERT_EQ (0, node1->peers.size ());
node1->network.send_keepalive (system.nodes [0]->network.endpoint ());
node1->network.send_keepalive (system.nodes[0]->network.endpoint ());
ASSERT_EQ (0, node1->peers.size ());
ASSERT_EQ (0, system.nodes [0]->peers.size ());
ASSERT_EQ (0, system.nodes[0]->peers.size ());
auto iterations1 (0);
while (system.nodes [0]->peers.size () != 1)
while (system.nodes[0]->peers.size () != 1)
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
}
rai::node_init init2;
auto node2 (std::make_shared <rai::node> (init2, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
auto node2 (std::make_shared<rai::node> (init2, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init2.error ());
node2->start ();
node2->network.send_keepalive (system.nodes [0]->network.endpoint ());
node2->network.send_keepalive (system.nodes[0]->network.endpoint ());
auto iterations2 (0);
while (node1->peers.size () != 2 || system.nodes [0]->peers.size () != 2 || node2->peers.size () != 2)
while (node1->peers.size () != 2 || system.nodes[0]->peers.size () != 2 || node2->peers.size () != 2)
{
system.poll ();
++iterations2;
@ -148,47 +145,47 @@ TEST (network, multi_keepalive)
TEST (network, send_discarded_publish)
{
rai::system system (24000, 2);
auto block (std::make_shared <rai::send_block> (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1)));
auto block (std::make_shared<rai::send_block> (1, 1, 2, rai::keypair ().prv, 4, system.work.generate (1)));
rai::genesis genesis;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
system.nodes [0]->network.republish_block (transaction, block);
ASSERT_EQ (genesis.hash (), system.nodes [0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes [1]->latest (rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.nodes[0]->network.republish_block (transaction, block);
ASSERT_EQ (genesis.hash (), system.nodes[0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes[1]->latest (rai::test_genesis_key.pub));
}
auto iterations (0);
while (system.nodes [1]->network.incoming.publish == 0)
while (system.nodes[1]->network.incoming.publish == 0)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (genesis.hash (), system.nodes [0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes [1]->latest (rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_EQ (genesis.hash (), system.nodes[0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes[1]->latest (rai::test_genesis_key.pub));
}
TEST (network, send_invalid_publish)
{
rai::system system (24000, 2);
rai::genesis genesis;
auto block (std::make_shared <rai::send_block> (1, 1, 20, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (1)));
auto block (std::make_shared<rai::send_block> (1, 1, 20, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (1)));
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
system.nodes [0]->network.republish_block (transaction, block);
ASSERT_EQ (genesis.hash (), system.nodes [0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes [1]->latest (rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.nodes[0]->network.republish_block (transaction, block);
ASSERT_EQ (genesis.hash (), system.nodes[0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes[1]->latest (rai::test_genesis_key.pub));
}
auto iterations (0);
while (system.nodes [1]->network.incoming.publish == 0)
while (system.nodes[1]->network.incoming.publish == 0)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (genesis.hash (), system.nodes [0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes [1]->latest (rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_EQ (genesis.hash (), system.nodes[0]->ledger.latest (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (genesis.hash (), system.nodes[1]->latest (rai::test_genesis_key.pub));
}
TEST (network, send_valid_confirm_ack)
@ -197,20 +194,20 @@ TEST (network, send_valid_confirm_ack)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (1)->insert_adhoc (key2.prv);
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
rai::send_block block2 (latest1, key2.pub, 50, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (latest1));
rai::block_hash latest2 (system.nodes [1]->latest (rai::test_genesis_key.pub));
system.nodes [0]->process_active (std::unique_ptr <rai::block> (new rai::send_block (block2)));
rai::block_hash latest2 (system.nodes[1]->latest (rai::test_genesis_key.pub));
system.nodes[0]->process_active (std::unique_ptr<rai::block> (new rai::send_block (block2)));
auto iterations (0);
// Keep polling until latest block changes
while (system.nodes [1]->latest (rai::test_genesis_key.pub) == latest2)
while (system.nodes[1]->latest (rai::test_genesis_key.pub) == latest2)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
// Make sure the balance has decreased after procssing the block.
ASSERT_EQ (50, system.nodes [1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (50, system.nodes[1]->balance (rai::test_genesis_key.pub));
}
TEST (network, send_valid_publish)
@ -219,61 +216,61 @@ TEST (network, send_valid_publish)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
system.wallet (1)->insert_adhoc (key2.prv);
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
rai::send_block block2 (latest1, key2.pub, 50, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (latest1));
auto hash2 (block2.hash ());
rai::block_hash latest2 (system.nodes [1]->latest (rai::test_genesis_key.pub));
system.nodes [1]->process_active (std::unique_ptr <rai::block> (new rai::send_block (block2)));
rai::block_hash latest2 (system.nodes[1]->latest (rai::test_genesis_key.pub));
system.nodes[1]->process_active (std::unique_ptr<rai::block> (new rai::send_block (block2)));
auto iterations (0);
while (system.nodes [0]->network.incoming.publish == 0)
while (system.nodes[0]->network.incoming.publish == 0)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
rai::block_hash latest3 (system.nodes [1]->latest (rai::test_genesis_key.pub));
rai::block_hash latest3 (system.nodes[1]->latest (rai::test_genesis_key.pub));
ASSERT_NE (latest2, latest3);
ASSERT_EQ (hash2, latest3);
ASSERT_EQ (50, system.nodes [1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (50, system.nodes[1]->balance (rai::test_genesis_key.pub));
}
TEST (network, send_insufficient_work)
{
rai::system system (24000, 2);
std::unique_ptr <rai::send_block> block (new rai::send_block (0, 1, 20, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
std::unique_ptr<rai::send_block> block (new rai::send_block (0, 1, 20, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
rai::publish publish (std::move (block));
std::shared_ptr <std::vector <uint8_t>> bytes (new std::vector <uint8_t>);
std::shared_ptr<std::vector<uint8_t>> bytes (new std::vector<uint8_t>);
{
rai::vectorstream stream (*bytes);
publish.serialize (stream);
}
auto node1 (system.nodes [1]->shared ());
system.nodes [0]->network.send_buffer (bytes->data (), bytes->size (), system.nodes [1]->network.endpoint (), [bytes, node1] (boost::system::error_code const & ec, size_t size) {});
ASSERT_EQ (0, system.nodes [0]->network.insufficient_work_count);
auto node1 (system.nodes[1]->shared ());
system.nodes[0]->network.send_buffer (bytes->data (), bytes->size (), system.nodes[1]->network.endpoint (), [bytes, node1](boost::system::error_code const & ec, size_t size) {});
ASSERT_EQ (0, system.nodes[0]->network.insufficient_work_count);
auto iterations (0);
while (system.nodes [1]->network.insufficient_work_count == 0)
while (system.nodes[1]->network.insufficient_work_count == 0)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (1, system.nodes [1]->network.insufficient_work_count);
ASSERT_EQ (1, system.nodes[1]->network.insufficient_work_count);
}
TEST (receivable_processor, confirm_insufficient_pos)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
auto block1 (std::make_shared <rai::send_block> (genesis.hash (), 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto block1 (std::make_shared<rai::send_block> (genesis.hash (), 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_EQ (rai::process_result::progress, node1.process (*block1).code);
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, block1);
}
rai::keypair key1;
auto vote (std::make_shared <rai::vote> (key1.pub, key1.prv, 0, block1));
auto vote (std::make_shared<rai::vote> (key1.pub, key1.prv, 0, block1));
rai::confirm_ack con1 (vote);
node1.process_message (con1, node1.network.endpoint ());
}
@ -281,52 +278,52 @@ TEST (receivable_processor, confirm_insufficient_pos)
TEST (receivable_processor, confirm_sufficient_pos)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
rai::genesis genesis;
auto block1 (std::make_shared <rai::send_block> (genesis.hash (), 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto block1 (std::make_shared<rai::send_block> (genesis.hash (), 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
ASSERT_EQ (rai::process_result::progress, node1.process (*block1).code);
auto node_l (system.nodes [0]);
auto node_l (system.nodes[0]);
{
rai::transaction transaction (node1.store.environment, nullptr, true);
node1.active.start (transaction, block1);
}
auto vote (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 0, block1));
auto vote (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, 0, block1));
rai::confirm_ack con1 (vote);
node1.process_message (con1, node1.network.endpoint ());
}
TEST (receivable_processor, send_with_receive)
{
auto amount (std::numeric_limits <rai::uint128_t>::max ());
auto amount (std::numeric_limits<rai::uint128_t>::max ());
rai::system system (24000, 2);
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
system.wallet (1)->insert_adhoc (key2.prv);
auto block1 (std::make_shared <rai::send_block> (latest1, key2.pub, amount - system.nodes [0]->config.receive_minimum.number (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (latest1)));
ASSERT_EQ (amount, system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes [0]->balance (key2.pub));
ASSERT_EQ (amount, system.nodes [1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes [1]->balance (key2.pub));
system.nodes [0]->process_active (block1);
system.nodes [0]->block_processor.flush ();
system.nodes [1]->process_active (block1);
system.nodes [1]->block_processor.flush ();
ASSERT_EQ (amount - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes [0]->balance (key2.pub));
ASSERT_EQ (amount - system.nodes [0]->config.receive_minimum.number (), system.nodes [1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes [1]->balance (key2.pub));
auto block1 (std::make_shared<rai::send_block> (latest1, key2.pub, amount - system.nodes[0]->config.receive_minimum.number (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (latest1)));
ASSERT_EQ (amount, system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes[0]->balance (key2.pub));
ASSERT_EQ (amount, system.nodes[1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes[1]->balance (key2.pub));
system.nodes[0]->process_active (block1);
system.nodes[0]->block_processor.flush ();
system.nodes[1]->process_active (block1);
system.nodes[1]->block_processor.flush ();
ASSERT_EQ (amount - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes[0]->balance (key2.pub));
ASSERT_EQ (amount - system.nodes[0]->config.receive_minimum.number (), system.nodes[1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes[1]->balance (key2.pub));
auto iterations (0);
while (system.nodes [0]->balance (key2.pub) != system.nodes [0]->config.receive_minimum.number ())
while (system.nodes[0]->balance (key2.pub) != system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (amount - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (key2.pub));
ASSERT_EQ (amount - system.nodes [0]->config.receive_minimum.number (), system.nodes [1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (system.nodes [0]->config.receive_minimum.number (), system.nodes [1]->balance (key2.pub));
ASSERT_EQ (amount - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (key2.pub));
ASSERT_EQ (amount - system.nodes[0]->config.receive_minimum.number (), system.nodes[1]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (system.nodes[0]->config.receive_minimum.number (), system.nodes[1]->balance (key2.pub));
}
TEST (network, receive_weight_change)
@ -336,12 +333,12 @@ TEST (network, receive_weight_change)
rai::keypair key2;
system.wallet (1)->insert_adhoc (key2.prv);
{
rai::transaction transaction (system.nodes [1]->store.environment, nullptr, true);
rai::transaction transaction (system.nodes[1]->store.environment, nullptr, true);
system.wallet (1)->store.representative_set (transaction, key2.pub);
}
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations (0);
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr <rai::node> const & node_a) { return node_a->weight (key2.pub) != system.nodes [0]->config.receive_minimum.number (); }))
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&](std::shared_ptr<rai::node> const & node_a) { return node_a->weight (key2.pub) != system.nodes[0]->config.receive_minimum.number (); }))
{
system.poll ();
++iterations;
@ -397,12 +394,12 @@ TEST (parse_endpoint, no_colon)
TEST (bulk_pull, no_address)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::bulk_pull> req (new rai::bulk_pull);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::bulk_pull> req (new rai::bulk_pull);
req->start = 1;
req->end = 2;
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::bulk_pull_server> (connection, std::move (req)));
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::bulk_pull_server> (connection, std::move (req)));
ASSERT_EQ (request->current, request->request->end);
ASSERT_TRUE (request->current.is_zero ());
}
@ -410,13 +407,13 @@ TEST (bulk_pull, no_address)
TEST (bulk_pull, genesis_to_end)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::bulk_pull> req (new rai::bulk_pull {});
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::bulk_pull> req (new rai::bulk_pull{});
req->start = rai::test_genesis_key.pub;
req->end.clear ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::bulk_pull_server> (connection, std::move (req)));
ASSERT_EQ (system.nodes [0]->latest (rai::test_genesis_key.pub), request->current);
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::bulk_pull_server> (connection, std::move (req)));
ASSERT_EQ (system.nodes[0]->latest (rai::test_genesis_key.pub), request->current);
ASSERT_EQ (request->request->end, request->request->end);
}
@ -424,13 +421,13 @@ TEST (bulk_pull, genesis_to_end)
TEST (bulk_pull, no_end)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::bulk_pull> req (new rai::bulk_pull {});
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::bulk_pull> req (new rai::bulk_pull{});
req->start = rai::test_genesis_key.pub;
req->end = 1;
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::bulk_pull_server> (connection, std::move (req)));
ASSERT_EQ (system.nodes [0]->latest (rai::test_genesis_key.pub), request->current);
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::bulk_pull_server> (connection, std::move (req)));
ASSERT_EQ (system.nodes[0]->latest (rai::test_genesis_key.pub), request->current);
ASSERT_TRUE (request->request->end.is_zero ());
}
@ -440,33 +437,33 @@ TEST (bulk_pull, end_not_owned)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, 100));
rai::block_hash latest (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest (system.nodes[0]->latest (rai::test_genesis_key.pub));
rai::open_block open (0, 1, 2, rai::keypair ().prv, 4, 5);
open.hashables.account = key2.pub;
open.hashables.representative = key2.pub;
open.hashables.source = latest;
open.signature = rai::sign_message (key2.prv, key2.pub, open.hash ());
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (open).code);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (open).code);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
rai::genesis genesis;
std::unique_ptr <rai::bulk_pull> req (new rai::bulk_pull {});
std::unique_ptr<rai::bulk_pull> req (new rai::bulk_pull{});
req->start = key2.pub;
req->end = genesis.hash ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::bulk_pull_server> (connection, std::move (req)));
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::bulk_pull_server> (connection, std::move (req)));
ASSERT_EQ (request->current, request->request->end);
}
TEST (bulk_pull, none)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
rai::genesis genesis;
std::unique_ptr <rai::bulk_pull> req (new rai::bulk_pull {});
std::unique_ptr<rai::bulk_pull> req (new rai::bulk_pull{});
req->start = genesis.hash ();
req->end = genesis.hash ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::bulk_pull_server> (connection, std::move (req)));
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::bulk_pull_server> (connection, std::move (req)));
auto block (request->get_next ());
ASSERT_EQ (nullptr, block);
}
@ -474,12 +471,12 @@ TEST (bulk_pull, none)
TEST (bulk_pull, get_next_on_open)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::bulk_pull> req (new rai::bulk_pull {});
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::bulk_pull> req (new rai::bulk_pull{});
req->start = rai::test_genesis_key.pub;
req->end.clear ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::bulk_pull_server> (connection, std::move (req)));
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::bulk_pull_server> (connection, std::move (req)));
auto block (request->get_next ());
ASSERT_NE (nullptr, block);
ASSERT_TRUE (block->previous ().is_zero ());
@ -491,10 +488,10 @@ TEST (bootstrap_processor, DISABLED_process_none)
{
rai::system system (24000, 1);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto done (false);
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
while (!done)
{
system.service.run_one ();
@ -509,14 +506,14 @@ TEST (bootstrap_processor, process_one)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, rai::test_genesis_key.pub, 100));
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
rai::block_hash hash1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
rai::block_hash hash1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
rai::block_hash hash2 (node1->latest (rai::test_genesis_key.pub));
ASSERT_NE (hash1, hash2);
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations (0);
ASSERT_NE (node1->latest (rai::test_genesis_key.pub), system.nodes [0]->latest (rai::test_genesis_key.pub));
while (node1->latest (rai::test_genesis_key.pub) != system.nodes [0]->latest (rai::test_genesis_key.pub))
ASSERT_NE (node1->latest (rai::test_genesis_key.pub), system.nodes[0]->latest (rai::test_genesis_key.pub));
while (node1->latest (rai::test_genesis_key.pub) != system.nodes[0]->latest (rai::test_genesis_key.pub))
{
system.poll ();
++iterations;
@ -530,21 +527,21 @@ TEST (bootstrap_processor, process_two)
{
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::block_hash hash1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash hash1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, rai::test_genesis_key.pub, 50));
rai::block_hash hash2 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash hash2 (system.nodes[0]->latest (rai::test_genesis_key.pub));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, rai::test_genesis_key.pub, 50));
rai::block_hash hash3 (system.nodes [0]->latest ( rai::test_genesis_key.pub));
rai::block_hash hash3 (system.nodes[0]->latest (rai::test_genesis_key.pub));
ASSERT_NE (hash1, hash2);
ASSERT_NE (hash1, hash3);
ASSERT_NE (hash2, hash3);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations (0);
ASSERT_NE (node1->latest (rai::test_genesis_key.pub), system.nodes [0]->latest (rai::test_genesis_key.pub));
while (node1->latest (rai::test_genesis_key.pub) != system.nodes [0]->latest (rai::test_genesis_key.pub))
ASSERT_NE (node1->latest (rai::test_genesis_key.pub), system.nodes[0]->latest (rai::test_genesis_key.pub));
while (node1->latest (rai::test_genesis_key.pub) != system.nodes[0]->latest (rai::test_genesis_key.pub))
{
system.poll ();
++iterations;
@ -559,20 +556,20 @@ TEST (bootstrap_processor, process_new)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
system.wallet (1)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations1 (0);
while (system.nodes [0]->balance (key2.pub).is_zero ())
while (system.nodes[0]->balance (key2.pub).is_zero ())
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
}
rai::uint128_t balance1 (system.nodes [0]->balance (rai::test_genesis_key.pub));
rai::uint128_t balance2 (system.nodes [0]->balance (key2.pub));
rai::uint128_t balance1 (system.nodes[0]->balance (rai::test_genesis_key.pub));
rai::uint128_t balance2 (system.nodes[0]->balance (key2.pub));
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations2 (0);
while (node1->balance (key2.pub) != balance2)
{
@ -588,18 +585,18 @@ TEST (bootstrap_processor, pull_diamond)
{
rai::system system (24000, 1);
rai::keypair key;
std::unique_ptr <rai::send_block> send1 (new rai::send_block (system.nodes [0]->latest (rai::test_genesis_key.pub), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (system.nodes [0]->latest (rai::test_genesis_key.pub))));
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (*send1).code);
std::unique_ptr <rai::open_block> open (new rai::open_block (send1->hash (), 1, key.pub, key.prv, key.pub, system.work.generate (key.pub)));
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (*open).code);
std::unique_ptr <rai::send_block> send2 (new rai::send_block (open->hash (), rai::test_genesis_key.pub, std::numeric_limits <rai::uint128_t>::max () - 100, key.prv, key.pub, system.work.generate (open->hash ())));
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (*send2).code);
std::unique_ptr <rai::receive_block> receive (new rai::receive_block (send1->hash (), send2->hash (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (*receive).code);
std::unique_ptr<rai::send_block> send1 (new rai::send_block (system.nodes[0]->latest (rai::test_genesis_key.pub), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (system.nodes[0]->latest (rai::test_genesis_key.pub))));
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (*send1).code);
std::unique_ptr<rai::open_block> open (new rai::open_block (send1->hash (), 1, key.pub, key.prv, key.pub, system.work.generate (key.pub)));
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (*open).code);
std::unique_ptr<rai::send_block> send2 (new rai::send_block (open->hash (), rai::test_genesis_key.pub, std::numeric_limits<rai::uint128_t>::max () - 100, key.prv, key.pub, system.work.generate (open->hash ())));
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (*send2).code);
std::unique_ptr<rai::receive_block> receive (new rai::receive_block (send1->hash (), send2->hash (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (*receive).code);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations (0);
while (node1->balance (rai::test_genesis_key.pub) != 100)
{
@ -616,28 +613,28 @@ TEST (bootstrap_processor, push_diamond)
rai::system system (24000, 1);
rai::keypair key;
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24002, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
auto wallet1 (node1->wallets.create (100));
wallet1->insert_adhoc (rai::test_genesis_key.prv);
wallet1->insert_adhoc (key.prv);
std::unique_ptr <rai::send_block> send1 (new rai::send_block (system.nodes [0]->latest (rai::test_genesis_key.pub), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (system.nodes [0]->latest (rai::test_genesis_key.pub))));
std::unique_ptr<rai::send_block> send1 (new rai::send_block (system.nodes[0]->latest (rai::test_genesis_key.pub), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (system.nodes[0]->latest (rai::test_genesis_key.pub))));
ASSERT_EQ (rai::process_result::progress, node1->process (*send1).code);
std::unique_ptr <rai::open_block> open (new rai::open_block (send1->hash (), 1, key.pub, key.prv, key.pub, system.work.generate (key.pub)));
std::unique_ptr<rai::open_block> open (new rai::open_block (send1->hash (), 1, key.pub, key.prv, key.pub, system.work.generate (key.pub)));
ASSERT_EQ (rai::process_result::progress, node1->process (*open).code);
std::unique_ptr <rai::send_block> send2 (new rai::send_block (open->hash (), rai::test_genesis_key.pub, std::numeric_limits <rai::uint128_t>::max () - 100, key.prv, key.pub, system.work.generate (open->hash ())));
std::unique_ptr<rai::send_block> send2 (new rai::send_block (open->hash (), rai::test_genesis_key.pub, std::numeric_limits<rai::uint128_t>::max () - 100, key.prv, key.pub, system.work.generate (open->hash ())));
ASSERT_EQ (rai::process_result::progress, node1->process (*send2).code);
std::unique_ptr <rai::receive_block> receive (new rai::receive_block (send1->hash (), send2->hash (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
std::unique_ptr<rai::receive_block> receive (new rai::receive_block (send1->hash (), send2->hash (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1->hash ())));
ASSERT_EQ (rai::process_result::progress, node1->process (*receive).code);
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations (0);
while (system.nodes [0]->balance (rai::test_genesis_key.pub) != 100)
while (system.nodes[0]->balance (rai::test_genesis_key.pub) != 100)
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (100, system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (100, system.nodes[0]->balance (rai::test_genesis_key.pub));
node1->stop ();
}
@ -646,16 +643,16 @@ TEST (bootstrap_processor, push_one)
rai::system system (24000, 1);
rai::node_init init1;
rai::keypair key1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto wallet (node1->wallets.create (rai::uint256_union ()));
ASSERT_NE (nullptr, wallet);
wallet->insert_adhoc (rai::test_genesis_key.prv);
rai::uint128_t balance1 (node1->balance (rai::test_genesis_key.pub));
ASSERT_NE (nullptr, wallet->send_action (rai::test_genesis_key.pub, key1.pub, 100));
ASSERT_NE (balance1, node1->balance (rai::test_genesis_key.pub));
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations (0);
while (system.nodes [0]->balance (rai::test_genesis_key.pub) == balance1)
while (system.nodes[0]->balance (rai::test_genesis_key.pub) == balance1)
{
system.poll ();
++iterations;
@ -667,16 +664,16 @@ TEST (bootstrap_processor, push_one)
TEST (frontier_req_response, DISABLED_destruction)
{
{
std::shared_ptr <rai::frontier_req_server> hold; // Destructing tcp acceptor on non-existent io_service
std::shared_ptr<rai::frontier_req_server> hold; // Destructing tcp acceptor on non-existent io_service
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::frontier_req> req (new rai::frontier_req);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::frontier_req> req (new rai::frontier_req);
req->start.clear ();
req->age = std::numeric_limits <decltype (req->age)>::max ();
req->count = std::numeric_limits <decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr <rai::message> {});
hold = std::make_shared <rai::frontier_req_server> (connection, std::move (req));
req->age = std::numeric_limits<decltype (req->age)>::max ();
req->count = std::numeric_limits<decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr<rai::message>{});
hold = std::make_shared<rai::frontier_req_server> (connection, std::move (req));
}
}
ASSERT_TRUE (true);
@ -685,13 +682,13 @@ TEST (frontier_req_response, DISABLED_destruction)
TEST (frontier_req, begin)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::frontier_req> req (new rai::frontier_req);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::frontier_req> req (new rai::frontier_req);
req->start.clear ();
req->age = std::numeric_limits <decltype (req->age)>::max ();
req->count = std::numeric_limits <decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::frontier_req_server> (connection, std::move (req)));
req->age = std::numeric_limits<decltype (req->age)>::max ();
req->count = std::numeric_limits<decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::frontier_req_server> (connection, std::move (req)));
ASSERT_EQ (rai::test_genesis_key.pub, request->current);
rai::genesis genesis;
ASSERT_EQ (genesis.hash (), request->info.head);
@ -700,39 +697,39 @@ TEST (frontier_req, begin)
TEST (frontier_req, end)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::frontier_req> req (new rai::frontier_req);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::frontier_req> req (new rai::frontier_req);
req->start = rai::test_genesis_key.pub.number () + 1;
req->age = std::numeric_limits <decltype (req->age)>::max ();
req->count = std::numeric_limits <decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::frontier_req_server> (connection, std::move (req)));
req->age = std::numeric_limits<decltype (req->age)>::max ();
req->count = std::numeric_limits<decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::frontier_req_server> (connection, std::move (req)));
ASSERT_TRUE (request->current.is_zero ());
}
TEST (frontier_req, time_bound)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::frontier_req> req (new rai::frontier_req);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::frontier_req> req (new rai::frontier_req);
req->start.clear ();
req->age = 0;
req->count = std::numeric_limits <decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::frontier_req_server> (connection, std::move (req)));
req->count = std::numeric_limits<decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::frontier_req_server> (connection, std::move (req)));
ASSERT_TRUE (request->current.is_zero ());
}
TEST (frontier_req, time_cutoff)
{
rai::system system (24000, 1);
auto connection (std::make_shared <rai::bootstrap_server> (nullptr, system.nodes [0]));
std::unique_ptr <rai::frontier_req> req (new rai::frontier_req);
auto connection (std::make_shared<rai::bootstrap_server> (nullptr, system.nodes[0]));
std::unique_ptr<rai::frontier_req> req (new rai::frontier_req);
req->start.clear ();
req->age = 10;
req->count = std::numeric_limits <decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr <rai::message> {});
auto request (std::make_shared <rai::frontier_req_server> (connection, std::move (req)));
req->count = std::numeric_limits<decltype (req->count)>::max ();
connection->requests.push (std::unique_ptr<rai::message>{});
auto request (std::make_shared<rai::frontier_req_server> (connection, std::move (req)));
ASSERT_EQ (rai::test_genesis_key.pub, request->current);
rai::genesis genesis;
ASSERT_EQ (genesis.hash (), request->info.head);
@ -743,24 +740,24 @@ TEST (bulk, genesis)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest2 (node1->latest (rai::test_genesis_key.pub));
ASSERT_EQ (latest1, latest2);
rai::keypair key2;
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, 100));
rai::block_hash latest3 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest3 (system.nodes[0]->latest (rai::test_genesis_key.pub));
ASSERT_NE (latest1, latest3);
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations (0);
while (node1->latest (rai::test_genesis_key.pub) != system.nodes [0]->latest (rai::test_genesis_key.pub))
while (node1->latest (rai::test_genesis_key.pub) != system.nodes[0]->latest (rai::test_genesis_key.pub))
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (node1->latest (rai::test_genesis_key.pub), system.nodes [0]->latest (rai::test_genesis_key.pub));
ASSERT_EQ (node1->latest (rai::test_genesis_key.pub), system.nodes[0]->latest (rai::test_genesis_key.pub));
node1->stop ();
}
@ -769,9 +766,9 @@ TEST (bulk, offline_send)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->network.send_keepalive (system.nodes [0]->network.endpoint ());
node1->network.send_keepalive (system.nodes[0]->network.endpoint ());
node1->start ();
auto iterations (0);
do
@ -779,15 +776,15 @@ TEST (bulk, offline_send)
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
} while (system.nodes [0]->peers.empty () || node1->peers.empty ());
} while (system.nodes[0]->peers.empty () || node1->peers.empty ());
rai::keypair key2;
auto wallet (node1->wallets.create (rai::uint256_union ()));
wallet->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (std::numeric_limits <rai::uint256_t>::max (), system.nodes [0]->balance (rai::test_genesis_key.pub));
node1->bootstrap_initiator.bootstrap (system.nodes [0]->network.endpoint ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (std::numeric_limits<rai::uint256_t>::max (), system.nodes[0]->balance (rai::test_genesis_key.pub));
node1->bootstrap_initiator.bootstrap (system.nodes[0]->network.endpoint ());
auto iterations2 (0);
while (node1->balance (key2.pub) != system.nodes [0]->config.receive_minimum.number ())
while (node1->balance (key2.pub) != system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations2;
@ -801,7 +798,7 @@ TEST (network, ipv6)
boost::asio::ip::address_v6 address (boost::asio::ip::address_v6::from_string ("::ffff:127.0.0.1"));
ASSERT_TRUE (address.is_v4_mapped ());
rai::endpoint endpoint1 (address, 16384);
std::vector <uint8_t> bytes1;
std::vector<uint8_t> bytes1;
{
rai::vectorstream stream (bytes1);
rai::write (stream, address.to_bytes ());
@ -811,9 +808,9 @@ TEST (network, ipv6)
{
ASSERT_EQ (0, *i);
}
ASSERT_EQ (0xff, bytes1 [10]);
ASSERT_EQ (0xff, bytes1 [11]);
std::array <uint8_t, 16> bytes2;
ASSERT_EQ (0xff, bytes1[10]);
ASSERT_EQ (0xff, bytes1[11]);
std::array<uint8_t, 16> bytes2;
rai::bufferstream stream (bytes1.data (), bytes1.size ());
rai::read (stream, bytes2);
rai::endpoint endpoint2 (boost::asio::ip::address_v6 (bytes2), 16384);
@ -822,7 +819,7 @@ TEST (network, ipv6)
TEST (network, ipv6_from_ipv4)
{
rai::endpoint endpoint1 (boost::asio::ip::address_v4::loopback(), 16000);
rai::endpoint endpoint1 (boost::asio::ip::address_v4::loopback (), 16000);
ASSERT_TRUE (endpoint1.address ().is_v4 ());
rai::endpoint endpoint2 (boost::asio::ip::address_v6::v4_mapped (endpoint1.address ().to_v4 ()), 16000);
ASSERT_TRUE (endpoint2.address ().is_v6 ());
@ -833,12 +830,11 @@ TEST (network, ipv6_bind_send_ipv4)
boost::asio::io_service service;
rai::endpoint endpoint1 (boost::asio::ip::address_v6::any (), 24000);
rai::endpoint endpoint2 (boost::asio::ip::address_v4::any (), 24001);
std::array <uint8_t, 16> bytes1;
std::array<uint8_t, 16> bytes1;
auto finish1 (false);
rai::endpoint endpoint3;
boost::asio::ip::udp::socket socket1 (service, endpoint1);
socket1.async_receive_from (boost::asio::buffer (bytes1.data (), bytes1.size ()), endpoint3, [&finish1] (boost::system::error_code const & error, size_t size_a)
{
socket1.async_receive_from (boost::asio::buffer (bytes1.data (), bytes1.size ()), endpoint3, [&finish1](boost::system::error_code const & error, size_t size_a) {
ASSERT_FALSE (error);
ASSERT_EQ (16, size_a);
finish1 = true;
@ -846,8 +842,7 @@ TEST (network, ipv6_bind_send_ipv4)
boost::asio::ip::udp::socket socket2 (service, endpoint2);
rai::endpoint endpoint5 (boost::asio::ip::address_v4::loopback (), 24000);
rai::endpoint endpoint6 (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4::loopback ()), 24001);
socket2.async_send_to (boost::asio::buffer (std::array <uint8_t, 16> {}, 16), endpoint5, [] (boost::system::error_code const & error, size_t size_a)
{
socket2.async_send_to (boost::asio::buffer (std::array<uint8_t, 16>{}, 16), endpoint5, [](boost::system::error_code const & error, size_t size_a) {
ASSERT_FALSE (error);
ASSERT_EQ (16, size_a);
});
@ -855,20 +850,18 @@ TEST (network, ipv6_bind_send_ipv4)
while (!finish1)
{
service.poll ();
++iterations;
ASSERT_LT (iterations, 200);
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (endpoint6, endpoint3);
std::array <uint8_t, 16> bytes2;
std::array<uint8_t, 16> bytes2;
auto finish2 (false);
rai::endpoint endpoint4;
socket2.async_receive_from (boost::asio::buffer (bytes2.data (), bytes2.size ()), endpoint4, [&finish2] (boost::system::error_code const & error, size_t size_a)
{
socket2.async_receive_from (boost::asio::buffer (bytes2.data (), bytes2.size ()), endpoint4, [&finish2](boost::system::error_code const & error, size_t size_a) {
ASSERT_FALSE (!error);
ASSERT_EQ (16, size_a);
});
socket1.async_send_to (boost::asio::buffer (std::array <uint8_t, 16> {}, 16), endpoint6, [] (boost::system::error_code const & error, size_t size_a)
{
socket1.async_send_to (boost::asio::buffer (std::array<uint8_t, 16>{}, 16), endpoint6, [](boost::system::error_code const & error, size_t size_a) {
ASSERT_FALSE (error);
ASSERT_EQ (16, size_a);
});
@ -877,8 +870,8 @@ TEST (network, ipv6_bind_send_ipv4)
TEST (network, endpoint_bad_fd)
{
rai::system system (24000, 1);
system.nodes [0]->stop ();
auto endpoint (system.nodes [0]->network.endpoint ());
system.nodes[0]->stop ();
auto endpoint (system.nodes[0]->network.endpoint ());
ASSERT_TRUE (endpoint.address ().is_loopback ());
ASSERT_EQ (0, endpoint.port ());
}
@ -891,7 +884,7 @@ TEST (network, reserved_address)
TEST (node, port_mapping)
{
rai::system system (24000, 1);
auto node0 (system.nodes [0]);
auto node0 (system.nodes[0]);
node0->port_mapping.refresh_devices ();
node0->port_mapping.start ();
auto end (std::chrono::system_clock::now () + std::chrono::seconds (500));

View file

@ -7,8 +7,8 @@
TEST (node, stop)
{
rai::system system (24000, 1);
ASSERT_NE (system.nodes [0]->wallets.items.end (), system.nodes [0]->wallets.items.begin ());
system.nodes [0]->stop ();
ASSERT_NE (system.nodes[0]->wallets.items.end (), system.nodes[0]->wallets.items.begin ());
system.nodes[0]->stop ();
system.service.run ();
ASSERT_TRUE (true);
}
@ -16,13 +16,13 @@ TEST (node, stop)
TEST (node, block_store_path_failure)
{
rai::node_init init;
auto service (boost::make_shared <boost::asio::io_service> ());
auto service (boost::make_shared<boost::asio::io_service> ());
rai::alarm alarm (*service);
auto path (rai::unique_path ());
rai::logging logging;
logging.init (path);
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
auto node (std::make_shared <rai::node> (init, *service, 0, path, alarm, logging, work));
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
auto node (std::make_shared<rai::node> (init, *service, 0, path, alarm, logging, work));
ASSERT_TRUE (node->wallets.items.empty ());
node->stop ();
}
@ -30,14 +30,14 @@ TEST (node, block_store_path_failure)
TEST (node, inactive_supply)
{
rai::node_init init;
auto service (boost::make_shared <boost::asio::io_service> ());
auto service (boost::make_shared<boost::asio::io_service> ());
rai::alarm alarm (*service);
auto path (rai::unique_path ());
rai::node_config config;
config.logging.init (path);
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
config.inactive_supply = 10;
auto node (std::make_shared <rai::node> (init, *service, path, alarm, config, work));
auto node (std::make_shared<rai::node> (init, *service, path, alarm, config, work));
ASSERT_EQ (10, node->ledger.inactive_supply);
node->stop ();
}
@ -45,14 +45,14 @@ TEST (node, inactive_supply)
TEST (node, password_fanout)
{
rai::node_init init;
auto service (boost::make_shared <boost::asio::io_service> ());
auto service (boost::make_shared<boost::asio::io_service> ());
rai::alarm alarm (*service);
auto path (rai::unique_path ());
rai::node_config config;
config.logging.init (path);
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
config.password_fanout = 10;
auto node (std::make_shared <rai::node> (init, *service, path, alarm, config, work));
auto node (std::make_shared<rai::node> (init, *service, path, alarm, config, work));
auto wallet (node->wallets.create (100));
ASSERT_EQ (10, wallet->store.password.values.size ());
node->stop ();
@ -62,20 +62,20 @@ TEST (node, balance)
{
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max (), system.nodes [0]->ledger.account_balance (transaction, rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max (), system.nodes[0]->ledger.account_balance (transaction, rai::test_genesis_key.pub));
}
TEST (node, representative)
{
rai::system system (24000, 1);
auto block1 (system.nodes [0]->representative (rai::test_genesis_key.pub));
auto block1 (system.nodes[0]->representative (rai::test_genesis_key.pub));
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_TRUE (system.nodes [0]->ledger.store.block_exists (transaction, block1));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_TRUE (system.nodes[0]->ledger.store.block_exists (transaction, block1));
}
rai::keypair key;
ASSERT_TRUE (system.nodes [0]->representative (key.pub).is_zero ());
ASSERT_TRUE (system.nodes[0]->representative (key.pub).is_zero ());
}
TEST (node, send_unkeyed)
@ -84,7 +84,7 @@ TEST (node, send_unkeyed)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->store.password.value_set (rai::keypair ().prv);
ASSERT_EQ (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_EQ (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
}
TEST (node, send_self)
@ -93,15 +93,15 @@ TEST (node, send_self)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations (0);
while (system.nodes [0]->balance (key2.pub).is_zero ())
while (system.nodes[0]->balance (key2.pub).is_zero ())
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
}
TEST (node, send_single)
@ -110,11 +110,11 @@ TEST (node, send_single)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (1)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_TRUE (system.nodes [0]->balance (key2.pub).is_zero ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_TRUE (system.nodes[0]->balance (key2.pub).is_zero ());
auto iterations (0);
while (system.nodes [0]->balance (key2.pub).is_zero ())
while (system.nodes[0]->balance (key2.pub).is_zero ())
{
system.poll ();
++iterations;
@ -128,11 +128,11 @@ TEST (node, send_single_observing_peer)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (1)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_TRUE (system.nodes [0]->balance (key2.pub).is_zero ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_TRUE (system.nodes[0]->balance (key2.pub).is_zero ());
auto iterations (0);
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr <rai::node> const & node_a) { return node_a->balance (key2.pub).is_zero (); }))
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&](std::shared_ptr<rai::node> const & node_a) { return node_a->balance (key2.pub).is_zero (); }))
{
system.poll ();
++iterations;
@ -146,11 +146,11 @@ TEST (node, send_single_many_peers)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (1)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_TRUE (system.nodes [0]->balance (key2.pub).is_zero ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_TRUE (system.nodes[0]->balance (key2.pub).is_zero ());
auto iterations (0);
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr <rai::node> const & node_a) { return node_a->balance (key2.pub).is_zero(); }))
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&](std::shared_ptr<rai::node> const & node_a) { return node_a->balance (key2.pub).is_zero (); }))
{
system.poll ();
++iterations;
@ -163,12 +163,12 @@ TEST (node, send_out_of_order)
rai::system system (24000, 2);
rai::keypair key2;
rai::genesis genesis;
rai::send_block send1 (genesis.hash (), key2.pub, std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ()));
rai::send_block send2 (send1.hash (), key2.pub, std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number () * 2, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1.hash ()));
system.nodes [0]->process_active (std::unique_ptr <rai::block> (new rai::send_block (send2)));
system.nodes [0]->process_active (std::unique_ptr <rai::block> (new rai::send_block (send1)));
rai::send_block send1 (genesis.hash (), key2.pub, std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ()));
rai::send_block send2 (send1.hash (), key2.pub, std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number () * 2, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (send1.hash ()));
system.nodes[0]->process_active (std::unique_ptr<rai::block> (new rai::send_block (send2)));
system.nodes[0]->process_active (std::unique_ptr<rai::block> (new rai::send_block (send1)));
auto iterations (0);
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr <rai::node> const & node_a) {return node_a->balance (rai::test_genesis_key.pub) != rai::genesis_amount - system.nodes [0]->config.receive_minimum.number () * 2;}))
while (std::any_of (system.nodes.begin (), system.nodes.end (), [&](std::shared_ptr<rai::node> const & node_a) { return node_a->balance (rai::test_genesis_key.pub) != rai::genesis_amount - system.nodes[0]->config.receive_minimum.number () * 2; }))
{
system.poll ();
++iterations;
@ -180,12 +180,12 @@ TEST (node, quick_confirm)
{
rai::system system (24000, 1);
rai::keypair key;
rai::block_hash previous (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash previous (system.nodes[0]->latest (rai::test_genesis_key.pub));
system.wallet (0)->insert_adhoc (key.prv);
auto send (std::make_shared <rai::send_block> (previous, key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (previous)));
system.nodes [0]->process_active (send);
auto send (std::make_shared<rai::send_block> (previous, key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (previous)));
system.nodes[0]->process_active (send);
auto iterations (0);
while (system.nodes [0]->balance (key.pub).is_zero ())
while (system.nodes[0]->balance (key.pub).is_zero ())
{
system.poll ();
++iterations;
@ -199,25 +199,25 @@ TEST (node, auto_bootstrap)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key2.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations1 (0);
while (system.nodes [0]->balance (key2.pub) != system.nodes [0]->config.receive_minimum.number ())
while (system.nodes[0]->balance (key2.pub) != system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
}
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->network.send_keepalive (system.nodes [0]->network.endpoint ());
node1->network.send_keepalive (system.nodes[0]->network.endpoint ());
node1->start ();
while (!node1->bootstrap_initiator.in_progress ())
{
system.poll ();
}
auto iterations3 (0);
while (node1->balance (key2.pub) != system.nodes [0]->config.receive_minimum.number ())
while (node1->balance (key2.pub) != system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations3;
@ -240,13 +240,13 @@ TEST (node, auto_bootstrap_reverse)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key2.prv);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
system.nodes [0]->network.send_keepalive (node1->network.endpoint ());
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
system.nodes[0]->network.send_keepalive (node1->network.endpoint ());
node1->start ();
auto iterations (0);
while (node1->balance (key2.pub) != system.nodes [0]->config.receive_minimum.number ())
while (node1->balance (key2.pub) != system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations;
@ -258,9 +258,9 @@ TEST (node, auto_bootstrap_reverse)
TEST (node, receive_gap)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
auto & node1 (*system.nodes[0]);
ASSERT_EQ (0, node1.gap_cache.blocks.size ());
auto block (std::make_shared <rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
auto block (std::make_shared<rai::send_block> (0, 1, 2, rai::keypair ().prv, 4, 5));
rai::confirm_req message;
message.block = block;
node1.process_message (message, node1.network.endpoint ());
@ -271,11 +271,11 @@ TEST (node, receive_gap)
TEST (node, merge_peers)
{
rai::system system (24000, 1);
std::array <rai::endpoint, 8> endpoints;
std::array<rai::endpoint, 8> endpoints;
endpoints.fill (rai::endpoint (boost::asio::ip::address_v6::loopback (), 24000));
endpoints [0] = rai::endpoint (boost::asio::ip::address_v6::loopback (), 24001);
system.nodes [0]->network.merge_peers (endpoints);
ASSERT_EQ (0, system.nodes [0]->peers.peers.size ());
endpoints[0] = rai::endpoint (boost::asio::ip::address_v6::loopback (), 24001);
system.nodes[0]->network.merge_peers (endpoints);
ASSERT_EQ (0, system.nodes[0]->peers.peers.size ());
}
TEST (node, search_pending)
@ -283,12 +283,12 @@ TEST (node, search_pending)
rai::system system (24000, 1);
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
system.wallet (0)->insert_adhoc (key2.prv);
auto node (system.nodes [0]);
auto node (system.nodes[0]);
ASSERT_FALSE (system.wallet (0)->search_pending ());
auto iterations2 (0);
while (system.nodes [0]->balance (key2.pub).is_zero ())
while (system.nodes[0]->balance (key2.pub).is_zero ())
{
system.poll ();
++iterations2;
@ -301,13 +301,13 @@ TEST (node, search_pending_same)
rai::system system (24000, 1);
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
system.wallet (0)->insert_adhoc (key2.prv);
auto node (system.nodes [0]);
auto node (system.nodes[0]);
ASSERT_FALSE (system.wallet (0)->search_pending ());
auto iterations2 (0);
while (system.nodes [0]->balance (key2.pub) != 2 * system.nodes [0]->config.receive_minimum.number ())
while (system.nodes[0]->balance (key2.pub) != 2 * system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations2;
@ -322,21 +322,21 @@ TEST (node, search_pending_multiple)
rai::keypair key3;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key3.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key3.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key3.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations1 (0);
while (system.nodes [0]->balance (key3.pub).is_zero ())
while (system.nodes[0]->balance (key3.pub).is_zero ())
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
}
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (key3.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (key3.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
system.wallet (0)->insert_adhoc (key2.prv);
auto node (system.nodes [0]);
auto node (system.nodes[0]);
ASSERT_FALSE (system.wallet (0)->search_pending ());
auto iterations2 (0);
while (system.nodes [0]->balance (key2.pub) != 2 * system.nodes [0]->config.receive_minimum.number ())
while (system.nodes[0]->balance (key2.pub) != 2 * system.nodes[0]->config.receive_minimum.number ())
{
system.poll ();
++iterations2;
@ -348,15 +348,15 @@ TEST (node, unlock_search)
{
rai::system system (24000, 1);
rai::keypair key2;
rai::uint128_t balance (system.nodes [0]->balance (rai::test_genesis_key.pub));
rai::uint128_t balance (system.nodes[0]->balance (rai::test_genesis_key.pub));
{
rai::transaction transaction (system.wallet (0)->store.environment, nullptr, true);
system.wallet (0)->store.rekey (transaction, "");
}
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations1 (0);
while (system.nodes [0]->balance (rai::test_genesis_key.pub) == balance)
while (system.nodes[0]->balance (rai::test_genesis_key.pub) == balance)
{
system.poll ();
++iterations1;
@ -364,10 +364,10 @@ TEST (node, unlock_search)
}
system.wallet (0)->insert_adhoc (key2.prv);
system.wallet (0)->store.password.value_set (rai::keypair ().prv);
auto node (system.nodes [0]);
auto node (system.nodes[0]);
ASSERT_FALSE (system.wallet (0)->enter_password (""));
auto iterations2 (0);
while (system.nodes [0]->balance (key2.pub).is_zero ())
while (system.nodes[0]->balance (key2.pub).is_zero ())
{
system.poll ();
++iterations2;
@ -379,18 +379,18 @@ TEST (node, connect_after_junk)
{
rai::system system (24000, 1);
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
uint64_t junk (0);
node1->network.socket.async_send_to (boost::asio::buffer (&junk, sizeof (junk)), system.nodes [0]->network.endpoint (), [] (boost::system::error_code const &, size_t) {});
node1->network.socket.async_send_to (boost::asio::buffer (&junk, sizeof (junk)), system.nodes[0]->network.endpoint (), [](boost::system::error_code const &, size_t) {});
auto iterations1 (0);
while (system.nodes [0]->network.error_count == 0)
while (system.nodes[0]->network.error_count == 0)
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
}
node1->start ();
node1->network.send_keepalive (system.nodes [0]->network.endpoint ());
node1->network.send_keepalive (system.nodes[0]->network.endpoint ());
auto iterations2 (0);
while (node1->peers.empty ())
{
@ -463,20 +463,20 @@ TEST (logging, upgrade_v1_v2)
tree.erase ("vote");
bool upgraded (false);
ASSERT_FALSE (logging2.deserialize_json (upgraded, tree));
ASSERT_EQ ("2", tree.get <std::string> ("version"));
ASSERT_EQ (false, tree.get <bool> ("vote"));
ASSERT_EQ ("2", tree.get<std::string> ("version"));
ASSERT_EQ (false, tree.get<bool> ("vote"));
}
TEST (node, price)
{
rai::system system (24000, 1);
auto price1 (system.nodes [0]->price (rai::Gxrb_ratio, 1));
auto price1 (system.nodes[0]->price (rai::Gxrb_ratio, 1));
ASSERT_EQ (rai::node::price_max * 100.0, price1);
auto price2 (system.nodes [0]->price (rai::Gxrb_ratio * int (rai::node::free_cutoff + 1), 1));
auto price2 (system.nodes[0]->price (rai::Gxrb_ratio * int(rai::node::free_cutoff + 1), 1));
ASSERT_EQ (0, price2);
auto price3 (system.nodes [0]->price (rai::Gxrb_ratio * int (rai::node::free_cutoff + 2) / 2, 1));
auto price3 (system.nodes[0]->price (rai::Gxrb_ratio * int(rai::node::free_cutoff + 2) / 2, 1));
ASSERT_EQ (rai::node::price_max * 100.0 / 2, price3);
auto price4 (system.nodes [0]->price (rai::Gxrb_ratio * int (rai::node::free_cutoff) * 2, 1));
auto price4 (system.nodes[0]->price (rai::Gxrb_ratio * int(rai::node::free_cutoff) * 2, 1));
ASSERT_EQ (0, price4);
}
@ -509,7 +509,7 @@ TEST (node_config, serialization)
ASSERT_NE (config2.callback_address, config1.callback_address);
ASSERT_NE (config2.callback_port, config1.callback_port);
ASSERT_NE (config2.callback_target, config1.callback_target);
bool upgraded (false);
config2.deserialize_json (upgraded, tree);
ASSERT_FALSE (upgraded);
@ -579,15 +579,15 @@ TEST (node_config, unversioned_v2_upgrade)
bool upgraded (false);
rai::node_config config1;
config1.logging.init (path);
ASSERT_FALSE (tree.get_optional <std::string> ("version"));
ASSERT_FALSE (tree.get_optional<std::string> ("version"));
config1.deserialize_json (upgraded, tree);
ASSERT_TRUE (upgraded);
ASSERT_EQ (1, config1.preconfigured_representatives.size ());
ASSERT_EQ ("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo", config1.preconfigured_representatives [0].to_account ());
ASSERT_EQ ("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo", config1.preconfigured_representatives[0].to_account ());
auto reps (tree.get_child ("preconfigured_representatives"));
ASSERT_EQ (1, reps.size ());
ASSERT_EQ ("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo", reps.begin ()->second.get <std::string> (""));
auto version (tree.get <std::string> ("version"));
ASSERT_EQ ("xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo", reps.begin ()->second.get<std::string> (""));
auto version (tree.get<std::string> ("version"));
ASSERT_GT (std::stoull (version), 1);
}
@ -619,17 +619,17 @@ TEST (node_config, v2_v3_upgrade)
bool upgraded (false);
rai::node_config config1;
config1.logging.init (path);
ASSERT_FALSE (tree.get_optional <std::string> ("inactive_supply"));
ASSERT_FALSE (tree.get_optional <std::string> ("password_fanout"));
ASSERT_FALSE (tree.get_optional <std::string> ("io_threads"));
ASSERT_FALSE (tree.get_optional <std::string> ("work_threads"));
ASSERT_FALSE (tree.get_optional<std::string> ("inactive_supply"));
ASSERT_FALSE (tree.get_optional<std::string> ("password_fanout"));
ASSERT_FALSE (tree.get_optional<std::string> ("io_threads"));
ASSERT_FALSE (tree.get_optional<std::string> ("work_threads"));
config1.deserialize_json (upgraded, tree);
ASSERT_EQ (rai::uint128_union (0).to_string_dec (), tree.get <std::string> ("inactive_supply"));
ASSERT_EQ ("1024", tree.get <std::string> ("password_fanout"));
ASSERT_NE (0, std::stoul (tree.get <std::string> ("password_fanout")));
ASSERT_NE (0, std::stoul (tree.get <std::string> ("password_fanout")));
ASSERT_EQ (rai::uint128_union (0).to_string_dec (), tree.get<std::string> ("inactive_supply"));
ASSERT_EQ ("1024", tree.get<std::string> ("password_fanout"));
ASSERT_NE (0, std::stoul (tree.get<std::string> ("password_fanout")));
ASSERT_NE (0, std::stoul (tree.get<std::string> ("password_fanout")));
ASSERT_TRUE (upgraded);
auto version (tree.get <std::string> ("version"));
auto version (tree.get<std::string> ("version"));
ASSERT_GT (std::stoull (version), 2);
}
@ -638,9 +638,9 @@ TEST (node, confirm_locked)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->enter_password ("1");
auto block (std::make_shared <rai::send_block> (0, 0, 0, rai::keypair ().prv, 0, 0));
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
system.nodes [0]->network.republish_block (transaction, block);
auto block (std::make_shared<rai::send_block> (0, 0, 0, rai::keypair ().prv, 0, 0));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.nodes[0]->network.republish_block (transaction, block);
}
TEST (node_config, random_rep)
@ -663,34 +663,34 @@ TEST (node, block_replace)
auto initial_work (block1->block_work ());
while (rai::work_value (block1->root (), block1->block_work ()) <= rai::work_value (block1->root (), initial_work))
{
system.nodes [1]->generate_work (*block1);
system.nodes[1]->generate_work (*block1);
}
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (block3->hash (), system.nodes [0]->store.block_successor (transaction, block1->hash ()));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_EQ (block3->hash (), system.nodes[0]->store.block_successor (transaction, block1->hash ()));
}
for (auto i (0); i < 1; ++i)
{
rai::transaction transaction_a (system.nodes [1]->store.environment, nullptr, false);
system.nodes [1]->network.republish_block (transaction_a, block1);
rai::transaction transaction_a (system.nodes[1]->store.environment, nullptr, false);
system.nodes[1]->network.republish_block (transaction_a, block1);
}
auto iterations1 (0);
std::unique_ptr <rai::block> block2;
std::unique_ptr<rai::block> block2;
while (block2 == nullptr)
{
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
auto block (system.nodes [0]->store.block_get (transaction, block1->hash ()));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto block (system.nodes[0]->store.block_get (transaction, block1->hash ()));
if (block->block_work () != initial_work)
{
block2 = std::move (block);
}
}
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
ASSERT_EQ (block3->hash (), system.nodes [0]->store.block_successor(transaction, block1->hash ()));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_EQ (block3->hash (), system.nodes[0]->store.block_successor (transaction, block1->hash ()));
}
ASSERT_NE (initial_work, block1->block_work ());
ASSERT_EQ (block1->block_work (), block2->block_work ());
@ -699,17 +699,17 @@ TEST (node, block_replace)
TEST (node, fork_publish)
{
std::weak_ptr <rai::node> node0;
std::weak_ptr<rai::node> node0;
{
rai::system system (24000, 1);
node0 = system.nodes [0];
auto & node1 (*system.nodes [0]);
node0 = system.nodes[0];
auto & node1 (*system.nodes[0]);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key1;
rai::genesis genesis;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
node1.process_active (send1);
node1.block_processor.flush ();
ASSERT_EQ (1, node1.active.roots.size ());
@ -733,16 +733,16 @@ TEST (node, fork_publish)
TEST (node, fork_keep)
{
rai::system system (24000, 2);
auto & node1 (*system.nodes [0]);
auto & node2 (*system.nodes [1]);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
ASSERT_EQ (1, node1.peers.size ());
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key1;
rai::keypair key2;
rai::genesis genesis;
// send1 and send2 fork to different accounts
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
auto send2 (std::make_shared <rai::send_block> (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
auto send2 (std::make_shared<rai::send_block> (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
node1.process_active (send1);
node1.block_processor.flush ();
node2.process_active (send1);
@ -759,10 +759,10 @@ TEST (node, fork_keep)
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
{
rai::transaction transaction0 (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction1 (system.nodes [1]->store.environment, nullptr, false);
ASSERT_TRUE (system.nodes [0]->store.block_exists (transaction0, send1->hash ()));
ASSERT_TRUE (system.nodes [1]->store.block_exists (transaction1, send1->hash ()));
rai::transaction transaction0 (system.nodes[0]->store.environment, nullptr, false);
rai::transaction transaction1 (system.nodes[1]->store.environment, nullptr, false);
ASSERT_TRUE (system.nodes[0]->store.block_exists (transaction0, send1->hash ()));
ASSERT_TRUE (system.nodes[1]->store.block_exists (transaction1, send1->hash ()));
}
auto iterations (0);
// Wait until the genesis rep makes a vote
@ -772,30 +772,30 @@ TEST (node, fork_keep)
++iterations;
ASSERT_LT (iterations, 2000);
}
rai::transaction transaction0 (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction1 (system.nodes [1]->store.environment, nullptr, false);
rai::transaction transaction0 (system.nodes[0]->store.environment, nullptr, false);
rai::transaction transaction1 (system.nodes[1]->store.environment, nullptr, false);
// The vote should be in agreement with what we already have.
auto winner (node1.ledger.winner (transaction0, votes1->votes));
ASSERT_EQ (*send1, *winner.second);
ASSERT_EQ (rai::genesis_amount - 100, winner.first);
ASSERT_TRUE (system.nodes [0]->store.block_exists (transaction0, send1->hash ()));
ASSERT_TRUE (system.nodes [1]->store.block_exists (transaction1, send1->hash ()));
ASSERT_TRUE (system.nodes[0]->store.block_exists (transaction0, send1->hash ()));
ASSERT_TRUE (system.nodes[1]->store.block_exists (transaction1, send1->hash ()));
}
TEST (node, fork_flip)
{
rai::system system (24000, 2);
auto & node1 (*system.nodes [0]);
auto & node2 (*system.nodes [1]);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
ASSERT_EQ (1, node1.peers.size ());
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key1;
rai::genesis genesis;
std::unique_ptr <rai::send_block> send1 (new rai::send_block (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
std::unique_ptr<rai::send_block> send1 (new rai::send_block (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
rai::publish publish1;
publish1.block = std::move (send1);
rai::keypair key2;
std::unique_ptr <rai::send_block> send2 (new rai::send_block (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
std::unique_ptr<rai::send_block> send2 (new rai::send_block (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
rai::publish publish2;
publish2.block = std::move (send2);
node1.process_message (publish1, node1.network.endpoint ());
@ -814,11 +814,11 @@ TEST (node, fork_flip)
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_TRUE (node1.store.block_exists (transaction, publish1.block->hash ()));
}
{
rai::transaction transaction (system.nodes [1]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[1]->store.environment, nullptr, false);
ASSERT_TRUE (node2.store.block_exists (transaction, publish2.block->hash ()));
}
auto iterations (0);
@ -828,7 +828,7 @@ TEST (node, fork_flip)
++iterations;
ASSERT_LT (iterations, 200);
}
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node2.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (rai::genesis_amount - 100, winner.first);
@ -840,20 +840,20 @@ TEST (node, fork_flip)
TEST (node, fork_multi_flip)
{
rai::system system (24000, 2);
auto & node1 (*system.nodes [0]);
auto & node2 (*system.nodes [1]);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
ASSERT_EQ (1, node1.peers.size ());
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key1;
rai::genesis genesis;
std::unique_ptr <rai::send_block> send1 (new rai::send_block (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
std::unique_ptr<rai::send_block> send1 (new rai::send_block (genesis.hash (), key1.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
rai::publish publish1;
publish1.block = std::move (send1);
rai::keypair key2;
std::unique_ptr <rai::send_block> send2 (new rai::send_block (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
std::unique_ptr<rai::send_block> send2 (new rai::send_block (genesis.hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
rai::publish publish2;
publish2.block = std::move (send2);
std::unique_ptr <rai::send_block> send3 (new rai::send_block (publish2.block->hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (publish2.block->hash ())));
std::unique_ptr<rai::send_block> send3 (new rai::send_block (publish2.block->hash (), key2.pub, rai::genesis_amount - 100, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (publish2.block->hash ())));
rai::publish publish3;
publish3.block = std::move (send3);
node1.process_message (publish1, node1.network.endpoint ());
@ -874,11 +874,11 @@ TEST (node, fork_multi_flip)
ASSERT_NE (nullptr, votes1);
ASSERT_EQ (1, votes1->votes.rep_votes.size ());
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_TRUE (node1.store.block_exists (transaction, publish1.block->hash ()));
}
{
rai::transaction transaction (system.nodes [1]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[1]->store.environment, nullptr, false);
ASSERT_TRUE (node2.store.block_exists (transaction, publish2.block->hash ()));
ASSERT_TRUE (node2.store.block_exists (transaction, publish3.block->hash ()));
}
@ -889,7 +889,7 @@ TEST (node, fork_multi_flip)
++iterations;
ASSERT_LT (iterations, 200);
}
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node1.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*publish1.block, *winner.second);
ASSERT_EQ (rai::genesis_amount - 100, winner.first);
@ -905,14 +905,14 @@ TEST (node, fork_bootstrap_flip)
{
rai::system system0 (24000, 1);
rai::system system1 (24001, 1);
auto & node1 (*system0.nodes [0]);
auto & node2 (*system1.nodes [0]);
auto & node1 (*system0.nodes[0]);
auto & node2 (*system1.nodes[0]);
system0.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::block_hash latest (system0.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest (system0.nodes[0]->latest (rai::test_genesis_key.pub));
rai::keypair key1;
auto send1 (std::make_shared <rai::send_block> (latest, key1.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system0.work.generate (latest)));
auto send1 (std::make_shared<rai::send_block> (latest, key1.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system0.work.generate (latest)));
rai::keypair key2;
auto send2 (std::make_shared <rai::send_block> (latest, key2.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system0.work.generate (latest)));
auto send2 (std::make_shared<rai::send_block> (latest, key2.pub, rai::genesis_amount - rai::Gxrb_ratio, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system0.work.generate (latest)));
// Insert but don't rebroadcast, simulating settled blocks
node1.block_processor.process_receive_many (rai::block_processor_item (send1));
node1.block_processor.flush ();
@ -948,21 +948,21 @@ TEST (node, fork_bootstrap_flip)
TEST (node, fork_open)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
system.wallet (0)->insert_adhoc ( rai::test_genesis_key.prv);
auto & node1 (*system.nodes[0]);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key1;
rai::genesis genesis;
std::unique_ptr <rai::send_block> send1 (new rai::send_block (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
std::unique_ptr<rai::send_block> send1 (new rai::send_block (genesis.hash (), key1.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
rai::publish publish1;
publish1.block = std::move (send1);
node1.process_message (publish1, node1.network.endpoint ());
node1.block_processor.flush ();
std::unique_ptr <rai::open_block> open1 (new rai::open_block (publish1.block->hash (), 1, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
std::unique_ptr<rai::open_block> open1 (new rai::open_block (publish1.block->hash (), 1, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
rai::publish publish2;
publish2.block = std::move (open1);
node1.process_message (publish2, node1.network.endpoint ());
node1.block_processor.flush ();
std::unique_ptr <rai::open_block> open2 (new rai::open_block (publish1.block->hash (), 2, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
std::unique_ptr<rai::open_block> open2 (new rai::open_block (publish1.block->hash (), 2, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
rai::publish publish3;
publish3.block = std::move (open2);
ASSERT_EQ (2, node1.active.roots.size ());
@ -973,21 +973,21 @@ TEST (node, fork_open)
TEST (node, fork_open_flip)
{
rai::system system (24000, 2);
auto & node1 (*system.nodes [0]);
auto & node2 (*system.nodes [1]);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
ASSERT_EQ (1, node1.peers.size ());
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key1;
rai::genesis genesis;
rai::keypair rep1;
rai::keypair rep2;
auto send1 (std::make_shared <rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 1, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
auto send1 (std::make_shared<rai::send_block> (genesis.hash (), key1.pub, rai::genesis_amount - 1, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (genesis.hash ())));
node1.process_active (send1);
node2.process_active (send1);
// We should be keeping this block
auto open1 (std::make_shared <rai::open_block> (send1->hash (), rep1.pub, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
auto open1 (std::make_shared<rai::open_block> (send1->hash (), rep1.pub, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
// This block should be evicted
auto open2 (std::make_shared <rai::open_block> (send1->hash (), rep2.pub, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
auto open2 (std::make_shared<rai::open_block> (send1->hash (), rep2.pub, key1.pub, key1.prv, key1.pub, system.work.generate (key1.pub)));
ASSERT_FALSE (*open1 == *open2);
// node1 gets copy that will remain
node1.process_active (open1);
@ -1017,7 +1017,7 @@ TEST (node, fork_open_flip)
++iterations;
ASSERT_LT (iterations, 200);
}
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
auto winner (node2.ledger.winner (transaction, votes1->votes));
ASSERT_EQ (*open1, *winner.second);
ASSERT_EQ (rai::genesis_amount - 1, winner.first);
@ -1029,9 +1029,8 @@ TEST (node, fork_open_flip)
TEST (node, coherent_observer)
{
rai::system system (24000, 1);
auto & node1 (*system.nodes [0]);
node1.observers.blocks.add ([&node1] (std::shared_ptr <rai::block> block_a, rai::account const & account_a, rai::amount const &)
{
auto & node1 (*system.nodes[0]);
node1.observers.blocks.add ([&node1](std::shared_ptr<rai::block> block_a, rai::account const & account_a, rai::amount const &) {
rai::transaction transaction (node1.store.environment, nullptr, false);
ASSERT_TRUE (node1.store.block_exists (transaction, block_a->hash ()));
});
@ -1043,9 +1042,9 @@ TEST (node, coherent_observer)
TEST (node, fork_no_vote_quorum)
{
rai::system system (24000, 3);
auto & node1 (*system.nodes [0]);
auto & node2 (*system.nodes [1]);
auto & node3 (*system.nodes [2]);
auto & node1 (*system.nodes[0]);
auto & node2 (*system.nodes[1]);
auto & node3 (*system.nodes[2]);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto key4 (system.wallet (0)->deterministic_insert ());
system.wallet (0)->send_action (rai::test_genesis_key.pub, key4, rai::genesis_amount / 4);
@ -1068,12 +1067,12 @@ TEST (node, fork_no_vote_quorum)
ASSERT_EQ (rai::process_result::progress, node2.process (send1).code);
ASSERT_EQ (rai::process_result::progress, node3.process (send1).code);
auto key2 (system.wallet (2)->deterministic_insert ());
auto send2 (std::make_shared <rai::send_block> (block->hash (), key2, (rai::genesis_amount / 4) - (node1.config.receive_minimum.number () * 2), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (block->hash ())));
auto send2 (std::make_shared<rai::send_block> (block->hash (), key2, (rai::genesis_amount / 4) - (node1.config.receive_minimum.number () * 2), rai::test_genesis_key.prv, rai::test_genesis_key.pub, system.work.generate (block->hash ())));
rai::raw_key key3;
ASSERT_FALSE (system.wallet (1)->store.fetch (rai::transaction (system.wallet (1)->store.environment, nullptr, false), key1, key3));
auto vote (std::make_shared <rai::vote> (key1, key3, 0, send2));
auto vote (std::make_shared<rai::vote> (key1, key3, 0, send2));
rai::confirm_ack confirm (vote);
std::shared_ptr <std::vector <uint8_t>> bytes (new std::vector <uint8_t>);
std::shared_ptr<std::vector<uint8_t>> bytes (new std::vector<uint8_t>);
{
rai::vectorstream stream (*bytes);
confirm.serialize (stream);
@ -1091,9 +1090,9 @@ TEST (node, fork_no_vote_quorum)
TEST (node, broadcast_elected)
{
rai::system system (24000, 3);
auto node0 (system.nodes [0]);
auto node1 (system.nodes [1]);
auto node2 (system.nodes [2]);
auto node0 (system.nodes[0]);
auto node1 (system.nodes[1]);
auto node2 (system.nodes[2]);
rai::keypair rep_big;
rai::keypair rep_small;
rai::keypair rep_other;
@ -1136,11 +1135,11 @@ TEST (node, broadcast_elected)
system.wallet (0)->insert_adhoc (rep_big.prv);
system.wallet (1)->insert_adhoc (rep_small.prv);
system.wallet (2)->insert_adhoc (rep_other.prv);
auto fork0 (std::make_shared <rai::send_block> (node2->latest (rai::test_genesis_key.pub), rep_small.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto fork0 (std::make_shared<rai::send_block> (node2->latest (rai::test_genesis_key.pub), rep_small.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
node0->generate_work (*fork0);
node0->process_active (fork0);
node1->process_active (fork0);
auto fork1 (std::make_shared <rai::send_block> (node2->latest (rai::test_genesis_key.pub), rep_big.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto fork1 (std::make_shared<rai::send_block> (node2->latest (rai::test_genesis_key.pub), rep_big.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
node0->generate_work (*fork1);
system.wallet (2)->insert_adhoc (rep_small.prv);
node2->process_active (fork1);
@ -1160,7 +1159,7 @@ TEST (node, broadcast_elected)
TEST (node, rep_self_vote)
{
rai::system system (24000, 1);
auto node0 (system.nodes [0]);
auto node0 (system.nodes[0]);
rai::keypair rep_big;
{
rai::transaction transaction0 (node0->store.environment, nullptr, true);
@ -1173,13 +1172,13 @@ TEST (node, rep_self_vote)
}
system.wallet (0)->insert_adhoc (rep_big.prv);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto block0 (std::make_shared <rai::send_block> (node0->latest (rai::test_genesis_key.pub), rep_big.pub, rai::uint128_t ("0x60000000000000000000000000000000"), rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto block0 (std::make_shared<rai::send_block> (node0->latest (rai::test_genesis_key.pub), rep_big.pub, rai::uint128_t ("0x60000000000000000000000000000000"), rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
node0->generate_work (*block0);
ASSERT_EQ (rai::process_result::progress, node0->process (*block0).code);
auto & active (node0->active);
{
rai::transaction transaction (node0->store.environment, nullptr, true);
active.start (transaction, block0, [] (std::shared_ptr <rai::block>) {});
active.start (transaction, block0, [](std::shared_ptr<rai::block>) {});
}
auto existing (active.roots.find (block0->root ()));
ASSERT_NE (active.roots.end (), existing);
@ -1194,14 +1193,14 @@ TEST (node, bootstrap_no_publish)
{
rai::system system0 (24000, 1);
rai::system system1 (24001, 1);
auto node0 (system0.nodes [0]);
auto node1 (system1.nodes [0]);
auto node0 (system0.nodes[0]);
auto node1 (system1.nodes[0]);
rai::keypair key0;
// node0 knows about send0 but node1 doesn't.
rai::send_block send0 (system0.nodes [0]->latest (rai::test_genesis_key.pub), key0.pub, 500, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::send_block send0 (system0.nodes[0]->latest (rai::test_genesis_key.pub), key0.pub, 500, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
{
rai::transaction transaction (node0->store.environment, nullptr, true);
ASSERT_EQ (rai::process_result::progress, system0.nodes [0]->ledger.process (transaction, send0).code);
ASSERT_EQ (rai::process_result::progress, system0.nodes[0]->ledger.process (transaction, send0).code);
}
ASSERT_FALSE (node1->bootstrap_initiator.in_progress ());
node1->bootstrap_initiator.bootstrap (node0->network.endpoint ());
@ -1223,11 +1222,11 @@ TEST (node, bootstrap_no_publish)
TEST (node, bootstrap_fork_open)
{
rai::system system0 (24000, 2);
system0.wallet(0)->insert_adhoc (rai::test_genesis_key.prv);
auto node0 (system0.nodes [0]);
auto node1 (system0.nodes [1]);
system0.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto node0 (system0.nodes[0]);
auto node1 (system0.nodes[1]);
rai::keypair key0;
rai::send_block send0 (system0.nodes [0]->latest (rai::test_genesis_key.pub), key0.pub, rai::genesis_amount - 500, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::send_block send0 (system0.nodes[0]->latest (rai::test_genesis_key.pub), key0.pub, rai::genesis_amount - 500, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::open_block open0 (send0.hash (), 1, key0.pub, key0.prv, key0.pub, 0);
rai::open_block open1 (send0.hash (), 2, key0.pub, key0.prv, key0.pub, 0);
node0->generate_work (send0);
@ -1260,8 +1259,8 @@ TEST (node, bootstrap_fork_open)
TEST (node, unconfirmed_send)
{
rai::system system (24000, 2);
auto & node0 (*system.nodes [0]);
auto & node1 (*system.nodes [1]);
auto & node0 (*system.nodes[0]);
auto & node1 (*system.nodes[1]);
auto wallet0 (system.wallet (0));
auto wallet1 (system.wallet (1));
rai::keypair key0;
@ -1295,8 +1294,8 @@ TEST (node, unconfirmed_send)
TEST (node, rep_list)
{
rai::system system (24000, 2);
auto & node0 (*system.nodes [0]);
auto & node1 (*system.nodes [1]);
auto & node0 (*system.nodes[0]);
auto & node1 (*system.nodes[1]);
auto wallet0 (system.wallet (0));
auto wallet1 (system.wallet (1));
// Node0 has a rep
@ -1312,9 +1311,9 @@ TEST (node, rep_list)
auto reps (node1.peers.representatives (1));
if (!reps.empty ())
{
if (reps [0].endpoint == node0.network.endpoint ())
if (reps[0].endpoint == node0.network.endpoint ())
{
if (!reps [0].rep_weight.is_zero ())
if (!reps[0].rep_weight.is_zero ())
{
done = true;
}
@ -1330,8 +1329,8 @@ TEST (node, rep_list)
TEST (node, no_voting)
{
rai::system system (24000, 2);
auto & node0 (*system.nodes [0]);
auto & node1 (*system.nodes [1]);
auto & node0 (*system.nodes[0]);
auto & node1 (*system.nodes[1]);
auto wallet0 (system.wallet (0));
auto wallet1 (system.wallet (1));
node0.config.enable_voting = false;
@ -1354,16 +1353,15 @@ TEST (node, no_voting)
TEST (node, start_observer)
{
rai::node_init init;
auto service (boost::make_shared <boost::asio::io_service> ());
auto service (boost::make_shared<boost::asio::io_service> ());
rai::alarm alarm (*service);
auto path (rai::unique_path ());
rai::logging logging;
logging.init (path);
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
auto node (std::make_shared <rai::node> (init, *service, 0, path, alarm, logging, work));
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
auto node (std::make_shared<rai::node> (init, *service, 0, path, alarm, logging, work));
auto started (false);
node->observers.started.add([&started] ()
{
node->observers.started.add ([&started]() {
started = true;
});
node->start ();
@ -1377,18 +1375,18 @@ TEST (node, send_callback)
rai::keypair key2;
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key2.prv);
system.nodes [0]->config.callback_address = "localhost";
system.nodes [0]->config.callback_port = 8010;
system.nodes [0]->config.callback_target = "/";
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes [0]->config.receive_minimum.number ()));
system.nodes[0]->config.callback_address = "localhost";
system.nodes[0]->config.callback_port = 8010;
system.nodes[0]->config.callback_target = "/";
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, system.nodes[0]->config.receive_minimum.number ()));
auto iterations (0);
while (system.nodes [0]->balance (key2.pub).is_zero ())
while (system.nodes[0]->balance (key2.pub).is_zero ())
{
system.poll ();
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - system.nodes [0]->config.receive_minimum.number (), system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - system.nodes[0]->config.receive_minimum.number (), system.nodes[0]->balance (rai::test_genesis_key.pub));
}
// Check that votes get replayed back to nodes if they sent an old sequence number.
@ -1397,17 +1395,17 @@ TEST (node, vote_replay)
{
rai::system system (24000, 2);
rai::keypair key;
auto open (std::make_shared <rai::open_block> (0, 1, key.pub, key.prv, key.pub, 0));
system.nodes [0]->generate_work (*open);
auto open (std::make_shared<rai::open_block> (0, 1, key.pub, key.prv, key.pub, 0));
system.nodes[0]->generate_work (*open);
for (auto i (0); i < 11000; ++i)
{
rai::transaction transaction (system.nodes [1]->store.environment, nullptr, false);
auto vote (system.nodes [1]->store.vote_generate (transaction, rai::test_genesis_key.pub, rai::test_genesis_key.prv, open));
rai::transaction transaction (system.nodes[1]->store.environment, nullptr, false);
auto vote (system.nodes[1]->store.vote_generate (transaction, rai::test_genesis_key.pub, rai::test_genesis_key.prv, open));
}
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
std::lock_guard <std::mutex> lock (system.nodes [0]->store.cache_mutex);
auto vote (system.nodes [0]->store.vote_current (transaction, rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
std::lock_guard<std::mutex> lock (system.nodes[0]->store.cache_mutex);
auto vote (system.nodes[0]->store.vote_current (transaction, rai::test_genesis_key.pub));
ASSERT_EQ (nullptr, vote);
}
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
@ -1418,9 +1416,9 @@ TEST (node, vote_replay)
while (!done)
{
system.poll ();
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
std::lock_guard <std::mutex> lock (system.nodes [0]->store.cache_mutex);
auto vote (system.nodes [0]->store.vote_current (transaction, rai::test_genesis_key.pub));
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
std::lock_guard<std::mutex> lock (system.nodes[0]->store.cache_mutex);
auto vote (system.nodes[0]->store.vote_current (transaction, rai::test_genesis_key.pub));
done = vote && (vote->sequence >= 10000);
++iterations;
ASSERT_GT (400, iterations);

View file

@ -3,20 +3,20 @@
TEST (peer_container, empty_peers)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
auto list (peers.purge_list (std::chrono::system_clock::now ()));
ASSERT_EQ (0, list.size ());
}
TEST (peer_container, no_recontact)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
auto observed_peer (0);
auto observed_disconnect (false);
rai::endpoint endpoint1 (boost::asio::ip::address_v6::loopback (), 10000);
ASSERT_EQ (0, peers.size ());
peers.peer_observer = [&observed_peer] (rai::endpoint const &) {++observed_peer;};
peers.disconnect_observer = [&observed_disconnect] () {observed_disconnect = true;};
peers.peer_observer = [&observed_peer](rai::endpoint const &) { ++observed_peer; };
peers.disconnect_observer = [&observed_disconnect]() { observed_disconnect = true; };
ASSERT_FALSE (peers.insert (endpoint1, 0));
ASSERT_EQ (1, peers.size ());
ASSERT_TRUE (peers.insert (endpoint1, 0));
@ -44,7 +44,7 @@ TEST (peer_container, no_self_contacting)
TEST (peer_container, reserved_peers_no_contact)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
ASSERT_TRUE (peers.insert (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0x00000001)), 10000), 0));
ASSERT_TRUE (peers.insert (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xc0000201)), 10000), 0));
ASSERT_TRUE (peers.insert (rai::endpoint (boost::asio::ip::address_v6::v4_mapped (boost::asio::ip::address_v4 (0xc6336401)), 10000), 0));
@ -57,7 +57,7 @@ TEST (peer_container, reserved_peers_no_contact)
TEST (peer_container, split)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
auto now (std::chrono::system_clock::now ());
rai::endpoint endpoint1 (boost::asio::ip::address_v6::any (), 100);
rai::endpoint endpoint2 (boost::asio::ip::address_v6::any (), 101);
@ -67,35 +67,35 @@ TEST (peer_container, split)
auto list (peers.purge_list (now));
ASSERT_EQ (1, peers.peers.size ());
ASSERT_EQ (1, list.size ());
ASSERT_EQ (endpoint2, list [0].endpoint);
ASSERT_EQ (endpoint2, list[0].endpoint);
}
TEST (peer_container, fill_random_clear)
{
rai::peer_container peers (rai::endpoint {});
std::array <rai::endpoint, 8> target;
rai::peer_container peers (rai::endpoint{});
std::array<rai::endpoint, 8> target;
std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000));
peers.random_fill (target);
ASSERT_TRUE (std::all_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::any (), 0); }));
ASSERT_TRUE (std::all_of (target.begin (), target.end (), [](rai::endpoint const & endpoint_a) { return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::any (), 0); }));
}
TEST (peer_container, fill_random_full)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
for (auto i (0); i < 100; ++i)
{
peers.insert (rai::endpoint (boost::asio::ip::address_v6::loopback (), i), 0);
}
std::array <rai::endpoint, 8> target;
std::array<rai::endpoint, 8> target;
std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000));
peers.random_fill (target);
ASSERT_TRUE (std::none_of (target.begin (), target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); }));
ASSERT_TRUE (std::none_of (target.begin (), target.end (), [](rai::endpoint const & endpoint_a) { return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); }));
}
TEST (peer_container, fill_random_part)
{
rai::peer_container peers (rai::endpoint {});
std::array <rai::endpoint, 8> target;
rai::peer_container peers (rai::endpoint{});
std::array<rai::endpoint, 8> target;
auto half (target.size () / 2);
for (auto i (0); i < half; ++i)
{
@ -103,14 +103,14 @@ TEST (peer_container, fill_random_part)
}
std::fill (target.begin (), target.end (), rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000));
peers.random_fill (target);
ASSERT_TRUE (std::none_of (target.begin (), target.begin () + half, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); }));
ASSERT_TRUE (std::none_of (target.begin (), target.begin () + half, [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 0); }));
ASSERT_TRUE (std::all_of (target.begin () + half, target.end (), [] (rai::endpoint const & endpoint_a) {return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::any (), 0); }));
ASSERT_TRUE (std::none_of (target.begin (), target.begin () + half, [](rai::endpoint const & endpoint_a) { return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000); }));
ASSERT_TRUE (std::none_of (target.begin (), target.begin () + half, [](rai::endpoint const & endpoint_a) { return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::loopback (), 0); }));
ASSERT_TRUE (std::all_of (target.begin () + half, target.end (), [](rai::endpoint const & endpoint_a) { return endpoint_a == rai::endpoint (boost::asio::ip::address_v6::any (), 0); }));
}
TEST (peer_container, list_sqrt)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
auto list1 (peers.list_sqrt ());
ASSERT_TRUE (list1.empty ());
for (auto i (0); i < 1000; ++i)
@ -123,7 +123,7 @@ TEST (peer_container, list_sqrt)
TEST (peer_container, rep_weight)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
peers.insert (rai::endpoint (boost::asio::ip::address_v6::loopback (), 24001), 0);
ASSERT_TRUE (peers.representatives (1).empty ());
rai::endpoint endpoint0 (boost::asio::ip::address_v6::loopback (), 24000);
@ -136,14 +136,14 @@ TEST (peer_container, rep_weight)
peers.rep_response (endpoint0, amount);
auto reps (peers.representatives (1));
ASSERT_EQ (1, reps.size ());
ASSERT_EQ (100, reps [0].rep_weight.number ());
ASSERT_EQ (endpoint0, reps [0].endpoint);
ASSERT_EQ (100, reps[0].rep_weight.number ());
ASSERT_EQ (endpoint0, reps[0].endpoint);
}
// Test to make sure we don't repeatedly send keepalive messages to nodes that aren't responding
TEST (peer_container, reachout)
{
rai::peer_container peers (rai::endpoint {});
rai::peer_container peers (rai::endpoint{});
rai::endpoint endpoint0 (boost::asio::ip::address_v6::loopback (), 24000);
// Make sure having been contacted by them already indicates we shouldn't reach out
peers.contacted (endpoint0, 0);

View file

@ -20,7 +20,7 @@ TEST (processor_service, bad_send_signature)
rai::keypair key2;
rai::send_block send (info1.head, rai::test_genesis_key.pub, 50, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::block_hash hash1 (send.hash ());
send.signature.bytes [32] ^= 0x1;
send.signature.bytes[32] ^= 0x1;
ASSERT_EQ (rai::process_result::bad_signature, ledger.process (transaction, send).code);
}
@ -41,7 +41,7 @@ TEST (processor_service, bad_receive_signature)
rai::account_info info2;
ASSERT_FALSE (store.account_get (transaction, rai::test_genesis_key.pub, info2));
rai::receive_block receive (hash1, hash1, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
receive.signature.bytes [32] ^= 0x1;
receive.signature.bytes[32] ^= 0x1;
ASSERT_EQ (rai::process_result::bad_signature, ledger.process (transaction, receive).code);
}
@ -49,19 +49,18 @@ TEST (alarm, one)
{
boost::asio::io_service service;
rai::alarm alarm (service);
std::atomic <bool> done (false);
std::atomic<bool> done (false);
std::mutex mutex;
std::condition_variable condition;
alarm.add (std::chrono::system_clock::now (), [&] ()
{
std::lock_guard <std::mutex> lock (mutex);
alarm.add (std::chrono::system_clock::now (), [&]() {
std::lock_guard<std::mutex> lock (mutex);
done = true;
condition.notify_one ();
});
boost::asio::io_service::work work (service);
std::thread thread ([&service] () {service.run ();});
std::unique_lock <std::mutex> unique (mutex);
condition.wait (unique, [&] () {return !!done;});
std::thread thread ([&service]() { service.run (); });
std::unique_lock<std::mutex> unique (mutex);
condition.wait (unique, [&]() { return !!done; });
service.stop ();
thread.join ();
}
@ -70,26 +69,25 @@ TEST (alarm, many)
{
boost::asio::io_service service;
rai::alarm alarm (service);
std::atomic <int> count (0);
std::atomic<int> count (0);
std::mutex mutex;
std::condition_variable condition;
for (auto i (0); i < 50; ++i)
{
alarm.add (std::chrono::system_clock::now (), [&] ()
{
std::lock_guard <std::mutex> lock (mutex);
alarm.add (std::chrono::system_clock::now (), [&]() {
std::lock_guard<std::mutex> lock (mutex);
count += 1;
condition.notify_one ();
});
}
boost::asio::io_service::work work (service);
std::vector <std::thread> threads;
std::vector<std::thread> threads;
for (auto i (0); i < 50; ++i)
{
threads.push_back (std::thread ([&service] () {service.run ();}));
threads.push_back (std::thread ([&service]() { service.run (); }));
}
std::unique_lock <std::mutex> unique (mutex);
condition.wait (unique, [&] () { return count == 50;});
std::unique_lock<std::mutex> unique (mutex);
condition.wait (unique, [&]() { return count == 50; });
service.stop ();
for (auto i (threads.begin ()), j (threads.end ()); i != j; ++i)
{
@ -104,26 +102,23 @@ TEST (alarm, top_execution)
int value1 (0);
int value2 (0);
std::mutex mutex;
std::promise <bool> promise;
alarm.add (std::chrono::system_clock::now (), [&] ()
{
std::lock_guard <std::mutex> lock (mutex);
std::promise<bool> promise;
alarm.add (std::chrono::system_clock::now (), [&]() {
std::lock_guard<std::mutex> lock (mutex);
value1 = 1;
value2 = 1;
});
alarm.add (std::chrono::system_clock::now () + std::chrono::milliseconds (1), [&] ()
{
std::lock_guard <std::mutex> lock (mutex);
alarm.add (std::chrono::system_clock::now () + std::chrono::milliseconds (1), [&]() {
std::lock_guard<std::mutex> lock (mutex);
value2 = 2;
promise.set_value (false);
});
boost::asio::io_service::work work (service);
std::thread thread ([&service] ()
{
std::thread thread ([&service]() {
service.run ();
});
promise.get_future ().get ();
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
ASSERT_EQ (1, value1);
ASSERT_EQ (2, value2);
service.stop ();

File diff suppressed because it is too large Load diff

View file

@ -10,7 +10,7 @@ TEST (uint128_union, decode_dec)
rai::uint128_union value;
std::string text ("16");
ASSERT_FALSE (value.decode_dec (text));
ASSERT_EQ (16, value.bytes [15]);
ASSERT_EQ (16, value.bytes[15]);
}
TEST (uint128_union, decode_dec_negative)
@ -37,12 +37,28 @@ TEST (uint128_union, decode_dec_leading_zero)
ASSERT_TRUE (error);
}
struct test_punct : std::moneypunct<char> {
pattern do_pos_format () const { return { {value, none, none, none} }; }
int do_frac_digits () const { return 0; }
char_type do_decimal_point () const { return '+'; }
char_type do_thousands_sep () const { return '-'; }
string_type do_grouping () const { return "\3\4"; }
struct test_punct : std::moneypunct<char>
{
pattern do_pos_format () const
{
return { { value, none, none, none } };
}
int do_frac_digits () const
{
return 0;
}
char_type do_decimal_point () const
{
return '+';
}
char_type do_thousands_sep () const
{
return '-';
}
string_type do_grouping () const
{
return "\3\4";
}
};
TEST (uint128_union, balance_format)
@ -76,9 +92,9 @@ TEST (uint128_union, balance_format)
TEST (unions, identity)
{
ASSERT_EQ (1, rai::uint128_union (1).number ().convert_to <uint8_t> ());
ASSERT_EQ (1, rai::uint256_union (1).number ().convert_to <uint8_t> ());
ASSERT_EQ (1, rai::uint512_union (1).number ().convert_to <uint8_t> ());
ASSERT_EQ (1, rai::uint128_union (1).number ().convert_to<uint8_t> ());
ASSERT_EQ (1, rai::uint256_union (1).number ().convert_to<uint8_t> ());
ASSERT_EQ (1, rai::uint512_union (1).number ().convert_to<uint8_t> ());
}
TEST (uint256_union, key_encryption)
@ -87,9 +103,9 @@ TEST (uint256_union, key_encryption)
rai::raw_key secret_key;
secret_key.data.bytes.fill (0);
rai::uint256_union encrypted;
encrypted.encrypt (key1.prv, secret_key, key1.pub.owords [0]);
encrypted.encrypt (key1.prv, secret_key, key1.pub.owords[0]);
rai::raw_key key4;
key4.decrypt (encrypted, secret_key, key1.pub.owords [0]);
key4.decrypt (encrypted, secret_key, key1.pub.owords[0]);
ASSERT_EQ (key1.prv, key4);
rai::public_key pub;
ed25519_publickey (key4.data.bytes.data (), pub.bytes.data ());
@ -103,12 +119,12 @@ TEST (uint256_union, encryption)
rai::raw_key number1;
number1.data = 1;
rai::uint256_union encrypted1;
encrypted1.encrypt (number1, key, key.data.owords [0]);
encrypted1.encrypt (number1, key, key.data.owords[0]);
rai::uint256_union encrypted2;
encrypted2.encrypt (number1, key, key.data.owords [0]);
encrypted2.encrypt (number1, key, key.data.owords[0]);
ASSERT_EQ (encrypted1, encrypted2);
rai::raw_key number2;
number2.decrypt (encrypted1, key, key.data.owords [0]);
number2.decrypt (encrypted1, key, key.data.owords[0]);
ASSERT_EQ (number1, number2);
}
@ -157,7 +173,7 @@ TEST (uint256_union, parse_error_symbol)
rai::uint256_union input (rai::uint256_t (1000));
std::string text;
input.encode_hex (text);
text [5] = '!';
text[5] = '!';
rai::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
@ -165,7 +181,7 @@ TEST (uint256_union, parse_error_symbol)
TEST (uint256_union, max_hex)
{
rai::uint256_union input (std::numeric_limits <rai::uint256_t>::max ());
rai::uint256_union input (std::numeric_limits<rai::uint256_t>::max ());
std::string text;
input.encode_hex (text);
rai::uint256_union output;
@ -180,12 +196,12 @@ TEST (uint256_union, decode_dec)
rai::uint256_union value;
std::string text ("16");
ASSERT_FALSE (value.decode_dec (text));
ASSERT_EQ (16, value.bytes [31]);
ASSERT_EQ (16, value.bytes[31]);
}
TEST (uint256_union, max_dec)
{
rai::uint256_union input (std::numeric_limits <rai::uint256_t>::max ());
rai::uint256_union input (std::numeric_limits<rai::uint256_t>::max ());
std::string text;
input.encode_dec (text);
rai::uint256_union output;
@ -221,7 +237,7 @@ TEST (uint256_union, decode_dec_leading_zero)
TEST (uint256_union, parse_error_overflow)
{
rai::uint256_union input (std::numeric_limits <rai::uint256_t>::max ());
rai::uint256_union input (std::numeric_limits<rai::uint256_t>::max ());
std::string text;
input.encode_hex (text);
text.push_back (0);
@ -234,52 +250,52 @@ TEST (uint256_union, big_endian_union_constructor)
{
rai::uint256_t value1 (1);
rai::uint256_union bytes1 (value1);
ASSERT_EQ (1, bytes1.bytes [31]);
ASSERT_EQ (1, bytes1.bytes[31]);
rai::uint512_t value2 (1);
rai::uint512_union bytes2 (value2);
ASSERT_EQ (1, bytes2.bytes [63]);
ASSERT_EQ (1, bytes2.bytes[63]);
}
TEST (uint256_union, big_endian_union_function)
{
rai::uint256_union bytes1 ("FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210");
ASSERT_EQ (0xfe, bytes1.bytes [0x00]);
ASSERT_EQ (0xdc, bytes1.bytes [0x01]);
ASSERT_EQ (0xba, bytes1.bytes [0x02]);
ASSERT_EQ (0x98, bytes1.bytes [0x03]);
ASSERT_EQ (0x76, bytes1.bytes [0x04]);
ASSERT_EQ (0x54, bytes1.bytes [0x05]);
ASSERT_EQ (0x32, bytes1.bytes [0x06]);
ASSERT_EQ (0x10, bytes1.bytes [0x07]);
ASSERT_EQ (0xfe, bytes1.bytes [0x08]);
ASSERT_EQ (0xdc, bytes1.bytes [0x09]);
ASSERT_EQ (0xba, bytes1.bytes [0x0a]);
ASSERT_EQ (0x98, bytes1.bytes [0x0b]);
ASSERT_EQ (0x76, bytes1.bytes [0x0c]);
ASSERT_EQ (0x54, bytes1.bytes [0x0d]);
ASSERT_EQ (0x32, bytes1.bytes [0x0e]);
ASSERT_EQ (0x10, bytes1.bytes [0x0f]);
ASSERT_EQ (0xfe, bytes1.bytes [0x10]);
ASSERT_EQ (0xdc, bytes1.bytes [0x11]);
ASSERT_EQ (0xba, bytes1.bytes [0x12]);
ASSERT_EQ (0x98, bytes1.bytes [0x13]);
ASSERT_EQ (0x76, bytes1.bytes [0x14]);
ASSERT_EQ (0x54, bytes1.bytes [0x15]);
ASSERT_EQ (0x32, bytes1.bytes [0x16]);
ASSERT_EQ (0x10, bytes1.bytes [0x17]);
ASSERT_EQ (0xfe, bytes1.bytes [0x18]);
ASSERT_EQ (0xdc, bytes1.bytes [0x19]);
ASSERT_EQ (0xba, bytes1.bytes [0x1a]);
ASSERT_EQ (0x98, bytes1.bytes [0x1b]);
ASSERT_EQ (0x76, bytes1.bytes [0x1c]);
ASSERT_EQ (0x54, bytes1.bytes [0x1d]);
ASSERT_EQ (0x32, bytes1.bytes [0x1e]);
ASSERT_EQ (0x10, bytes1.bytes [0x1f]);
ASSERT_EQ (0xfe, bytes1.bytes[0x00]);
ASSERT_EQ (0xdc, bytes1.bytes[0x01]);
ASSERT_EQ (0xba, bytes1.bytes[0x02]);
ASSERT_EQ (0x98, bytes1.bytes[0x03]);
ASSERT_EQ (0x76, bytes1.bytes[0x04]);
ASSERT_EQ (0x54, bytes1.bytes[0x05]);
ASSERT_EQ (0x32, bytes1.bytes[0x06]);
ASSERT_EQ (0x10, bytes1.bytes[0x07]);
ASSERT_EQ (0xfe, bytes1.bytes[0x08]);
ASSERT_EQ (0xdc, bytes1.bytes[0x09]);
ASSERT_EQ (0xba, bytes1.bytes[0x0a]);
ASSERT_EQ (0x98, bytes1.bytes[0x0b]);
ASSERT_EQ (0x76, bytes1.bytes[0x0c]);
ASSERT_EQ (0x54, bytes1.bytes[0x0d]);
ASSERT_EQ (0x32, bytes1.bytes[0x0e]);
ASSERT_EQ (0x10, bytes1.bytes[0x0f]);
ASSERT_EQ (0xfe, bytes1.bytes[0x10]);
ASSERT_EQ (0xdc, bytes1.bytes[0x11]);
ASSERT_EQ (0xba, bytes1.bytes[0x12]);
ASSERT_EQ (0x98, bytes1.bytes[0x13]);
ASSERT_EQ (0x76, bytes1.bytes[0x14]);
ASSERT_EQ (0x54, bytes1.bytes[0x15]);
ASSERT_EQ (0x32, bytes1.bytes[0x16]);
ASSERT_EQ (0x10, bytes1.bytes[0x17]);
ASSERT_EQ (0xfe, bytes1.bytes[0x18]);
ASSERT_EQ (0xdc, bytes1.bytes[0x19]);
ASSERT_EQ (0xba, bytes1.bytes[0x1a]);
ASSERT_EQ (0x98, bytes1.bytes[0x1b]);
ASSERT_EQ (0x76, bytes1.bytes[0x1c]);
ASSERT_EQ (0x54, bytes1.bytes[0x1d]);
ASSERT_EQ (0x32, bytes1.bytes[0x1e]);
ASSERT_EQ (0x10, bytes1.bytes[0x1f]);
ASSERT_EQ ("FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210", bytes1.to_string ());
ASSERT_EQ (rai::uint256_t ("0xFEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210"), bytes1.number ());
rai::uint512_union bytes2;
bytes2.clear ();
bytes2.bytes [63] = 1;
bytes2.bytes[63] = 1;
ASSERT_EQ (rai::uint512_t (1), bytes2.number ());
}
@ -296,8 +312,8 @@ TEST (uint256_union, account_transcode)
auto text (rai::test_genesis_key.pub.to_account ());
ASSERT_FALSE (value.decode_account (text));
ASSERT_EQ (rai::test_genesis_key.pub, value);
ASSERT_EQ ('_', text [3]);
text [3] = '-';
ASSERT_EQ ('_', text[3]);
text[3] = '-';
rai::uint256_union value2;
ASSERT_FALSE (value2.decode_account (text));
ASSERT_EQ (value, value2);
@ -336,16 +352,16 @@ TEST (uint256_union, bounds)
{
rai::uint256_union key;
std::string bad1 (64, '\x000');
bad1 [0] = 'x';
bad1 [1] = 'r';
bad1 [2] = 'b';
bad1 [3] = '-';
bad1[0] = 'x';
bad1[1] = 'r';
bad1[2] = 'b';
bad1[3] = '-';
ASSERT_TRUE (key.decode_account (bad1));
std::string bad2 (64, '\x0ff');
bad2 [0] = 'x';
bad2 [1] = 'r';
bad2 [2] = 'b';
bad2 [3] = '-';
bad2[0] = 'x';
bad2[1] = 'r';
bad2[2] = 'b';
bad2[3] = '-';
ASSERT_TRUE (key.decode_account (bad2));
}
@ -357,7 +373,7 @@ public:
auto error (false);
if (!tree_a.empty ())
{
auto text_l (tree_a.get <std::string> ("thing"));
auto text_l (tree_a.get<std::string> ("thing"));
if (text_l == "junktest")
{
upgraded = true;
@ -397,7 +413,7 @@ TEST (json, fetch_object)
stream1.close ();
rai::open_or_create (stream1, path1.string ());
boost::property_tree::read_json (stream1, tree1);
ASSERT_EQ ("changed", tree1.get <std::string> ("thing"));
ASSERT_EQ ("changed", tree1.get<std::string> ("thing"));
std::string string2 ("{ \"thing\": \"junktest2\" }");
std::stringstream stream2 (string2);
json_upgrade_test object2;
@ -412,25 +428,25 @@ TEST (json, fetch_object)
ASSERT_TRUE (error3);
auto path2 (rai::unique_path ());
std::fstream stream4;
rai::open_or_create (stream4, path2.string());
rai::open_or_create (stream4, path2.string ());
json_upgrade_test object4;
auto error4 (rai::fetch_object (object4, path2, stream4));
ASSERT_FALSE (error4);
ASSERT_EQ ("created", object4.text);
boost::property_tree::ptree tree2;
stream4.close ();
rai::open_or_create (stream4, path2.string());
rai::open_or_create (stream4, path2.string ());
boost::property_tree::read_json (stream4, tree2);
ASSERT_EQ ("created", tree2.get <std::string> ("thing"));
ASSERT_EQ ("created", tree2.get<std::string> ("thing"));
}
TEST(json, DISABLED_fetch_write_fail)
TEST (json, DISABLED_fetch_write_fail)
{
std::string string4("");
std::stringstream stream4(string4, std::ios_base::in);
std::string string4 ("");
std::stringstream stream4 (string4, std::ios_base::in);
json_upgrade_test object4;
auto error4(rai::fetch_object(object4, stream4));
ASSERT_TRUE(error4);
auto error4 (rai::fetch_object (object4, stream4));
ASSERT_TRUE (error4);
}
TEST (uint64_t, parse)

View file

@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <rai/node/testing.hpp>
#include <fstream>
#include <rai/node/testing.hpp>
TEST (wallet, no_key)
{
@ -56,7 +56,7 @@ TEST (wallet, retrieval)
ASSERT_FALSE (wallet.fetch (transaction, key1.pub, prv1));
ASSERT_TRUE (wallet.valid_password (transaction));
ASSERT_EQ (key1.prv, prv1);
wallet.password.values [0]->bytes [16] ^= 1;
wallet.password.values[0]->bytes[16] ^= 1;
rai::raw_key prv2;
ASSERT_TRUE (wallet.fetch (transaction, key1.pub, prv2));
ASSERT_FALSE (wallet.valid_password (transaction));
@ -93,7 +93,7 @@ TEST (wallet, one_item_iteration)
rai::raw_key password;
wallet.wallet_key (password, transaction);
rai::raw_key key;
key.decrypt (rai::wallet_value (i->second).key, password, wallet.salt (transaction).owords [0]);
key.decrypt (rai::wallet_value (i->second).key, password, wallet.salt (transaction).owords[0]);
ASSERT_EQ (key1.prv, key);
}
}
@ -106,8 +106,8 @@ TEST (wallet, two_item_iteration)
rai::keypair key1;
rai::keypair key2;
ASSERT_NE (key1.pub, key2.pub);
std::unordered_set <rai::public_key> pubs;
std::unordered_set <rai::private_key> prvs;
std::unordered_set<rai::public_key> pubs;
std::unordered_set<rai::private_key> prvs;
rai::kdf kdf;
{
rai::transaction transaction (environment, nullptr, true);
@ -121,7 +121,7 @@ TEST (wallet, two_item_iteration)
rai::raw_key password;
wallet.wallet_key (password, transaction);
rai::raw_key key;
key.decrypt (rai::wallet_value (i->second).key, password, wallet.salt (transaction).owords [0]);
key.decrypt (rai::wallet_value (i->second).key, password, wallet.salt (transaction).owords[0]);
prvs.insert (key.data);
}
}
@ -146,21 +146,21 @@ TEST (wallet, insufficient_spend_one)
TEST (wallet, spend_all_one)
{
rai::system system (24000, 1);
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, std::numeric_limits <rai::uint128_t>::max ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, std::numeric_limits<rai::uint128_t>::max ()));
rai::account_info info2;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
system.nodes [0]->store.account_get (transaction, rai::test_genesis_key.pub, info2);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.nodes[0]->store.account_get (transaction, rai::test_genesis_key.pub, info2);
ASSERT_NE (latest1, info2.head);
auto block (system.nodes [0]->store.block_get (transaction, info2.head));
auto block (system.nodes[0]->store.block_get (transaction, info2.head));
ASSERT_NE (nullptr, block);
ASSERT_EQ (latest1, block->previous ());
}
ASSERT_TRUE (info2.balance.is_zero ());
ASSERT_EQ (0, system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes[0]->balance (rai::test_genesis_key.pub));
}
TEST (wallet, send_async)
@ -168,10 +168,9 @@ TEST (wallet, send_async)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
std::thread thread ([&system] ()
{
std::thread thread ([&system]() {
auto iterations (0);
while (!system.nodes [0]->balance (rai::test_genesis_key.pub).is_zero ())
while (!system.nodes[0]->balance (rai::test_genesis_key.pub).is_zero ())
{
system.poll ();
++iterations;
@ -179,7 +178,7 @@ TEST (wallet, send_async)
}
});
bool success (false);
system.wallet (0)->send_async (rai::test_genesis_key.pub, key2.pub, std::numeric_limits <rai::uint128_t>::max (), [&success] (std::shared_ptr <rai::block> block_a) { ASSERT_NE (nullptr, block_a); success = true; });
system.wallet (0)->send_async (rai::test_genesis_key.pub, key2.pub, std::numeric_limits<rai::uint128_t>::max (), [&success](std::shared_ptr<rai::block> block_a) { ASSERT_NE (nullptr, block_a); success = true; });
thread.join ();
ASSERT_TRUE (success);
}
@ -187,23 +186,23 @@ TEST (wallet, send_async)
TEST (wallet, spend)
{
rai::system system (24000, 1);
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
// Sending from empty accounts should always be an error. Accounts need to be opened with an open block, not a send block.
ASSERT_EQ (nullptr, system.wallet (0)->send_action (0, key2.pub, 0));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, std::numeric_limits <rai::uint128_t>::max ()));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, std::numeric_limits<rai::uint128_t>::max ()));
rai::account_info info2;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
system.nodes [0]->store.account_get (transaction, rai::test_genesis_key.pub, info2);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.nodes[0]->store.account_get (transaction, rai::test_genesis_key.pub, info2);
ASSERT_NE (latest1, info2.head);
auto block (system.nodes [0]->store.block_get (transaction, info2.head));
auto block (system.nodes[0]->store.block_get (transaction, info2.head));
ASSERT_NE (nullptr, block);
ASSERT_EQ (latest1, block->previous ());
}
ASSERT_TRUE (info2.balance.is_zero ());
ASSERT_EQ (0, system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (0, system.nodes[0]->balance (rai::test_genesis_key.pub));
}
TEST (wallet, change)
@ -211,10 +210,10 @@ TEST (wallet, change)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
auto block1 (system.nodes [0]->representative (rai::test_genesis_key.pub));
auto block1 (system.nodes[0]->representative (rai::test_genesis_key.pub));
ASSERT_FALSE (block1.is_zero ());
ASSERT_NE (nullptr, system.wallet (0)->change_action (rai::test_genesis_key.pub, key2.pub));
auto block2 (system.nodes [0]->representative (rai::test_genesis_key.pub));
auto block2 (system.nodes[0]->representative (rai::test_genesis_key.pub));
ASSERT_FALSE (block2.is_zero ());
ASSERT_NE (block1, block2);
}
@ -225,7 +224,7 @@ TEST (wallet, partial_spend)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, 500));
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - 500, system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - 500, system.nodes[0]->balance (rai::test_genesis_key.pub));
}
TEST (wallet, spend_no_previous)
@ -233,9 +232,9 @@ TEST (wallet, spend_no_previous)
rai::system system (24000, 1);
{
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
rai::account_info info1;
ASSERT_FALSE (system.nodes [0]->store.account_get (transaction, rai::test_genesis_key.pub, info1));
ASSERT_FALSE (system.nodes[0]->store.account_get (transaction, rai::test_genesis_key.pub, info1));
for (auto i (0); i < 50; ++i)
{
rai::keypair key;
@ -244,7 +243,7 @@ TEST (wallet, spend_no_previous)
}
rai::keypair key2;
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, 500));
ASSERT_EQ (std::numeric_limits <rai::uint128_t>::max () - 500, system.nodes [0]->balance (rai::test_genesis_key.pub));
ASSERT_EQ (std::numeric_limits<rai::uint128_t>::max () - 500, system.nodes[0]->balance (rai::test_genesis_key.pub));
}
TEST (wallet, find_none)
@ -305,7 +304,7 @@ TEST (wallet, rekey)
rai::raw_key prv2;
wallet.fetch (transaction, key1.pub, prv2);
ASSERT_EQ (key1.prv, prv2);
*wallet.password.values [0] = 2;
*wallet.password.values[0] = 2;
ASSERT_TRUE (wallet.rekey (transaction, "2"));
}
@ -337,7 +336,7 @@ TEST (account, encode_fail)
rai::uint256_union number0 (0);
std::string str0;
number0.encode_account (str0);
str0 [16] ^= 1;
str0[16] ^= 1;
rai::uint256_union number1;
ASSERT_TRUE (number1.decode_account (str0));
}
@ -365,7 +364,7 @@ TEST (fan, reconstitute)
{
rai::uint256_union value0 (0);
rai::fan fan (value0, 1024);
for (auto & i: fan.values)
for (auto & i : fan.values)
{
ASSERT_NE (value0, *i);
}
@ -552,7 +551,7 @@ TEST (wallet_store, move)
wallet2.insert_adhoc (transaction, key2.prv);
ASSERT_FALSE (wallet1.exists (transaction, key2.pub));
ASSERT_TRUE (wallet2.exists (transaction, key2.pub));
std::vector <rai::public_key> keys;
std::vector<rai::public_key> keys;
keys.push_back (key2.pub);
ASSERT_FALSE (wallet1.move (transaction, wallet2, keys));
ASSERT_TRUE (wallet1.exists (transaction, key2.pub));
@ -602,7 +601,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);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
ASSERT_FALSE (rai::work_validate (1, wallet->work_fetch (transaction, 0, 1)));
}
@ -616,9 +615,9 @@ TEST (wallet, work)
rai::uint256_union root1;
rai::account account1;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account1 = system.account (transaction, 0);
root1 = system.nodes [0]->ledger.latest_root (transaction, account1);
root1 = system.nodes[0]->ledger.latest_root (transaction, account1);
work4 = wallet->work_fetch (transaction, account1, root1);
}
ASSERT_FALSE (rai::work_validate (root1, work4));
@ -627,7 +626,7 @@ TEST (wallet, work)
while (rai::work_validate (root1, work3))
{
system.poll ();
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
// Make sure work_get and work_fetch retrieve the same thing
ASSERT_FALSE (wallet->store.work_get (transaction, account1, work3));
++iteration;
@ -639,18 +638,18 @@ TEST (wallet, work_generate)
{
rai::system system (24000, 1);
auto wallet (system.wallet (0));
rai::uint128_t amount1 (system.nodes [0]->balance (rai::test_genesis_key.pub));
rai::uint128_t amount1 (system.nodes[0]->balance (rai::test_genesis_key.pub));
uint64_t work1;
wallet->insert_adhoc (rai::test_genesis_key.prv);
rai::account account1;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account1 = system.account (transaction, 0);
}
rai::keypair key;
wallet->send_action (rai::test_genesis_key.pub, key.pub, 100);
auto iterations1 (0);
while (system.nodes [0]->ledger.account_balance (rai::transaction (system.nodes [0]->store.environment, nullptr, false), rai::test_genesis_key.pub) == amount1)
while (system.nodes[0]->ledger.account_balance (rai::transaction (system.nodes[0]->store.environment, nullptr, false), rai::test_genesis_key.pub) == amount1)
{
system.poll ();
++iterations1;
@ -663,8 +662,8 @@ TEST (wallet, work_generate)
system.poll ();
++iterations2;
ASSERT_LT (iterations2, 200);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
again = wallet->store.work_get (transaction, account1, work1) || rai::work_validate (system.nodes [0]->ledger.latest_root (transaction, account1), work1);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
again = wallet->store.work_get (transaction, account1, work1) || rai::work_validate (system.nodes[0]->ledger.latest_root (transaction, account1), work1);
}
}
@ -672,7 +671,7 @@ TEST (wallet, unsynced_work)
{
rai::system system (24000, 1);
auto wallet (system.wallet (0));
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
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 (rai::work_validate (1, work1));
@ -705,29 +704,29 @@ TEST (wallet, version_1_2_upgrade)
rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special));
rai::raw_key kdf;
kdf.data.clear ();
password_l.decrypt (value.key, kdf, wallet->store.salt (transaction).owords [0]);
password_l.decrypt (value.key, kdf, wallet->store.salt (transaction).owords[0]);
rai::uint256_union ciphertext;
ciphertext.encrypt (key.prv, password_l, wallet->store.salt (transaction).owords [0]);
ciphertext.encrypt (key.prv, password_l, wallet->store.salt (transaction).owords[0]);
wallet->store.entry_put_raw (transaction, key.pub, rai::wallet_value (ciphertext, 0));
wallet->store.version_put (transaction, 1);
}
wallet->enter_password ("1");
ASSERT_EQ (true, wallet->valid_password ());
ASSERT_EQ (2, wallet->store.version (rai::transaction (wallet->store.environment, nullptr, false)));
rai::raw_key prv;
ASSERT_FALSE (wallet->store.fetch (rai::transaction (wallet->store.environment, nullptr, false), key.pub, prv));
ASSERT_EQ (key.prv, prv);
{
rai::transaction transaction (wallet->store.environment, nullptr, true);
rai::raw_key password_l;
rai::wallet_value value (wallet->store.entry_get_raw (transaction, rai::wallet_store::wallet_key_special));
rai::raw_key kdf;
wallet->store.derive_key (kdf, transaction, "");
password_l.decrypt (value.key, kdf, wallet->store.salt (transaction).owords [0]);
password_l.decrypt (value.key, kdf, wallet->store.salt (transaction).owords[0]);
rai::uint256_union ciphertext;
ciphertext.encrypt (key.prv, password_l, wallet->store.salt (transaction).owords [0]);
ciphertext.encrypt (key.prv, password_l, wallet->store.salt (transaction).owords[0]);
wallet->store.entry_put_raw (transaction, key.pub, rai::wallet_value (ciphertext, 0));
wallet->store.version_put (transaction, 1);
}
@ -860,7 +859,7 @@ TEST (wallet, no_work)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
auto block (system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, std::numeric_limits <rai::uint128_t>::max (), false));
auto block (system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, std::numeric_limits<rai::uint128_t>::max (), false));
ASSERT_NE (nullptr, block);
ASSERT_EQ (0, block->block_work ());
}
@ -870,13 +869,13 @@ TEST (wallet, send_race)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::keypair key2;
system.nodes [0]->block_processor.stop ();
system.nodes[0]->block_processor.stop ();
{
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, rai::Gxrb_ratio));
ASSERT_NE (nullptr, system.wallet (0)->send_action (rai::test_genesis_key.pub, key2.pub, rai::Gxrb_ratio));
}
auto iterations (0);
while (system.nodes [0]->balance (rai::test_genesis_key.pub) != rai::genesis_amount - rai::Gxrb_ratio * 2)
while (system.nodes[0]->balance (rai::test_genesis_key.pub) != rai::genesis_amount - rai::Gxrb_ratio * 2)
{
system.poll ();
++iterations;

View file

@ -6,7 +6,7 @@ TEST (wallets, DISABLED_open_create)
{
rai::system system (24000, 1);
bool error (false);
rai::wallets wallets (error, *system.nodes [0]);
rai::wallets wallets (error, *system.nodes[0]);
ASSERT_FALSE (error);
ASSERT_EQ (0, wallets.items.size ());
rai::uint256_union id;
@ -22,7 +22,7 @@ TEST (wallets, DISABLED_open_existing)
rai::uint256_union id;
{
bool error (false);
rai::wallets wallets (error, *system.nodes [0]);
rai::wallets wallets (error, *system.nodes[0]);
ASSERT_FALSE (error);
ASSERT_EQ (0, wallets.items.size ());
auto wallet (wallets.create (id));
@ -41,7 +41,7 @@ TEST (wallets, DISABLED_open_existing)
}
{
bool error (false);
rai::wallets wallets (error, *system.nodes [0]);
rai::wallets wallets (error, *system.nodes[0]);
ASSERT_FALSE (error);
ASSERT_EQ (1, wallets.items.size ());
ASSERT_NE (nullptr, wallets.open (id));
@ -54,7 +54,7 @@ TEST (wallets, DISABLED_remove)
rai::uint256_union one (1);
{
bool error (false);
rai::wallets wallets (error, *system.nodes [0]);
rai::wallets wallets (error, *system.nodes[0]);
ASSERT_FALSE (error);
ASSERT_EQ (0, wallets.items.size ());
auto wallet (wallets.create (one));
@ -65,7 +65,7 @@ TEST (wallets, DISABLED_remove)
}
{
bool error (false);
rai::wallets wallets (error, *system.nodes [0]);
rai::wallets wallets (error, *system.nodes[0]);
ASSERT_FALSE (error);
ASSERT_EQ (0, wallets.items.size ());
}

View file

@ -5,7 +5,7 @@
TEST (work, one)
{
rai::work_pool pool (std::numeric_limits <unsigned>::max (), nullptr);
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 (rai::work_validate (block));
@ -13,7 +13,7 @@ TEST (work, one)
TEST (work, validate)
{
rai::work_pool pool (std::numeric_limits <unsigned>::max (), nullptr);
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 (rai::work_validate (send_block));
send_block.block_work_set (pool.generate (send_block.root ()));
@ -22,11 +22,10 @@ TEST (work, validate)
TEST (work, cancel)
{
rai::work_pool pool (std::numeric_limits <unsigned>::max (), nullptr);
rai::work_pool pool (std::numeric_limits<unsigned>::max (), nullptr);
rai::uint256_union key (1);
boost::optional <uint64_t> work (0);
pool.generate (key, [&work] (boost::optional <uint64_t> work_a)
{
boost::optional<uint64_t> work (0);
pool.generate (key, [&work](boost::optional<uint64_t> work_a) {
work = work_a;
});
pool.cancel (key);
@ -35,19 +34,19 @@ TEST (work, cancel)
TEST (work, cancel_many)
{
rai::work_pool pool (std::numeric_limits <unsigned>::max (), nullptr);
rai::work_pool pool (std::numeric_limits<unsigned>::max (), nullptr);
rai::uint256_union key1 (1);
rai::uint256_union key2 (2);
rai::uint256_union key3 (1);
rai::uint256_union key4 (1);
rai::uint256_union key5 (3);
rai::uint256_union key6 (1);
pool.generate (key1, [] (boost::optional <uint64_t>) {});
pool.generate (key2, [] (boost::optional <uint64_t>) {});
pool.generate (key3, [] (boost::optional <uint64_t>) {});
pool.generate (key4, [] (boost::optional <uint64_t>) {});
pool.generate (key5, [] (boost::optional <uint64_t>) {});
pool.generate (key6, [] (boost::optional <uint64_t>) {});
pool.generate (key1, [](boost::optional<uint64_t>) {});
pool.generate (key2, [](boost::optional<uint64_t>) {});
pool.generate (key3, [](boost::optional<uint64_t>) {});
pool.generate (key4, [](boost::optional<uint64_t>) {});
pool.generate (key5, [](boost::optional<uint64_t>) {});
pool.generate (key6, [](boost::optional<uint64_t>) {});
pool.cancel (key1);
}
@ -55,13 +54,13 @@ TEST (work, DISABLED_opencl)
{
rai::logging logging;
logging.init (rai::unique_path ());
auto opencl (rai::opencl_work::create (true, {0, 1, 1024 * 1024}, logging));
auto opencl (rai::opencl_work::create (true, { 0, 1, 1024 * 1024 }, logging));
if (opencl != nullptr)
{
rai::work_pool pool (std::numeric_limits <unsigned>::max (), opencl ? [&opencl] (rai::uint256_union const & root_a)
{
rai::work_pool pool (std::numeric_limits<unsigned>::max (), opencl ? [&opencl](rai::uint256_union const & root_a) {
return opencl->generate_work (root_a);
} : std::function <boost::optional <uint64_t> (rai::uint256_union const &)> (nullptr));
}
: std::function<boost::optional<uint64_t> (rai::uint256_union const &)> (nullptr));
ASSERT_NE (nullptr, pool.opencl);
rai::uint256_union root;
for (auto i (0); i < 1; ++i)

View file

@ -23,7 +23,7 @@ bool rai::from_string_hex (std::string const & value_a, uint64_t & target_a)
{
stream >> number_l;
target_a = number_l;
if (!stream.eof())
if (!stream.eof ())
{
result = true;
}
@ -100,9 +100,9 @@ rai::send_hashables::send_hashables (bool & error_a, boost::property_tree::ptree
{
try
{
auto previous_l (tree_a.get <std::string> ("previous"));
auto destination_l (tree_a.get <std::string> ("destination"));
auto balance_l (tree_a.get <std::string> ("balance"));
auto previous_l (tree_a.get<std::string> ("previous"));
auto destination_l (tree_a.get<std::string> ("destination"));
auto balance_l (tree_a.get<std::string> ("balance"));
error_a = previous.decode_hex (previous_l);
if (!error_a)
{
@ -186,12 +186,12 @@ bool rai::send_block::deserialize_json (boost::property_tree::ptree const & tree
auto result (false);
try
{
assert (tree_a.get <std::string> ("type") == "send");
auto previous_l (tree_a.get <std::string> ("previous"));
auto destination_l (tree_a.get <std::string> ("destination"));
auto balance_l (tree_a.get <std::string> ("balance"));
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get <std::string> ("signature"));
assert (tree_a.get<std::string> ("type") == "send");
auto previous_l (tree_a.get<std::string> ("previous"));
auto destination_l (tree_a.get<std::string> ("destination"));
auto balance_l (tree_a.get<std::string> ("balance"));
auto work_l (tree_a.get<std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
result = hashables.previous.decode_hex (previous_l);
if (!result)
{
@ -244,8 +244,8 @@ hashables (error_a, tree_a)
{
try
{
auto signature_l (tree_a.get <std::string> ("signature"));
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
auto work_l (tree_a.get<std::string> ("work"));
error_a = signature.decode_hex (signature_l);
if (!error_a)
{
@ -259,9 +259,9 @@ hashables (error_a, tree_a)
}
}
bool rai::send_block::operator == (rai::block const & other_a) const
bool rai::send_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast <rai::send_block const *> (&other_a));
auto other_l (dynamic_cast<rai::send_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
@ -275,7 +275,7 @@ rai::block_type rai::send_block::type () const
return rai::block_type::send;
}
bool rai::send_block::operator == (rai::send_block const & other_a) const
bool rai::send_block::operator== (rai::send_block const & other_a) const
{
auto result (hashables.destination == other_a.hashables.destination && hashables.previous == other_a.hashables.previous && hashables.balance == other_a.hashables.balance && work == other_a.work && signature == other_a.signature);
return result;
@ -330,9 +330,9 @@ rai::open_hashables::open_hashables (bool & error_a, boost::property_tree::ptree
{
try
{
auto source_l (tree_a.get <std::string> ("source"));
auto representative_l (tree_a.get <std::string> ("representative"));
auto account_l (tree_a.get <std::string> ("account"));
auto source_l (tree_a.get<std::string> ("source"));
auto representative_l (tree_a.get<std::string> ("representative"));
auto account_l (tree_a.get<std::string> ("account"));
error_a = source.decode_hex (source_l);
if (!error_a)
{
@ -391,8 +391,8 @@ hashables (error_a, tree_a)
{
try
{
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get <std::string> ("signature"));
auto work_l (tree_a.get<std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
error_a = rai::from_string_hex (work_l, work);
if (!error_a)
{
@ -424,7 +424,7 @@ void rai::open_block::block_work_set (uint64_t work_a)
rai::block_hash rai::open_block::previous () const
{
rai::block_hash result (0);
return result;
return result;
}
void rai::open_block::serialize (rai::stream & stream_a) const
@ -479,12 +479,12 @@ bool rai::open_block::deserialize_json (boost::property_tree::ptree const & tree
auto result (false);
try
{
assert (tree_a.get <std::string> ("type") == "open");
auto source_l (tree_a.get <std::string> ("source"));
auto representative_l (tree_a.get <std::string> ("representative"));
auto account_l (tree_a.get <std::string> ("account"));
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get <std::string> ("signature"));
assert (tree_a.get<std::string> ("type") == "open");
auto source_l (tree_a.get<std::string> ("source"));
auto representative_l (tree_a.get<std::string> ("representative"));
auto account_l (tree_a.get<std::string> ("account"));
auto work_l (tree_a.get<std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
result = hashables.source.decode_hex (source_l);
if (!result)
{
@ -520,9 +520,9 @@ rai::block_type rai::open_block::type () const
return rai::block_type::open;
}
bool rai::open_block::operator == (rai::block const & other_a) const
bool rai::open_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast <rai::open_block const *> (&other_a));
auto other_l (dynamic_cast<rai::open_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
@ -531,7 +531,7 @@ bool rai::open_block::operator == (rai::block const & other_a) const
return result;
}
bool rai::open_block::operator == (rai::open_block const & other_a) const
bool rai::open_block::operator== (rai::open_block const & other_a) const
{
return hashables.source == other_a.hashables.source && hashables.representative == other_a.hashables.representative && hashables.account == other_a.hashables.account && work == other_a.work && signature == other_a.signature;
}
@ -575,8 +575,8 @@ rai::change_hashables::change_hashables (bool & error_a, boost::property_tree::p
{
try
{
auto previous_l (tree_a.get <std::string> ("previous"));
auto representative_l (tree_a.get <std::string> ("representative"));
auto previous_l (tree_a.get<std::string> ("previous"));
auto representative_l (tree_a.get<std::string> ("representative"));
error_a = previous.decode_hex (previous_l);
if (!error_a)
{
@ -622,8 +622,8 @@ hashables (error_a, tree_a)
{
try
{
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get <std::string> ("signature"));
auto work_l (tree_a.get<std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
error_a = rai::from_string_hex (work_l, work);
if (!error_a)
{
@ -703,11 +703,11 @@ bool rai::change_block::deserialize_json (boost::property_tree::ptree const & tr
auto result (false);
try
{
assert (tree_a.get <std::string> ("type") == "change");
auto previous_l (tree_a.get <std::string> ("previous"));
auto representative_l (tree_a.get <std::string> ("representative"));
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get <std::string> ("signature"));
assert (tree_a.get<std::string> ("type") == "change");
auto previous_l (tree_a.get<std::string> ("previous"));
auto representative_l (tree_a.get<std::string> ("representative"));
auto work_l (tree_a.get<std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
result = hashables.previous.decode_hex (previous_l);
if (!result)
{
@ -739,9 +739,9 @@ rai::block_type rai::change_block::type () const
return rai::block_type::change;
}
bool rai::change_block::operator == (rai::block const & other_a) const
bool rai::change_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast <rai::change_block const *> (&other_a));
auto other_l (dynamic_cast<rai::change_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
@ -750,7 +750,7 @@ bool rai::change_block::operator == (rai::block const & other_a) const
return result;
}
bool rai::change_block::operator == (rai::change_block const & other_a) const
bool rai::change_block::operator== (rai::change_block const & other_a) const
{
return hashables.previous == other_a.hashables.previous && hashables.representative == other_a.hashables.representative && work == other_a.work && signature == other_a.signature;
}
@ -775,16 +775,16 @@ void rai::change_block::signature_set (rai::uint512_union const & signature_a)
signature = signature_a;
}
std::unique_ptr <rai::block> rai::deserialize_block_json (boost::property_tree::ptree const & tree_a)
std::unique_ptr<rai::block> rai::deserialize_block_json (boost::property_tree::ptree const & tree_a)
{
std::unique_ptr <rai::block> result;
std::unique_ptr<rai::block> result;
try
{
auto type (tree_a.get <std::string> ("type"));
auto type (tree_a.get<std::string> ("type"));
if (type == "receive")
{
bool error;
std::unique_ptr <rai::receive_block> obj (new rai::receive_block (error, tree_a));
std::unique_ptr<rai::receive_block> obj (new rai::receive_block (error, tree_a));
if (!error)
{
result = std::move (obj);
@ -793,7 +793,7 @@ std::unique_ptr <rai::block> rai::deserialize_block_json (boost::property_tree::
else if (type == "send")
{
bool error;
std::unique_ptr <rai::send_block> obj (new rai::send_block (error, tree_a));
std::unique_ptr<rai::send_block> obj (new rai::send_block (error, tree_a));
if (!error)
{
result = std::move (obj);
@ -802,7 +802,7 @@ std::unique_ptr <rai::block> rai::deserialize_block_json (boost::property_tree::
else if (type == "open")
{
bool error;
std::unique_ptr <rai::open_block> obj (new rai::open_block (error, tree_a));
std::unique_ptr<rai::open_block> obj (new rai::open_block (error, tree_a));
if (!error)
{
result = std::move (obj);
@ -811,7 +811,7 @@ std::unique_ptr <rai::block> rai::deserialize_block_json (boost::property_tree::
else if (type == "change")
{
bool error;
std::unique_ptr <rai::change_block> obj (new rai::change_block (error, tree_a));
std::unique_ptr<rai::change_block> obj (new rai::change_block (error, tree_a));
if (!error)
{
result = std::move (obj);
@ -824,27 +824,27 @@ std::unique_ptr <rai::block> rai::deserialize_block_json (boost::property_tree::
return result;
}
std::unique_ptr <rai::block> rai::deserialize_block (rai::stream & stream_a)
std::unique_ptr<rai::block> rai::deserialize_block (rai::stream & stream_a)
{
rai::block_type type;
auto error (read (stream_a, type));
std::unique_ptr <rai::block> result;
std::unique_ptr<rai::block> result;
if (!error)
{
result = rai::deserialize_block (stream_a, type);
result = rai::deserialize_block (stream_a, type);
}
return result;
}
std::unique_ptr <rai::block> rai::deserialize_block (rai::stream & stream_a, rai::block_type type_a)
std::unique_ptr<rai::block> rai::deserialize_block (rai::stream & stream_a, rai::block_type type_a)
{
std::unique_ptr <rai::block> result;
std::unique_ptr<rai::block> result;
switch (type_a)
{
case rai::block_type::receive:
{
bool error;
std::unique_ptr <rai::receive_block> obj (new rai::receive_block (error, stream_a));
std::unique_ptr<rai::receive_block> obj (new rai::receive_block (error, stream_a));
if (!error)
{
result = std::move (obj);
@ -854,7 +854,7 @@ std::unique_ptr <rai::block> rai::deserialize_block (rai::stream & stream_a, rai
case rai::block_type::send:
{
bool error;
std::unique_ptr <rai::send_block> obj (new rai::send_block (error, stream_a));
std::unique_ptr<rai::send_block> obj (new rai::send_block (error, stream_a));
if (!error)
{
result = std::move (obj);
@ -864,7 +864,7 @@ std::unique_ptr <rai::block> rai::deserialize_block (rai::stream & stream_a, rai
case rai::block_type::open:
{
bool error;
std::unique_ptr <rai::open_block> obj (new rai::open_block (error, stream_a));
std::unique_ptr<rai::open_block> obj (new rai::open_block (error, stream_a));
if (!error)
{
result = std::move (obj);
@ -874,7 +874,7 @@ std::unique_ptr <rai::block> rai::deserialize_block (rai::stream & stream_a, rai
case rai::block_type::change:
{
bool error;
std::unique_ptr <rai::change_block> obj (new rai::change_block (error, stream_a));
std::unique_ptr<rai::change_block> obj (new rai::change_block (error, stream_a));
if (!error)
{
result = std::move (obj);
@ -893,7 +893,7 @@ void rai::receive_block::visit (rai::block_visitor & visitor_a) const
visitor_a.receive_block (*this);
}
bool rai::receive_block::operator == (rai::receive_block const & other_a) const
bool rai::receive_block::operator== (rai::receive_block const & other_a) const
{
auto result (hashables.previous == other_a.hashables.previous && hashables.source == other_a.hashables.source && work == other_a.work && signature == other_a.signature);
return result;
@ -923,11 +923,11 @@ bool rai::receive_block::deserialize_json (boost::property_tree::ptree const & t
auto result (false);
try
{
assert (tree_a.get <std::string> ("type") == "receive");
auto previous_l (tree_a.get <std::string> ("previous"));
auto source_l (tree_a.get <std::string> ("source"));
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get <std::string> ("signature"));
assert (tree_a.get<std::string> ("type") == "receive");
auto previous_l (tree_a.get<std::string> ("previous"));
auto source_l (tree_a.get<std::string> ("source"));
auto work_l (tree_a.get<std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
result = hashables.previous.decode_hex (previous_l);
if (!result)
{
@ -978,7 +978,7 @@ void rai::receive_block::serialize_json (std::string & string_a) const
rai::receive_block::receive_block (rai::block_hash const & previous_a, rai::block_hash const & source_a, rai::raw_key const & prv_a, rai::public_key const & pub_a, uint64_t work_a) :
hashables (previous_a, source_a),
signature (rai::sign_message (prv_a, pub_a, hash())),
signature (rai::sign_message (prv_a, pub_a, hash ())),
work (work_a)
{
}
@ -1003,8 +1003,8 @@ hashables (error_a, tree_a)
{
try
{
auto signature_l (tree_a.get <std::string> ("signature"));
auto work_l (tree_a.get <std::string> ("work"));
auto signature_l (tree_a.get<std::string> ("signature"));
auto work_l (tree_a.get<std::string> ("work"));
error_a = signature.decode_hex (signature_l);
if (!error_a)
{
@ -1033,9 +1033,9 @@ void rai::receive_block::block_work_set (uint64_t work_a)
work = work_a;
}
bool rai::receive_block::operator == (rai::block const & other_a) const
bool rai::receive_block::operator== (rai::block const & other_a) const
{
auto other_l (dynamic_cast <rai::receive_block const *> (&other_a));
auto other_l (dynamic_cast<rai::receive_block const *> (&other_a));
auto result (other_l != nullptr);
if (result)
{
@ -1093,8 +1093,8 @@ rai::receive_hashables::receive_hashables (bool & error_a, boost::property_tree:
{
try
{
auto previous_l (tree_a.get <std::string> ("previous"));
auto source_l (tree_a.get <std::string> ("source"));
auto previous_l (tree_a.get<std::string> ("previous"));
auto source_l (tree_a.get<std::string> ("source"));
error_a = previous.decode_hex (previous_l);
if (!error_a)
{

View file

@ -12,20 +12,20 @@ namespace rai
std::string to_string_hex (uint64_t);
bool from_string_hex (std::string const &, uint64_t &);
// We operate on streams of uint8_t by convention
using stream = std::basic_streambuf <uint8_t>;
using stream = std::basic_streambuf<uint8_t>;
// Read a raw byte stream the size of `T' and fill value.
template <typename T>
bool read (rai::stream & stream_a, T & value)
{
static_assert (std::is_pod <T>::value, "Can't stream read non-standard layout types");
auto amount_read (stream_a.sgetn (reinterpret_cast <uint8_t *> (&value), sizeof (value)));
static_assert (std::is_pod<T>::value, "Can't stream read non-standard layout types");
auto amount_read (stream_a.sgetn (reinterpret_cast<uint8_t *> (&value), sizeof (value)));
return amount_read != sizeof (value);
}
template <typename T>
void write (rai::stream & stream_a, T const & value)
{
static_assert (std::is_pod <T>::value, "Can't stream write non-standard layout types");
auto amount_written (stream_a.sputn (reinterpret_cast <uint8_t const *> (&value), sizeof (value)));
static_assert (std::is_pod<T>::value, "Can't stream write non-standard layout types");
auto amount_written (stream_a.sputn (reinterpret_cast<uint8_t const *> (&value), sizeof (value)));
assert (amount_written == sizeof (value));
}
class block_visitor;
@ -57,7 +57,7 @@ public:
virtual void serialize (rai::stream &) const = 0;
virtual void serialize_json (std::string &) const = 0;
virtual void visit (rai::block_visitor &) const = 0;
virtual bool operator == (rai::block const &) const = 0;
virtual bool operator== (rai::block const &) const = 0;
virtual rai::block_type type () const = 0;
virtual void signature_set (rai::uint512_union const &) = 0;
};
@ -93,8 +93,8 @@ public:
void visit (rai::block_visitor &) const override;
rai::block_type type () const override;
void signature_set (rai::uint512_union const &) override;
bool operator == (rai::block const &) const override;
bool operator == (rai::send_block const &) const;
bool operator== (rai::block const &) const override;
bool operator== (rai::send_block const &) const;
static size_t constexpr size = sizeof (rai::account) + sizeof (rai::block_hash) + sizeof (rai::amount) + sizeof (rai::signature) + sizeof (uint64_t);
send_hashables hashables;
rai::signature signature;
@ -131,8 +131,8 @@ public:
void visit (rai::block_visitor &) const override;
rai::block_type type () const override;
void signature_set (rai::uint512_union const &) override;
bool operator == (rai::block const &) const override;
bool operator == (rai::receive_block const &) const;
bool operator== (rai::block const &) const override;
bool operator== (rai::receive_block const &) const;
static size_t constexpr size = sizeof (rai::block_hash) + sizeof (rai::block_hash) + sizeof (rai::signature) + sizeof (uint64_t);
receive_hashables hashables;
rai::signature signature;
@ -171,8 +171,8 @@ public:
void visit (rai::block_visitor &) const override;
rai::block_type type () const override;
void signature_set (rai::uint512_union const &) override;
bool operator == (rai::block const &) const override;
bool operator == (rai::open_block const &) const;
bool operator== (rai::block const &) const override;
bool operator== (rai::open_block const &) const;
static size_t constexpr size = sizeof (rai::block_hash) + sizeof (rai::account) + sizeof (rai::account) + sizeof (rai::signature) + sizeof (uint64_t);
rai::open_hashables hashables;
rai::signature signature;
@ -209,8 +209,8 @@ public:
void visit (rai::block_visitor &) const override;
rai::block_type type () const override;
void signature_set (rai::uint512_union const &) override;
bool operator == (rai::block const &) const override;
bool operator == (rai::change_block const &) const;
bool operator== (rai::block const &) const override;
bool operator== (rai::change_block const &) const;
static size_t constexpr size = sizeof (rai::block_hash) + sizeof (rai::account) + sizeof (rai::signature) + sizeof (uint64_t);
rai::change_hashables hashables;
rai::signature signature;
@ -224,8 +224,8 @@ public:
virtual void open_block (rai::open_block const &) = 0;
virtual void change_block (rai::change_block const &) = 0;
};
std::unique_ptr <rai::block> deserialize_block (rai::stream &);
std::unique_ptr <rai::block> deserialize_block (rai::stream &, rai::block_type);
std::unique_ptr <rai::block> deserialize_block_json (boost::property_tree::ptree const &);
std::unique_ptr<rai::block> deserialize_block (rai::stream &);
std::unique_ptr<rai::block> deserialize_block (rai::stream &, rai::block_type);
std::unique_ptr<rai::block> deserialize_block_json (boost::property_tree::ptree const &);
void serialize_block (rai::stream &, rai::block const &);
}

View file

@ -17,25 +17,25 @@
extern "C" {
void xrb_uint256_to_string (xrb_uint256 source, char * destination)
{
auto const & number (*reinterpret_cast <rai::uint256_union *> (source));
auto const & number (*reinterpret_cast<rai::uint256_union *> (source));
strncpy (destination, number.to_string ().c_str (), 64);
}
void xrb_uint256_to_address (xrb_uint256 source, char * destination)
{
auto const & number (*reinterpret_cast <rai::uint256_union *> (source));
auto const & number (*reinterpret_cast<rai::uint256_union *> (source));
strncpy (destination, number.to_account ().c_str (), 65);
}
void xrb_uint512_to_string (xrb_uint512 source, char * destination)
{
auto const & number (*reinterpret_cast <rai::uint512_union *> (source));
auto const & number (*reinterpret_cast<rai::uint512_union *> (source));
strncpy (destination, number.to_string ().c_str (), 128);
}
int xrb_uint256_from_string (const char * source, xrb_uint256 destination)
{
auto & number (*reinterpret_cast <rai::uint256_union *> (destination));
auto & number (*reinterpret_cast<rai::uint256_union *> (destination));
auto error (number.decode_hex (source));
auto result (error ? 1 : 0);
return result;
@ -43,7 +43,7 @@ int xrb_uint256_from_string (const char * source, xrb_uint256 destination)
int xrb_uint512_from_string (const char * source, xrb_uint512 destination)
{
auto & number (*reinterpret_cast <rai::uint512_union *> (destination));
auto & number (*reinterpret_cast<rai::uint512_union *> (destination));
auto error (number.decode_hex (source));
auto result (error ? 1 : 0);
return result;
@ -59,14 +59,14 @@ int xrb_valid_address (const char * account_a)
void xrb_generate_random (xrb_uint256 seed)
{
auto & number (*reinterpret_cast <rai::uint256_union *> (seed));
auto & number (*reinterpret_cast<rai::uint256_union *> (seed));
rai::random_pool.GenerateBlock (number.bytes.data (), number.bytes.size ());
}
void xrb_seed_key (xrb_uint256 seed, int index, xrb_uint256 destination)
{
auto & seed_l (*reinterpret_cast <rai::uint256_union *> (seed));
auto & destination_l (*reinterpret_cast <rai::uint256_union *> (destination));
auto & seed_l (*reinterpret_cast<rai::uint256_union *> (seed));
auto & destination_l (*reinterpret_cast<rai::uint256_union *> (destination));
rai::deterministic_key (seed_l, index, destination_l);
}
@ -90,10 +90,10 @@ char * xrb_sign_transaction (const char * transaction, const xrb_uint256 private
rai::uint256_union pub;
ed25519_publickey (private_key, pub.bytes.data ());
rai::raw_key prv;
prv.data = *reinterpret_cast <rai::uint256_union *> (private_key);
prv.data = *reinterpret_cast<rai::uint256_union *> (private_key);
block->signature_set (rai::sign_message (prv, pub, block->hash ()));
auto json (block->to_json ());
result = reinterpret_cast <char *> (malloc (json.size () + 1));
result = reinterpret_cast<char *> (malloc (json.size () + 1));
strncpy (result, json.c_str (), json.size () + 1);
}
}
@ -119,7 +119,7 @@ char * xrb_work_transaction (const char * transaction)
auto work (pool.generate (block->root ()));
block->block_work_set (work);
auto json (block->to_json ());
result = reinterpret_cast <char *> (malloc (json.size () + 1));
result = reinterpret_cast<char *> (malloc (json.size () + 1));
strncpy (result, json.c_str (), json.size () + 1);
}
}
@ -132,23 +132,23 @@ char * xrb_work_transaction (const char * transaction)
#include <ed25519-donna/ed25519-hash-custom.h>
void ed25519_randombytes_unsafe (void * out, size_t outlen)
{
rai::random_pool.GenerateBlock (reinterpret_cast <uint8_t *> (out), outlen);
rai::random_pool.GenerateBlock (reinterpret_cast<uint8_t *> (out), outlen);
}
void ed25519_hash_init (ed25519_hash_context * ctx)
{
ctx->blake2 = new blake2b_state;
blake2b_init (reinterpret_cast <blake2b_state *> (ctx->blake2), 64);
blake2b_init (reinterpret_cast<blake2b_state *> (ctx->blake2), 64);
}
void ed25519_hash_update (ed25519_hash_context * ctx, uint8_t const * in, size_t inlen)
{
blake2b_update (reinterpret_cast <blake2b_state *> (ctx->blake2), in, inlen);
blake2b_update (reinterpret_cast<blake2b_state *> (ctx->blake2), in, inlen);
}
void ed25519_hash_final (ed25519_hash_context * ctx, uint8_t * out)
{
blake2b_final (reinterpret_cast <blake2b_state *> (ctx->blake2), out, 64);
delete reinterpret_cast <blake2b_state *> (ctx->blake2);
blake2b_final (reinterpret_cast<blake2b_state *> (ctx->blake2), out, 64);
delete reinterpret_cast<blake2b_state *> (ctx->blake2);
}
void ed25519_hash (uint8_t * out, uint8_t const * in, size_t inlen)

View file

@ -11,29 +11,29 @@ thread_local CryptoPP::AutoSeededRandomPool rai::random_pool;
namespace
{
char const * base58_reverse ("~012345678~~~~~~~9:;<=>?@~ABCDE~FGHIJKLMNOP~~~~~~QRSTUVWXYZ[~\\]^_`abcdefghi");
uint8_t base58_decode (char value)
{
assert (value >= '0');
assert (value <= '~');
auto result (base58_reverse [value - 0x30] - 0x30);
return result;
}
char const * account_lookup ("13456789abcdefghijkmnopqrstuwxyz");
char const * account_reverse ("~0~1234567~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~89:;<=>?@AB~CDEFGHIJK~LMNO~~~~~");
char account_encode (uint8_t value)
{
assert (value < 32);
auto result (account_lookup [value]);
return result;
}
uint8_t account_decode (char value)
{
assert (value >= '0');
assert (value <= '~');
auto result (account_reverse [value - 0x30] - 0x30);
return result;
}
char const * base58_reverse ("~012345678~~~~~~~9:;<=>?@~ABCDE~FGHIJKLMNOP~~~~~~QRSTUVWXYZ[~\\]^_`abcdefghi");
uint8_t base58_decode (char value)
{
assert (value >= '0');
assert (value <= '~');
auto result (base58_reverse[value - 0x30] - 0x30);
return result;
}
char const * account_lookup ("13456789abcdefghijkmnopqrstuwxyz");
char const * account_reverse ("~0~1234567~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~89:;<=>?@AB~CDEFGHIJK~LMNO~~~~~");
char account_encode (uint8_t value)
{
assert (value < 32);
auto result (account_lookup[value]);
return result;
}
uint8_t account_decode (char value)
{
assert (value >= '0');
assert (value <= '~');
auto result (account_reverse[value - 0x30] - 0x30);
return result;
}
}
void rai::uint256_union::encode_account (std::string & destination_a) const
@ -44,13 +44,13 @@ void rai::uint256_union::encode_account (std::string & destination_a) const
blake2b_state hash;
blake2b_init (&hash, 5);
blake2b_update (&hash, bytes.data (), bytes.size ());
blake2b_final (&hash, reinterpret_cast <uint8_t *> (&check), 5);
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&check), 5);
rai::uint512_t number_l (number ());
number_l <<= 40;
number_l |= rai::uint512_t (check);
for (auto i (0); i < 60; ++i)
{
auto r (number_l.convert_to <uint8_t> () & 0x1f);
auto r (number_l.convert_to<uint8_t> () & 0x1f);
number_l >>= 5;
destination_a.push_back (account_encode (r));
}
@ -96,8 +96,8 @@ bool rai::uint256_union::decode_account_v1 (std::string const & source_a)
}
if (!result)
{
*this = number_l.convert_to <rai::uint256_t> ();
uint32_t check ((number_l >> 256).convert_to <uint32_t> ());
*this = number_l.convert_to<rai::uint256_t> ();
uint32_t check ((number_l >> 256).convert_to<uint32_t> ());
result = (number_l >> (256 + 32)) != 13;
if (!result)
{
@ -105,7 +105,7 @@ bool rai::uint256_union::decode_account_v1 (std::string const & source_a)
blake2b_state hash;
blake2b_init (&hash, sizeof (validation));
blake2b_update (&hash, bytes.data (), sizeof (bytes));
blake2b_final (&hash, reinterpret_cast <uint8_t *> (&validation), sizeof (validation));
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&validation), sizeof (validation));
result = check != validation;
}
}
@ -118,7 +118,7 @@ bool rai::uint256_union::decode_account (std::string const & source_a)
auto result (source_a.size () != 64);
if (!result)
{
if (source_a [0] == 'x' && source_a [1] == 'r' && source_a [2] == 'b' && (source_a [3] == '_' || source_a [3] == '-'))
if (source_a[0] == 'x' && source_a[1] == 'r' && source_a[2] == 'b' && (source_a[3] == '_' || source_a[3] == '-'))
{
rai::uint512_t number_l;
for (auto i (source_a.begin () + 4), j (source_a.end ()); !result && i != j; ++i)
@ -138,14 +138,14 @@ bool rai::uint256_union::decode_account (std::string const & source_a)
}
if (!result)
{
*this = (number_l >> 40).convert_to <rai::uint256_t> ();
uint64_t check (number_l.convert_to <uint64_t> ());
check &= 0xffffffffff;
*this = (number_l >> 40).convert_to<rai::uint256_t> ();
uint64_t check (number_l.convert_to<uint64_t> ());
check &= 0xffffffffff;
uint64_t validation (0);
blake2b_state hash;
blake2b_init (&hash, 5);
blake2b_update (&hash, bytes.data (), bytes.size ());
blake2b_final (&hash, reinterpret_cast <uint8_t *> (&validation), 5);
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&validation), 5);
result = check != validation;
}
}
@ -166,12 +166,12 @@ rai::uint256_union::uint256_union (rai::uint256_t const & number_a)
rai::uint256_t number_l (number_a);
for (auto i (bytes.rbegin ()), n (bytes.rend ()); i != n; ++i)
{
*i = ((number_l) & 0xff).convert_to <uint8_t> ();
*i = ((number_l)&0xff).convert_to<uint8_t> ();
number_l >>= 8;
}
}
bool rai::uint256_union::operator == (rai::uint256_union const & other_a) const
bool rai::uint256_union::operator== (rai::uint256_union const & other_a) const
{
return bytes == other_a.bytes;
}
@ -186,7 +186,7 @@ void rai::uint256_union::encrypt (rai::raw_key const & cleartext, rai::raw_key c
bool rai::uint256_union::is_zero () const
{
return qwords [0] == 0 && qwords [1] == 0 && qwords [2] == 0 && qwords [3] == 0;
return qwords[0] == 0 && qwords[1] == 0 && qwords[2] == 0 && qwords[3] == 0;
}
std::string rai::uint256_union::to_string () const
@ -196,12 +196,12 @@ std::string rai::uint256_union::to_string () const
return result;
}
bool rai::uint256_union::operator < (rai::uint256_union const & other_a) const
bool rai::uint256_union::operator< (rai::uint256_union const & other_a) const
{
return number () < other_a.number ();
}
rai::uint256_union & rai::uint256_union::operator ^= (rai::uint256_union const & other_a)
rai::uint256_union & rai::uint256_union::operator^= (rai::uint256_union const & other_a)
{
auto j (other_a.qwords.begin ());
for (auto i (qwords.begin ()), n (qwords.end ()); i != n; ++i, ++j)
@ -211,7 +211,7 @@ rai::uint256_union & rai::uint256_union::operator ^= (rai::uint256_union const &
return *this;
}
rai::uint256_union rai::uint256_union::operator ^ (rai::uint256_union const & other_a) const
rai::uint256_union rai::uint256_union::operator^ (rai::uint256_union const & other_a) const
{
rai::uint256_union result;
auto k (result.qwords.begin ());
@ -329,12 +329,12 @@ rai::uint256_union::uint256_union (uint64_t value0)
*this = rai::uint256_t (value0);
}
bool rai::uint256_union::operator != (rai::uint256_union const & other_a) const
bool rai::uint256_union::operator!= (rai::uint256_union const & other_a) const
{
return ! (*this == other_a);
return !(*this == other_a);
}
bool rai::uint512_union::operator == (rai::uint512_union const & other_a) const
bool rai::uint512_union::operator== (rai::uint512_union const & other_a) const
{
return bytes == other_a.bytes;
}
@ -344,7 +344,7 @@ rai::uint512_union::uint512_union (rai::uint512_t const & number_a)
rai::uint512_t number_l (number_a);
for (auto i (bytes.rbegin ()), n (bytes.rend ()); i != n; ++i)
{
*i = ((number_l) & 0xff).convert_to <uint8_t> ();
*i = ((number_l)&0xff).convert_to<uint8_t> ();
number_l >>= 8;
}
}
@ -401,15 +401,15 @@ bool rai::uint512_union::decode_hex (std::string const & text)
return result;
}
bool rai::uint512_union::operator != (rai::uint512_union const & other_a) const
bool rai::uint512_union::operator!= (rai::uint512_union const & other_a) const
{
return ! (*this == other_a);
return !(*this == other_a);
}
rai::uint512_union & rai::uint512_union::operator ^= (rai::uint512_union const & other_a)
rai::uint512_union & rai::uint512_union::operator^= (rai::uint512_union const & other_a)
{
uint256s [0] ^= other_a.uint256s [0];
uint256s [1] ^= other_a.uint256s [1];
uint256s[0] ^= other_a.uint256s[0];
uint256s[1] ^= other_a.uint256s[1];
return *this;
}
@ -425,12 +425,12 @@ rai::raw_key::~raw_key ()
data.clear ();
}
bool rai::raw_key::operator == (rai::raw_key const & other_a) const
bool rai::raw_key::operator== (rai::raw_key const & other_a) const
{
return data == other_a.data;
}
bool rai::raw_key::operator != (rai::raw_key const & other_a) const
bool rai::raw_key::operator!= (rai::raw_key const & other_a) const
{
return !(*this == other_a);
}
@ -456,7 +456,7 @@ void rai::deterministic_key (rai::uint256_union const & seed_a, uint32_t index_a
blake2b_init (&hash, prv_a.bytes.size ());
blake2b_update (&hash, seed_a.bytes.data (), seed_a.bytes.size ());
rai::uint256_union index (index_a);
blake2b_update (&hash, reinterpret_cast <uint8_t *> (&index.dwords [7]), sizeof (uint32_t));
blake2b_update (&hash, reinterpret_cast<uint8_t *> (&index.dwords[7]), sizeof (uint32_t));
blake2b_final (&hash, prv_a.bytes.data (), prv_a.bytes.size ());
}
@ -481,27 +481,27 @@ rai::uint128_union::uint128_union (rai::uint128_t const & value_a)
rai::uint128_t number_l (value_a);
for (auto i (bytes.rbegin ()), n (bytes.rend ()); i != n; ++i)
{
*i = ((number_l) & 0xff).convert_to <uint8_t> ();
*i = ((number_l)&0xff).convert_to<uint8_t> ();
number_l >>= 8;
}
}
bool rai::uint128_union::operator == (rai::uint128_union const & other_a) const
bool rai::uint128_union::operator== (rai::uint128_union const & other_a) const
{
return qwords [0] == other_a.qwords [0] && qwords [1] == other_a.qwords [1];
return qwords[0] == other_a.qwords[0] && qwords[1] == other_a.qwords[1];
}
bool rai::uint128_union::operator != (rai::uint128_union const & other_a) const
bool rai::uint128_union::operator!= (rai::uint128_union const & other_a) const
{
return !(*this == other_a);
}
bool rai::uint128_union::operator < (rai::uint128_union const & other_a) const
bool rai::uint128_union::operator< (rai::uint128_union const & other_a) const
{
return number () < other_a.number ();
}
bool rai::uint128_union::operator > (rai::uint128_union const & other_a) const
bool rai::uint128_union::operator> (rai::uint128_union const & other_a) const
{
return number () > other_a.number ();
}
@ -587,10 +587,12 @@ bool rai::uint128_union::decode_dec (std::string const & text)
return result;
}
void format_frac(std::ostringstream & stream, rai::uint128_t value, rai::uint128_t scale, int precision) {
void format_frac (std::ostringstream & stream, rai::uint128_t value, rai::uint128_t scale, int precision)
{
auto reduce = scale;
auto rem = value;
while (reduce > 1 && rem > 0 && precision > 0) {
while (reduce > 1 && rem > 0 && precision > 0)
{
reduce /= 10;
auto val = rem / reduce;
rem -= val * reduce;
@ -599,50 +601,60 @@ void format_frac(std::ostringstream & stream, rai::uint128_t value, rai::uint128
}
}
void format_dec(std::ostringstream & stream, rai::uint128_t value, char group_sep, const std::string & groupings) {
void format_dec (std::ostringstream & stream, rai::uint128_t value, char group_sep, const std::string & groupings)
{
auto largestPow10 = rai::uint256_t (1);
int dec_count = 1;
while (1) {
while (1)
{
auto next = largestPow10 * 10;
if (next > value) {
if (next > value)
{
break;
}
largestPow10 = next;
dec_count++;
}
if (dec_count > 39) {
if (dec_count > 39)
{
// Impossible.
return;
}
// This could be cached per-locale.
bool emit_group[39];
if (group_sep != 0) {
if (group_sep != 0)
{
int group_index = 0;
int group_count = 0;
for (int i = 0; i < dec_count; i++) {
int groupMax = groupings [group_index];
for (int i = 0; i < dec_count; i++)
{
int groupMax = groupings[group_index];
group_count++;
if (group_count > groupings [group_index]) {
group_index = std::min (group_index + 1, (int)groupings.length() - 1);
if (group_count > groupings[group_index])
{
group_index = std::min (group_index + 1, (int)groupings.length () - 1);
group_count = 1;
emit_group [i] = true;
} else {
emit_group [i] = false;
emit_group[i] = true;
}
else
{
emit_group[i] = false;
}
}
}
auto reduce = rai::uint128_t(largestPow10);
auto reduce = rai::uint128_t (largestPow10);
rai::uint128_t rem = value;
while (reduce > 0) {
while (reduce > 0)
{
auto val = rem / reduce;
rem -= val * reduce;
stream << val;
dec_count--;
if (group_sep != 0 && emit_group [dec_count] && reduce > 1) {
if (group_sep != 0 && emit_group[dec_count] && reduce > 1)
{
stream << group_sep;
}
reduce /= 10;
@ -655,43 +667,50 @@ std::string format_balance (rai::uint128_t balance, rai::uint128_t scale, int pr
auto int_part = balance / scale;
auto frac_part = balance % scale;
auto prec_scale = scale;
for (int i = 0; i < precision; i++) {
for (int i = 0; i < precision; i++)
{
prec_scale /= 10;
}
if (int_part == 0 && frac_part > 0 && frac_part / prec_scale == 0) {
if (int_part == 0 && frac_part > 0 && frac_part / prec_scale == 0)
{
// Display e.g. "< 0.01" rather than 0.
stream << "< ";
if (precision > 0) {
if (precision > 0)
{
stream << "0";
stream << decimal_point;
for (int i = 0; i < precision - 1; i++) {
for (int i = 0; i < precision - 1; i++)
{
stream << "0";
}
}
stream << "1";
} else {
}
else
{
format_dec (stream, int_part, group_digits && grouping.length () > 0 ? thousands_sep : 0, grouping);
if (precision > 0 && frac_part > 0) {
if (precision > 0 && frac_part > 0)
{
stream << decimal_point;
format_frac (stream, frac_part, scale, precision);
}
}
return stream.str();
return stream.str ();
}
std::string rai::uint128_union::format_balance(rai::uint128_t scale, int precision, bool group_digits)
std::string rai::uint128_union::format_balance (rai::uint128_t scale, int precision, bool group_digits)
{
auto thousands_sep = std::use_facet< std::numpunct<char> >(std::locale ()).thousands_sep ();
auto decimal_point = std::use_facet< std::numpunct<char> >(std::locale ()).decimal_point ();
auto thousands_sep = std::use_facet<std::numpunct<char>> (std::locale ()).thousands_sep ();
auto decimal_point = std::use_facet<std::numpunct<char>> (std::locale ()).decimal_point ();
std::string grouping = "\3";
return ::format_balance (number (), scale, precision, group_digits, thousands_sep, decimal_point, grouping);
}
std::string rai::uint128_union::format_balance (rai::uint128_t scale, int precision, bool group_digits, const std::locale & locale)
{
auto thousands_sep = std::use_facet< std::moneypunct<char> >(locale).thousands_sep ();
auto decimal_point = std::use_facet< std::moneypunct<char> >(locale).decimal_point ();
std::string grouping = std::use_facet< std::moneypunct<char> >(locale).grouping ();
auto thousands_sep = std::use_facet<std::moneypunct<char>> (locale).thousands_sep ();
auto decimal_point = std::use_facet<std::moneypunct<char>> (locale).decimal_point ();
std::string grouping = std::use_facet<std::moneypunct<char>> (locale).grouping ();
return ::format_balance (number (), scale, precision, group_digits, thousands_sep, decimal_point, grouping);
}
@ -702,7 +721,7 @@ void rai::uint128_union::clear ()
bool rai::uint128_union::is_zero () const
{
return qwords [0] == 0 && qwords [1] == 0;
return qwords[0] == 0 && qwords[1] == 0;
}
std::string rai::uint128_union::to_string () const

View file

@ -4,7 +4,8 @@
#include <cryptopp/osrng.h>
namespace rai {
namespace rai
{
// Random pool used by RaiBlocks.
// This must be thread_local as long as the AutoSeededRandomPool implementation requires it
extern thread_local CryptoPP::AutoSeededRandomPool random_pool;
@ -15,7 +16,7 @@ using uint512_t = boost::multiprecision::uint512_t;
rai::uint128_t const Gxrb_ratio = rai::uint128_t ("1000000000000000000000000000000000"); // 10^33
rai::uint128_t const Mxrb_ratio = rai::uint128_t ("1000000000000000000000000000000"); // 10^30
rai::uint128_t const kxrb_ratio = rai::uint128_t ("1000000000000000000000000000"); // 10^27
rai::uint128_t const xrb_ratio = rai::uint128_t ("1000000000000000000000000"); // 10^24
rai::uint128_t const xrb_ratio = rai::uint128_t ("1000000000000000000000000"); // 10^24
rai::uint128_t const mxrb_ratio = rai::uint128_t ("1000000000000000000000"); // 10^21
rai::uint128_t const uxrb_ratio = rai::uint128_t ("1000000000000000000"); // 10^18
@ -27,10 +28,10 @@ public:
uint128_union (uint64_t);
uint128_union (rai::uint128_union const &) = default;
uint128_union (rai::uint128_t const &);
bool operator == (rai::uint128_union const &) const;
bool operator != (rai::uint128_union const &) const;
bool operator < (rai::uint128_union const &) const;
bool operator > (rai::uint128_union const &) const;
bool operator== (rai::uint128_union const &) const;
bool operator!= (rai::uint128_union const &) const;
bool operator< (rai::uint128_union const &) const;
bool operator> (rai::uint128_union const &) const;
void encode_hex (std::string &) const;
bool decode_hex (std::string const &);
void encode_dec (std::string &) const;
@ -42,10 +43,10 @@ public:
bool is_zero () const;
std::string to_string () const;
std::string to_string_dec () const;
std::array <uint8_t, 16> bytes;
std::array <char, 16> chars;
std::array <uint32_t, 4> dwords;
std::array <uint64_t, 2> qwords;
std::array<uint8_t, 16> bytes;
std::array<char, 16> chars;
std::array<uint32_t, 4> dwords;
std::array<uint64_t, 2> qwords;
};
// Balances are 128 bit.
using amount = uint128_union;
@ -57,11 +58,11 @@ union uint256_union
uint256_union (uint64_t);
uint256_union (rai::uint256_t const &);
void encrypt (rai::raw_key const &, rai::raw_key const &, uint128_union const &);
uint256_union & operator ^= (rai::uint256_union const &);
uint256_union operator ^ (rai::uint256_union const &) const;
bool operator == (rai::uint256_union const &) const;
bool operator != (rai::uint256_union const &) const;
bool operator < (rai::uint256_union const &) const;
uint256_union & operator^= (rai::uint256_union const &);
uint256_union operator^ (rai::uint256_union const &) const;
bool operator== (rai::uint256_union const &) const;
bool operator!= (rai::uint256_union const &) const;
bool operator< (rai::uint256_union const &) const;
void encode_hex (std::string &) const;
bool decode_hex (std::string const &);
void encode_dec (std::string &) const;
@ -71,11 +72,11 @@ union uint256_union
std::string to_account_split () const;
bool decode_account_v1 (std::string const &);
bool decode_account (std::string const &);
std::array <uint8_t, 32> bytes;
std::array <char, 32> chars;
std::array <uint32_t, 8> dwords;
std::array <uint64_t, 4> qwords;
std::array <uint128_union, 2> owords;
std::array<uint8_t, 32> bytes;
std::array<char, 32> chars;
std::array<uint32_t, 8> dwords;
std::array<uint64_t, 4> qwords;
std::array<uint128_union, 2> owords;
void clear ();
bool is_zero () const;
std::string to_string () const;
@ -96,24 +97,24 @@ public:
void decrypt (rai::uint256_union const &, rai::raw_key const &, uint128_union const &);
raw_key (rai::raw_key const &) = delete;
raw_key (rai::raw_key const &&) = delete;
rai::raw_key & operator = (rai::raw_key const &) = delete;
bool operator == (rai::raw_key const &) const;
bool operator != (rai::raw_key const &) const;
rai::raw_key & operator= (rai::raw_key const &) = delete;
bool operator== (rai::raw_key const &) const;
bool operator!= (rai::raw_key const &) const;
rai::uint256_union data;
};
union uint512_union
{
uint512_union () = default;
uint512_union (rai::uint512_t const &);
bool operator == (rai::uint512_union const &) const;
bool operator != (rai::uint512_union const &) const;
rai::uint512_union & operator ^= (rai::uint512_union const &);
bool operator== (rai::uint512_union const &) const;
bool operator!= (rai::uint512_union const &) const;
rai::uint512_union & operator^= (rai::uint512_union const &);
void encode_hex (std::string &) const;
bool decode_hex (std::string const &);
std::array <uint8_t, 64> bytes;
std::array <uint32_t, 16> dwords;
std::array <uint64_t, 8> qwords;
std::array <uint256_union, 2> uint256s;
std::array<uint8_t, 64> bytes;
std::array<uint32_t, 16> dwords;
std::array<uint64_t, 8> qwords;
std::array<uint256_union, 2> uint256s;
void clear ();
rai::uint512_t number () const;
std::string to_string () const;
@ -129,19 +130,19 @@ void deterministic_key (rai::uint256_union const &, uint32_t, rai::uint256_union
namespace std
{
template <>
struct hash <rai::uint256_union>
struct hash<rai::uint256_union>
{
size_t operator () (rai::uint256_union const & data_a) const
size_t operator() (rai::uint256_union const & data_a) const
{
return *reinterpret_cast <size_t const *> (data_a.bytes.data ());
return *reinterpret_cast<size_t const *> (data_a.bytes.data ());
}
};
template <>
struct hash <rai::uint256_t>
struct hash<rai::uint256_t>
{
size_t operator () (rai::uint256_t const & number_a) const
size_t operator() (rai::uint256_t const & number_a) const
{
return number_a.convert_to <size_t> ();
return number_a.convert_to<size_t> ();
}
};
}

View file

@ -9,24 +9,24 @@ namespace rai
{
// Lower priority of calling work generating thread
void work_thread_reprioritize ();
template <typename ... T>
template <typename... T>
class observer_set
{
public:
void add (std::function <void (T...)> const & observer_a)
void add (std::function<void(T...)> const & observer_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
observers.push_back (observer_a);
}
void operator () (T ... args)
void operator() (T... args)
{
std::lock_guard <std::mutex> lock (mutex);
for (auto & i: observers)
std::lock_guard<std::mutex> lock (mutex);
for (auto & i : observers)
{
i (args...);
}
}
std::mutex mutex;
std::vector <std::function <void (T...)>> observers;
std::vector<std::function<void(T...)>> observers;
};
}

View file

@ -21,13 +21,13 @@ 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, 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));
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&result), sizeof (result));
return result;
}
rai::work_pool::work_pool (unsigned max_threads_a, std::function <boost::optional<uint64_t> (rai::uint256_union const &)> opencl_a) :
rai::work_pool::work_pool (unsigned max_threads_a, std::function<boost::optional<uint64_t> (rai::uint256_union const &)> opencl_a) :
ticket (0),
done (false),
opencl (opencl_a)
@ -36,8 +36,7 @@ opencl (opencl_a)
auto count (rai::rai_network == rai::rai_networks::rai_test_network ? 1 : std::max (1u, std::min (max_threads_a, std::thread::hardware_concurrency ())));
for (auto i (0); i < count; ++i)
{
auto thread (std::thread ([this, i] ()
{
auto thread (std::thread ([this, i]() {
rai::work_thread_reprioritize ();
loop (i);
}));
@ -48,7 +47,7 @@ opencl (opencl_a)
rai::work_pool::~work_pool ()
{
stop ();
for (auto &i: threads)
for (auto & i : threads)
{
i.join ();
}
@ -58,13 +57,13 @@ void rai::work_pool::loop (uint64_t thread)
{
// Quick RNG for work attempts.
xorshift1024star rng;
rai::random_pool.GenerateBlock (reinterpret_cast <uint8_t *> (rng.s.data ()), rng.s.size () * sizeof (decltype (rng.s)::value_type));
rai::random_pool.GenerateBlock (reinterpret_cast<uint8_t *> (rng.s.data ()), rng.s.size () * sizeof (decltype (rng.s)::value_type));
uint64_t work;
uint64_t output;
blake2b_state hash;
blake2b_init (&hash, sizeof (output));
std::unique_lock <std::mutex> lock (mutex);
while (!done || !pending.empty())
std::unique_lock<std::mutex> lock (mutex);
while (!done || !pending.empty ())
{
auto empty (pending.empty ());
if (thread == 0)
@ -88,9 +87,9 @@ void rai::work_pool::loop (uint64_t thread)
while (iteration && output < rai::work_pool::publish_threshold)
{
work = rng.next ();
blake2b_update (&hash, reinterpret_cast <uint8_t *> (&work), sizeof (work));
blake2b_update (&hash, reinterpret_cast<uint8_t *> (&work), sizeof (work));
blake2b_update (&hash, current_l.first.bytes.data (), current_l.first.bytes.size ());
blake2b_final (&hash, reinterpret_cast <uint8_t *> (&output), sizeof (output));
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&output), sizeof (output));
blake2b_init (&hash, sizeof (output));
iteration -= 1;
}
@ -121,7 +120,7 @@ void rai::work_pool::loop (uint64_t thread)
void rai::work_pool::cancel (rai::uint256_union const & root_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
if (!pending.empty ())
{
if (pending.front ().first == root_a)
@ -129,8 +128,7 @@ void rai::work_pool::cancel (rai::uint256_union const & root_a)
++ticket;
}
}
pending.remove_if ([&root_a] (decltype (pending)::value_type const & item_a)
{
pending.remove_if ([&root_a](decltype (pending)::value_type const & item_a) {
bool result;
if (item_a.first == root_a)
{
@ -147,22 +145,22 @@ void rai::work_pool::cancel (rai::uint256_union const & root_a)
void rai::work_pool::stop ()
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
done = true;
producer_condition.notify_all ();
}
void rai::work_pool::generate (rai::uint256_union const & root_a, std::function <void (boost::optional <uint64_t> const &)> callback_a)
void rai::work_pool::generate (rai::uint256_union const & root_a, std::function<void(boost::optional<uint64_t> const &)> callback_a)
{
assert (!root_a.is_zero ());
boost::optional <uint64_t> result;
boost::optional<uint64_t> result;
if (opencl)
{
result = opencl (root_a);
}
if (!result)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
pending.push_back (std::make_pair (root_a, callback_a));
producer_condition.notify_all ();
}
@ -174,9 +172,8 @@ void rai::work_pool::generate (rai::uint256_union const & root_a, std::function
uint64_t rai::work_pool::generate (rai::uint256_union const & hash_a)
{
std::promise <boost::optional <uint64_t>> work;
generate (hash_a, [&work] (boost::optional <uint64_t> work_a)
{
std::promise<boost::optional<uint64_t>> work;
generate (hash_a, [&work](boost::optional<uint64_t> work_a) {
work.set_value (work_a);
});
auto result (work.get_future ().get ());

View file

@ -6,9 +6,9 @@
#include <rai/lib/utility.hpp>
#include <atomic>
#include <condition_variable>
#include <memory>
#include <thread>
#include <condition_variable>
namespace rai
{
@ -20,25 +20,24 @@ class opencl_work;
class work_pool
{
public:
work_pool (unsigned, std::function <boost::optional <uint64_t> (rai::uint256_union const &)> = nullptr);
work_pool (unsigned, std::function<boost::optional<uint64_t> (rai::uint256_union const &)> = nullptr);
~work_pool ();
void loop (uint64_t);
void stop ();
void cancel (rai::uint256_union const &);
void generate (rai::uint256_union const &, std::function <void (boost::optional <uint64_t> const &)>);
void generate (rai::uint256_union const &, std::function<void(boost::optional<uint64_t> const &)>);
uint64_t generate (rai::uint256_union const &);
std::atomic <int> ticket;
std::atomic<int> ticket;
bool done;
std::vector <std::thread> threads;
std::list <std::pair <rai::uint256_union, std::function <void (boost::optional <uint64_t> const &)>>> pending;
std::vector<std::thread> threads;
std::list<std::pair<rai::uint256_union, std::function<void(boost::optional<uint64_t> const &)>>> pending;
std::mutex mutex;
std::condition_variable producer_condition;
std::function <boost::optional<uint64_t> (rai::uint256_union const &)> opencl;
rai::observer_set <bool> work_observers;
std::function<boost::optional<uint64_t> (rai::uint256_union const &)> opencl;
rai::observer_set<bool> work_observers;
// Local work threshold for rate-limiting publishing blocks. ~5 seconds of work.
static uint64_t const publish_test_threshold = 0xff00000000000000;
static uint64_t const publish_full_threshold = 0xffffffc000000000;
static uint64_t const publish_threshold = rai::rai_network == rai::rai_networks::rai_test_network ? publish_test_threshold : publish_full_threshold;
};
}

272
rai/node/bootstrap.cpp Executable file → Normal file
View file

@ -14,7 +14,8 @@ rai::block_synchronization::~block_synchronization ()
{
}
namespace {
namespace
{
class add_dependency_visitor : public rai::block_visitor
{
public:
@ -119,7 +120,7 @@ rai::sync_result rai::block_synchronization::synchronize (MDB_txn * transaction_
return result;
}
rai::push_synchronization::push_synchronization (rai::node & node_a, std::function <rai::sync_result (MDB_txn *, rai::block const &)> const & target_a) :
rai::push_synchronization::push_synchronization (rai::node & node_a, std::function<rai::sync_result (MDB_txn *, rai::block const &)> const & target_a) :
block_synchronization (node_a.log),
target_m (target_a),
node (node_a)
@ -136,7 +137,7 @@ bool rai::push_synchronization::synchronized (MDB_txn * transaction_a, rai::bloc
return result;
}
std::unique_ptr <rai::block> rai::push_synchronization::retrieve (MDB_txn * transaction_a, rai::block_hash const & hash_a)
std::unique_ptr<rai::block> rai::push_synchronization::retrieve (MDB_txn * transaction_a, rai::block_hash const & hash_a)
{
return node.store.block_get (transaction_a, hash_a);
}
@ -146,7 +147,7 @@ rai::sync_result rai::push_synchronization::target (MDB_txn * transaction_a, rai
return target_m (transaction_a, block_a);
}
rai::bootstrap_client::bootstrap_client (std::shared_ptr <rai::node> node_a, std::shared_ptr <rai::bootstrap_attempt> attempt_a, rai::tcp_endpoint const & endpoint_a) :
rai::bootstrap_client::bootstrap_client (std::shared_ptr<rai::node> node_a, std::shared_ptr<rai::bootstrap_attempt> attempt_a, rai::tcp_endpoint const & endpoint_a) :
node (node_a),
attempt (attempt_a),
socket (node_a->service),
@ -164,9 +165,8 @@ rai::bootstrap_client::~bootstrap_client ()
void rai::bootstrap_client::start_timeout ()
{
timeout.expires_from_now (boost::posix_time::seconds (15));
std::weak_ptr <rai::bootstrap_client> this_w (shared ());
timeout.async_wait ([this_w] (boost::system::error_code const & ec)
{
std::weak_ptr<rai::bootstrap_client> this_w (shared ());
timeout.async_wait ([this_w](boost::system::error_code const & ec) {
if (ec != boost::asio::error::operation_aborted)
{
auto this_l (this_w.lock ());
@ -185,15 +185,14 @@ void rai::bootstrap_client::start_timeout ()
void rai::bootstrap_client::stop_timeout ()
{
size_t killed (timeout.cancel ());
(void) killed;
(void)killed;
}
void rai::bootstrap_client::run ()
{
auto this_l (shared_from_this ());
start_timeout ();
socket.async_connect (endpoint, [this_l] (boost::system::error_code const & ec)
{
socket.async_connect (endpoint, [this_l](boost::system::error_code const & ec) {
this_l->stop_timeout ();
if (!ec)
{
@ -204,7 +203,7 @@ void rai::bootstrap_client::run ()
{
if (this_l->node->config.logging.network_logging ())
{
switch (ec.value())
switch (ec.value ())
{
default:
BOOST_LOG (this_l->node->log) << boost::str (boost::format ("Error initiating bootstrap connection to %2%: %1%") % ec.message () % this_l->endpoint);
@ -221,19 +220,18 @@ void rai::bootstrap_client::run ()
void rai::frontier_req_client::run ()
{
std::unique_ptr <rai::frontier_req> request (new rai::frontier_req);
std::unique_ptr<rai::frontier_req> request (new rai::frontier_req);
request->start.clear ();
request->age = std::numeric_limits <decltype (request->age)>::max ();
request->count = std::numeric_limits <decltype (request->age)>::max ();
auto send_buffer (std::make_shared <std::vector <uint8_t>> ());
request->age = std::numeric_limits<decltype (request->age)>::max ();
request->count = std::numeric_limits<decltype (request->age)>::max ();
auto send_buffer (std::make_shared<std::vector<uint8_t>> ());
{
rai::vectorstream stream (*send_buffer);
request->serialize (stream);
}
auto this_l (shared_from_this ());
connection->start_timeout ();
boost::asio::async_write (connection->socket, boost::asio::buffer (send_buffer->data (), send_buffer->size ()), [this_l, send_buffer] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_write (connection->socket, boost::asio::buffer (send_buffer->data (), send_buffer->size ()), [this_l, send_buffer](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
if (!ec)
{
@ -249,12 +247,12 @@ void rai::frontier_req_client::run ()
});
}
std::shared_ptr <rai::bootstrap_client> rai::bootstrap_client::shared ()
std::shared_ptr<rai::bootstrap_client> rai::bootstrap_client::shared ()
{
return shared_from_this ();
}
rai::frontier_req_client::frontier_req_client (std::shared_ptr <rai::bootstrap_client> connection_a) :
rai::frontier_req_client::frontier_req_client (std::shared_ptr<rai::bootstrap_client> connection_a) :
connection (connection_a),
current (0),
count (0),
@ -274,8 +272,7 @@ void rai::frontier_req_client::receive_frontier ()
{
auto this_l (shared_from_this ());
connection->start_timeout ();
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data (), sizeof (rai::uint256_union) + sizeof (rai::uint256_union)), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data (), sizeof (rai::uint256_union) + sizeof (rai::uint256_union)), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
this_l->received_frontier (ec, size_a);
});
@ -439,7 +436,7 @@ void rai::frontier_req_client::next (MDB_txn * transaction_a)
}
}
rai::bulk_pull_client::bulk_pull_client (std::shared_ptr <rai::bootstrap_client> connection_a) :
rai::bulk_pull_client::bulk_pull_client (std::shared_ptr<rai::bootstrap_client> connection_a) :
connection (connection_a)
{
assert (!connection->attempt->mutex.try_lock ());
@ -450,7 +447,7 @@ connection (connection_a)
rai::bulk_pull_client::~bulk_pull_client ()
{
{
std::lock_guard <std::mutex> mutex (connection->attempt->mutex);
std::lock_guard<std::mutex> mutex (connection->attempt->mutex);
--connection->attempt->pulling;
connection->attempt->condition.notify_all ();
}
@ -467,7 +464,7 @@ void rai::bulk_pull_client::request (rai::pull_info const & pull_a)
rai::bulk_pull req;
req.start = pull_a.account;
req.end = pull_a.end;
auto buffer (std::make_shared <std::vector <uint8_t>> ());
auto buffer (std::make_shared<std::vector<uint8_t>> ());
{
rai::vectorstream stream (*buffer);
req.serialize (stream);
@ -482,8 +479,7 @@ void rai::bulk_pull_client::request (rai::pull_info const & pull_a)
}
auto this_l (shared_from_this ());
connection->start_timeout ();
boost::asio::async_write (connection->socket, boost::asio::buffer (buffer->data (), buffer->size ()), [this_l, buffer] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_write (connection->socket, boost::asio::buffer (buffer->data (), buffer->size ()), [this_l, buffer](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
if (!ec)
{
@ -500,8 +496,7 @@ void rai::bulk_pull_client::receive_block ()
{
auto this_l (shared_from_this ());
connection->start_timeout ();
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data (), 1), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data (), 1), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
if (!ec)
{
@ -517,14 +512,13 @@ void rai::bulk_pull_client::receive_block ()
void rai::bulk_pull_client::received_type ()
{
auto this_l (shared_from_this ());
rai::block_type type (static_cast <rai::block_type> (connection->receive_buffer [0]));
rai::block_type type (static_cast<rai::block_type> (connection->receive_buffer[0]));
switch (type)
{
case rai::block_type::send:
{
connection->start_timeout ();
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::send_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::send_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
this_l->received_block (ec, size_a);
});
@ -533,8 +527,7 @@ void rai::bulk_pull_client::received_type ()
case rai::block_type::receive:
{
connection->start_timeout ();
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::receive_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::receive_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
this_l->received_block (ec, size_a);
});
@ -543,8 +536,7 @@ void rai::bulk_pull_client::received_type ()
case rai::block_type::open:
{
connection->start_timeout ();
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::open_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::open_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
this_l->received_block (ec, size_a);
});
@ -553,8 +545,7 @@ void rai::bulk_pull_client::received_type ()
case rai::block_type::change:
{
connection->start_timeout ();
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::change_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (connection->socket, boost::asio::buffer (connection->receive_buffer.data () + 1, rai::change_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
this_l->received_block (ec, size_a);
});
@ -571,7 +562,7 @@ void rai::bulk_pull_client::received_type ()
}
default:
{
BOOST_LOG (connection->node->log) << boost::str (boost::format ("Unknown type received as block type: %1%") % static_cast <int> (type));
BOOST_LOG (connection->node->log) << boost::str (boost::format ("Unknown type received as block type: %1%") % static_cast<int> (type));
break;
}
}
@ -582,7 +573,7 @@ void rai::bulk_pull_client::received_block (boost::system::error_code const & ec
if (!ec)
{
rai::bufferstream stream (connection->receive_buffer.data (), 1 + size_a);
std::shared_ptr <rai::block> block (rai::deserialize_block (stream));
std::shared_ptr<rai::block> block (rai::deserialize_block (stream));
if (block != nullptr)
{
auto hash (block->hash ());
@ -598,8 +589,7 @@ void rai::bulk_pull_client::received_block (boost::system::error_code const & ec
}
auto attempt_l (connection->attempt);
auto pull_l (pull);
attempt_l->node->block_processor.add (rai::block_processor_item (block, [attempt_l, pull_l] (MDB_txn * transaction_a, rai::process_return result_a, std::shared_ptr <rai::block> block_a)
{
attempt_l->node->block_processor.add (rai::block_processor_item (block, [attempt_l, pull_l](MDB_txn * transaction_a, rai::process_return result_a, std::shared_ptr<rai::block> block_a) {
switch (result_a.code)
{
case rai::process_result::progress:
@ -608,7 +598,7 @@ void rai::bulk_pull_client::received_block (boost::system::error_code const & ec
case rai::process_result::fork:
{
auto node_l (attempt_l->node);
std::shared_ptr <rai::block> block (node_l->ledger.forked_block (transaction_a, *block_a));
std::shared_ptr<rai::block> block (node_l->ledger.forked_block (transaction_a, *block_a));
if (!node_l->active.start (transaction_a, block))
{
node_l->network.broadcast_confirm_req (block_a);
@ -636,10 +626,9 @@ void rai::bulk_pull_client::received_block (boost::system::error_code const & ec
}
}
rai::bulk_push_client::bulk_push_client (std::shared_ptr <rai::bootstrap_client> const & connection_a) :
rai::bulk_push_client::bulk_push_client (std::shared_ptr<rai::bootstrap_client> const & connection_a) :
connection (connection_a),
synchronization (*connection->node, [this] (MDB_txn * transaction_a, rai::block const & block_a)
{
synchronization (*connection->node, [this](MDB_txn * transaction_a, rai::block const & block_a) {
push_block (block_a);
return rai::sync_result::success;
})
@ -653,15 +642,14 @@ rai::bulk_push_client::~bulk_push_client ()
void rai::bulk_push_client::start ()
{
rai::bulk_push message;
auto buffer (std::make_shared <std::vector <uint8_t>> ());
auto buffer (std::make_shared<std::vector<uint8_t>> ());
{
rai::vectorstream stream (*buffer);
message.serialize (stream);
}
auto this_l (shared_from_this ());
connection->start_timeout ();
boost::asio::async_write (connection->socket, boost::asio::buffer (buffer->data (), buffer->size ()), [this_l, buffer] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_write (connection->socket, boost::asio::buffer (buffer->data (), buffer->size ()), [this_l, buffer](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
rai::transaction transaction (this_l->connection->node->store.environment, nullptr, true);
if (!ec)
@ -707,15 +695,14 @@ void rai::bulk_push_client::push (MDB_txn * transaction_a)
void rai::bulk_push_client::send_finished ()
{
auto buffer (std::make_shared <std::vector <uint8_t>> ());
buffer->push_back (static_cast <uint8_t> (rai::block_type::not_a_block));
auto buffer (std::make_shared<std::vector<uint8_t>> ());
buffer->push_back (static_cast<uint8_t> (rai::block_type::not_a_block));
if (connection->node->config.logging.network_logging ())
{
BOOST_LOG (connection->node->log) << "Bulk push finished";
}
auto this_l (shared_from_this ());
async_write (connection->socket, boost::asio::buffer (buffer->data (), 1), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
async_write (connection->socket, boost::asio::buffer (buffer->data (), 1), [this_l](boost::system::error_code const & ec, size_t size_a) {
try
{
this_l->promise.set_value (false);
@ -728,15 +715,14 @@ void rai::bulk_push_client::send_finished ()
void rai::bulk_push_client::push_block (rai::block const & block_a)
{
auto buffer (std::make_shared <std::vector <uint8_t>> ());
auto buffer (std::make_shared<std::vector<uint8_t>> ());
{
rai::vectorstream stream (*buffer);
rai::serialize_block (stream, block_a);
}
auto this_l (shared_from_this ());
connection->start_timeout ();
boost::asio::async_write (connection->socket, boost::asio::buffer (buffer->data (), buffer->size ()), [this_l, buffer] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_write (connection->socket, boost::asio::buffer (buffer->data (), buffer->size ()), [this_l, buffer](boost::system::error_code const & ec, size_t size_a) {
this_l->connection->stop_timeout ();
if (!ec)
{
@ -772,7 +758,7 @@ attempts (0)
{
}
rai::bootstrap_attempt::bootstrap_attempt (std::shared_ptr <rai::node> node_a) :
rai::bootstrap_attempt::bootstrap_attempt (std::shared_ptr<rai::node> node_a) :
connections (0),
pulling (0),
node (node_a),
@ -789,15 +775,15 @@ rai::bootstrap_attempt::~bootstrap_attempt ()
node->bootstrap_initiator.notify_listeners (false);
}
bool rai::bootstrap_attempt::request_frontier (std::unique_lock <std::mutex> & lock_a)
bool rai::bootstrap_attempt::request_frontier (std::unique_lock<std::mutex> & lock_a)
{
auto result (true);
auto connection_l (connection (lock_a));
if (connection_l)
{
std::future <bool> future;
std::future<bool> future;
{
auto client (std::make_shared <rai::frontier_req_client> (connection_l));
auto client (std::make_shared<rai::frontier_req_client> (connection_l));
client->run ();
frontiers = client;
future = client->promise.get_future ();
@ -824,32 +810,31 @@ bool rai::bootstrap_attempt::request_frontier (std::unique_lock <std::mutex> & l
return result;
}
void rai::bootstrap_attempt::request_pull (std::unique_lock <std::mutex> & lock_a)
void rai::bootstrap_attempt::request_pull (std::unique_lock<std::mutex> & lock_a)
{
auto connection_l (connection (lock_a));
if (connection_l)
{
auto pull (pulls.front ());
pulls.pop_front ();
auto client (std::make_shared <rai::bulk_pull_client> (connection_l));
auto client (std::make_shared<rai::bulk_pull_client> (connection_l));
// The bulk_pull_client destructor attempt to requeue_pull which can cause a deadlock if this is the last reference
// Dispatch request in an external thread in case it needs to be destroyed
node->background ([client, pull] ()
{
node->background ([client, pull]() {
client->request (pull);
});
}
}
bool rai::bootstrap_attempt::request_push (std::unique_lock <std::mutex> & lock_a)
bool rai::bootstrap_attempt::request_push (std::unique_lock<std::mutex> & lock_a)
{
auto result (true);
auto connection_l (connection (lock_a));
if (connection_l)
{
std::future <bool> future;
std::future<bool> future;
{
auto client (std::make_shared <rai::bulk_push_client> (connection_l));
auto client (std::make_shared<rai::bulk_push_client> (connection_l));
client->start ();
push = client;
future = client->promise.get_future ();
@ -881,7 +866,7 @@ bool rai::bootstrap_attempt::still_pulling ()
void rai::bootstrap_attempt::run ()
{
populate_connections ();
std::unique_lock <std::mutex> lock (mutex);
std::unique_lock<std::mutex> lock (mutex);
auto frontier_failure (true);
while (!stopped && frontier_failure)
{
@ -921,13 +906,13 @@ void rai::bootstrap_attempt::run ()
idle.clear ();
}
std::shared_ptr <rai::bootstrap_client> rai::bootstrap_attempt::connection (std::unique_lock <std::mutex> & lock_a)
std::shared_ptr<rai::bootstrap_client> rai::bootstrap_attempt::connection (std::unique_lock<std::mutex> & lock_a)
{
while (!stopped && idle.empty ())
{
condition.wait (lock_a);
}
std::shared_ptr <rai::bootstrap_client> result;
std::shared_ptr<rai::bootstrap_client> result;
if (!idle.empty ())
{
result = idle.back ();
@ -936,7 +921,7 @@ std::shared_ptr <rai::bootstrap_client> rai::bootstrap_attempt::connection (std:
return result;
}
bool rai::bootstrap_attempt::consume_future (std::future <bool> & future_a)
bool rai::bootstrap_attempt::consume_future (std::future<bool> & future_a)
{
bool result;
try
@ -957,9 +942,9 @@ void rai::bootstrap_attempt::populate_connections ()
auto peer (node->peers.bootstrap_peer ());
if (peer != rai::endpoint (boost::asio::ip::address_v6::any (), 0))
{
auto client (std::make_shared <rai::bootstrap_client> (node, shared_from_this (), rai::tcp_endpoint (peer.address (), peer.port ())));
auto client (std::make_shared<rai::bootstrap_client> (node, shared_from_this (), rai::tcp_endpoint (peer.address (), peer.port ())));
client->run ();
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
clients.push_back (client);
}
else
@ -971,9 +956,8 @@ void rai::bootstrap_attempt::populate_connections ()
}
if (!stopped)
{
std::weak_ptr <rai::bootstrap_attempt> this_w (shared_from_this ());
node->alarm.add (std::chrono::system_clock::now () + std::chrono::seconds (5), [this_w] ()
{
std::weak_ptr<rai::bootstrap_attempt> this_w (shared_from_this ());
node->alarm.add (std::chrono::system_clock::now () + std::chrono::seconds (5), [this_w]() {
if (auto this_l = this_w.lock ())
{
this_l->populate_connections ();
@ -984,20 +968,20 @@ void rai::bootstrap_attempt::populate_connections ()
void rai::bootstrap_attempt::add_connection (rai::endpoint const & endpoint_a)
{
auto client (std::make_shared <rai::bootstrap_client> (node, shared_from_this (), rai::tcp_endpoint (endpoint_a.address (), endpoint_a.port ())));
auto client (std::make_shared<rai::bootstrap_client> (node, shared_from_this (), rai::tcp_endpoint (endpoint_a.address (), endpoint_a.port ())));
client->run ();
}
void rai::bootstrap_attempt::pool_connection (std::shared_ptr <rai::bootstrap_client> client_a)
void rai::bootstrap_attempt::pool_connection (std::shared_ptr<rai::bootstrap_client> client_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
idle.push_back (client_a);
condition.notify_all ();
}
void rai::bootstrap_attempt::stop ()
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
stopped = true;
condition.notify_all ();
for (auto i : clients)
@ -1034,7 +1018,7 @@ void rai::bootstrap_attempt::requeue_pull (rai::pull_info const & pull_a)
auto pull (pull_a);
if (++pull.attempts < 4)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
pulls.push_front (pull);
condition.notify_all ();
}
@ -1060,15 +1044,14 @@ rai::bootstrap_initiator::~bootstrap_initiator ()
void rai::bootstrap_initiator::bootstrap ()
{
std::unique_lock <std::mutex> lock (mutex);
std::unique_lock<std::mutex> lock (mutex);
if (!stopped && attempt == nullptr)
{
stop_attempt (lock);
attempt = std::make_shared <rai::bootstrap_attempt> (node.shared ());
attempt_thread.reset (new std::thread ([this] ()
{
attempt = std::make_shared<rai::bootstrap_attempt> (node.shared ());
attempt_thread.reset (new std::thread ([this]() {
attempt->run ();
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
attempt.reset ();
condition.notify_all ();
}));
@ -1079,33 +1062,33 @@ void rai::bootstrap_initiator::bootstrap (rai::endpoint const & endpoint_a)
{
node.peers.insert (endpoint_a, 0x5);
bootstrap ();
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
if (attempt != nullptr)
{
attempt->add_connection (endpoint_a);
}
}
void rai::bootstrap_initiator::add_observer (std::function <void (bool)> const & observer_a)
void rai::bootstrap_initiator::add_observer (std::function<void(bool)> const & observer_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
observers.push_back (observer_a);
}
bool rai::bootstrap_initiator::in_progress ()
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
return attempt != nullptr;
}
void rai::bootstrap_initiator::stop ()
{
std::unique_lock <std::mutex> lock (mutex);
std::unique_lock<std::mutex> lock (mutex);
stopped = true;
stop_attempt (lock);
}
void rai::bootstrap_initiator::stop_attempt (std::unique_lock <std::mutex> & lock_a)
void rai::bootstrap_initiator::stop_attempt (std::unique_lock<std::mutex> & lock_a)
{
assert (!mutex.try_lock ());
if (attempt != nullptr)
@ -1125,7 +1108,7 @@ void rai::bootstrap_initiator::stop_attempt (std::unique_lock <std::mutex> & loc
void rai::bootstrap_initiator::notify_listeners (bool in_progress_a)
{
for (auto & i: observers)
for (auto & i : observers)
{
i (in_progress_a);
}
@ -1148,8 +1131,8 @@ void rai::bootstrap_listener::start ()
acceptor.bind (local, ec);
if (ec)
{
BOOST_LOG (node.log) << boost::str (boost::format ("Error while binding for bootstrap on port %1%: %2%") % local.port() % ec.message ());
throw std::runtime_error (ec.message());
BOOST_LOG (node.log) << boost::str (boost::format ("Error while binding for bootstrap on port %1%: %2%") % local.port () % ec.message ());
throw std::runtime_error (ec.message ());
}
acceptor.listen ();
@ -1159,7 +1142,7 @@ void rai::bootstrap_listener::start ()
void rai::bootstrap_listener::stop ()
{
on = false;
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
acceptor.close ();
for (auto & i : connections)
{
@ -1173,24 +1156,23 @@ void rai::bootstrap_listener::stop ()
void rai::bootstrap_listener::accept_connection ()
{
auto socket (std::make_shared <boost::asio::ip::tcp::socket> (service));
acceptor.async_accept (*socket, [this, socket] (boost::system::error_code const & ec)
{
auto socket (std::make_shared<boost::asio::ip::tcp::socket> (service));
acceptor.async_accept (*socket, [this, socket](boost::system::error_code const & ec) {
accept_action (ec, socket);
});
}
void rai::bootstrap_listener::accept_action (boost::system::error_code const & ec, std::shared_ptr <boost::asio::ip::tcp::socket> socket_a)
void rai::bootstrap_listener::accept_action (boost::system::error_code const & ec, std::shared_ptr<boost::asio::ip::tcp::socket> socket_a)
{
if (!ec)
{
accept_connection ();
auto connection (std::make_shared <rai::bootstrap_server> (socket_a, node.shared ()));
auto connection (std::make_shared<rai::bootstrap_server> (socket_a, node.shared ()));
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
if (acceptor.is_open ())
{
connections [connection.get ()] = connection;
connections[connection.get ()] = connection;
connection->receive ();
}
}
@ -1212,11 +1194,11 @@ rai::bootstrap_server::~bootstrap_server ()
{
BOOST_LOG (node->log) << "Exiting bootstrap server";
}
std::lock_guard <std::mutex> lock (node->bootstrap.mutex);
std::lock_guard<std::mutex> lock (node->bootstrap.mutex);
node->bootstrap.connections.erase (this);
}
rai::bootstrap_server::bootstrap_server (std::shared_ptr <boost::asio::ip::tcp::socket> socket_a, std::shared_ptr <rai::node> node_a) :
rai::bootstrap_server::bootstrap_server (std::shared_ptr<boost::asio::ip::tcp::socket> socket_a, std::shared_ptr<rai::node> node_a) :
socket (socket_a),
node (node_a)
{
@ -1225,8 +1207,7 @@ node (node_a)
void rai::bootstrap_server::receive ()
{
auto this_l (shared_from_this ());
boost::asio::async_read (*socket, boost::asio::buffer (receive_buffer.data (), 8), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*socket, boost::asio::buffer (receive_buffer.data (), 8), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->receive_header_action (ec, size_a);
});
}
@ -1241,7 +1222,7 @@ void rai::bootstrap_server::receive_header_action (boost::system::error_code con
uint8_t version_using;
uint8_t version_min;
rai::message_type type;
std::bitset <16> extensions;
std::bitset<16> extensions;
if (!rai::message::read_header (type_stream, version_max, version_using, version_min, type, extensions))
{
switch (type)
@ -1249,8 +1230,7 @@ void rai::bootstrap_server::receive_header_action (boost::system::error_code con
case rai::message_type::bulk_pull:
{
auto this_l (shared_from_this ());
boost::asio::async_read (*socket, boost::asio::buffer (receive_buffer.data () + 8, sizeof (rai::uint256_union) + sizeof (rai::uint256_union)), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*socket, boost::asio::buffer (receive_buffer.data () + 8, sizeof (rai::uint256_union) + sizeof (rai::uint256_union)), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->receive_bulk_pull_action (ec, size_a);
});
break;
@ -1258,22 +1238,21 @@ void rai::bootstrap_server::receive_header_action (boost::system::error_code con
case rai::message_type::frontier_req:
{
auto this_l (shared_from_this ());
boost::asio::async_read (*socket, boost::asio::buffer (receive_buffer.data () + 8, sizeof (rai::uint256_union) + sizeof (uint32_t) + sizeof (uint32_t)), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*socket, boost::asio::buffer (receive_buffer.data () + 8, sizeof (rai::uint256_union) + sizeof (uint32_t) + sizeof (uint32_t)), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->receive_frontier_req_action (ec, size_a);
});
break;
}
case rai::message_type::bulk_push:
{
add_request (std::unique_ptr <rai::message> (new rai::bulk_push));
add_request (std::unique_ptr<rai::message> (new rai::bulk_push));
break;
}
default:
{
if (node->config.logging.network_logging ())
{
BOOST_LOG (node->log) << boost::str (boost::format ("Received invalid type from bootstrap connection %1%") % static_cast <uint8_t> (type));
BOOST_LOG (node->log) << boost::str (boost::format ("Received invalid type from bootstrap connection %1%") % static_cast<uint8_t> (type));
}
break;
}
@ -1293,7 +1272,7 @@ void rai::bootstrap_server::receive_bulk_pull_action (boost::system::error_code
{
if (!ec)
{
std::unique_ptr <rai::bulk_pull> request (new rai::bulk_pull);
std::unique_ptr<rai::bulk_pull> request (new rai::bulk_pull);
rai::bufferstream stream (receive_buffer.data (), 8 + sizeof (rai::uint256_union) + sizeof (rai::uint256_union));
auto error (request->deserialize (stream));
if (!error)
@ -1302,7 +1281,7 @@ void rai::bootstrap_server::receive_bulk_pull_action (boost::system::error_code
{
BOOST_LOG (node->log) << boost::str (boost::format ("Received bulk pull for %1% down to %2%") % request->start.to_string () % request->end.to_string ());
}
add_request (std::unique_ptr <rai::message> (request.release ()));
add_request (std::unique_ptr<rai::message> (request.release ()));
receive ();
}
}
@ -1312,7 +1291,7 @@ void rai::bootstrap_server::receive_frontier_req_action (boost::system::error_co
{
if (!ec)
{
std::unique_ptr <rai::frontier_req> request (new rai::frontier_req);
std::unique_ptr<rai::frontier_req> request (new rai::frontier_req);
rai::bufferstream stream (receive_buffer.data (), 8 + sizeof (rai::uint256_union) + sizeof (uint32_t) + sizeof (uint32_t));
auto error (request->deserialize (stream));
if (!error)
@ -1321,7 +1300,7 @@ void rai::bootstrap_server::receive_frontier_req_action (boost::system::error_co
{
BOOST_LOG (node->log) << boost::str (boost::format ("Received frontier request for %1% with age %2%") % request->start.to_string () % request->age);
}
add_request (std::unique_ptr <rai::message> (request.release ()));
add_request (std::unique_ptr<rai::message> (request.release ()));
receive ();
}
}
@ -1334,9 +1313,9 @@ void rai::bootstrap_server::receive_frontier_req_action (boost::system::error_co
}
}
void rai::bootstrap_server::add_request (std::unique_ptr <rai::message> message_a)
void rai::bootstrap_server::add_request (std::unique_ptr<rai::message> message_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
auto start (requests.empty ());
requests.push (std::move (message_a));
if (start)
@ -1347,7 +1326,7 @@ void rai::bootstrap_server::add_request (std::unique_ptr <rai::message> message_
void rai::bootstrap_server::finish_request ()
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
requests.pop ();
if (!requests.empty ())
{
@ -1360,7 +1339,7 @@ namespace
class request_response_visitor : public rai::message_visitor
{
public:
request_response_visitor (std::shared_ptr <rai::bootstrap_server> connection_a) :
request_response_visitor (std::shared_ptr<rai::bootstrap_server> connection_a) :
connection (connection_a)
{
}
@ -1382,20 +1361,20 @@ public:
}
void bulk_pull (rai::bulk_pull const &) override
{
auto response (std::make_shared <rai::bulk_pull_server> (connection, std::unique_ptr <rai::bulk_pull> (static_cast <rai::bulk_pull *> (connection->requests.front ().release ()))));
auto response (std::make_shared<rai::bulk_pull_server> (connection, std::unique_ptr<rai::bulk_pull> (static_cast<rai::bulk_pull *> (connection->requests.front ().release ()))));
response->send_next ();
}
void bulk_push (rai::bulk_push const &) override
{
auto response (std::make_shared <rai::bulk_push_server> (connection));
auto response (std::make_shared<rai::bulk_push_server> (connection));
response->receive ();
}
void frontier_req (rai::frontier_req const &) override
{
auto response (std::make_shared <rai::frontier_req_server> (connection, std::unique_ptr <rai::frontier_req> (static_cast <rai::frontier_req *> (connection->requests.front ().release ()))));
auto response (std::make_shared<rai::frontier_req_server> (connection, std::unique_ptr<rai::frontier_req> (static_cast<rai::frontier_req *> (connection->requests.front ().release ()))));
response->send_next ();
}
std::shared_ptr <rai::bootstrap_server> connection;
std::shared_ptr<rai::bootstrap_server> connection;
};
}
@ -1451,7 +1430,7 @@ void rai::bulk_pull_server::set_current_end ()
void rai::bulk_pull_server::send_next ()
{
std::unique_ptr <rai::block> block (get_next ());
std::unique_ptr<rai::block> block (get_next ());
if (block != nullptr)
{
{
@ -1464,8 +1443,7 @@ void rai::bulk_pull_server::send_next ()
{
BOOST_LOG (connection->node->log) << boost::str (boost::format ("Sending block: %1%") % block->hash ().to_string ());
}
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), send_buffer.size ()), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), send_buffer.size ()), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->sent_action (ec, size_a);
});
}
@ -1475,9 +1453,9 @@ void rai::bulk_pull_server::send_next ()
}
}
std::unique_ptr <rai::block> rai::bulk_pull_server::get_next ()
std::unique_ptr<rai::block> rai::bulk_pull_server::get_next ()
{
std::unique_ptr <rai::block> result;
std::unique_ptr<rai::block> result;
if (current != request->end)
{
rai::transaction transaction (connection->node->store.environment, nullptr, false);
@ -1517,14 +1495,13 @@ void rai::bulk_pull_server::sent_action (boost::system::error_code const & ec, s
void rai::bulk_pull_server::send_finished ()
{
send_buffer.clear ();
send_buffer.push_back (static_cast <uint8_t> (rai::block_type::not_a_block));
send_buffer.push_back (static_cast<uint8_t> (rai::block_type::not_a_block));
auto this_l (shared_from_this ());
if (connection->node->config.logging.bulk_pull_logging ())
{
BOOST_LOG (connection->node->log) << "Bulk sending finished";
}
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), 1), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), 1), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->no_block_sent (ec, size_a);
});
}
@ -1542,14 +1519,14 @@ void rai::bulk_pull_server::no_block_sent (boost::system::error_code const & ec,
}
}
rai::bulk_pull_server::bulk_pull_server (std::shared_ptr <rai::bootstrap_server> const & connection_a, std::unique_ptr <rai::bulk_pull> request_a) :
rai::bulk_pull_server::bulk_pull_server (std::shared_ptr<rai::bootstrap_server> const & connection_a, std::unique_ptr<rai::bulk_pull> request_a) :
connection (connection_a),
request (std::move (request_a))
{
set_current_end ();
}
rai::bulk_push_server::bulk_push_server (std::shared_ptr <rai::bootstrap_server> const & connection_a) :
rai::bulk_push_server::bulk_push_server (std::shared_ptr<rai::bootstrap_server> const & connection_a) :
connection (connection_a)
{
}
@ -1557,8 +1534,7 @@ connection (connection_a)
void rai::bulk_push_server::receive ()
{
auto this_l (shared_from_this ());
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data (), 1), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data (), 1), [this_l](boost::system::error_code const & ec, size_t size_a) {
if (!ec)
{
this_l->received_type ();
@ -1573,37 +1549,33 @@ void rai::bulk_push_server::receive ()
void rai::bulk_push_server::received_type ()
{
auto this_l (shared_from_this ());
rai::block_type type (static_cast <rai::block_type> (receive_buffer [0]));
rai::block_type type (static_cast<rai::block_type> (receive_buffer[0]));
switch (type)
{
case rai::block_type::send:
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::send_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::send_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->received_block (ec, size_a);
});
break;
}
case rai::block_type::receive:
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::receive_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::receive_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->received_block (ec, size_a);
});
break;
}
case rai::block_type::open:
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::open_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::open_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->received_block (ec, size_a);
});
break;
}
case rai::block_type::change:
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::change_block::size), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
boost::asio::async_read (*connection->socket, boost::asio::buffer (receive_buffer.data () + 1, rai::change_block::size), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->received_block (ec, size_a);
});
break;
@ -1642,7 +1614,7 @@ void rai::bulk_push_server::received_block (boost::system::error_code const & ec
}
}
rai::frontier_req_server::frontier_req_server (std::shared_ptr <rai::bootstrap_server> const & connection_a, std::unique_ptr <rai::frontier_req> request_a) :
rai::frontier_req_server::frontier_req_server (std::shared_ptr<rai::bootstrap_server> const & connection_a, std::unique_ptr<rai::frontier_req> request_a) :
connection (connection_a),
current (request_a->start.number () - 1),
info (0, 0, 0, 0, 0, 0),
@ -1680,8 +1652,7 @@ void rai::frontier_req_server::send_next ()
BOOST_LOG (connection->node->log) << boost::str (boost::format ("Sending frontier for %1% %2%") % current.to_account () % info.head.to_string ());
}
next ();
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), send_buffer.size ()), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), send_buffer.size ()), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->sent_action (ec, size_a);
});
}
@ -1705,8 +1676,7 @@ void rai::frontier_req_server::send_finished ()
{
BOOST_LOG (connection->node->log) << "Frontier sending finished";
}
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), send_buffer.size ()), [this_l] (boost::system::error_code const & ec, size_t size_a)
{
async_write (*connection->socket, boost::asio::buffer (send_buffer.data (), send_buffer.size ()), [this_l](boost::system::error_code const & ec, size_t size_a) {
this_l->no_block_sent (ec, size_a);
});
}

View file

@ -1,13 +1,13 @@
#pragma once
#include <rai/secure.hpp>
#include <rai/node/common.hpp>
#include <rai/secure.hpp>
#include <atomic>
#include <future>
#include <queue>
#include <unordered_set>
#include <stack>
#include <unordered_set>
#include <boost/log/sources/logger.hpp>
@ -28,7 +28,7 @@ public:
~block_synchronization ();
// Return true if target already has block
virtual bool synchronized (MDB_txn *, rai::block_hash const &) = 0;
virtual std::unique_ptr <rai::block> retrieve (MDB_txn *, rai::block_hash const &) = 0;
virtual std::unique_ptr<rai::block> retrieve (MDB_txn *, rai::block_hash const &) = 0;
virtual rai::sync_result target (MDB_txn *, rai::block const &) = 0;
// return true if all dependencies are synchronized
bool add_dependency (MDB_txn *, rai::block const &);
@ -36,16 +36,16 @@ public:
rai::sync_result synchronize_one (MDB_txn *);
rai::sync_result synchronize (MDB_txn *, rai::block_hash const &);
boost::log::sources::logger_mt & log;
std::deque <rai::block_hash> blocks;
std::deque<rai::block_hash> blocks;
};
class push_synchronization : public rai::block_synchronization
{
public:
push_synchronization (rai::node &, std::function <rai::sync_result (MDB_txn *, rai::block const &)> const &);
push_synchronization (rai::node &, std::function<rai::sync_result (MDB_txn *, rai::block const &)> const &);
bool synchronized (MDB_txn *, rai::block_hash const &) override;
std::unique_ptr <rai::block> retrieve (MDB_txn *, rai::block_hash const &) override;
std::unique_ptr<rai::block> retrieve (MDB_txn *, rai::block_hash const &) override;
rai::sync_result target (MDB_txn *, rai::block const &) override;
std::function <rai::sync_result (MDB_txn *, rai::block const &)> target_m;
std::function<rai::sync_result (MDB_txn *, rai::block const &)> target_m;
rai::node & node;
};
class bootstrap_client;
@ -61,40 +61,40 @@ public:
};
class frontier_req_client;
class bulk_push_client;
class bootstrap_attempt : public std::enable_shared_from_this <bootstrap_attempt>
class bootstrap_attempt : public std::enable_shared_from_this<bootstrap_attempt>
{
public:
bootstrap_attempt (std::shared_ptr <rai::node> node_a);
bootstrap_attempt (std::shared_ptr<rai::node> node_a);
~bootstrap_attempt ();
void run ();
std::shared_ptr <rai::bootstrap_client> connection (std::unique_lock <std::mutex> &);
bool consume_future (std::future <bool> &);
std::shared_ptr<rai::bootstrap_client> connection (std::unique_lock<std::mutex> &);
bool consume_future (std::future<bool> &);
void populate_connections ();
bool request_frontier (std::unique_lock <std::mutex> &);
void request_pull (std::unique_lock <std::mutex> &);
bool request_push (std::unique_lock <std::mutex> &);
bool request_frontier (std::unique_lock<std::mutex> &);
void request_pull (std::unique_lock<std::mutex> &);
bool request_push (std::unique_lock<std::mutex> &);
void add_connection (rai::endpoint const &);
void pool_connection (std::shared_ptr <rai::bootstrap_client>);
void pool_connection (std::shared_ptr<rai::bootstrap_client>);
void stop ();
void requeue_pull (rai::pull_info const &);
bool still_pulling ();
std::deque <std::weak_ptr <rai::bootstrap_client>> clients;
std::weak_ptr <rai::frontier_req_client> frontiers;
std::weak_ptr <rai::bulk_push_client> push;
std::deque <rai::pull_info> pulls;
std::vector <std::shared_ptr <rai::bootstrap_client>> idle;
std::atomic <unsigned> connections;
std::atomic <unsigned> pulling;
std::shared_ptr <rai::node> node;
std::atomic <unsigned> account_count;
std::deque<std::weak_ptr<rai::bootstrap_client>> clients;
std::weak_ptr<rai::frontier_req_client> frontiers;
std::weak_ptr<rai::bulk_push_client> push;
std::deque<rai::pull_info> pulls;
std::vector<std::shared_ptr<rai::bootstrap_client>> idle;
std::atomic<unsigned> connections;
std::atomic<unsigned> pulling;
std::shared_ptr<rai::node> node;
std::atomic<unsigned> account_count;
bool stopped;
std::mutex mutex;
std::condition_variable condition;
};
class frontier_req_client : public std::enable_shared_from_this <rai::frontier_req_client>
class frontier_req_client : public std::enable_shared_from_this<rai::frontier_req_client>
{
public:
frontier_req_client (std::shared_ptr <rai::bootstrap_client>);
frontier_req_client (std::shared_ptr<rai::bootstrap_client>);
~frontier_req_client ();
void run ();
void receive_frontier ();
@ -103,57 +103,57 @@ public:
void unsynced (MDB_txn *, rai::account const &, rai::block_hash const &);
void next (MDB_txn *);
void insert_pull (rai::pull_info const &);
std::shared_ptr <rai::bootstrap_client> connection;
std::shared_ptr<rai::bootstrap_client> connection;
rai::account current;
rai::account_info info;
unsigned count;
rai::account landing;
rai::account faucet;
std::chrono::system_clock::time_point next_report;
std::promise <bool> promise;
std::promise<bool> promise;
};
class bulk_pull_client : public std::enable_shared_from_this <rai::bulk_pull_client>
class bulk_pull_client : public std::enable_shared_from_this<rai::bulk_pull_client>
{
public:
bulk_pull_client (std::shared_ptr <rai::bootstrap_client>);
bulk_pull_client (std::shared_ptr<rai::bootstrap_client>);
~bulk_pull_client ();
void request (rai::pull_info const &);
void receive_block ();
void received_type ();
void received_block (boost::system::error_code const &, size_t);
rai::block_hash first ();
std::shared_ptr <rai::bootstrap_client> connection;
std::shared_ptr<rai::bootstrap_client> connection;
rai::block_hash expected;
rai::pull_info pull;
};
class bootstrap_client : public std::enable_shared_from_this <bootstrap_client>
class bootstrap_client : public std::enable_shared_from_this<bootstrap_client>
{
public:
bootstrap_client (std::shared_ptr <rai::node>, std::shared_ptr <rai::bootstrap_attempt>, rai::tcp_endpoint const &);
bootstrap_client (std::shared_ptr<rai::node>, std::shared_ptr<rai::bootstrap_attempt>, rai::tcp_endpoint const &);
~bootstrap_client ();
void run ();
std::shared_ptr <rai::bootstrap_client> shared ();
std::shared_ptr<rai::bootstrap_client> shared ();
void start_timeout ();
void stop_timeout ();
std::shared_ptr <rai::node> node;
std::shared_ptr <rai::bootstrap_attempt> attempt;
std::shared_ptr<rai::node> node;
std::shared_ptr<rai::bootstrap_attempt> attempt;
boost::asio::ip::tcp::socket socket;
std::array <uint8_t, 200> receive_buffer;
std::array<uint8_t, 200> receive_buffer;
rai::tcp_endpoint endpoint;
boost::asio::deadline_timer timeout;
};
class bulk_push_client : public std::enable_shared_from_this <rai::bulk_push_client>
class bulk_push_client : public std::enable_shared_from_this<rai::bulk_push_client>
{
public:
bulk_push_client (std::shared_ptr <rai::bootstrap_client> const &);
bulk_push_client (std::shared_ptr<rai::bootstrap_client> const &);
~bulk_push_client ();
void start ();
void push (MDB_txn *);
void push_block (rai::block const &);
void send_finished ();
std::shared_ptr <rai::bootstrap_client> connection;
std::shared_ptr<rai::bootstrap_client> connection;
rai::push_synchronization synchronization;
std::promise <bool> promise;
std::promise<bool> promise;
};
class bootstrap_initiator
{
@ -163,18 +163,19 @@ public:
void bootstrap (rai::endpoint const &);
void bootstrap ();
void notify_listeners (bool);
void add_observer (std::function <void (bool)> const &);
void add_observer (std::function<void(bool)> const &);
bool in_progress ();
void stop ();
rai::node & node;
std::shared_ptr <rai::bootstrap_attempt> attempt;
std::unique_ptr <std::thread> attempt_thread;
std::shared_ptr<rai::bootstrap_attempt> attempt;
std::unique_ptr<std::thread> attempt_thread;
bool stopped;
private:
void stop_attempt (std::unique_lock <std::mutex> &);
void stop_attempt (std::unique_lock<std::mutex> &);
std::mutex mutex;
std::condition_variable condition;
std::vector <std::function <void (bool)>> observers;
std::vector<std::function<void(bool)>> observers;
};
class bootstrap_server;
class bootstrap_listener
@ -184,9 +185,9 @@ public:
void start ();
void stop ();
void accept_connection ();
void accept_action (boost::system::error_code const &, std::shared_ptr <boost::asio::ip::tcp::socket>);
void accept_action (boost::system::error_code const &, std::shared_ptr<boost::asio::ip::tcp::socket>);
std::mutex mutex;
std::unordered_map <rai::bootstrap_server *, std::weak_ptr <rai::bootstrap_server>> connections;
std::unordered_map<rai::bootstrap_server *, std::weak_ptr<rai::bootstrap_server>> connections;
rai::tcp_endpoint endpoint ();
boost::asio::ip::tcp::acceptor acceptor;
rai::tcp_endpoint local;
@ -195,68 +196,68 @@ public:
bool on;
};
class message;
class bootstrap_server : public std::enable_shared_from_this <rai::bootstrap_server>
class bootstrap_server : public std::enable_shared_from_this<rai::bootstrap_server>
{
public:
bootstrap_server (std::shared_ptr <boost::asio::ip::tcp::socket>, std::shared_ptr <rai::node>);
bootstrap_server (std::shared_ptr<boost::asio::ip::tcp::socket>, std::shared_ptr<rai::node>);
~bootstrap_server ();
void receive ();
void receive_header_action (boost::system::error_code const &, size_t);
void receive_bulk_pull_action (boost::system::error_code const &, size_t);
void receive_frontier_req_action (boost::system::error_code const &, size_t);
void receive_bulk_push_action ();
void add_request (std::unique_ptr <rai::message>);
void add_request (std::unique_ptr<rai::message>);
void finish_request ();
void run_next ();
std::array <uint8_t, 128> receive_buffer;
std::shared_ptr <boost::asio::ip::tcp::socket> socket;
std::shared_ptr <rai::node> node;
std::array<uint8_t, 128> receive_buffer;
std::shared_ptr<boost::asio::ip::tcp::socket> socket;
std::shared_ptr<rai::node> node;
std::mutex mutex;
std::queue <std::unique_ptr <rai::message>> requests;
std::queue<std::unique_ptr<rai::message>> requests;
};
class bulk_pull;
class bulk_pull_server : public std::enable_shared_from_this <rai::bulk_pull_server>
class bulk_pull_server : public std::enable_shared_from_this<rai::bulk_pull_server>
{
public:
bulk_pull_server (std::shared_ptr <rai::bootstrap_server> const &, std::unique_ptr <rai::bulk_pull>);
bulk_pull_server (std::shared_ptr<rai::bootstrap_server> const &, std::unique_ptr<rai::bulk_pull>);
void set_current_end ();
std::unique_ptr <rai::block> get_next ();
std::unique_ptr<rai::block> get_next ();
void send_next ();
void sent_action (boost::system::error_code const &, size_t);
void send_finished ();
void no_block_sent (boost::system::error_code const &, size_t);
std::shared_ptr <rai::bootstrap_server> connection;
std::unique_ptr <rai::bulk_pull> request;
std::vector <uint8_t> send_buffer;
std::shared_ptr<rai::bootstrap_server> connection;
std::unique_ptr<rai::bulk_pull> request;
std::vector<uint8_t> send_buffer;
rai::block_hash current;
};
class bulk_push_server : public std::enable_shared_from_this <rai::bulk_push_server>
class bulk_push_server : public std::enable_shared_from_this<rai::bulk_push_server>
{
public:
bulk_push_server (std::shared_ptr <rai::bootstrap_server> const &);
bulk_push_server (std::shared_ptr<rai::bootstrap_server> const &);
void receive ();
void receive_block ();
void received_type ();
void received_block (boost::system::error_code const &, size_t);
std::array <uint8_t, 256> receive_buffer;
std::shared_ptr <rai::bootstrap_server> connection;
std::array<uint8_t, 256> receive_buffer;
std::shared_ptr<rai::bootstrap_server> connection;
};
class frontier_req;
class frontier_req_server : public std::enable_shared_from_this <rai::frontier_req_server>
class frontier_req_server : public std::enable_shared_from_this<rai::frontier_req_server>
{
public:
frontier_req_server (std::shared_ptr <rai::bootstrap_server> const &, std::unique_ptr <rai::frontier_req>);
frontier_req_server (std::shared_ptr<rai::bootstrap_server> const &, std::unique_ptr<rai::frontier_req>);
void skip_old ();
void send_next ();
void sent_action (boost::system::error_code const &, size_t);
void send_finished ();
void no_block_sent (boost::system::error_code const &, size_t);
void next ();
std::shared_ptr <rai::bootstrap_server> connection;
std::shared_ptr<rai::bootstrap_server> connection;
rai::account current;
rai::account_info info;
std::unique_ptr <rai::frontier_req> request;
std::vector <uint8_t> send_buffer;
std::unique_ptr<rai::frontier_req> request;
std::vector<uint8_t> send_buffer;
size_t count;
};
}

View file

@ -1,12 +1,12 @@
#include <rai/node/common.hpp>
#include <rai/node/wallet.hpp>
#include <rai/lib/work.hpp>
#include <rai/node/wallet.hpp>
std::array <uint8_t, 2> constexpr rai::message::magic_number;
std::array<uint8_t, 2> constexpr rai::message::magic_number;
size_t constexpr rai::message::ipv4_only_position;
size_t constexpr rai::message::bootstrap_server_position;
std::bitset <16> constexpr rai::message::block_type_mask;
std::bitset<16> constexpr rai::message::block_type_mask;
rai::message::message (rai::message_type type_a) :
version_max (0x05),
@ -23,13 +23,13 @@ rai::message::message (bool & error_a, rai::stream & stream_a)
rai::block_type rai::message::block_type () const
{
return static_cast <rai::block_type> (((extensions & block_type_mask) >> 8).to_ullong ());
return static_cast<rai::block_type> (((extensions & block_type_mask) >> 8).to_ullong ());
}
void rai::message::block_type_set (rai::block_type type_a)
{
extensions &= ~rai::message::block_type_mask;
extensions |= std::bitset <16> (static_cast <unsigned long long> (type_a) << 8);
extensions |= std::bitset<16> (static_cast<unsigned long long> (type_a) << 8);
}
bool rai::message::ipv4_only ()
@ -49,12 +49,12 @@ void rai::message::write_header (rai::stream & stream_a)
rai::write (stream_a, version_using);
rai::write (stream_a, version_min);
rai::write (stream_a, type);
rai::write (stream_a, static_cast <uint16_t> (extensions.to_ullong ()));
rai::write (stream_a, static_cast<uint16_t> (extensions.to_ullong ()));
}
bool rai::message::read_header (rai::stream & stream_a, uint8_t & version_max_a, uint8_t & version_using_a, uint8_t & version_min_a, rai::message_type & type_a, std::bitset <16> & extensions_a)
bool rai::message::read_header (rai::stream & stream_a, uint8_t & version_max_a, uint8_t & version_using_a, uint8_t & version_min_a, rai::message_type & type_a, std::bitset<16> & extensions_a)
{
std::array <uint8_t, 2> magic_number_l;
std::array<uint8_t, 2> magic_number_l;
auto result (rai::read (stream_a, magic_number_l));
if (!result)
{
@ -104,7 +104,7 @@ void rai::message_parser::deserialize_buffer (uint8_t const * buffer_a, size_t s
uint8_t version_using;
uint8_t version_min;
rai::message_type type;
std::bitset <16> extensions;
std::bitset<16> extensions;
if (!rai::message::read_header (header_stream, version_max, version_using, version_min, type, extensions))
{
switch (type)
@ -233,7 +233,7 @@ bool rai::message_parser::at_end (rai::bufferstream & stream_a)
rai::keepalive::keepalive () :
message (rai::message_type::keepalive)
{
rai::endpoint endpoint (boost::asio::ip::address_v6 {}, 0);
rai::endpoint endpoint (boost::asio::ip::address_v6{}, 0);
for (auto i (peers.begin ()), n (peers.end ()); i != n; ++i)
{
*i = endpoint;
@ -264,7 +264,7 @@ bool rai::keepalive::deserialize (rai::stream & stream_a)
assert (type == rai::message_type::keepalive);
for (auto i (peers.begin ()), j (peers.end ()); i != j; ++i)
{
std::array <uint8_t, 16> address;
std::array<uint8_t, 16> address;
uint16_t port;
read (stream_a, address);
read (stream_a, port);
@ -273,7 +273,7 @@ bool rai::keepalive::deserialize (rai::stream & stream_a)
return result;
}
bool rai::keepalive::operator == (rai::keepalive const & other_a) const
bool rai::keepalive::operator== (rai::keepalive const & other_a) const
{
return peers == other_a.peers;
}
@ -283,7 +283,7 @@ message (rai::message_type::publish)
{
}
rai::publish::publish (std::shared_ptr <rai::block> block_a) :
rai::publish::publish (std::shared_ptr<rai::block> block_a) :
message (rai::message_type::publish),
block (block_a)
{
@ -315,7 +315,7 @@ void rai::publish::visit (rai::message_visitor & visitor_a) const
visitor_a.publish (*this);
}
bool rai::publish::operator == (rai::publish const & other_a) const
bool rai::publish::operator== (rai::publish const & other_a) const
{
return *block == *other_a.block;
}
@ -325,7 +325,7 @@ message (rai::message_type::confirm_req)
{
}
rai::confirm_req::confirm_req (std::shared_ptr <rai::block> block_a) :
rai::confirm_req::confirm_req (std::shared_ptr<rai::block> block_a) :
message (rai::message_type::confirm_req),
block (block_a)
{
@ -357,18 +357,18 @@ void rai::confirm_req::serialize (rai::stream & stream_a)
block->serialize (stream_a);
}
bool rai::confirm_req::operator == (rai::confirm_req const & other_a) const
bool rai::confirm_req::operator== (rai::confirm_req const & other_a) const
{
return *block == *other_a.block;
}
rai::confirm_ack::confirm_ack (bool & error_a, rai::stream & stream_a) :
message (error_a, stream_a),
vote (std::make_shared <rai::vote> (error_a, stream_a, block_type ()))
vote (std::make_shared<rai::vote> (error_a, stream_a, block_type ()))
{
}
rai::confirm_ack::confirm_ack (std::shared_ptr <rai::vote> vote_a) :
rai::confirm_ack::confirm_ack (std::shared_ptr<rai::vote> vote_a) :
message (rai::message_type::confirm_ack),
vote (vote_a)
{
@ -407,7 +407,7 @@ void rai::confirm_ack::serialize (rai::stream & stream_a)
vote->serialize (stream_a, block_type ());
}
bool rai::confirm_ack::operator == (rai::confirm_ack const & other_a) const
bool rai::confirm_ack::operator== (rai::confirm_ack const & other_a) const
{
auto result (*vote == *other_a.vote);
return result;
@ -457,7 +457,7 @@ void rai::frontier_req::visit (rai::message_visitor & visitor_a) const
visitor_a.frontier_req (*this);
}
bool rai::frontier_req::operator == (rai::frontier_req const & other_a) const
bool rai::frontier_req::operator== (rai::frontier_req const & other_a) const
{
return start == other_a.start && age == other_a.age && count == other_a.count;
}

View file

@ -40,29 +40,29 @@ struct endpoint_hash
{
};
template <>
struct endpoint_hash <8>
struct endpoint_hash<8>
{
size_t operator () (rai::endpoint const & endpoint_a) const
size_t operator() (rai::endpoint const & endpoint_a) const
{
return endpoint_hash_raw (endpoint_a);
}
};
template <>
struct endpoint_hash <4>
struct endpoint_hash<4>
{
size_t operator () (rai::endpoint const & endpoint_a) const
size_t operator() (rai::endpoint const & endpoint_a) const
{
uint64_t big (endpoint_hash_raw (endpoint_a));
uint32_t result (static_cast <uint32_t> (big) ^ static_cast <uint32_t> (big >> 32));
uint32_t result (static_cast<uint32_t> (big) ^ static_cast<uint32_t> (big >> 32));
return result;
}
};
template <>
struct hash <rai::endpoint>
struct hash<rai::endpoint>
{
size_t operator () (rai::endpoint const & endpoint_a) const
size_t operator() (rai::endpoint const & endpoint_a) const
{
endpoint_hash <sizeof (size_t)> ehash;
endpoint_hash<sizeof (size_t)> ehash;
return ehash (endpoint_a);
}
};
@ -70,11 +70,11 @@ struct hash <rai::endpoint>
namespace boost
{
template <>
struct hash <rai::endpoint>
struct hash<rai::endpoint>
{
size_t operator () (rai::endpoint const & endpoint_a) const
size_t operator() (rai::endpoint const & endpoint_a) const
{
std::hash <rai::endpoint> hash;
std::hash<rai::endpoint> hash;
return hash (endpoint_a);
}
};
@ -102,7 +102,7 @@ public:
message (bool &, rai::stream &);
virtual ~message () = default;
void write_header (rai::stream &);
static bool read_header (rai::stream &, uint8_t &, uint8_t &, uint8_t &, rai::message_type &, std::bitset <16> &);
static bool read_header (rai::stream &, uint8_t &, uint8_t &, uint8_t &, rai::message_type &, std::bitset<16> &);
virtual void serialize (rai::stream &) = 0;
virtual bool deserialize (rai::stream &) = 0;
virtual void visit (rai::message_visitor &) const = 0;
@ -110,15 +110,15 @@ public:
void block_type_set (rai::block_type);
bool ipv4_only ();
void ipv4_only_set (bool);
static std::array <uint8_t, 2> constexpr magic_number = rai::rai_network == rai::rai_networks::rai_test_network ? std::array <uint8_t, 2>({ 'R', 'A' }) : rai::rai_network == rai::rai_networks::rai_beta_network ? std::array <uint8_t, 2>({ 'R', 'B' }) : std::array <uint8_t, 2>({ 'R', 'C' });
static std::array<uint8_t, 2> constexpr magic_number = rai::rai_network == rai::rai_networks::rai_test_network ? std::array<uint8_t, 2> ({ 'R', 'A' }) : rai::rai_network == rai::rai_networks::rai_beta_network ? std::array<uint8_t, 2> ({ 'R', 'B' }) : std::array<uint8_t, 2> ({ 'R', 'C' });
uint8_t version_max;
uint8_t version_using;
uint8_t version_min;
rai::message_type type;
std::bitset <16> extensions;
std::bitset<16> extensions;
static size_t constexpr ipv4_only_position = 1;
static size_t constexpr bootstrap_server_position = 2;
static std::bitset <16> constexpr block_type_mask = std::bitset <16> (0x0f00);
static std::bitset<16> constexpr block_type_mask = std::bitset<16> (0x0f00);
};
class work_pool;
class message_parser
@ -143,41 +143,41 @@ public:
void visit (rai::message_visitor &) const override;
bool deserialize (rai::stream &) override;
void serialize (rai::stream &) override;
bool operator == (rai::keepalive const &) const;
std::array <rai::endpoint, 8> peers;
bool operator== (rai::keepalive const &) const;
std::array<rai::endpoint, 8> peers;
};
class publish : public message
{
public:
publish ();
publish (std::shared_ptr <rai::block>);
publish (std::shared_ptr<rai::block>);
void visit (rai::message_visitor &) const override;
bool deserialize (rai::stream &) override;
void serialize (rai::stream &) override;
bool operator == (rai::publish const &) const;
std::shared_ptr <rai::block> block;
bool operator== (rai::publish const &) const;
std::shared_ptr<rai::block> block;
};
class confirm_req : public message
{
public:
confirm_req ();
confirm_req (std::shared_ptr <rai::block>);
confirm_req (std::shared_ptr<rai::block>);
bool deserialize (rai::stream &) override;
void serialize (rai::stream &) override;
void visit (rai::message_visitor &) const override;
bool operator == (rai::confirm_req const &) const;
std::shared_ptr <rai::block> block;
bool operator== (rai::confirm_req const &) const;
std::shared_ptr<rai::block> block;
};
class confirm_ack : public message
{
public:
confirm_ack (bool &, rai::stream &);
confirm_ack (std::shared_ptr <rai::vote>);
confirm_ack (std::shared_ptr<rai::vote>);
bool deserialize (rai::stream &) override;
void serialize (rai::stream &) override;
void visit (rai::message_visitor &) const override;
bool operator == (rai::confirm_ack const &) const;
std::shared_ptr <rai::vote> vote;
bool operator== (rai::confirm_ack const &) const;
std::shared_ptr<rai::vote> vote;
};
class frontier_req : public message
{
@ -186,7 +186,7 @@ public:
bool deserialize (rai::stream &) override;
void serialize (rai::stream &) override;
void visit (rai::message_visitor &) const override;
bool operator == (rai::frontier_req const &) const;
bool operator== (rai::frontier_req const &) const;
rai::account start;
uint32_t age;
uint32_t count;

1169
rai/node/node.cpp Executable file → Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,46 +4,47 @@
#include <rai/node/bootstrap.hpp>
#include <rai/node/wallet.hpp>
#include <unordered_set>
#include <memory>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
#include <unordered_set>
#include <boost/asio.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/log/trivial.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/multi_index_container.hpp>
#include <miniupnpc.h>
std::ostream & operator << (std::ostream &, std::chrono::system_clock::time_point const &);
std::ostream & operator<< (std::ostream &, std::chrono::system_clock::time_point const &);
namespace boost
{
namespace program_options
{
class options_description;
class variables_map;
class options_description;
class variables_map;
}
}
namespace rai
{
class node;
class election : public std::enable_shared_from_this <rai::election>
class election : public std::enable_shared_from_this<rai::election>
{
std::function <void (std::shared_ptr <rai::block>)> confirmation_action;
std::function<void(std::shared_ptr<rai::block>)> confirmation_action;
void confirm_once (MDB_txn *);
public:
election (MDB_txn *, rai::node &, std::shared_ptr <rai::block>, std::function <void (std::shared_ptr <rai::block>)> const &);
void vote (std::shared_ptr <rai::vote>);
election (MDB_txn *, rai::node &, std::shared_ptr<rai::block>, std::function<void(std::shared_ptr<rai::block>)> const &);
void vote (std::shared_ptr<rai::vote>);
// Check if we have vote quorum
bool have_quorum (MDB_txn *);
// Tell the network our view of the winner
@ -59,14 +60,14 @@ public:
rai::votes votes;
rai::node & node;
std::chrono::system_clock::time_point last_vote;
std::shared_ptr <rai::block> last_winner;
std::shared_ptr<rai::block> last_winner;
std::atomic_flag confirmed;
};
class conflict_info
{
public:
rai::block_hash root;
std::shared_ptr <rai::election> election;
std::shared_ptr<rai::election> election;
// Number of announcements in a row for this fork
unsigned announcements;
};
@ -78,20 +79,17 @@ public:
active_transactions (rai::node &);
// Start an election for a block
// Call action with confirmed block, may be different than what we started with
bool start (MDB_txn *, std::shared_ptr <rai::block>, std::function <void (std::shared_ptr <rai::block>)> const & = [] (std::shared_ptr <rai::block>) {});
void vote (std::shared_ptr <rai::vote>);
bool start (MDB_txn *, std::shared_ptr<rai::block>, std::function<void(std::shared_ptr<rai::block>)> const & = [](std::shared_ptr<rai::block>) {});
void vote (std::shared_ptr<rai::vote>);
// Is the root of this block in the roots container
bool active (rai::block const &);
void announce_votes ();
void stop ();
boost::multi_index_container
<
rai::conflict_info,
boost::multi_index::indexed_by
<
boost::multi_index::ordered_unique <boost::multi_index::member <rai::conflict_info, rai::block_hash, &rai::conflict_info::root>>
>
> roots;
boost::multi_index_container<
rai::conflict_info,
boost::multi_index::indexed_by<
boost::multi_index::ordered_unique<boost::multi_index::member<rai::conflict_info, rai::block_hash, &rai::conflict_info::root>>>>
roots;
rai::node & node;
std::mutex mutex;
// Maximum number of conflicts to vote on per interval, lowest root hash first
@ -103,21 +101,21 @@ public:
class operation
{
public:
bool operator > (rai::operation const &) const;
bool operator> (rai::operation const &) const;
std::chrono::system_clock::time_point wakeup;
std::function <void ()> function;
std::function<void()> function;
};
class alarm
{
public:
alarm (boost::asio::io_service &);
~alarm ();
void add (std::chrono::system_clock::time_point const &, std::function <void ()> const &);
void add (std::chrono::system_clock::time_point const &, std::function<void()> const &);
void run ();
boost::asio::io_service & service;
std::mutex mutex;
std::condition_variable condition;
std::priority_queue <operation, std::vector <operation>, std::greater <operation>> operations;
std::priority_queue<operation, std::vector<operation>, std::greater<operation>> operations;
std::thread thread;
};
class gap_information
@ -125,25 +123,22 @@ class gap_information
public:
std::chrono::system_clock::time_point arrival;
rai::block_hash hash;
std::unique_ptr <rai::votes> votes;
std::unique_ptr<rai::votes> votes;
};
class gap_cache
{
public:
gap_cache (rai::node &);
void add (MDB_txn *, std::shared_ptr <rai::block>);
void vote (std::shared_ptr <rai::vote>);
void add (MDB_txn *, std::shared_ptr<rai::block>);
void vote (std::shared_ptr<rai::vote>);
rai::uint128_t bootstrap_threshold (MDB_txn *);
void purge_old ();
boost::multi_index_container
<
rai::gap_information,
boost::multi_index::indexed_by
<
boost::multi_index::ordered_non_unique <boost::multi_index::member <gap_information, std::chrono::system_clock::time_point, &gap_information::arrival>>,
boost::multi_index::hashed_unique <boost::multi_index::member <gap_information, rai::block_hash, &gap_information::hash>>
>
> blocks;
boost::multi_index_container<
rai::gap_information,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<boost::multi_index::member<gap_information, std::chrono::system_clock::time_point, &gap_information::arrival>>,
boost::multi_index::hashed_unique<boost::multi_index::member<gap_information, rai::block_hash, &gap_information::hash>>>>
blocks;
size_t const max = 256;
std::mutex mutex;
rai::node & node;
@ -181,20 +176,20 @@ public:
bool known_peer (rai::endpoint const &);
// Notify of peer we received from
bool insert (rai::endpoint const &, unsigned);
std::unordered_set <rai::endpoint> random_set (size_t);
void random_fill (std::array <rai::endpoint, 8> &);
std::unordered_set<rai::endpoint> random_set (size_t);
void random_fill (std::array<rai::endpoint, 8> &);
// Request a list of the top known representatives
std::vector <peer_information> representatives (size_t);
std::vector<peer_information> representatives (size_t);
// List of all peers
std::vector <rai::endpoint> list ();
std::map <rai::endpoint, unsigned> list_version ();
std::vector<rai::endpoint> list ();
std::map<rai::endpoint, unsigned> list_version ();
// A list of random peers with size the square root of total peer count
std::vector <rai::endpoint> list_sqrt ();
std::vector<rai::endpoint> list_sqrt ();
// Get the next peer for attempting bootstrap
rai::endpoint bootstrap_peer ();
// Purge any peer where last_contact < time_point and return what was left
std::vector <rai::peer_information> purge_list (std::chrono::system_clock::time_point const &);
std::vector <rai::endpoint> rep_crawl ();
std::vector<rai::peer_information> purge_list (std::chrono::system_clock::time_point const &);
std::vector<rai::endpoint> rep_crawl ();
bool rep_response (rai::endpoint const &, rai::amount const &);
void rep_request (rai::endpoint const &);
// Should we reach out to this endpoint with a keepalive message
@ -204,32 +199,26 @@ public:
bool empty ();
std::mutex mutex;
rai::endpoint self;
boost::multi_index_container
<
peer_information,
boost::multi_index::indexed_by
<
boost::multi_index::hashed_unique <boost::multi_index::member <peer_information, rai::endpoint, &peer_information::endpoint>>,
boost::multi_index::ordered_non_unique <boost::multi_index::member <peer_information, std::chrono::system_clock::time_point, &peer_information::last_contact>>,
boost::multi_index::ordered_non_unique <boost::multi_index::member <peer_information, std::chrono::system_clock::time_point, &peer_information::last_attempt>, std::greater <std::chrono::system_clock::time_point>>,
boost::multi_index::random_access <>,
boost::multi_index::ordered_non_unique <boost::multi_index::member <peer_information, std::chrono::system_clock::time_point, &peer_information::last_bootstrap_attempt>>,
boost::multi_index::ordered_non_unique <boost::multi_index::member <peer_information, std::chrono::system_clock::time_point, &peer_information::last_rep_request>>,
boost::multi_index::ordered_non_unique <boost::multi_index::member <peer_information, rai::amount, &peer_information::rep_weight>, std::greater <rai::amount>>
>
> peers;
boost::multi_index_container
<
peer_attempt,
boost::multi_index::indexed_by
<
boost::multi_index::hashed_unique <boost::multi_index::member <peer_attempt, rai::endpoint, &peer_attempt::endpoint>>,
boost::multi_index::ordered_non_unique <boost::multi_index::member <peer_attempt, std::chrono::system_clock::time_point, &peer_attempt::last_attempt>>
>
> attempts;
boost::multi_index_container<
peer_information,
boost::multi_index::indexed_by<
boost::multi_index::hashed_unique<boost::multi_index::member<peer_information, rai::endpoint, &peer_information::endpoint>>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<peer_information, std::chrono::system_clock::time_point, &peer_information::last_contact>>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<peer_information, std::chrono::system_clock::time_point, &peer_information::last_attempt>, std::greater<std::chrono::system_clock::time_point>>,
boost::multi_index::random_access<>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<peer_information, std::chrono::system_clock::time_point, &peer_information::last_bootstrap_attempt>>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<peer_information, std::chrono::system_clock::time_point, &peer_information::last_rep_request>>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<peer_information, rai::amount, &peer_information::rep_weight>, std::greater<rai::amount>>>>
peers;
boost::multi_index_container<
peer_attempt,
boost::multi_index::indexed_by<
boost::multi_index::hashed_unique<boost::multi_index::member<peer_attempt, rai::endpoint, &peer_attempt::endpoint>>,
boost::multi_index::ordered_non_unique<boost::multi_index::member<peer_attempt, std::chrono::system_clock::time_point, &peer_attempt::last_attempt>>>>
attempts;
// Called when a new peer is observed
std::function <void (rai::endpoint const &)> peer_observer;
std::function <void ()> disconnect_observer;
std::function<void(rai::endpoint const &)> peer_observer;
std::function<void()> disconnect_observer;
// Number of peers to crawl for being a rep every period
static size_t constexpr peers_per_crawl = 8;
};
@ -239,7 +228,7 @@ public:
uint8_t const * data;
size_t size;
rai::endpoint endpoint;
std::function <void (boost::system::error_code const &, size_t)> callback;
std::function<void(boost::system::error_code const &, size_t)> callback;
};
class mapping_protocol
{
@ -272,7 +261,7 @@ public:
static int constexpr mapping_timeout = rai::rai_network == rai::rai_networks::rai_test_network ? 53 : 3593;
static int constexpr check_timeout = rai::rai_network == rai::rai_networks::rai_test_network ? 17 : 53;
boost::asio::ip::address_v4 address;
std::array <mapping_protocol, 2> protocols;
std::array<mapping_protocol, 2> protocols;
uint64_t check_count;
bool on;
};
@ -280,10 +269,10 @@ class message_statistics
{
public:
message_statistics ();
std::atomic <uint64_t> keepalive;
std::atomic <uint64_t> publish;
std::atomic <uint64_t> confirm_req;
std::atomic <uint64_t> confirm_ack;
std::atomic<uint64_t> keepalive;
std::atomic<uint64_t> publish;
std::atomic<uint64_t> confirm_req;
std::atomic<uint64_t> confirm_ack;
};
class block_arrival_info
{
@ -298,15 +287,12 @@ class block_arrival
public:
void add (rai::block_hash const &);
bool recent (rai::block_hash const &);
boost::multi_index_container
<
rai::block_arrival_info,
boost::multi_index::indexed_by
<
boost::multi_index::ordered_non_unique <boost::multi_index::member <rai::block_arrival_info, std::chrono::system_clock::time_point, &rai::block_arrival_info::arrival>>,
boost::multi_index::hashed_unique <boost::multi_index::member <rai::block_arrival_info, rai::block_hash, &rai::block_arrival_info::hash>>
>
> arrival;
boost::multi_index_container<
rai::block_arrival_info,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<boost::multi_index::member<rai::block_arrival_info, std::chrono::system_clock::time_point, &rai::block_arrival_info::arrival>>,
boost::multi_index::hashed_unique<boost::multi_index::member<rai::block_arrival_info, rai::block_hash, &rai::block_arrival_info::hash>>>>
arrival;
std::mutex mutex;
};
class network
@ -317,20 +303,20 @@ public:
void stop ();
void receive_action (boost::system::error_code const &, size_t);
void rpc_action (boost::system::error_code const &, size_t);
void rebroadcast_reps (std::shared_ptr <rai::block>);
void republish_vote (std::chrono::system_clock::time_point const &, std::shared_ptr <rai::vote>);
void republish_block (MDB_txn *, std::shared_ptr <rai::block>);
void republish (rai::block_hash const &, std::shared_ptr <std::vector <uint8_t>>, rai::endpoint);
void publish_broadcast (std::vector <rai::peer_information> &, std::unique_ptr <rai::block>);
void confirm_send (rai::confirm_ack const &, std::shared_ptr <std::vector <uint8_t>>, rai::endpoint const &);
void merge_peers (std::array <rai::endpoint, 8> const &);
void rebroadcast_reps (std::shared_ptr<rai::block>);
void republish_vote (std::chrono::system_clock::time_point const &, std::shared_ptr<rai::vote>);
void republish_block (MDB_txn *, std::shared_ptr<rai::block>);
void republish (rai::block_hash const &, std::shared_ptr<std::vector<uint8_t>>, rai::endpoint);
void publish_broadcast (std::vector<rai::peer_information> &, std::unique_ptr<rai::block>);
void confirm_send (rai::confirm_ack const &, std::shared_ptr<std::vector<uint8_t>>, rai::endpoint const &);
void merge_peers (std::array<rai::endpoint, 8> const &);
void send_keepalive (rai::endpoint const &);
void broadcast_confirm_req (std::shared_ptr <rai::block>);
void send_confirm_req (rai::endpoint const &, std::shared_ptr <rai::block>);
void send_buffer (uint8_t const *, size_t, rai::endpoint const &, std::function <void (boost::system::error_code const &, size_t)>);
void broadcast_confirm_req (std::shared_ptr<rai::block>);
void send_confirm_req (rai::endpoint const &, std::shared_ptr<rai::block>);
void send_buffer (uint8_t const *, size_t, rai::endpoint const &, std::function<void(boost::system::error_code const &, size_t)>);
rai::endpoint endpoint ();
rai::endpoint remote;
std::array <uint8_t, 512> buffer;
std::array<uint8_t, 512> buffer;
boost::asio::ip::udp::socket socket;
std::mutex socket_mutex;
boost::asio::ip::udp::resolver resolver;
@ -366,7 +352,7 @@ public:
bool work_generation_time () const;
bool log_to_cerr () const;
void init (boost::filesystem::path const &);
bool ledger_logging_value;
bool ledger_duplicate_logging_value;
bool vote_logging_value;
@ -403,9 +389,9 @@ public:
rai::account random_representative ();
uint16_t peering_port;
rai::logging logging;
std::vector <std::pair <boost::asio::ip::address, uint16_t>> work_peers;
std::vector <std::string> preconfigured_peers;
std::vector <rai::account> preconfigured_representatives;
std::vector<std::pair<boost::asio::ip::address, uint16_t>> work_peers;
std::vector<std::string> preconfigured_peers;
std::vector<rai::account> preconfigured_representatives;
unsigned bootstrap_fraction_numerator;
rai::amount receive_minimum;
rai::amount inactive_supply;
@ -424,18 +410,18 @@ public:
class node_observers
{
public:
rai::observer_set <std::shared_ptr <rai::block>, rai::account const &, rai::amount const &> blocks;
rai::observer_set <rai::account const &, bool> wallet;
rai::observer_set <std::shared_ptr <rai::vote>, rai::endpoint const &> vote;
rai::observer_set <rai::endpoint const &> endpoint;
rai::observer_set <> disconnect;
rai::observer_set <> started;
rai::observer_set<std::shared_ptr<rai::block>, rai::account const &, rai::amount const &> blocks;
rai::observer_set<rai::account const &, bool> wallet;
rai::observer_set<std::shared_ptr<rai::vote>, rai::endpoint const &> vote;
rai::observer_set<rai::endpoint const &> endpoint;
rai::observer_set<> disconnect;
rai::observer_set<> started;
};
class vote_processor
{
public:
vote_processor (rai::node &);
rai::vote_result vote (std::shared_ptr <rai::vote>, rai::endpoint);
rai::vote_result vote (std::shared_ptr<rai::vote>, rai::endpoint);
rai::node & node;
};
// The network is crawled for representatives by ocassionally sending a unicast confirm_req for a specific block and watching to see if it's acknowledged with a vote.
@ -446,17 +432,17 @@ public:
void remove (rai::block_hash const &);
bool exists (rai::block_hash const &);
std::mutex mutex;
std::unordered_set <rai::block_hash> active;
std::unordered_set<rai::block_hash> active;
};
class block_processor_item
{
public:
block_processor_item (std::shared_ptr <rai::block>);
block_processor_item (std::shared_ptr <rai::block>, bool);
block_processor_item (std::shared_ptr <rai::block>, std::function <void (MDB_txn *, rai::process_return, std::shared_ptr <rai::block>)>);
block_processor_item (std::shared_ptr <rai::block>, std::function <void (MDB_txn *, rai::process_return, std::shared_ptr <rai::block>)>, bool);
std::shared_ptr <rai::block> block;
std::function <void (MDB_txn *, rai::process_return, std::shared_ptr <rai::block>)> callback;
block_processor_item (std::shared_ptr<rai::block>);
block_processor_item (std::shared_ptr<rai::block>, bool);
block_processor_item (std::shared_ptr<rai::block>, std::function<void(MDB_txn *, rai::process_return, std::shared_ptr<rai::block>)>);
block_processor_item (std::shared_ptr<rai::block>, std::function<void(MDB_txn *, rai::process_return, std::shared_ptr<rai::block>)>, bool);
std::shared_ptr<rai::block> block;
std::function<void(MDB_txn *, rai::process_return, std::shared_ptr<rai::block>)> callback;
bool force;
};
// Processing blocks is a potentially long IO operation
@ -470,18 +456,19 @@ public:
void flush ();
void add (rai::block_processor_item const &);
void process_receive_many (rai::block_processor_item const &);
void process_receive_many (std::deque <rai::block_processor_item> &);
rai::process_return process_receive_one (MDB_txn *, std::shared_ptr <rai::block>);
void process_receive_many (std::deque<rai::block_processor_item> &);
rai::process_return process_receive_one (MDB_txn *, std::shared_ptr<rai::block>);
void process_blocks ();
private:
bool stopped;
bool idle;
std::deque <rai::block_processor_item> blocks;
std::deque<rai::block_processor_item> blocks;
std::mutex mutex;
std::condition_variable condition;
rai::node & node;
};
class node : public std::enable_shared_from_this <rai::node>
class node : public std::enable_shared_from_this<rai::node>
{
public:
node (rai::node_init &, boost::asio::io_service &, uint16_t, boost::filesystem::path const &, rai::alarm &, rai::logging const &, rai::work_pool &);
@ -496,17 +483,17 @@ public:
void keepalive (std::string const &, uint16_t);
void start ();
void stop ();
std::shared_ptr <rai::node> shared ();
std::shared_ptr<rai::node> shared ();
int store_version ();
void process_confirmed (std::shared_ptr <rai::block>);
void process_confirmed (std::shared_ptr<rai::block>);
void process_message (rai::message &, rai::endpoint const &);
void process_active (std::shared_ptr <rai::block>);
void process_active (std::shared_ptr<rai::block>);
rai::process_return process (rai::block const &);
void keepalive_preconfigured (std::vector <std::string> const &);
void keepalive_preconfigured (std::vector<std::string> const &);
rai::block_hash latest (rai::account const &);
rai::uint128_t balance (rai::account const &);
std::unique_ptr <rai::block> block (rai::block_hash const &);
std::pair <rai::uint128_t, rai::uint128_t> balance_pending (rai::account const &);
std::unique_ptr<rai::block> block (rai::block_hash const &);
std::pair<rai::uint128_t, rai::uint128_t> balance_pending (rai::account const &);
rai::uint128_t weight (rai::account const &);
rai::account representative (rai::account const &);
void ongoing_keepalive ();
@ -517,7 +504,7 @@ public:
int price (rai::uint128_t const &, int);
void generate_work (rai::block &);
uint64_t generate_work (rai::uint256_union const &);
void generate_work (rai::uint256_union const &, std::function <void (uint64_t)>);
void generate_work (rai::uint256_union const &, std::function<void(uint64_t)>);
void add_initial_peers ();
boost::asio::io_service & service;
rai::node_config config;
@ -554,7 +541,7 @@ public:
thread_runner (boost::asio::io_service &, unsigned);
~thread_runner ();
void join ();
std::vector <std::thread> threads;
std::vector<std::thread> threads;
};
void add_node_options (boost::program_options::options_description &);
bool handle_node_options (boost::program_options::variables_map &);
@ -564,11 +551,11 @@ public:
inactive_node ();
~inactive_node ();
boost::filesystem::path path;
boost::shared_ptr <boost::asio::io_service> service;
boost::shared_ptr<boost::asio::io_service> service;
rai::alarm alarm;
rai::logging logging;
rai::node_init init;
rai::work_pool work;
std::shared_ptr <rai::node> node;
std::shared_ptr<rai::node> node;
};
}

View file

@ -4,11 +4,11 @@
#include <rai/node/node.hpp>
#include <rai/node/wallet.hpp>
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <array>
#include <iostream>
#include <map>
#include <string>
#include <vector>
namespace
{
@ -394,15 +394,15 @@ rai::opencl_environment::opencl_environment (bool & error_a)
{
cl_uint platformIdCount = 0;
clGetPlatformIDs (0, nullptr, &platformIdCount);
std::vector <cl_platform_id> platformIds (platformIdCount);
clGetPlatformIDs (platformIdCount, platformIds.data(), nullptr);
std::vector<cl_platform_id> platformIds (platformIdCount);
clGetPlatformIDs (platformIdCount, platformIds.data (), nullptr);
for (auto i (platformIds.begin ()), n (platformIds.end ()); i != n; ++i)
{
rai::opencl_platform platform;
platform.platform = *i;
cl_uint deviceIdCount = 0;
clGetDeviceIDs (*i, CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceIdCount);
std::vector <cl_device_id> deviceIds (deviceIdCount);
std::vector<cl_device_id> deviceIds (deviceIdCount);
clGetDeviceIDs (*i, CL_DEVICE_TYPE_ALL, deviceIdCount, deviceIds.data (), nullptr);
for (auto j (deviceIds.begin ()), m (deviceIds.end ()); j != m; ++j)
{
@ -416,72 +416,72 @@ void rai::opencl_environment::dump (std::ostream & stream)
{
auto index (0);
auto device_count (0);
for (auto & i: platforms)
for (auto & i : platforms)
{
device_count += i.devices.size ();
}
stream << boost::str (boost::format ("OpenCL found %1% platforms and %2% devices\n") % platforms.size () % device_count);
for (auto i (platforms.begin ()), n (platforms.end ()); i != n; ++i, ++index)
{
std::vector <unsigned> queries = {CL_PLATFORM_PROFILE, CL_PLATFORM_VERSION, CL_PLATFORM_NAME, CL_PLATFORM_VENDOR, CL_PLATFORM_EXTENSIONS};
std::vector<unsigned> queries = { CL_PLATFORM_PROFILE, CL_PLATFORM_VERSION, CL_PLATFORM_NAME, CL_PLATFORM_VENDOR, CL_PLATFORM_EXTENSIONS };
stream << "Platform: " << index << std::endl;
for (auto j (queries.begin ()), m (queries.end ()); j != m; ++j)
{
size_t platformInfoCount = 0;
clGetPlatformInfo(i->platform, *j, 0, nullptr, &platformInfoCount);
std::vector <char> info (platformInfoCount);
clGetPlatformInfo(i->platform, *j, info.size (), info.data (), nullptr);
clGetPlatformInfo (i->platform, *j, 0, nullptr, &platformInfoCount);
std::vector<char> info (platformInfoCount);
clGetPlatformInfo (i->platform, *j, info.size (), info.data (), nullptr);
stream << info.data () << std::endl;
}
for (auto j (i->devices.begin ()), m (i->devices.end ()); j != m; ++j)
{
std::vector <unsigned> queries = {CL_DEVICE_NAME, CL_DEVICE_VENDOR, CL_DEVICE_PROFILE};
std::vector<unsigned> queries = { CL_DEVICE_NAME, CL_DEVICE_VENDOR, CL_DEVICE_PROFILE };
stream << "Device: " << j - i->devices.begin () << std::endl;
for (auto k (queries.begin ()), o (queries.end ()); k != o; ++k)
{
size_t platformInfoCount = 0;
clGetDeviceInfo(*j, *k, 0, nullptr, &platformInfoCount);
std::vector <char> info (platformInfoCount);
clGetDeviceInfo(*j, *k, info.size (), info.data (), nullptr);
clGetDeviceInfo (*j, *k, 0, nullptr, &platformInfoCount);
std::vector<char> info (platformInfoCount);
clGetDeviceInfo (*j, *k, info.size (), info.data (), nullptr);
stream << '\t' << info.data () << std::endl;
}
size_t deviceTypeCount = 0;
clGetDeviceInfo(*j, CL_DEVICE_TYPE, 0, nullptr, &deviceTypeCount);
std::vector <uint8_t> deviceTypeInfo (deviceTypeCount);
clGetDeviceInfo(*j, CL_DEVICE_TYPE, deviceTypeCount, deviceTypeInfo.data (), 0);
clGetDeviceInfo (*j, CL_DEVICE_TYPE, 0, nullptr, &deviceTypeCount);
std::vector<uint8_t> deviceTypeInfo (deviceTypeCount);
clGetDeviceInfo (*j, CL_DEVICE_TYPE, deviceTypeCount, deviceTypeInfo.data (), 0);
std::string device_type_string;
switch (deviceTypeInfo [0])
switch (deviceTypeInfo[0])
{
case CL_DEVICE_TYPE_ACCELERATOR:
device_type_string = "ACCELERATOR";
break;
device_type_string = "ACCELERATOR";
break;
case CL_DEVICE_TYPE_CPU:
device_type_string = "CPU";
break;
device_type_string = "CPU";
break;
case CL_DEVICE_TYPE_CUSTOM:
device_type_string = "CUSTOM";
break;
device_type_string = "CUSTOM";
break;
case CL_DEVICE_TYPE_DEFAULT:
device_type_string = "DEFAULT";
break;
device_type_string = "DEFAULT";
break;
case CL_DEVICE_TYPE_GPU:
device_type_string = "GPU";
break;
default:
device_type_string = "Unknown";
break;
device_type_string = "GPU";
break;
default:
device_type_string = "Unknown";
break;
}
stream << '\t' << device_type_string << std::endl;
size_t compilerAvailableCount = 0;
clGetDeviceInfo(*j, CL_DEVICE_COMPILER_AVAILABLE, 0, nullptr, &compilerAvailableCount);
std::vector <uint8_t> compilerAvailableInfo (compilerAvailableCount);
clGetDeviceInfo(*j, CL_DEVICE_COMPILER_AVAILABLE, compilerAvailableCount, compilerAvailableInfo.data (), 0);
stream << '\t' << "Compiler available: " << (compilerAvailableInfo [0] ? "true" : "false") << std::endl;
clGetDeviceInfo (*j, CL_DEVICE_COMPILER_AVAILABLE, 0, nullptr, &compilerAvailableCount);
std::vector<uint8_t> compilerAvailableInfo (compilerAvailableCount);
clGetDeviceInfo (*j, CL_DEVICE_COMPILER_AVAILABLE, compilerAvailableCount, compilerAvailableInfo.data (), 0);
stream << '\t' << "Compiler available: " << (compilerAvailableInfo[0] ? "true" : "false") << std::endl;
size_t computeUnitsAvailableCount = 0;
clGetDeviceInfo(*j, CL_DEVICE_MAX_COMPUTE_UNITS, 0, nullptr, &computeUnitsAvailableCount);
std::vector <uint8_t> computeUnitsAvailableInfo (computeUnitsAvailableCount);
clGetDeviceInfo(*j, CL_DEVICE_MAX_COMPUTE_UNITS, computeUnitsAvailableCount, computeUnitsAvailableInfo.data (), 0);
uint64_t computeUnits (computeUnitsAvailableInfo [0] | (computeUnitsAvailableInfo [1] << 8) | (computeUnitsAvailableInfo [2] << 16) | (computeUnitsAvailableInfo [3] << 24));
clGetDeviceInfo (*j, CL_DEVICE_MAX_COMPUTE_UNITS, 0, nullptr, &computeUnitsAvailableCount);
std::vector<uint8_t> computeUnitsAvailableInfo (computeUnitsAvailableCount);
clGetDeviceInfo (*j, CL_DEVICE_MAX_COMPUTE_UNITS, computeUnitsAvailableCount, computeUnitsAvailableInfo.data (), 0);
uint64_t computeUnits (computeUnitsAvailableInfo[0] | (computeUnitsAvailableInfo[1] << 8) | (computeUnitsAvailableInfo[2] << 16) | (computeUnitsAvailableInfo[3] << 24));
stream << '\t' << "Compute units available: " << computeUnits << std::endl;
}
}
@ -513,9 +513,9 @@ bool rai::opencl_config::deserialize_json (boost::property_tree::ptree const & t
auto result (false);
try
{
auto platform_l (tree_a.get <std::string> ("platform"));
auto device_l (tree_a.get <std::string> ("device"));
auto threads_l (tree_a.get <std::string> ("threads"));
auto platform_l (tree_a.get<std::string> ("platform"));
auto device_l (tree_a.get<std::string> ("device"));
auto threads_l (tree_a.get<std::string> ("threads"));
try
{
platform = std::stoull (platform_l);
@ -548,15 +548,14 @@ logging (logging_a)
error_a |= config.platform >= environment_a.platforms.size ();
if (!error_a)
{
auto & platform (environment_a.platforms [config.platform]);
auto & platform (environment_a.platforms[config.platform]);
error_a |= config.device >= platform.devices.size ();
if (!error_a)
{
rai::random_pool.GenerateBlock (reinterpret_cast <uint8_t *> (rand.s.data ()), rand.s.size () * sizeof (decltype (rand.s)::value_type));
std::array <cl_device_id, 1> selected_devices;
selected_devices [0] = platform.devices [config.device];
cl_context_properties contextProperties [] =
{
rai::random_pool.GenerateBlock (reinterpret_cast<uint8_t *> (rand.s.data ()), rand.s.size () * sizeof (decltype (rand.s)::value_type));
std::array<cl_device_id, 1> selected_devices;
selected_devices[0] = platform.devices[config.device];
cl_context_properties contextProperties[] = {
CL_CONTEXT_PLATFORM,
reinterpret_cast<cl_context_properties> (platform.platform),
0, 0
@ -567,7 +566,7 @@ logging (logging_a)
if (!error_a)
{
cl_int queue_error (0);
queue = clCreateCommandQueue (context, selected_devices [0], 0, &queue_error);
queue = clCreateCommandQueue (context, selected_devices[0], 0, &queue_error);
error_a |= queue_error != CL_SUCCESS;
if (!error_a)
{
@ -594,7 +593,7 @@ logging (logging_a)
error_a |= program_error != CL_SUCCESS;
if (!error_a)
{
auto clBuildProgramError (clBuildProgram(program, selected_devices.size(), selected_devices.data(), "-D __APPLE__", nullptr, nullptr));
auto clBuildProgramError (clBuildProgram (program, selected_devices.size (), selected_devices.data (), "-D __APPLE__", nullptr, nullptr));
error_a |= clBuildProgramError != CL_SUCCESS;
if (!error_a)
{
@ -643,7 +642,7 @@ logging (logging_a)
{
size_t log_size (0);
clGetProgramBuildInfo (program, *i, CL_PROGRAM_BUILD_LOG, 0, nullptr, &log_size);
std::vector <char> log (log_size);
std::vector<char> log (log_size);
clGetProgramBuildInfo (program, *i, CL_PROGRAM_BUILD_LOG, log.size (), log.data (), nullptr);
BOOST_LOG (logging.log) << log.data ();
}
@ -706,13 +705,13 @@ rai::opencl_work::~opencl_work ()
}
}
boost::optional <uint64_t> rai::opencl_work::generate_work (rai::uint256_union const & root_a)
boost::optional<uint64_t> rai::opencl_work::generate_work (rai::uint256_union const & root_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
bool error (false);
uint64_t result (0);
unsigned thread_count (config.threads);
size_t work_size [] = { thread_count, 0, 0 };
size_t work_size[] = { thread_count, 0, 0 };
while (rai::work_validate (root_a, result) && !error)
{
result = rand.next ();
@ -725,7 +724,7 @@ boost::optional <uint64_t> rai::opencl_work::generate_work (rai::uint256_union c
cl_int enqueue_error = clEnqueueNDRangeKernel (queue, kernel, 1, nullptr, work_size, nullptr, 0, nullptr, nullptr);
if (enqueue_error == CL_SUCCESS)
{
cl_int read_error1 = clEnqueueReadBuffer(queue, result_buffer, false, 0, sizeof (uint64_t), &result, 0, nullptr, nullptr);
cl_int read_error1 = clEnqueueReadBuffer (queue, result_buffer, false, 0, sizeof (uint64_t), &result, 0, nullptr, nullptr);
if (read_error1 == CL_SUCCESS)
{
cl_int finishError = clFinish (queue);
@ -762,7 +761,7 @@ boost::optional <uint64_t> rai::opencl_work::generate_work (rai::uint256_union c
BOOST_LOG (logging.log) << boost::str (boost::format ("Error writing attempt %1%") % write_error1);
}
}
boost::optional <uint64_t> value;
boost::optional<uint64_t> value;
if (!error)
{
value = result;
@ -770,9 +769,9 @@ boost::optional <uint64_t> rai::opencl_work::generate_work (rai::uint256_union c
return value;
}
std::unique_ptr <rai::opencl_work> rai::opencl_work::create (bool create_a, rai::opencl_config const & config_a, rai::logging & logging_a)
std::unique_ptr<rai::opencl_work> rai::opencl_work::create (bool create_a, rai::opencl_config const & config_a, rai::logging & logging_a)
{
std::unique_ptr <rai::opencl_work> result;
std::unique_ptr<rai::opencl_work> result;
if (create_a)
{
auto error (false);

View file

@ -23,14 +23,14 @@ class opencl_platform
{
public:
cl_platform_id platform;
std::vector <cl_device_id> devices;
std::vector<cl_device_id> devices;
};
class opencl_environment
{
public:
opencl_environment (bool &);
void dump (std::ostream & stream);
std::vector <rai::opencl_platform> platforms;
std::vector<rai::opencl_platform> platforms;
};
union uint256_union;
class work_pool;
@ -51,7 +51,7 @@ public:
opencl_work (bool &, rai::opencl_config const &, rai::opencl_environment &, rai::logging &);
~opencl_work ();
boost::optional<uint64_t> generate_work (rai::uint256_union const &);
static std::unique_ptr <opencl_work> create (bool, rai::opencl_config const &, rai::logging &);
static std::unique_ptr<opencl_work> create (bool, rai::opencl_config const &, rai::logging &);
rai::opencl_config const & config;
std::mutex mutex;
cl_context context;

579
rai/node/rpc.cpp Executable file → Normal file

File diff suppressed because it is too large Load diff

View file

@ -50,28 +50,28 @@ public:
void observer_action (rai::account const &);
boost::asio::ip::tcp::acceptor acceptor;
std::mutex mutex;
std::unordered_map <rai::account, std::shared_ptr <rai::payment_observer>> payment_observers;
std::unordered_map<rai::account, std::shared_ptr<rai::payment_observer>> payment_observers;
rai::rpc_config config;
rai::node & node;
bool on;
static uint16_t const rpc_port = rai::rai_network == rai::rai_networks::rai_live_network ? 7076 : 55000;
};
class rpc_connection : public std::enable_shared_from_this <rai::rpc_connection>
class rpc_connection : public std::enable_shared_from_this<rai::rpc_connection>
{
public:
rpc_connection (rai::node &, rai::rpc &);
void parse_connection ();
std::shared_ptr <rai::node> node;
std::shared_ptr<rai::node> node;
rai::rpc & rpc;
boost::asio::ip::tcp::socket socket;
boost::beast::flat_buffer buffer;
boost::beast::http::request <boost::beast::http::string_body> request;
boost::beast::http::response <boost::beast::http::string_body> res;
boost::beast::http::request<boost::beast::http::string_body> request;
boost::beast::http::response<boost::beast::http::string_body> res;
};
class payment_observer : public std::enable_shared_from_this <rai::payment_observer>
class payment_observer : public std::enable_shared_from_this<rai::payment_observer>
{
public:
payment_observer (std::function <void (boost::property_tree::ptree const &)> const &, rai::rpc &, rai::account const &, rai::amount const &);
payment_observer (std::function<void(boost::property_tree::ptree const &)> const &, rai::rpc &, rai::account const &, rai::amount const &);
~payment_observer ();
void start (uint64_t);
void observe ();
@ -82,13 +82,13 @@ public:
rai::rpc & rpc;
rai::account account;
rai::amount amount;
std::function <void (boost::property_tree::ptree const &)> response;
std::function<void(boost::property_tree::ptree const &)> response;
std::atomic_flag completed;
};
class rpc_handler : public std::enable_shared_from_this <rai::rpc_handler>
class rpc_handler : public std::enable_shared_from_this<rai::rpc_handler>
{
public:
rpc_handler (rai::node &, rai::rpc &, std::string const &, std::function <void (boost::property_tree::ptree const &)> const &);
rpc_handler (rai::node &, rai::rpc &, std::string const &, std::function<void(boost::property_tree::ptree const &)> const &);
void process_request ();
void account_balance ();
void account_block_count ();
@ -189,6 +189,6 @@ public:
rai::node & node;
rai::rpc & rpc;
boost::property_tree::ptree request;
std::function <void (boost::property_tree::ptree const &)> response;
std::function<void(boost::property_tree::ptree const &)> response;
};
}

View file

@ -1,7 +1,7 @@
#include <rai/node/testing.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
rai::system::system (uint16_t port_a, size_t count_a) :
alarm (service),
@ -13,7 +13,7 @@ work (1, nullptr)
{
rai::node_init init;
rai::node_config config (port_a + i, logging);
auto node (std::make_shared <rai::node> (init, service, rai::unique_path (), alarm, config, work));
auto node (std::make_shared<rai::node> (init, service, rai::unique_path (), alarm, config, work));
assert (!init.error ());
node->start ();
rai::uint256_union wallet;
@ -28,14 +28,15 @@ work (1, nullptr)
auto starting2 ((*j)->peers.size ());
auto new2 (starting2);
(*j)->network.send_keepalive ((*i)->network.endpoint ());
do {
do
{
poll ();
new1 = (*i)->peers.size ();
new2 = (*j)->peers.size ();
} while (new1 == starting1 || new2 == starting2);
}
auto iterations1 (0);
while (std::any_of (nodes.begin (), nodes.end (), [] (std::shared_ptr <rai::node> const & node_a) {return node_a->bootstrap_initiator.in_progress ();}))
while (std::any_of (nodes.begin (), nodes.end (), [](std::shared_ptr<rai::node> const & node_a) { return node_a->bootstrap_initiator.in_progress (); }))
{
poll ();
++iterations1;
@ -45,18 +46,18 @@ work (1, nullptr)
rai::system::~system ()
{
for (auto & i: nodes)
for (auto & i : nodes)
{
i->stop ();
}
}
std::shared_ptr <rai::wallet> rai::system::wallet (size_t index_a)
std::shared_ptr<rai::wallet> rai::system::wallet (size_t index_a)
{
assert (nodes.size () > index_a);
auto size (nodes [index_a]->wallets.items.size ());
auto size (nodes[index_a]->wallets.items.size ());
assert (size == 1);
return nodes [index_a]->wallets.items.begin ()->second;
return nodes[index_a]->wallets.items.begin ()->second;
}
rai::account rai::system::account (MDB_txn * transaction_a, size_t index_a)
@ -78,13 +79,12 @@ void rai::system::poll ()
}
}
namespace
{
class traffic_generator : public std::enable_shared_from_this <traffic_generator>
class traffic_generator : public std::enable_shared_from_this<traffic_generator>
{
public:
traffic_generator (uint32_t count_a, uint32_t wait_a, std::shared_ptr <rai::node> node_a, rai::system & system_a) :
traffic_generator (uint32_t count_a, uint32_t wait_a, std::shared_ptr<rai::node> node_a, rai::system & system_a) :
count (count_a),
wait (wait_a),
node (node_a),
@ -99,13 +99,13 @@ public:
if (count_l > 0)
{
auto this_l (shared_from_this ());
node->alarm.add (std::chrono::system_clock::now () + std::chrono::milliseconds (wait), [this_l] () {this_l->run ();});
node->alarm.add (std::chrono::system_clock::now () + std::chrono::milliseconds (wait), [this_l]() { this_l->run (); });
}
}
std::vector <rai::account> accounts;
std::vector<rai::account> accounts;
uint32_t count;
uint32_t wait;
std::shared_ptr <rai::node> node;
std::shared_ptr<rai::node> node;
rai::system & system;
};
}
@ -122,11 +122,11 @@ void rai::system::generate_usage_traffic (uint32_t count_a, uint32_t wait_a, siz
{
assert (nodes.size () > index_a);
assert (count_a > 0);
auto generate (std::make_shared <traffic_generator> (count_a, wait_a, nodes [index_a], *this));
auto generate (std::make_shared<traffic_generator> (count_a, wait_a, nodes[index_a], *this));
generate->run ();
}
void rai::system::generate_rollback (rai::node & node_a, std::vector <rai::account> & accounts_a)
void rai::system::generate_rollback (rai::node & node_a, std::vector<rai::account> & accounts_a)
{
rai::block_hash current (node_a.latest (get_random_account (accounts_a)));
rai::block_hash target (current);
@ -144,7 +144,7 @@ void rai::system::generate_rollback (rai::node & node_a, std::vector <rai::accou
auto block2 (node_a.store.block_get (transaction, current));
current = block2->previous ();
}
auto open (dynamic_cast <rai::open_block *> (block2.get()));
auto open (dynamic_cast<rai::open_block *> (block2.get ()));
if (open != nullptr)
{
if (!node_a.ledger.block_exists (open->hashables.source))
@ -161,7 +161,7 @@ void rai::system::generate_rollback (rai::node & node_a, std::vector <rai::accou
void rai::system::generate_receive (rai::node & node_a)
{
std::shared_ptr <rai::send_block> send_block;
std::shared_ptr<rai::send_block> send_block;
{
rai::transaction transaction (node_a.store.environment, nullptr, false);
rai::uint256_union random_block;
@ -172,18 +172,18 @@ void rai::system::generate_receive (rai::node & node_a)
rai::pending_key send_hash (i->first);
rai::pending_info info (i->second);
auto block (node_a.store.block_get (transaction, send_hash.hash));
assert (dynamic_cast <rai::send_block *> (block.get ()) != nullptr);
send_block.reset (static_cast <rai::send_block *> (block.release ()));
assert (dynamic_cast<rai::send_block *> (block.get ()) != nullptr);
send_block.reset (static_cast<rai::send_block *> (block.release ()));
}
}
if (send_block != nullptr)
{
auto receive_error (wallet (0)->receive_sync (std::move (send_block), rai::genesis_account, std::numeric_limits<rai::uint128_t>::max ()));
(void) receive_error;
(void)receive_error;
}
}
void rai::system::generate_activity (rai::node & node_a, std::vector <rai::account> & accounts_a)
void rai::system::generate_activity (rai::node & node_a, std::vector<rai::account> & accounts_a)
{
auto what (random_pool.GenerateByte ());
if (what < 0x10)
@ -212,25 +212,25 @@ void rai::system::generate_activity (rai::node & node_a, std::vector <rai::accou
}
}
rai::account rai::system::get_random_account (std::vector <rai::account> & accounts_a)
rai::account rai::system::get_random_account (std::vector<rai::account> & accounts_a)
{
auto index (random_pool.GenerateWord32 (0, accounts_a.size () - 1));
auto result (accounts_a [index]);
auto result (accounts_a[index]);
return result;
}
rai::uint128_t rai::system::get_random_amount (MDB_txn * transaction_a, rai::node & node_a, rai::account const & account_a)
{
rai::uint128_t balance (node_a.ledger.account_balance (transaction_a, account_a));
std::string balance_text (balance.convert_to <std::string> ());
std::string balance_text (balance.convert_to<std::string> ());
rai::uint128_union random_amount;
random_pool.GenerateBlock (random_amount.bytes.data (), sizeof (random_amount.bytes));
auto result (((rai::uint256_t {random_amount.number ()} * balance) / rai::uint256_t {std::numeric_limits <rai::uint128_t>::max ()}).convert_to <rai::uint128_t> ());
std::string text (result.convert_to <std::string> ());
auto result (((rai::uint256_t{ random_amount.number () } * balance) / rai::uint256_t{ std::numeric_limits<rai::uint128_t>::max () }).convert_to<rai::uint128_t> ());
std::string text (result.convert_to<std::string> ());
return result;
}
void rai::system::generate_send_existing (rai::node & node_a, std::vector <rai::account> & accounts_a)
void rai::system::generate_send_existing (rai::node & node_a, std::vector<rai::account> & accounts_a)
{
rai::uint128_t amount;
rai::account destination;
@ -256,7 +256,7 @@ void rai::system::generate_send_existing (rai::node & node_a, std::vector <rai::
}
}
void rai::system::generate_change_known (rai::node & node_a, std::vector <rai::account> & accounts_a)
void rai::system::generate_change_known (rai::node & node_a, std::vector<rai::account> & accounts_a)
{
rai::account source (get_random_account (accounts_a));
if (!node_a.latest (source).is_zero ())
@ -267,7 +267,7 @@ void rai::system::generate_change_known (rai::node & node_a, std::vector <rai::a
}
}
void rai::system::generate_change_unknown (rai::node & node_a, std::vector <rai::account> & accounts_a)
void rai::system::generate_change_unknown (rai::node & node_a, std::vector<rai::account> & accounts_a)
{
rai::account source (get_random_account (accounts_a));
if (!node_a.latest (source).is_zero ())
@ -279,7 +279,7 @@ void rai::system::generate_change_unknown (rai::node & node_a, std::vector <rai:
}
}
void rai::system::generate_send_new (rai::node & node_a, std::vector <rai::account> & accounts_a)
void rai::system::generate_send_new (rai::node & node_a, std::vector<rai::account> & accounts_a)
{
assert (node_a.wallets.items.size () == 1);
rai::uint128_t amount;
@ -300,7 +300,7 @@ void rai::system::generate_send_new (rai::node & node_a, std::vector <rai::accou
void rai::system::generate_mass_activity (uint32_t count_a, rai::node & node_a)
{
std::vector <rai::account> accounts;
std::vector<rai::account> accounts;
wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
accounts.push_back (rai::test_genesis_key.pub);
auto previous (std::chrono::system_clock::now ());
@ -309,7 +309,7 @@ void rai::system::generate_mass_activity (uint32_t count_a, rai::node & node_a)
if ((i & 0xfff) == 0)
{
auto now (std::chrono::system_clock::now ());
auto us (std::chrono::duration_cast <std::chrono::microseconds> (now - previous).count ());
auto us (std::chrono::duration_cast<std::chrono::microseconds> (now - previous).count ());
std::cerr << boost::str (boost::format ("Mass activity iteration %1% us %2% us/t %3%\n") % i % us % (us / 256));
previous = now;
}
@ -350,10 +350,10 @@ bool rai::landing_store::deserialize (std::istream & stream_a)
{
boost::property_tree::ptree tree;
boost::property_tree::read_json (stream_a, tree);
auto source_l (tree.get <std::string> ("source"));
auto destination_l (tree.get <std::string> ("destination"));
auto start_l (tree.get <std::string> ("start"));
auto last_l (tree.get <std::string> ("last"));
auto source_l (tree.get<std::string> ("source"));
auto destination_l (tree.get<std::string> ("destination"));
auto start_l (tree.get<std::string> ("start"));
auto last_l (tree.get<std::string> ("last"));
result = source.decode_account (source_l);
if (!result)
{
@ -386,12 +386,12 @@ void rai::landing_store::serialize (std::ostream & stream_a) const
boost::property_tree::write_json (stream_a, tree);
}
bool rai::landing_store::operator == (rai::landing_store const & other_a) const
bool rai::landing_store::operator== (rai::landing_store const & other_a) const
{
return source == other_a.source && destination == other_a.destination && start == other_a.start && last == other_a.last;
}
rai::landing::landing (rai::node & node_a, std::shared_ptr <rai::wallet> wallet_a, rai::landing_store & store_a, boost::filesystem::path const & path_a) :
rai::landing::landing (rai::node & node_a, std::shared_ptr<rai::wallet> wallet_a, rai::landing_store & store_a, boost::filesystem::path const & path_a) :
path (path_a),
store (store_a),
wallet (wallet_a),
@ -464,7 +464,7 @@ rai::uint128_t rai::landing::distribution_amount (uint64_t interval)
uint64_t rai::landing::seconds_since_epoch ()
{
return std::chrono::duration_cast <std::chrono::seconds> (std::chrono::system_clock::now ().time_since_epoch ()).count ();
return std::chrono::duration_cast<std::chrono::seconds> (std::chrono::system_clock::now ().time_since_epoch ()).count ();
}
void rai::landing::distribute_one ()
@ -492,9 +492,8 @@ void rai::landing::distribute_ongoing ()
{
distribute_one ();
BOOST_LOG (node.log) << "Waiting for next distribution cycle";
node.alarm.add (std::chrono::system_clock::now () + sleep_seconds, [this] () {distribute_ongoing ();});
node.alarm.add (std::chrono::system_clock::now () + sleep_seconds, [this]() { distribute_ongoing (); });
}
std::chrono::seconds constexpr rai::landing::distribution_interval;
std::chrono::seconds constexpr rai::landing::sleep_seconds;

View file

@ -9,25 +9,25 @@ class system
public:
system (uint16_t, size_t);
~system ();
void generate_activity (rai::node &, std::vector <rai::account> &);
void generate_activity (rai::node &, std::vector<rai::account> &);
void generate_mass_activity (uint32_t, rai::node &);
void generate_usage_traffic (uint32_t, uint32_t, size_t);
void generate_usage_traffic (uint32_t, uint32_t);
rai::account get_random_account (std::vector <rai::account> &);
rai::account get_random_account (std::vector<rai::account> &);
rai::uint128_t get_random_amount (MDB_txn *, rai::node &, rai::account const &);
void generate_rollback (rai::node &, std::vector <rai::account> &);
void generate_change_known (rai::node &, std::vector <rai::account> &);
void generate_change_unknown (rai::node &, std::vector <rai::account> &);
void generate_rollback (rai::node &, std::vector<rai::account> &);
void generate_change_known (rai::node &, std::vector<rai::account> &);
void generate_change_unknown (rai::node &, std::vector<rai::account> &);
void generate_receive (rai::node &);
void generate_send_new (rai::node &, std::vector <rai::account> &);
void generate_send_existing (rai::node &, std::vector <rai::account> &);
std::shared_ptr <rai::wallet> wallet (size_t);
void generate_send_new (rai::node &, std::vector<rai::account> &);
void generate_send_existing (rai::node &, std::vector<rai::account> &);
std::shared_ptr<rai::wallet> wallet (size_t);
rai::account account (MDB_txn *, size_t);
void poll ();
void stop ();
boost::asio::io_service service;
rai::alarm alarm;
std::vector <std::shared_ptr <rai::node>> nodes;
std::vector<std::shared_ptr<rai::node>> nodes;
rai::logging logging;
rai::work_pool work;
};
@ -43,12 +43,12 @@ public:
uint64_t last;
bool deserialize (std::istream &);
void serialize (std::ostream &) const;
bool operator == (rai::landing_store const &) const;
bool operator== (rai::landing_store const &) const;
};
class landing
{
public:
landing (rai::node &, std::shared_ptr <rai::wallet>, rai::landing_store &, boost::filesystem::path const &);
landing (rai::node &, std::shared_ptr<rai::wallet>, rai::landing_store &, boost::filesystem::path const &);
void write_store ();
rai::uint128_t distribution_amount (uint64_t);
uint64_t seconds_since_epoch ();
@ -56,7 +56,7 @@ public:
void distribute_ongoing ();
boost::filesystem::path path;
rai::landing_store & store;
std::shared_ptr <rai::wallet> wallet;
std::shared_ptr<rai::wallet> wallet;
rai::node & node;
static int constexpr interval_exponent = 10;
static std::chrono::seconds constexpr distribution_interval = std::chrono::seconds (1 << interval_exponent); // 1024 seconds

View file

@ -58,7 +58,7 @@ rai::mdb_env::operator MDB_env * () const
}
rai::mdb_val::mdb_val () :
value ({0, nullptr})
value ({ 0, nullptr })
{
}
@ -68,17 +68,17 @@ value (value_a)
}
rai::mdb_val::mdb_val (size_t size_a, void * data_a) :
value ({size_a, data_a})
value ({ size_a, data_a })
{
}
rai::mdb_val::mdb_val (rai::uint128_union const & val_a) :
mdb_val (sizeof (val_a), const_cast <rai::uint128_union *> (&val_a))
mdb_val (sizeof (val_a), const_cast<rai::uint128_union *> (&val_a))
{
}
rai::mdb_val::mdb_val (rai::uint256_union const & val_a) :
mdb_val (sizeof (val_a), const_cast <rai::uint256_union *> (&val_a))
mdb_val (sizeof (val_a), const_cast<rai::uint256_union *> (&val_a))
{
}
@ -96,14 +96,14 @@ rai::uint256_union rai::mdb_val::uint256 () const
{
rai::uint256_union result;
assert (size () == sizeof (result));
std::copy (reinterpret_cast <uint8_t const *> (data ()), reinterpret_cast <uint8_t const *> (data ()) + sizeof (result), result.bytes.data ());
std::copy (reinterpret_cast<uint8_t const *> (data ()), reinterpret_cast<uint8_t const *> (data ()) + sizeof (result), result.bytes.data ());
return result;
}
rai::mdb_val::operator MDB_val * () const
{
// Allow passing a temporary to a non-c++ function which doesn't have constness
return const_cast <MDB_val *> (&value);
return const_cast<MDB_val *> (&value);
};
rai::mdb_val::operator MDB_val const & () const

10
rai/node/utility.hpp Executable file → Normal file
View file

@ -5,9 +5,9 @@
#include <condition_variable>
#include <type_traits>
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/filesystem.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <cryptopp/osrng.h>
@ -20,8 +20,8 @@
namespace rai
{
using bufferstream = boost::iostreams::stream_buffer <boost::iostreams::basic_array_source <uint8_t>>;
using vectorstream = boost::iostreams::stream_buffer <boost::iostreams::back_insert_device <std::vector <uint8_t>>>;
using bufferstream = boost::iostreams::stream_buffer<boost::iostreams::basic_array_source<uint8_t>>;
using vectorstream = boost::iostreams::stream_buffer<boost::iostreams::back_insert_device<std::vector<uint8_t>>>;
// OS-specific way of finding a path to a home directory.
boost::filesystem::path working_path ();
// Get a unique path within the home directory, used for testing
@ -43,7 +43,7 @@ bool fetch_object (T & object, std::iostream & stream_a)
catch (std::runtime_error const &)
{
auto pos (stream_a.tellg ());
if (pos != std::streampos(0))
if (pos != std::streampos (0))
{
error = true;
}
@ -71,7 +71,7 @@ bool fetch_object (T & object, boost::filesystem::path const & path_a, std::fstr
catch (std::runtime_error const &)
{
auto pos (stream_a.tellg ());
if (pos != std::streampos(0))
if (pos != std::streampos (0))
{
error = true;
}

View file

@ -6,8 +6,8 @@
#include <argon2.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <future>
@ -30,7 +30,7 @@ void rai::wallet_store::wallet_key (rai::raw_key & prv_a, MDB_txn * transaction_
rai::wallet_value value (entry_get_raw (transaction_a, rai::wallet_store::wallet_key_special));
rai::raw_key password_l;
password.value (password_l);
prv_a.decrypt (value.key, password_l, salt (transaction_a).owords [0]);
prv_a.decrypt (value.key, password_l, salt (transaction_a).owords[0]);
}
void rai::wallet_store::seed (rai::raw_key & prv_a, MDB_txn * transaction_a)
@ -38,7 +38,7 @@ void rai::wallet_store::seed (rai::raw_key & prv_a, MDB_txn * transaction_a)
rai::wallet_value value (entry_get_raw (transaction_a, rai::wallet_store::seed_special));
rai::raw_key password_l;
wallet_key (password_l, transaction_a);
prv_a.decrypt (value.key, password_l, salt (transaction_a).owords [0]);
prv_a.decrypt (value.key, password_l, salt (transaction_a).owords[0]);
}
void rai::wallet_store::seed_set (MDB_txn * transaction_a, rai::raw_key const & prv_a)
@ -46,7 +46,7 @@ void rai::wallet_store::seed_set (MDB_txn * transaction_a, rai::raw_key const &
rai::raw_key password_l;
wallet_key (password_l, transaction_a);
rai::uint256_union ciphertext;
ciphertext.encrypt (prv_a, password_l, salt (transaction_a).owords [0]);
ciphertext.encrypt (prv_a, password_l, salt (transaction_a).owords[0]);
entry_put_raw (transaction_a, rai::wallet_store::seed_special, rai::wallet_value (ciphertext, 0));
deterministic_clear (transaction_a);
}
@ -84,7 +84,7 @@ void rai::wallet_store::deterministic_key (rai::raw_key & prv_a, MDB_txn * trans
uint32_t rai::wallet_store::deterministic_index_get (MDB_txn * transaction_a)
{
rai::wallet_value value (entry_get_raw (transaction_a, rai::wallet_store::deterministic_index_special));
return value.key.number ().convert_to <uint32_t> ();
return value.key.number ().convert_to<uint32_t> ();
}
void rai::wallet_store::deterministic_index_set (MDB_txn * transaction_a, uint32_t index_a)
@ -101,18 +101,18 @@ void rai::wallet_store::deterministic_clear (MDB_txn * transaction_a)
{
switch (key_type (rai::wallet_value (i->second)))
{
case rai::key_type::deterministic:
{
rai::uint256_union key (i->first.uint256 ());
erase (transaction_a, key);
i = begin (transaction_a, key);
break;
}
default:
{
++i;
break;
}
case rai::key_type::deterministic:
{
rai::uint256_union key (i->first.uint256 ());
erase (transaction_a, key);
i = begin (transaction_a, key);
break;
}
default:
{
++i;
break;
}
}
}
deterministic_index_set (transaction_a, 0);
@ -125,7 +125,7 @@ bool rai::wallet_store::valid_password (MDB_txn * transaction_a)
rai::raw_key wallet_key_l;
wallet_key (wallet_key_l, transaction_a);
rai::uint256_union check_l;
check_l.encrypt (zero, wallet_key_l, salt (transaction_a).owords [0]);
check_l.encrypt (zero, wallet_key_l, salt (transaction_a).owords[0]);
return check (transaction_a) == check_l;
}
@ -162,7 +162,7 @@ bool rai::wallet_store::rekey (MDB_txn * transaction_a, std::string const & pass
password.value (password_l);
password.value_set (password_new);
rai::uint256_union encrypted;
encrypted.encrypt (wallet_key_l, password_new, salt (transaction_a).owords [0]);
encrypted.encrypt (wallet_key_l, password_new, salt (transaction_a).owords[0]);
entry_put_raw (transaction_a, rai::wallet_store::wallet_key_special, rai::wallet_value (encrypted, 0));
}
else
@ -180,10 +180,10 @@ void rai::wallet_store::derive_key (rai::raw_key & prv_a, MDB_txn * transaction_
rai::fan::fan (rai::uint256_union const & key, size_t count_a)
{
std::unique_ptr <rai::uint256_union> first (new rai::uint256_union (key));
std::unique_ptr<rai::uint256_union> first (new rai::uint256_union (key));
for (auto i (1); i < count_a; ++i)
{
std::unique_ptr <rai::uint256_union> entry (new rai::uint256_union);
std::unique_ptr<rai::uint256_union> entry (new rai::uint256_union);
random_pool.GenerateBlock (entry->bytes.data (), entry->bytes.size ());
*first ^= *entry;
values.push_back (std::move (entry));
@ -193,7 +193,7 @@ rai::fan::fan (rai::uint256_union const & key, size_t count_a)
void rai::fan::value (rai::raw_key & prv_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
value_get (prv_a);
}
@ -201,7 +201,7 @@ void rai::fan::value_get (rai::raw_key & prv_a)
{
assert (!mutex.try_lock ());
prv_a.data.clear ();
for (auto & i: values)
for (auto & i : values)
{
prv_a.data ^= *i;
}
@ -209,18 +209,18 @@ void rai::fan::value_get (rai::raw_key & prv_a)
void rai::fan::value_set (rai::raw_key const & value_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
rai::raw_key value_l;
value_get (value_l);
*(values [0]) ^= value_l.data;
*(values [0]) ^= value_a.data;
*(values[0]) ^= value_l.data;
*(values[0]) ^= value_a.data;
}
rai::wallet_value::wallet_value (rai::mdb_val const & val_a)
{
assert (val_a.size () == sizeof (*this));
std::copy (reinterpret_cast <uint8_t const *> (val_a.data ()), reinterpret_cast <uint8_t const *> (val_a.data ()) + sizeof (key), key.chars.begin ());
std::copy (reinterpret_cast <uint8_t const *> (val_a.data ()) + sizeof (key), reinterpret_cast <uint8_t const *> (val_a.data ()) + sizeof (key) + sizeof (work), reinterpret_cast <char *> (&work));
std::copy (reinterpret_cast<uint8_t const *> (val_a.data ()), reinterpret_cast<uint8_t const *> (val_a.data ()) + sizeof (key), key.chars.begin ());
std::copy (reinterpret_cast<uint8_t const *> (val_a.data ()) + sizeof (key), reinterpret_cast<uint8_t const *> (val_a.data ()) + sizeof (key) + sizeof (work), reinterpret_cast<char *> (&work));
}
rai::wallet_value::wallet_value (rai::uint256_union const & key_a, uint64_t work_a) :
@ -232,7 +232,7 @@ work (work_a)
rai::mdb_val rai::wallet_value::val () const
{
static_assert (sizeof (*this) == sizeof (key) + sizeof (work), "Class not packed");
return rai::mdb_val (sizeof (*this), const_cast <rai::wallet_value *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::wallet_value *> (this));
}
unsigned const rai::wallet_store::version_1 (1);
@ -283,7 +283,7 @@ environment (transaction_a.environment)
if (!init_a)
{
rai::uint256_union value;
init_a = value.decode_hex (wallet_l.get <std::string> (i->first));
init_a = value.decode_hex (wallet_l.get<std::string> (i->first));
if (!init_a)
{
entry_put_raw (transaction_a, key, rai::wallet_value (value, 0));
@ -299,12 +299,12 @@ environment (transaction_a.environment)
}
}
init_a |= mdb_get (transaction_a, handle, rai::mdb_val (version_special), &junk) != 0;
init_a |= mdb_get (transaction_a, handle, rai::mdb_val (wallet_key_special), & junk) != 0;
init_a |= mdb_get (transaction_a, handle, rai::mdb_val (wallet_key_special), &junk) != 0;
init_a |= mdb_get (transaction_a, handle, rai::mdb_val (salt_special), &junk) != 0;
init_a |= mdb_get (transaction_a, handle, rai::mdb_val (check_special), &junk) != 0;
init_a |= mdb_get (transaction_a, handle, rai::mdb_val (representative_special), &junk) != 0;
rai::raw_key key;
key.data.clear();
key.data.clear ();
password.value_set (key);
}
}
@ -337,10 +337,10 @@ environment (transaction_a.environment)
zero.data.clear ();
// Wallet key is encrypted by the user's password
rai::uint256_union encrypted;
encrypted.encrypt (wallet_key, zero, salt_l.owords [0]);
encrypted.encrypt (wallet_key, zero, salt_l.owords[0]);
entry_put_raw (transaction_a, rai::wallet_store::wallet_key_special, rai::wallet_value (encrypted, 0));
rai::uint256_union check;
check.encrypt (zero, wallet_key, salt_l.owords [0]);
check.encrypt (zero, wallet_key, salt_l.owords[0]);
entry_put_raw (transaction_a, rai::wallet_store::check_special, rai::wallet_value (check, 0));
entry_put_raw (transaction_a, rai::wallet_store::representative_special, rai::wallet_value (representative_a, 0));
rai::raw_key seed;
@ -351,9 +351,9 @@ environment (transaction_a.environment)
}
}
std::vector <rai::account> rai::wallet_store::accounts (MDB_txn * transaction_a)
std::vector<rai::account> rai::wallet_store::accounts (MDB_txn * transaction_a)
{
std::vector <rai::account> result;
std::vector<rai::account> result;
for (auto i (begin (transaction_a)), n (end ()); i != n; ++i)
{
rai::account account (i->first.uint256 ());
@ -393,7 +393,7 @@ rai::public_key rai::wallet_store::insert_adhoc (MDB_txn * transaction_a, rai::r
rai::raw_key password_l;
wallet_key (password_l, transaction_a);
rai::uint256_union ciphertext;
ciphertext.encrypt (prv, password_l, salt (transaction_a).owords [0]);
ciphertext.encrypt (prv, password_l, salt (transaction_a).owords[0]);
entry_put_raw (transaction_a, pub, rai::wallet_value (ciphertext, 0));
return pub;
}
@ -431,14 +431,14 @@ rai::key_type rai::wallet_store::key_type (rai::wallet_value const & value_a)
{
auto number (value_a.key.number ());
rai::key_type result;
auto text (number.convert_to <std::string> ());
if (number > std::numeric_limits <uint64_t>::max ())
auto text (number.convert_to<std::string> ());
if (number > std::numeric_limits<uint64_t>::max ())
{
result = rai::key_type::adhoc;
}
else
{
if ((number >> 32).convert_to <uint32_t> () == 1)
if ((number >> 32).convert_to<uint32_t> () == 1)
{
result = rai::key_type::deterministic;
}
@ -460,27 +460,27 @@ bool rai::wallet_store::fetch (MDB_txn * transaction_a, rai::public_key const &
{
switch (key_type (value))
{
case rai::key_type::deterministic:
{
rai::raw_key seed_l;
seed (seed_l, transaction_a);
uint32_t index (value.key.number ().convert_to <uint32_t> ());
deterministic_key (prv, transaction_a, index);
break;
}
case rai::key_type::adhoc:
{
// Ad-hoc keys
rai::raw_key password_l;
wallet_key (password_l, transaction_a);
prv.decrypt (value.key, password_l, salt (transaction_a).owords [0]);
break;
}
default:
{
result = true;
break;
}
case rai::key_type::deterministic:
{
rai::raw_key seed_l;
seed (seed_l, transaction_a);
uint32_t index (value.key.number ().convert_to<uint32_t> ());
deterministic_key (prv, transaction_a, index);
break;
}
case rai::key_type::adhoc:
{
// Ad-hoc keys
rai::raw_key password_l;
wallet_key (password_l, transaction_a);
prv.decrypt (value.key, password_l, salt (transaction_a).owords[0]);
break;
}
default:
{
result = true;
break;
}
}
}
else
@ -533,7 +533,7 @@ void rai::wallet_store::write_backup (MDB_txn * transaction_a, boost::filesystem
}
}
bool rai::wallet_store::move (MDB_txn * transaction_a, rai::wallet_store & other_a, std::vector <rai::public_key> const & keys)
bool rai::wallet_store::move (MDB_txn * transaction_a, rai::wallet_store & other_a, std::vector<rai::public_key> const & keys)
{
assert (valid_password (transaction_a));
assert (other_a.valid_password (transaction_a));
@ -598,7 +598,7 @@ unsigned rai::wallet_store::version (MDB_txn * transaction_a)
{
rai::wallet_value value (entry_get_raw (transaction_a, rai::wallet_store::version_special));
auto entry (value.key);
auto result (static_cast <unsigned> (entry.bytes [31]));
auto result (static_cast<unsigned> (entry.bytes[31]));
return result;
}
@ -616,10 +616,10 @@ void rai::wallet_store::upgrade_v1_v2 ()
rai::wallet_value value (entry_get_raw (transaction, rai::wallet_store::wallet_key_special));
rai::raw_key kdf;
kdf.data.clear ();
zero_password.decrypt (value.key, kdf, salt (transaction).owords [0]);
zero_password.decrypt (value.key, kdf, salt (transaction).owords[0]);
derive_key (kdf, transaction, "");
rai::raw_key empty_password;
empty_password.decrypt (value.key, kdf, salt (transaction).owords [0]);
empty_password.decrypt (value.key, kdf, salt (transaction).owords[0]);
for (auto i (begin (transaction)), n (end ()); i != n; ++i)
{
rai::public_key key (i->first.uint256 ());
@ -628,7 +628,7 @@ void rai::wallet_store::upgrade_v1_v2 ()
{
// Key failed to decrypt despite valid password
rai::wallet_value data (entry_get_raw (transaction, key));
prv.decrypt (data.key, zero_password, salt (transaction).owords [0]);
prv.decrypt (data.key, zero_password, salt (transaction).owords[0]);
rai::public_key compare;
ed25519_publickey (prv.data.bytes.data (), compare.bytes.data ());
if (compare == key)
@ -640,7 +640,7 @@ void rai::wallet_store::upgrade_v1_v2 ()
{
// Also try the empty password
rai::wallet_value data (entry_get_raw (transaction, key));
prv.decrypt (data.key, empty_password, salt (transaction).owords [0]);
prv.decrypt (data.key, empty_password, salt (transaction).owords[0]);
rai::public_key compare;
ed25519_publickey (prv.data.bytes.data (), compare.bytes.data ());
if (compare == key)
@ -667,20 +667,21 @@ void rai::wallet_store::upgrade_v2_v3 ()
void rai::kdf::phs (rai::raw_key & result_a, std::string const & password_a, rai::uint256_union const & salt_a)
{
std::lock_guard <std::mutex> lock (mutex);
std::lock_guard<std::mutex> lock (mutex);
auto success (argon2_hash (1, rai::wallet_store::kdf_work, 1, password_a.data (), password_a.size (), salt_a.bytes.data (), salt_a.bytes.size (), result_a.data.bytes.data (), result_a.data.bytes.size (), NULL, 0, Argon2_d, 0x10));
assert (success == 0); (void) success;
assert (success == 0);
(void)success;
}
rai::wallet::wallet (bool & init_a, rai::transaction & transaction_a, rai::node & node_a, std::string const & wallet_a) :
lock_observer ([](bool, bool){}),
lock_observer ([](bool, bool) {}),
store (init_a, node_a.wallets.kdf, transaction_a, node_a.config.random_representative (), node_a.config.password_fanout, wallet_a),
node (node_a)
{
}
rai::wallet::wallet (bool & init_a, rai::transaction & transaction_a, rai::node & node_a, std::string const & wallet_a, std::string const & json) :
lock_observer ([](bool, bool){}),
lock_observer ([](bool, bool) {}),
store (init_a, node_a.wallets.kdf, transaction_a, node_a.config.random_representative (), node_a.config.password_fanout, wallet_a, json),
node (node_a)
{
@ -716,12 +717,11 @@ bool rai::wallet::enter_password (std::string const & password_a)
if (!result)
{
auto this_l (shared_from_this ());
node.background([this_l] ()
{
node.background ([this_l]() {
this_l->search_pending ();
});
}
lock_observer (result, password_a.empty());
lock_observer (result, password_a.empty ());
return result;
}
@ -776,7 +776,7 @@ bool rai::wallet::exists (rai::public_key const & account_a)
bool rai::wallet::import (std::string const & json_a, std::string const & password_a)
{
auto error (false);
std::unique_ptr <rai::wallet_store> temp;
std::unique_ptr<rai::wallet_store> temp;
{
rai::transaction transaction (store.environment, nullptr, true);
rai::uint256_union id;
@ -811,16 +811,17 @@ void rai::wallet_store::destroy (MDB_txn * transaction_a)
namespace
{
bool check_ownership (rai::wallets & wallets_a, rai::account const & account_a) {
std::lock_guard <std::mutex> lock (wallets_a.action_mutex);
bool check_ownership (rai::wallets & wallets_a, rai::account const & account_a)
{
std::lock_guard<std::mutex> lock (wallets_a.action_mutex);
return wallets_a.current_actions.find (account_a) == wallets_a.current_actions.end ();
}
}
std::shared_ptr <rai::block> rai::wallet::receive_action (rai::send_block const & send_a, rai::account const & representative_a, rai::uint128_union const & amount_a, bool generate_work_a)
std::shared_ptr<rai::block> rai::wallet::receive_action (rai::send_block const & send_a, rai::account const & representative_a, rai::uint128_union const & amount_a, bool generate_work_a)
{
auto hash (send_a.hash ());
std::shared_ptr <rai::block> block;
std::shared_ptr<rai::block> block;
if (node.config.receive_minimum.number () <= amount_a.number ())
{
rai::transaction transaction (node.ledger.store.environment, nullptr, false);
@ -866,8 +867,7 @@ std::shared_ptr <rai::block> rai::wallet::receive_action (rai::send_block const
auto hash (block->hash ());
auto this_l (shared_from_this ());
auto source (send_a.hashables.destination);
node.wallets.queue_wallet_action (source, rai::wallets::generate_priority, [this_l, source, hash]
{
node.wallets.queue_wallet_action (source, rai::wallets::generate_priority, [this_l, source, hash] {
this_l->work_generate (source, hash);
});
}
@ -875,9 +875,9 @@ std::shared_ptr <rai::block> rai::wallet::receive_action (rai::send_block const
return block;
}
std::shared_ptr <rai::block> rai::wallet::change_action (rai::account const & source_a, rai::account const & representative_a, bool generate_work_a)
std::shared_ptr<rai::block> rai::wallet::change_action (rai::account const & source_a, rai::account const & representative_a, bool generate_work_a)
{
std::shared_ptr <rai::block> block;
std::shared_ptr<rai::block> block;
{
rai::transaction transaction (store.environment, nullptr, false);
if (store.valid_password (transaction))
@ -907,8 +907,7 @@ std::shared_ptr <rai::block> rai::wallet::change_action (rai::account const & so
{
auto hash (block->hash ());
auto this_l (shared_from_this ());
node.wallets.queue_wallet_action (source_a, rai::wallets::generate_priority, [this_l, source_a, hash]
{
node.wallets.queue_wallet_action (source_a, rai::wallets::generate_priority, [this_l, source_a, hash] {
this_l->work_generate (source_a, hash);
});
}
@ -916,9 +915,9 @@ std::shared_ptr <rai::block> rai::wallet::change_action (rai::account const & so
return block;
}
std::shared_ptr <rai::block> rai::wallet::send_action (rai::account const & source_a, rai::account const & account_a, rai::uint128_t const & amount_a, bool generate_work_a)
std::shared_ptr<rai::block> rai::wallet::send_action (rai::account const & source_a, rai::account const & account_a, rai::uint128_t const & amount_a, bool generate_work_a)
{
std::shared_ptr <rai::block> block;
std::shared_ptr<rai::block> block;
{
rai::transaction transaction (store.environment, nullptr, false);
if (store.valid_password (transaction))
@ -952,8 +951,7 @@ std::shared_ptr <rai::block> rai::wallet::send_action (rai::account const & sour
{
auto hash (block->hash ());
auto this_l (shared_from_this ());
node.wallets.queue_wallet_action (source_a, rai::wallets::generate_priority, [this_l, source_a, hash]
{
node.wallets.queue_wallet_action (source_a, rai::wallets::generate_priority, [this_l, source_a, hash] {
this_l->work_generate (source_a, hash);
});
}
@ -963,61 +961,57 @@ std::shared_ptr <rai::block> rai::wallet::send_action (rai::account const & sour
bool rai::wallet::change_sync (rai::account const & source_a, rai::account const & representative_a)
{
std::promise <bool> result;
change_async (source_a, representative_a, [this, source_a, representative_a, &result] (std::shared_ptr <rai::block> block_a)
{
std::promise<bool> result;
change_async (source_a, representative_a, [this, source_a, representative_a, &result](std::shared_ptr<rai::block> block_a) {
result.set_value (block_a == nullptr);
}, true);
},
true);
return result.get_future ().get ();
}
void rai::wallet::change_async (rai::account const & source_a, rai::account const & representative_a, std::function <void (std::shared_ptr <rai::block>)> const & action_a, bool generate_work_a)
void rai::wallet::change_async (rai::account const & source_a, rai::account const & representative_a, std::function<void(std::shared_ptr<rai::block>)> const & action_a, bool generate_work_a)
{
node.wallets.queue_wallet_action (source_a, rai::wallets::high_priority, [this, source_a, representative_a, action_a, generate_work_a] ()
{
node.wallets.queue_wallet_action (source_a, rai::wallets::high_priority, [this, source_a, representative_a, action_a, generate_work_a]() {
assert (!check_ownership (node.wallets, source_a));
auto block (change_action (source_a, representative_a, generate_work_a));
action_a (block);
});
}
bool rai::wallet::receive_sync (std::shared_ptr <rai::block> block_a, rai::account const & representative_a, rai::uint128_t const & amount_a)
bool rai::wallet::receive_sync (std::shared_ptr<rai::block> block_a, rai::account const & representative_a, rai::uint128_t const & amount_a)
{
std::promise <bool> result;
receive_async (block_a, representative_a, amount_a, [&result] (std::shared_ptr <rai::block> block_a)
{
std::promise<bool> result;
receive_async (block_a, representative_a, amount_a, [&result](std::shared_ptr<rai::block> block_a) {
result.set_value (block_a == nullptr);
}, true);
},
true);
return result.get_future ().get ();
}
void rai::wallet::receive_async (std::shared_ptr <rai::block> block_a, rai::account const & representative_a, rai::uint128_t const & amount_a, std::function <void (std::shared_ptr <rai::block>)> const & action_a, bool generate_work_a)
void rai::wallet::receive_async (std::shared_ptr<rai::block> block_a, rai::account const & representative_a, rai::uint128_t const & amount_a, std::function<void(std::shared_ptr<rai::block>)> const & action_a, bool generate_work_a)
{
assert (dynamic_cast <rai::send_block *> (block_a.get ()) != nullptr);
node.wallets.queue_wallet_action (static_cast <rai::send_block *> (block_a.get ())->hashables.destination, amount_a, [this, block_a, representative_a, amount_a, action_a, generate_work_a] ()
{
assert (!check_ownership (node.wallets, static_cast <rai::send_block *> (block_a.get ())->hashables.destination));
auto block (receive_action (*static_cast <rai::send_block *> (block_a.get ()), representative_a, amount_a, generate_work_a));
assert (dynamic_cast<rai::send_block *> (block_a.get ()) != nullptr);
node.wallets.queue_wallet_action (static_cast<rai::send_block *> (block_a.get ())->hashables.destination, amount_a, [this, block_a, representative_a, amount_a, action_a, generate_work_a]() {
assert (!check_ownership (node.wallets, static_cast<rai::send_block *> (block_a.get ())->hashables.destination));
auto block (receive_action (*static_cast<rai::send_block *> (block_a.get ()), representative_a, amount_a, generate_work_a));
action_a (block);
});
}
rai::block_hash rai::wallet::send_sync (rai::account const & source_a, rai::account const & account_a, rai::uint128_t const & amount_a)
{
std::promise <rai::block_hash> result;
send_async (source_a, account_a, amount_a, [&result] (std::shared_ptr <rai::block> block_a)
{
std::promise<rai::block_hash> result;
send_async (source_a, account_a, amount_a, [&result](std::shared_ptr<rai::block> block_a) {
result.set_value (block_a->hash ());
}, true);
},
true);
return result.get_future ().get ();
}
void rai::wallet::send_async (rai::account const & source_a, rai::account const & account_a, rai::uint128_t const & amount_a, std::function <void (std::shared_ptr <rai::block>)> const & action_a, bool generate_work_a)
void rai::wallet::send_async (rai::account const & source_a, rai::account const & account_a, rai::uint128_t const & amount_a, std::function<void(std::shared_ptr<rai::block>)> const & action_a, bool generate_work_a)
{
node.background ([this, source_a, account_a, amount_a, action_a, generate_work_a] ()
{
this->node.wallets.queue_wallet_action (source_a, rai::wallets::high_priority, [this, source_a, account_a, amount_a, action_a, generate_work_a] ()
{
node.background ([this, source_a, account_a, amount_a, action_a, generate_work_a]() {
this->node.wallets.queue_wallet_action (source_a, rai::wallets::high_priority, [this, source_a, account_a, amount_a, action_a, generate_work_a]() {
assert (!check_ownership (node.wallets, source_a));
auto block (send_action (source_a, account_a, amount_a, generate_work_a));
action_a (block);
@ -1071,7 +1065,7 @@ void rai::wallet::work_ensure (MDB_txn * transaction_a, rai::account const & acc
if (rai::work_validate (root, work))
{
auto this_l (shared_from_this ());
node.background ([this_l, account_a, root] () {
node.background ([this_l, account_a, root]() {
this_l->work_generate (account_a, root);
});
}
@ -1079,10 +1073,10 @@ void rai::wallet::work_ensure (MDB_txn * transaction_a, rai::account const & acc
namespace
{
class search_action : public std::enable_shared_from_this <search_action>
class search_action : public std::enable_shared_from_this<search_action>
{
public:
search_action (std::shared_ptr <rai::wallet> const & wallet_a, MDB_txn * transaction_a) :
search_action (std::shared_ptr<rai::wallet> const & wallet_a, MDB_txn * transaction_a) :
wallet (wallet_a)
{
for (auto i (wallet_a->store.begin (transaction_a)), n (wallet_a->store.end ()); i != n; ++i)
@ -1094,7 +1088,7 @@ public:
{
BOOST_LOG (wallet->node.log) << "Beginning pending block search";
rai::transaction transaction (wallet->node.store.environment, nullptr, false);
std::unordered_set <rai::account> already_searched;
std::unordered_set<rai::account> already_searched;
for (auto i (wallet->node.store.pending_begin (transaction)), n (wallet->node.store.pending_end ()); i != n; ++i)
{
rai::pending_key key (i->first);
@ -1113,12 +1107,10 @@ public:
if (already_searched.find (account) == already_searched.end ())
{
auto this_l (shared_from_this ());
std::shared_ptr <rai::block> block_l (wallet->node.store.block_get (transaction, info.head));
wallet->node.background ([this_l, account, block_l]
{
std::shared_ptr<rai::block> block_l (wallet->node.store.block_get (transaction, info.head));
wallet->node.background ([this_l, account, block_l] {
rai::transaction transaction (this_l->wallet->node.store.environment, nullptr, true);
this_l->wallet->node.active.start (transaction, block_l, [this_l, account] (std::shared_ptr <rai::block>)
{
this_l->wallet->node.active.start (transaction, block_l, [this_l, account](std::shared_ptr<rai::block>) {
// If there were any forks for this account they've been rolled back and we can receive anything remaining from this account
this_l->receive_all (account);
});
@ -1151,17 +1143,17 @@ public:
if (wallet->store.valid_password (transaction))
{
rai::pending_key key (i->first);
std::shared_ptr <rai::block> block (wallet->node.store.block_get (transaction, key.hash));
std::shared_ptr<rai::block> block (wallet->node.store.block_get (transaction, key.hash));
auto wallet_l (wallet);
auto amount (pending.amount.number ());
BOOST_LOG (wallet_l->node.log) << boost::str (boost::format ("Receiving block: %1%") % block->hash ().to_string ());
wallet_l->receive_async (block, representative, amount, [wallet_l, block] (std::shared_ptr <rai::block> block_a)
{
wallet_l->receive_async (block, representative, amount, [wallet_l, block](std::shared_ptr<rai::block> block_a) {
if (block_a == nullptr)
{
BOOST_LOG (wallet_l->node.log) << boost::str (boost::format ("Error receiving block %1%") % block->hash ().to_string ());
}
}, true);
},
true);
}
else
{
@ -1171,8 +1163,8 @@ public:
}
}
}
std::unordered_set <rai::uint256_union> keys;
std::shared_ptr <rai::wallet> wallet;
std::unordered_set<rai::uint256_union> keys;
std::shared_ptr<rai::wallet> wallet;
};
}
@ -1182,9 +1174,8 @@ bool rai::wallet::search_pending ()
auto result (!store.valid_password (transaction));
if (!result)
{
auto search (std::make_shared <search_action> (shared_from_this (), transaction));
node.background ([search] ()
{
auto search (std::make_shared<search_action> (shared_from_this (), transaction));
node.background ([search]() {
search->run ();
});
}
@ -1210,7 +1201,7 @@ void rai::wallet::work_generate (rai::account const & account_a, rai::block_hash
auto work (node.generate_work (root_a));
if (node.config.logging.work_generation_time ())
{
BOOST_LOG (node.log) << "Work generation complete: " << (std::chrono::duration_cast <std::chrono::microseconds> (std::chrono::system_clock::now () - begin).count ()) << " us";
BOOST_LOG (node.log) << "Work generation complete: " << (std::chrono::duration_cast<std::chrono::microseconds> (std::chrono::system_clock::now () - begin).count ()) << " us";
}
rai::transaction transaction (store.environment, nullptr, true);
if (store.exists (transaction, account_a))
@ -1220,7 +1211,7 @@ void rai::wallet::work_generate (rai::account const & account_a, rai::block_hash
}
rai::wallets::wallets (bool & error_a, rai::node & node_a) :
observer ([] (rai::account const &, bool) {}),
observer ([](rai::account const &, bool) {}),
node (node_a)
{
if (!error_a)
@ -1230,21 +1221,20 @@ node (node_a)
assert (status == 0);
std::string beginning (rai::uint256_union (0).to_string ());
std::string end ((rai::uint256_union (rai::uint256_t (0) - rai::uint256_t (1))).to_string ());
for (rai::store_iterator i (transaction, handle, rai::mdb_val (beginning.size (), const_cast <char *> (beginning.c_str ()))), n (transaction, handle, rai::mdb_val (end.size (), const_cast <char *> (end.c_str ()))); i != n; ++i)
for (rai::store_iterator i (transaction, handle, rai::mdb_val (beginning.size (), const_cast<char *> (beginning.c_str ()))), n (transaction, handle, rai::mdb_val (end.size (), const_cast<char *> (end.c_str ()))); i != n; ++i)
{
rai::uint256_union id;
std::string text (reinterpret_cast <char const *> (i->first.data ()), i->first.size ());
std::string text (reinterpret_cast<char const *> (i->first.data ()), i->first.size ());
auto error (id.decode_hex (text));
assert (!error);
assert (items.find (id) == items.end ());
auto wallet (std::make_shared <rai::wallet> (error, transaction, node_a, text));
auto wallet (std::make_shared<rai::wallet> (error, transaction, node_a, text));
if (!error)
{
node_a.background ([wallet] ()
{
node_a.background ([wallet]() {
wallet->enter_initial_password ();
});
items [id] = wallet;
items[id] = wallet;
}
else
{
@ -1254,9 +1244,9 @@ node (node_a)
}
}
std::shared_ptr <rai::wallet> rai::wallets::open (rai::uint256_union const & id_a)
std::shared_ptr<rai::wallet> rai::wallets::open (rai::uint256_union const & id_a)
{
std::shared_ptr <rai::wallet> result;
std::shared_ptr<rai::wallet> result;
auto existing (items.find (id_a));
if (existing != items.end ())
{
@ -1265,21 +1255,20 @@ std::shared_ptr <rai::wallet> rai::wallets::open (rai::uint256_union const & id_
return result;
}
std::shared_ptr <rai::wallet> rai::wallets::create (rai::uint256_union const & id_a)
std::shared_ptr<rai::wallet> rai::wallets::create (rai::uint256_union const & id_a)
{
assert (items.find (id_a) == items.end ());
std::shared_ptr <rai::wallet> result;
std::shared_ptr<rai::wallet> result;
bool error;
{
rai::transaction transaction (node.store.environment, nullptr, true);
result = std::make_shared <rai::wallet> (error, transaction, node, id_a.to_string ());
items [id_a] = result;
result = std::make_shared<rai::wallet> (error, transaction, node, id_a.to_string ());
items[id_a] = result;
result = result;
}
if (!error)
{
node.background ([result] ()
{
node.background ([result]() {
result->enter_initial_password ();
});
}
@ -1301,7 +1290,7 @@ bool rai::wallets::search_pending (rai::uint256_union const & wallet_a)
void rai::wallets::search_pending_all ()
{
for (auto i: items)
for (auto i : items)
{
i.second->search_pending ();
}
@ -1320,7 +1309,7 @@ void rai::wallets::destroy (rai::uint256_union const & id_a)
void rai::wallets::do_wallet_actions (rai::account const & account_a)
{
observer (account_a, true);
std::unique_lock <std::mutex> lock (node.wallets.action_mutex);
std::unique_lock<std::mutex> lock (node.wallets.action_mutex);
auto existing (node.wallets.pending_actions.find (account_a));
while (existing != node.wallets.pending_actions.end ())
{
@ -1329,7 +1318,8 @@ void rai::wallets::do_wallet_actions (rai::account const & account_a)
{
node.wallets.pending_actions.erase (existing);
auto erased (node.wallets.current_actions.erase (account_a));
assert (erased == 1); (void) erased;
assert (erased == 1);
(void)erased;
}
else
{
@ -1345,21 +1335,20 @@ void rai::wallets::do_wallet_actions (rai::account const & account_a)
observer (account_a, false);
}
void rai::wallets::queue_wallet_action (rai::account const & account_a, rai::uint128_t const & amount_a, std::function <void ()> const & action_a)
void rai::wallets::queue_wallet_action (rai::account const & account_a, rai::uint128_t const & amount_a, std::function<void()> const & action_a)
{
std::lock_guard <std::mutex> lock (action_mutex);
pending_actions [account_a].insert (decltype (pending_actions)::mapped_type::value_type (amount_a, std::move (action_a)));
std::lock_guard<std::mutex> lock (action_mutex);
pending_actions[account_a].insert (decltype (pending_actions)::mapped_type::value_type (amount_a, std::move (action_a)));
if (current_actions.insert (account_a).second)
{
auto node_l (node.shared ());
node.background ([node_l, account_a] ()
{
node.background ([node_l, account_a]() {
node_l->wallets.do_wallet_actions (account_a);
});
}
}
void rai::wallets::foreach_representative (MDB_txn * transaction_a, std::function <void (rai::public_key const & pub_a, rai::raw_key const & prv_a)> const & action_a)
void rai::wallets::foreach_representative (MDB_txn * transaction_a, std::function<void(rai::public_key const & pub_a, rai::raw_key const & prv_a)> const & action_a)
{
for (auto i (items.begin ()), n (items.end ()); i != n; ++i)
{
@ -1400,8 +1389,8 @@ bool rai::wallets::exists (MDB_txn * transaction_a, rai::public_key const & acco
return result;
}
rai::uint128_t const rai::wallets::generate_priority = std::numeric_limits <rai::uint128_t>::max ();
rai::uint128_t const rai::wallets::high_priority = std::numeric_limits <rai::uint128_t>::max () - 1;
rai::uint128_t const rai::wallets::generate_priority = std::numeric_limits<rai::uint128_t>::max ();
rai::uint128_t const rai::wallets::high_priority = std::numeric_limits<rai::uint128_t>::max () - 1;
rai::store_iterator rai::wallet_store::begin (MDB_txn * transaction_a)
{

View file

@ -1,8 +1,8 @@
#pragma once
#include <rai/secure.hpp>
#include <rai/node/common.hpp>
#include <rai/node/openclwork.hpp>
#include <rai/secure.hpp>
#include <mutex>
#include <queue>
@ -18,7 +18,8 @@ public:
fan (rai::uint256_union const &, size_t);
void value (rai::raw_key &);
void value_set (rai::raw_key const &);
std::vector <std::unique_ptr <rai::uint256_union>> values;
std::vector<std::unique_ptr<rai::uint256_union>> values;
private:
std::mutex mutex;
void value_get (rai::raw_key &);
@ -52,7 +53,7 @@ class wallet_store
public:
wallet_store (bool &, rai::kdf &, rai::transaction &, rai::account, unsigned, std::string const &);
wallet_store (bool &, rai::kdf &, rai::transaction &, rai::account, unsigned, std::string const &, std::string const &);
std::vector <rai::account> accounts (MDB_txn *);
std::vector<rai::account> accounts (MDB_txn *);
void initialize (MDB_txn *, bool &, std::string const &);
rai::uint256_union check (MDB_txn *);
bool rekey (MDB_txn *, std::string const &);
@ -85,7 +86,7 @@ public:
void derive_key (rai::raw_key &, MDB_txn *, std::string const &);
void serialize_json (MDB_txn *, std::string &);
void write_backup (MDB_txn *, boost::filesystem::path const &);
bool move (MDB_txn *, rai::wallet_store &, std::vector <rai::public_key> const &);
bool move (MDB_txn *, rai::wallet_store &, std::vector<rai::public_key> const &);
bool import (MDB_txn *, rai::wallet_store &);
bool work_get (MDB_txn *, rai::public_key const &, uint64_t &);
void work_put (MDB_txn *, rai::public_key const &, uint64_t);
@ -115,12 +116,12 @@ public:
};
class node;
// A wallet is a set of account keys encrypted by a common encryption key
class wallet : public std::enable_shared_from_this <rai::wallet>
class wallet : public std::enable_shared_from_this<rai::wallet>
{
public:
std::shared_ptr <rai::block> change_action (rai::account const &, rai::account const &, bool = true);
std::shared_ptr <rai::block> receive_action (rai::send_block const &, rai::account const &, rai::uint128_union const &, bool = true);
std::shared_ptr <rai::block> send_action (rai::account const &, rai::account const &, rai::uint128_t const &, bool = true);
std::shared_ptr<rai::block> change_action (rai::account const &, rai::account const &, bool = true);
std::shared_ptr<rai::block> receive_action (rai::send_block const &, rai::account const &, rai::uint128_union const &, bool = true);
std::shared_ptr<rai::block> send_action (rai::account const &, rai::account const &, rai::uint128_t const &, bool = true);
wallet (bool &, rai::transaction &, rai::node &, std::string const &);
wallet (bool &, rai::transaction &, rai::node &, std::string const &, std::string const &);
void enter_initial_password ();
@ -134,19 +135,19 @@ public:
bool import (std::string const &, std::string const &);
void serialize (std::string &);
bool change_sync (rai::account const &, rai::account const &);
void change_async (rai::account const &, rai::account const &, std::function <void (std::shared_ptr <rai::block>)> const &, bool = true);
bool receive_sync (std::shared_ptr <rai::block>, rai::account const &, rai::uint128_t const &);
void receive_async (std::shared_ptr <rai::block>, rai::account const &, rai::uint128_t const &, std::function <void (std::shared_ptr <rai::block>)> const &, bool = true);
void change_async (rai::account const &, rai::account const &, std::function<void(std::shared_ptr<rai::block>)> const &, bool = true);
bool receive_sync (std::shared_ptr<rai::block>, rai::account const &, rai::uint128_t const &);
void receive_async (std::shared_ptr<rai::block>, rai::account const &, rai::uint128_t const &, std::function<void(std::shared_ptr<rai::block>)> const &, bool = true);
rai::block_hash send_sync (rai::account const &, rai::account const &, rai::uint128_t const &);
void send_async (rai::account const &, rai::account const &, rai::uint128_t const &, std::function <void (std::shared_ptr <rai::block>)> const &, bool = true);
void send_async (rai::account const &, rai::account const &, rai::uint128_t const &, std::function<void(std::shared_ptr<rai::block>)> const &, bool = true);
void work_generate (rai::account const &, rai::block_hash const &);
void work_update (MDB_txn *, rai::account const &, rai::block_hash const &, uint64_t);
uint64_t work_fetch (MDB_txn *, rai::account const &, rai::block_hash const &);
void work_ensure (MDB_txn *, rai::account const &);
bool search_pending ();
void init_free_accounts (MDB_txn *);
std::unordered_set <rai::account> free_accounts;
std::function <void (bool, bool)> lock_observer;
std::unordered_set<rai::account> free_accounts;
std::function<void(bool, bool)> lock_observer;
rai::wallet_store store;
rai::node & node;
};
@ -155,19 +156,19 @@ class wallets
{
public:
wallets (bool &, rai::node &);
std::shared_ptr <rai::wallet> open (rai::uint256_union const &);
std::shared_ptr <rai::wallet> create (rai::uint256_union const &);
std::shared_ptr<rai::wallet> open (rai::uint256_union const &);
std::shared_ptr<rai::wallet> create (rai::uint256_union const &);
bool search_pending (rai::uint256_union const &);
void search_pending_all ();
void destroy (rai::uint256_union const &);
void do_wallet_actions (rai::account const &);
void queue_wallet_action (rai::account const &, rai::uint128_t const &, std::function <void ()> const &);
void foreach_representative (MDB_txn *, std::function <void (rai::public_key const &, rai::raw_key const &)> const &);
void queue_wallet_action (rai::account const &, rai::uint128_t const &, std::function<void()> const &);
void foreach_representative (MDB_txn *, std::function<void(rai::public_key const &, rai::raw_key const &)> const &);
bool exists (MDB_txn *, rai::public_key const &);
std::function <void (rai::account const &, bool)> observer;
std::unordered_map <rai::uint256_union, std::shared_ptr <rai::wallet>> items;
std::unordered_map <rai::account, std::multimap <rai::uint128_t, std::function <void ()>, std::greater <rai::uint128_t>>> pending_actions;
std::unordered_set <rai::account> current_actions;
std::function<void(rai::account const &, bool)> observer;
std::unordered_map<rai::uint256_union, std::shared_ptr<rai::wallet>> items;
std::unordered_map<rai::account, std::multimap<rai::uint128_t, std::function<void()>, std::greater<rai::uint128_t>>> pending_actions;
std::unordered_set<rai::account> current_actions;
std::mutex action_mutex;
rai::kdf kdf;
MDB_dbi handle;

View file

@ -6,23 +6,23 @@ namespace rai
class xorshift1024star
{
public:
xorshift1024star ():
xorshift1024star () :
p (0)
{
}
std::array <uint64_t, 16> s;
std::array<uint64_t, 16> s;
unsigned p;
uint64_t next ()
{
auto p_l (p);
auto pn ((p_l + 1) & 15);
p = pn;
uint64_t s0 = s[ p_l ];
uint64_t s1 = s[ pn ];
uint64_t s0 = s[p_l];
uint64_t s1 = s[pn];
s1 ^= s1 << 31; // a
s1 ^= s1 >> 11; // b
s0 ^= s0 >> 30; // c
return ( s[ pn ] = s0 ^ s1 ) * 1181783497276652981LL;
return (s[pn] = s0 ^ s1) * 1181783497276652981LL;
}
};
}

View file

@ -11,6 +11,6 @@ void rai::work_thread_reprioritize ()
{
policy = SCHED_BATCH;
auto result (pthread_setschedparam (handle, policy, &sched));
(void) result;
(void)result;
}
}

View file

@ -12,25 +12,25 @@ public:
opencl_library = dlopen ("libOpenCL.so", RTLD_NOW);
if (opencl_library != nullptr)
{
clGetPlatformIDs = reinterpret_cast <decltype (clGetPlatformIDs)> (dlsym(opencl_library, "clGetPlatformIDs"));
clGetPlatformInfo = reinterpret_cast <decltype (clGetPlatformInfo)> (dlsym(opencl_library, "clGetPlatformInfo"));
clGetDeviceIDs = reinterpret_cast <decltype (clGetDeviceIDs)> (dlsym(opencl_library, "clGetDeviceIDs"));
clGetDeviceInfo = reinterpret_cast <decltype (clGetDeviceInfo)> (dlsym(opencl_library, "clGetDeviceInfo"));
clCreateContext = reinterpret_cast <decltype (clCreateContext)> (dlsym(opencl_library, "clCreateContext"));
clCreateCommandQueue = reinterpret_cast <decltype (clCreateCommandQueue)> (dlsym(opencl_library, "clCreateCommandQueue"));
clCreateBuffer = reinterpret_cast <decltype (clCreateBuffer)> (dlsym(opencl_library, "clCreateBuffer"));
clCreateProgramWithSource = reinterpret_cast <decltype (clCreateProgramWithSource)> (dlsym(opencl_library, "clCreateProgramWithSource"));
clBuildProgram = reinterpret_cast <decltype (clBuildProgram)> (dlsym(opencl_library, "clBuildProgram"));
clGetProgramBuildInfo = reinterpret_cast <decltype (clGetProgramBuildInfo)> (dlsym(opencl_library, "clGetProgramBuildInfo"));
clCreateKernel = reinterpret_cast <decltype (clCreateKernel)> (dlsym(opencl_library, "clCreateKernel"));
clSetKernelArg = reinterpret_cast <decltype (clSetKernelArg)> (dlsym(opencl_library, "clSetKernelArg"));
clReleaseKernel = reinterpret_cast <decltype (clReleaseKernel)> (dlsym(opencl_library, "clReleaseKernel"));
clReleaseProgram = reinterpret_cast <decltype (clReleaseProgram)> (dlsym(opencl_library, "clReleaseProgram"));
clReleaseContext = reinterpret_cast <decltype (clReleaseContext)> (dlsym(opencl_library, "clReleaseContext"));
clEnqueueWriteBuffer = reinterpret_cast <decltype (clEnqueueWriteBuffer)> (dlsym(opencl_library, "clEnqueueWriteBuffer"));
clEnqueueNDRangeKernel = reinterpret_cast <decltype (clEnqueueNDRangeKernel)> (dlsym(opencl_library, "clEnqueueNDRangeKernel"));
clEnqueueReadBuffer = reinterpret_cast <decltype (clEnqueueReadBuffer)> (dlsym(opencl_library, "clEnqueueReadBuffer"));
clFinish = reinterpret_cast <decltype (clFinish)> (dlsym(opencl_library, "clFinish"));
clGetPlatformIDs = reinterpret_cast<decltype (clGetPlatformIDs)> (dlsym (opencl_library, "clGetPlatformIDs"));
clGetPlatformInfo = reinterpret_cast<decltype (clGetPlatformInfo)> (dlsym (opencl_library, "clGetPlatformInfo"));
clGetDeviceIDs = reinterpret_cast<decltype (clGetDeviceIDs)> (dlsym (opencl_library, "clGetDeviceIDs"));
clGetDeviceInfo = reinterpret_cast<decltype (clGetDeviceInfo)> (dlsym (opencl_library, "clGetDeviceInfo"));
clCreateContext = reinterpret_cast<decltype (clCreateContext)> (dlsym (opencl_library, "clCreateContext"));
clCreateCommandQueue = reinterpret_cast<decltype (clCreateCommandQueue)> (dlsym (opencl_library, "clCreateCommandQueue"));
clCreateBuffer = reinterpret_cast<decltype (clCreateBuffer)> (dlsym (opencl_library, "clCreateBuffer"));
clCreateProgramWithSource = reinterpret_cast<decltype (clCreateProgramWithSource)> (dlsym (opencl_library, "clCreateProgramWithSource"));
clBuildProgram = reinterpret_cast<decltype (clBuildProgram)> (dlsym (opencl_library, "clBuildProgram"));
clGetProgramBuildInfo = reinterpret_cast<decltype (clGetProgramBuildInfo)> (dlsym (opencl_library, "clGetProgramBuildInfo"));
clCreateKernel = reinterpret_cast<decltype (clCreateKernel)> (dlsym (opencl_library, "clCreateKernel"));
clSetKernelArg = reinterpret_cast<decltype (clSetKernelArg)> (dlsym (opencl_library, "clSetKernelArg"));
clReleaseKernel = reinterpret_cast<decltype (clReleaseKernel)> (dlsym (opencl_library, "clReleaseKernel"));
clReleaseProgram = reinterpret_cast<decltype (clReleaseProgram)> (dlsym (opencl_library, "clReleaseProgram"));
clReleaseContext = reinterpret_cast<decltype (clReleaseContext)> (dlsym (opencl_library, "clReleaseContext"));
clEnqueueWriteBuffer = reinterpret_cast<decltype (clEnqueueWriteBuffer)> (dlsym (opencl_library, "clEnqueueWriteBuffer"));
clEnqueueNDRangeKernel = reinterpret_cast<decltype (clEnqueueNDRangeKernel)> (dlsym (opencl_library, "clEnqueueNDRangeKernel"));
clEnqueueReadBuffer = reinterpret_cast<decltype (clEnqueueReadBuffer)> (dlsym (opencl_library, "clEnqueueReadBuffer"));
clFinish = reinterpret_cast<decltype (clFinish)> (dlsym (opencl_library, "clFinish"));
}
}
~opencl_initializer ()
@ -41,25 +41,25 @@ public:
}
}
void * opencl_library;
cl_int (* clGetPlatformIDs) (cl_uint, cl_platform_id *, cl_uint *);
cl_int (* clGetPlatformInfo) (cl_platform_id, cl_platform_info, size_t, void *, size_t *);
cl_int (* clGetDeviceIDs) (cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
cl_int (* clGetDeviceInfo) (cl_device_id, cl_device_info, size_t, void *, size_t *);
cl_context (* clCreateContext) (cl_context_properties const *, cl_uint, cl_device_id const *, void (*)(const char *, const void *, size_t, void *), void *, cl_int *);
cl_command_queue (* clCreateCommandQueue) (cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
cl_mem (* clCreateBuffer) (cl_context, cl_mem_flags, size_t, void *, cl_int *);
cl_program (* clCreateProgramWithSource) (cl_context, cl_uint, char const **, size_t const *, cl_int *);
cl_int (* clBuildProgram) (cl_program, cl_uint, cl_device_id const *, char const *, void (*)(cl_program, void *), void *);
cl_int(*clGetProgramBuildInfo) (cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
cl_kernel (* clCreateKernel) (cl_program, char const *, cl_int *);
cl_int (* clSetKernelArg) (cl_kernel, cl_uint, size_t, void const *);
cl_int (* clReleaseKernel) (cl_kernel);
cl_int (* clReleaseProgram) (cl_program);
cl_int (* clReleaseContext) (cl_context);
cl_int (* clEnqueueWriteBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void const *, cl_uint, cl_event const *, cl_event *);
cl_int (* clEnqueueNDRangeKernel) (cl_command_queue, cl_kernel, cl_uint, size_t const *, size_t const *, size_t const *, cl_uint, cl_event const *, cl_event *);
cl_int (* clEnqueueReadBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, cl_event const *, cl_event *);
cl_int (* clFinish) (cl_command_queue);
cl_int (*clGetPlatformIDs) (cl_uint, cl_platform_id *, cl_uint *);
cl_int (*clGetPlatformInfo) (cl_platform_id, cl_platform_info, size_t, void *, size_t *);
cl_int (*clGetDeviceIDs) (cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
cl_int (*clGetDeviceInfo) (cl_device_id, cl_device_info, size_t, void *, size_t *);
cl_context (*clCreateContext) (cl_context_properties const *, cl_uint, cl_device_id const *, void (*) (const char *, const void *, size_t, void *), void *, cl_int *);
cl_command_queue (*clCreateCommandQueue) (cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
cl_mem (*clCreateBuffer) (cl_context, cl_mem_flags, size_t, void *, cl_int *);
cl_program (*clCreateProgramWithSource) (cl_context, cl_uint, char const **, size_t const *, cl_int *);
cl_int (*clBuildProgram) (cl_program, cl_uint, cl_device_id const *, char const *, void (*) (cl_program, void *), void *);
cl_int (*clGetProgramBuildInfo) (cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
cl_kernel (*clCreateKernel) (cl_program, char const *, cl_int *);
cl_int (*clSetKernelArg) (cl_kernel, cl_uint, size_t, void const *);
cl_int (*clReleaseKernel) (cl_kernel);
cl_int (*clReleaseProgram) (cl_program);
cl_int (*clReleaseContext) (cl_context);
cl_int (*clEnqueueWriteBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void const *, cl_uint, cl_event const *, cl_event *);
cl_int (*clEnqueueNDRangeKernel) (cl_command_queue, cl_kernel, cl_uint, size_t const *, size_t const *, size_t const *, cl_uint, cl_event const *, cl_event *);
cl_int (*clEnqueueReadBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, cl_event const *, cl_event *);
cl_int (*clFinish) (cl_command_queue);
static opencl_initializer initializer;
};
}
@ -99,7 +99,7 @@ cl_int clGetDeviceInfo (cl_device_id device, cl_device_info param_name, size_t p
return opencl_initializer::initializer.clGetDeviceInfo (device, param_name, param_value_size, param_value, param_value_size_ret);
}
cl_context clCreateContext (cl_context_properties const * properties, cl_uint num_devices, cl_device_id const * devices, void (* pfn_notify)(const char *, const void *, size_t, void *), void * user_data, cl_int * errcode_ret)
cl_context clCreateContext (cl_context_properties const * properties, cl_uint num_devices, cl_device_id const * devices, void (*pfn_notify) (const char *, const void *, size_t, void *), void * user_data, cl_int * errcode_ret)
{
return opencl_initializer::initializer.clCreateContext (properties, num_devices, devices, pfn_notify, user_data, errcode_ret);
}
@ -119,14 +119,14 @@ cl_program clCreateProgramWithSource (cl_context context, cl_uint count, char co
return opencl_initializer::initializer.clCreateProgramWithSource (context, count, strings, lengths, errcode_ret);
}
cl_int clBuildProgram (cl_program program, cl_uint num_devices, cl_device_id const * device_list, char const * options, void (*pfn_notify) (cl_program , void *), void * user_data)
cl_int clBuildProgram (cl_program program, cl_uint num_devices, cl_device_id const * device_list, char const * options, void (*pfn_notify) (cl_program, void *), void * user_data)
{
return opencl_initializer::initializer.clBuildProgram (program, num_devices, device_list, options, pfn_notify, user_data);
}
cl_int clGetProgramBuildInfo(cl_program program, cl_device_id device, cl_program_build_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
cl_int clGetProgramBuildInfo (cl_program program, cl_device_id device, cl_program_build_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
{
return opencl_initializer::initializer.clGetProgramBuildInfo(program, device, param_name, param_value_size, param_value, param_value_size_ret);
return opencl_initializer::initializer.clGetProgramBuildInfo (program, device, param_name, param_value_size, param_value, param_value_size_ret);
}
cl_kernel clCreateKernel (cl_program program, char const * kernel_name, cl_int * errcode_ret)
@ -154,7 +154,7 @@ cl_int clReleaseContext (cl_context context)
return opencl_initializer::initializer.clReleaseContext (context);
}
cl_int clEnqueueWriteBuffer (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset, size_t size, void const * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list,cl_event * event)
cl_int clEnqueueWriteBuffer (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset, size_t size, void const * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
{
return opencl_initializer::initializer.clEnqueueWriteBuffer (command_queue, buffer, blocking_write, offset, size, ptr, num_events_in_wait_list, event_wait_list, event);
}
@ -164,7 +164,7 @@ cl_int clEnqueueNDRangeKernel (cl_command_queue command_queue, cl_kernel kernel,
return opencl_initializer::initializer.clEnqueueNDRangeKernel (command_queue, kernel, work_dim, global_work_offset, global_work_size, local_work_size, num_events_in_wait_list, event_wait_list, event);
}
cl_int clEnqueueReadBuffer (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t size, void * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event *event)
cl_int clEnqueueReadBuffer (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t size, void * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
{
return opencl_initializer::initializer.clEnqueueReadBuffer (command_queue, buffer, blocking_read, offset, size, ptr, num_events_in_wait_list, event_wait_list, event);
}

View file

@ -1,7 +1,7 @@
#include <rai/node/working.hpp>
#include <sys/types.h>
#include <pwd.h>
#include <sys/types.h>
namespace rai
{

View file

@ -6,7 +6,7 @@
void rai::set_application_icon (QApplication & application_a)
{
HICON hIcon = static_cast <HICON> (LoadImage (GetModuleHandle (nullptr), MAKEINTRESOURCE (1), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADTRANSPARENT));
HICON hIcon = static_cast<HICON> (LoadImage (GetModuleHandle (nullptr), MAKEINTRESOURCE (1), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADTRANSPARENT));
application_a.setWindowIcon (QIcon (QtWin::fromHICON (hIcon)));
DestroyIcon (hIcon);
}

View file

@ -4,74 +4,74 @@
namespace
{
class opencl_initializer
class opencl_initializer
{
public:
opencl_initializer ()
{
public:
opencl_initializer()
opencl_library = LoadLibrary ("OpenCL.dll");
if (opencl_library != nullptr)
{
opencl_library = LoadLibrary("OpenCL.dll");
if (opencl_library != nullptr)
{
clGetPlatformIDs = reinterpret_cast <decltype (clGetPlatformIDs)> (GetProcAddress(opencl_library, "clGetPlatformIDs"));
clGetPlatformInfo = reinterpret_cast <decltype (clGetPlatformInfo)> (GetProcAddress(opencl_library, "clGetPlatformInfo"));
clGetDeviceIDs = reinterpret_cast <decltype (clGetDeviceIDs)> (GetProcAddress(opencl_library, "clGetDeviceIDs"));
clGetDeviceInfo = reinterpret_cast <decltype (clGetDeviceInfo)> (GetProcAddress(opencl_library, "clGetDeviceInfo"));
clCreateContext = reinterpret_cast <decltype (clCreateContext)> (GetProcAddress(opencl_library, "clCreateContext"));
clCreateCommandQueue = reinterpret_cast <decltype (clCreateCommandQueue)> (GetProcAddress(opencl_library, "clCreateCommandQueue"));
clCreateBuffer = reinterpret_cast <decltype (clCreateBuffer)> (GetProcAddress(opencl_library, "clCreateBuffer"));
clCreateProgramWithSource = reinterpret_cast <decltype (clCreateProgramWithSource)> (GetProcAddress(opencl_library, "clCreateProgramWithSource"));
clBuildProgram = reinterpret_cast <decltype (clBuildProgram)> (GetProcAddress(opencl_library, "clBuildProgram"));
clGetProgramBuildInfo = reinterpret_cast <decltype (clGetProgramBuildInfo)> (GetProcAddress(opencl_library, "clGetProgramBuildInfo"));
clCreateKernel = reinterpret_cast <decltype (clCreateKernel)> (GetProcAddress(opencl_library, "clCreateKernel"));
clSetKernelArg = reinterpret_cast <decltype (clSetKernelArg)> (GetProcAddress(opencl_library, "clSetKernelArg"));
clReleaseKernel = reinterpret_cast <decltype (clReleaseKernel)> (GetProcAddress(opencl_library, "clReleaseKernel"));
clReleaseProgram = reinterpret_cast <decltype (clReleaseProgram)> (GetProcAddress(opencl_library, "clReleaseProgram"));
clReleaseContext = reinterpret_cast <decltype (clReleaseContext)> (GetProcAddress(opencl_library, "clReleaseContext"));
clEnqueueWriteBuffer = reinterpret_cast <decltype (clEnqueueWriteBuffer)> (GetProcAddress(opencl_library, "clEnqueueWriteBuffer"));
clEnqueueNDRangeKernel = reinterpret_cast <decltype (clEnqueueNDRangeKernel)> (GetProcAddress(opencl_library, "clEnqueueNDRangeKernel"));
clEnqueueReadBuffer = reinterpret_cast <decltype (clEnqueueReadBuffer)> (GetProcAddress(opencl_library, "clEnqueueReadBuffer"));
clFinish = reinterpret_cast <decltype (clFinish)> (GetProcAddress(opencl_library, "clFinish"));
}
clGetPlatformIDs = reinterpret_cast<decltype (clGetPlatformIDs)> (GetProcAddress (opencl_library, "clGetPlatformIDs"));
clGetPlatformInfo = reinterpret_cast<decltype (clGetPlatformInfo)> (GetProcAddress (opencl_library, "clGetPlatformInfo"));
clGetDeviceIDs = reinterpret_cast<decltype (clGetDeviceIDs)> (GetProcAddress (opencl_library, "clGetDeviceIDs"));
clGetDeviceInfo = reinterpret_cast<decltype (clGetDeviceInfo)> (GetProcAddress (opencl_library, "clGetDeviceInfo"));
clCreateContext = reinterpret_cast<decltype (clCreateContext)> (GetProcAddress (opencl_library, "clCreateContext"));
clCreateCommandQueue = reinterpret_cast<decltype (clCreateCommandQueue)> (GetProcAddress (opencl_library, "clCreateCommandQueue"));
clCreateBuffer = reinterpret_cast<decltype (clCreateBuffer)> (GetProcAddress (opencl_library, "clCreateBuffer"));
clCreateProgramWithSource = reinterpret_cast<decltype (clCreateProgramWithSource)> (GetProcAddress (opencl_library, "clCreateProgramWithSource"));
clBuildProgram = reinterpret_cast<decltype (clBuildProgram)> (GetProcAddress (opencl_library, "clBuildProgram"));
clGetProgramBuildInfo = reinterpret_cast<decltype (clGetProgramBuildInfo)> (GetProcAddress (opencl_library, "clGetProgramBuildInfo"));
clCreateKernel = reinterpret_cast<decltype (clCreateKernel)> (GetProcAddress (opencl_library, "clCreateKernel"));
clSetKernelArg = reinterpret_cast<decltype (clSetKernelArg)> (GetProcAddress (opencl_library, "clSetKernelArg"));
clReleaseKernel = reinterpret_cast<decltype (clReleaseKernel)> (GetProcAddress (opencl_library, "clReleaseKernel"));
clReleaseProgram = reinterpret_cast<decltype (clReleaseProgram)> (GetProcAddress (opencl_library, "clReleaseProgram"));
clReleaseContext = reinterpret_cast<decltype (clReleaseContext)> (GetProcAddress (opencl_library, "clReleaseContext"));
clEnqueueWriteBuffer = reinterpret_cast<decltype (clEnqueueWriteBuffer)> (GetProcAddress (opencl_library, "clEnqueueWriteBuffer"));
clEnqueueNDRangeKernel = reinterpret_cast<decltype (clEnqueueNDRangeKernel)> (GetProcAddress (opencl_library, "clEnqueueNDRangeKernel"));
clEnqueueReadBuffer = reinterpret_cast<decltype (clEnqueueReadBuffer)> (GetProcAddress (opencl_library, "clEnqueueReadBuffer"));
clFinish = reinterpret_cast<decltype (clFinish)> (GetProcAddress (opencl_library, "clFinish"));
}
~opencl_initializer()
}
~opencl_initializer ()
{
if (opencl_library != nullptr)
{
if (opencl_library != nullptr)
{
FreeLibrary(opencl_library);
}
FreeLibrary (opencl_library);
}
HMODULE opencl_library;
cl_int(CL_API_CALL *clGetPlatformIDs) (cl_uint, cl_platform_id *, cl_uint *);
cl_int(CL_API_CALL *clGetPlatformInfo) (cl_platform_id, cl_platform_info, size_t, void *, size_t *);
cl_int(CL_API_CALL *clGetDeviceIDs) (cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
cl_int(CL_API_CALL *clGetDeviceInfo) (cl_device_id, cl_device_info, size_t, void *, size_t *);
cl_context(CL_API_CALL *clCreateContext) (cl_context_properties const *, cl_uint, cl_device_id const *, void(CL_CALLBACK*)(const char *, const void *, size_t, void *), void *, cl_int *);
cl_command_queue(CL_API_CALL *clCreateCommandQueue) (cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
cl_mem(CL_API_CALL *clCreateBuffer) (cl_context, cl_mem_flags, size_t, void *, cl_int *);
cl_program(CL_API_CALL *clCreateProgramWithSource) (cl_context, cl_uint, char const **, size_t const *, cl_int *);
cl_int(CL_API_CALL *clBuildProgram) (cl_program, cl_uint, cl_device_id const *, char const *, void(CL_CALLBACK*)(cl_program, void *), void *);
cl_int (CL_API_CALL *clGetProgramBuildInfo) (cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
cl_kernel(CL_API_CALL *clCreateKernel) (cl_program, char const *, cl_int *);
cl_int(CL_API_CALL *clSetKernelArg) (cl_kernel, cl_uint, size_t, void const *);
cl_int(CL_API_CALL *clReleaseKernel) (cl_kernel);
cl_int(CL_API_CALL *clReleaseProgram) (cl_program);
cl_int(CL_API_CALL *clReleaseContext) (cl_context);
cl_int(CL_API_CALL *clEnqueueWriteBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void const *, cl_uint, cl_event const *, cl_event *);
cl_int(CL_API_CALL *clEnqueueNDRangeKernel) (cl_command_queue, cl_kernel, cl_uint, size_t const *, size_t const *, size_t const *, cl_uint, cl_event const *, cl_event *);
cl_int(CL_API_CALL *clEnqueueReadBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, cl_event const *, cl_event *);
cl_int(CL_API_CALL *clFinish) (cl_command_queue);
static opencl_initializer initializer;
};
}
HMODULE opencl_library;
cl_int (CL_API_CALL * clGetPlatformIDs) (cl_uint, cl_platform_id *, cl_uint *);
cl_int (CL_API_CALL * clGetPlatformInfo) (cl_platform_id, cl_platform_info, size_t, void *, size_t *);
cl_int (CL_API_CALL * clGetDeviceIDs) (cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
cl_int (CL_API_CALL * clGetDeviceInfo) (cl_device_id, cl_device_info, size_t, void *, size_t *);
cl_context (CL_API_CALL * clCreateContext) (cl_context_properties const *, cl_uint, cl_device_id const *, void(CL_CALLBACK *) (const char *, const void *, size_t, void *), void *, cl_int *);
cl_command_queue (CL_API_CALL * clCreateCommandQueue) (cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
cl_mem (CL_API_CALL * clCreateBuffer) (cl_context, cl_mem_flags, size_t, void *, cl_int *);
cl_program (CL_API_CALL * clCreateProgramWithSource) (cl_context, cl_uint, char const **, size_t const *, cl_int *);
cl_int (CL_API_CALL * clBuildProgram) (cl_program, cl_uint, cl_device_id const *, char const *, void(CL_CALLBACK *) (cl_program, void *), void *);
cl_int (CL_API_CALL * clGetProgramBuildInfo) (cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
cl_kernel (CL_API_CALL * clCreateKernel) (cl_program, char const *, cl_int *);
cl_int (CL_API_CALL * clSetKernelArg) (cl_kernel, cl_uint, size_t, void const *);
cl_int (CL_API_CALL * clReleaseKernel) (cl_kernel);
cl_int (CL_API_CALL * clReleaseProgram) (cl_program);
cl_int (CL_API_CALL * clReleaseContext) (cl_context);
cl_int (CL_API_CALL * clEnqueueWriteBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void const *, cl_uint, cl_event const *, cl_event *);
cl_int (CL_API_CALL * clEnqueueNDRangeKernel) (cl_command_queue, cl_kernel, cl_uint, size_t const *, size_t const *, size_t const *, cl_uint, cl_event const *, cl_event *);
cl_int (CL_API_CALL * clEnqueueReadBuffer) (cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, cl_event const *, cl_event *);
cl_int (CL_API_CALL * clFinish) (cl_command_queue);
static opencl_initializer initializer;
};
}
opencl_initializer opencl_initializer::initializer;
cl_int CL_API_CALL clGetPlatformIDs(cl_uint num_entries, cl_platform_id * platforms, cl_uint * num_platforms)
cl_int CL_API_CALL clGetPlatformIDs (cl_uint num_entries, cl_platform_id * platforms, cl_uint * num_platforms)
{
cl_int result;
if (opencl_initializer::initializer.opencl_library != nullptr)
{
result = opencl_initializer::initializer.clGetPlatformIDs(num_entries, platforms, num_platforms);
result = opencl_initializer::initializer.clGetPlatformIDs (num_entries, platforms, num_platforms);
}
else
{
@ -84,92 +84,92 @@ cl_int CL_API_CALL clGetPlatformIDs(cl_uint num_entries, cl_platform_id * platfo
return result;
}
cl_int CL_API_CALL clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
cl_int CL_API_CALL clGetPlatformInfo (cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
{
return opencl_initializer::initializer.clGetPlatformInfo(platform, param_name, param_value_size, param_value, param_value_size_ret);
return opencl_initializer::initializer.clGetPlatformInfo (platform, param_name, param_value_size, param_value, param_value_size_ret);
}
cl_int CL_API_CALL clGetDeviceIDs(cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id * devices, cl_uint * num_devices)
cl_int CL_API_CALL clGetDeviceIDs (cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id * devices, cl_uint * num_devices)
{
return opencl_initializer::initializer.clGetDeviceIDs(platform, device_type, num_entries, devices, num_devices);
return opencl_initializer::initializer.clGetDeviceIDs (platform, device_type, num_entries, devices, num_devices);
}
cl_int CL_API_CALL clGetDeviceInfo(cl_device_id device, cl_device_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
cl_int CL_API_CALL clGetDeviceInfo (cl_device_id device, cl_device_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
{
return opencl_initializer::initializer.clGetDeviceInfo(device, param_name, param_value_size, param_value, param_value_size_ret);
return opencl_initializer::initializer.clGetDeviceInfo (device, param_name, param_value_size, param_value, param_value_size_ret);
}
cl_context CL_API_CALL clCreateContext(cl_context_properties const * properties, cl_uint num_devices, cl_device_id const * devices, void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), void * user_data, cl_int * errcode_ret)
cl_context CL_API_CALL clCreateContext (cl_context_properties const * properties, cl_uint num_devices, cl_device_id const * devices, void(CL_CALLBACK * pfn_notify) (const char *, const void *, size_t, void *), void * user_data, cl_int * errcode_ret)
{
return opencl_initializer::initializer.clCreateContext(properties, num_devices, devices, pfn_notify, user_data, errcode_ret);
return opencl_initializer::initializer.clCreateContext (properties, num_devices, devices, pfn_notify, user_data, errcode_ret);
}
cl_command_queue CL_API_CALL clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int * errcode_ret)
cl_command_queue CL_API_CALL clCreateCommandQueue (cl_context context, cl_device_id device, cl_command_queue_properties properties, cl_int * errcode_ret)
{
return opencl_initializer::initializer.clCreateCommandQueue(context, device, properties, errcode_ret);
return opencl_initializer::initializer.clCreateCommandQueue (context, device, properties, errcode_ret);
}
cl_mem CL_API_CALL clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void * host_ptr, cl_int * errcode_ret)
cl_mem CL_API_CALL clCreateBuffer (cl_context context, cl_mem_flags flags, size_t size, void * host_ptr, cl_int * errcode_ret)
{
return opencl_initializer::initializer.clCreateBuffer(context, flags, size, host_ptr, errcode_ret);
return opencl_initializer::initializer.clCreateBuffer (context, flags, size, host_ptr, errcode_ret);
}
cl_program CL_API_CALL clCreateProgramWithSource(cl_context context, cl_uint count, char const ** strings, size_t const * lengths, cl_int * errcode_ret)
cl_program CL_API_CALL clCreateProgramWithSource (cl_context context, cl_uint count, char const ** strings, size_t const * lengths, cl_int * errcode_ret)
{
return opencl_initializer::initializer.clCreateProgramWithSource(context, count, strings, lengths, errcode_ret);
return opencl_initializer::initializer.clCreateProgramWithSource (context, count, strings, lengths, errcode_ret);
}
cl_int CL_API_CALL clBuildProgram(cl_program program, cl_uint num_devices, cl_device_id const * device_list, char const * options, void(CL_CALLBACK *pfn_notify) (cl_program, void *), void * user_data)
cl_int CL_API_CALL clBuildProgram (cl_program program, cl_uint num_devices, cl_device_id const * device_list, char const * options, void(CL_CALLBACK * pfn_notify) (cl_program, void *), void * user_data)
{
return opencl_initializer::initializer.clBuildProgram(program, num_devices, device_list, options, pfn_notify, user_data);
return opencl_initializer::initializer.clBuildProgram (program, num_devices, device_list, options, pfn_notify, user_data);
}
cl_int CL_API_CALL clGetProgramBuildInfo(cl_program program, cl_device_id device, cl_program_build_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
cl_int CL_API_CALL clGetProgramBuildInfo (cl_program program, cl_device_id device, cl_program_build_info param_name, size_t param_value_size, void * param_value, size_t * param_value_size_ret)
{
return opencl_initializer::initializer.clGetProgramBuildInfo(program, device, param_name, param_value_size, param_value, param_value_size_ret);
return opencl_initializer::initializer.clGetProgramBuildInfo (program, device, param_name, param_value_size, param_value, param_value_size_ret);
}
cl_kernel CL_API_CALL clCreateKernel(cl_program program, char const * kernel_name, cl_int * errcode_ret)
cl_kernel CL_API_CALL clCreateKernel (cl_program program, char const * kernel_name, cl_int * errcode_ret)
{
return opencl_initializer::initializer.clCreateKernel(program, kernel_name, errcode_ret);
return opencl_initializer::initializer.clCreateKernel (program, kernel_name, errcode_ret);
}
cl_int CL_API_CALL clSetKernelArg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, void const * arg_value)
cl_int CL_API_CALL clSetKernelArg (cl_kernel kernel, cl_uint arg_index, size_t arg_size, void const * arg_value)
{
return opencl_initializer::initializer.clSetKernelArg(kernel, arg_index, arg_size, arg_value);
return opencl_initializer::initializer.clSetKernelArg (kernel, arg_index, arg_size, arg_value);
}
cl_int CL_API_CALL clReleaseKernel(cl_kernel kernel)
cl_int CL_API_CALL clReleaseKernel (cl_kernel kernel)
{
return opencl_initializer::initializer.clReleaseKernel(kernel);
return opencl_initializer::initializer.clReleaseKernel (kernel);
}
cl_int CL_API_CALL clReleaseProgram(cl_program program)
cl_int CL_API_CALL clReleaseProgram (cl_program program)
{
return opencl_initializer::initializer.clReleaseProgram(program);
return opencl_initializer::initializer.clReleaseProgram (program);
}
cl_int CL_API_CALL clReleaseContext(cl_context context)
cl_int CL_API_CALL clReleaseContext (cl_context context)
{
return opencl_initializer::initializer.clReleaseContext(context);
return opencl_initializer::initializer.clReleaseContext (context);
}
cl_int CL_API_CALL clEnqueueWriteBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset, size_t size, void const * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
cl_int CL_API_CALL clEnqueueWriteBuffer (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset, size_t size, void const * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
{
return opencl_initializer::initializer.clEnqueueWriteBuffer(command_queue, buffer, blocking_write, offset, size, ptr, num_events_in_wait_list, event_wait_list, event);
return opencl_initializer::initializer.clEnqueueWriteBuffer (command_queue, buffer, blocking_write, offset, size, ptr, num_events_in_wait_list, event_wait_list, event);
}
cl_int CL_API_CALL clEnqueueNDRangeKernel(cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, size_t const * global_work_offset, size_t const * global_work_size, size_t const * local_work_size, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
cl_int CL_API_CALL clEnqueueNDRangeKernel (cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, size_t const * global_work_offset, size_t const * global_work_size, size_t const * local_work_size, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
{
return opencl_initializer::initializer.clEnqueueNDRangeKernel(command_queue, kernel, work_dim, global_work_offset, global_work_size, local_work_size, num_events_in_wait_list, event_wait_list, event);
return opencl_initializer::initializer.clEnqueueNDRangeKernel (command_queue, kernel, work_dim, global_work_offset, global_work_size, local_work_size, num_events_in_wait_list, event_wait_list, event);
}
cl_int CL_API_CALL clEnqueueReadBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t size, void * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event *event)
cl_int CL_API_CALL clEnqueueReadBuffer (cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, size_t size, void * ptr, cl_uint num_events_in_wait_list, cl_event const * event_wait_list, cl_event * event)
{
return opencl_initializer::initializer.clEnqueueReadBuffer(command_queue, buffer, blocking_read, offset, size, ptr, num_events_in_wait_list, event_wait_list, event);
return opencl_initializer::initializer.clEnqueueReadBuffer (command_queue, buffer, blocking_read, offset, size, ptr, num_events_in_wait_list, event_wait_list, event);
}
cl_int CL_API_CALL clFinish(cl_command_queue command_queue)
cl_int CL_API_CALL clFinish (cl_command_queue command_queue)
{
return opencl_initializer::initializer.clFinish(command_queue);
return opencl_initializer::initializer.clFinish (command_queue);
}

View file

@ -4,5 +4,5 @@
void rai::work_thread_reprioritize ()
{
auto SUCCESS (SetThreadPriority (GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN));
auto SUCCESS (SetThreadPriority (GetCurrentThread (), THREAD_MODE_BACKGROUND_BEGIN));
}

View file

@ -8,7 +8,7 @@ boost::filesystem::path app_path ()
{
boost::filesystem::path result;
WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path)))
if (SUCCEEDED (SHGetFolderPathW (NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path)))
{
result = boost::filesystem::path (path);
}

440
rai/qt/qt.cpp Executable file → Normal file

File diff suppressed because it is too large Load diff

View file

@ -9,326 +9,328 @@
#include <QtGui>
#include <QtWidgets>
namespace rai_qt {
class wallet;
class eventloop_processor : public QObject
{
public:
bool event (QEvent *) override;
};
class eventloop_event : public QEvent
{
public:
eventloop_event (std::function <void ()> const &);
std::function <void ()> action;
};
class settings
{
public:
settings (rai_qt::wallet &);
void refresh_representative ();
void activate ();
void update_locked (bool, bool);
QWidget * window;
QVBoxLayout * layout;
QLineEdit * password;
QPushButton * lock_toggle;
QFrame * sep1;
QLineEdit * new_password;
QLineEdit * retype_password;
QPushButton * change;
QFrame * sep2;
QLabel * representative;
QLabel * current_representative;
QLineEdit * new_representative;
QPushButton * change_rep;
QPushButton * back;
rai_qt::wallet & wallet;
};
class advanced_actions
{
public:
advanced_actions (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QPushButton * show_ledger;
QPushButton * show_peers;
QPushButton * search_for_receivables;
QPushButton * bootstrap;
QPushButton * wallet_refresh;
QPushButton * create_block;
QPushButton * enter_block;
QPushButton * block_viewer;
QPushButton * account_viewer;
QWidget * scale_window;
QHBoxLayout * scale_layout;
QLabel * scale_label;
QButtonGroup * ratio_group;
QRadioButton * mrai;
QRadioButton * krai;
QRadioButton * rai;
QPushButton * back;
namespace rai_qt
{
class wallet;
class eventloop_processor : public QObject
{
public:
bool event (QEvent *) override;
};
class eventloop_event : public QEvent
{
public:
eventloop_event (std::function<void()> const &);
std::function<void()> action;
};
class settings
{
public:
settings (rai_qt::wallet &);
void refresh_representative ();
void activate ();
void update_locked (bool, bool);
QWidget * window;
QVBoxLayout * layout;
QLineEdit * password;
QPushButton * lock_toggle;
QFrame * sep1;
QLineEdit * new_password;
QLineEdit * retype_password;
QPushButton * change;
QFrame * sep2;
QLabel * representative;
QLabel * current_representative;
QLineEdit * new_representative;
QPushButton * change_rep;
QPushButton * back;
rai_qt::wallet & wallet;
};
class advanced_actions
{
public:
advanced_actions (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QPushButton * show_ledger;
QPushButton * show_peers;
QPushButton * search_for_receivables;
QPushButton * bootstrap;
QPushButton * wallet_refresh;
QPushButton * create_block;
QPushButton * enter_block;
QPushButton * block_viewer;
QPushButton * account_viewer;
QWidget * scale_window;
QHBoxLayout * scale_layout;
QLabel * scale_label;
QButtonGroup * ratio_group;
QRadioButton * mrai;
QRadioButton * krai;
QRadioButton * rai;
QPushButton * back;
QWidget * ledger_window;
QVBoxLayout * ledger_layout;
QStandardItemModel * ledger_model;
QTableView * ledger_view;
QPushButton * ledger_refresh;
QPushButton * ledger_back;
QWidget * ledger_window;
QVBoxLayout * ledger_layout;
QStandardItemModel * ledger_model;
QTableView * ledger_view;
QPushButton * ledger_refresh;
QPushButton * ledger_back;
QWidget * peers_window;
QVBoxLayout * peers_layout;
QStandardItemModel * peers_model;
QTableView * peers_view;
QLabel * bootstrap_label;
QLineEdit * bootstrap_line;
QPushButton * peers_bootstrap;
QPushButton * peers_refresh;
QPushButton * peers_back;
rai_qt::wallet & wallet;
private:
void refresh_ledger ();
void refresh_peers ();
};
class block_entry
{
public:
block_entry (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QPlainTextEdit * block;
QLabel * status;
QPushButton * process;
QPushButton * back;
rai_qt::wallet & wallet;
};
class block_creation
{
public:
block_creation (rai_qt::wallet &);
void deactivate_all ();
void activate_send ();
void activate_receive ();
void activate_change ();
void activate_open ();
void create_send ();
void create_receive ();
void create_change ();
void create_open ();
QWidget * window;
QVBoxLayout * layout;
QButtonGroup * group;
QHBoxLayout * button_layout;
QRadioButton * send;
QRadioButton * receive;
QRadioButton * change;
QRadioButton * open;
QLabel * account_label;
QLineEdit * account;
QLabel * source_label;
QLineEdit * source;
QLabel * amount_label;
QLineEdit * amount;
QLabel * destination_label;
QLineEdit * destination;
QLabel * representative_label;
QLineEdit * representative;
QPlainTextEdit * block;
QLabel * status;
QPushButton * create;
QPushButton * back;
rai_qt::wallet & wallet;
};
class self_pane
{
public:
self_pane (rai_qt::wallet &, rai::account const &);
void refresh_balance ();
QWidget * window;
QVBoxLayout * layout;
QHBoxLayout * self_layout;
QWidget * self_window;
QLabel * your_account_label;
QLabel * version;
QWidget * account_window;
QHBoxLayout * account_layout;
QLineEdit * account_text;
QPushButton * copy_button;
QWidget * balance_window;
QHBoxLayout * balance_layout;
QLabel * balance_label;
rai_qt::wallet & wallet;
};
class accounts
{
public:
accounts (rai_qt::wallet &);
void refresh ();
void refresh_wallet_balance ();
QLabel * wallet_balance_label;
QWidget * window;
QVBoxLayout * layout;
QStandardItemModel * model;
QTableView * view;
QPushButton * use_account;
QPushButton * create_account;
QPushButton * import_wallet;
QPushButton * backup_seed;
QFrame * separator;
QLineEdit * account_key_line;
QPushButton * account_key_button;
QPushButton * back;
rai_qt::wallet & wallet;
};
class import
{
public:
import (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QLabel * seed_label;
QLineEdit * seed;
QLabel * clear_label;
QLineEdit * clear_line;
QPushButton * import_seed;
QFrame * separator;
QLabel * filename_label;
QLineEdit * filename;
QLabel * password_label;
QLineEdit * password;
QPushButton * perform;
QPushButton * back;
rai_qt::wallet & wallet;
};
class history
{
public:
history (rai::ledger &, rai::account const &, rai_qt::wallet &);
void refresh ();
QWidget * window;
QVBoxLayout * layout;
QStandardItemModel * model;
QTableView * view;
QWidget * tx_window;
QHBoxLayout * tx_layout;
QLabel * tx_label;
QSpinBox * tx_count;
rai::ledger & ledger;
rai::account const & account;
rai_qt::wallet & wallet;
};
class block_viewer
{
public:
block_viewer (rai_qt::wallet &);
void rebroadcast_action (rai::uint256_union const &);
QWidget * window;
QVBoxLayout * layout;
QLabel * hash_label;
QLineEdit * hash;
QLabel * block_label;
QPlainTextEdit * block;
QLabel * successor_label;
QLineEdit * successor;
QPushButton * retrieve;
QPushButton * rebroadcast;
QPushButton * back;
rai_qt::wallet & wallet;
};
class account_viewer
{
public:
account_viewer (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QLabel * account_label;
QLineEdit * account_line;
QPushButton * refresh;
QWidget * balance_window;
QHBoxLayout * balance_layout;
QLabel * balance_label;
rai_qt::history history;
QPushButton * back;
rai::account account;
rai_qt::wallet & wallet;
};
enum class status_types
{
not_a_status,
disconnected,
working,
locked,
vulnerable,
active,
synchronizing,
nominal
};
class status
{
public:
status (rai_qt::wallet &);
void erase (rai_qt::status_types);
void insert (rai_qt::status_types);
void set_text ();
std::string text ();
std::string color ();
std::set <rai_qt::status_types> active;
rai_qt::wallet & wallet;
};
class wallet : public std::enable_shared_from_this <rai_qt::wallet>
{
public:
wallet (QApplication &, rai_qt::eventloop_processor &, rai::node &, std::shared_ptr <rai::wallet>, rai::account &);
void start ();
void refresh ();
void update_connected ();
void empty_password ();
void change_rendering_ratio (rai::uint128_t const &);
std::string format_balance (rai::uint128_t const &) const;
rai::uint128_t rendering_ratio;
rai::node & node;
std::shared_ptr <rai::wallet> wallet_m;
rai::account & account;
rai_qt::eventloop_processor & processor;
rai_qt::history history;
rai_qt::accounts accounts;
rai_qt::self_pane self;
rai_qt::settings settings;
rai_qt::advanced_actions advanced;
rai_qt::block_creation block_creation;
rai_qt::block_entry block_entry;
rai_qt::block_viewer block_viewer;
rai_qt::account_viewer account_viewer;
rai_qt::import import;
QApplication & application;
QLabel * status;
QStackedWidget * main_stack;
QWidget * client_window;
QVBoxLayout * client_layout;
QWidget * entry_window;
QVBoxLayout * entry_window_layout;
QFrame * separator;
QLabel * account_history_label;
QPushButton * send_blocks;
QPushButton * settings_button;
QPushButton * accounts_button;
QPushButton * show_advanced;
QWidget * send_blocks_window;
QVBoxLayout * send_blocks_layout;
QLabel * send_account_label;
QLineEdit * send_account;
QLabel * send_count_label;
QLineEdit * send_count;
QPushButton * send_blocks_send;
QPushButton * send_blocks_back;
rai_qt::status active_status;
void pop_main_stack ();
void push_main_stack (QWidget *);
};
QWidget * peers_window;
QVBoxLayout * peers_layout;
QStandardItemModel * peers_model;
QTableView * peers_view;
QLabel * bootstrap_label;
QLineEdit * bootstrap_line;
QPushButton * peers_bootstrap;
QPushButton * peers_refresh;
QPushButton * peers_back;
rai_qt::wallet & wallet;
private:
void refresh_ledger ();
void refresh_peers ();
};
class block_entry
{
public:
block_entry (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QPlainTextEdit * block;
QLabel * status;
QPushButton * process;
QPushButton * back;
rai_qt::wallet & wallet;
};
class block_creation
{
public:
block_creation (rai_qt::wallet &);
void deactivate_all ();
void activate_send ();
void activate_receive ();
void activate_change ();
void activate_open ();
void create_send ();
void create_receive ();
void create_change ();
void create_open ();
QWidget * window;
QVBoxLayout * layout;
QButtonGroup * group;
QHBoxLayout * button_layout;
QRadioButton * send;
QRadioButton * receive;
QRadioButton * change;
QRadioButton * open;
QLabel * account_label;
QLineEdit * account;
QLabel * source_label;
QLineEdit * source;
QLabel * amount_label;
QLineEdit * amount;
QLabel * destination_label;
QLineEdit * destination;
QLabel * representative_label;
QLineEdit * representative;
QPlainTextEdit * block;
QLabel * status;
QPushButton * create;
QPushButton * back;
rai_qt::wallet & wallet;
};
class self_pane
{
public:
self_pane (rai_qt::wallet &, rai::account const &);
void refresh_balance ();
QWidget * window;
QVBoxLayout * layout;
QHBoxLayout * self_layout;
QWidget * self_window;
QLabel * your_account_label;
QLabel * version;
QWidget * account_window;
QHBoxLayout * account_layout;
QLineEdit * account_text;
QPushButton * copy_button;
QWidget * balance_window;
QHBoxLayout * balance_layout;
QLabel * balance_label;
rai_qt::wallet & wallet;
};
class accounts
{
public:
accounts (rai_qt::wallet &);
void refresh ();
void refresh_wallet_balance ();
QLabel * wallet_balance_label;
QWidget * window;
QVBoxLayout * layout;
QStandardItemModel * model;
QTableView * view;
QPushButton * use_account;
QPushButton * create_account;
QPushButton * import_wallet;
QPushButton * backup_seed;
QFrame * separator;
QLineEdit * account_key_line;
QPushButton * account_key_button;
QPushButton * back;
rai_qt::wallet & wallet;
};
class import
{
public:
import (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QLabel * seed_label;
QLineEdit * seed;
QLabel * clear_label;
QLineEdit * clear_line;
QPushButton * import_seed;
QFrame * separator;
QLabel * filename_label;
QLineEdit * filename;
QLabel * password_label;
QLineEdit * password;
QPushButton * perform;
QPushButton * back;
rai_qt::wallet & wallet;
};
class history
{
public:
history (rai::ledger &, rai::account const &, rai_qt::wallet &);
void refresh ();
QWidget * window;
QVBoxLayout * layout;
QStandardItemModel * model;
QTableView * view;
QWidget * tx_window;
QHBoxLayout * tx_layout;
QLabel * tx_label;
QSpinBox * tx_count;
rai::ledger & ledger;
rai::account const & account;
rai_qt::wallet & wallet;
};
class block_viewer
{
public:
block_viewer (rai_qt::wallet &);
void rebroadcast_action (rai::uint256_union const &);
QWidget * window;
QVBoxLayout * layout;
QLabel * hash_label;
QLineEdit * hash;
QLabel * block_label;
QPlainTextEdit * block;
QLabel * successor_label;
QLineEdit * successor;
QPushButton * retrieve;
QPushButton * rebroadcast;
QPushButton * back;
rai_qt::wallet & wallet;
};
class account_viewer
{
public:
account_viewer (rai_qt::wallet &);
QWidget * window;
QVBoxLayout * layout;
QLabel * account_label;
QLineEdit * account_line;
QPushButton * refresh;
QWidget * balance_window;
QHBoxLayout * balance_layout;
QLabel * balance_label;
rai_qt::history history;
QPushButton * back;
rai::account account;
rai_qt::wallet & wallet;
};
enum class status_types
{
not_a_status,
disconnected,
working,
locked,
vulnerable,
active,
synchronizing,
nominal
};
class status
{
public:
status (rai_qt::wallet &);
void erase (rai_qt::status_types);
void insert (rai_qt::status_types);
void set_text ();
std::string text ();
std::string color ();
std::set<rai_qt::status_types> active;
rai_qt::wallet & wallet;
};
class wallet : public std::enable_shared_from_this<rai_qt::wallet>
{
public:
wallet (QApplication &, rai_qt::eventloop_processor &, rai::node &, std::shared_ptr<rai::wallet>, rai::account &);
void start ();
void refresh ();
void update_connected ();
void empty_password ();
void change_rendering_ratio (rai::uint128_t const &);
std::string format_balance (rai::uint128_t const &) const;
rai::uint128_t rendering_ratio;
rai::node & node;
std::shared_ptr<rai::wallet> wallet_m;
rai::account & account;
rai_qt::eventloop_processor & processor;
rai_qt::history history;
rai_qt::accounts accounts;
rai_qt::self_pane self;
rai_qt::settings settings;
rai_qt::advanced_actions advanced;
rai_qt::block_creation block_creation;
rai_qt::block_entry block_entry;
rai_qt::block_viewer block_viewer;
rai_qt::account_viewer account_viewer;
rai_qt::import import;
QApplication & application;
QLabel * status;
QStackedWidget * main_stack;
QWidget * client_window;
QVBoxLayout * client_layout;
QWidget * entry_window;
QVBoxLayout * entry_window_layout;
QFrame * separator;
QLabel * account_history_label;
QPushButton * send_blocks;
QPushButton * settings_button;
QPushButton * accounts_button;
QPushButton * show_advanced;
QWidget * send_blocks_window;
QVBoxLayout * send_blocks_layout;
QLabel * send_account_label;
QLineEdit * send_account;
QLabel * send_count_label;
QLineEdit * send_count;
QPushButton * send_blocks_send;
QPushButton * send_blocks_back;
rai_qt::status active_status;
void pop_main_stack ();
void push_main_stack (QWidget *);
};
}

View file

@ -1,5 +1,5 @@
#include <rai/qt/qt.hpp>
#include <rai/node/testing.hpp>
#include <rai/qt/qt.hpp>
#include <thread>
@ -9,22 +9,21 @@ int main (int argc, char ** argv)
rai_qt::eventloop_processor processor;
static int count (16);
rai::system system (24000, count);
std::unique_ptr <QTabWidget> client_tabs (new QTabWidget);
std::vector <std::unique_ptr <rai_qt::wallet>> guis;
std::unique_ptr<QTabWidget> client_tabs (new QTabWidget);
std::vector<std::unique_ptr<rai_qt::wallet>> guis;
for (auto i (0); i < count; ++i)
{
rai::uint256_union wallet_id;
rai::random_pool.GenerateBlock (wallet_id.bytes.data (), wallet_id.bytes.size ());
auto wallet (system.nodes [i]->wallets.create (wallet_id));
auto wallet (system.nodes[i]->wallets.create (wallet_id));
rai::keypair key;
wallet->insert_adhoc (key.prv);
guis.push_back (std::unique_ptr <rai_qt::wallet> (new rai_qt::wallet (application, processor, *system.nodes [i], wallet, key.pub)));
guis.push_back (std::unique_ptr<rai_qt::wallet> (new rai_qt::wallet (application, processor, *system.nodes[i], wallet, key.pub)));
client_tabs->addTab (guis.back ()->client_window, boost::str (boost::format ("Wallet %1%") % i).c_str ());
}
client_tabs->show ();
rai::thread_runner runner (system.service, system.nodes [0]->config.io_threads);
QObject::connect (&application, &QApplication::aboutToQuit, [&] ()
{
rai::thread_runner runner (system.service, system.nodes[0]->config.io_threads);
QObject::connect (&application, &QApplication::aboutToQuit, [&]() {
system.stop ();
});
int result;

View file

@ -4,7 +4,7 @@
QApplication * test_application = nullptr;
int main (int argc, char **argv)
int main (int argc, char ** argv)
{
QApplication application (argc, argv);
test_application = &application;

View file

@ -1,12 +1,12 @@
#include <gtest/gtest.h>
#include <rai/qt/qt.hpp>
#include <rai/node/testing.hpp>
#include <rai/qt/qt.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <thread>
#include <QTest>
#include <thread>
extern QApplication * test_application;
@ -14,9 +14,9 @@ TEST (wallet, construction)
{
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto wallet_l (system.nodes [0]->wallets.create (rai::uint256_union ()));
auto wallet_l (system.nodes[0]->wallets.create (rai::uint256_union ()));
auto key (wallet_l->deterministic_insert ());
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], wallet_l, key));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], wallet_l, key));
wallet->start ();
ASSERT_EQ (key.to_account_split (), wallet->self.account_text->text ().toStdString ());
ASSERT_EQ (1, wallet->accounts.model->rowCount ());
@ -28,13 +28,13 @@ TEST (wallet, status)
{
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto wallet_l (system.nodes [0]->wallets.create (rai::uint256_union ()));
auto wallet_l (system.nodes[0]->wallets.create (rai::uint256_union ()));
rai::keypair key;
wallet_l->insert_adhoc (key.prv);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], wallet_l, key.pub));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], wallet_l, key.pub));
wallet->start ();
ASSERT_EQ ("Status: Disconnected, Block: 1", wallet->status->text ().toStdString ());
system.nodes [0]->peers.insert (rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000), 0);
system.nodes[0]->peers.insert (rai::endpoint (boost::asio::ip::address_v6::loopback (), 10000), 0);
ASSERT_NE ("Status: Synchronizing", wallet->status->text ().toStdString ());
auto iterations (0);
while (wallet->status->text ().toStdString () != "Status: Synchronizing")
@ -44,7 +44,7 @@ TEST (wallet, status)
++iterations;
ASSERT_LT (iterations, 200);
}
system.nodes [0]->peers.purge_list (std::chrono::system_clock::now () + std::chrono::seconds (5));
system.nodes[0]->peers.purge_list (std::chrono::system_clock::now () + std::chrono::seconds (5));
while (wallet->status->text ().toStdString () == "Status: Synchronizing")
{
test_application->processEvents ();
@ -56,22 +56,22 @@ TEST (wallet, startup_balance)
{
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto wallet_l (system.nodes [0]->wallets.create (rai::uint256_union ()));
auto wallet_l (system.nodes[0]->wallets.create (rai::uint256_union ()));
rai::keypair key;
wallet_l->insert_adhoc (key.prv);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], wallet_l, key.pub));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], wallet_l, key.pub));
wallet->start ();
ASSERT_EQ ("Balance (XRB): 0", wallet->self.balance_label->text().toStdString ());
ASSERT_EQ ("Balance (XRB): 0", wallet->self.balance_label->text ().toStdString ());
}
TEST (wallet, select_account)
{
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto wallet_l (system.nodes [0]->wallets.create (rai::uint256_union ()));
auto wallet_l (system.nodes[0]->wallets.create (rai::uint256_union ()));
rai::public_key key1 (wallet_l->deterministic_insert ());
rai::public_key key2 (wallet_l->deterministic_insert ());
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], wallet_l, key1));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], wallet_l, key1));
wallet->start ();
ASSERT_EQ (key1, wallet->account);
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
@ -89,10 +89,10 @@ TEST (wallet, main)
{
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto wallet_l (system.nodes [0]->wallets.create (rai::uint256_union ()));
auto wallet_l (system.nodes[0]->wallets.create (rai::uint256_union ()));
rai::keypair key;
wallet_l->insert_adhoc (key.prv);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], wallet_l, key.pub));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], wallet_l, key.pub));
wallet->start ();
ASSERT_EQ (wallet->entry_window, wallet->main_stack->currentWidget ());
QTest::mouseClick (wallet->send_blocks, Qt::LeftButton);
@ -123,14 +123,14 @@ TEST (wallet, password_change)
rai::account account;
system.wallet (0)->insert_adhoc (rai::keypair ().prv);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->settings_button, Qt::LeftButton);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
rai::raw_key password1;
rai::raw_key password2;
system.wallet (0)->store.derive_key (password1, transaction, "1");
@ -141,7 +141,7 @@ TEST (wallet, password_change)
QTest::keyClicks (wallet->settings.retype_password, "1");
QTest::mouseClick (wallet->settings.change, Qt::LeftButton);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
rai::raw_key password1;
rai::raw_key password2;
system.wallet (0)->store.derive_key (password1, transaction, "1");
@ -159,10 +159,10 @@ TEST (client, password_nochange)
rai::account account;
system.wallet (0)->insert_adhoc (rai::keypair ().prv);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->settings_button, Qt::LeftButton);
auto iterations (0);
@ -176,7 +176,7 @@ TEST (client, password_nochange)
system.wallet (0)->store.password.value (password);
}
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
rai::raw_key password1;
system.wallet (0)->store.derive_key (password1, transaction, "");
rai::raw_key password2;
@ -187,7 +187,7 @@ TEST (client, password_nochange)
QTest::keyClicks (wallet->settings.retype_password, "2");
QTest::mouseClick (wallet->settings.change, Qt::LeftButton);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
rai::raw_key password1;
system.wallet (0)->store.derive_key (password1, transaction, "");
rai::raw_key password2;
@ -205,29 +205,29 @@ TEST (wallet, enter_password)
rai::account account;
system.wallet (0)->insert_adhoc (rai::keypair ().prv);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
ASSERT_NE (-1, wallet->settings.layout->indexOf (wallet->settings.password));
ASSERT_NE (-1, wallet->settings.layout->indexOf (wallet->settings.lock_toggle));
ASSERT_NE (-1, wallet->settings.layout->indexOf (wallet->settings.back));
QTest::mouseClick (wallet->settings_button, Qt::LeftButton);
test_application->processEvents();
test_application->processEvents ();
ASSERT_EQ ("Status: Wallet password empty", wallet->status->text ().toStdString ());
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
ASSERT_FALSE (system.wallet (0)->store.rekey (transaction, "abc"));
}
QTest::mouseClick (wallet->settings_button, Qt::LeftButton);
QTest::mouseClick (wallet->settings.lock_toggle, Qt::LeftButton);
test_application->processEvents();
test_application->processEvents ();
ASSERT_EQ ("Status: Wallet locked", wallet->status->text ().toStdString ());
wallet->settings.new_password->setText ("");
QTest::keyClicks (wallet->settings.password, "abc");
QTest::mouseClick (wallet->settings.lock_toggle, Qt::LeftButton);
test_application->processEvents();
test_application->processEvents ();
ASSERT_EQ ("Status: Running", wallet->status->text ().toStdString ());
ASSERT_EQ ("", wallet->settings.password->text ());
}
@ -239,7 +239,7 @@ TEST (wallet, send)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
rai::public_key key1 (system.wallet (1)->insert_adhoc (rai::keypair ().prv));
auto account (rai::test_genesis_key.pub);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->send_blocks, Qt::LeftButton);
QTest::keyClicks (wallet->send_account, key1.to_account ().c_str ());
@ -272,7 +272,7 @@ TEST (wallet, send_locked)
rai::keypair key1;
system.wallet (0)->enter_password ("0");
auto account (rai::test_genesis_key.pub);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->send_blocks, Qt::LeftButton);
QTest::keyClicks (wallet->send_account, key1.pub.to_account ().c_str ());
@ -293,13 +293,13 @@ TEST (wallet, process_block)
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
rai::account account;
rai::block_hash latest (system.nodes [0]->latest (rai::genesis_account));
rai::block_hash latest (system.nodes[0]->latest (rai::genesis_account));
system.wallet (0)->insert_adhoc (rai::keypair ().prv);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
ASSERT_EQ ("Process", wallet->block_entry.process->text ());
ASSERT_EQ ("Back", wallet->block_entry.back->text ());
@ -318,8 +318,8 @@ TEST (wallet, process_block)
auto block_json (boost::str (boost::format ("{\"type\": \"send\", \"previous\": \"%1%\", \"balance\": \"%2%\", \"destination\": \"%3%\", \"work\": \"%4%\", \"signature\": \"%5%\"}") % previous % balance % send.hashables.destination.to_account () % rai::to_string_hex (send.work) % signature));
QTest::keyClicks (wallet->block_entry.block, block_json.c_str ());
QTest::mouseClick (wallet->block_entry.process, Qt::LeftButton);
ASSERT_EQ (send.hash (), system.nodes [0]->latest (rai::genesis_account));
QTest::mouseClick(wallet->block_entry.back, Qt::LeftButton);
ASSERT_EQ (send.hash (), system.nodes[0]->latest (rai::genesis_account));
QTest::mouseClick (wallet->block_entry.back, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
}
@ -331,7 +331,7 @@ TEST (wallet, create_send)
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->insert_adhoc (key.prv);
auto account (rai::test_genesis_key.pub);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
wallet->client_window->show ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
@ -349,8 +349,8 @@ TEST (wallet, create_send)
bool error;
rai::send_block send (error, tree1);
ASSERT_FALSE (error);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (send).code);
ASSERT_EQ (rai::process_result::old, system.nodes [0]->process (send).code);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (send).code);
ASSERT_EQ (rai::process_result::old, system.nodes[0]->process (send).code);
}
TEST (wallet, create_open_receive)
@ -360,13 +360,13 @@ TEST (wallet, create_open_receive)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
system.wallet (0)->send_action (rai::test_genesis_key.pub, key.pub, 100);
rai::block_hash latest1 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest1 (system.nodes[0]->latest (rai::test_genesis_key.pub));
system.wallet (0)->send_action (rai::test_genesis_key.pub, key.pub, 100);
rai::block_hash latest2 (system.nodes [0]->latest (rai::test_genesis_key.pub));
rai::block_hash latest2 (system.nodes[0]->latest (rai::test_genesis_key.pub));
ASSERT_NE (latest1, latest2);
system.wallet (0)->insert_adhoc (key.prv);
auto account (rai::test_genesis_key.pub);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
wallet->client_window->show ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
@ -383,8 +383,8 @@ TEST (wallet, create_open_receive)
bool error;
rai::open_block open (error, tree1);
ASSERT_FALSE (error);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (open).code);
ASSERT_EQ (rai::process_result::old, system.nodes [0]->process (open).code);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (open).code);
ASSERT_EQ (rai::process_result::old, system.nodes[0]->process (open).code);
wallet->block_creation.block->clear ();
wallet->block_creation.source->clear ();
QTest::mouseClick (wallet->block_creation.receive, Qt::LeftButton);
@ -398,8 +398,8 @@ TEST (wallet, create_open_receive)
bool error2;
rai::receive_block receive (error2, tree2);
ASSERT_FALSE (error2);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (receive).code);
ASSERT_EQ (rai::process_result::old, system.nodes [0]->process (receive).code);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (receive).code);
ASSERT_EQ (rai::process_result::old, system.nodes[0]->process (receive).code);
}
TEST (wallet, create_change)
@ -409,7 +409,7 @@ TEST (wallet, create_change)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto account (rai::test_genesis_key.pub);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
wallet->client_window->show ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
@ -426,8 +426,8 @@ TEST (wallet, create_change)
bool error (false);
rai::change_block change (error, tree1);
ASSERT_FALSE (error);
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->process (change).code);
ASSERT_EQ (rai::process_result::old, system.nodes [0]->process (change).code);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->process (change).code);
ASSERT_EQ (rai::process_result::old, system.nodes[0]->process (change).code);
}
TEST (history, short_text)
@ -439,10 +439,10 @@ TEST (history, short_text)
system.wallet (0)->insert_adhoc (key.prv);
rai::account account;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
rai::block_store store (init, rai::unique_path ());
ASSERT_TRUE (!init);
rai::genesis genesis;
@ -471,15 +471,15 @@ TEST (wallet, startup_work)
system.wallet (0)->insert_adhoc (key.prv);
rai::account account;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
uint64_t work1;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
ASSERT_TRUE (wallet->wallet_m->store.work_get (transaction, rai::test_genesis_key.pub, work1));
}
QTest::mouseClick (wallet->accounts_button, Qt::LeftButton);
@ -492,7 +492,7 @@ TEST (wallet, startup_work)
system.poll ();
++iterations1;
ASSERT_LT (iterations1, 200);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
again = wallet->wallet_m->store.work_get (transaction, rai::test_genesis_key.pub, work1);
}
}
@ -505,16 +505,16 @@ TEST (wallet, block_viewer)
system.wallet (0)->insert_adhoc (key.prv);
rai::account account;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
account = system.account (transaction, 0);
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_NE (-1, wallet->advanced.layout->indexOf (wallet->advanced.block_viewer));
QTest::mouseClick (wallet->advanced.block_viewer, Qt::LeftButton);
ASSERT_EQ (wallet->block_viewer.window, wallet->main_stack->currentWidget ());
rai::block_hash latest (system.nodes [0]->latest (rai::genesis_account));
rai::block_hash latest (system.nodes[0]->latest (rai::genesis_account));
QTest::keyClicks (wallet->block_viewer.hash, latest.to_string ().c_str ());
QTest::mouseClick (wallet->block_viewer.retrieve, Qt::LeftButton);
ASSERT_FALSE (wallet->block_viewer.block->toPlainText ().toStdString ().empty ());
@ -531,7 +531,7 @@ TEST (wallet, import)
rai::keypair key2;
system.wallet (0)->insert_adhoc (key1.prv);
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
system.wallet (0)->store.serialize_json (transaction, json);
}
system.wallet (1)->insert_adhoc (key2.prv);
@ -541,7 +541,7 @@ TEST (wallet, import)
stream.open (path.string ().c_str ());
stream << json;
}
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [1], system.wallet (1), key2.pub));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[1], system.wallet (1), key2.pub));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -564,13 +564,13 @@ TEST (wallet, republish)
rai::keypair key;
rai::block_hash hash;
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
rai::send_block block (system.nodes [0]->ledger.latest (transaction, rai::test_genesis_key.pub), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
rai::send_block block (system.nodes[0]->ledger.latest (transaction, rai::test_genesis_key.pub), key.pub, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0);
hash = block.hash ();
ASSERT_EQ (rai::process_result::progress, system.nodes [0]->ledger.process (transaction, block).code);
ASSERT_EQ (rai::process_result::progress, system.nodes[0]->ledger.process (transaction, block).code);
}
auto account (rai::test_genesis_key.pub);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), account));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), account));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -578,9 +578,9 @@ TEST (wallet, republish)
ASSERT_EQ (wallet->block_viewer.window, wallet->main_stack->currentWidget ());
QTest::keyClicks (wallet->block_viewer.hash, hash.to_string ().c_str ());
QTest::mouseClick (wallet->block_viewer.rebroadcast, Qt::LeftButton);
ASSERT_FALSE (system.nodes [1]->balance (rai::test_genesis_key.pub).is_zero ());
ASSERT_FALSE (system.nodes[1]->balance (rai::test_genesis_key.pub).is_zero ());
int iterations (0);
while (system.nodes [1]->balance (rai::test_genesis_key.pub).is_zero ())
while (system.nodes[1]->balance (rai::test_genesis_key.pub).is_zero ())
{
++iterations;
ASSERT_LT (iterations, 200);
@ -594,7 +594,7 @@ TEST (wallet, ignore_empty_adhoc)
rai::system system (24000, 1);
rai::keypair key1;
system.wallet (0)->insert_adhoc (key1.prv);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), key1.pub));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), key1.pub));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -622,7 +622,7 @@ TEST (wallet, change_seed)
rai::raw_key seed3;
system.wallet (0)->store.seed (seed3, rai::transaction (system.wallet (0)->store.environment, nullptr, false));
auto wallet_key (key1);
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), wallet_key));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), wallet_key));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -664,7 +664,7 @@ TEST (wallet, seed_work_generation)
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto key1 (system.wallet (0)->deterministic_insert ());
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), key1));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), key1));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -688,7 +688,7 @@ TEST (wallet, seed_work_generation)
++iterations;
ASSERT_LT (iterations, 200);
}
ASSERT_FALSE (rai::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)
@ -696,7 +696,7 @@ TEST (wallet, backup_seed)
rai_qt::eventloop_processor processor;
rai::system system (24000, 1);
auto key1 (system.wallet (0)->deterministic_insert ());
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), key1));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), key1));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -714,7 +714,7 @@ TEST (wallet, import_locked)
rai::system system (24000, 1);
auto key1 (system.wallet (0)->deterministic_insert ());
system.wallet (0)->store.rekey (rai::transaction (system.wallet (0)->store.environment, nullptr, true), "1");
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system.nodes [0], system.wallet (0), key1));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system.nodes[0], system.wallet (0), key1));
wallet->start ();
QTest::mouseClick (wallet->show_advanced, Qt::LeftButton);
ASSERT_EQ (wallet->advanced.window, wallet->main_stack->currentWidget ());
@ -742,22 +742,22 @@ TEST (wallet, synchronizing)
rai::system system0 (24000, 1);
rai::system system1 (24001, 1);
auto key1 (system0.wallet (0)->deterministic_insert ());
auto wallet (std::make_shared <rai_qt::wallet> (*test_application, processor, *system0.nodes [0], system0.wallet (0), key1));
auto wallet (std::make_shared<rai_qt::wallet> (*test_application, processor, *system0.nodes[0], system0.wallet (0), key1));
wallet->start ();
{
rai::transaction transaction (system1.nodes [0]->store.environment, nullptr, true);
auto latest (system1.nodes [0]->ledger.latest (transaction, rai::genesis_account));
rai::transaction transaction (system1.nodes[0]->store.environment, nullptr, true);
auto latest (system1.nodes[0]->ledger.latest (transaction, rai::genesis_account));
rai::send_block send (latest, key1, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, system1.work.generate (latest));
system1.nodes [0]->ledger.process (transaction, send);
system1.nodes[0]->ledger.process (transaction, send);
}
ASSERT_EQ (0, wallet->active_status.active.count (rai_qt::status_types::synchronizing));
system0.nodes [0]->bootstrap_initiator.bootstrap (system1.nodes [0]->network.endpoint ());
system0.nodes[0]->bootstrap_initiator.bootstrap (system1.nodes[0]->network.endpoint ());
auto iterations0 (0);
while (wallet->active_status.active.count (rai_qt::status_types::synchronizing) == 0)
{
system0.poll ();
system1.poll ();
test_application->processEvents();
test_application->processEvents ();
++iterations0;
ASSERT_GT (200, iterations0);
}
@ -766,7 +766,7 @@ TEST (wallet, synchronizing)
{
system0.poll ();
system1.poll ();
test_application->processEvents();
test_application->processEvents ();
++iterations1;
ASSERT_GT (200, iterations1);
}

View file

@ -1,9 +1,9 @@
#include <rai/rai_node/daemon.hpp>
#include <rai/node/working.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <iostream>
#include <fstream>
#include <iostream>
#include <rai/node/working.hpp>
rai_daemon::daemon_config::daemon_config (boost::filesystem::path const & application_path_a) :
rpc_enable (false),
@ -34,19 +34,19 @@ bool rai_daemon::daemon_config::deserialize_json (bool & upgraded_a, boost::prop
{
if (!tree_a.empty ())
{
auto version_l (tree_a.get_optional <std::string> ("version"));
auto version_l (tree_a.get_optional<std::string> ("version"));
if (!version_l)
{
tree_a.put ("version", "1");
version_l = "1";
}
upgraded_a |= upgrade_json (std::stoull (version_l.get ()), tree_a);
rpc_enable = tree_a.get <bool> ("rpc_enable");
rpc_enable = tree_a.get<bool> ("rpc_enable");
auto rpc_l (tree_a.get_child ("rpc"));
error |= rpc.deserialize_json (rpc_l);
auto & node_l (tree_a.get_child ("node"));
error |= node.deserialize_json (upgraded_a, node_l);
opencl_enable = tree_a.get <bool> ("opencl_enable");
opencl_enable = tree_a.get<bool> ("opencl_enable");
auto & opencl_l (tree_a.get_child ("opencl"));
error |= opencl.deserialize_json (opencl_l);
}
@ -63,33 +63,32 @@ bool rai_daemon::daemon_config::deserialize_json (bool & upgraded_a, boost::prop
return error;
}
bool rai_daemon::daemon_config::upgrade_json (unsigned version_a, boost::property_tree::ptree & tree_a)
{
auto result (false);
switch (version_a)
{
case 1:
{
auto opencl_enable_l (tree_a.get_optional <bool> ("opencl_enable"));
if (!opencl_enable_l)
case 1:
{
tree_a.put ("opencl_enable", "false");
auto opencl_enable_l (tree_a.get_optional<bool> ("opencl_enable"));
if (!opencl_enable_l)
{
tree_a.put ("opencl_enable", "false");
}
auto opencl_l (tree_a.get_child_optional ("opencl"));
if (!opencl_l)
{
boost::property_tree::ptree opencl_l;
opencl.serialize_json (opencl_l);
tree_a.put_child ("opencl", opencl_l);
}
tree_a.put ("version", "2");
result = true;
}
auto opencl_l (tree_a.get_child_optional ("opencl"));
if (!opencl_l)
{
boost::property_tree::ptree opencl_l;
opencl.serialize_json (opencl_l);
tree_a.put_child ("opencl", opencl_l);
}
tree_a.put ("version", "2");
result = true;
}
case 2:
break;
default:
throw std::runtime_error ("Unknown daemon_config version");
case 2:
break;
default:
throw std::runtime_error ("Unknown daemon_config version");
}
return result;
}
@ -100,7 +99,7 @@ void rai_daemon::daemon::run (boost::filesystem::path const & data_path)
rai_daemon::daemon_config config (data_path);
auto config_path ((data_path / "config.json"));
std::fstream config_file;
std::unique_ptr <rai::thread_runner> runner;
std::unique_ptr<rai::thread_runner> runner;
auto error (rai::fetch_object (config, config_path, config_file));
if (!error)
{
@ -108,15 +107,15 @@ void rai_daemon::daemon::run (boost::filesystem::path const & data_path)
config_file.close ();
boost::asio::io_service service;
auto opencl (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging));
rai::work_pool opencl_work (config.node.work_threads, opencl ? [&opencl] (rai::uint256_union const & root_a)
{
rai::work_pool opencl_work (config.node.work_threads, opencl ? [&opencl](rai::uint256_union const & root_a) {
return opencl->generate_work (root_a);
} : std::function <boost::optional <uint64_t> (rai::uint256_union const &)> (nullptr));
}
: std::function<boost::optional<uint64_t> (rai::uint256_union const &)> (nullptr));
rai::alarm alarm (service);
rai::node_init init;
try
{
auto node (std::make_shared <rai::node> (init, service, data_path, alarm, config.node, opencl_work));
auto node (std::make_shared<rai::node> (init, service, data_path, alarm, config.node, opencl_work));
if (!init.error ())
{
node->start ();
@ -133,9 +132,9 @@ void rai_daemon::daemon::run (boost::filesystem::path const & data_path)
std::cerr << "Error initializing node\n";
}
}
catch(const std::runtime_error& e)
catch (const std::runtime_error & e)
{
std::cerr << "Error while running node (" << e.what() << ")\n";
std::cerr << "Error while running node (" << e.what () << ")\n";
}
}
else

View file

@ -3,22 +3,22 @@
namespace rai_daemon
{
class daemon
{
public:
void run (boost::filesystem::path const &);
};
class daemon_config
{
public:
daemon_config (boost::filesystem::path const &);
bool deserialize_json (bool &, boost::property_tree::ptree &);
void serialize_json (boost::property_tree::ptree &);
bool upgrade_json (unsigned, boost::property_tree::ptree &);
bool rpc_enable;
rai::rpc_config rpc;
rai::node_config node;
bool opencl_enable;
rai::opencl_config opencl;
};
class daemon
{
public:
void run (boost::filesystem::path const &);
};
class daemon_config
{
public:
daemon_config (boost::filesystem::path const &);
bool deserialize_json (bool &, boost::property_tree::ptree &);
void serialize_json (boost::property_tree::ptree &);
bool upgrade_json (unsigned, boost::property_tree::ptree &);
bool rpc_enable;
rai::rpc_config rpc;
rai::node_config node;
bool opencl_enable;
rai::opencl_config opencl;
};
}

View file

@ -4,45 +4,47 @@
#include <argon2.h>
#include <boost/program_options.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/program_options.hpp>
class xorshift128
{
public:
uint64_t s[ 2 ];
uint64_t next(void) {
uint64_t s1 = s[ 0 ];
const uint64_t s0 = s[ 1 ];
s[ 0 ] = s0;
uint64_t s[2];
uint64_t next (void)
{
uint64_t s1 = s[0];
const uint64_t s0 = s[1];
s[0] = s0;
s1 ^= s1 << 23; // a
return ( s[ 1 ] = ( s1 ^ s0 ^ ( s1 >> 17 ) ^ ( s0 >> 26 ) ) ) + s0; // b, c
return (s[1] = (s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26))) + s0; // b, c
}
};
class xorshift1024
{
public:
uint64_t s[ 16 ];
uint64_t s[16];
int p;
uint64_t next(void) {
uint64_t s0 = s[ p ];
uint64_t s1 = s[ p = ( p + 1 ) & 15 ];
uint64_t next (void)
{
uint64_t s0 = s[p];
uint64_t s1 = s[p = (p + 1) & 15];
s1 ^= s1 << 31; // a
s1 ^= s1 >> 11; // b
s0 ^= s0 >> 30; // c
return ( s[ p ] = s0 ^ s1 ) * 1181783497276652981LL;
return (s[p] = s0 ^ s1) * 1181783497276652981LL;
}
};
void fill_128_reference (void * data)
{
xorshift128 rng;
rng.s [0] = 1;
rng.s [1] = 0;
for (auto i (reinterpret_cast <uint64_t *> (data)), n (reinterpret_cast <uint64_t *> (data) + 1024 * 1024); i != n; ++i)
rng.s[0] = 1;
rng.s[1] = 0;
for (auto i (reinterpret_cast<uint64_t *> (data)), n (reinterpret_cast<uint64_t *> (data) + 1024 * 1024); i != n; ++i)
{
*i = rng.next ();
}
@ -67,12 +69,12 @@ void fill_1024_reference (void * data)
{
xorshift1024 rng;
rng.p = 0;
rng.s [0] = 1;
rng.s[0] = 1;
for (auto i (0u); i < 16; ++i)
{
rng.s [i] = 0;
rng.s[i] = 0;
}
for (auto i (reinterpret_cast <uint64_t *> (data)), n (reinterpret_cast <uint64_t *> (data) + 1024 * 1024); i != n; ++i)
for (auto i (reinterpret_cast<uint64_t *> (data)), n (reinterpret_cast<uint64_t *> (data) + 1024 * 1024); i != n; ++i)
{
*i = rng.next ();
}
@ -109,26 +111,9 @@ int main (int argc, char * const * argv)
{
boost::program_options::options_description description ("Command line options");
rai::add_node_options (description);
description.add_options ()
("help", "Print out options")
("daemon", "Start node daemon")
("debug_block_count", "Display the number of block")
("debug_bootstrap_generate", "Generate bootstrap sequence of blocks")
("debug_dump_representatives", "List representatives and weights")
("debug_frontier_count", "Display the number of accounts")
("debug_mass_activity", "Generates fake debug activity")
("debug_profile_generate", "Profile work generation")
("debug_opencl", "OpenCL work generation")
("debug_profile_verify", "Profile work verification")
("debug_profile_kdf", "Profile kdf function")
("debug_verify_profile", "Profile signature verification")
("debug_profile_sign", "Profile signature generation")
("debug_xorshift_profile", "Profile xorshift algorithms")
("platform", boost::program_options::value <std::string> (), "Defines the <platform> for OpenCL commands")
("device", boost::program_options::value <std::string> (), "Defines <device> for OpenCL command")
("threads", boost::program_options::value <std::string> (), "Defines <threads> count for OpenCL command");
description.add_options () ("help", "Print out options") ("daemon", "Start node daemon") ("debug_block_count", "Display the number of block") ("debug_bootstrap_generate", "Generate bootstrap sequence of blocks") ("debug_dump_representatives", "List representatives and weights") ("debug_frontier_count", "Display the number of accounts") ("debug_mass_activity", "Generates fake debug activity") ("debug_profile_generate", "Profile work generation") ("debug_opencl", "OpenCL work generation") ("debug_profile_verify", "Profile work verification") ("debug_profile_kdf", "Profile kdf function") ("debug_verify_profile", "Profile signature verification") ("debug_profile_sign", "Profile signature generation") ("debug_xorshift_profile", "Profile xorshift algorithms") ("platform", boost::program_options::value<std::string> (), "Defines the <platform> for OpenCL commands") ("device", boost::program_options::value<std::string> (), "Defines <device> for OpenCL command") ("threads", boost::program_options::value<std::string> (), "Defines <threads> count for OpenCL command");
boost::program_options::variables_map vm;
boost::program_options::store (boost::program_options::parse_command_line(argc, argv, description), vm);
boost::program_options::store (boost::program_options::parse_command_line (argc, argv, description), vm);
boost::program_options::notify (vm);
int result (0);
if (!rai::handle_node_options (vm))
@ -139,7 +124,7 @@ int main (int argc, char * const * argv)
boost::filesystem::path data_path;
if (vm.count ("data_path"))
{
data_path = boost::filesystem::path (vm ["data_path"].as <std::string> ());
data_path = boost::filesystem::path (vm["data_path"].as<std::string> ());
}
else
{
@ -159,19 +144,25 @@ int main (int argc, char * const * argv)
if (vm.count ("key") == 1)
{
rai::uint256_union key;
if (!key.decode_hex (vm ["key"].as <std::string> ()))
if (!key.decode_hex (vm["key"].as<std::string> ()))
{
rai::keypair genesis (key.to_string ());
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
std::cout << "Genesis: " << genesis.prv.data.to_string () << std::endl << "Public: " << genesis.pub.to_string () << std::endl << "Account: " << genesis.pub.to_account () << std::endl;
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
std::cout << "Genesis: " << genesis.prv.data.to_string () << std::endl
<< "Public: " << genesis.pub.to_string () << std::endl
<< "Account: " << genesis.pub.to_account () << std::endl;
rai::keypair landing;
std::cout << "Landing: " << landing.prv.data.to_string () << std::endl << "Public: " << landing.pub.to_string () << std::endl << "Account: " << landing.pub.to_account () << std::endl;
std::cout << "Landing: " << landing.prv.data.to_string () << std::endl
<< "Public: " << landing.pub.to_string () << std::endl
<< "Account: " << landing.pub.to_account () << std::endl;
for (auto i (0); i != 32; ++i)
{
rai::keypair rep;
std::cout << "Rep" << i << ": " << rep.prv.data.to_string () << std::endl << "Public: " << rep.pub.to_string () << std::endl << "Account: " << rep.pub.to_account () << std::endl;
std::cout << "Rep" << i << ": " << rep.prv.data.to_string () << std::endl
<< "Public: " << rep.pub.to_string () << std::endl
<< "Account: " << rep.pub.to_account () << std::endl;
}
rai::uint128_t balance (std::numeric_limits <rai::uint128_t>::max ());
rai::uint128_t balance (std::numeric_limits<rai::uint128_t>::max ());
rai::open_block genesis_block (genesis.pub, genesis.pub, genesis.pub, genesis.prv, genesis.pub, work.generate (genesis.pub));
std::cout << genesis_block.to_json ();
rai::block_hash previous (genesis_block.hash ());
@ -207,27 +198,27 @@ int main (int argc, char * const * argv)
rai::inactive_node node;
rai::transaction transaction (node.node->store.environment, nullptr, false);
rai::uint128_t total;
for (auto i(node.node->store.representation_begin(transaction)), n(node.node->store.representation_end()); i != n; ++i)
for (auto i (node.node->store.representation_begin (transaction)), n (node.node->store.representation_end ()); i != n; ++i)
{
rai::account account (i->first.uint256 ());
auto amount (node.node->store.representation_get(transaction, account));
auto amount (node.node->store.representation_get (transaction, account));
total += amount;
std::cout << boost::str(boost::format("%1% %2% %3%\n") % account.to_account () % amount.convert_to <std::string> () % total.convert_to<std::string> ());
std::cout << boost::str (boost::format ("%1% %2% %3%\n") % account.to_account () % amount.convert_to<std::string> () % total.convert_to<std::string> ());
}
std::map <rai::account, rai::uint128_t> calculated;
std::map<rai::account, rai::uint128_t> calculated;
for (auto i (node.node->store.latest_begin (transaction)), n (node.node->store.latest_end ()); i != n; ++i)
{
rai::account account (i->first.uint256 ());
rai::account_info info (i->second);
rai::block_hash rep_block (node.node->ledger.representative_calculated (transaction, info.head));
std::unique_ptr <rai::block> block (node.node->store.block_get (transaction, rep_block));
calculated [block->representative()] += info.balance.number();
std::unique_ptr<rai::block> block (node.node->store.block_get (transaction, rep_block));
calculated[block->representative ()] += info.balance.number ();
}
total = 0;
for (auto i (calculated.begin ()), n (calculated.end ()); i != n; ++i)
{
total += i->second;
std::cout << boost::str(boost::format("%1% %2% %3%\n") % i->first.to_account () % i->second.convert_to <std::string> () % total.convert_to<std::string> ());
std::cout << boost::str (boost::format ("%1% %2% %3%\n") % i->first.to_account () % i->second.convert_to<std::string> () % total.convert_to<std::string> ());
}
}
else if (vm.count ("debug_frontier_count"))
@ -240,7 +231,7 @@ int main (int argc, char * const * argv)
{
rai::system system (24000, 1);
size_t count (1000000);
system.generate_mass_activity (count, *system.nodes [0]);
system.generate_mass_activity (count, *system.nodes[0]);
}
else if (vm.count ("debug_profile_kdf"))
{
@ -252,21 +243,21 @@ int main (int argc, char * const * argv)
auto begin1 (std::chrono::high_resolution_clock::now ());
auto success (argon2_hash (1, rai::wallet_store::kdf_work, 1, password.data (), password.size (), salt.bytes.data (), salt.bytes.size (), result.bytes.data (), result.bytes.size (), NULL, 0, Argon2_d, 0x10));
auto end1 (std::chrono::high_resolution_clock::now ());
std::cerr << boost::str (boost::format ("Derivation time: %1%us\n") % std::chrono::duration_cast <std::chrono::microseconds> (end1 - begin1).count ());
std::cerr << boost::str (boost::format ("Derivation time: %1%us\n") % std::chrono::duration_cast<std::chrono::microseconds> (end1 - begin1).count ());
}
}
else if (vm.count ("debug_profile_generate"))
{
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
rai::change_block block (0, 0, rai::keypair ().prv, 0, 0);
std::cerr << "Starting generation profiling\n";
for (uint64_t i (0); true; ++i)
{
block.hashables.previous.qwords [0] += 1;
block.hashables.previous.qwords[0] += 1;
auto begin1 (std::chrono::high_resolution_clock::now ());
block.block_work_set (work.generate (block.root ()));
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 ());
std::cerr << boost::str (boost::format ("%|1$ 12d|\n") % std::chrono::duration_cast<std::chrono::microseconds> (end1 - begin1).count ());
}
}
else if (vm.count ("debug_opencl"))
@ -278,10 +269,12 @@ int main (int argc, char * const * argv)
unsigned short platform (0);
if (vm.count ("platform") == 1)
{
try {
platform = boost::lexical_cast <unsigned short> (vm ["platform"].as <std::string> ());
try
{
platform = boost::lexical_cast<unsigned short> (vm["platform"].as<std::string> ());
}
catch (boost::bad_lexical_cast & e ) {
catch (boost::bad_lexical_cast & e)
{
std::cerr << "Invalid platform id\n";
result = -1;
}
@ -289,10 +282,12 @@ int main (int argc, char * const * argv)
unsigned short device (0);
if (vm.count ("device") == 1)
{
try {
device = boost::lexical_cast <unsigned short> (vm ["device"].as <std::string> ());
try
{
device = boost::lexical_cast<unsigned short> (vm["device"].as<std::string> ());
}
catch (boost::bad_lexical_cast & e ) {
catch (boost::bad_lexical_cast & e)
{
std::cerr << "Invalid device id\n";
result = -1;
}
@ -300,10 +295,12 @@ int main (int argc, char * const * argv)
unsigned threads (1024 * 1024);
if (vm.count ("threads") == 1)
{
try {
threads = boost::lexical_cast <unsigned> (vm ["threads"].as <std::string> ());
try
{
threads = boost::lexical_cast<unsigned> (vm["threads"].as<std::string> ());
}
catch (boost::bad_lexical_cast & e ) {
catch (boost::bad_lexical_cast & e)
{
std::cerr << "Invalid threads count\n";
result = -1;
}
@ -317,31 +314,33 @@ int main (int argc, char * const * argv)
if (!error)
{
rai::logging logging;
auto opencl (rai::opencl_work::create (true, {platform, device, threads}, logging));
rai::work_pool work_pool (std::numeric_limits <unsigned>::max (), opencl ? [&opencl] (rai::uint256_union const & root_a)
{
auto opencl (rai::opencl_work::create (true, { platform, device, threads }, logging));
rai::work_pool work_pool (std::numeric_limits<unsigned>::max (), opencl ? [&opencl](rai::uint256_union const & root_a) {
return opencl->generate_work (root_a);
} : std::function <boost::optional <uint64_t> (rai::uint256_union const &)> (nullptr));
}
: std::function<boost::optional<uint64_t> (rai::uint256_union const &)> (nullptr));
rai::change_block block (0, 0, rai::keypair ().prv, 0, 0);
std::cerr << boost::str (boost::format ("Starting OpenCL generation profiling. Platform: %1%. Device: %2%. Threads: %3%\n") % platform % device % threads);
for (uint64_t i (0); true; ++i)
{
block.hashables.previous.qwords [0] += 1;
block.hashables.previous.qwords[0] += 1;
auto begin1 (std::chrono::high_resolution_clock::now ());
block.block_work_set (work_pool.generate (block.root ()));
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 ());
std::cerr << boost::str (boost::format ("%|1$ 12d|\n") % std::chrono::duration_cast<std::chrono::microseconds> (end1 - begin1).count ());
}
}
else
{
std::cout << "Not available device id\n" << std::endl;
std::cout << "Not available device id\n"
<< std::endl;
result = -1;
}
}
else
{
std::cout << "Not available platform id\n" << std::endl;
std::cout << "Not available platform id\n"
<< std::endl;
result = -1;
}
}
@ -354,21 +353,21 @@ int main (int argc, char * const * argv)
}
else if (vm.count ("debug_profile_verify"))
{
rai::work_pool work (std::numeric_limits <unsigned>::max (), nullptr);
rai::work_pool work (std::numeric_limits<unsigned>::max (), nullptr);
rai::change_block block (0, 0, rai::keypair ().prv, 0, 0);
std::cerr << "Starting verification profiling\n";
for (uint64_t i (0); true; ++i)
{
block.hashables.previous.qwords [0] += 1;
block.hashables.previous.qwords[0] += 1;
auto begin1 (std::chrono::high_resolution_clock::now ());
for (uint64_t t (0); t < 1000000; ++t)
{
block.hashables.previous.qwords [0] += 1;
block.hashables.previous.qwords[0] += 1;
block.block_work_set (t);
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 ());
std::cerr << boost::str (boost::format ("%|1$ 12d|\n") % std::chrono::duration_cast<std::chrono::microseconds> (end1 - begin1).count ());
}
}
else if (vm.count ("debug_verify_profile"))
@ -383,7 +382,7 @@ int main (int argc, char * const * argv)
rai::validate_message (key.pub, message, signature);
}
auto end (std::chrono::high_resolution_clock::now ());
std::cerr << "Signature verifications " << std::chrono::duration_cast <std::chrono::microseconds> (end - begin).count () << std::endl;
std::cerr << "Signature verifications " << std::chrono::duration_cast<std::chrono::microseconds> (end - begin).count () << std::endl;
}
else if (vm.count ("debug_profile_sign"))
{
@ -399,10 +398,10 @@ int main (int argc, char * const * argv)
latest = send.hash ();
}
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 ());
std::cerr << boost::str (boost::format ("%|1$ 12d|\n") % std::chrono::duration_cast<std::chrono::microseconds> (end1 - begin1).count ());
}
}
#if 0
#if 0
else if (vm.count ("debug_xorshift_profile"))
{
auto unaligned (new uint8_t [64 * 1024 * 1024 + 16]);
@ -458,7 +457,7 @@ int main (int argc, char * const * argv)
std::cerr << "SSE fill 1024 " << std::chrono::duration_cast <std::chrono::microseconds> (end - begin).count () << std::endl;
}
}
#endif // 0
#endif // 0
else
{
std::cout << description << std::endl;

126
rai/rai_wallet/entry.cpp Executable file → Normal file
View file

@ -1,14 +1,14 @@
#include <rai/qt/qt.hpp>
#include <rai/node/working.hpp>
#include <rai/icon.hpp>
#include <rai/node/rpc.hpp>
#include <rai/node/working.hpp>
#include <boost/make_shared.hpp>
#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
class qt_wallet_config
{
@ -26,47 +26,47 @@ public:
auto result (false);
switch (version_a)
{
case 1:
{
rai::account account;
account.decode_account (tree_a.get <std::string> ("account"));
tree_a.erase ("account");
tree_a.put ("account", account.to_account ());
tree_a.erase ("version");
tree_a.put ("version", "2");
result = true;
}
case 2:
{
boost::property_tree::ptree rpc_l;
rpc.serialize_json (rpc_l);
tree_a.put ("rpc_enable", "false");
tree_a.put_child ("rpc", rpc_l);
tree_a.erase ("version");
tree_a.put ("version", "3");
result = true;
}
case 3:
{
auto opencl_enable_l (tree_a.get_optional <bool> ("opencl_enable"));
if (!opencl_enable_l)
case 1:
{
tree_a.put ("opencl_enable", "false");
rai::account account;
account.decode_account (tree_a.get<std::string> ("account"));
tree_a.erase ("account");
tree_a.put ("account", account.to_account ());
tree_a.erase ("version");
tree_a.put ("version", "2");
result = true;
}
auto opencl_l (tree_a.get_child_optional ("opencl"));
if (!opencl_l)
case 2:
{
boost::property_tree::ptree opencl_l;
opencl.serialize_json (opencl_l);
tree_a.put_child ("opencl", opencl_l);
boost::property_tree::ptree rpc_l;
rpc.serialize_json (rpc_l);
tree_a.put ("rpc_enable", "false");
tree_a.put_child ("rpc", rpc_l);
tree_a.erase ("version");
tree_a.put ("version", "3");
result = true;
}
tree_a.put ("version", "4");
result = true;
}
case 4:
break;
default:
throw std::runtime_error ("Unknown qt_wallet_config version");
case 3:
{
auto opencl_enable_l (tree_a.get_optional<bool> ("opencl_enable"));
if (!opencl_enable_l)
{
tree_a.put ("opencl_enable", "false");
}
auto opencl_l (tree_a.get_child_optional ("opencl"));
if (!opencl_l)
{
boost::property_tree::ptree opencl_l;
opencl.serialize_json (opencl_l);
tree_a.put_child ("opencl", opencl_l);
}
tree_a.put ("version", "4");
result = true;
}
case 4:
break;
default:
throw std::runtime_error ("Unknown qt_wallet_config version");
}
return result;
}
@ -75,7 +75,7 @@ public:
auto error (false);
if (!tree_a.empty ())
{
auto version_l (tree_a.get_optional <std::string> ("version"));
auto version_l (tree_a.get_optional<std::string> ("version"));
if (!version_l)
{
tree_a.put ("version", "1");
@ -83,12 +83,12 @@ public:
upgraded_a = true;
}
upgraded_a |= upgrade_json (std::stoull (version_l.get ()), tree_a);
auto wallet_l (tree_a.get <std::string> ("wallet"));
auto account_l (tree_a.get <std::string> ("account"));
auto wallet_l (tree_a.get<std::string> ("wallet"));
auto account_l (tree_a.get<std::string> ("account"));
auto & node_l (tree_a.get_child ("node"));
rpc_enable = tree_a.get <bool> ("rpc_enable");
rpc_enable = tree_a.get<bool> ("rpc_enable");
auto & rpc_l (tree_a.get_child ("rpc"));
opencl_enable = tree_a.get <bool> ("opencl_enable");
opencl_enable = tree_a.get<bool> ("opencl_enable");
auto & opencl_l (tree_a.get_child ("opencl"));
try
{
@ -192,12 +192,12 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
{
rai_qt::eventloop_processor processor;
boost::filesystem::create_directories (data_path);
QPixmap pixmap(":/logo.png");
QSplashScreen *splash = new QSplashScreen(pixmap);
splash->show();
application.processEvents();
splash->showMessage(QSplashScreen::tr("Remember - Backup Your Wallet Seed"), Qt::AlignBottom | Qt::AlignHCenter, Qt::black);
application.processEvents();
QPixmap pixmap (":/logo.png");
QSplashScreen * splash = new QSplashScreen (pixmap);
splash->show ();
application.processEvents ();
splash->showMessage (QSplashScreen::tr ("Remember - Backup Your Wallet Seed"), Qt::AlignBottom | Qt::AlignHCenter, Qt::black);
application.processEvents ();
qt_wallet_config config (data_path);
auto config_path ((data_path / "config.json"));
int result (0);
@ -208,17 +208,17 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
{
boost::asio::io_service service;
config.node.logging.init (data_path);
std::shared_ptr <rai::node> node;
std::shared_ptr <rai_qt::wallet> gui;
std::shared_ptr<rai::node> node;
std::shared_ptr<rai_qt::wallet> gui;
rai::set_application_icon (application);
auto opencl (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging));
rai::work_pool work (config.node.work_threads, opencl ? [&opencl] (rai::uint256_union const & root_a)
{
rai::work_pool work (config.node.work_threads, opencl ? [&opencl](rai::uint256_union const & root_a) {
return opencl->generate_work (root_a);
} : std::function <boost::optional <uint64_t> (rai::uint256_union const &)> (nullptr));
}
: std::function<boost::optional<uint64_t> (rai::uint256_union const &)> (nullptr));
rai::alarm alarm (service);
rai::node_init init;
node = std::make_shared <rai::node> (init, service, data_path, alarm, config.node, work);
node = std::make_shared<rai::node> (init, service, data_path, alarm, config.node, work);
if (!init.error ())
{
auto wallet (node->wallets.open (config.wallet));
@ -258,15 +258,13 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
rpc.start ();
}
rai::thread_runner runner (service, node->config.io_threads);
QObject::connect (&application, &QApplication::aboutToQuit, [&] ()
{
QObject::connect (&application, &QApplication::aboutToQuit, [&]() {
rpc.stop ();
node->stop ();
});
application.postEvent (&processor, new rai_qt::eventloop_event ([&] ()
{
gui = std::make_shared <rai_qt::wallet> (application, processor, *node, wallet, config.account);
splash->close();
application.postEvent (&processor, new rai_qt::eventloop_event ([&]() {
gui = std::make_shared<rai_qt::wallet> (application, processor, *node, wallet, config.account);
splash->close ();
gui->start ();
gui->client_window->show ();
}));
@ -290,7 +288,7 @@ int main (int argc, char * const * argv)
{
try
{
QApplication application (argc, const_cast <char **> (argv));
QApplication application (argc, const_cast<char **> (argv));
boost::program_options::options_description description ("Command line options");
description.add_options () ("help", "Print out options");
rai::add_node_options (description);
@ -312,7 +310,7 @@ int main (int argc, char * const * argv)
boost::filesystem::path data_path;
if (vm.count ("data_path"))
{
auto name (vm ["data_path"].as <std::string> ());
auto name (vm["data_path"].as<std::string> ());
data_path = boost::filesystem::path (name);
}
else

317
rai/secure.cpp Executable file → Normal file
View file

@ -49,39 +49,39 @@ char const * live_genesis_data = R"%%%({
class ledger_constants
{
public:
ledger_constants () :
zero_key ("0"),
test_genesis_key (test_private_key_data),
rai_test_account (test_public_key_data),
rai_beta_account (beta_public_key_data),
rai_live_account (live_public_key_data),
rai_test_genesis (test_genesis_data),
rai_beta_genesis (beta_genesis_data),
rai_live_genesis (live_genesis_data),
genesis_account (rai::rai_network == rai::rai_networks::rai_test_network ? rai_test_account : rai::rai_network == rai::rai_networks::rai_beta_network ? rai_beta_account : rai_live_account),
genesis_block (rai::rai_network == rai::rai_networks::rai_test_network ? rai_test_genesis : rai::rai_network == rai::rai_networks::rai_beta_network ? rai_beta_genesis : rai_live_genesis),
genesis_amount (std::numeric_limits <rai::uint128_t>::max ()),
burn_account (0)
{
CryptoPP::AutoSeededRandomPool random_pool;
// Randomly generating these mean no two nodes will ever have the same sentinal values which protects against some insecure algorithms
random_pool.GenerateBlock (not_a_block.bytes.data (), not_a_block.bytes.size ());
random_pool.GenerateBlock (not_an_account.bytes.data (), not_an_account.bytes.size ());
}
rai::keypair zero_key;
rai::keypair test_genesis_key;
rai::account rai_test_account;
rai::account rai_beta_account;
rai::account rai_live_account;
std::string rai_test_genesis;
std::string rai_beta_genesis;
std::string rai_live_genesis;
rai::account genesis_account;
std::string genesis_block;
rai::uint128_t genesis_amount;
rai::block_hash not_a_block;
rai::account not_an_account;
rai::account burn_account;
ledger_constants () :
zero_key ("0"),
test_genesis_key (test_private_key_data),
rai_test_account (test_public_key_data),
rai_beta_account (beta_public_key_data),
rai_live_account (live_public_key_data),
rai_test_genesis (test_genesis_data),
rai_beta_genesis (beta_genesis_data),
rai_live_genesis (live_genesis_data),
genesis_account (rai::rai_network == rai::rai_networks::rai_test_network ? rai_test_account : rai::rai_network == rai::rai_networks::rai_beta_network ? rai_beta_account : rai_live_account),
genesis_block (rai::rai_network == rai::rai_networks::rai_test_network ? rai_test_genesis : rai::rai_network == rai::rai_networks::rai_beta_network ? rai_beta_genesis : rai_live_genesis),
genesis_amount (std::numeric_limits<rai::uint128_t>::max ()),
burn_account (0)
{
CryptoPP::AutoSeededRandomPool random_pool;
// Randomly generating these mean no two nodes will ever have the same sentinal values which protects against some insecure algorithms
random_pool.GenerateBlock (not_a_block.bytes.data (), not_a_block.bytes.size ());
random_pool.GenerateBlock (not_an_account.bytes.data (), not_an_account.bytes.size ());
}
rai::keypair zero_key;
rai::keypair test_genesis_key;
rai::account rai_test_account;
rai::account rai_beta_account;
rai::account rai_live_account;
std::string rai_test_genesis;
std::string rai_beta_genesis;
std::string rai_live_genesis;
rai::account genesis_account;
std::string genesis_block;
rai::uint128_t genesis_amount;
rai::block_hash not_a_block;
rai::account not_an_account;
rai::account burn_account;
};
ledger_constants globals;
}
@ -125,31 +125,31 @@ boost::filesystem::path rai::working_path ()
return result;
}
size_t rai::shared_ptr_block_hash::operator () (std::shared_ptr <rai::block> const & block_a) const
size_t rai::shared_ptr_block_hash::operator() (std::shared_ptr<rai::block> const & block_a) const
{
auto hash (block_a->hash ());
auto result (static_cast <size_t> (hash.qwords [0]));
auto result (static_cast<size_t> (hash.qwords[0]));
return result;
}
bool rai::shared_ptr_block_hash::operator () (std::shared_ptr <rai::block> const & lhs, std::shared_ptr <rai::block> const & rhs) const
bool rai::shared_ptr_block_hash::operator() (std::shared_ptr<rai::block> const & lhs, std::shared_ptr<rai::block> const & rhs) const
{
return *lhs == *rhs;
}
// Sum the weights for each vote and return the winning block with its vote tally
std::pair <rai::uint128_t, std::shared_ptr <rai::block>> rai::ledger::winner (MDB_txn * transaction_a, rai::votes const & votes_a)
std::pair<rai::uint128_t, std::shared_ptr<rai::block>> rai::ledger::winner (MDB_txn * transaction_a, rai::votes const & votes_a)
{
auto tally_l (tally (transaction_a, votes_a));
auto existing (tally_l.begin ());
return std::make_pair (existing->first, existing->second);
}
std::map <rai::uint128_t, std::shared_ptr <rai::block>, std::greater <rai::uint128_t>> rai::ledger::tally (MDB_txn * transaction_a, rai::votes const & votes_a)
std::map<rai::uint128_t, std::shared_ptr<rai::block>, std::greater<rai::uint128_t>> rai::ledger::tally (MDB_txn * transaction_a, rai::votes const & votes_a)
{
std::unordered_map <std::shared_ptr <block>, rai::uint128_t, rai::shared_ptr_block_hash, rai::shared_ptr_block_hash> totals;
std::unordered_map<std::shared_ptr<block>, rai::uint128_t, rai::shared_ptr_block_hash, rai::shared_ptr_block_hash> totals;
// Construct a map of blocks -> vote total.
for (auto & i: votes_a.rep_votes)
for (auto & i : votes_a.rep_votes)
{
auto existing (totals.find (i.second));
if (existing == totals.end ())
@ -162,21 +162,21 @@ std::map <rai::uint128_t, std::shared_ptr <rai::block>, std::greater <rai::uint1
existing->second += weight_l;
}
// Construction a map of vote total -> block in decreasing order.
std::map <rai::uint128_t, std::shared_ptr <rai::block>, std::greater <rai::uint128_t>> result;
for (auto & i: totals)
std::map<rai::uint128_t, std::shared_ptr<rai::block>, std::greater<rai::uint128_t>> result;
for (auto & i : totals)
{
result [i.second] = i.first;
result[i.second] = i.first;
}
return result;
}
rai::votes::votes (std::shared_ptr <rai::block> block_a) :
rai::votes::votes (std::shared_ptr<rai::block> block_a) :
id (block_a->root ())
{
rep_votes.insert (std::make_pair (rai::not_an_account, block_a));
}
rai::tally_result rai::votes::vote (std::shared_ptr <rai::vote> vote_a)
rai::tally_result rai::votes::vote (std::shared_ptr<rai::vote> vote_a)
{
rai::tally_result result;
auto existing (rep_votes.find (vote_a->account));
@ -231,9 +231,9 @@ void rai::serialize_block (rai::stream & stream_a, rai::block const & block_a)
block_a.serialize (stream_a);
}
std::unique_ptr <rai::block> rai::deserialize_block (MDB_val const & val_a)
std::unique_ptr<rai::block> rai::deserialize_block (MDB_val const & val_a)
{
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (val_a.mv_data), val_a.mv_size);
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (val_a.mv_data), val_a.mv_size);
return deserialize_block (stream);
}
@ -251,7 +251,7 @@ rai::account_info::account_info (MDB_val const & val_a)
{
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (head) + sizeof (rep_block) + sizeof (open_block) + sizeof (balance) + sizeof (modified) + sizeof (block_count) == sizeof (*this), "Class not packed");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
rai::account_info::account_info (rai::block_hash const & head_a, rai::block_hash const & rep_block_a, rai::block_hash const & open_block_a, rai::amount const & balance_a, uint64_t modified_a, uint64_t block_count_a) :
@ -300,19 +300,19 @@ bool rai::account_info::deserialize (rai::stream & stream_a)
return result;
}
bool rai::account_info::operator == (rai::account_info const & other_a) const
bool rai::account_info::operator== (rai::account_info const & other_a) const
{
return head == other_a.head && rep_block == other_a.rep_block && open_block == other_a.open_block && balance == other_a.balance && modified == other_a.modified && block_count == other_a.block_count;
}
bool rai::account_info::operator != (rai::account_info const & other_a) const
bool rai::account_info::operator!= (rai::account_info const & other_a) const
{
return ! (*this == other_a);
return !(*this == other_a);
}
rai::mdb_val rai::account_info::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::account_info *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::account_info *> (this));
}
rai::store_entry::store_entry () :
@ -323,16 +323,16 @@ second (0, nullptr)
void rai::store_entry::clear ()
{
first = {0, nullptr};
second = {0, nullptr};
first = { 0, nullptr };
second = { 0, nullptr };
}
rai::store_entry * rai::store_entry::operator -> ()
rai::store_entry * rai::store_entry::operator-> ()
{
return this;
}
rai::store_entry & rai::store_iterator::operator -> ()
rai::store_entry & rai::store_iterator::operator-> ()
{
return current;
}
@ -394,7 +394,7 @@ rai::store_iterator::~store_iterator ()
}
}
rai::store_iterator & rai::store_iterator::operator ++ ()
rai::store_iterator & rai::store_iterator::operator++ ()
{
assert (cursor != nullptr);
auto status (mdb_cursor_get (cursor, &current.first.value, &current.second.value, MDB_NEXT));
@ -415,7 +415,7 @@ void rai::store_iterator::next_dup ()
}
}
rai::store_iterator & rai::store_iterator::operator = (rai::store_iterator && other_a)
rai::store_iterator & rai::store_iterator::operator= (rai::store_iterator && other_a)
{
if (cursor != nullptr)
{
@ -428,7 +428,7 @@ rai::store_iterator & rai::store_iterator::operator = (rai::store_iterator && ot
return *this;
}
bool rai::store_iterator::operator == (rai::store_iterator const & other_a) const
bool rai::store_iterator::operator== (rai::store_iterator const & other_a) const
{
auto result (current.first.data () == other_a.current.first.data ());
assert (!result || (current.first.size () == other_a.current.first.size ()));
@ -437,7 +437,7 @@ bool rai::store_iterator::operator == (rai::store_iterator const & other_a) cons
return result;
}
bool rai::store_iterator::operator != (rai::store_iterator const & other_a) const
bool rai::store_iterator::operator!= (rai::store_iterator const & other_a) const
{
return !(*this == other_a);
}
@ -516,8 +516,8 @@ int rai::block_store::version_get (MDB_txn * transaction_a)
else
{
rai::uint256_union version_value (data.uint256 ());
assert (version_value.qwords [2] == 0 && version_value.qwords [1] == 0 && version_value.qwords [0] == 0);
result = version_value.number ().convert_to <int> ();
assert (version_value.qwords[2] == 0 && version_value.qwords[1] == 0 && version_value.qwords[0] == 0);
result = version_value.number ().convert_to<int> ();
}
return result;
}
@ -547,7 +547,7 @@ void rai::block_store::do_upgrades (MDB_txn * transaction_a)
case 10:
break;
default:
assert (false);
assert (false);
}
}
@ -640,14 +640,14 @@ void rai::block_store::upgrade_v2_to_v3 (MDB_txn * transaction_a)
assert (!visitor.result.is_zero ());
info.rep_block = visitor.result;
mdb_cursor_put (i.cursor, rai::mdb_val (account_l), info.val (), MDB_CURRENT);
representation_add (transaction_a, visitor.result, info.balance.number());
representation_add (transaction_a, visitor.result, info.balance.number ());
}
}
void rai::block_store::upgrade_v3_to_v4 (MDB_txn * transaction_a)
{
version_put (transaction_a, 4);
std::queue <std::pair <rai::pending_key, rai::pending_info>> items;
std::queue<std::pair<rai::pending_key, rai::pending_info>> items;
for (auto i (pending_begin (transaction_a)), n (pending_end ()); i != n; ++i)
{
rai::block_hash hash (i->first.uint256 ());
@ -691,7 +691,7 @@ void rai::block_store::upgrade_v4_to_v5 (MDB_txn * transaction_a)
void rai::block_store::upgrade_v5_to_v6 (MDB_txn * transaction_a)
{
version_put (transaction_a, 6);
std::deque <std::pair <rai::account, rai::account_info>> headers;
std::deque<std::pair<rai::account, rai::account_info>> headers;
for (auto i (latest_begin (transaction_a)), n (latest_end ()); i != n; ++i)
{
rai::account account (i->first.uint256 ());
@ -733,16 +733,16 @@ void rai::block_store::upgrade_v8_to_v9 (MDB_txn * transaction_a)
MDB_dbi sequence;
mdb_dbi_open (transaction_a, "sequence", MDB_CREATE | MDB_DUPSORT, &sequence);
rai::genesis genesis;
std::shared_ptr <rai::block> block (std::move (genesis.open));
std::shared_ptr<rai::block> block (std::move (genesis.open));
rai::keypair junk;
for (rai::store_iterator i (transaction_a, sequence), n (nullptr); i != n; ++i)
{
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (i->second.data ()), i->second.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (i->second.data ()), i->second.size ());
uint64_t sequence;
auto error (rai::read (stream, sequence));
// Create a dummy vote with the same sequence number for easy upgrading. This won't have a valid signature.
auto dummy (std::make_shared <rai::vote> (rai::account (i->first.uint256 ()), junk.prv, sequence, block));
std::vector <uint8_t> vector;
auto dummy (std::make_shared<rai::vote> (rai::account (i->first.uint256 ()), junk.prv, sequence, block));
std::vector<uint8_t> vector;
{
rai::vectorstream stream (vector);
dummy->serialize (stream);
@ -809,9 +809,9 @@ public:
rai::block_type type;
auto value (store.block_get_raw (transaction, block_a.previous (), type));
assert (value.mv_size != 0);
std::vector <uint8_t> data (static_cast <uint8_t *> (value.mv_data), static_cast <uint8_t *> (value.mv_data) + value.mv_size);
std::vector<uint8_t> data (static_cast<uint8_t *> (value.mv_data), static_cast<uint8_t *> (value.mv_data) + value.mv_size);
std::copy (hash.bytes.begin (), hash.bytes.end (), data.end () - hash.bytes.size ());
store.block_put_raw (transaction, store.block_database (type), block_a.previous (), rai::mdb_val (data.size (), data.data()));
store.block_put_raw (transaction, store.block_database (type), block_a.previous (), rai::mdb_val (data.size (), data.data ()));
}
void send_block (rai::send_block const & block_a) override
{
@ -852,7 +852,7 @@ MDB_dbi rai::block_store::block_database (rai::block_type type_a)
result = change_blocks;
break;
default:
assert(false);
assert (false);
break;
}
return result;
@ -867,13 +867,13 @@ void rai::block_store::block_put_raw (MDB_txn * transaction_a, MDB_dbi database_
void rai::block_store::block_put (MDB_txn * transaction_a, rai::block_hash const & hash_a, rai::block const & block_a, rai::block_hash const & successor_a)
{
assert (successor_a.is_zero () || block_exists (transaction_a, successor_a));
std::vector <uint8_t> vector;
std::vector<uint8_t> vector;
{
rai::vectorstream stream (vector);
block_a.serialize (stream);
rai::write (stream, successor_a.bytes);
}
block_put_raw (transaction_a, block_database (block_a.type ()), hash_a, {vector.size (), vector.data ()});
block_put_raw (transaction_a, block_database (block_a.type ()), hash_a, { vector.size (), vector.data () });
set_predecessor predecessor (transaction_a, *this);
block_a.visit (predecessor);
assert (block_a.previous ().is_zero () || block_successor (transaction_a, block_a.previous ()) == hash_a);
@ -918,7 +918,7 @@ MDB_val rai::block_store::block_get_raw (MDB_txn * transaction_a, rai::block_has
return result;
}
std::unique_ptr <rai::block> rai::block_store::block_random (MDB_txn * transaction_a, MDB_dbi database)
std::unique_ptr<rai::block> rai::block_store::block_random (MDB_txn * transaction_a, MDB_dbi database)
{
rai::block_hash hash;
rai::random_pool.GenerateBlock (hash.bytes.data (), hash.bytes.size ());
@ -931,11 +931,11 @@ std::unique_ptr <rai::block> rai::block_store::block_random (MDB_txn * transacti
return block_get (transaction_a, rai::block_hash (existing->first.uint256 ()));
}
std::unique_ptr <rai::block> rai::block_store::block_random (MDB_txn * transaction_a)
std::unique_ptr<rai::block> rai::block_store::block_random (MDB_txn * transaction_a)
{
auto count (block_count (transaction_a));
auto region (rai::random_pool.GenerateWord32 (0, count.sum () - 1));
std::unique_ptr <rai::block> result;
std::unique_ptr<rai::block> result;
if (region < count.send)
{
result = block_random (transaction_a, send_blocks);
@ -972,7 +972,7 @@ rai::block_hash rai::block_store::block_successor (MDB_txn * transaction_a, rai:
if (value.mv_size != 0)
{
assert (value.mv_size >= result.bytes.size ());
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.mv_data) + value.mv_size - result.bytes.size (), result.bytes.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.mv_data) + value.mv_size - result.bytes.size (), result.bytes.size ());
auto error (rai::read (stream, result.bytes));
assert (!error);
}
@ -989,14 +989,14 @@ void rai::block_store::block_successor_clear (MDB_txn * transaction_a, rai::bloc
block_put (transaction_a, hash_a, *block);
}
std::unique_ptr <rai::block> rai::block_store::block_get (MDB_txn * transaction_a, rai::block_hash const & hash_a)
std::unique_ptr<rai::block> rai::block_store::block_get (MDB_txn * transaction_a, rai::block_hash const & hash_a)
{
rai::block_type type;
auto value (block_get_raw (transaction_a, hash_a, type));
std::unique_ptr <rai::block> result;
std::unique_ptr<rai::block> result;
if (value.mv_size != 0)
{
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.mv_data), value.mv_size);
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.mv_data), value.mv_size);
result = rai::deserialize_block (stream, type);
assert (result != nullptr);
}
@ -1098,13 +1098,13 @@ bool rai::block_store::account_get (MDB_txn * transaction_a, rai::account const
}
else
{
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.data ()), value.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.data ()), value.size ());
result = info_a.deserialize (stream);
assert (!result);
}
return result;
}
void rai::block_store::frontier_put (MDB_txn * transaction_a, rai::block_hash const & block_a, rai::account const & account_a)
{
auto status (mdb_put (transaction_a, frontiers, rai::mdb_val (block_a), rai::mdb_val (account_a), 0));
@ -1177,7 +1177,7 @@ bool rai::block_store::pending_get (MDB_txn * transaction_a, rai::pending_key co
{
result = false;
assert (value.size () == sizeof (pending_a.source.bytes) + sizeof (pending_a.amount.bytes));
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.data ()), value.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.data ()), value.size ());
auto error1 (rai::read (stream, pending_a.source));
assert (!error1);
auto error2 (rai::read (stream, pending_a.amount));
@ -1212,9 +1212,9 @@ amount (0)
rai::pending_info::pending_info (MDB_val const & val_a)
{
assert(val_a.mv_size == sizeof (*this));
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (source) + sizeof (amount) == sizeof (*this), "Packed class");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
rai::pending_info::pending_info (rai::account const & source_a, rai::amount const & amount_a) :
@ -1239,14 +1239,14 @@ bool rai::pending_info::deserialize (rai::stream & stream_a)
return result;
}
bool rai::pending_info::operator == (rai::pending_info const & other_a) const
bool rai::pending_info::operator== (rai::pending_info const & other_a) const
{
return source == other_a.source && amount == other_a.amount;
}
rai::mdb_val rai::pending_info::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::pending_info *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::pending_info *> (this));
}
rai::pending_key::pending_key (rai::account const & account_a, rai::block_hash const & hash_a) :
@ -1257,9 +1257,9 @@ hash (hash_a)
rai::pending_key::pending_key (MDB_val const & val_a)
{
assert(val_a.mv_size == sizeof (*this));
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (account) + sizeof (hash) == sizeof (*this), "Packed class");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
void rai::pending_key::serialize (rai::stream & stream_a) const
@ -1278,14 +1278,14 @@ bool rai::pending_key::deserialize (rai::stream & stream_a)
return result;
}
bool rai::pending_key::operator == (rai::pending_key const & other_a) const
bool rai::pending_key::operator== (rai::pending_key const & other_a) const
{
return account == other_a.account && hash == other_a.hash;
}
rai::mdb_val rai::pending_key::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::pending_key *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::pending_key *> (this));
}
void rai::block_store::block_info_put (MDB_txn * transaction_a, rai::block_hash const & hash_a, rai::block_info const & block_info_a)
@ -1320,7 +1320,7 @@ bool rai::block_store::block_info_get (MDB_txn * transaction_a, rai::block_hash
{
result = false;
assert (value.size () == sizeof (block_info_a.account.bytes) + sizeof (block_info_a.balance.bytes));
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.data ()), value.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.data ()), value.size ());
auto error1 (rai::read (stream, block_info_a.account));
assert (!error1);
auto error2 (rai::read (stream, block_info_a.balance));
@ -1355,9 +1355,9 @@ balance (0)
rai::block_info::block_info (MDB_val const & val_a)
{
assert(val_a.mv_size == sizeof (*this));
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (account) + sizeof (balance) == sizeof (*this), "Packed class");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
rai::block_info::block_info (rai::account const & account_a, rai::amount const & balance_a) :
@ -1382,14 +1382,14 @@ bool rai::block_info::deserialize (rai::stream & stream_a)
return result;
}
bool rai::block_info::operator == (rai::block_info const & other_a) const
bool rai::block_info::operator== (rai::block_info const & other_a) const
{
return account == other_a.account && balance == other_a.balance;
}
rai::mdb_val rai::block_info::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::block_info *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::block_info *> (this));
}
rai::uint128_t rai::block_store::representation_get (MDB_txn * transaction_a, rai::account const & account_a)
@ -1401,7 +1401,7 @@ rai::uint128_t rai::block_store::representation_get (MDB_txn * transaction_a, ra
if (status == 0)
{
rai::uint128_union rep;
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.data ()), value.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.data ()), value.size ());
auto error (rai::read (stream, rep));
assert (!error);
result = rep.number ();
@ -1428,7 +1428,7 @@ rai::store_iterator rai::block_store::representation_begin (MDB_txn * transactio
rai::store_iterator rai::block_store::representation_end ()
{
rai::store_iterator result(nullptr);
rai::store_iterator result (nullptr);
return result;
}
@ -1438,17 +1438,17 @@ void rai::block_store::unchecked_clear (MDB_txn * transaction_a)
assert (status == 0);
}
void rai::block_store::unchecked_put (MDB_txn * transaction_a, rai::block_hash const & hash_a, std::shared_ptr <rai::block> const & block_a)
void rai::block_store::unchecked_put (MDB_txn * transaction_a, rai::block_hash const & hash_a, std::shared_ptr<rai::block> const & block_a)
{
std::lock_guard <std::mutex> lock (cache_mutex);
std::lock_guard<std::mutex> lock (cache_mutex);
unchecked_cache.insert (std::make_pair (hash_a, block_a));
}
std::vector <std::shared_ptr <rai::block>> rai::block_store::unchecked_get (MDB_txn * transaction_a, rai::block_hash const & hash_a)
std::vector<std::shared_ptr<rai::block>> rai::block_store::unchecked_get (MDB_txn * transaction_a, rai::block_hash const & hash_a)
{
std::vector <std::shared_ptr <rai::block>> result;
std::vector<std::shared_ptr<rai::block>> result;
{
std::lock_guard <std::mutex> lock (cache_mutex);
std::lock_guard<std::mutex> lock (cache_mutex);
for (auto i (unchecked_cache.find (hash_a)), n (unchecked_cache.end ()); i != n && i->first == hash_a; ++i)
{
result.push_back (i->second);
@ -1456,7 +1456,7 @@ std::vector <std::shared_ptr <rai::block>> rai::block_store::unchecked_get (MDB_
}
for (auto i (unchecked_begin (transaction_a, hash_a)), n (unchecked_end ()); i != n && rai::block_hash (i->first.uint256 ()) == hash_a; i.next_dup ())
{
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (i->second.data ()), i->second.size());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (i->second.data ()), i->second.size ());
result.push_back (rai::deserialize_block (stream));
}
return result;
@ -1465,7 +1465,7 @@ std::vector <std::shared_ptr <rai::block>> rai::block_store::unchecked_get (MDB_
void rai::block_store::unchecked_del (MDB_txn * transaction_a, rai::block_hash const & hash_a, rai::block const & block_a)
{
{
std::lock_guard <std::mutex> lock (cache_mutex);
std::lock_guard<std::mutex> lock (cache_mutex);
for (auto i (unchecked_cache.find (hash_a)), n (unchecked_cache.end ()); i != n && i->first == hash_a;)
{
if (*i->second == block_a)
@ -1478,7 +1478,7 @@ void rai::block_store::unchecked_del (MDB_txn * transaction_a, rai::block_hash c
}
}
}
std::vector <uint8_t> vector;
std::vector<uint8_t> vector;
{
rai::vectorstream stream (vector);
rai::serialize_block (stream, block_a);
@ -1566,7 +1566,7 @@ bool rai::block_store::checksum_get (MDB_txn * transaction_a, uint64_t prefix, u
if (status == 0)
{
result = false;
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value.data ()), value.size ());
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value.data ()), value.size ());
auto error (rai::read (stream, hash_a));
assert (!error);
}
@ -1587,16 +1587,16 @@ void rai::block_store::checksum_del (MDB_txn * transaction_a, uint64_t prefix, u
void rai::block_store::flush (MDB_txn * transaction_a)
{
std::unordered_map <rai::account, std::shared_ptr <rai::vote>> sequence_cache_l;
std::unordered_multimap <rai::block_hash, std::shared_ptr <rai::block>> unchecked_cache_l;
std::unordered_map<rai::account, std::shared_ptr<rai::vote>> sequence_cache_l;
std::unordered_multimap<rai::block_hash, std::shared_ptr<rai::block>> unchecked_cache_l;
{
std::lock_guard <std::mutex> lock (cache_mutex);
std::lock_guard<std::mutex> lock (cache_mutex);
sequence_cache_l.swap (vote_cache);
unchecked_cache_l.swap (unchecked_cache);
}
for (auto &i: unchecked_cache_l)
for (auto & i : unchecked_cache_l)
{
std::vector <uint8_t> vector;
std::vector<uint8_t> vector;
{
rai::vectorstream stream (vector);
rai::serialize_block (stream, *i.second);
@ -1606,7 +1606,7 @@ void rai::block_store::flush (MDB_txn * transaction_a)
}
for (auto i (sequence_cache_l.begin ()), n (sequence_cache_l.end ()); i != n; ++i)
{
std::vector <uint8_t> vector;
std::vector<uint8_t> vector;
{
rai::vectorstream stream (vector);
i->second->serialize (stream);
@ -1626,24 +1626,24 @@ rai::store_iterator rai::block_store::vote_end ()
return rai::store_iterator (nullptr);
}
std::shared_ptr <rai::vote> rai::block_store::vote_get (MDB_txn * transaction_a, rai::account const & account_a)
std::shared_ptr<rai::vote> rai::block_store::vote_get (MDB_txn * transaction_a, rai::account const & account_a)
{
std::shared_ptr <rai::vote> result;
std::shared_ptr<rai::vote> result;
rai::mdb_val value;
auto status (mdb_get (transaction_a, vote, rai::mdb_val (account_a), value));
assert (status == 0 || status == MDB_NOTFOUND);
if (status == 0)
{
result = std::make_shared <rai::vote> (value);
result = std::make_shared<rai::vote> (value);
assert (result != nullptr);
}
return result;
}
std::shared_ptr <rai::vote> rai::block_store::vote_current (MDB_txn * transaction_a, rai::account const & account_a)
std::shared_ptr<rai::vote> rai::block_store::vote_current (MDB_txn * transaction_a, rai::account const & account_a)
{
assert (!cache_mutex.try_lock ());
std::shared_ptr <rai::vote> result;
std::shared_ptr<rai::vote> result;
auto existing (vote_cache.find (account_a));
if (existing != vote_cache.end ())
{
@ -1655,20 +1655,20 @@ std::shared_ptr <rai::vote> rai::block_store::vote_current (MDB_txn * transactio
}
return result;
}
std::shared_ptr <rai::vote> rai::block_store::vote_generate (MDB_txn * transaction_a, rai::account const & account_a, rai::raw_key const & key_a, std::shared_ptr <rai::block> block_a)
std::shared_ptr<rai::vote> rai::block_store::vote_generate (MDB_txn * transaction_a, rai::account const & account_a, rai::raw_key const & key_a, std::shared_ptr<rai::block> block_a)
{
std::lock_guard <std::mutex> lock (cache_mutex);
std::lock_guard<std::mutex> lock (cache_mutex);
auto result (vote_current (transaction_a, account_a));
uint64_t sequence ((result ? result->sequence : 0) + 1);
result = std::make_shared <rai::vote> (account_a, key_a, sequence, block_a);
vote_cache [account_a] = result;
result = std::make_shared<rai::vote> (account_a, key_a, sequence, block_a);
vote_cache[account_a] = result;
return result;
}
std::shared_ptr <rai::vote> rai::block_store::vote_max (MDB_txn * transaction_a, std::shared_ptr <rai::vote> vote_a)
std::shared_ptr<rai::vote> rai::block_store::vote_max (MDB_txn * transaction_a, std::shared_ptr<rai::vote> vote_a)
{
std::lock_guard <std::mutex> lock (cache_mutex);
std::lock_guard<std::mutex> lock (cache_mutex);
auto current (vote_current (transaction_a, vote_a->account));
auto result (vote_a);
if (current != nullptr)
@ -1678,18 +1678,18 @@ std::shared_ptr <rai::vote> rai::block_store::vote_max (MDB_txn * transaction_a,
result = current;
}
}
vote_cache [vote_a->account] = result;
vote_cache[vote_a->account] = result;
return result;
}
rai::vote_result rai::block_store::vote_validate (MDB_txn * transaction_a, std::shared_ptr <rai::vote> vote_a)
rai::vote_result rai::block_store::vote_validate (MDB_txn * transaction_a, std::shared_ptr<rai::vote> vote_a)
{
rai::vote_result result ({rai::vote_code::invalid, 0});
rai::vote_result result ({ rai::vote_code::invalid, 0 });
// Reject unsigned votes
if (!rai::validate_message (vote_a->account, vote_a->hash (), vote_a->signature))
{
result.code = rai::vote_code::replay;
result.vote = vote_max (transaction_a, vote_a); // Make sure this sequence number is > any we've seen from this account before
result.vote = vote_max (transaction_a, vote_a); // Make sure this sequence number is > any we've seen from this account before
if (result.vote == vote_a)
{
result.code = rai::vote_code::vote;
@ -1698,14 +1698,14 @@ rai::vote_result rai::block_store::vote_validate (MDB_txn * transaction_a, std::
return result;
}
bool rai::vote::operator == (rai::vote const & other_a) const
bool rai::vote::operator== (rai::vote const & other_a) const
{
return sequence == other_a.sequence && *block == *other_a.block && account == other_a.account && signature == other_a.signature;
}
bool rai::vote::operator != (rai::vote const & other_a) const
bool rai::vote::operator!= (rai::vote const & other_a) const
{
return ! (*this == other_a);
return !(*this == other_a);
}
std::string rai::vote::to_json () const
@ -1745,7 +1745,7 @@ public:
auto source (store.block_get (transaction, hash));
if (source != nullptr)
{
auto send (dynamic_cast <rai::send_block *> (source.get ()));
auto send (dynamic_cast<rai::send_block *> (source.get ()));
if (send != nullptr)
{
result = send->hashables.destination;
@ -1900,7 +1900,8 @@ void balance_visitor::receive_block (rai::receive_block const & block_a)
result += block_info.balance.number ();
current = 0;
}
else {
else
{
result += source.result;
current = block_a.hashables.previous;
}
@ -1922,7 +1923,8 @@ void balance_visitor::change_block (rai::change_block const & block_a)
result += block_info.balance.number ();
current = 0;
}
else {
else
{
current = block_a.hashables.previous;
}
}
@ -1972,7 +1974,7 @@ public:
ledger.store.representation_add (transaction, ledger.representative (transaction, hash), 0 - amount);
ledger.change_latest (transaction, destination_account, block_a.hashables.previous, representative, ledger.balance (transaction, block_a.hashables.previous), info.block_count - 1);
ledger.store.block_del (transaction, hash);
ledger.store.pending_put (transaction, rai::pending_key (destination_account, block_a.hashables.source), {ledger.account (transaction, block_a.hashables.source), amount});
ledger.store.pending_put (transaction, rai::pending_key (destination_account, block_a.hashables.source), { ledger.account (transaction, block_a.hashables.source), amount });
ledger.store.frontier_del (transaction, hash);
ledger.store.frontier_put (transaction, block_a.hashables.previous, destination_account);
ledger.store.block_successor_clear (transaction, block_a.hashables.previous);
@ -1989,7 +1991,7 @@ public:
ledger.store.representation_add (transaction, ledger.representative (transaction, hash), 0 - amount);
ledger.change_latest (transaction, destination_account, 0, 0, 0, 0);
ledger.store.block_del (transaction, hash);
ledger.store.pending_put (transaction, rai::pending_key (destination_account, block_a.hashables.source), {ledger.account (transaction, block_a.hashables.source), amount});
ledger.store.pending_put (transaction, rai::pending_key (destination_account, block_a.hashables.source), { ledger.account (transaction, block_a.hashables.source), amount });
ledger.store.frontier_del (transaction, hash);
}
void change_block (rai::change_block const & block_a) override
@ -2029,7 +2031,7 @@ void amount_visitor::compute (rai::block_hash const & block_hash)
{
if (block_hash == rai::genesis_account)
{
result = std::numeric_limits <rai::uint128_t>::max ();
result = std::numeric_limits<rai::uint128_t>::max ();
}
else
{
@ -2278,10 +2280,10 @@ void rai::ledger::change_latest (MDB_txn * transaction_a, rai::account const & a
}
else
{
assert (dynamic_cast <rai::open_block *> (store.block_get (transaction_a, hash_a).get ()) != nullptr);
assert (dynamic_cast<rai::open_block *> (store.block_get (transaction_a, hash_a).get ()) != nullptr);
info.open_block = hash_a;
}
if (!hash_a.is_zero())
if (!hash_a.is_zero ())
{
info.head = hash_a;
info.rep_block = rep_block_a;
@ -2304,7 +2306,7 @@ void rai::ledger::change_latest (MDB_txn * transaction_a, rai::account const & a
}
}
std::unique_ptr <rai::block> rai::ledger::successor (MDB_txn * transaction_a, rai::block_hash const & block_a)
std::unique_ptr<rai::block> rai::ledger::successor (MDB_txn * transaction_a, rai::block_hash const & block_a)
{
assert (store.account_exists (transaction_a, block_a) || store.block_exists (transaction_a, block_a));
assert (store.account_exists (transaction_a, block_a) || latest (transaction_a, account (transaction_a, block_a)) != block_a);
@ -2326,12 +2328,12 @@ std::unique_ptr <rai::block> rai::ledger::successor (MDB_txn * transaction_a, ra
return result;
}
std::unique_ptr <rai::block> rai::ledger::forked_block (MDB_txn * transaction_a, rai::block const & block_a)
std::unique_ptr<rai::block> rai::ledger::forked_block (MDB_txn * transaction_a, rai::block const & block_a)
{
assert (!store.block_exists (transaction_a, block_a.hash ()));
auto root (block_a.root ());
assert (store.block_exists (transaction_a, root) || store.account_exists (transaction_a, root));
std::unique_ptr <rai::block> result (store.block_get (transaction_a, store.block_successor (transaction_a, root)));
std::unique_ptr<rai::block> result (store.block_get (transaction_a, store.block_successor (transaction_a, root)));
if (result == nullptr)
{
rai::account_info info;
@ -2351,7 +2353,7 @@ void ledger_processor::change_block (rai::change_block const & block_a)
if (result.code == rai::process_result::progress)
{
auto previous (ledger.store.block_exists (transaction, block_a.hashables.previous));
result.code = previous ? rai::process_result::progress : rai::process_result::gap_previous; // Have we seen the previous block already? (Harmless)
result.code = previous ? rai::process_result::progress : rai::process_result::gap_previous; // Have we seen the previous block already? (Harmless)
if (result.code == rai::process_result::progress)
{
auto account (ledger.store.frontier_get (transaction, block_a.hashables.previous));
@ -2409,7 +2411,7 @@ void ledger_processor::send_block (rai::send_block const & block_a)
ledger.store.representation_add (transaction, info.rep_block, 0 - amount);
ledger.store.block_put (transaction, hash, block_a);
ledger.change_latest (transaction, account, hash, info.rep_block, block_a.hashables.balance, info.block_count + 1);
ledger.store.pending_put (transaction, rai::pending_key (block_a.hashables.destination, hash), {account, amount});
ledger.store.pending_put (transaction, rai::pending_key (block_a.hashables.destination, hash), { account, amount });
ledger.store.frontier_del (transaction, block_a.hashables.previous);
ledger.store.frontier_put (transaction, hash, account);
result.account = account;
@ -2428,11 +2430,11 @@ void ledger_processor::receive_block (rai::receive_block const & block_a)
result.code = existing ? rai::process_result::old : rai::process_result::progress; // Have we seen this block already? (Harmless)
if (result.code == rai::process_result::progress)
{
result.code = ledger.store.block_exists (transaction, block_a.hashables.source) ? rai::process_result::progress: rai::process_result::gap_source; // Have we seen the source block already? (Harmless)
result.code = ledger.store.block_exists (transaction, block_a.hashables.source) ? rai::process_result::progress : rai::process_result::gap_source; // Have we seen the source block already? (Harmless)
if (result.code == rai::process_result::progress)
{
auto account (ledger.store.frontier_get (transaction, block_a.hashables.previous));
result.code = account.is_zero () ? rai::process_result::gap_previous : rai::process_result::progress; //Have we seen the previous block? No entries for account at all (Harmless)
result.code = account.is_zero () ? rai::process_result::gap_previous : rai::process_result::progress; //Have we seen the previous block? No entries for account at all (Harmless)
if (result.code == rai::process_result::progress)
{
result.code = rai::validate_message (account, hash, block_a.signature) ? rai::process_result::bad_signature : rai::process_result::progress; // Is the signature valid (Malformed)
@ -2572,7 +2574,7 @@ rai::vote::vote (bool & error_a, rai::stream & stream_a, rai::block_type type_a)
}
}
rai::vote::vote (rai::account const & account_a, rai::raw_key const & prv_a, uint64_t sequence_a, std::shared_ptr <rai::block> block_a) :
rai::vote::vote (rai::account const & account_a, rai::raw_key const & prv_a, uint64_t sequence_a, std::shared_ptr<rai::block> block_a) :
sequence (sequence_a),
block (block_a),
account (account_a),
@ -2582,7 +2584,7 @@ signature (rai::sign_message (prv_a, account_a, hash ()))
rai::vote::vote (MDB_val const & value_a)
{
rai::bufferstream stream (reinterpret_cast <uint8_t const *> (value_a.mv_data), value_a.mv_size);
rai::bufferstream stream (reinterpret_cast<uint8_t const *> (value_a.mv_data), value_a.mv_size);
auto error (rai::read (stream, account.bytes));
assert (!error);
error = rai::read (stream, signature.bytes);
@ -2599,9 +2601,10 @@ rai::uint256_union rai::vote::hash () const
blake2b_state hash;
blake2b_init (&hash, sizeof (result.bytes));
blake2b_update (&hash, block->hash ().bytes.data (), sizeof (result.bytes));
union {
union
{
uint64_t qword;
std::array <uint8_t, 8> bytes;
std::array<uint8_t, 8> bytes;
};
qword = sequence;
blake2b_update (&hash, bytes.data (), sizeof (bytes));
@ -2631,8 +2634,8 @@ rai::genesis::genesis ()
std::stringstream istream (rai::genesis_block);
boost::property_tree::read_json (istream, tree);
auto block (rai::deserialize_block_json (tree));
assert (dynamic_cast <rai::open_block *> (block.get ()) != nullptr);
open.reset (static_cast <rai::open_block *> (block.release ()));
assert (dynamic_cast<rai::open_block *> (block.get ()) != nullptr);
open.reset (static_cast<rai::open_block *> (block.release ()));
}
void rai::genesis::initialize (MDB_txn * transaction_a, rai::block_store & store_a) const
@ -2640,8 +2643,8 @@ void rai::genesis::initialize (MDB_txn * transaction_a, rai::block_store & store
auto hash_l (hash ());
assert (store_a.latest_begin (transaction_a) == store_a.latest_end ());
store_a.block_put (transaction_a, hash_l, *open);
store_a.account_put (transaction_a, genesis_account, {hash_l, open->hash (), open->hash (), std::numeric_limits <rai::uint128_t>::max (), store_a.now (), 1});
store_a.representation_put (transaction_a, genesis_account, std::numeric_limits <rai::uint128_t>::max ());
store_a.account_put (transaction_a, genesis_account, { hash_l, open->hash (), open->hash (), std::numeric_limits<rai::uint128_t>::max (), store_a.now (), 1 });
store_a.representation_put (transaction_a, genesis_account, std::numeric_limits<rai::uint128_t>::max ());
store_a.checksum_put (transaction_a, 0, 0, hash_l);
store_a.frontier_put (transaction_a, hash_l, genesis_account);
}

View file

@ -1,4 +1,4 @@
#pragma once
#pragma once
#include <rai/lib/blocks.hpp>
#include <rai/node/utility.hpp>
@ -12,11 +12,11 @@
namespace boost
{
template <>
struct hash <rai::uint256_union>
struct hash<rai::uint256_union>
{
size_t operator () (rai::uint256_union const & value_a) const
size_t operator() (rai::uint256_union const & value_a) const
{
std::hash <rai::uint256_union> hash;
std::hash<rai::uint256_union> hash;
return hash (value_a);
}
};
@ -34,10 +34,10 @@ public:
class shared_ptr_block_hash
{
public:
size_t operator () (std::shared_ptr <rai::block> const &) const;
bool operator () (std::shared_ptr <rai::block> const &, std::shared_ptr <rai::block> const &) const;
size_t operator() (std::shared_ptr<rai::block> const &) const;
bool operator() (std::shared_ptr<rai::block> const &, std::shared_ptr<rai::block> const &) const;
};
std::unique_ptr <rai::block> deserialize_block (MDB_val const &);
std::unique_ptr<rai::block> deserialize_block (MDB_val const &);
// Latest information about an account
class account_info
{
@ -48,8 +48,8 @@ public:
account_info (rai::block_hash const &, rai::block_hash const &, rai::block_hash const &, rai::amount const &, uint64_t, uint64_t);
void serialize (rai::stream &) const;
bool deserialize (rai::stream &);
bool operator == (rai::account_info const &) const;
bool operator != (rai::account_info const &) const;
bool operator== (rai::account_info const &) const;
bool operator!= (rai::account_info const &) const;
rai::mdb_val val () const;
rai::block_hash head;
rai::block_hash rep_block;
@ -63,7 +63,7 @@ class store_entry
public:
store_entry ();
void clear ();
store_entry * operator -> ();
store_entry * operator-> ();
rai::mdb_val first;
rai::mdb_val second;
};
@ -76,13 +76,13 @@ public:
store_iterator (rai::store_iterator &&);
store_iterator (rai::store_iterator const &) = delete;
~store_iterator ();
rai::store_iterator & operator ++ ();
void next_dup();
rai::store_iterator & operator = (rai::store_iterator &&);
rai::store_iterator & operator = (rai::store_iterator const &) = delete;
rai::store_entry & operator -> ();
bool operator == (rai::store_iterator const &) const;
bool operator != (rai::store_iterator const &) const;
rai::store_iterator & operator++ ();
void next_dup ();
rai::store_iterator & operator= (rai::store_iterator &&);
rai::store_iterator & operator= (rai::store_iterator const &) = delete;
rai::store_entry & operator-> ();
bool operator== (rai::store_iterator const &) const;
bool operator!= (rai::store_iterator const &) const;
MDB_cursor * cursor;
rai::store_entry current;
};
@ -95,7 +95,7 @@ public:
pending_info (rai::account const &, rai::amount const &);
void serialize (rai::stream &) const;
bool deserialize (rai::stream &);
bool operator == (rai::pending_info const &) const;
bool operator== (rai::pending_info const &) const;
rai::mdb_val val () const;
rai::account source;
rai::amount amount;
@ -107,7 +107,7 @@ public:
pending_key (MDB_val const &);
void serialize (rai::stream &) const;
bool deserialize (rai::stream &);
bool operator == (rai::pending_key const &) const;
bool operator== (rai::pending_key const &) const;
rai::mdb_val val () const;
rai::account account;
rai::block_hash hash;
@ -120,7 +120,7 @@ public:
block_info (rai::account const &, rai::amount const &);
void serialize (rai::stream &) const;
bool deserialize (rai::stream &);
bool operator == (rai::block_info const &) const;
bool operator== (rai::block_info const &) const;
rai::mdb_val val () const;
rai::account account;
rai::amount balance;
@ -142,17 +142,17 @@ public:
vote (rai::vote const &);
vote (bool &, rai::stream &);
vote (bool &, rai::stream &, rai::block_type);
vote (rai::account const &, rai::raw_key const &, uint64_t, std::shared_ptr <rai::block>);
vote (rai::account const &, rai::raw_key const &, uint64_t, std::shared_ptr<rai::block>);
vote (MDB_val const &);
rai::uint256_union hash () const;
bool operator == (rai::vote const &) const;
bool operator != (rai::vote const &) const;
bool operator== (rai::vote const &) const;
bool operator!= (rai::vote const &) const;
void serialize (rai::stream &, rai::block_type);
void serialize (rai::stream &);
std::string to_json () const;
// Vote round sequence number
uint64_t sequence;
std::shared_ptr <rai::block> block;
std::shared_ptr<rai::block> block;
// Account that's voting
rai::account account;
// Signature of sequence + block hash
@ -168,32 +168,32 @@ class vote_result
{
public:
rai::vote_code code;
std::shared_ptr <rai::vote> vote;
std::shared_ptr<rai::vote> vote;
};
class block_store
{
public:
block_store (bool &, boost::filesystem::path const &);
uint64_t now ();
MDB_dbi block_database (rai::block_type);
void block_put_raw (MDB_txn *, MDB_dbi, rai::block_hash const &, MDB_val);
void block_put (MDB_txn *, rai::block_hash const &, rai::block const &, rai::block_hash const & = rai::block_hash (0));
MDB_val block_get_raw (MDB_txn *, rai::block_hash const &, rai::block_type &);
rai::block_hash block_successor (MDB_txn *, rai::block_hash const &);
void block_successor_clear (MDB_txn *, rai::block_hash const &);
std::unique_ptr <rai::block> block_get (MDB_txn *, rai::block_hash const &);
std::unique_ptr <rai::block> block_random (MDB_txn *);
std::unique_ptr <rai::block> block_random (MDB_txn *, MDB_dbi);
std::unique_ptr<rai::block> block_get (MDB_txn *, rai::block_hash const &);
std::unique_ptr<rai::block> block_random (MDB_txn *);
std::unique_ptr<rai::block> block_random (MDB_txn *, MDB_dbi);
void block_del (MDB_txn *, rai::block_hash const &);
bool block_exists (MDB_txn *, rai::block_hash const &);
rai::block_counts block_count (MDB_txn *);
void frontier_put (MDB_txn *, rai::block_hash const &, rai::account const &);
rai::account frontier_get (MDB_txn *, rai::block_hash const &);
void frontier_del (MDB_txn *, rai::block_hash const &);
size_t frontier_count (MDB_txn *);
void account_put (MDB_txn *, rai::account const &, rai::account_info const &);
bool account_get (MDB_txn *, rai::account const &, rai::account_info &);
void account_del (MDB_txn *, rai::account const &);
@ -201,7 +201,7 @@ public:
rai::store_iterator latest_begin (MDB_txn *, rai::account const &);
rai::store_iterator latest_begin (MDB_txn *);
rai::store_iterator latest_end ();
void pending_put (MDB_txn *, rai::pending_key const &, rai::pending_info const &);
void pending_del (MDB_txn *, rai::pending_key const &);
bool pending_get (MDB_txn *, rai::pending_key const &, rai::pending_info &);
@ -209,7 +209,7 @@ public:
rai::store_iterator pending_begin (MDB_txn *, rai::pending_key const &);
rai::store_iterator pending_begin (MDB_txn *);
rai::store_iterator pending_end ();
void block_info_put (MDB_txn *, rai::block_hash const &, rai::block_info const &);
void block_info_del (MDB_txn *, rai::block_hash const &);
bool block_info_get (MDB_txn *, rai::block_hash const &, rai::block_info &);
@ -219,49 +219,49 @@ public:
rai::store_iterator block_info_end ();
rai::uint128_t block_balance (MDB_txn *, rai::block_hash const &);
static size_t const block_info_max = 32;
rai::uint128_t representation_get (MDB_txn *, rai::account const &);
void representation_put (MDB_txn *, rai::account const &, rai::uint128_t const &);
void representation_add (MDB_txn *, rai::account const &, rai::uint128_t const &);
rai::store_iterator representation_begin (MDB_txn *);
rai::store_iterator representation_end ();
void unchecked_clear (MDB_txn *);
void unchecked_put (MDB_txn *, rai::block_hash const &, std::shared_ptr <rai::block> const &);
std::vector <std::shared_ptr <rai::block>> unchecked_get (MDB_txn *, rai::block_hash const &);
void unchecked_put (MDB_txn *, rai::block_hash const &, std::shared_ptr<rai::block> const &);
std::vector<std::shared_ptr<rai::block>> unchecked_get (MDB_txn *, rai::block_hash const &);
void unchecked_del (MDB_txn *, rai::block_hash const &, rai::block const &);
rai::store_iterator unchecked_begin (MDB_txn *);
rai::store_iterator unchecked_begin (MDB_txn *, rai::block_hash const &);
rai::store_iterator unchecked_end ();
size_t unchecked_count (MDB_txn *);
std::unordered_multimap <rai::block_hash, std::shared_ptr <rai::block>> unchecked_cache;
std::unordered_multimap<rai::block_hash, std::shared_ptr<rai::block>> unchecked_cache;
void unsynced_put (MDB_txn *, rai::block_hash const &);
void unsynced_del (MDB_txn *, rai::block_hash const &);
bool unsynced_exists (MDB_txn *, rai::block_hash const &);
rai::store_iterator unsynced_begin (MDB_txn *, rai::block_hash const &);
rai::store_iterator unsynced_begin (MDB_txn *);
rai::store_iterator unsynced_end ();
void checksum_put (MDB_txn *, uint64_t, uint8_t, rai::checksum const &);
bool checksum_get (MDB_txn *, uint64_t, uint8_t, rai::checksum &);
void checksum_del (MDB_txn *, uint64_t, uint8_t);
rai::vote_result vote_validate (MDB_txn *, std::shared_ptr <rai::vote>);
rai::vote_result vote_validate (MDB_txn *, std::shared_ptr<rai::vote>);
// Return latest vote for an account from store
std::shared_ptr <rai::vote> vote_get (MDB_txn *, rai::account const &);
std::shared_ptr<rai::vote> vote_get (MDB_txn *, rai::account const &);
// Populate vote with the next sequence number
std::shared_ptr <rai::vote> vote_generate (MDB_txn *, rai::account const &, rai::raw_key const &, std::shared_ptr <rai::block>);
std::shared_ptr<rai::vote> vote_generate (MDB_txn *, rai::account const &, rai::raw_key const &, std::shared_ptr<rai::block>);
// Return either vote or the stored vote with a higher sequence number
std::shared_ptr <rai::vote> vote_max (MDB_txn *, std::shared_ptr <rai::vote>);
std::shared_ptr<rai::vote> vote_max (MDB_txn *, std::shared_ptr<rai::vote>);
// Return latest vote for an account considering the vote cache
std::shared_ptr <rai::vote> vote_current (MDB_txn *, rai::account const &);
std::shared_ptr<rai::vote> vote_current (MDB_txn *, rai::account const &);
void flush (MDB_txn *);
rai::store_iterator vote_begin (MDB_txn *);
rai::store_iterator vote_end ();
std::mutex cache_mutex;
std::unordered_map <rai::account, std::shared_ptr <rai::vote>> vote_cache;
std::unordered_map<rai::account, std::shared_ptr<rai::vote>> vote_cache;
void version_put (MDB_txn *, int);
int version_get (MDB_txn *);
void do_upgrades (MDB_txn *);
@ -274,9 +274,9 @@ public:
void upgrade_v7_to_v8 (MDB_txn *);
void upgrade_v8_to_v9 (MDB_txn *);
void upgrade_v9_to_v10 (MDB_txn *);
void clear (MDB_dbi);
rai::mdb_env environment;
// block_hash -> account // Maps head blocks to owning account
MDB_dbi frontiers;
@ -337,28 +337,28 @@ enum class tally_result
class votes
{
public:
votes (std::shared_ptr <rai::block>);
rai::tally_result vote (std::shared_ptr <rai::vote>);
votes (std::shared_ptr<rai::block>);
rai::tally_result vote (std::shared_ptr<rai::vote>);
// Root block of fork
rai::block_hash id;
// All votes received by account
std::unordered_map <rai::account, std::shared_ptr <rai::block>> rep_votes;
std::unordered_map<rai::account, std::shared_ptr<rai::block>> rep_votes;
};
class ledger
{
public:
ledger (rai::block_store &, rai::uint128_t const & = 0);
std::pair <rai::uint128_t, std::shared_ptr <rai::block>> winner (MDB_txn *, rai::votes const & votes_a);
std::pair<rai::uint128_t, std::shared_ptr<rai::block>> winner (MDB_txn *, rai::votes const & votes_a);
// Map of weight -> associated block, ordered greatest to least
std::map <rai::uint128_t, std::shared_ptr <rai::block>, std::greater <rai::uint128_t>> tally (MDB_txn *, rai::votes const &);
std::map<rai::uint128_t, std::shared_ptr<rai::block>, std::greater<rai::uint128_t>> tally (MDB_txn *, rai::votes const &);
rai::account account (MDB_txn *, rai::block_hash const &);
rai::uint128_t amount (MDB_txn *, rai::block_hash const &);
rai::uint128_t balance (MDB_txn *, rai::block_hash const &);
rai::uint128_t account_balance (MDB_txn *, rai::account const &);
rai::uint128_t account_pending (MDB_txn *, rai::account const &);
rai::uint128_t weight (MDB_txn *, rai::account const &);
std::unique_ptr <rai::block> successor (MDB_txn *, rai::block_hash const &);
std::unique_ptr <rai::block> forked_block (MDB_txn *, rai::block const &);
std::unique_ptr<rai::block> successor (MDB_txn *, rai::block_hash const &);
std::unique_ptr<rai::block> forked_block (MDB_txn *, rai::block const &);
rai::block_hash latest (MDB_txn *, rai::account const &);
rai::block_hash latest_root (MDB_txn *, rai::account const &);
rai::block_hash representative (MDB_txn *, rai::block_hash const &);
@ -399,6 +399,6 @@ public:
explicit genesis ();
void initialize (MDB_txn *, rai::block_store &) const;
rai::block_hash hash () const;
std::unique_ptr <rai::open_block> open;
std::unique_ptr<rai::open_block> open;
};
}

View file

@ -8,10 +8,10 @@ TEST (system, generate_mass_activity)
rai::system system (24000, 1);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
size_t count (20);
system.generate_mass_activity (count, *system.nodes [0]);
system.generate_mass_activity (count, *system.nodes[0]);
size_t accounts (0);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
for (auto i (system.nodes [0]->store.latest_begin (transaction)), n (system.nodes [0]->store.latest_end ()); i != n; ++i)
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
for (auto i (system.nodes[0]->store.latest_begin (transaction)), n (system.nodes[0]->store.latest_end ()); i != n; ++i)
{
++accounts;
}
@ -19,16 +19,16 @@ TEST (system, generate_mass_activity)
TEST (system, generate_mass_activity_long)
{
std::vector <std::thread> threads;
std::vector<std::thread> threads;
{
rai::system system (24000, 1);
rai::thread_runner runner (system.service, system.nodes [0]->config.io_threads);
rai::thread_runner runner (system.service, system.nodes[0]->config.io_threads);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
size_t count (1000000000);
system.generate_mass_activity (count, *system.nodes [0]);
system.generate_mass_activity (count, *system.nodes[0]);
size_t accounts (0);
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, false);
for (auto i (system.nodes [0]->store.latest_begin (transaction)), n (system.nodes [0]->store.latest_end ()); i != n; ++i)
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, false);
for (auto i (system.nodes[0]->store.latest_begin (transaction)), n (system.nodes[0]->store.latest_end ()); i != n; ++i)
{
++accounts;
}
@ -43,25 +43,24 @@ TEST (system, generate_mass_activity_long)
TEST (system, receive_while_synchronizing)
{
std::vector <std::thread> threads;
std::vector<std::thread> threads;
{
rai::system system (24000, 1);
rai::thread_runner runner (system.service, system.nodes [0]->config.io_threads);
rai::thread_runner runner (system.service, system.nodes[0]->config.io_threads);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
size_t count (1000);
system.generate_mass_activity (count, *system.nodes [0]);
system.generate_mass_activity (count, *system.nodes[0]);
rai::keypair key;
rai::node_init init1;
auto node1 (std::make_shared <rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
auto node1 (std::make_shared<rai::node> (init1, system.service, 24001, rai::unique_path (), system.alarm, system.logging, system.work));
ASSERT_FALSE (init1.error ());
node1->network.send_keepalive (system.nodes [0]->network.endpoint ());
node1->network.send_keepalive (system.nodes[0]->network.endpoint ());
auto wallet (node1->wallets.create (1));
ASSERT_EQ (key.pub, wallet->insert_adhoc (key.prv));
node1->start ();
system.alarm.add (std::chrono::system_clock::now () + std::chrono::milliseconds(200), ([&system, &key] ()
{
auto hash (system.wallet (0)->send_sync (rai::test_genesis_key.pub, key.pub, system.nodes [0]->config.receive_minimum.number ()));
auto block (system.nodes [0]->store.block_get (rai::transaction (system.nodes [0]->store.environment, nullptr, false), hash));
system.alarm.add (std::chrono::system_clock::now () + std::chrono::milliseconds (200), ([&system, &key]() {
auto hash (system.wallet (0)->send_sync (rai::test_genesis_key.pub, key.pub, system.nodes[0]->config.receive_minimum.number ()));
auto block (system.nodes[0]->store.block_get (rai::transaction (system.nodes[0]->store.environment, nullptr, false), hash));
std::string block_text;
block->serialize_json (block_text);
}));
@ -110,15 +109,15 @@ TEST (ledger, deep_account_compute)
std::cerr << i << ' ';
}
auto account (ledger.account (transaction, sprevious));
(void) account;
(void)account;
auto balance (ledger.balance (transaction, rprevious));
(void) balance;
(void)balance;
}
}
TEST (wallet, multithreaded_send)
{
std::vector <std::thread> threads;
std::vector<std::thread> threads;
{
rai::system system (24000, 1);
rai::keypair key;
@ -126,15 +125,14 @@ TEST (wallet, multithreaded_send)
wallet_l->insert_adhoc (rai::test_genesis_key.prv);
for (auto i (0); i < 20; ++i)
{
threads.push_back (std::thread ([wallet_l, &key] ()
{
threads.push_back (std::thread ([wallet_l, &key]() {
for (auto i (0); i < 1000; ++i)
{
wallet_l->send_action (rai::test_genesis_key.pub, key.pub, 1000);
}
}));
}
while (system.nodes [0]->balance(rai::test_genesis_key.pub) != (rai::genesis_amount - 20 * 1000 * 1000))
while (system.nodes[0]->balance (rai::test_genesis_key.pub) != (rai::genesis_amount - 20 * 1000 * 1000))
{
system.poll ();
}
@ -148,24 +146,23 @@ TEST (wallet, multithreaded_send)
TEST (store, load)
{
rai::system system (24000, 1);
std::vector <std::thread> threads;
std::vector<std::thread> threads;
for (auto i (0); i < 100; ++i)
{
threads.push_back (std::thread ([&system] ()
{
threads.push_back (std::thread ([&system]() {
for (auto i (0); i != 1000; ++i)
{
rai::transaction transaction (system.nodes [0]->store.environment, nullptr, true);
rai::transaction transaction (system.nodes[0]->store.environment, nullptr, true);
for (auto j (0); j != 10; ++j)
{
rai::block_hash hash;
rai::random_pool.GenerateBlock (hash.bytes.data (), hash.bytes.size ());
system.nodes [0]->store.account_put (transaction, hash, rai::account_info ());
system.nodes[0]->store.account_put (transaction, hash, rai::account_info ());
}
}
}));
}
for (auto &i: threads)
for (auto & i : threads)
{
i.join ();
}
@ -175,8 +172,8 @@ TEST (node, fork_storm)
{
rai::system system (24000, 64);
system.wallet (0)->insert_adhoc (rai::test_genesis_key.prv);
auto previous (system.nodes [0]->latest (rai::test_genesis_key.pub));
auto balance (system.nodes [0]->balance (rai::test_genesis_key.pub));
auto previous (system.nodes[0]->latest (rai::test_genesis_key.pub));
auto balance (system.nodes[0]->balance (rai::test_genesis_key.pub));
ASSERT_FALSE (previous.is_zero ());
for (auto j (0); j != system.nodes.size (); ++j)
{
@ -186,19 +183,19 @@ TEST (node, fork_storm)
previous = send.hash ();
for (auto i (0); i != system.nodes.size (); ++i)
{
auto send_result (system.nodes [i]->process (send));
auto send_result (system.nodes[i]->process (send));
ASSERT_EQ (rai::process_result::progress, send_result.code);
rai::keypair rep;
auto open (std::make_shared <rai::open_block> (previous, rep.pub, key.pub, key.prv, key.pub, 0));
system.nodes [i]->generate_work (*open);
auto open_result (system.nodes [i]->process (*open));
auto open (std::make_shared<rai::open_block> (previous, rep.pub, key.pub, key.prv, key.pub, 0));
system.nodes[i]->generate_work (*open);
auto open_result (system.nodes[i]->process (*open));
ASSERT_EQ (rai::process_result::progress, open_result.code);
rai::transaction transaction (system.nodes [i]->store.environment, nullptr, false);
system.nodes [i]->network.republish_block (transaction, open);
rai::transaction transaction (system.nodes[i]->store.environment, nullptr, false);
system.nodes[i]->network.republish_block (transaction, open);
}
}
auto again (true);
int empty (0);
int single (0);
int iteration (0);
@ -206,8 +203,7 @@ TEST (node, fork_storm)
{
empty = 0;
single = 0;
std::for_each (system.nodes.begin (), system.nodes.end (), [&] (std::shared_ptr <rai::node> const & node_a)
{
std::for_each (system.nodes.begin (), system.nodes.end (), [&](std::shared_ptr<rai::node> const & node_a) {
if (node_a->active.roots.empty ())
{
++empty;
@ -233,7 +229,7 @@ TEST (node, fork_storm)
namespace
{
size_t heard_count (std::vector <uint8_t> const & nodes)
size_t heard_count (std::vector<uint8_t> const & nodes)
{
auto result (0);
for (auto i (nodes.begin ()), n (nodes.end ()); i != n; ++i)
@ -260,9 +256,9 @@ TEST (broadcast, world_broadcast_simulate)
// 0 = starting state
// 1 = heard transaction
// 2 = repeated transaction
std::vector <uint8_t> nodes;
std::vector<uint8_t> nodes;
nodes.resize (node_count, 0);
nodes [0] = 1;
nodes[0] = 1;
auto any_changed (true);
auto message_count (0);
while (any_changed)
@ -273,7 +269,7 @@ TEST (broadcast, world_broadcast_simulate)
switch (*i)
{
case 0:
break;
break;
case 1:
for (auto j (nodes.begin ()), m (nodes.end ()); j != m; ++j)
{
@ -312,9 +308,9 @@ TEST (broadcast, sqrt_broadcast_simulate)
// 0 = starting state
// 1 = heard transaction
// 2 = repeated transaction
std::vector <uint8_t> nodes;
std::vector<uint8_t> nodes;
nodes.resize (node_count, 0);
nodes [0] = 1;
nodes[0] = 1;
auto any_changed (true);
uint64_t message_count (0);
while (any_changed)
@ -325,16 +321,16 @@ TEST (broadcast, sqrt_broadcast_simulate)
switch (*i)
{
case 0:
break;
break;
case 1:
for (auto j (0); j != broadcast_count; ++j)
{
++message_count;
auto entry (rai::random_pool.GenerateWord32 (0, node_count - 1));
switch (nodes [entry])
switch (nodes[entry])
{
case 0:
nodes [entry] = 1;
nodes[entry] = 1;
any_changed = true;
break;
case 1:
@ -377,15 +373,15 @@ TEST (peer_container, random_set)
auto list (container.random_set (15));
}
auto end (std::chrono::system_clock::now ());
auto old_ms (std::chrono::duration_cast <std::chrono::milliseconds> (current - old));
auto new_ms (std::chrono::duration_cast <std::chrono::milliseconds> (end - current));
auto old_ms (std::chrono::duration_cast<std::chrono::milliseconds> (current - old));
auto new_ms (std::chrono::duration_cast<std::chrono::milliseconds> (end - current));
}
TEST (store, unchecked_load)
{
rai::system system (24000, 1);
auto & node (*system.nodes [0]);
auto block (std::make_shared <rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto & node (*system.nodes[0]);
auto block (std::make_shared<rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
for (auto i (0); i < 1000000; ++i)
{
rai::transaction transaction (node.store.environment, nullptr, true);
@ -398,12 +394,12 @@ TEST (store, unchecked_load)
TEST (store, vote_load)
{
rai::system system (24000, 1);
auto & node (*system.nodes [0]);
auto block (std::make_shared <rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
auto & node (*system.nodes[0]);
auto block (std::make_shared<rai::send_block> (0, 0, 0, rai::test_genesis_key.prv, rai::test_genesis_key.pub, 0));
for (auto i (0); i < 1000000; ++i)
{
rai::transaction transaction (node.store.environment, nullptr, true);
auto vote (std::make_shared <rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, i, block));
auto vote (std::make_shared<rai::vote> (rai::test_genesis_key.pub, rai::test_genesis_key.prv, i, block));
node.store.vote_validate (transaction, vote);
}
}

View file

@ -12,7 +12,7 @@ rai::account_info_v1::account_info_v1 (MDB_val const & val_a)
{
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (head) + sizeof (rep_block) + sizeof (balance) + sizeof (modified) == sizeof (*this), "Class not packed");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
rai::account_info_v1::account_info_v1 (rai::block_hash const & head_a, rai::block_hash const & rep_block_a, rai::amount const & balance_a, uint64_t modified_a) :
@ -51,7 +51,7 @@ bool rai::account_info_v1::deserialize (rai::stream & stream_a)
rai::mdb_val rai::account_info_v1::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::account_info_v1 *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::account_info_v1 *> (this));
}
rai::pending_info_v3::pending_info_v3 () :
@ -63,9 +63,9 @@ destination (0)
rai::pending_info_v3::pending_info_v3 (MDB_val const & val_a)
{
assert(val_a.mv_size == sizeof (*this));
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (source) + sizeof (amount) + sizeof (destination) == sizeof (*this), "Packed class");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
rai::pending_info_v3::pending_info_v3 (rai::account const & source_a, rai::amount const & amount_a, rai::account const & destination_a) :
@ -96,14 +96,14 @@ bool rai::pending_info_v3::deserialize (rai::stream & stream_a)
return result;
}
bool rai::pending_info_v3::operator == (rai::pending_info_v3 const & other_a) const
bool rai::pending_info_v3::operator== (rai::pending_info_v3 const & other_a) const
{
return source == other_a.source && amount == other_a.amount && destination == other_a.destination;
}
rai::mdb_val rai::pending_info_v3::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::pending_info_v3 *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::pending_info_v3 *> (this));
}
rai::account_info_v5::account_info_v5 () :
@ -119,7 +119,7 @@ rai::account_info_v5::account_info_v5 (MDB_val const & val_a)
{
assert (val_a.mv_size == sizeof (*this));
static_assert (sizeof (head) + sizeof (rep_block) + sizeof (open_block) + sizeof (balance) + sizeof (modified) == sizeof (*this), "Class not packed");
std::copy (reinterpret_cast <uint8_t const *> (val_a.mv_data), reinterpret_cast <uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast <uint8_t *> (this));
std::copy (reinterpret_cast<uint8_t const *> (val_a.mv_data), reinterpret_cast<uint8_t const *> (val_a.mv_data) + sizeof (*this), reinterpret_cast<uint8_t *> (this));
}
rai::account_info_v5::account_info_v5 (rai::block_hash const & head_a, rai::block_hash const & rep_block_a, rai::block_hash const & open_block_a, rai::amount const & balance_a, uint64_t modified_a) :
@ -164,5 +164,5 @@ bool rai::account_info_v5::deserialize (rai::stream & stream_a)
rai::mdb_val rai::account_info_v5::val () const
{
return rai::mdb_val (sizeof (*this), const_cast <rai::account_info_v5 *> (this));
return rai::mdb_val (sizeof (*this), const_cast<rai::account_info_v5 *> (this));
}

View file

@ -28,7 +28,7 @@ public:
pending_info_v3 (rai::account const &, rai::amount const &, rai::account const &);
void serialize (rai::stream &) const;
bool deserialize (rai::stream &);
bool operator == (rai::pending_info_v3 const &) const;
bool operator== (rai::pending_info_v3 const &) const;
rai::mdb_val val () const;
rai::account source;
rai::amount amount;