-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
123 lines (96 loc) · 3.91 KB
/
main.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
import hashlib
import datetime
import json
import pprint
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config['DEBUG'] = True
@app.route('/hello', methods=['GET'])
def home():
return 'Your web app is running'
class Block:
def __init__(self, timeStamp, trans, previousBlock=''):
self.timeStamp = timeStamp
self.trans = trans
self.previousBlock = previousBlock
self.difficultyIncrement = 0
self.hash = self.calculateHash(trans, timeStamp, self.difficultyIncrement)
def calculateHash(self, data, timeStamp, difficultyIncrement):
data = str(data) + str(timeStamp) + str(difficultyIncrement)
data = data.encode()
hash = hashlib.sha256(data)
return hash.hexdigest()
def mineBlock(self, difficulty):
difficultyCheck = "9" * difficulty
while self.hash[:difficulty] != difficultyCheck:
self.hash = self.calculateHash(self.trans, self.timeStamp, self.difficultyIncrement)
self.difficultyIncrement = self.difficultyIncrement + 1
class Blockchain:
def __init__(self):
self.chain = [self.GenesisBlock()]
self.difficulty = 5
self.pendingTransaction = []
self.reward = 10
def GenesisBlock(self):
genesisBlock = Block(str(datetime.datetime.now()), " Gensis Block")
return genesisBlock
def getLastBlock(self):
return self.chain[len(self.chain) - 1]
def minePendingTrans(self, minerRewardAddress):
# in reality not all of the pending transaction go into the block the miner gets to pick which one to mine
newBlock = Block(str(datetime.datetime.now()), self.pendingTransaction)
newBlock.mineBlock(self.difficulty)
newBlock.previousBlock = self.getLastBlock().hash
print("Previous Block's Hash: " + newBlock.previousBlock)
testChain = []
for trans in newBlock.trans:
temp = json.dumps(trans.__dict__, indent=5, separators=(',', ': '))
testChain.append(temp)
pprint.pprint(testChain)
self.chain.append(newBlock)
print("Block's Hash: " + newBlock.hash)
print("Block added")
rewardTrans = Transaction("System", minerRewardAddress, self.reward)
self.pendingTransaction.append(rewardTrans)
self.pendingTransaction = []
def isChainValid(self):
for x in range(1, len(self.chain)):
currentBlock = self.chain[x]
previousBlock = self.chain[x - 1]
if (currentBlock.previousBlock != previousBlock.hash):
return ("The Chain is not valid!")
return ("The Chain is valid and secure")
def createTrans(self, transaction):
self.pendingTransaction.append(transaction)
@app.route('/name/balance', methods=['GET', 'POST'])
def getBalance(self):
if request.method == 'POST':
walletAddress = request.form['name']
balance = 0
for block in self.chain:
if block.previousBlock == "":
# dont check the first block
continue
for transaction in block.trans:
if transaction.fromWallet == walletAddress:
balance -= transaction.amount
if transaction.toWallet == walletAddress:
balance += transaction.amount
return jsonify(balance)
class Transaction:
def __init__(self, fromWallet, toWallet, amount):
self.fromWallet = fromWallet
self.toWallet = toWallet
self.amount = amount
@app.route('/buy',methods=['GET','POST'])
def buy(name, price):
solariCrypto.createTrans(Transaction(name, "SpaceBrokers", price))
@app.route('/mine',methods=['GET','POST'])
def mine():
if request.method == 'POST':
walletAddress = request.form['name']
solariCrypto.minePendingTrans(walletAddress)
solariCrypto = Blockchain()
if __name__ == '__main__':
app.run()