Disclaimer: chiccocoin is not a true cryptocurrency and we do not sell it , this article is for recreational / educational purposes only
Every day on our feeds we find news about new cryptocurrency or someone who says that they are a big bubble that will soon explode and of which only the blockchain will remain. But, what is the blockchain?
By definition :
Is a continuously growing list of records, called blocks, which are linked and secured using cryptography.
So, the blockchain is an immutable, sequential chain of records called Blocks. Each block can contain transactions, files or any data you like. The important thing is that they’re chained together using hashes .
Blockchains are secure by design and are an example of a distributed computing system with high Byzantine fault tolerance . This makes blockchains potentially suitable for the recording of events, medical records, and other records management activities, such as identity management, transaction processing or voting.
Table of Contents
How does a blockchain technically work?
Understanding Blockchains reading article or tutorials isn’t easy. Wandering among the many online guides I found this article by Daniel van Flymen and my curiosity to understand how it really worked has grown so much to try to create a blockchain using NodeJS.
1. Create a logo
The first step to creating a new project is to create the logo. It makes everything real. To do this I used Squarespace logo creator and this is the result:
Now we can really start with our project.
2. Start with Express
To facilitate the creation of the APIs to interact with the blockchain I started the project directly with ExpressJS using the package npm express-generator
npm install -g express-generator
express ./chiccocoin
cd chiccocoin
yarn
You can find all the code online on this repo:
thecreazy/chiccocoin
3. Create the blockchain
Now we’re going to create our blockchain.Rereading the definition of blockchain we can summarize that the absolutely necessary functions are:
- newBlock: Function that creates a new block
- newTransaction: Function that creates a transaction and queues it to be saved the next time a block is created
- hash: Function that encrypts a block
- lastBlock: Function that will return the last block of the chain
So we can create our blockchain.js file with this structure:
The constructor of our class will create two important variables, chain, and currenttransaction. Chain will contain, in order, all our block. Currenttransactions will contain all the transaction that will be added to a block at the next mining
3.1 Create a block
But what is a block? The representation that will use of a block is a javascript object that will contain:
- Index
- Timestamp
- List of transactions
- Proof
- Hash of the previous Block
In this way the idea of blockchain becomes already a little clearer. Is a set of sequential blocks ( index ) linked to the previous one and secured using cryptography ( previoushash _). The previoushash inside the block it’s the crux of the whole blockchain. It allows us to guarantee the safety and immutability of the whole chain. If an attacker could modify a block of a chain all the hashes immediately after that block would be wrong. Obviously it could try to recalculate the hashes of the whole chain and that’s why more blocks there are more the blockchain is safe. Moreover there is an (identical) copy of the blockchain in every node ( _instance of NodeJS in this case ) this makes it almost impossible for a hacker to be able to modify all the copies at the same time.
So create a new block is very simple in our case. We just need to push a new object to the chain. The function will receive the proof (then we’ll talk about what it is) and the previous block hash and will return the block. The function also will add to the block all the transactions that have not yet been saved and clean up our variable current_transactions
3.1.1 Hash function
The hash function that uses for our Chiccocoin is a trivial SHA256 but you can use the hash function that you prefer. The important thing is to do the hash of the previous block, to do this we will do the hash of the serialized object
3.2 Create a transaction
The function to add a new transaction is very trivial. Is to add to the currenttransaction array the transaction. A transaction is an object formed of _sender , recipient and amount . It will be the mining function of storing transactions within a block. For utility we will make sure that the function returns to us the index of the block on which it will be saved
4. The Proof of Work
In general, the proof of work is a function or a protocol invented and used to deter denial of service attacks, but the blockchain used it to determinate how new Blocks are created or mined on themselves . The goal of a POW is to discover a number which solves a problem. This number must be difficult to find but easy to verify, like the calculation of prime numbers, more we go on the more difficult it will be to find one, but the effort to understand if it is or not will always be very banal.
To mining a Chiccocoin we decide the create a c4ff3. Our POW will be this:
Find a number p that when hashed with the previous block’s solution a hash which starts with c4ff3 is produced.
An example:
c4ff3e9373e...5e3600155e860
Let’s code it. The functions that will be necessary are two:
- validProof: given the previous POW and a p number checks if the solution to the problem is correct
- proofOfWork: cycle until a solution is found
5. Serve over API
Having started the project with expressjs serving our blockchain via API will be very simple.
We’ll create three API:
- /transactions/new to create a new transaction to a block
- /mine to tell our server to mine a new block.
- /chain to return the full Blockchain.
I created a Chiccocoin support class, inside /middleware/chiccocoin.js
, which contains all the necessary middleware for our APIs and instantiates a new blockchain
5.1 The Chain Endopoint
This endpoint will be very very simple. Just will return the chain array stored inside the blockchain
5.2 The Transactions Endpoint
The transaction endpoint will check the data passed and will call the newTransaction function of the blockchain. In the future we could use this middleware to check if actually sender and recipient are correct and/or the transaction can be done. We will use the information returned by the newTransaction function to inform the user on which block the transaction will be saved.
5.3 The Mining Endpoint
Our mining endpoint is where the chiccocoin becomes reality. It has to do four things:
- Calculate the Proof of Work
- Reward the miner (us) by adding a transaction granting us 1 coin
- Add any pending transactions
- Forge the new Block by adding it to the chain
We can test all our API’s via curl or via Postman. I added the postman_collection.json inside the repository in order to simplify you the use of postman.
Conclusions
The idea behind the blockchain is nothing new or complicated as we have seen. Banally developers use a blockchain every day without realizing it ( git ). It is a very old concept that, however, brought back to life under a new point of view is changing the way of thinking about the economy, creating bubbles (in my opinion) and new possibilities for a “safer” economy from the point of view of common knowledge. Having the ability to control when and how a transaction took place is something that is going quietly but has enormous potential.
What we have created is a simple blockchain that works with only one node and on which only one can make it (there are cryptocurrencies based on this). Surely the management of the multi-node and of all the problems around this topic are equally interesting. Maybe we’ll do a second article about this, but put a star in the repository for more updates.
thecreazy/chiccocoin
I hope this article has pushed you to look beyond what is bitcoin trading and to have you intrigued over what you can really do with the blockchain.
Thanks and stay tuned 🙂
This content was originally published here.