Bitcoin Forum
April 19, 2024, 06:21:46 PM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Mining a block c++ code  (Read 265 times)
TheWolf666 (OP)
Full Member
***
Offline Offline

Activity: 615
Merit: 154


CEO of Metaisland.gg and W.O.K Corp


View Profile WWW
December 05, 2018, 01:25:18 PM
Last edit: December 05, 2018, 05:25:03 PM by TheWolf666
 #1

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

1713550906
Hero Member
*
Offline Offline

Posts: 1713550906

View Profile Personal Message (Offline)

Ignore
1713550906
Reply with quote  #2

1713550906
Report to moderator
"You Asked For Change, We Gave You Coins" -- casascius
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713550906
Hero Member
*
Offline Offline

Posts: 1713550906

View Profile Personal Message (Offline)

Ignore
1713550906
Reply with quote  #2

1713550906
Report to moderator
1713550906
Hero Member
*
Offline Offline

Posts: 1713550906

View Profile Personal Message (Offline)

Ignore
1713550906
Reply with quote  #2

1713550906
Report to moderator
1713550906
Hero Member
*
Offline Offline

Posts: 1713550906

View Profile Personal Message (Offline)

Ignore
1713550906
Reply with quote  #2

1713550906
Report to moderator
cryptoDEADBEEFFFFF
Newbie
*
Offline Offline

Activity: 24
Merit: 4


View Profile
January 02, 2019, 01:42:39 PM
Merited by frodocooper (2), NeuroticFish (1)
 #2

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.
TheWolf666 (OP)
Full Member
***
Offline Offline

Activity: 615
Merit: 154


CEO of Metaisland.gg and W.O.K Corp


View Profile WWW
January 03, 2019, 08:32:37 AM
 #3

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

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!