Recently, there were a lot of questions about understanding how blockchain
Recently, there were a lot of questions about understanding how blockchain works. There are many individuals who don’t really understand how blockchains function. If you too are one of those countless individuals who couldn’t find a comprehensive guide, then this ‘Blockchain Programming’ guide has some decent knowledge for you.
Let’s Start With The Purpose Behind Blockchain
If you give some time to yourself to do some brainstorming, you will notice that to live in the society, you have to put your trust in a number of central authorities. Also, whatever you have have been going through so far, you had to get your work done with the involvement of a third party. Of course, you have been paying some fixed amount of fee to every third party, be it knowingly or unknowingly.
The problem is the middleman, who is hungry for money and power. His motto has become to get profit by all means in response to support the producers. In order to empower the poor, we always needed middlemen to act ethically, even if it’s not so. Thus, the idea behind blockchain was to replace middleman with an autonomous system.
Since computers don’t do biases, they neither need power nor money. This might have been Satoshi Nakamoto’s thought when he invented bitcoin with blockchain technology in 2008.
Decentralization
To understand this term better, there couldn’t be a better example than the Torrent.
Yes, Torrents use user-to-user technology to share files. The torrent app doesn’t download the file from a single server or computer. Instead, it connects you to the people in its network to find who has the file, and then download it from there.
Decentralization of blockchain is no different thing! It is about connecting to one another through a network and has the same data stored on multiple platforms. So there’s no way you lose your data, and the best thing is that it’s always available to you.
Satoshi Nakamoto adopted this technology making money stored in a decentralized way so that no single entity will have control over it.
To put everything together and get done everything in a secure way, Satoshi created a shared ledger using cryptography and peer-to-peer networks. At the time when anyone sends bitcoins to someone, then a message is signed cryptographically and then broadcasted to the entire network as a proof of the transaction done. That’ updated in the ledger of people in the network.
Blockchain Again
After every 10 minutes, transactions are grouped into a block and linked to the previous blocks. This entire process makes a continuous blockchain. Mining is a process whereby a block is verified. This process involves powerful computers in the network for solving mathematical problems. The first miner or computer that solved the problem gets rewarded in bitcoins made out of thin air.
Once the block is verified and included into the network, it’s engrossed throughout the network. Blockchain was first invented in the process of making the autonomous Bitcoin system that confirmed all the transactions without any human intervention.
A Little Bit About Ethereum And Smart Contracts
Previously, we talked about replacing the need for middlemen with autonomous systems. Well, this is something that can be done through programming. Initially, the system Bitcoin worked upon seemed to be difficult for people to code the autonomous system. Aftermath, Vitalik Buterin built a new cryptocurrency known as Ethereum. It was not just a decentralized cryptocurrency, but a network of computers to host code regarding smart contracts.
In Blockchain Smart Contracts, we can program certain conditions. In case you wish to build a decentralized bookstore, you need to write some instructions to help the authors add new books and send the download link to an ebook once the customer makes a transaction.
Smart contracts not just simply store certain conditions, but they also store data. A smart contract of a decentralized bookstore stores the books in itself.
Understanding the Programming with Smart Contracts on Ethereum
For instance, you wish to build a simple smart contract that not just stores but retrieves the grades of the students. Indeed, the contract will be coded in solidity. But we will still try to draw a better look for you through the words.
In the coding, the first line tells about the compiler, like what is the version of solidity you are using. After that, you define the contract grades.
Then, you need to store two things in the contract, the student names as well as their grades. Hence, you will create an array for storing names of the students and associative array for storing their grades.
Done with that, you will make a way whereby you can send the contract, which is a list of student names. And, you will do that in the constructor.
In Solidity, you would be calling the constructor for just one time. You will pass student names like an argument, that would be stored in the list of student that we already declared beforehand.
Then, you need to write a function for assigning students their grades. Also, you need another function to check whether the student name is valid or not.
The “giveGradeToStudent” function needs two arguments, the name of the students and the grades. The required function checks whether the “validStudent” function stands true or false. In case, it returns false; then the execution is cancelled.
Ultimately, you need to write a function that fetches the grades of students using “getGradeForStudent” function. This function takes student name as an argument and then return the respective grade from the associative array.
Compiling Of Codes
Now, you can be able to deploy the smart contract. Deploying such contracts on a blockchain will cost you gas, which is to reward people who give their computational power to you for a reason. Thus, you need to clarify the amount of gas you wish to allocate. You can assess that using a gas calculator. But you don’t need to pay as you are deploying on a personal blockchain, which is your resource being utilized. You will need to pay when you deploy the contracts on the public Ethereum blockchain.
Many Congos! You have deployed your smart contract.