Ivan Voras
Ivan Voras is a freelancer and entrepreneur, proud of the breadth of the projects under his belt, and those span from Bitcoin and blockchain technologies, to hardware solutions for Internet of Things devices.
Toptal academy Blockchain lectures #4
2018-03-05
ivan.voras@toptal.com
In Bitcoin and cryptocurrencies forked from Bitcoin, most of this is in chainparams.c / chainparams.h
Needs to be done "manually" by adding code, because mining was removed from the official Bitcoin Core software.
Difficulty statement:
if block_hash < difficulity_target: return True
nBits: compact representation of the difficulty target
0x1d00ffff
number of 0 bytes+3
prefix
means: starts with 00ffff, and 0x1d-3=26 zero bytes
So if nBits is 0x1d00ffff, the difficulty statement is:
if block_hash < 0x00000000ffff0000000000000000000000000000000000000000000000000000: true
Because of the nBits format, the "easiest" difficulty still has 3 bytes fixed.
srand(time(NULL));
genesis = CreateGenesisBlock(1520173282, rand(), 0x1f00ffff, 1, 50 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();
while (!CheckProofOfWork(genesis.GetHash(), genesis.nBits, consensus.powLimit)) {
genesis.nNonce++;
if (genesis.nNonce % (1024*1024) == 0) {
printf("Genesis mining trying %u\r", genesis.nNonce);
}
}
printf("@@@ mainnet new genesis block hash: %s\n@@ merkle root: %s\n@@ nonce: %x\n", consensus.hashGenesisBlock.GetHex().c_str(), genesis.hashMerkleRoot.GetHex().c_str(), genesis.nNonce);
assert(consensus.hashGenesisBlock == uint256S("0x1cae3ba472a97ef60cb3e9b72cc0588142574db4b8a0f0680091ddadb998a1ea"));
assert(genesis.hashMerkleRoot == uint256S("0xebe85c5648c26076e6db053964c402882b0ed89393d23ab978f1795e35d0b10d"));
mining :)
lower difficulty (less leading 0s),
also needs lowering the powLimit
bool CheckProofOfWork(uint256 hash, unsigned int nBits, uint256 powLimit)
{
bool fNegative;
bool fOverflow;
arith_uint256 bnTarget;
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
// Check range
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(powLimit))
return false;
// Check proof of work matches claimed amount
if (UintToArith256(hash) > bnTarget)
return false;
return true;
}
Adapted from pow.cpp
Next: live demo
Homework
By Ivan Voras
The minimal set of changes which need to be made to the C++ code of the Bitcoin Core daemon to make a new cryptocurrency.
Ivan Voras is a freelancer and entrepreneur, proud of the breadth of the projects under his belt, and those span from Bitcoin and blockchain technologies, to hardware solutions for Internet of Things devices.