-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyblock.py
More file actions
101 lines (84 loc) · 3.44 KB
/
pyblock.py
File metadata and controls
101 lines (84 loc) · 3.44 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
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
import hashlib
import time
import csv
import random
class Block:
#A basic block contains, index (blockheight), the previous hash, a timestamp, tx information, a nonce, and the current hash
def __init__(self, index, previousHash, timestamp, data, proof, currentHash):
self.index = index
self.previousHash = previousHash
self.timestamp = timestamp
self.data = data
self.currentHash = currentHash
self.proof = proof
def getGenesisBlock():
return Block(0, '0', '1516636420.03901', "Initial block", 0, '02d779570304667b4c28ba1dbfd4428844a7cab89023205c66858a40937557f8')
def calculateHash(index, previousHash, timestamp, data, proof):
value = str(index) + str(previousHash) + str(timestamp) + str(data) + str(proof)
sha = hashlib.sha256(value.encode('utf-8'))
return str(sha.hexdigest())
def calculateHashForBlock(block):
return calculateHash(block.index, block.previousHash, block.timestamp, block.data, block.proof)
def getLatestBlock(blockchain):
return blockchain[len(blockchain)-1]
def generateNextBlock(blockchain, blockData, timestamp, proof):
previousBlock = getLatestBlock(blockchain)
nextIndex = int(previousBlock.index) + 1
nextTimestamp = timestamp
nextHash = calculateHash(nextIndex, previousBlock.currentHash, nextTimestamp, proof, blockData)
return Block(nextIndex, previousBlock.currentHash, nextTimestamp, blockData, proof, nextHash)
"""
writing blockchain into file
"""
def writeBlockchain(blockchain):
blockchainList = []
for block in blockchain:
blockList = [block.index, block.previousHash, str(block.timestamp), block.data, block.proof, block.currentHash]
blockchainList.append(blockList)
with open("testtest.csv", "w") as file:
writer = csv.writer(file)
writer.writerows(blockchainList)
print('Blockchain written to blockchain.csv.')
def readBlockchain(blockchainFilePath):
importedBlockchain = []
try:
with open(blockchainFilePath, 'r') as file:
blockReader = csv.reader(file)
for line in blockReader:
block = Block(line[0], line[1], line[2], line[3], line[4], line[5])
importedBlockchain.append(block)
print("Pulling blockchain from csv...")
return importedBlockchain
except:
print('No blockchain located. Generating new genesis block...')
return [getGenesisBlock()]
def newData():
#test purposes only
some_data = {'sent_from':'user1' ,
'sent_to': 'user2',
'amount': 100
}
return some_data
def mineNewBlock(blockchainPath = 'testtest.csv'):
blockchain = readBlockchain(blockchainPath)
timestamp = time.time()
proof = 0
newBlockFound = False
print('Mining a block...')
while not newBlockFound:
#print("Trying new block proof...")
newBlockAttempt = generateNextBlock(blockchain, newData(), timestamp, proof)
if newBlockAttempt.currentHash[0:1] == '5':
stopTime = time.time()
timer = stopTime - timestamp
print('New block found with proof', proof, 'in', round(timer, 2), 'seconds.')
newBlockFound = True
else:
proof += 1
blockchain.append(newBlockAttempt)
writeBlockchain(blockchain)
def mine(blocksToMine):
for _ in range(blocksToMine):
mineNewBlock()
# amount = int(input('Enter an integer amount of blocks to mine'))
mine(500)