Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the beginners' knowledge points of blockchain programming?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you what are the relevant knowledge points for beginners in blockchain programming. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

The purpose of the blockchain

Roopa lives in a remote area of Delhi. The Indian government allocates a small amount of her food resources every month. Because she belongs to BPL (below the poverty line) category. The government allocates these food resources through intermediaries. Only 1/3 of food resources are available for people to use, such as Roopa, and the rest are sold by intermediaries for a profit.

Sara writes novels, which she publishes on Amazon. She is frustrated because Amazon takes 50% of its sales as a commission. This is unfair because she has devoted herself to writing and marketing efforts.

The problem is that intermediaries crave power and money. Their motto has become "profit at all costs", and we need intermediaries to act morally in order to support producers and empower the poor. This is almost impossible, but what if we can replace intermediaries with autonomous systems?

Because computers are unbiased, they need neither money nor power. This may be Satoshi Namakato's idea when he invented Bitcoin using blockchain technology in 2008.

How was Blockchain invented?

With the passage of time, the currency gradually developed, and each development reduced the production cost and made the transaction more convenient. The production cost of gold coins is very high. The invention of paper money solved this problem. However, after the invention of the computer and the Internet, people have found a more convenient and faster way to trade.

In order to safely maintain our lifetime income and promote digital transactions, we need an intermediary (bank). This makes the banks so strong that they can charge high fees for our withdrawals / transactions, sell our private information, etc.

Banks' thirst for money led to the financial crisis of 2008. Banks fail to respect customers' privacy. Their weak security system led to digital fraud.

The next evolution of currency must solve the following problems.

It should not be stored in a central entity.

It needs to be highly secure.

It should ensure privacy.

Since legal tender is controlled by the government, Satoshi has no choice but to invent a new currency (Bitcoin). He solved these problems with the help of peer-to-peer networks and cryptography.

Decentralization and power sharing

Torrent uses peer-to-peer technology to share files. Instead of downloading files from a central server or a single computer, torrent applications connect to people on their network to find out who owns the file and download it from their computers.

You can get files from different computers around the world. If someone on the network leaves, your download will not be affected because there are others who can share the file.

Satoshi uses this technology because it stores money in a decentralized way. No single entity can control it.

Encrypt

In cryptography, people can digitally sign messages. To do this, we need three things: the public key, the private key, and the message.

The public and private keys are a set of long characters connected mathematically. The public key is as public as your user name, and the private key is as secret as your password.

The message is the information you want to authorize, such as "I authorize you to pay $100to John".

If you use public key, private key and message input algorithm. The encryption algorithm will produce a signature. This is the only other set of characters in the message.

Public Key-BEGIN EC PUBLIC KEY- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE50uE+YSxqDgMkFByhpcgTVqXCqHOh78Ljt1z0jklDff/WV7xo+U6o3REBtK/C0/LM+Ef3FB3wR9aXMGNMLb9EA==-END EC PUBLIC KEY-Private Key-BEGIN EC PRIVATE KEY- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgwqIsXl9FqsgrzMdxaxI6flBwWIev0Z7i+WF4j8BGnrKhRANCAATnS4T5hLGoOAyQUHKGlyBNWpcKoc6HrwuO3XPSOSUN9/9ZXvGj5TqjdEQG0r8LT8sz4R/cUHfBH1pcwY0wtv0Q-END EC PRIVATE KEY-MessageHello WorldSignatureB0A9A4F641D3A2E3A65576B7311DCD62ABE78BBF4D3F5FE856598508E24FCB2E6F0277C2F8D57E9E2E108B7C493986E783F5316B8046597019951669B4EE6922

To verify the message, you must enter the public key, message, and signature. The encryption algorithm verifies that the message is signed by the owner of the public key.

It will take 1000 years to crack the encryption algorithm. This cannot be done faster because of our computational limitations. Future quantum computers may challenge this. However, the Bitcoin system can be upgraded to ensure security.

Satoshi has added cryptography to his system to help people authorize bitcoin transactions from their wallets.

Privacy

You register with Bitcoin by generating a wallet (public key / private key). The system will not collect email ID, full name and other information. Therefore, unless you publish your public key, you will be anonymous.

Put it together Satoshi uses encryption and peer-to-peer networks to build a shared ledger. When someone sends a bitcoin to someone, it signs a message in encrypted form and broadcasts it to everyone on the network. They update their ledger so that everyone on the network knows who owns what.

Blockchain

Every ten minutes, the transaction is grouped into a block and linked back to the previous block. This process produces a continuous chain of blocks. Mining is the process of identifying blocks, which involves computers in the network to solve mathematical problems. The first computer / miner to solve the problem was rewarded with bitcoin made out of thin air.

Once the chunk is confirmed and added to the network, it will be replicated throughout the network. Blockchain was invented in the process of making its own bitcoin system, which confirms transactions without human intervention. If you are looking for a simplified description of the blockchain, here is a story.

Brief introduction of Ethernet Square and Intelligent contract

Earlier we talked about replacing intermediaries with autonomous systems. This can be done programmatically. Bitcoin's system makes it difficult for people to encode autonomous systems.

As a result, Vitalik Buterin established a new cryptocurrency called Ethernet Fang. It is not only a decentralized cryptocurrency, but also a computer network that can host code in the form of intelligent contracts.

In a smart contract, we can make conditions. If you want to build a decentralized bookstore. You write instructions to help the author add a new book and send the download link to the ebook after the customer has made the transaction.

Smart contracts store not only conditions, but also data. The smart contract of the decentralized bookstore itself stores book lists, purchases, etc.

However, we should acknowledge the limitations of smart contracts. Some systems require manual support and cannot be handled by the computer. Implementing smart contracts in the real world is not easy. Once a published smart contract cannot be changed, stupid mistakes can be costly.

Write a smart contract on Ethernet Square

We will build a simple intelligent contract to store and retrieve students' scores. We will write the contract in a reasonable way. This is github repo.

Pragma solidity ^ 0.4.18 position contract Grades {}

The first line tells the compiler which version of solidity we are using. Then we define the contract level.

We need to store two items in the contract, the student's name and grade. Therefore, we will create an array to store the students' names and an associative array to store their scores.

Pragma solidity ^ 0.4.18 X contract Grades {mapping (bytes32 = > string) public grades;bytes32 [] public studentList;}

Now, we will create a way to send contracts, that is, a list of student names. We will do this in the constructor.

Pragma solidity ^ 0.4.18 X contract Grades {mapping (bytes32 = > string) public grades;bytes32 [] public studentList;function Grades (bytes32 [] studentNames) public {studentList = studentNames;}}

In Solidity, we only call the constructor once. We pass the student name as a parameter, which will be stored in the studentList array we declared earlier.

Now, we need to write a function to assign students their grades. We also need another function to check whether the student is effective.

Pragma solidity ^ 0.4.18 X contract Grades {mapping (bytes32 = > string) public grades;bytes32 [] public studentList;function Grades (bytes32 [] studentNames) public {studentList = studentNames;} function giveGradeToStudent (bytes32 student, string grade) public {require (validStudent (student)); grades [student] = grade;} function validStudent (bytes32 student) view public returns (bool) {for (uint I = 0; I)

< studentList.length; i++) {if (studentList[i] == student) {return true;}}return false;}} giveGradeToStudent函数有两个参数,学生姓名和成绩。require函数检查validStudent函数是返回true还是false。如果返回false,则取消执行。 最后,我们需要编写一个函数来获取学生的成绩。getGradeForStudent函数将学生姓名作为参数,从关联数组中返回相应的成绩。 pragma solidity ^0.4.18;contract Grades{mapping (bytes32 =>

String) public grades;bytes32 [] public studentList;function Grades (bytes32 [] studentNames) public {studentList = studentNames;} function giveGradeToStudent (bytes32 student, string grade) public {require (validStudent (student)); grades [student] = grade;} function validStudent (bytes32 student) view public returns (bool) {for (uint I = 0; I < studentList.length; iota +) {if (studentList [I] = student) {return true;} return false;} function getGradeForStudent (bytes32 student) view public returns (string) {require (validStudent (validStudent)); student [validStudent] }}

For the purposes of this tutorial, you can deploy it on a personal block chain. You can create it with Ganache. Here are the commands to install and run ganache.

Npm install ganache-cli web3@0.20.3 solcnode_modules/.bin/ganache-cli

Keep ganache running and we will deploy our smart contract on the new terminal. Please save the smart contract as Grades.sol.

Let's compile the code.

Nodecode = fs.readFileSync ('Grades.sol'). ToString () solc = require (' solc') compiledCode = solc.compile (code)

Now let's deploy the smart contract. Deploying a contract on a blockchain costs your gas to reward those who rent computing power from you. So we have to specify the amount of gas you are willing to allocate. You can use the gas calculator to estimate. However, you don't have to pay now because you are using a personal blockchain for deployment, which is the resource you are using. When you deploy a contract on a public etheric block chain, you must pay.

Web3 = require ('web3') web3 = new Web3 (new Web3.providers.HttpProvider ("http://localhost:8545"));abiDefinition = JSON.parse (compiledCode.contracts [': Grades'] .interface) GradesContract = web3.eth.contract (abiDefinition) byteCode = compiledCode.contracts [': Grades']. BytecodedeployedContract = GradesContract.new (['John','James'], {data: byteCode, from: web3.eth.accounts [0], gas: 4700000})

Now let's call our function to provide our student John with a grade A +. Later, we will use the getGradeForStudent function to check if it has been updated.

DeployedContract.giveGradeToStudent ('John',' web3.eth.accounts, {from: web3.eth.accounts [0]}) deployedContract.getGradeForStudent.call ('John')'

Congratulations, you have deployed a smart contract.

These are all the contents of this article entitled "what are the basic knowledge points for beginners in blockchain programming?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report