-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathduckchain.py
175 lines (123 loc) · 3.37 KB
/
duckchain.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
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
import hashlib
from time import time
import json
from uuid import uuid4
import urllib2
import requests
class Blockchain:
def __init__(self):
# blocks list
self.blocks = []
self.node_identifier = str(uuid4()).replace('-', '')
self.fullNodeUrl = "http://localhost:5000"
# nodes address list
self.nodes_list = [self.fullNodeUrl]
def generate_genesis_block(self):
"""
generate genesis block
:return: json format block
"""
genesis_block = {
"version":1,
"index" : 1,
"data" : [],
"proof" : 100,
"timestamp" : time(),
"previous_hash" : "0000",
}
genesis_block["this_hash"] = self.hash(genesis_block)
# reward the miner
self.new_transaction(0,self.node_identifier,1,genesis_block)
# add to the block list
self.blocks.append(genesis_block)
return genesis_block
def hash(self,block):
"""
generate hash of a block
:param block: <json> json format block
:return: <json> sha256 hash of the block
"""
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
def mine_new_block(self):
"""
solve the proof-of-work puzzle and mine a new block
:return: <json> json format new block
"""
previous_block = self.blocks[-1]
index = previous_block['index'] + 1
# solve the pow
proof = self.proof_of_work(previous_block)
previous_hash = previous_block['this_hash']
new_block = {
"version":1,
"index" : index,
"data" : [],
"proof" : proof,
"timestamp" : time(),
"previous_hash" : previous_hash,
}
new_block["this_hash"] = self.hash(new_block)
return new_block
def proof_of_work(self,previous_block):
"""
solve the proof-of-work,and find a proof
:param previous_block: <json> json format block
:return proof:<int> number to solve the pow
"""
proof = 0
while not self.validate_proof(proof,previous_block):
proof += 1
return proof
def validate_proof(self,proof,previous_block):
"""
validate the proof that user input
:param proof: <int>
:param previous_block:<json>
:return: <bool> True if proof is valid
"""
previous_proof = previous_block['proof']
previous_hash = previous_block['this_hash']
proof_str = str( proof * previous_proof) + previous_hash
result = hashlib.sha256(proof_str).hexdigest()
return result[:4] == "0000"
def new_transaction(self,sender,receiver,amount,block):
"""
add transaction to the block
:param sender:<string>
:param receiver:<string>
:param amount:<int>
:param block:<json>
:return:<int> the index transaction inserted
"""
transaction = {
"sender":sender,
"receiver":receiver,
"amount":amount,
"timestamp":time()
}
block["data"].append(transaction)
return block["index"]
def loadUrl(self,url):
"""
load url content,and convert it to json
:param url:<string>
:return:<json> (if url respond) or <string> ("wrong" if url does not respond)
"""
try:
data = urllib2.urlopen(url).read()
return json.loads(data)
except Exception,e:
return "wrong"
def postData(self,url,post_data):
"""
post data to url,and convert response to json
:param url:<string>
:param post_data:<json> data need to post
:return:<json> (if url respond) or <string> ("wrong" if url does not respond)
"""
try:
data = requests.post(url, data=post_data)
return json.loads(data.text)
except Exception,e:
return "wrong"