-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
71 lines (59 loc) · 2.53 KB
/
node.py
File metadata and controls
71 lines (59 loc) · 2.53 KB
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
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from chain.block import Block
from chain.blockchain import Blockchain
from util.helpers import DIFFICULTY_LEVEL, BASE_VALUE
from transaction.transaction import Transaction
import random
class Client:
def __init__(self, mode='POW'):
self.mining_mode = mode
self.__secret_key = rsa.generate_private_key(
public_exponent=65537,
key_size=1028,
backend=default_backend()
)
self.public_key = self.__secret_key.public_key()
self.__wallet = []
self.__blockchain = None
self.genesis_block()
# TODO: Change the hardcoded pk with the actual models pk
def genesis_block(self):
transactions = []
for i in range(1, 51):
transactions.append(Transaction(outputs=[(i, BASE_VALUE)]))
genesis_block = Block(transactions=transactions, previous_hash="genesis")
self.__blockchain = Blockchain(block=genesis_block)
self.__wallet.append(transactions[0].get_outputs()[0])
# TODO: delete this method after integration
def get_sk(self):
return self.__secret_key
# TODO: transaction generation mechanism
def generate_tx(self, outputs, prev_tx_hash, output_index):
for utxo in self.__wallet:
if utxo.get_transaction_hash() == prev_tx_hash and utxo.get_index() == output_index:
utxo.sign(self.__secret_key)
tx = Transaction(originator_sk=self.__secret_key, originator_pk=self.public_key, inputs=[utxo],
outputs=outputs, witnesses_included=True)
tx.sign_transaction()
return tx
def maybe_store_output(self, block):
for tx in block.transactions():
for op in tx.get_outputs():
if op.get_recipient_pk() == self.public_key:
self.__wallet.append(op)
def verify_block(self, block):
# Step #1
# check the difficulty number of zeros in the block hash
if self.mining_mode == 'POW':
if block.hash_difficulty() != DIFFICULTY_LEVEL:
return False
# Step #2:
# check the referenced previous block
return self.__blockchain.add_block(block)
# TODO: delete this method after integration
def get_random_input(self):
return random.choice(self.__wallet)
# TODO: delete this method after integration
def wallet_size(self):
return len(self.__wallet)