0

How to build a simple blockchain in python

The blockchain that can hold node between sender (private) and receiver (public)

27th Oct 2021, 8:40 PM
Yasin Coder
Yasin Coder - avatar
5 odpowiedzi
+ 4
Coding Cat It is a flat list ;). No need for additional dimensions. That is essentially a blockchain. Linked Lists might not always be used in the real-world though, instead, blocks will be stored in files on your device. So you aren't going to be holding the entire blockchain at once in your RAM (main memory). Although they could then be read from the files and then placed in a linked list if needed. The reason for the linked list was mostly to understand that they are connected. What makes the blockchain special is that the information is 'chained' together. They store the previous hash, so that if anything changes, it can be known. If you modify the previous hash of the next node, then the node after that one will not match. The purpose of the NodeHeight in my example is to act as an index and so you know the order at which blocks have been added. I was more or less trying to show how it relates to popular blockchains, since you will see 'block height' commonly used with cryptocurrencies. There are a couple reasons for this, the blocks are typically stored as files and so you can know the order at which they are stored and retrieve them by index, as well as getting the size of the list (the block height of the last block) without actually storing its size. However, the most important benefit is that each block is forced to have unique content. That is, you cannot have two blocks with the same block height, but all the other information can be the same as some connected block. This reduces the likelihood of the hashes of different blocks being the same. Edit: Edited some of the comment. Switching to mobile to make my reply through a ping had removed parts of the message that I have re-added.
28th Oct 2021, 4:21 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Thank you Rrestoring faith I've now tryed to build something like that. A very simple version. But it seems to be working. https://code.sololearn.com/cQqU0PRHODcb/?ref=app
29th Oct 2021, 10:21 AM
Coding Cat
Coding Cat - avatar
+ 2
I will only follow this post. Because if someone know how blockchain becomes simple, I will think about how to make it in python.
27th Oct 2021, 9:12 PM
Coding Cat
Coding Cat - avatar
+ 2
Heyo, No worries. Despite common belief, Blockchain is a simpler construct than you may expect. Prerequisites: Hash functions, linked lists Consider how the data-structure works. The easiest way to think of a blockchain is to call it an append-only linked-list that uses hashing to assure data-integrity. I will use the term "node" to refer to a block. Also recall, a blockchain and a cryptocurrency are not the same thing. Cryptocurrencies are an application of blockchain, certain aspects in cryptocurrency are not needed. For a simple blockchain, it will not be distributed. So we will also not use timestamps or "proof-of-x" concepts. Take a list of connected nodes. A node is represented with a hash of all of its contents, such that, if any content changes, so does this hash. Each node will then store the previous nodes hash, this way, if any data changes within a node, it can easily be detected since the previous hash of the next node will not be equal. Let me explain through a code example. // In Java (You can Convert it to python) class Node // "Block" { String blockHash; // >Important!< int nodeHeight; int someValue; String someInfo; final String prevHash; // >Important!< // Since we used linked list Node nextNode; Node prevNode; } Now our LinkedList class LinkedList // "BlockChain" // Try to Implement some methods, add to it only by appending to the end of the list { Node head; Node tail; } All of our blocks are connected. What makes the blockchain special? When we create a block, we will be creating hashes like so: prevHash = valueOf (myBlock.prevNode.blockHash) // Set once blockHash = hash (nodeHeight, someValue, someInfo, prevHash) // Changes every time something is modified Now what? Well If any content changes we can re-calculate the hash and compare it with the next blocks. Does hash (blocks info) = nextblock's prevHash? NO = Ignore whatever you are trying to do. The data is 'chained' ;)
27th Oct 2021, 10:36 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Rrestoring faith could you explain, why we use NodeHeight here? It looks like a flat list. Doesn't have all nodes the same level?
28th Oct 2021, 2:03 PM
Coding Cat
Coding Cat - avatar