From daf5ab65492a7f7f94e28b9a9b0f70afaf8c9577 Mon Sep 17 00:00:00 2001 From: "f.strw" Date: Sat, 15 Jun 2024 15:37:41 +0300 Subject: [PATCH] v.1.1 - PoW checks implemented - Networking implemented, two instances of Memechain can now be connected to each other and share one blockchain. - Minor front-end changes --- blockchain.py | 26 +++++++++++++++---- templates/index.html | 59 +++++++++++++++++++++++++++++++------------- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/blockchain.py b/blockchain.py index 5c5d0f8..10e2efd 100644 --- a/blockchain.py +++ b/blockchain.py @@ -7,24 +7,36 @@ from datetime import datetime from flask import Flask, jsonify, request, render_template, flash, redirect, url_for class Block: - def __init__(self, index, timestamp, data, previous_hash): + def __init__(self, index, timestamp, data, previous_hash, nonce=0): self.index = index self.timestamp = timestamp self.data = data self.previous_hash = previous_hash + self.nonce = nonce self.hash = self.calculate_hash() def calculate_hash(self): data_string = json.dumps(self.data, sort_keys=True) - return hashlib.sha256((str(self.index) + self.timestamp + data_string + self.previous_hash).encode()).hexdigest() + hash_data = str(self.index) + self.timestamp + data_string + self.previous_hash + str(self.nonce) + return hashlib.sha256(hash_data.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"Mining block {self.index}: Nonce = {self.nonce}, Hash = {self.hash}") + print(f"Block {self.index} mined successfully!") + class Blockchain: def __init__(self): self.chain = [] + self.difficulty = 4 self.load_chain() def create_genesis_block(self): - return Block(0, "00-00-0000", "Genesis Block", "0") + return Block(0, "0000-00-00 00:00:00", "Genesis Block", "0") def get_latest_block(self): return self.chain[-1] @@ -70,10 +82,13 @@ class Blockchain: if current_block.previous_hash != previous_block.hash: return False + if current_block.hash[:self.difficulty] != '0' * self.difficulty: + return False + return True app = Flask(__name__) -app.secret_key = 'lesecretkey' +app.secret_key = 'lesecretkey' blockchain = Blockchain() CONNECTED_NODES = [] @@ -87,10 +102,11 @@ def upload_image(): image_data = base64.b64encode(file.read()).decode('utf-8') current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") block = Block(len(blockchain.chain), current_time, image_data, blockchain.get_latest_block().hash) + block.mine_block(blockchain.difficulty) blockchain.add_block(block) if blockchain.is_chain_valid(): - flash('Image uploaded successfully', 'success') + flash('Image uploaded successfully.', 'success') else: flash('Corrupted blocks detected. Image upload failed.', 'danger') blockchain.chain.pop() diff --git a/templates/index.html b/templates/index.html index 0713418..ea62266 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,6 +3,17 @@ Memechain +
@@ -19,23 +30,35 @@ {% endif %} {% endwith %} -
-
- - +
+
+

New Block

+ +
+ + +
+ +
+
+ +
+

Mining block...

+
+
- - -
-

Connected Nodes

-
-
- - +
+

Connected Nodes

+ +
+ + +
+ + +
    - - -
      +

      Sync Blockchain
      @@ -64,7 +87,6 @@ blockchainDiv.appendChild(blockDiv); }); }); - fetch('/nodes') .then(response => response.json()) .then(nodes => { @@ -75,7 +97,10 @@ listItem.classList.add('list-group-item'); nodeList.appendChild(listItem); }); - }); + }); + $('form').on('submit', function() { + $('.loading').show(); + }); \ No newline at end of file