-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_chain.py
79 lines (62 loc) · 2.06 KB
/
block_chain.py
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
import hashlib
import json
from time import time
from urllib.parse import urlparse
from uuid import uuid4
#from Crypto.PublicKey import RSA
from flask import Flask, jsonify, request
import requests
import wallet
import data
#import flask
class Blockchain:
def __init__(self):
self.chain = []
def register_node(self, address,key):
self.publicKeys.append(key)
parsed_url = urlparse(address)
if parsed_url.netloc:
self.nodes.append(parsed_url.netloc)
elif parsed_url.path:
# Accepts an URL without scheme like '192.168.0.5:5000'.
self.nodes.append(parsed_url.path)
else:
raise ValueError('Invalid URL')
def valid_chain(self, chain):
last_block = chain[0]
current_index = 1
while current_index < len(chain):
block = chain[current_index]
print(f'{last_block}')
print(f'{block}')
print("\n-----------\n")
# Check that the hash of the block is correct
last_block_hash = self.hash(last_block)
if block['previous_hash'] != last_block_hash:
return False
# Check that the Proof of Work is correct
if not self.valid_proof(last_block['proof'], block['proof'], last_block_hash):
return False
last_block = block
current_index += 1
return True
def print_chain(self):
blockAsList=[]
for bl in self.chain:
blockAsList.append(bl.asDictionary())
print (blockAsList)
def imported_block(self, index, transactions,timestamp,proof,previous_hash,current_hash):
block = {
'index': index,
'timestamp': timestamp,
'transactions': transactions,
'proof': proof,
'previous_hash': previous_hash ,
'current_hash':current_hash
}
self.current_transactions = []
self.chain.append(block)
return block
@property
def last_block(self):
return self.chain[-1]