8000 a simple algorithm on blockchain technology · glitches-coder/Algorithms@0bc937f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bc937f

Browse files
authored
a simple algorithm on blockchain technology
this is the simple blockchain algorithm implementation making use of classes for securing data and making final blockchain
1 parent 11d1730 commit 0bc937f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Blockchain/blockchain_algorithm.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from hashlib import sha256
2+
import time
3+
4+
class block:
5+
def __init__ (self, timestamp, data, previousHash = ' '):
6+
self.timestamp = timestamp
7+
self.data = data
8+
self.previousHash = previousHash
9+
self.hash = self.calculateHash()
10+
11+
def calculateHash(self):
12+
return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest()
13+
14+
15+
class blockchain:
16+
def __init__(self):
17+
self.chain = [self.createGenesis()]
18+
19+
def createGenesis(self):
20+
return block(time.ctime(), "genesisBlock", "00000")
21+
22+
def mineBlock(self, data):
23+
node = block(time.ctime(), data, self.chain[-1].hash)
24+
# mining a new block to the blockchain
25+
self.chain.append(node)
26+
27+
def printBlockchain(self):
28+
for i in range(len(self.chain)):
29+
print("\n-----Block ", i ,"---------\n timestamp = "\
30+
, self.chain[i].timestamp,"\n data = ", \
31+
self.chain[i].data, "\n previousHash = ",\
32+
self.chain[i].previousHash,"\n hash = ", \
33+
self.chain[i].hash)
34+
35+
36+
37+
CEVcoin = blockchain()
38+
39+
data = input()
40+
41+
# sending data to get mined
42+
print("\n\n ----> Mining New Block -->")
43+
CEVcoin.mineBlock(data)
44+
45+
print("\n\n ----> New Block mined successfully --> ")
46+
47+
CEVcoin.printBlockchain()

0 commit comments

Comments
 (0)
0