-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into solicitor/directed-broadcast
- Loading branch information
Showing
51 changed files
with
3,092 additions
and
3,479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <nano/core_test/testutil.hpp> | ||
#include <nano/node/common.hpp> | ||
#include <nano/secure/buffer.hpp> | ||
#include <nano/secure/common.hpp> | ||
#include <nano/secure/network_filter.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST (network_filter, unit) | ||
{ | ||
nano::genesis genesis; | ||
nano::network_filter filter (1); | ||
auto one_block = [&genesis, &filter](std::shared_ptr<nano::block> const & block_a, bool expect_duplicate_a) { | ||
nano::publish message (block_a); | ||
auto bytes (message.to_bytes ()); | ||
nano::bufferstream stream (bytes->data (), bytes->size ()); | ||
|
||
// First read the header | ||
bool error{ false }; | ||
nano::message_header header (error, stream); | ||
ASSERT_FALSE (error); | ||
|
||
// This validates nano::message_header::size | ||
ASSERT_EQ (bytes->size (), block_a->size (block_a->type ()) + header.size); | ||
|
||
// Now filter the rest of the stream | ||
bool duplicate (filter.apply (bytes->data (), bytes->size () - header.size)); | ||
ASSERT_EQ (expect_duplicate_a, duplicate); | ||
|
||
// Make sure the stream was rewinded correctly | ||
auto block (nano::deserialize_block (stream, header.block_type ())); | ||
ASSERT_NE (nullptr, block); | ||
ASSERT_EQ (*block, *block_a); | ||
}; | ||
one_block (genesis.open, false); | ||
for (int i = 0; i < 10; ++i) | ||
{ | ||
one_block (genesis.open, true); | ||
} | ||
auto new_block (std::make_shared<nano::state_block> (nano::test_genesis_key.pub, genesis.open->hash (), nano::test_genesis_key.pub, nano::genesis_amount - 10 * nano::xrb_ratio, nano::public_key (), nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0)); | ||
one_block (new_block, false); | ||
for (int i = 0; i < 10; ++i) | ||
{ | ||
one_block (new_block, true); | ||
} | ||
for (int i = 0; i < 100; ++i) | ||
{ | ||
one_block (genesis.open, false); | ||
one_block (new_block, false); | ||
} | ||
} | ||
|
||
TEST (network_filter, many) | ||
{ | ||
nano::genesis genesis; | ||
nano::network_filter filter (4); | ||
nano::keypair key1; | ||
for (int i = 0; i < 100; ++i) | ||
{ | ||
auto block (std::make_shared<nano::state_block> (nano::test_genesis_key.pub, genesis.open->hash (), nano::test_genesis_key.pub, nano::genesis_amount - i * 10 * nano::xrb_ratio, key1.pub, nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0)); | ||
|
||
nano::publish message (block); | ||
auto bytes (message.to_bytes ()); | ||
nano::bufferstream stream (bytes->data (), bytes->size ()); | ||
|
||
// First read the header | ||
bool error{ false }; | ||
nano::message_header header (error, stream); | ||
ASSERT_FALSE (error); | ||
|
||
// This validates nano::message_header::size | ||
ASSERT_EQ (bytes->size (), block->size + header.size); | ||
|
||
// Now filter the rest of the stream | ||
// All blocks should pass through | ||
ASSERT_FALSE (filter.apply (bytes->data (), block->size)); | ||
ASSERT_FALSE (error); | ||
|
||
// Make sure the stream was rewinded correctly | ||
auto deserialized_block (nano::deserialize_block (stream, header.block_type ())); | ||
ASSERT_NE (nullptr, deserialized_block); | ||
ASSERT_EQ (*block, *deserialized_block); | ||
} | ||
} | ||
|
||
TEST (network_filter, clear) | ||
{ | ||
nano::network_filter filter (1); | ||
std::vector<uint8_t> bytes1{ 1, 2, 3 }; | ||
std::vector<uint8_t> bytes2{ 1 }; | ||
ASSERT_FALSE (filter.apply (bytes1.data (), bytes1.size ())); | ||
ASSERT_TRUE (filter.apply (bytes1.data (), bytes1.size ())); | ||
filter.clear (bytes1.data (), bytes1.size ()); | ||
ASSERT_FALSE (filter.apply (bytes1.data (), bytes1.size ())); | ||
ASSERT_TRUE (filter.apply (bytes1.data (), bytes1.size ())); | ||
filter.clear (bytes2.data (), bytes2.size ()); | ||
ASSERT_TRUE (filter.apply (bytes1.data (), bytes1.size ())); | ||
ASSERT_FALSE (filter.apply (bytes2.data (), bytes2.size ())); | ||
} | ||
|
||
TEST (network_filter, optional_digest) | ||
{ | ||
nano::network_filter filter (1); | ||
std::vector<uint8_t> bytes1{ 1, 2, 3 }; | ||
nano::uint128_t digest{ 0 }; | ||
ASSERT_FALSE (filter.apply (bytes1.data (), bytes1.size (), &digest)); | ||
ASSERT_NE (0, digest); | ||
ASSERT_TRUE (filter.apply (bytes1.data (), bytes1.size ())); | ||
filter.clear (digest); | ||
ASSERT_FALSE (filter.apply (bytes1.data (), bytes1.size ())); | ||
} |
Oops, something went wrong.