-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlockchain.js
More file actions
32 lines (27 loc) · 973 Bytes
/
Blockchain.js
File metadata and controls
32 lines (27 loc) · 973 Bytes
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
class Blockchain:
"""
Código anterior...
"""
def add_block(self, block, proof):
"""
Una función que agrega el bloque a la cadena luego de la verificación.
La verificación incluye:
* Chequear que la prueba es válida.
* El valor del previous_hash del bloque coincide con el hash del último
bloque de la cadena.
"""
previous_hash = self.last_block.hash
if previous_hash != block.previous_hash:
return False
if not self.is_valid_proof(block, proof):
return False
block.hash = proof
self.chain.append(block)
return True
def is_valid_proof(self, block, block_hash):
"""
Chquear si block_hash es un hash válido y satisface nuestro
criterio de dificultad.
"""
return (block_hash.startswith('0' * Blockchain.difficulty) and
block_hash == block.compute_hash())