Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block builder #1451

Merged
merged 5 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions rai/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fstream>

#include <gtest/gtest.h>
#include <rai/core_test/testutil.hpp>

#include <rai/lib/interface.h>
#include <rai/node/common.hpp>
Expand Down Expand Up @@ -452,3 +453,239 @@ TEST (block_uniquer, cleanup)
ASSERT_LT (iterations++, 200);
}
}

TEST (block_builder, zeroed_state_block)
{
std::error_code ec;
rai::block_builder builder;
rai::keypair key;
// Make sure manually- and builder constructed all-zero blocks have equal hashes, and check signature.
auto zero_block_manual (std::make_shared<rai::state_block> (0, 0, 0, 0, 0, key.prv, key.pub, 0));
auto zero_block_build = builder.state ().zero ().sign (key.prv, key.pub).build ();
ASSERT_TRUE (zero_block_manual->hash () == zero_block_build->hash ());
ASSERT_FALSE (rai::validate_message (key.pub, zero_block_build->hash (), zero_block_build->signature));
}

TEST (block_builder, state)
{
// Test against a random hash from the live network
std::error_code ec;
rai::block_builder builder;
auto block = builder
.state ()
.account_address ("xrb_15nhh1kzw3x8ohez6s75wy3jr6dqgq65oaede1fzk5hqxk4j8ehz7iqtb3to")
.previous_hex ("FEFBCE274E75148AB31FF63EFB3082EF1126BF72BF3FA9C76A97FD5A9F0EBEC5")
.balance_dec ("2251569974100400000000000000000000")
.representative_address ("xrb_1stofnrxuz3cai7ze75o174bpm7scwj9jn3nxsn8ntzg784jf1gzn1jjdkou")
.link_hex ("E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "2D243F8F92CDD0AD94A1D456A6B15F3BE7A6FCBD98D4C5831D06D15C818CD81F");
}

TEST (block_builder, state_missing_rep)
{
// Test against a random hash from the live network
std::error_code ec;
rai::block_builder builder;
auto block = builder
.state ()
.account_address ("xrb_15nhh1kzw3x8ohez6s75wy3jr6dqgq65oaede1fzk5hqxk4j8ehz7iqtb3to")
.previous_hex ("FEFBCE274E75148AB31FF63EFB3082EF1126BF72BF3FA9C76A97FD5A9F0EBEC5")
.balance_dec ("2251569974100400000000000000000000")
.link_hex ("E16DD58C1EFA8B521545B0A74375AA994D9FC43828A4266D75ECF57F07A7EE86")
.sign_zero ()
.work (0)
.build (ec);
ASSERT_EQ (ec, nano::error_common::missing_representative);
}

TEST (block_builder, state_equality)
{
std::error_code ec;
rai::block_builder builder;

// With constructor
rai::keypair key1, key2;
rai::state_block block1 (key1.pub, 1, key2.pub, 2, 4, key1.prv, key1.pub, 5);

// With builder
auto block2 = builder
.state ()
.account (key1.pub)
.previous (1)
.representative (key2.pub)
.balance (2)
.link (4)
.sign (key1.prv, key1.pub)
.work (5)
.build (ec);

ASSERT_NO_ERROR (ec);
ASSERT_EQ (block1.hash (), block2->hash ());
ASSERT_EQ (block1.work, block2->work);
}

TEST (block_builder, state_errors)
{
std::error_code ec;
rai::block_builder builder;

// Make sure we assert when building a block without an std::error_code
EXPECT_DEATH (builder
.state ()
.account_hex ("xyz")
.build (),
".*");

// Ensure the proper error is generated
builder.state ().account_hex ("xrb_bad").build (ec);
ASSERT_EQ (ec, nano::error_common::bad_account_number);

builder.state ().zero ().account_address ("xrb_1111111111111111111111111111111111111111111111111111hifc8npp").build (ec);
ASSERT_NO_ERROR (ec);
}

TEST (block_builder, open)
{
// Test built block's hash against the Genesis open block from the live network
std::error_code ec;
rai::block_builder builder;
auto block = builder
.open ()
.account_address ("xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3")
.representative_address ("xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3")
.source_hex ("E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "991CF190094C00F0B68E2E5F75F6BEE95A2E0BD93CEAA4A6734DB9F19B728948");
}

TEST (block_builder, open_equality)
{
std::error_code ec;
rai::block_builder builder;

// With constructor
rai::keypair key1, key2;
rai::open_block block1 (1, key1.pub, key2.pub, key1.prv, key1.pub, 5);

// With builder
auto block2 = builder
.open ()
.source (1)
.account (key2.pub)
.representative (key1.pub)
.sign (key1.prv, key1.pub)
.work (5)
.build (ec);

ASSERT_NO_ERROR (ec);
ASSERT_EQ (block1.hash (), block2->hash ());
ASSERT_EQ (block1.work, block2->work);
}

TEST (block_builder, change)
{
std::error_code ec;
rai::block_builder builder;
auto block = builder
.change ()
.representative_address ("xrb_3rropjiqfxpmrrkooej4qtmm1pueu36f9ghinpho4esfdor8785a455d16nf")
.previous_hex ("088EE46429CA936F76C4EAA20B97F6D33E5D872971433EE0C1311BCB98764456")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "13552AC3928E93B5C6C215F61879358E248D4A5246B8B3D1EEC5A566EDCEE077");
}

TEST (block_builder, change_equality)
{
std::error_code ec;
rai::block_builder builder;

// With constructor
rai::keypair key1, key2;
rai::change_block block1 (1, key1.pub, key1.prv, key1.pub, 5);

// With builder
auto block2 = builder
.change ()
.previous (1)
.representative (key1.pub)
.sign (key1.prv, key1.pub)
.work (5)
.build (ec);

ASSERT_NO_ERROR (ec);
ASSERT_EQ (block1.hash (), block2->hash ());
ASSERT_EQ (block1.work, block2->work);
}

TEST (block_builder, send)
{
std::error_code ec;
rai::block_builder builder;
auto block = builder
.send ()
.destination_address ("xrb_1gys8r4crpxhp94n4uho5cshaho81na6454qni5gu9n53gksoyy1wcd4udyb")
.previous_hex ("F685856D73A488894F7F3A62BC3A88E17E985F9969629FF3FDD4A0D4FD823F24")
.balance_hex ("00F035A9C7D818E7C34148C524FFFFEE")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "4560E7B1F3735D082700CFC2852F5D1F378F7418FD24CEF1AD45AB69316F15CD");
}

TEST (block_builder, send_equality)
{
std::error_code ec;
rai::block_builder builder;

// With constructor
rai::keypair key1, key2;
rai::send_block block1 (1, key1.pub, 2, key1.prv, key1.pub, 5);

// With builder
auto block2 = builder
.send ()
.previous (1)
.destination (key1.pub)
.balance (2)
.sign (key1.prv, key1.pub)
.work (5)
.build (ec);

ASSERT_NO_ERROR (ec);
ASSERT_EQ (block1.hash (), block2->hash ());
ASSERT_EQ (block1.work, block2->work);
}

TEST (block_builder, receive_equality)
{
std::error_code ec;
rai::block_builder builder;

// With constructor
rai::keypair key1;
rai::receive_block block1 (1, 2, key1.prv, key1.pub, 5);

// With builder
auto block2 = builder
.receive ()
.previous (1)
.source (2)
.sign (key1.prv, key1.pub)
.work (5)
.build (ec);

ASSERT_NO_ERROR (ec);
ASSERT_EQ (block1.hash (), block2->hash ());
ASSERT_EQ (block1.work, block2->work);
}

TEST (block_builder, receive)
{
std::error_code ec;
rai::block_builder builder;
auto block = builder
.receive ()
.previous_hex ("59660153194CAC5DAC08509D87970BF86F6AEA943025E2A7ED7460930594950E")
.source_hex ("7B2B0A29C1B235FDF9B4DEF2984BB3573BD1A52D28246396FBB3E4C5FE662135")
.build (ec);
ASSERT_EQ (block->hash ().to_string (), "6C004BF911D9CF2ED75CF6EC45E795122AD5D093FF5A83EDFBA43EC4A3EDC722");
}
5 changes: 5 additions & 0 deletions rai/core_test/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
#define ASSERT_NO_ERROR(condition) \
GTEST_TEST_ERROR_CODE (!(condition), #condition, condition.message ().c_str (), "", \
GTEST_FATAL_FAILURE_)

/** Extends gtest with a std::error_code assert that expects an error */
#define ASSERT_IS_ERROR(condition) \
GTEST_TEST_ERROR_CODE ((condition.value () > 0), #condition, "An error was expected", "", \
GTEST_FATAL_FAILURE_)
2 changes: 2 additions & 0 deletions rai/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ add_library (rai_lib
errors.hpp
errors.cpp
expected.hpp
blockbuilders.cpp
blockbuilders.hpp
blocks.cpp
blocks.hpp
config.hpp
Expand Down
Loading