Leverage nonce for overwhelming chance of single deterministic winner for chain power in POS

This commit is contained in:
miketout
2018-09-28 14:35:38 -07:00
parent 8449c166ed
commit c68ca1a225
3 changed files with 26 additions and 24 deletions

View File

@@ -468,13 +468,18 @@ CChainPower GetBlockProof(const CBlockIndex& block)
bnStakeTarget.SetCompact(header.GetVerusPOSTarget(), &fNegative, &fOverflow);
if (fNegative || fOverflow || bnWorkTarget == 0)
return CChainPower(0);
}
// as the nonce has a fixed definition for a POS block, add the random amount of "work" from the nonce, so there will
// statistically always be a deterministic winner in POS
arith_uint256 aNonce;
aNonce = UintToArith256(block.nNonce);
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
// or ~bnTarget / (nTarget+1) + 1.
return CChainPower(0, (~bnStakeTarget / (bnStakeTarget + 1)) + 1, (~bnWorkTarget / (bnWorkTarget + 1)) + 1);
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
// or ~bnTarget / (nTarget+1) + 1.
return CChainPower(0, ((~bnStakeTarget / (bnStakeTarget + 1)) + 1) + ((~aNonce / (aNonce + 1)) + 1),
(~bnWorkTarget / (bnWorkTarget + 1)) + 1);
}
}
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)