Thursday, December 26, 2024

mining swimming pools – Stratum protocol – drawback with implementation in python

For a few days I am attempting to put in writing easy script in python to speak with stratum pool server. I am utilizing simplified code of NightMiner (https://github.com/ricmoo/nightminer/) and a few testnet I’ve foud ( stratum+tcp://pool.bitcoincloud.web:4008), however I am unable to get the correct consequence. Possibly somebody extra skilled may assist me. Beneath I present every step of my script (step, no python supply code).

1. connect with the server, authorize, get the job information

After connecting to the server I ship this requests:

b'{"id": 1, "methodology": "mining.subscribe", "params": []}n'
b'{"params": ["testuser", "anything"], "id": 2, "methodology": "mining.authorize"}n'

and get the next response:

{"id":1,"consequence":[[["mining.set_difficulty","deadbeefcafebabea200000000000000"],["mining.notify","deadbeefcafebabea200000000000000"]],"40000004",4],"error":null}
{"id":null,"methodology":"mining.set_difficulty","params":[0.01]}
{"id":null,"methodology":"mining.notify","params":["4ca","4128bf630f57387b00e25b419d1bb77e667e4036a7d8fee80000015600000000","01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2503a77614048c8a145e08","122f626974636f696e636c6f75642e6e65742f0000000002062c9c04000000001976a91423e020eacd64acfe093150331d44fdbcc0c7ce0688acc2eb0b00000000001976a91400bf6d61c2a34df5a9ea338fcad188c31bb4a52388ac00000000",[],"20000000","1a02b098","5e148a8c",true]}

2. get the coinbase and merkle root

With this information and extranounce=”00000000″ I can construct the coinbase (in byte kind, however I’ll present as hex on this instance):

coinbase=coinb1+extranounce1+extranounce2+coinb2
coinbase=b'01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2503a77614048c8a145e084000000400000000122f626974636f696e636c6f75642e6e65742f0000000002062c9c04000000001976a91423e020eacd64acfe093150331d44fdbcc0c7ce0688acc2eb0b00000000001976a91400bf6d61c2a34df5a9ea338fcad188c31bb4a52388ac00000000'

Subsequent factor to do is double hash the coinbase with SHA256 algo, which provides as the next bytes:

b'9b9c9ab0b1c92844e4fed3f895f9443fefcda5ae02cc5a8ad4444f9303081a23'

There isn’t a merkle branches (merkle branches array is empty), so hash above is our merkle root.

3. constructing the header

With merkle root and information obtained in step 1, I can begin constructing a header:

header= model + prevhash + merkle root + ntime + nbits + nonce

I exploit the values as follows:

model - obtained '20000000', after little-endian conversion it's '00000020'
prevhash - obtained '4128bf630f57387b00e25b419d1bb77e667e4036a7d8fee80000015600000000', after conversion it's '63bf28417b38570f415be2007eb71b9d36407e66e8fed8a75601000000000000' (reverse bytes order each 4 bytes)
merkle root - get '9b9c9ab0b1c92844e4fed3f895f9443fefcda5ae02cc5a8ad4444f9303081a23' in step 2, stays that approach
ntime - obtained '5e148a8c', after conversion it's '8c8a145e'
nbits - obtained '1a02b098', after conversion it's '98b0021a'

So the header is

b'0000002063bf28417b38570f415be2007eb71b9d36407e66e8fed8a756010000000000009b9c9ab0b1c92844e4fed3f895f9443fefcda5ae02cc5a8ad4444f9303081a238c8a145e98b0021a' + nonce

4. search nonce

the issue is 0.01, so my goal is

00000063ff9c0000000000000000000000000000000000000000000000000000

after some calculations, I discover this 4 bytes to make use of it as a nonce:

b'd0cf1040'

so my full header is now

b'0000002063bf28417b38570f415be2007eb71b9d36407e66e8fed8a756010000000000009b9c9ab0b1c92844e4fed3f895f9443fefcda5ae02cc5a8ad4444f9303081a238c8a145e98b0021ad0cf1040'

after double-hashing it with SHA256 algo and reverse bytes order I get this hash:

000000032f201567fc4d02fa468bf2dc6a5c21ae70098c124e99ee7d2520ca26

which is beneath my goal, so ‘d0cf1040’ ought to be the right nonce.

5. submit nounce

final thing to do is submitting the outcomes, so I ship this request to the server:

b'{"id":2, "methodology":"mining.submit","params":["testuser","4ca","00000000","5e148a8c","d0cf1040"]}n'

and that is response I get:

{"id":2,"consequence":false,"error":null}

I don’t know why I get consequence=false. I can solely suppose there’s a mistake in step 3 (constructing header) and this error is said to byte-order of some values, however I am unable to discover this error (I’ve tried many variants of byte-order, however by no means get the response consequence=true).

I will probably be a lot obliged if anybody can test this calculations and provides me some suggestions.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles