Skip to content

Instantly share code, notes, and snippets.

@somegeekintn
Created February 9, 2014 20:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save somegeekintn/8905544 to your computer and use it in GitHub Desktop.
Block time calculations
void bridgetest_dumpBlocks()
{
CBlockIndex *blockIndex = pindexGenesisBlock;
uint256 prevHash = 0;
int64_t expectedMintValue;
int32_t blockCount = 0;
int32_t badBlockCount = 0;
time_t lastBlockTime, thisBlockTime;
int32_t totalTime[10][10];
int32_t timeCount[10][10];
lastBlockTime = 0;
thisBlockTime = 0;
memset(totalTime, 0, sizeof(int32_t)*10*10);
memset(timeCount, 0, sizeof(int32_t)*10*10);
printf("--->>>Begin block value test\n");
do {
if (blockIndex->pprev) {
prevHash = blockIndex->pprev->GetBlockHash();
expectedMintValue = bridge_getBlockMintedValue(blockIndex->nHeight, prevHash);
thisBlockTime = blockIndex->nTime;
if (lastBlockTime) {
time_t timeDiff = thisBlockTime - lastBlockTime;
int32_t truncVal = expectedMintValue / COIN;
int32_t valSection = truncVal / 100000;
int32_t blockSection = blockCount / 10000;
if (timeDiff > 0 && timeDiff < 900) {
if (valSection > 10)
valSection = 9;
totalTime[blockSection][valSection] += timeDiff;
timeCount[blockSection][valSection]++;
}
else {
printf("time to %d: %ld\n", blockCount, timeDiff);
}
}
printf("%d\n", blockCount);
lastBlockTime = thisBlockTime;
}
blockCount++;
blockIndex = blockIndex->pnext;
} while (blockIndex != NULL);
for (int32_t blockSection=0; blockSection<10; blockSection++) {
printf("--------------------------\n");
printf("blocks %d - %d\n", blockSection * 10000, (blockSection * 10000) + 9999);
for (int32_t valSection=0; valSection<10; valSection++) {
printf("\tval %06d - %06d: %f secs (%d)\n", valSection * 100000, (valSection * 100000) + 99999, (double)totalTime[blockSection][valSection] / (double)timeCount[blockSection][valSection], timeCount[blockSection][valSection]);
}
}
printf("--->>>End block value test\n");
printf("--->>>Tested %d blocks\n", blockCount);
printf("--->>>%d blocks incorrect\n", badBlockCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment