Bitcoin Forum

Alternate cryptocurrencies => Mining (Altcoins) => Topic started by: TheWolf666 on December 05, 2018, 01:25:18 PM



Title: Mining a block c++ code
Post by: TheWolf666 on December 05, 2018, 01:25:18 PM
I know it will not mine anything and it is not the idea, but I would like to know if anyone had made a mining block function.
It is for testing how the blockchain is working, not to mine bitcoin.

I would like to have something equivalent to this program written in Python, but as a function that could be run from the chainparams.cpp initialisation for example.
The idea is that with such a function, it would be possible to mine the first blocks of a new blockchain, or mine any type of algorithm, since it would run inside the wallet at the initialisation.
If no one did it already I will do it myself but if it already exists, why re-create the wheel?

Here is the code in python that I have found.

https://gist.githubusercontent.com/shirriff/cd5c66da6ba21a96bb26/raw/85a75b2fc2457f9e8bf5c148fde98b5ddd2094c3/mine.py

Code:
import hashlib, struct
 
ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root = "871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a"
time_ = 0x53058b35 # 2014-02-20 04:57:25
bits = 0x19015f53
 
# https://en.bitcoin.it/wiki/Difficulty
exp = bits >> 24
mant = bits & 0xffffff
target_hexstr = '%064x' % (mant * (1<<(8*(exp - 3))))
target_str = target_hexstr.decode('hex')
 
nonce = 0
while nonce < 0x100000000:
    header = ( struct.pack("<L", ver) + prev_block.decode('hex')[::-1] +
          mrkl_root.decode('hex')[::-1] + struct.pack("<LLL", time_, bits, nonce))
    hash = hashlib.sha256(hashlib.sha256(header).digest()).digest()
    print nonce, hash[::-1].encode('hex')
    if hash[::-1] < target_str:
        print 'success'
        break
    nonce += 1


Title: Re: Mining a block c++ code
Post by: cryptoDEADBEEFFFFF on January 02, 2019, 01:42:39 PM
There is a miner written in pure python compatible with rpc getblocktemplate. Feel free to check it out. https://github.com/vsergeev/ntgbtminer

It helped me a lot to understand how mining works.


Title: Re: Mining a block c++ code
Post by: TheWolf666 on January 03, 2019, 08:32:37 AM
Thanks for the answer, at least 1 answer to that post lol, I was waiting desperately that some Legendary gurus come to help, but no luck!

Actually, Bitcoin Core 0.17.0.1 (latest at the time I am writing) has a lot of security checks, that are basically blocking any altcoin to be developed from their source code without removing them.

I understand that Bitcoin needs to be secured, but adding a "if (nheight>somevalue) {" when they are adding these security checks would help newbies to get into this code in a shorter time.

Some would say, if you cannot find out, then you are not good enough to maintain a coin, I got these kind of answers from people who are supposed to have a hi-IQ in these forums. Amazing.

So now the way to overcome the fact that there is no internal mining code (since version 0.14) is to use a pool and stratum.

I have fixed my problem by installing Yiimp (there are install scripts for each version of Ubuntu, latest is here: https://github.com/xavatar/yiimp_install_scrypt_ubuntu18.04 )

Once you have installed Yiimp, you can use ccminer to premine or just run the blockchain because without mining the blockchain is dead.

You need to comment all the timing/headers/Tips security tests  otherwise your nodes will ban your mining wallet, or just comment the ban system as it is not working for new coins...and that's it.

No documentation about these kind of issues in any "make your own altcoin in 10 min" posts of course. All these tutorials are deprecated for Bitcoin 0.17.x