{"id":205,"date":"2026-05-15T19:24:28","date_gmt":"2026-05-15T19:24:28","guid":{"rendered":"https:\/\/theroyalscode.com\/students\/a_carpenter\/?p=205"},"modified":"2026-05-15T19:24:28","modified_gmt":"2026-05-15T19:24:28","slug":"friday-5-15-2026","status":"publish","type":"post","link":"https:\/\/theroyalscode.com\/students\/a_carpenter\/2026\/05\/15\/friday-5-15-2026\/","title":{"rendered":"Friday 5\/15\/2026"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>This week I worked mostly on a blockchain project.<\/strong> <strong>The concept is to learn more about cryptocurrencies and hashing.<\/strong><br><br><strong>server.py:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime\nimport time\nimport socket\nfrom wallet import Wallet\nfrom block import Blockchain, Block\n\nblockchain = Blockchain()\nace = Wallet()\n\ndef mine_new_blocks(blockchain):\n    last_block = blockchain.get_latest_block()\n    new_index = last_block.index + 1\n    current_time = datetime.now()\n    coins_per_block = 1\n    data = f\"wilmoth +0 coins! orlando -0 coins!\"\n\n    ace.add_balance(coins_per_block)\n\n    new_block = Block(new_index, current_time, data, last_block.hash)\n    blockchain.add_block(new_block)\n\n    mined_hash = new_block.hash\n    print(f\"Mined Hash: {mined_hash}\")\n\n    # Broadcast the new block to all connected peers\n    broadcast_new_block(new_block)\n\npeers = &#91;]\n\ndef broadcast_new_block(block):\n    for peer in peers:\n        send_block_to_peer(peer, block)\n\ndef start_server(host='localhost', port=12345):\n    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server_socket.bind((host, port))\n    server_socket.listen(10)  # Allow multiple connections\n    print(f\"Server listening on {host}:{port}\")\n\n\n\n    while True:\n        client_socket, addr = server_socket.accept()\n        print(f\"Connection from {addr}\")\n        if len(peers) &lt; 5:  # Limit the number of connected peers for simplicity\n            peers.append(client_socket)\n        else:\n            client_socket.close()\n\ndef handle_client(client_socket):\n    data = client_socket.recv(1024).decode()\n\n    if data == \"validate\":\n        is_valid = blockchain.validate_chain()\n        if is_valid:\n            response = \"Chain is valid\"\n        else:\n            response = \"Chain is invalid\"\n        client_socket.sendall(response.encode())\n    elif data.startswith(\"BLOCK\"):\n        block_data = data.split(\" \", 1)&#91;1]\n        received_block = Block.from_string(block_data)\n        if validate_and_add_block(received_block):\n            response = \"Block added successfully\"\n        else:\n            response = \"Invalid block\"\n        client_socket.sendall(response.encode())\n\n    client_socket.close()\n\ndef send_block_to_peer(peer, block):\n    peer.sendall(f\"BLOCK {block.to_string()}\".encode())\n\ndef validate_and_add_block(block):\n    try:\n        for i in range(1, len(blockchain.chain)):\n            blockchain.chain&#91;i].validate_block(blockchain.chain&#91;i - 1])\n        if not blockchain.validate_chain():\n            return False\n        blockchain.add_block(block)\n        return True\n    except Exception as e:\n        print(f\"Error validating block: {e}\")\n        return False\n\nif __name__ == \"__main__\":\n    import threading\n\n    server_thread = threading.Thread(target=start_server, args=())\n    server_thread.start()\n\n    while True:\n        mine_new_blocks(blockchain)\n        time.sleep(1)  # Adjust the sleep time as needed\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>client.py:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>from datetime import datetime<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import time<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import socket<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">from wallet import Wallet<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">from block import Blockchain, Block<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">blockchain = Blockchain()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ace = Wallet()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">def start_client(host=&#8217;localhost&#8217;, port=12345):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; client_socket.connect((host, port))<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; print(f&#8221;Connected to {host}:{port}&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; while True:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; data = input(&#8220;Enter command (validate\/block\/exit): &#8220;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; if data == &#8220;validate&#8221;:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_valid = blockchain.validate_chain()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if is_valid:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = &#8220;Chain is valid&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = &#8220;Chain is invalid&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(response)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; elif data.startswith(&#8220;BLOCK&#8221;):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block_data = data.split(&#8221; &#8220;, 1)[1]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; received_block = Block.from_string(block_data)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if validate_and_add_block(received_block):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = &#8220;Block added successfully&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = &#8220;Invalid block&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(response)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; elif data == &#8220;exit&#8221;:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; client_socket.close()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">def validate_and_add_block(block):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; try:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; for i in range(1, len(blockchain.chain)):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blockchain.chain[i].validate_block(blockchain.chain[i &#8211; 1])<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; if not blockchain.validate_chain():<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return False<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; blockchain.add_block(block)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; return True<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; except Exception as e:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; print(f&#8221;Error validating block: {e}&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; &nbsp; &nbsp; return False<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">if __name__ == &#8220;__main__&#8221;:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; start_client()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>block.py:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import hashlib\nfrom datetime import datetime\nimport json\n\nclass Block:\n    def __init__(self, index, timestamp, data, previous_hash, nonce=0):\n        self.index = index\n        self.timestamp = timestamp\n        self.data = data\n        self.previous_hash = previous_hash\n        self.nonce = nonce\n        self.hash = self.calculate_hash()\n\n    @staticmethod\n    def from_string(block_data):\n        data_dict = json.loads(block_data)\n        \n        return Block(\n            index=data_dict&#91;'index'],\n            timestamp=datetime.strptime(data_dict&#91;'timestamp'], \"%Y-%m-%d %H:%M:%S\"),\n            data=data_dict&#91;'data'],\n            previous_hash=data_dict&#91;'previous_hash'],\n            nonce=data_dict.get('nonce', 0)\n        )\n\n    def calculate_hash(self):\n        return hashlib.sha256(f\"{self.index}{self.timestamp}{self.data}{self.previous_hash}{self.nonce}\".encode()).hexdigest()\n\n    def validate_block(self, previous_block):\n        if self.previous_hash != previous_block.hash:\n            return False\n        if not self.calculate_hash().startswith('0' * 4):\n            return False\n        return True\n\nclass Blockchain:\n    def __init__(self, difficulty=4):\n        self.chain = &#91;self.create_genesis_block()]\n        self.difficulty = difficulty\n\n    def create_genesis_block(self):\n        return Block(0, datetime.now(), \"Genesis_Block\", \"0\")\n\n    def get_latest_block(self):\n        return self.chain&#91;-1]\n\n    def proof_of_work(self, block):\n        nonce = 0\n        while True:\n            block.nonce = nonce\n            if block.calculate_hash().startswith('0' * self.difficulty):\n                return block.hash\n            nonce += 1\n\n    def add_block(self, new_block):\n        new_block.previous_hash = self.get_latest_block().hash\n        new_block.hash = self.proof_of_work(new_block)\n        self.chain.append(new_block)\n\n    def validate_chain(self):\n        for i in range(1, len(self.chain)):\n            block = self.chain&#91;i]\n            previous_block = self.chain&#91;i - 1]\n            if not block.validate_block(previous_block):\n                return False\n        return True\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>wallet.py:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\nimport string\n\nclass Wallet:\n    def __init__(self):\n        self.balance = 0.0\n        self.address = self.generate_address()\n\n    @staticmethod\n    def generate_address():\n        return ''.join(&#91;random.choice(string.ascii_letters + string.digits) for _ in range(32)])\n\n    def add_balance(self, amount):\n        self.balance += amount\n\n    def spend_balance(self, amount):\n        if amount > self.balance:\n            raise ValueError(\"Insufficient balance\")\n        self.balance -= amount<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week I worked mostly on a blockchain project. The concept is to learn more about cryptocurrencies and hashing. server.py: client.py: from datetime import datetime import time import socket from wallet import Wallet from block import Blockchain, Block blockchain = Blockchain() ace = Wallet() def start_client(host=&#8217;localhost&#8217;, port=12345): &nbsp; &nbsp; client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) &nbsp; &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-friday"],"_links":{"self":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":1,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/posts\/205\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theroyalscode.com\/students\/a_carpenter\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}