Skip to content

Commit

Permalink
- Moved withdrawal_period_descriptor object into unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTaconator committed Aug 31, 2017
1 parent 9c88361 commit 8a70136
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,6 @@
#include <boost/multi_index/composite_key.hpp>

namespace graphene { namespace chain {
/***
* A descriptor of a particular withdrawal period
*/
struct withdrawal_period_descriptor {
withdrawal_period_descriptor(const time_point_sec start, const time_point_sec end, const asset available, const asset claimed)
: period_start_time(start), period_end_time(end), available_this_period(available), claimed_this_period(claimed) {}

// Start of period
time_point_sec period_start_time;

// End of period
time_point_sec period_end_time;

// Quantify how much is still available to be withdrawn during this period
asset available_this_period;

// Quantify how much has already been claimed during this period
asset claimed_this_period;

string const to_string() const {
string asset_id = fc::to_string(available_this_period.asset_id.space_id)
+ "." + fc::to_string(available_this_period.asset_id.type_id)
+ "." + fc::to_string(available_this_period.asset_id.instance.value);
string text = fc::to_string(available_this_period.amount.value)
+ " " + asset_id
+ " is available from " + period_start_time.to_iso_string()
+ " to " + period_end_time.to_iso_string();
return text;
}
};

/**
* @class withdraw_permission_object
* @brief Grants another account authority to withdraw a limited amount of funds per interval
Expand Down Expand Up @@ -85,7 +54,6 @@ namespace graphene { namespace chain {
/***
* The beginning of the next withdrawal period
* WARNING: Due to caching, this value does not always represent the start of the next or current period (because it is only updated after a withdrawal operation such as claim). For the latest current period, use current_period().
* @see current_period()
*/
time_point_sec period_start_time;
/// The time at which this withdraw permission expires
Expand All @@ -94,7 +62,6 @@ namespace graphene { namespace chain {
/***
* Tracks the total amount
* WARNING: Due to caching, this value does not always represent the total amount claimed during the current period; it may represent what was claimed during the last claimed period (because it is only updated after a withdrawal operation such as claim). For the latest current period, use current_period().
* @see current_period()
*/
share_type claimed_this_period;

Expand All @@ -110,23 +77,6 @@ namespace graphene { namespace chain {
? withdrawal_limit.amount - claimed_this_period
: 0, withdrawal_limit.asset_id );
}

/***
* Get a description of the current withdrawal period
* @param current_time Current time
* @return A description of the current period
*/
withdrawal_period_descriptor current_period(fc::time_point_sec current_time) const {
// @todo [6] Is there a potential race condition where a call to available_this_period might become out of sync with this function's later use of period start time?
asset available = available_this_period(current_time);
asset claimed = asset(withdrawal_limit.amount - available.amount, withdrawal_limit.asset_id);
auto periods = (current_time - period_start_time).to_seconds() / withdrawal_period_sec;
time_point_sec current_period_start = period_start_time + (periods * withdrawal_period_sec);
time_point_sec current_period_end = current_period_start + withdrawal_period_sec;
withdrawal_period_descriptor descriptor = withdrawal_period_descriptor(current_period_start, current_period_end, available, claimed);

return descriptor;
}
};

struct by_from;
Expand Down
73 changes: 61 additions & 12 deletions tests/tests/operation_tests2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,55 @@ using namespace graphene::chain::test;

BOOST_FIXTURE_TEST_SUITE( operation_tests, database_fixture )

/***
* A descriptor of a particular withdrawal period
*/
struct withdrawal_period_descriptor {
withdrawal_period_descriptor(const time_point_sec start, const time_point_sec end, const asset available, const asset claimed)
: period_start_time(start), period_end_time(end), available_this_period(available), claimed_this_period(claimed) {}

// Start of period
time_point_sec period_start_time;

// End of period
time_point_sec period_end_time;

// Quantify how much is still available to be withdrawn during this period
asset available_this_period;

// Quantify how much has already been claimed during this period
asset claimed_this_period;

string const to_string() const {
string asset_id = fc::to_string(available_this_period.asset_id.space_id)
+ "." + fc::to_string(available_this_period.asset_id.type_id)
+ "." + fc::to_string(available_this_period.asset_id.instance.value);
string text = fc::to_string(available_this_period.amount.value)
+ " " + asset_id
+ " is available from " + period_start_time.to_iso_string()
+ " to " + period_end_time.to_iso_string();
return text;
}
};


/***
* Get a description of the current withdrawal period
* @param current_time Current time
* @return A description of the current period
*/
withdrawal_period_descriptor current_period(const withdraw_permission_object& permit, fc::time_point_sec current_time) {
// @todo [6] Is there a potential race condition where a call to available_this_period might become out of sync with this function's later use of period start time?
asset available = permit.available_this_period(current_time);
asset claimed = asset(permit.withdrawal_limit.amount - available.amount, permit.withdrawal_limit.asset_id);
auto periods = (current_time - permit.period_start_time).to_seconds() / permit.withdrawal_period_sec;
time_point_sec current_period_start = permit.period_start_time + (periods * permit.withdrawal_period_sec);
time_point_sec current_period_end = current_period_start + permit.withdrawal_period_sec;
withdrawal_period_descriptor descriptor = withdrawal_period_descriptor(current_period_start, current_period_end, available, claimed);

return descriptor;
}

/**
* This auxiliary test is used for two purposes:
* (a) it checks the creation of withdrawal claims,
Expand Down Expand Up @@ -489,7 +538,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_nominal_case )
BOOST_CHECK(db.find_object(permit) != nullptr);
BOOST_CHECK( permit_object.claimed_this_period == 5 );
BOOST_CHECK_EQUAL( permit_object.available_this_period(db.head_block_time()).amount.value, 0 );
BOOST_CHECK_EQUAL( permit_object.current_period(db.head_block_time()).available_this_period.amount.value, 0 );
BOOST_CHECK_EQUAL( current_period(permit_object, db.head_block_time()).available_this_period.amount.value, 0 );
trx.clear();
generate_blocks(
permit_object.period_start_time
Expand Down Expand Up @@ -538,7 +587,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
}
// Before withdrawing, check the period description
const withdraw_permission_object &first_permit_object = permit(db);
const withdrawal_period_descriptor first_period = first_permit_object.current_period(db.head_block_time());
const withdrawal_period_descriptor first_period = current_period(first_permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(first_period.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch());
BOOST_CHECK_EQUAL(first_period.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + expected_period_duration_seconds);
BOOST_CHECK_EQUAL(first_period.available_this_period.amount.value, 5);
Expand All @@ -548,7 +597,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
// Before claiming, check the period description
const withdraw_permission_object& permit_object = permit(db);
BOOST_CHECK(db.find_object(permit) != nullptr);
withdrawal_period_descriptor period_descriptor = permit_object.current_period(db.head_block_time());
withdrawal_period_descriptor period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 5);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 0));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 1));
Expand All @@ -568,7 +617,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
BOOST_CHECK(db.find_object(permit) != nullptr);
BOOST_CHECK( permit_object.claimed_this_period == 4 );
BOOST_CHECK_EQUAL( permit_object.claimed_this_period.value, 4 );
period_descriptor = permit_object.current_period(db.head_block_time());
period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 1);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 0));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 1));
Expand All @@ -585,7 +634,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
// Before claiming, check the period description
const withdraw_permission_object& permit_object = permit(db);
BOOST_CHECK(db.find_object(permit) != nullptr);
withdrawal_period_descriptor period_descriptor = permit_object.current_period(db.head_block_time());
withdrawal_period_descriptor period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 5);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 1));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 2));
Expand All @@ -605,7 +654,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
BOOST_CHECK(db.find_object(permit) != nullptr);
BOOST_CHECK( permit_object.claimed_this_period == 1 );
BOOST_CHECK_EQUAL( permit_object.claimed_this_period.value, 1 );
period_descriptor = permit_object.current_period(db.head_block_time());
period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 4);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 1));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 2));
Expand All @@ -622,15 +671,15 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
// Before claiming, check the period description
const withdraw_permission_object& permit_object = permit(db);
BOOST_CHECK(db.find_object(permit) != nullptr);
withdrawal_period_descriptor period_descriptor = permit_object.current_period(db.head_block_time());
withdrawal_period_descriptor period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 5);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 2));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 3));

// No claim

// After doing nothing, check the period description
period_descriptor = permit_object.current_period(db.head_block_time());
period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 5);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 2));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 3));
Expand All @@ -645,7 +694,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
// Before claiming, check the period description
const withdraw_permission_object& permit_object = permit(db);
BOOST_CHECK(db.find_object(permit) != nullptr);
withdrawal_period_descriptor period_descriptor = permit_object.current_period(db.head_block_time());
withdrawal_period_descriptor period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 5);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 3));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 4));
Expand All @@ -665,7 +714,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
BOOST_CHECK(db.find_object(permit) != nullptr);
BOOST_CHECK( permit_object.claimed_this_period == 5 );
BOOST_CHECK_EQUAL( permit_object.claimed_this_period.value, 5 );
period_descriptor = permit_object.current_period(db.head_block_time());
period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 0);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 3));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 4));
Expand All @@ -682,7 +731,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
// Before claiming, check the period description
const withdraw_permission_object& permit_object = permit(db);
BOOST_CHECK(db.find_object(permit) != nullptr);
withdrawal_period_descriptor period_descriptor = permit_object.current_period(db.head_block_time());
withdrawal_period_descriptor period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 5);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 4));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 5));
Expand All @@ -702,7 +751,7 @@ BOOST_AUTO_TEST_CASE( withdraw_permission_incremental_case )
BOOST_CHECK(db.find_object(permit) != nullptr);
BOOST_CHECK( permit_object.claimed_this_period == 3 );
BOOST_CHECK_EQUAL( permit_object.claimed_this_period.value, 3 );
period_descriptor = permit_object.current_period(db.head_block_time());
period_descriptor = current_period(permit_object, db.head_block_time());
BOOST_CHECK_EQUAL(period_descriptor.available_this_period.amount.value, 2);
BOOST_CHECK_EQUAL(period_descriptor.period_start_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 4));
BOOST_CHECK_EQUAL(period_descriptor.period_end_time.sec_since_epoch(), expected_first_period_start_time.sec_since_epoch() + (expected_period_duration_seconds * 5));
Expand Down

0 comments on commit 8a70136

Please sign in to comment.