-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #917 from denravonska/correct-tally-on-init
Correct tally on init
- Loading branch information
Showing
8 changed files
with
138 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,5 @@ OBJS= \ | |
obj/boinc.o \ | ||
obj/allocators.o \ | ||
obj/backup.o \ | ||
obj/appcache.o | ||
obj/appcache.o \ | ||
obj/tally.o |
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 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,20 @@ | ||
#include "tally.h" | ||
#include "main.h" | ||
|
||
#include <cassert> | ||
|
||
CBlockIndex* FindTallyTrigger(CBlockIndex* block) | ||
{ | ||
// Scan backwards until we find one where accepting it would | ||
// trigger a tally. | ||
for(; | ||
block && block->pprev && !IsTallyTrigger(block); | ||
block = block->pprev); | ||
|
||
return block; | ||
} | ||
|
||
bool IsTallyTrigger(const CBlockIndex *block) | ||
{ | ||
return block && block->nHeight % TALLY_GRANULARITY == 0; | ||
} |
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,25 @@ | ||
#pragma once | ||
|
||
#include "fwd.h" | ||
|
||
//! | ||
//! \brief Find previous tally trigger from block. | ||
//! | ||
//! Scans the chain back from \p block to find the block which should be used | ||
//! as the starting point for a tally. \p block must not be NULL. | ||
//! | ||
//! \param block Block to start from. | ||
//! \return Tally trigger point from \p block. | ||
//! | ||
CBlockIndex* FindTallyTrigger(CBlockIndex* block); | ||
|
||
//! | ||
//! \brief Check if a block is a tally trigger. | ||
//! | ||
//! Checks the height of \p block to see if it matches the tally granularity. | ||
//! If this returns \c false then no tally should be made on that block. | ||
//! | ||
//! \param block Block to check. | ||
//! \return | ||
//! | ||
bool IsTallyTrigger(const CBlockIndex* block); |
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,58 @@ | ||
#include "tally.h" | ||
#include "main.h" | ||
|
||
#include <boost/test/unit_test.hpp> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace | ||
{ | ||
typedef std::shared_ptr<CBlockIndex> CBlockIndexPtr; | ||
std::vector<CBlockIndexPtr> CreateChain() | ||
{ | ||
std::vector<CBlockIndexPtr> blocks; | ||
|
||
// Create a chain of 100 blocks | ||
for(int i=1; i<=100; ++i) | ||
{ | ||
CBlockIndexPtr block = std::make_shared<CBlockIndex>(); | ||
block->nHeight = i; | ||
|
||
// Attach to chain | ||
if(!blocks.empty()) | ||
{ | ||
CBlockIndexPtr previous = blocks.back(); | ||
previous->pnext = block.get(); | ||
block->pprev = previous.get(); | ||
} | ||
|
||
blocks.push_back(block); | ||
} | ||
|
||
return blocks; | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE(tally_tests) | ||
|
||
BOOST_AUTO_TEST_CASE(tally_FindTallyTriggerShouldUseTallyGranularity) | ||
{ | ||
BOOST_CHECK_EQUAL(TALLY_GRANULARITY, 10); | ||
|
||
auto chain = CreateChain(); | ||
CBlockIndexPtr head = chain.back(); | ||
|
||
const CBlockIndex* block = FindTallyTrigger(head.get()->pprev); | ||
BOOST_CHECK_EQUAL(block->nHeight, 90); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(tally_IsTallyTriggerShouldUseTallyGranularity) | ||
{ | ||
auto chain = CreateChain(); | ||
CBlockIndexPtr head = chain.back(); | ||
|
||
BOOST_CHECK(IsTallyTrigger(head.get())); | ||
BOOST_CHECK(IsTallyTrigger(head->pprev) == false); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |