Blockchain Development with PHP

Tram Ho

2021 is sure to be a boom year for Blockchain, when family members make Blockchain, NFT games, Token…. So what if Blockchain is developed on PHP language?

What is Blockchain?

Blockchain (also known as the ledger) is a database system that allows the storage and transmission of information blocks (blocks). They are linked together by encryption. These information blocks operate independently and can expand over time. They are managed by system participants and not through an intermediary. That is, when a block of information is written to the Blockchain system, there is no way to change it. More can only be added when everyone agrees.

Create a block

We will initialize a class called Block, this class will do the job of encrypting all input data into a key chain.

Next we create a Blockchain class

Here, I will create a block when there is 1 data passed in. If the data is a transaction hunghd send money to hunghd2 , at this time I will create a new Block. This block is the first block so index = 0 , previousHash = 'the first'.And when there is more transaction hunghd2 sent to hunghd3 , the next block will have previousHash = hash of the previous block. Since then, every new transaction added will create a new block connecting with the previous ones. From there we have a basic blockchain. This is the resulting code when I run the block:

Check if the chain is changed or not

We have created a chain above, but now what if the hacker wants to change any block in the chain? If it was so easy to change, the block chain wouldn’t be as popular as it is now. To prevent that, I’ll have to check the chain’s validity as follows previousHash === hash before.

Now I will try to change 1 block (change data or hash ) in the chain, the result will be as follows:

Here, the hash of the 2nd block has been changed, but the 3rd block has saved the hash of the 2nd block before the change. So the test result is false

Proof-of-work

But real, hash and previousHash when people can change the data 1 block and then change previousHash and hash of the following blocks is still creating a valid chain and we also want the users to agree on a unique history of the chain. And proof-of-work was born to solve this problem.

If you want to modify a previous block, you will have to re-mine all the blocks after it. It requires scanning for a value that starts with a certain number of zeroes when hashed. The value is called the  nonce value, the number of leading 0 bits is called  difficulty. By increasing the difficulty of mining, it means that mining will become harder and harder. We can make this system by creating mine . method

Now, my Block class will finally code like this:

In the Blockchain class also need to add code like this:

Ok!! Now let’s try to run it

As a result, we will have blocks with numbers nonce as desired. To increase the difficulty to 5 zeros, I just need to change difficulty = 5.

However, this article is basic, so we will use proof-of-work. In fact, this technology is outdated, slow, resource-intensive and harmful to the environment, so today’s platforms often use proof-of-stake.

This is an article based on my personal research, so there are many mistakes I hope everyone can contribute!!!

Share the news now