CloakProject / codename-phoenix

New CloakCoin wallet base, Bitcoin fork 0.21.1
MIT License
5 stars 3 forks source link

Investigate bnChainTrust usage and whether or not it needs porting. #20

Open r3rcloak opened 5 years ago

r3rcloak commented 5 years ago

Cloak and PeerCoin etc use a variable called bnChainTrust (member of block index) to ascertain the current chain trust at the particular block height associated with the block index. Seeing as the code used to calculate bnChainTrust has separate paths for PoW and PoS, it is likely that this will need to be ported in some manner, even if that just involves tweaking the nChainTrust property for a block index in the new codebase.

More information to follow after some investigation...

anorakthagreat commented 5 years ago

Client calls GetBlockTrust() in AddToBlockIndex() to calculate bnChainTrust. bnChainTrust is calculated as 2**256 / (bnTarget+1) bnTarget is a function of nBits: bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

https://github.com/CloakProject/CloakCoin/blob/b813d5f21387143b572e7f605f302ec56377de66/src/main.cpp#L2240

GetBlockTrust uses a trick in calculation to still use uint256 instead of uint512. I did some CPU profiling with Valgrind/KCachegrind on this before to see if I can reduce the CPU/mem load - there was an extremely large amount of calls to GetBlockTrust. Think we should stick with the uint256 implementation for that reason.