Bonjour à toutes et à tous ,

Voici un code en Python pour effectuer le minage de crypto BTC, retrouvé sur le site ci-joint. J'ai légèrement modifié le code car il présentait un problème de division par zéro :




https://128mots.com/2021/02/23/bitco...rithme-python/

Ca marche super pour block 671712 de l'exemple :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import hashlibimport time
import struct
import binascii
import datetime
from binascii import unhexlify, hexlify
from dateutil import parser
from datetime import datetime
from datetime import timedelta


nontrouve = True
dtprec = datetime.now()
inonc = 1768459821 - 400  # Starting 400 before the good nonce
resultat = []


while nontrouve:
    inonc += 1
    if inonc % 50 == 0:
        time_difference = datetime.now() - dtprec
        if time_difference.total_seconds() != 0:
            print(
                str(round(timedelta(seconds=1) / time_difference)) + " H/s"
            )
    dtprec = datetime.now()
    header_hex = (
        binascii.hexlify(struct.Struct("<L").pack(int("0x3fff0000", 16))).decode()
        + binascii.hexlify(
            binascii.unhexlify("000000000000000000062a949bc297739a12e639ba9e2107638b469afe11d0f8")[
                ::-1
            ]
        ).decode()
        + binascii.hexlify(
            binascii.unhexlify("692e5f889b4d5c35925402ae46a6b00bf249d768715d1475f15e4a2325b8930e")[
                ::-1
            ]
        ).decode()
        + binascii.hexlify(
            struct.Struct("<L").pack(
                int(hex(int(parser.parse("2021-02-22 15:39:00 GMT +1").timestamp()) - 3600), 16)
            )
        ).decode()
        + binascii.hexlify(struct.Struct("<L").pack(int("0x170cf4e3", 16))).decode()
        + binascii.hexlify(struct.Struct("<L").pack(int(hex(inonc), 16))).decode()
    )
    header_bin = unhexlify(header_hex)
    dt1 = datetime.now().strftime("%H:%M:%S.%f")
    hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
    hexlify(hash).decode("utf-8")
    hexlify(hash[::-1]).decode("utf-8")
    hash = hexlify(hash[::-1]).decode("utf-8")
    resultat.append([round(int(hash, 16) / 10 ** 65)])


    MAX_TARGET = int("00000000FFFF0000000000000000000000000000000000000000000000000000", 16)
    Difficulty = 21724134900047
    target = int(MAX_TARGET / Difficulty)
    target32 = "{:0>64x}".format(target)
    if int(hash, 16) < int(target32, 16):
        print("###########BLOC MINED###################")
        print("HEADER=" + header_hex)
        print("HASH=" + hash)
        print("NONCE=" + str(inonc))
        print("NONCE (hex)=" + hex(inonc))
        print("###########BLOC MINED###################")
        break
Mais pour autres block ca ne marche par exemple pour le Bloc 790732:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import hashlibimport time
import struct
import binascii
import datetime
from binascii import unhexlify, hexlify
from dateutil import parser
from datetime import datetime
from datetime import timedelta


nontrouve = True
dtprec = datetime.now()
inonc = 4006631778 - 400  # Starting 400 before the good nonce
resultat = []


while nontrouve:
    inonc += 1
    if inonc % 50 == 0:
        time_difference = datetime.now() - dtprec
        if time_difference.total_seconds() != 0:
            print(
                str(round(timedelta(seconds=1) / time_difference)) + " H/s"
            )
    dtprec = datetime.now()
    header_hex = (
        binascii.hexlify(struct.Struct("<L").pack(int("0x30e0c000", 16))).decode()
        + binascii.hexlify(
            binascii.unhexlify("00000000000000000003e7e3d5d006a6e8145e564f07e873f3dd857132066868")[
                ::-1
            ]
        ).decode()
        + binascii.hexlify(
            binascii.unhexlify("6429ba685a97048c9a50196de127e668c1d32379a38f41cab2609882359cae23")[
                ::-1
            ]
        ).decode()
        + binascii.hexlify(
            struct.Struct("<L").pack(
                int(hex(int(parser.parse("2023-05-21 12:02:17 GMT +1").timestamp()) - 3600), 16)
            )
        ).decode()
        + binascii.hexlify(struct.Struct("<L").pack(int("0x1705ae3a", 16))).decode()
        + binascii.hexlify(struct.Struct("<L").pack(int(hex(inonc), 16))).decode()
    )
    header_bin = unhexlify(header_hex)
    dt1 = datetime.now().strftime("%H:%M:%S.%f")
    hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
    hexlify(hash).decode("utf-8")
    hexlify(hash[::-1]).decode("utf-8")
    hash = hexlify(hash[::-1]).decode("utf-8")
    resultat.append([round(int(hash, 16) / 10 ** 65)])


    MAX_TARGET = int("00000000FFFF0000000000000000000000000000000000000000000000000000", 16)
    Difficulty = 49549703178592
    target = int(MAX_TARGET / Difficulty)
    target32 = "{:0>64x}".format(target)
    if int(hash, 16) < int(target32, 16):
        print("###########BLOC MINED###################")
        print("HEADER=" + header_hex)
        print("HASH=" + hash)
        print("NONCE=" + str(inonc))
        print("NONCE (hex)=" + hex(inonc))
        print("###########BLOC MINED###################")
        break
https://blockstream.info/block/00000...a6d1b981a065d3


Le calcul de minage utilise une boucle "for" qui tente de trouver la valeur d'entrée, qui est une variable d'entrée inférieure à la difficulté , et autres entrées de transactions précédentes, et la sortie de cette boucle doit commencer aujourd'hui par 19 zéros et le bloc trouvé doit être inférieur à 2.5 Mga de mémoire.

Pour simplifier, nous avons une fonction de hachage sha256 qui rend presque impossible de prédire sa valeur de sortie en fonction d'entré donnée. Nous essayons de trouver cette entrée en utilisant une boucle "for" pour vérifier certaines conditions.

Donc, rien n'interdit de prendre une entrée alétoire est essayer notre chance si l'entrée est proche, tu finiras par trouver cette sortie qui respecte ses condtions.


En plus je pense qu'il est possible de prévoir la fonction de hachage et donc le inonc en entrainent une IA car je peux avoir plien de donné avec cette fonction pour nourir une IA et c'est ca le but de ce topic.

Pourriez-vous m'aider à faire fonctionner ce code pour les blocs récents et constuire cette IA? Je n'aime pas jouer au loto, mais jouer à ça m'intéresse, surtout que je ne vais pas acheter un ticket.