Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update blockchain_algorithm.py #1645

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions Blockchain/blockchain_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,52 @@
import time

class block:
def __init__ (self, timestamp, data, previousHash = ' '):
self.timestamp = timestamp
self.data = data
self.previousHash = previousHash
self.hash = self.calculateHash()
def __init__ (self, timestamp, data, previousHash = ' '):
self.timestamp = timestamp
self.data = data
self.previousHash = previousHash
self.hash = self.calculateHash()

def calculateHash(self):
return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest()
def calculateHash(self):
return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest()


class blockchain:
def __init__(self):
self.chain = [self.createGenesis()]
def __init__(self):
self.chain = [self.createGenesis()]

def createGenesis(self):
return block(time.ctime(), "genesisBlock", "00000")
def createGenesis(self):
return block(time.ctime(), "genesisBlock", "00000")

def mineBlock(self, data):
node = block(time.ctime(), data, self.chain[-1].hash)
# mining a new block to the blockchain
self.chain.append(node)
def mineBlock(self, data):
if not self.checkChain():
return "Mining unsuccessfully"
node = block(time.ctime(), data, self.chain[-1].hash)
# mining a new block to the blockchain
self.chain.append(node)
return "New Block mined successfully"


def printBlockchain(self):
for i in range(len(self.chain)):
print("\n-----Block ", i ,"---------\n timestamp = "\
, self.chain[i].timestamp,"\n data = ", \
self.chain[i].data, "\n previousHash = ",\
self.chain[i].previousHash,"\n hash = ", \
self.chain[i].hash)
def checkChain(self):
for i in range(len(self.chain)):
if i > 0 and self.chain[i-1].hash != self.chain[i].previousHash:
return False

return True

def printBlockchain(self):
if self.checkChain():
print("Chain is valid")
else:
print("Chain is invalid")
return 0

for i in range(len(self.chain)):
print("\n-----Block ", i ,"---------\n timestamp = "\
, self.chain[i].timestamp,"\n data = ", \
self.chain[i].data, "\n previousHash = ",\
self.chain[i].previousHash,"\n hash = ", \
self.chain[i].hash)



Expand All @@ -40,8 +57,8 @@ def printBlockchain(self):

# sending data to get mined
print("\n\n ----> Mining New Block -->")
CEVcoin.mineBlock(data)

print("\n\n ----> New Block mined successfully --> ")

print("\n\n ----> ",CEVcoin.mineBlock(data)," --> ")

CEVcoin.printBlockchain()