Skip to content

Commit

Permalink
Unit tests for post rate limiting steemit#176
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed Jul 20, 2016
1 parent 93e6700 commit d46cb3c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
4 changes: 2 additions & 2 deletions libraries/chain/include/steemit/chain/account_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ namespace steemit { namespace chain {
uint64_t average_market_bandwidth = 0;
time_point_sec last_market_bandwidth_update;
time_point_sec last_post;
time_point_sec last_root_post;
uint32_t post_bandwidth;
time_point_sec last_root_post = fc::time_point_sec::min();
uint32_t post_bandwidth = 0;

/**
* Used to track activity rewards, updated on every post and comment
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/steemit/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@

#define STEEMIT_MIN_ROOT_COMMENT_INTERVAL (fc::seconds(60*5)) // 5 minutes
#define STEEMIT_MIN_REPLY_INTERVAL (fc::seconds(20)) // 20 seconds
#define STEEMIT_POST_AVERAGE_WINDOW (60*60*24u) // 1 hours
#define STEEMIT_POST_AVERAGE_WINDOW (60*60*24u) // 1 day
#define STEEMIT_POST_MAX_BANDWIDTH (4*STEEMIT_100_PERCENT) // 2 posts per 1 days, average 1 every 12 hours
#define STEEMIT_POST_WEIGHT_CONSTANT (STEEMIT_POST_MAX_BANDWIDTH * STEEMIT_POST_MAX_BANDWIDTH)
#define STEEMIT_POST_WEIGHT_CONSTANT (uint64_t(STEEMIT_POST_MAX_BANDWIDTH) * STEEMIT_POST_MAX_BANDWIDTH)

#define STEEMIT_MAX_ACCOUNT_WITNESS_VOTES 30

Expand Down
6 changes: 3 additions & 3 deletions libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,21 @@ void comment_evaluator::do_apply( const comment_operation& o )
}

uint16_t reward_weight = STEEMIT_100_PERCENT;
uint32_t post_bandwidth = auth.post_bandwidth;
uint64_t post_bandwidth = auth.post_bandwidth;

if( db().has_hardfork( STEEMIT_HARDFORK_0_12__176 ) && o.parent_author.size() == 0 )
{
uint64_t post_delta_time = std::min( db().head_block_time().sec_since_epoch() - auth.last_root_post.sec_since_epoch(), STEEMIT_POST_AVERAGE_WINDOW );
uint32_t old_weight = uint32_t( ( post_bandwidth * ( STEEMIT_POST_AVERAGE_WINDOW - post_delta_time ) ) / STEEMIT_POST_AVERAGE_WINDOW );
post_bandwidth = ( old_weight + STEEMIT_100_PERCENT );
reward_weight = std::min( uint16_t( ( STEEMIT_POST_WEIGHT_CONSTANT * STEEMIT_100_PERCENT ) / ( post_bandwidth * post_bandwidth ) ), uint16_t( STEEMIT_100_PERCENT ) );
reward_weight = uint16_t( std::min( ( STEEMIT_POST_WEIGHT_CONSTANT * STEEMIT_100_PERCENT ) / ( post_bandwidth * post_bandwidth ), uint64_t( STEEMIT_100_PERCENT ) ) );
}

db().modify( auth, [&]( account_object& a ) {
if( o.parent_author.size() == 0 )
{
a.last_root_post = now;
a.post_bandwidth = post_bandwidth;
a.post_bandwidth = uint32_t( post_bandwidth );
}
a.last_post = now;
a.post_count++;
Expand Down
96 changes: 96 additions & 0 deletions tests/tests/operation_time_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2166,5 +2166,101 @@ BOOST_AUTO_TEST_CASE( liquidity_rewards )
FC_LOG_AND_RETHROW();
}

BOOST_AUTO_TEST_CASE( post_rate_limit )
{
try
{
ACTORS( (alice) )

fund( "alice", 10000 );
vest( "alice", 10000 );

comment_operation op;
op.author = "alice";
op.permlink = "test1";
op.parent_author = "";
op.parent_permlink = "test";
op.body = "test";

signed_transaction tx;

tx.operations.push_back( op );
tx.set_expiration( db.head_block_time() + STEEMIT_MAX_TIME_UNTIL_EXPIRATION );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

uint64_t alice_post_bandwidth = STEEMIT_100_PERCENT;

BOOST_REQUIRE( alice.post_bandwidth == alice_post_bandwidth );
BOOST_REQUIRE( db.get_comment( "alice", "test1" ).reward_weight == STEEMIT_100_PERCENT );

tx.operations.clear();
tx.signatures.clear();

generate_blocks( db.head_block_time() + STEEMIT_MIN_ROOT_COMMENT_INTERVAL + fc::seconds( STEEMIT_BLOCK_INTERVAL ), true );

op.permlink = "test2";

tx.operations.push_back( op );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

alice_post_bandwidth = STEEMIT_100_PERCENT + ( alice_post_bandwidth * ( STEEMIT_POST_AVERAGE_WINDOW - STEEMIT_MIN_ROOT_COMMENT_INTERVAL.to_seconds() - STEEMIT_BLOCK_INTERVAL ) / STEEMIT_POST_AVERAGE_WINDOW );

BOOST_REQUIRE( db.get_account( "alice" ).post_bandwidth == alice_post_bandwidth );
BOOST_REQUIRE( db.get_comment( "alice", "test2" ).reward_weight == STEEMIT_100_PERCENT );

generate_blocks( db.head_block_time() + STEEMIT_MIN_ROOT_COMMENT_INTERVAL + fc::seconds( STEEMIT_BLOCK_INTERVAL ), true );

tx.operations.clear();
tx.signatures.clear();

op.permlink = "test3";

tx.operations.push_back( op );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

alice_post_bandwidth = STEEMIT_100_PERCENT + ( alice_post_bandwidth * ( STEEMIT_POST_AVERAGE_WINDOW - STEEMIT_MIN_ROOT_COMMENT_INTERVAL.to_seconds() - STEEMIT_BLOCK_INTERVAL ) / STEEMIT_POST_AVERAGE_WINDOW );

BOOST_REQUIRE( db.get_account( "alice" ).post_bandwidth == alice_post_bandwidth );
BOOST_REQUIRE( db.get_comment( "alice", "test3" ).reward_weight == STEEMIT_100_PERCENT );

generate_blocks( db.head_block_time() + STEEMIT_MIN_ROOT_COMMENT_INTERVAL + fc::seconds( STEEMIT_BLOCK_INTERVAL ), true );

tx.operations.clear();
tx.signatures.clear();

op.permlink = "test4";

tx.operations.push_back( op );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

alice_post_bandwidth = STEEMIT_100_PERCENT + ( alice_post_bandwidth * ( STEEMIT_POST_AVERAGE_WINDOW - STEEMIT_MIN_ROOT_COMMENT_INTERVAL.to_seconds() - STEEMIT_BLOCK_INTERVAL ) / STEEMIT_POST_AVERAGE_WINDOW );

BOOST_REQUIRE( db.get_account( "alice" ).post_bandwidth == alice_post_bandwidth );
BOOST_REQUIRE( db.get_comment( "alice", "test4" ).reward_weight == STEEMIT_100_PERCENT );

generate_blocks( db.head_block_time() + STEEMIT_MIN_ROOT_COMMENT_INTERVAL + fc::seconds( STEEMIT_BLOCK_INTERVAL ), true );

tx.operations.clear();
tx.signatures.clear();

op.permlink = "test5";

tx.operations.push_back( op );
tx.sign( alice_private_key, db.get_chain_id() );
db.push_transaction( tx, 0 );

alice_post_bandwidth = STEEMIT_100_PERCENT + ( alice_post_bandwidth * ( STEEMIT_POST_AVERAGE_WINDOW - STEEMIT_MIN_ROOT_COMMENT_INTERVAL.to_seconds() - STEEMIT_BLOCK_INTERVAL ) / STEEMIT_POST_AVERAGE_WINDOW );
auto reward_weight = ( STEEMIT_POST_WEIGHT_CONSTANT * STEEMIT_100_PERCENT ) / ( alice_post_bandwidth * alice_post_bandwidth );

BOOST_REQUIRE( db.get_account( "alice" ).post_bandwidth == alice_post_bandwidth );
BOOST_REQUIRE( db.get_comment( "alice", "test5" ).reward_weight == reward_weight );
}
FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_SUITE_END()
#endif

0 comments on commit d46cb3c

Please sign in to comment.