-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import hashlib | ||
import time | ||
|
||
class Ledger: | ||
def __init__(self): | ||
self.chain = [] | ||
self.create_genesis_block() | ||
|
||
def create_genesis_block(self): | ||
"""Create the genesis block of the blockchain.""" | ||
genesis_block = { | ||
'index': 0, | ||
'timestamp': int(time.time()), | ||
'data': 'Genesis Block', | ||
'previous_hash': '0' * 64 | ||
} | ||
self.chain.append(genesis_block) | ||
|
||
def calculate_hash(self, block): | ||
"""Calculate the hash of a block.""" | ||
block_string = json.dumps(block, sort_keys=True).encode() | ||
return hashlib.sha256(block_string).hexdigest() | ||
|
||
def add_block(self, data):"""Add a new block to the blockchain.""" | ||
previous_block = self.chain[-1] | ||
new_block = { | ||
'index': len(self.chain), | ||
'timestamp': int(time.time()), | ||
'data': data, | ||
'previous_hash': self.calculate_hash(previous_block) | ||
} | ||
self.chain.append(new_block) |