To be able to mine a block, a miner has to find a kernel hash that is lower then the coin age of one of his stakes. In short: SHA256(kernel) < stake.
Now, if I create a hash, it may look like this: 32b686fc00275e00daaefc338a356cbb1221b1715b442b6fcda795d10a91b80e (SHA256 hash of ‘peercoin’)
Suppose I have 1000 coins, and their age is 60 days, from their last transaction. That makes my stake 60 * 1000 = 60.000.
How does Peercoin determine that the above hash is lower then the stake?
32b686fc00275e00daaefc338a356cbb1221b1715b442b6fcda795d10a91b80e < 60.000 ?
a hash is just a seq of 32 bytes, which can represent a big number. 0x32, the first byte, is hex for 50 in decimal. etc…
in javascript that would look like this:
var hashProofOfStakeInt = BigInteger.fromByteArrayUnsigned(hashProofOfStake);
this number can be compared to the target, which is in javascript:
var targetInt = bnCoinDayWeight.multiply(bnTargetPerCoinDay);
While bnTargetPerCoinDay is determined by current difficulty, your coin age does make bnCoinDayWeight bigger which helps to get targetInt > hashProofOfStakeInt
My excuses for explaining in code, but at some point it is easier to explain
coinstake must meet hash target according to the protocol:
kernel (input 0) must meet the formula
hash(nStakeModifier + txPrev.block.nTime + txPrev.offset + txPrev.nTime + txPrev.vout.n + nTime) < bnTarget * nCoinDayWeight
this ensures that the chance of getting a coinstake is proportional to the amount of coin age one owns.
So, var hashProofOfStakeInt – just creating a variable that will hold a certain value.
= BigInteger – a value is turned in to a BigInteger.
.fromByteArrayUnsigned(hashProofOfStake) – is the value that turns into a BigInteger, in this case, our hash is converted to a BigInteger. hashProofOfStake is calculated, as mably states above, in the ‘hash’.
I get the concept, but to what is the actual value of hashProofOfStakeInt, and how do we get there? Are you (thehuntergames) suggesting that the hash is hexed, so 32b686… becomes 50-107-134… (without the -)?
This one seems easy:
var targetInt = bnCoinDayWeight.multiply(bnTargetPerCoinDay);
So, a variable ‘targetInt’ is created, which is created by ‘coin day’ * ‘target’.
Then targetInt is compared to hashProofOfStakeInt.
@ mably, thanks, I did find those pages; as stated above, never programmed in Java before. I was hoping for an easy answer
Actually original peercoin code is written in C/C++.
[quote=“fevir, post:4, topic:3616”]So, var hashProofOfStakeInt – just creating a variable that will hold a certain value.
= BigInteger – a value is turned in to a BigInteger.
.fromByteArrayUnsigned(hashProofOfStake) – is the value that turns into a BigInteger, in this case, our hash is converted to a BigInteger. hashProofOfStake is calculated, as mably states above, in the ‘hash’.
I get the concept, but to what is the actual value of hashProofOfStakeInt, and how do we get there? Are you (thehuntergames) suggesting that the hash is hexed, so 32b686… becomes 50-107-134… (without the -)?[/quote]
The Hash function used here actually returns a uint256 (256 bits unsigned integer) value which can be easily converted to a CBigNum (BigInteger) as can be seen here.