IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Réseau/Web Python Discussion :

explication d'un script de minage


Sujet :

Réseau/Web Python

  1. #1
    Membre régulier Avatar de animalx123
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Janvier 2015
    Messages
    148
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Janvier 2015
    Messages : 148
    Points : 96
    Points
    96
    Par défaut explication d'un script de minage
    Bonjour
    j'espere que vous pouvez m'aider, voila j'ai un script pyhon que j'ai telecharger qui est open source et qui mine des bitcoin,et comme moi je suis nouveau sur python et je n'ai aucune idee sur les instruction et les procedure effectuer , et je veux que vous m'exliquais le script etape par etape si c'est possible ,pour avoire une idee
    et devlopper mes connaissance sur le sujet, voici le script :
    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    import time
    import json
    import pprint
    import hashlib
    import struct
    import re
    import base64
    import httplib
    import sys
    from multiprocessing import Process
     
    ERR_SLEEP = 15
    MAX_NONCE = 1000000L
     
    settings = {}
    pp = pprint.PrettyPrinter(indent=4)
     
    class BitcoinRPC:
    	OBJID = 1
     
    	def __init__(self, host, port, username, password):
    		authpair = "%s:%s" % (username, password)
    		self.authhdr = "Basic %s" % (base64.b64encode(authpair))
    		self.conn = httplib.HTTPConnection(host, port, False, 30)
    	def rpc(self, method, params=None):
    		self.OBJID += 1
    		obj = { 'version' : '1.1',
    			'method' : method,
    			'id' : self.OBJID }
    		if params is None:
    			obj['params'] = []
    		else:
    			obj['params'] = params
    		self.conn.request('POST', '/', json.dumps(obj),
    			{ 'Authorization' : self.authhdr,
    			  'Content-type' : 'application/json' })
     
    		resp = self.conn.getresponse()
    		if resp is None:
    			print "JSON-RPC: no response"
    			return None
     
    		body = resp.read()
    		resp_obj = json.loads(body)
    		if resp_obj is None:
    			print "JSON-RPC: cannot JSON-decode body"
    			return None
    		if 'error' in resp_obj and resp_obj['error'] != None:
    			return resp_obj['error']
    		if 'result' not in resp_obj:
    			print "JSON-RPC: no result in object"
    			return None
     
    		return resp_obj['result']
    	def getblockcount(self):
    		return self.rpc('getblockcount')
    	def getwork(self, data=None):
    		return self.rpc('getwork', data)
     
    def uint32(x):
    	return x & 0xffffffffL
     
    def bytereverse(x):
    	return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) |
    			(((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
     
    def bufreverse(in_buf):
    	out_words = []
    	for i in range(0, len(in_buf), 4):
    		word = struct.unpack('@I', in_buf[i:i+4])[0]
    		out_words.append(struct.pack('@I', bytereverse(word)))
    	return ''.join(out_words)
     
    def wordreverse(in_buf):
    	out_words = []
    	for i in range(0, len(in_buf), 4):
    		out_words.append(in_buf[i:i+4])
    	out_words.reverse()
    	return ''.join(out_words)
     
    class Miner:
    	def __init__(self, id):
    		self.id = id
    		self.max_nonce = MAX_NONCE
     
    	def work(self, datastr, targetstr):
    		# decode work data hex string to binary
    		static_data = datastr.decode('hex')
    		static_data = bufreverse(static_data)
     
    		# the first 76b of 80b do not change
    		blk_hdr = static_data[:76]
     
    		# decode 256-bit target value
    		targetbin = targetstr.decode('hex')
    		targetbin = targetbin[::-1]	# byte-swap and dword-swap
    		targetbin_str = targetbin.encode('hex')
    		target = long(targetbin_str, 16)
     
    		# pre-hash first 76b of block header
    		static_hash = hashlib.sha256()
    		static_hash.update(blk_hdr)
     
    		for nonce in xrange(self.max_nonce):
     
    			# encode 32-bit nonce value
    			nonce_bin = struct.pack("<I", nonce)
     
    			# hash final 4b, the nonce value
    			hash1_o = static_hash.copy()
    			hash1_o.update(nonce_bin)
    			hash1 = hash1_o.digest()
     
    			# sha256 hash of sha256 hash
    			hash_o = hashlib.sha256()
    			hash_o.update(hash1)
    			hash = hash_o.digest()
     
    			# quick test for winning solution: high 32 bits zero?
    			if hash[-4:] != '\0\0\0\0':
    				continue
     
    			# convert binary hash to 256-bit Python long
    			hash = bufreverse(hash)
    			hash = wordreverse(hash)
     
    			hash_str = hash.encode('hex')
    			l = long(hash_str, 16)
     
    			# proof-of-work test:  hash < target
    			if l < target:
    				print time.asctime(), "PROOF-OF-WORK found: %064x" % (l,)
    				return (nonce + 1, nonce_bin)
    			else:
    				print time.asctime(), "PROOF-OF-WORK false positive %064x" % (l,)
    #				return (nonce + 1, nonce_bin)
     
    		return (nonce + 1, None)
     
    	def submit_work(self, rpc, original_data, nonce_bin):
    		nonce_bin = bufreverse(nonce_bin)
    		nonce = nonce_bin.encode('hex')
    		solution = original_data[:152] + nonce + original_data[160:256]
    		param_arr = [ solution ]
    		result = rpc.getwork(param_arr)
    		print time.asctime(), "--> Upstream RPC result:", result
     
    	def iterate(self, rpc):
    		work = rpc.getwork()
    		if work is None:
    			time.sleep(ERR_SLEEP)
    			return
    		if 'data' not in work or 'target' not in work:
    			time.sleep(ERR_SLEEP)
    			return
     
    		time_start = time.time()
     
    		(hashes_done, nonce_bin) = self.work(work['data'],
    						     work['target'])
     
    		time_end = time.time()
    		time_diff = time_end - time_start
     
    		self.max_nonce = long(
    			(hashes_done * settings['scantime']) / time_diff)
    		if self.max_nonce > 0xfffffffaL:
    			self.max_nonce = 0xfffffffaL
     
    		if settings['hashmeter']:
    			print "HashMeter(%d): %d hashes, %.2f Khash/sec" % (
    			      self.id, hashes_done,
    			      (hashes_done / 1000.0) / time_diff)
     
    		if nonce_bin is not None:
    			self.submit_work(rpc, work['data'], nonce_bin)
     
    	def loop(self):
    		rpc = BitcoinRPC(settings['host'], settings['port'],
    				 settings['rpcuser'], settings['rpcpass'])
    		if rpc is None:
    			return
     
    		while True:
    			self.iterate(rpc)
     
    def miner_thread(id):
    	miner = Miner(id)
    	miner.loop()
     
    if __name__ == '__main__':
    	if len(sys.argv) != 2:
    		print "Usage: pyminer.py CONFIG-FILE"
    		sys.exit(1)
     
    	f = open(sys.argv[1])
    	for line in f:
    		# skip comment lines
    		m = re.search('^\s*#', line)
    		if m:
    			continue
     
    		# parse key=value lines
    		m = re.search('^(\w+)\s*=\s*(\S.*)$', line)
    		if m is None:
    			continue
    		settings[m.group(1)] = m.group(2)
    	f.close()
     
    	if 'host' not in settings:
    		settings['host'] = '127.0.0.1'
    	if 'port' not in settings:
    		settings['port'] = 8332
    	if 'threads' not in settings:
    		settings['threads'] = 1
    	if 'hashmeter' not in settings:
    		settings['hashmeter'] = 0
    	if 'scantime' not in settings:
    		settings['scantime'] = 30L
    	if 'rpcuser' not in settings or 'rpcpass' not in settings:
    		print "Missing username and/or password in cfg file"
    		sys.exit(1)
     
    	settings['port'] = int(settings['port'])
    	settings['threads'] = int(settings['threads'])
    	settings['hashmeter'] = int(settings['hashmeter'])
    	settings['scantime'] = long(settings['scantime'])
     
    	thr_list = []
    	for thr_id in range(settings['threads']):
    		p = Process(target=miner_thread, args=(thr_id,))
    		p.start()
    		thr_list.append(p)
    		time.sleep(1)			# stagger threads
     
    	print settings['threads'], "mining threads started"
     
    	print time.asctime(), "Miner Starts - %s:%s" % (settings['host'], settings['port'])
    	try:
    		for thr_proc in thr_list:
    			thr_proc.join()
    	except KeyboardInterrupt:
    		pass
    	print time.asctime(), "Miner Stops - %s:%s" % (settings['host'], settings['port'])
    merci de m'aider

  2. #2
    Membre régulier Avatar de animalx123
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Janvier 2015
    Messages
    148
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Janvier 2015
    Messages : 148
    Points : 96
    Points
    96
    Par défaut
    ya il une reponse svp je suis coinser

  3. #3
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 776
    Points
    36 776
    Par défaut
    Citation Envoyé par animalx123 Voir le message
    ya il une reponse svp je suis coinser
    Si vous connaissiez un minimum un langage de programmation impératif (et il y en a plein), vous pourriez a peut près lire/comprendre tout seul ce que fait ce code et poser des questions sur des détails spécifiques à Python. A défaut, si on veut décrire ce que çà fait on ne sait même pas quels mots employer pour que vous puissiez comprendre quoi que ce soit.
    Ceci dit, vous avez des cours et des tutos à disposition. C'est moins rapide mais si vous avez l'ambition de programmer un jour, il faudra bien que vous vous jetiez à l'eau...

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  4. #4
    Membre régulier Avatar de animalx123
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Janvier 2015
    Messages
    148
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Janvier 2015
    Messages : 148
    Points : 96
    Points
    96
    Par défaut
    et bien pour la syntax et la programmation python je suis capable de cree des scripts et comprendre, mais oui j'avoue j'ai etais impeu vague sur ma question,
    en fait je voulais dire dans ce script qui impeu avancer paraport a mes connaissance ,mais quelle sont les differente etape de ce script pour miner ,or pas des details
    sur ce dernier mais en general les procedures traitent quoi?,par exemple cette methode:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    def rpc(self, method, params=None):
    		self.OBJID += 1
    		obj = { 'version' : '1.1',
    			'method' : method,
    			'id' : self.OBJID }
    		if params is None:
    			obj['params'] = []
    		else:
    			obj['params'] = params
    		self.conn.request('POST', '/', json.dumps(obj),
    			{ 'Authorization' : self.authhdr,
    			  'Content-type' : 'application/json' })
    traite quoi ,parsque je sais que c'est une connection a un serveur ou une authentification mais rien d'autre,ou ca:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    def submit_work(self, rpc, original_data, nonce_bin):
    		nonce_bin = bufreverse(nonce_bin)
    		nonce = nonce_bin.encode('hex')
    		solution = original_data[:152] + nonce + original_data[160:256]
    		param_arr = [ solution ]
    		result = rpc.getwork(param_arr)
    		print time.asctime(), "--> Upstream RPC result:", result
    je ne sais pas de quoi ca traite mais je sais que c'est es affectation des valeur apartir des methodes precedentes,
    j'aispere que vous avez compris ma problematique ,merci de m'aide

  5. #5
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 287
    Points : 36 776
    Points
    36 776
    Par défaut
    Citation Envoyé par animalx123 Voir le message
    je ne sais pas de quoi ca traite mais je sais que c'est es affectation des valeur apartir des methodes precedentes,
    j'aispere que vous avez compris ma problematique ,merci de m'aide
    BitcoinRPC est un protocole de communication réseau.
    Pour comprendre/lire le code, il faut aussi connaître ce protocole là...

    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  6. #6
    Membre régulier Avatar de animalx123
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Janvier 2015
    Messages
    148
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Janvier 2015
    Messages : 148
    Points : 96
    Points
    96
    Par défaut
    ben c'est un bon debut pour commencer, merci beaucoups pour l'information

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Une aide pour explication d'un script
    Par jibidy dans le forum Langage
    Réponses: 2
    Dernier message: 08/07/2008, 13h45
  2. Quelques explications sur un script shell
    Par Olivier Regnier dans le forum Shell et commandes GNU
    Réponses: 14
    Dernier message: 03/07/2007, 19h54
  3. explication d'un script
    Par amazircool dans le forum Langage
    Réponses: 1
    Dernier message: 04/04/2007, 08h53
  4. Explication sur un script
    Par donny dans le forum Linux
    Réponses: 6
    Dernier message: 29/06/2006, 11h33
  5. Explication sur un script
    Par Krispy dans le forum Linux
    Réponses: 1
    Dernier message: 22/03/2006, 12h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo