-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding block_pipeline::epoch_restrictions_filter which only allows ep…
…och blocks to pass if they don't modify the state of the account other than its epoch.
- Loading branch information
Showing
7 changed files
with
158 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <nano/node/block_pipeline/context.hpp> | ||
#include <nano/node/block_pipeline/epoch_restrictions_filter.hpp> | ||
#include <nano/secure/common.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace | ||
{ | ||
class context | ||
{ | ||
public: | ||
context () | ||
{ | ||
filter.pass = [this] (nano::block_pipeline::context & context) { | ||
pass.push_back (context); | ||
}; | ||
filter.reject_balance = [this] (nano::block_pipeline::context & context) { | ||
reject_balance.push_back (context); | ||
}; | ||
filter.reject_representative = [this] (nano::block_pipeline::context & context) { | ||
reject_representative.push_back (context); | ||
}; | ||
} | ||
nano::block_pipeline::epoch_restrictions_filter filter; | ||
std::vector<nano::block_pipeline::context> pass; | ||
std::vector<nano::block_pipeline::context> reject_balance; | ||
std::vector<nano::block_pipeline::context> reject_representative; | ||
}; | ||
nano::block_pipeline::context epoch_pass_blocks () | ||
{ | ||
nano::block_builder builder; | ||
nano::block_pipeline::context result; | ||
result.block = builder.state () | ||
.account (nano::dev::genesis_key.pub) | ||
.previous (nano::dev::genesis->hash ()) | ||
.representative (nano::dev::genesis_key.pub) | ||
.balance (nano::dev::constants.genesis_amount) // Unchanged balance | ||
.link (nano::dev::constants.epochs.link (nano::epoch::epoch_1)) | ||
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) | ||
.work (0) | ||
.build_shared (); | ||
result.previous = nano::dev::genesis; | ||
result.state = nano::account_info{}; | ||
result.state->balance = nano::dev::constants.genesis_amount; | ||
result.state->representative = nano::dev::genesis_key.pub; | ||
return result; | ||
} | ||
nano::block_pipeline::context epoch_reject_balance_blocks () | ||
{ | ||
nano::block_builder builder; | ||
nano::block_pipeline::context result; | ||
result.block = builder.state () | ||
.account (nano::dev::genesis_key.pub) | ||
.previous (nano::dev::genesis->hash ()) | ||
.representative (nano::dev::genesis_key.pub) // Unchacked representative | ||
.balance (nano::dev::constants.genesis_amount - 1) | ||
.link (nano::dev::constants.epochs.link (nano::epoch::epoch_1)) | ||
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) | ||
.work (0) | ||
.build_shared (); | ||
result.previous = nano::dev::genesis; | ||
result.state = nano::account_info{}; | ||
result.state->balance = nano::dev::constants.genesis_amount; // Changed balance | ||
result.state->representative = nano::dev::genesis_key.pub; | ||
return result; | ||
} | ||
nano::block_pipeline::context epoch_reject_representative_blocks () | ||
{ | ||
nano::block_builder builder; | ||
nano::block_pipeline::context result; | ||
result.block = builder.state () | ||
.account (nano::dev::genesis_key.pub) | ||
.previous (nano::dev::genesis->hash ()) | ||
.representative (nano::dev::genesis_key.pub) | ||
.balance (nano::dev::constants.genesis_amount) // Unchanged balance | ||
.link (nano::dev::constants.epochs.link (nano::epoch::epoch_1)) | ||
.sign (nano::dev::genesis_key.prv, nano::dev::genesis_key.pub) | ||
.work (0) | ||
.build_shared (); | ||
result.previous = nano::dev::genesis; | ||
result.state = nano::account_info{}; | ||
result.state->balance = nano::dev::constants.genesis_amount; | ||
nano::keypair dummy; | ||
result.state->representative = dummy.pub; // Changed representative | ||
return result; | ||
} | ||
} | ||
|
||
TEST (epoch_restrictions_filter, epoch_pass) | ||
{ | ||
context context; | ||
auto blocks = epoch_pass_blocks (); | ||
context.filter.sink (blocks); | ||
ASSERT_EQ (1, context.pass.size ()); | ||
} | ||
|
||
TEST (epoch_restrictions_filter, epoch_reject_balance) | ||
{ | ||
context context; | ||
auto blocks = epoch_reject_balance_blocks (); | ||
context.filter.sink (blocks); | ||
ASSERT_EQ (1, context.reject_balance.size ()); | ||
} | ||
|
||
TEST (epoch_restrictions_filter, epoch_reject_representative) | ||
{ | ||
context context; | ||
auto blocks = epoch_reject_representative_blocks (); | ||
context.filter.sink (blocks); | ||
ASSERT_EQ (1, context.reject_representative.size ()); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <nano/node/block_pipeline/context.hpp> | ||
#include <nano/node/block_pipeline/epoch_restrictions_filter.hpp> | ||
|
||
void nano::block_pipeline::epoch_restrictions_filter::sink (context & context) | ||
{ | ||
debug_assert (context.state.has_value ()); | ||
if (context.state->balance != context.block->balance ()) | ||
{ | ||
reject_balance (context); | ||
return; | ||
} | ||
if (context.state->representative != context.block->representative ()) | ||
{ | ||
reject_representative (context); | ||
return; | ||
} | ||
pass (context); | ||
} |
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,23 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
namespace nano | ||
{ | ||
namespace block_pipeline | ||
{ | ||
class context; | ||
/** | ||
This filter checks the restrictions on epoch blocks. | ||
Epoch blocks cannot change the state of an account other than changing the account's epoch | ||
*/ | ||
class epoch_restrictions_filter | ||
{ | ||
public: | ||
void sink (context & context); | ||
std::function<void (context & context)> pass; | ||
std::function<void (context & context)> reject_balance; | ||
std::function<void (context & context)> reject_representative; | ||
}; | ||
} | ||
} |
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