

Creating a blockchain with less than 100 code lines
source link: https://medium.com/dataseries/creating-a-blockchain-with-less-100-code-lines-7168849d687d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating a blockchain with less than 100 code lines
The basic concept of blockchain is very simple: a distributed database that maintains a continuously growing list of ordered records.
“Blockchain” is a term normally associated with Bitcoin and/or Ethereum, but the blockchain is more than that. Blockchain is the technology behind these and behind any other cryptocurrency.
There are several uses for blockchain. For example, games (CryptoKitties) or also blockchain+IOT (Internet of things), and this is just the start for the technology.


Blockchain is, like the name implies, one chain of blocks, so, we have the first class, the Block.

In this stage my block has the following attributes:
- index — the block index the position in the chain
- timestamp — date and time that the block was added in Blockchain
- data — the data value, in other words, what you want to save
- previous hash — the hash of the block index -1
- hash — the hash of the block
If you don’t know what is hash, please check my last post here.
You will see some things interesting in the picture and here I will explain a little bit:
- let more OOP the function isValid is for each block respond if he is valid
- The constructor defines all the things in the block
- the function “update” is to update the dict when reading from a file, this is for saving the data for future
- calculating the hash for previously saved files, to convert always to the same encode, because, differents encodes have different characters and different characters produce different hashes.
This is a chain valid, if the block was changed, the current block will know, and make yourself invalid, and if any previous block was changed, the chain will know, and all the chain will be invalid. That is the concept that makes the data save in Blockchain immutable.
So looking in our second class, Blockchain, it looks like:

Therefore, the blockchain class create the blocks and look for any problems in the chain. Plus, this class is responsible for saving in a simple JSON file and read from him. Our first version blockchain is ready!! \o/
All the code is below, you can execute and see the output
In this version of our blockchain, we not implemented the Proof of Work. The idea is to first create the blockchain and guaranteed the chain’s integration. That will be my next step.
I created a project that you can follow in GitHub, I will increment more and more my blockchain. If you have interesting just follow me! I will write some posts about the process.
P.S: I am not an expert in the blockchain, so any problem, code fix, or tips, I gladly welcome you to comment below and will help people too. If you wish, you can talk to me privately on the LinkedIn.
See you soon!
Recommend
-
168
This tutorial is adapted from this excellent post about writing a basic blockchain using Javascript. We’ve ported it over to Go and added some extra goodies like viewing your blockchain in a web…
-
20
lowleveldesign.org Software tracing, debugging, and security While working on a new version of wtrace
-
7
How I Created a Landing page in Less than 100 lines of CodeJanuary 21st 2021 new story
-
4
In recent years, cryptocurrencies and blockchains are two uprising fields, so today, I will share my way of creating a blockchain in Javascript in just 60 lines of codes. I'm still new to the field, if I'm wrong with something, please remind...
-
9
Creating a blockchain in 60 lines of Python When I read the document written by Phu Minh...
-
11
Introduction Have you ever considered utilizing a computer language to create a rough sketch of an image? I'll show you how to make a sketch of an image without having any programming skills in this article. Hold on to...
-
6
T-Wand: Beat Lucene in Less Than 600 Lines of Code ...
-
7
Writing a simple 16 bit VM in less than 125 lines of C December 1, 2021 This tutorial is intended for C beginners who want to do some coding p...
-
10
Wordle in less than 50 lines of Bash · GitHub Instantly share code, notes, and snippets. Wordle in less than 50 lines of Bash ...
-
7
Disclaimer: What the title means by 'in less than 10 lines of code' is referring to the number of lines of JavaScript code. You know those annoying notifications popping up at the least...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK