Coding a Blockchain with AI. Progress so far

in #codinglast month

In my previous post on d.buzz, here I posted that I would be coding a functional Blockchain with AI and update on my progress.
I wondered if the AI could do the coding job considering my prior experience with it, plus my coming across an article that seems to suggest that it can't be done. Well I understand we are still in the early stage of the AI thing, but I think what it's capable of at the stage especially in the area of images and texts generation is pretty nice. If AI can create brand new images or texts that are pretty detailed, based on our instructions why not code generation? From my experience with AI Image generation, I would say the accuracy of the generated images depends on the humans telling the AI what to generate. If your instruction is full of garbage, it generates garbage, if it's good and accurate, it generate something good and accurate. I believe that can be replicated in the area of computer programming. The AI needs to be fed with the most accurate instruction to generate the most accurate and functional codes.

So, i decided to test this out to see if AI can create a functional Blockchain based on my instruction. Note that this is about coding a fully functional Multi-chain Network that can stand the test of time and used by many people. Ofcourse, I would have to sure everything is right by probably involving a qualified human reviewer/reviewers to review the code and tell me what they think.
So, I started this project by feeding the below instruction to the AI. The instruction is really long one, but I shorten it for certain reasons:

Part of the instruction:
"Firstly let's start by building the framework of the Blockchain network by coding a modular Blockchain that'll be made of multiple interconnected Blockchains/Networks. The Network starts with one Blockchain, which we call "Genesis chain or genesis Blockchain", and eventually expand into multiple Blockchains.
Each Blockchain within the Multi-chain Network is run via its Nodes by 2 or more globally spread participants to form a single Network. One Blockchain forms a network while multiple Blockchains form multiple Networks. All the Blockchains put together is called a Super Blockchain network or Supernetwork. Unlimited number of Blockchains can be created by participants on the Supernetwork.
A new Blockchain/Network can be created from the genesis Blockchain in the Network, Better still, whenever a new Chain is needed, a duplicate is created from the genesis Chain and it becomes a new network within the multi-chain Network. The dublicated chain can be customized however the creator wants as long as allowed.
The users of the Supernetwork can participate in peer-to-peer transactions by sending and receiving coins, assets, values or information via the network.
All the Chains/Networks can only function safely together in harmony and become single super-chain Network if they are compatible enough and follow thesame rules/principles... "

Code generated by the AI based on the instruction:
import hashlib import datetime as date import time class Block: def init(self, index, timestamp, data, previous_hash): self.index = index self.timestamp = timestamp self.data = data self.previous_hash = previous_hash self.hash = self.calculate_hash() def calculate_hash(self): return hashlib.sha256((str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)).encode()).hexdigest() class Blockchain: def init(self, name, max_data_size, block_time): self.name = name self.max_data_size = max_data_size self.block_time = block_time self.blocks = [] self.locked = False self.super_node = None def add_block(self, new_block): if self.locked: print(f"This chain '{self.name}' is locked. No more data can be added.") return new_block.previous_hash = self.get_latest_block().hash new_block.hash = new_block.calculate_hash() self.blocks.append(new_block) def get_latest_block(self): if len(self.blocks) == 0: return None return self.blocks[-1] def lock_chain(self): self.locked = True print(f"Chain '{self.name}' is locked. No more data can be added.") def unplug_chain(self): if self.super_node: self.super_node.remove_chain(self) print(f"Chain '{self.name}' has been unplugged.") else: print(f"Chain '{self.name}' is not connected to any super node.") class SuperNode: def init(self): self.chains = [] def add_chain(self, chain): self.chains.append(chain) chain.super_node = self print(f"Chain '{chain.name}' added to the super node.") def remove_chain(self, chain): if chain in self.chains: self.chains.remove(chain) chain.super_node = None print(f"Chain '{chain.name}' removed from the super node.") else: print(f"Chain '{chain.name}' is not connected to this super node.") class Node: def init(self, node_type, data_capacity): self.node_type = node_type self.data_capacity = data_capacity self.reputation = 0 def verify_transaction(self, transaction): # Placeholder for transaction verification return True def award_reputation(self, points, reason): self.reputation += points print(f"{self.node_type} node awarded {points} reputation points for {reason}.") def penalize_reputation(self, points, reason): self.reputation -= points print(f"{self.node_type} node penalized {points} reputation points for {reason}.") def process_transactions(self, transactions, total_block_data): block_reward = 0.3472 percentage = (len(transactions) / total_block_data) * 100 reward = percentage * block_reward print(f"{self.node_type} node processed {len(transactions)} transactions and earned {reward} coins.") class BasicSmartContract: def init(self, sender_address, recipient_address, amount, condition, expiration_date): self.sender_address = sender_address self.recipient_address = recipient_address self.amount = amount self.condition = condition self.expiration_date = expiration_date self.funds_locked = True def fulfill_condition(self): # Check if the condition is met if self.condition: self.release_funds() else: print("Contract condition not met. Funds remain locked.") def release_funds(self): # Release the locked funds to the recipient print(f"Funds of {self.amount} coins released to {self.recipient_address}.") self.funds_locked = False Example usage: super_node = SuperNode() chain1 = Blockchain("Chain1", max_data_size=100, block_time=10) chain2 = Blockchain("Chain2", max_data_size=100, block_time=10) super_node.add_chain(chain1) super_node.add_chain(chain2) Creating different types of nodes super_node1 = Node(node_type="Super-Node", data_capacity="Unlimited") multi_node1 = Node(node_type="Multi-Node", data_capacity="Unlimited") big_node1 = Node(node_type="Big-Node", data_capacity="Unlimited") small_node1 = Node(node_type="Small-Node", data_capacity="100GB") micro_node1 = Node(node_type="Micro-Node", data_capacity="10GB") Process transactions and calculate rewards transactions = [transaction1, transaction2, transaction3] # Placeholder for transactions total_block_data = 2 # Placeholder for total block data super_node1.process_transactions(transactions, total_block_data) Create a basic smart contract sender_address = "SenderAddress123" recipient_address = "RecipientAddress456" amount = 52 condition = True # Placeholder for the condition to be met expiration_date = "2024-03-20" # Placeholder for the expiration date basic_contract = BasicSmartContract(sender_address, recipient_address, amount, condition, expiration_date) Fulfill the condition of the contract basic_contract.fulfill_condition() 0 At first, I noticed the AI couldn't handle the whole instruction at once, even from its response, then I fed it bit by bit and it seems to like that. We ended up creating what I would regard as the framework of the project with some features here and there. It's bit like building a house without the ceiling, wiring, plaster, doors and windows to close and open etc. Ofcourse, I do not expect this to be perfect as I have lots of editing to do plus things to include in the future to have a finish product that I hope will be great. My Previous post: Coding a fully functional Multi-chain Network using AI. Can it be done? https://hive.blog/hive-193084/@manuel-nk/31taa5ex7cdul8yrp2xzln **My other AI related post: ** My conversation with an AI on fear of destruction & others https://hive.blog/ai/@manuel-nk/my-conversation-with-an-ai

Sort:  

Good to see you were able to post this!
Looking forward for the next one(s)!

Congratulations @manuel-nk! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You published more than 10 posts.
Your next target is to reach 20 posts.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP