Blockchain as a data structure

Hello, Habr!



Today we would like to outline a new topic for discussion, briefly examining the blockchain from the point of view of computer science - as one of the data structures. Recently, blockchain has been increasingly used outside the cryptocurrency segment , and this trend certainly deserves attention. Let's talk about it!



Blockchain technology can be explained in many different ways. Until recently, blockchain was viewed primarily in terms of cryptocurrencies. Bitcoin is the first blockchain association that many of us have. But storing cryptocurrency transactions is just one of many blockchain use cases. In this article, we will step back from this point of view and consider blockchain in the more general context of computer science.



Blockchain is a data structure



A data structure is a way of storing, organizing, and managing data. The data structure allows you to access this data, add it, modify and search for the data contained in this structure. The most common and basic data structures include arrays and linked lists .



Array



The array contains a number of enumerated elements. These can be numbers, letters, words, or even whole files. Thanks to the indices, you can refer to every single element in the array. Thus, if you want to change an entry in the array and you know where it is, you can instantly access it .







One-dimensional array with six elements



Linked Lists



Nodes are data items included in a linked list. The node contains at least one data object and a pointer to the next item. The function of a pointer is to tell the computer where the next item in a given list is.



If you look at the first item in the list and want to refer to the second, then look at the pointer that will take you to the next node. It is easier to add data to a linked list than to an array, since it needs to be expanded by one node, and adding data to an array will increase the number of elements in this array. But when working with linked lists, you don't have instant access to the data.



If you are looking for a specific data item in a linked list, you first look at the first node in this list, its head... If this is not the element you were looking for, then you follow the pointer that takes you to the next node. If this node does not contain the data that you are looking for, then you continue to follow the links from node to node until you find the data you need.







Three-node linked list



Blockchain



In the context of data structures, blockchain most closely resembles a linked list. Also, in the blockchain, data is divided into containers called blocks . Blocks are quite similar to linked list nodes . Each block contains a link that is a hash of the previous block. It acts as a link to the previous block and helps maintain order in the block chain.







The key difference between blockchain and linked list is that every link on the blockchain is cryptographically secure. You may have heard the term "append only" applied to the blockchain. It means that you can only add new data to the blockchain by completing the chain from the front. The validity of protected links is constantly checked. If it was possible to insert a malicious block into the middle of the blockchain, for example, between blocks 1 and 3 in the diagram below, then it would be possible to put a link to the previous block 1, but not to the following block 3.







Each new block is built on top of the existing one, and this procedure is usually called confirmation. The older the block, the more confirmations it will have. Each confirmation makes it difficult to tamper with the data in the block. In the following diagram, Block 2 has one confirmation. To fake the data in block 2, you will have to recreate one valid block containing a new valid link. After each next confirmation, you will have to recreate another block. Thus, the older the block, the higher the confidence that no changes will be made to this block.



References between blocks depend not only on the order of the blocks, but also on what data is contained in each block. It is not possible to add or remove data from a block in the blockchain. It is on this property that the confidence is based that nothing will happen to the data placed in the blockchain. Naturally, any fake is obvious in the blockchain data structure. Any change made to the data breaks links to all subsequent blocks.



While it is impossible to delete or change data on the blockchain, it is easy to add data to a new block attached to the chain. For example, a new transaction can be added to the cryptocurrency blockchain. The transaction is easy to verify because all previous transactions recorded on the network are immutable. When an amount of X is required from address Y, the balance of this address must have an amount of at least X.



Cryptocurrencies are just a particular application of blockchain technology. Blockchain is rapidly becoming an increasingly viable option for supply chain management, fleet management and other purposes.



Conclusion



Blockchain is a data storage method applicable in computer science. The elements of the blockchain - its blocks - are cryptographically linked to each other. It is not possible to change the data after it has been written to the block. This is the value of the blockchain. It is an immutable registry in which you can safely store data while operating in an untrusted environment.



I would like to end this article with a tweet containing the quintessence of the properties of blockchain technology and illustrating its place among other data structures.



, , .



, , . , – — @NickSzabo4



All Articles