Skip to content

Commit

Permalink
Merge pull request #104 from KOSASIH/deepsource-transform-c9fffffd
Browse files Browse the repository at this point in the history
style: format code with Autopep8, Black, ClangFormat, dotnet-format, Go fmt, Gofumpt, Google Java Format, isort, Ktlint, PHP CS Fixer, Prettier, RuboCop, Ruff Formatter, Rustfmt, Scalafmt, StandardJS, StandardRB, swift-format and Yapf
  • Loading branch information
KOSASIH authored May 10, 2024
2 parents 6e4226e + d42f9e4 commit 6a647cb
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions blockchain/blockchain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hashlib
import time


class Block:
def __init__(self, index, previous_hash, timestamp, data, hash):
self.index = index
Expand All @@ -9,29 +10,43 @@ def __init__(self, index, previous_hash, timestamp, data, hash):
self.data = data
self.hash = hash


class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]
self.difficulty = 5

def create_genesis_block(self):
return Block(0, "0" * 64, int(time.time()), "Genesis Block", self.calculate_hash(0, "0" * 64, int(time.time()), "Genesis Block"))
return Block(
0,
"0" * 64,
int(time.time()),
"Genesis Block",
self.calculate_hash(0, "0" * 64, int(time.time()), "Genesis Block"),
)

def calculate_hash(self, index, previous_hash, timestamp, data):
value = str(index) + str(previous_hash) + str(timestamp) + str(data)
return hashlib.sha256(value.encode('utf-8')).hexdigest()
return hashlib.sha256(value.encode("utf-8")).hexdigest()

def add_block(self, block):
block.previous_hash = self.chain[-1].hash
block.hash = self.calculate_hash(block.index, block.previous_hash, block.timestamp, block.data)
block.hash = self.calculate_hash(
block.index, block.previous_hash, block.timestamp, block.data
)
self.chain.append(block)

def is_valid(self):
for i in range(1, len(self.chain)):
current_block = self.chain[i]
previous_block = self.chain[i - 1]

if current_block.hash != self.calculate_hash(current_block.index, current_block.previous_hash, current_block.timestamp, current_block.data):
if current_block.hash != self.calculate_hash(
current_block.index,
current_block.previous_hash,
current_block.timestamp,
current_block.data,
):
return False

if previous_block.hash != current_block.previous_hash:
Expand Down

0 comments on commit 6a647cb

Please sign in to comment.