Ke_eS is here for you
HERE I AM WITH YOU, FOR YOU ALWAYS
BLOCK CHAIN
import hashlib import time class Block: def __init__(self, index, previous_hash, timestamp, data, nonce=0): self.index = index self.previous_hash = previous_hash self.timestamp = timestamp self.data = data self.nonce = nonce self.hash = self.calculate_hash() def calculate_hash(self): block_string = f"{self.index}{self.previous_hash}{self.timestamp}{self.data}{self.nonce}" return hashlib.sha256(block_string.encode()).hexdigest() def mine_block(self, difficulty): target = '0' * difficulty while self.hash[:difficulty] != target: self.nonce += 1 self.hash = self.calculate_hash() print(f"Block mined: {self.hash}") class Blockchain: def __init__(self, difficulty=4): self.chain = [self.create_genesis_block()] self.difficulty = difficulty def create_genesis_block(self): return Block(0, "0", time.time(), "Genesis Block") def get_latest_block(self): return self.chain[-1] def add_block(self, new_block): new_block.previous_hash = self.get_latest_block().hash new_block.mine_block(self.difficulty) self.chain.append(new_block) def is_chain_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 != current_block.calculate_hash(): print("Current block hash is invalid!") return False if current_block.previous_hash != previous_block.hash: print("Previous block hash is invalid!") return False return True # Example usage blockchain = Blockchain() print("Mining block 1...") blockchain.add_block(Block(1, "", time.time(), "Block 1 Data")) print("Mining block 2...") blockchain.add_block(Block(2, "", time.time(), "Block 2 Data")) print("Mining block 3...") blockchain.add_block(Block(3, "", time.time(), "Block 3 Data")) # Print the blockchain for block in blockchain.chain: print(f"Block {block.index} [Hash: {block.hash}, Previous Hash: {block.previous_hash}, Data: {block.data}]") # Check if the blockchain is valid print("Is blockchain valid?", blockchain.is_chain_valid())
http://www.pu-zo.com
http://www.zogam.com
http://www.zogamonline.com
http://www.zokuomthawn.com
http://www.zolengthe.net,
Blog Archive
▼
2024
(1)
▼
April
(1)
THE STATE OF MIND
►
2018
(1)
►
December
(1)
►
2017
(1)
►
April
(1)
►
2010
(1)
►
May
(1)
►
2009
(4)
►
August
(3)
►
January
(1)
►
2008
(10)
►
August
(1)
►
July
(5)
►
April
(1)
►
March
(3)
ALBUM LINKS
:: Shillong Tour
:: Pupute Zu puohna
:: Phiamphu Phungpi Organisation
:: Thongmang Mualsuong Dohna
:: Thangliensang Muolsuong Dohna
:: HolidayIn2008
Blogger Template
by
Blogcrowds
Friday, April 26, 2024
THE STATE OF MIND
Yet to be uploaded
0 Comments:
Post a Comment
Older Post
Home
Subscribe to:
Post Comments (Atom)
My favourite Blog List
MY HAPPY FAMILY
IN TUNE WITH THE TIME
YOU ARE ONE OF THEM
VISITORS" TRIAL
Feedjit Live Blog Stats
0 Comments:
Post a Comment