Skip to content

Conversation

mmachicao
Copy link
Contributor

Objective: Make the contract for AddTimeData testable.

The procedure AddTimeData is stateful, so any testing beyond trivialities requires some modification in the code:

  • Separation between orchestration (lock/unlock) and algorithm
  • Pushing static (stateful) variables out of the procedure body
  • Applied clang-format-diff.py

Additionally:
wallet_tests.cpp contains errors -> Tests for ComputeTimeSmart falsely assumed that time offset is zero

@fanquake fanquake added the Tests label Dec 28, 2018
BOOST_AUTO_TEST_CASE(util_UtilBuildAddress)
{
CNetAddr cn1 = UtilBuildAddress(0x001, 0x001, 0x001, 0x0D2); // 1.1.1.210

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop all these empty lines surrounding single code lines. Applies throughout this PR :-)


BOOST_AUTO_TEST_CASE(util_AddTimeDataAlgorithmComputeOffsetWhenNewSampleCountIsGreaterEqualFiveAndUneven)
{
int capacity = 20; // can store up to 20 samples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be unsigned to match what CMedianFilter expects?

Applies for all int capacity in this file.



for (unsigned int sample = 1; sample < 10; sample++) { // precondition: at least 4 samples. (including the init sample : 0)
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, sample);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the implicit conversion from unsigned int to unsigned char explicit here.

Applies for all CNetAddr addr = UtilBuildAddress(…, sample); in this file.

{
// The ComputeTimeSmart function within the wallet reads the current time using the GetAdjustedTime function in timedata.
//
// This function computes the time based on the system time (which suports mocking)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be “supports” :-)

BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 200, 220), 100);
SetMockTime(20);
int64_t newBlockTime = GetAdjustedTime() + 10;
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, newBlockTime), clockTime); // time has not changed from the preceeding transaction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be “preceding” :-)

// New transaction should use clock time if lower than block time.
BOOST_CHECK_EQUAL(AddTx(m_wallet, 1, 100, 120), 100);
SetMockTime(10);
int64_t clockTime = GetAdjustedTime(); // time + time offset (unfortunately, from a statefull data structure)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be “stateful” :-)

int capacity = 10;
int64_t offset = 0;
std::set<CNetAddr> knownSet;
CMedianFilter<int64_t> offsetFilter(capacity, 0); // max size : 10 , init samplee: 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be “sample”?


BOOST_CHECK(offset != 0); // offset has changed from the initial value

assert(offsetFilter.size() == 10); // next sample will be the 11th (uneven) and will trigger a new computation of the offset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why assert(…) and not BOOST_CHECK(…)? Applies throughout this PR.

for (int sample = 1; sample < 4; sample++) { // precondition: 4 samples, all outside bounds
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, sample); // 1.1.1.[1,2,3]
AddTimeDataAlgorithm(addr, 2 * DEFAULT_MAX_TIME_ADJUSTMENT, knownSet, offsetFilter, offset); // offsetSample = 2 * DEFAULT_MAX_TIME_ADJUSTMENT (out of bounds)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop empty line at end of block.

@DrahtBot
Copy link
Contributor

Needs rebase

@fanquake
Copy link
Member

Going to close as "Up for Grabs". @dongcarl You might be interested in having a look here.

@adamjonas
Copy link
Member

Believe #16563 removes the need for this. If so, "up for grabs" label should be removed.

@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants