Account Info
Log Out
English
Back
Log in to access Online Inquiry
Back to the Top

$Bitcoin (BTC.CC)$ import hashlib import datetime class Blo...

$Bitcoin (BTC.CC)$ import hashlib
import datetime

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):
return hashlib.sha256((str(self.index) + self.previous_hash + str(self.timestamp) + self.data + str(self.nonce)).encode()).hexdigest()

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

def create_genesis_block(self):
return Block(0, "0", datetime.datetime.now(), "Genesis Block")

def add_block(self, new_block):
new_block.previous_hash = self.chain[-1].hash
new_block.hash = new_block.calculate_hash()
self.chain.append(new_block)

def proof_of_work(self, block, difficulty):
while block.hash[:difficulty] != '0' * difficulty:
block.nonce += 1
block.hash = block.calculate_hash()
print("Block mined:", block.hash)

# Example usage
difficulty = 4 # Number of zeros required at the start of the hash output
blockchain = Blockchain()

# Mining genesis block
blockchain.proof_of_work(blockchain.chain[0], difficulty)
print("Genesis Block mined")

# Adding additional blocks
num_blocks = 3
for i in range(1, num_blocks + 1):
new_block = Block(i, "", datetime.datetime.now(), "Block " + str(i))
blockchain.proof_of_work(new_block, difficulty)
blockchain.add_block(new_block)
print("Block", i, "added to the blockchain")
Disclaimer: Community is offered by Moomoo Technologies Inc. and is for educational purposes only. Read more
1
Translate
Report
11K Views
Comment
Sign in to post a comment
  • 104686276 : I give u a clue, Not just checking BTC candle Genesis,
    That is not enough, You need to check to what it is Linked, for example to some companies stock genesis, then you see the genesis and source of the flow, then u trully know why it up or down, that coding will explained you everything, and gameplan become "a little" much easier

8Followers
5Following
109Visitors
Follow