altmarkets / altmarketscoin Goto Github PK
View Code? Open in Web Editor NEWUtility Coin for Altmarkets Exchange
License: GNU General Public License v3.0
Utility Coin for Altmarkets Exchange
License: GNU General Public License v3.0
I'm not sure where the root of this code comes from; searching github reveals 4700+ repositories with it in it.
double GetPoWMHashPS(const CBlockIndex* blockindex)
{
int nPoWInterval = 72;
int64_t nTargetSpacingWorkMin = 30, nTargetSpacingWork = 30;
const CBlockIndex* pindex = chainActive.Genesis();
const CBlockIndex* pindexPrevWork = chainActive.Genesis();
while (pindex)
{
if (pindex->IsProofOfWork())
{
int64_t nActualSpacingWork = pindex->GetBlockTime() - pindexPrevWork->GetBlockTime();
nTargetSpacingWork = ((nPoWInterval - 1) * nTargetSpacingWork + nActualSpacingWork + nActualSpacingWork) / (nPoWInterval + 1);
nTargetSpacingWork = std::max(nTargetSpacingWork, nTargetSpacingWorkMin);
pindexPrevWork = pindex;
}
pindex = chainActive.Next(pindex);
}
return GetDifficulty() * 4294.967296 / nTargetSpacingWork;
}
This code, appears to either be ridiculously inefficient, or just not functioning as it was intended to.
First; it takes an argument "blockindex", which isn't used anywhere. The prototype defines it to be NULL if a caller doesn't include it; which the caller doesn't. So it comes in as null. Not that it matters, since it's not used.
The code starts at the genesis block, and while (pindex), walks through the whole chain from genesis to tip. When it comes across a a block that is Proof of Work, it gets the time difference between the current block and the previous proof of work block; creates nTargetSpacingWork from that information, and then checks if it's bigger then the minimum...
... and then continues to the next block. When it finds another Proof of Work block, it overwrites nTargetSpacingWork with the new information.
In other words; it walks down the whole entire chain, doing calculations at every proof of work block it finds, only to return the nTargetSpacingWork for the last Proof of Work block it found.
If that is really what was intended; then it would make much more sense to start at chainActive.Tip(), and walk backwards through the chain until you find a proof of work block (setting pindexNextWork to be that block), and continuing until the next earlier PoW block is found, calculate the spacing if pIndexNextWork != NULL, and breaking out of the while loop when done.
e.g.
pindex = chainActive.Tip();
pindexNextWork = NULL;
while (pindex) {
if (pindex->isProofOfWork) {
if (pindexNextWork == NULL) {
pindexNextWork = pindex;
continue; // found the last, find the next to last.
} else {
int64_t nActualSpacingWork = pindexNextWork->GetBlockTime() - pindex->GetBlockTime();
nTargetSpacingWork = ((nPoWInterval - 1) * nTargetSpacingWork + nActualSpacingWork + nActualSpacingWork) / (nPoWInterval + 1);
nTargetSpacingWork = std::max(nTargetSpacingWork, nTargetSpacingWorkMin);
break; // found both, got our number; get out.
}
}
pindex = pindex->pprev;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.