Editor’s note: this paper Qtum Howard leaf times macro lecturer, in organized by the nuggets technology community, etheric lane community foundation, fang lovers and ConsenSys co-host of the etheric fang “developers start guide | Jeth – the first phase of the Beijing games” sharing activities. Jeth’s series of offline activities centered on the theme of Ethereum technology development. Every issue of Jeth will invite excellent technical teams and engineers in the field of Taifang development to share technical dry goods offline. It aims to provide offline technology exchange and interaction opportunities for developers to help them grow.
Miss Howard: Thank you
Share sort the portal
The etheric fang intelligent contract + DApp from entry to online: practical guide from the front end engineer – Wang Shijun | Jeth first issue
Explanation ERC20 tokens, and the raise – Xiong Libing | Jeth first issue
Howard is the author of Deep Dive Into Ethereum Virtual Machine, an in-depth analysis of Solidity and Ethereum. Currently, Howard is the core engineer of Qtum Quantum chain DApp platform, responsible for developing tools and database. Howard has 10 years of product development experience in the startup world and is passionate about building decentralized products. He is also the producer of this event.
Hello everyone, it is my honor to share with you some of the development experience of Ethereum smart contract. Before we start, I will give you a high-level introduction today, from the front end to the back end of the technology stack of Ethereum; And then we two teachers will introduce some details to you. Therefore, I only need you to have a concept of Ethereum in your mind. Our two teachers will give you more wonderful explanation of the details later.
I’m going to start today by talking about smart contracts and why they exist, and then I’m going to take you through what they look like, and finally I’m going to take you through the entire architecture of Ethereum, from the DApp in front of the user to the storage database in the back.
Why smart contracts
To get to the point, why do you have smart contracts, mining is very expensive, why do you spend so much energy and so much resources on mining?
One of the blockchain pioneers proposed the concept that technology allows people to collaborate with each other. We personally cooperate with ourselves, but I may do more things, I will ask my friends and my relatives to cooperate, so that I can do bigger things, so we need some cooperation, I trust my brother, I trust my classmates this type of cooperation. More cooperation our fans with the etheric fang we nuggets we hobby and binds us to each other together, let’s do more things, again a little bit more in a city, this time we want to have the law, we need to various government departments, agencies, even if I don’t know about you, I don’t know the man who bought my product, But I can guarantee the feasibility of our cooperation through our contract, and more importantly, the cooperation between states and the International Court of Justice. These cross-border cooperation is a more complicated system. Human civilization and technology have grown with the complexity of this collaboration.
Why do we have rules? Trust is actually a very expensive resource. The people I trust most are my relatives and friends, especially those I grew up with. However, when I come to the level of city or country, it is actually difficult to trust an unknown person, so we should reduce the minimum need for trust to improve the possibility of cooperation. As long as we reduce the risk of hurting each other through this concept, the friendship boat will not capsize.
We have to trust people as much as we can, he says these things are technology, we move from trust between people to law, so we don’t have to talk about trust when we start talking about law. From human feelings to market, I may be willing to cooperate with others. When it comes to market, we start to talk about price. We have a price coordination mechanism, so there is no need to talk about human feelings. In our peer-to-peer dialogue, I have to know you before I can cooperate with you. Many-to-many market matching mechanism is a process of continuous evolution.
So when we start talking about these things, we’re talking about social expansion, and social expansion comes with costs, we need governments, we need courts, we need police agencies. In the chart above we can see that OECD governments’ share of GDP ranges from 36% to 58%. We don’t pay for it, we don’t pay for it directly, but it’s a huge part of government spending. What blockchain does is it replaces trust, the concept of computers instead of humans at its core. So a smart contract is essentially a computer program that replaces these human-run institutions.
What exactly are smart contracts
Let’s take a look at what smart contracts are. The first example is Amazon’s LAMBDA service. One of LAMBDA’s features is that when you write a program on LAMBDA, you just write your business logic, which you upload directly to LAMBDA, as if it were just a callback from a server. It calls this callback as soon as a request comes in, and when it’s done, it just stops, and you have to deploy a server yourself, there’s no logic. LAMBDA is a platform that allows you to put the business logic of the server directly on the platform, regardless of which server it corresponds to, and start LAMBDA when a request comes in. This is very similar to the architecture of smart contracts.
If we look at a very simple example, the business logic of this counter is very simple, we have a COUNT here, think of it as the data in the database, and this contract has both data and business logic, and what the business logic is doing is incrementing this number. When external call this increasing it to modify the database in the COUNT this variable, this is your smart contracts, conceptually it’s nothing, it is because we use the cow force decentralized network to agree with the data should be what is it, specific next we see a better example.
We think of these variables in this contract as a database, and we might normally be writing an additive type, it just exists in the type, and if your type dies, the memory is empty. In a smart contract, even though it’s a variable, you’re writing it on a chain, and this database happens to be a very expensive database. Our false analogy into Java code, we will imagine behind every contract has a corresponding data storage, it is an object that is a database, in this way just about incremental method, it is to operate the database, in this key to modify the value of the inside it, so every time you go to change a variable is actually do an operation on the database, And this operation is extremely expensive operation, can give an idea, ordinary all these designations are expensive, ordinary a note, such as addition, subtraction, multiplication and division, about 2 to 5, the storage of 20,000, about 5,000 times.
Let’s look at a more complete example, the Chain of Merit and Virtue, which illustrates something very fundamental about smart contracts. The reason why we write smart contract is not just to write this data, it is awesome because it has financial attributes, you can trust you, if I create a merit and virtue incense chain on my Amazon LAMBDA, no one will believe it, I can modify it in the database, But if we change it to a smart contract it becomes a decentralized program that doesn’t need to be trusted.
So if you look at this thing, there’s some demand, and when it initializes we’re going to set up the administrator, anyone we want to allow him to donate, and then finally when it’s time to use the money we’re going to allow the administrator to withdraw, and then we’re going to ask him to write down all the withdrawals where he’s going where he’s going.
This is our entire smart contract code, just a dozen lines, there are probably two points to pay attention to, there are contract attributes, the owner’s name, we will use this attribute to store his administrator, we control the administrator permission can be modified. Below are the donation and withdrawal functions, which are explained separately.
Initialize the set administrator, so in this case we’re going to store the data, we’re going to know who the administrator is, and when we create this contract it’s created with a transaction, there’s a wallet that’s going to pay for it and say who created this transaction. The attributes here indicate the identity of the current user, the operator of the transaction, and the operator of the contract. What we do on our side is very simple, when we create this contract we store the current operator in this database, with this thing we can verify that the person who is operating is not the administrator.
And then we started doing this core thing, which is anyone can transfer money, and then you have to crowdfund people to transfer money to you, and this thing has a financial aspect to it. And we see that this function is doing the same thing, operating on this and we’re going to write down the money users, so we’re going to write down this merit; ‘PAYABLE’ is just generally for things that have to do with money, in this case, money deposited in. There is an assertion here, to make sure that the amount of money must be greater than 0.001, because I do not want too little, too little is not good, do not want; Finally, there’s a log here that says we’ll write down this output and this merit, and bless the person who sent the money, and it’s written on the chain. Error is directly a refund, he did not satisfy a condition that the deal would have failed, he donated money, back to the original account, of course the real contract various other needs, can you do idea is you satisfied your need after check, you to operate your business logic, and then finally you can output record the things. I think the more interesting things, the contract he has a corresponding to the account, the account contract how many money, you see the inside of the business logic is not dominant said the money to the contract, the implicit just you say the deal came in, only like this logic, then finally will give the account, but you don’t need to code directly said this thing.
Administrators see see next withdrawal, we this function allows the administrator is pointed out that the number of draw money, here is an assertion, we in the assertion judge whether the current operation of the deal is the owner of the contract, we will record the owner at the beginning, after operation, we go to verify whether he is the person that created the contract, If so, I will send money to the user and transfer the money to him. Let’s start with a small test of what would happen if the account had 10 ethereum coins and the owner tried to retrieve 11 ethereum coins. If it tries to transfer money, this is transferring money from the contract to A, if it doesn’t have enough money in its account, it will report an error, and its status will go straight back to the original state. This concept of rollback is particularly important. Usually when you write a program to do an operation, a request comes in, you do an operation on the database, you report an error, the previous database needs to be rolled back; Ethereum does this automatically, and it rolls back as soon as it fails.
We can look at some of the features of the platform, in the past, you used to buy the server, people use the service for free; Is now a user pay for all the calculations, to calculate GAS model, and then is the core point is he mastered permissions users, for example you use you in paid treasure to pay treasure to transfer platform can confirm the account, and now with the client on the client side to do the signature, then the server said that let you transfer money, he won’t go to check this customer who is in the terminal. Another point is that although ethereum or quantum has tens of thousands of nodes, ethereum has more than 17,000 nodes. It seems that these 17,000 nodes have good computing power, but in fact there is no such thing. Because every node has to repeat every calculation, every piece of data, so when we talk about storing a database, it’s not feasible to use it as a traditional database, because it’s too expensive.
Decentralized platforms have their own disadvantages. They are slow, taking 20 seconds to process a transaction. Secondly, it is expensive. Each transaction costs about $0.1 or $0.2. If you want to issue a larger contract, it is about $10 to $100 RMB. Application lifecycle extremely complex trading, you do this thing you will have to wait for his confirmation, once twice three times, then he may flip a fault, among various fault conditions, is likely to be the business logic behind the wrong, it is possible that he give enough GAS, and then ran half logic he had no money, is annulled, there are some other have no way to do have some in our business, There’s no network requests there’s no randomers, and you can’t update the contract code, and there were some bugs, and somebody wrote a contract, and it was deleted, and then he got a lot of money stuck in it, tens of millions of dollars stuck in it. Therefore, when doing this, we may need to think about whether it makes sense for me to decentralize this thing and what things are worth decentralizing.
DAPP full stack once
So let’s take a deeper look at the whole stack of dApps, let’s take a simple example and then go to the data stored on the bottom blockchain. In this contract, I have a variable, I have a value, and the only thing I have to do is change that value or read that value, and this is my simple smart contract.
We’re going to start at the front end, and we’re going to increment RPC from the front end, which is the code for the ABI, and we’re going to expand that out in a moment, which is the contract that’s its business logic, EVM and finally blockchain storage, and we’re going to expand that out one by one.
So you can compare WEB 2.0 to WEB3.0, and WEB3.0 is bragging that WEB3.0 is the same technology, it’s just the same, in WEB 2.0 we make a request, the user presses a button produces a request, the user presses a button produces a transaction, it has a certain amount of money, This requires the entire network to confirm, rather than a single server, but at the front end the user approves the transaction and then throws it to the network for processing. And then you write the back end and you say, I’m asking you what’s the specific data in it and you give me some structure, and now we’re doing the back end and we’re using the ABI structure, and on the Ethereum side this data structure that corresponds to it is called ABI encoding, and the data that goes to the smart contract is ABI encoding, and in WEB 2.0 we’re doing services, Later on in WEB 3.0 we wanted to write smart contracts to do these services. But the operating system level was Windows in WEB 2.0, and now Ethereum is a virtual machine, and it can also do some operating system stuff, like store data and transfer money, or call other contracts, and that’s done with EBM, and that’s a mix of two different things. It’s a virtual machine and an operating system. And finally, we’re going to store data, which is in the WEB 2.0 file system where we store data, and in WEB 3.0 we have electronic version control, where you can have versions.
That we look at the front, the front to do is to show the data on the chain, the key is still the same, the user signature then submit the data locally on the chain, the right is held in the user’s hands, we can compare the WEB services and pure client two concepts, people familiar with the application of Ann is currency this platform, In fact, the control that is not enough to form these permissions is all in the hands of the platform, and things like wallets are more seriously decentralized. It’s not your word, it’s the user control permissions. If I really wanted to make a decentralized app, it would be a very poor user experience and very difficult to use; The advantage is centralized service, good user experience and quick response, but if something goes wrong, everyone will be thrown out, and now we are in the two extremes to struggle with how to do.
Let’s take a look at the front end example, this REMIX IDE, it points to the contract, it has two buttons, one button for GET and one button for SET, and I’m going to change that value by calling a method. And then when I press that button and I’m going to set 666, at that point he uses RPC to submit an item, and then in Ethereum that’s the way that RPC does it, the concept is the same.
So I’m going to call this method to type this data from my wallet, and then we’re going to expand this data, which is also an RPC, which is the data that goes into the smart contract, so let’s expand it.
I call that thing on the network, and I can see the transaction, and it shows some information about the transaction, how much Gas I spent, and then it sends some data.
We see ABI encoding, we can open two parts, the 60 fe47b1 he also calls the method, this is his name of the method and the stitching together to get this in front of four bytes is this method selector, tone I use smart contract when the front four bytes specify what I want to call methods, behind those is what I preach, This is how it is encoded, and although 666 actually requires only 2 bytes, its ABI is designed to require 32 bytes, so you can see that it is a bit wasteful here. As soon as the data is uploaded, the NODE of EVM will add the smart contract code to this to execute. This is the method we just set, and the value is passed in for modification.
We can look at the bytecode of EVM, and we just saw that this is a smart contract, and this smart contract is finally compiled into bytecode by the compiler, and it’s basically executed from top to bottom. So what we’re going to focus on is there are two things we can focus on, 60Fe47B1 up here and this is our selector, so I’m going to check if this is the method, if this is the method I’m going to jump over here and go ahead. So this is what we SED method, the bottom we store data, it is a key point, we just want to deposit to the value of this attribute, this property can be understood as a build value, he is directly mapped to the location of the zero, I use a scale of 0 to storage, finally put the data stored in the memory, so it is the most key’s instructions, the entire contract Store data.
We just say those things from the EVM entry bytecode into code, this is when I send him with some data, the data is to have a command, and then I take four bytes in the front of this is the way I choose, if match the byte I jump into the tag side, here I want to take parameters, I also is to get from here, I just take 32 bytes from four bytes, that’s my argument, and then FINALLY I put the argument that I read into this position, and that’s what we just contracted to do.
We analyse the sstore below the instructions to be doing, we just do is to put the data is the first position, L1 this position, this position can be unlimited, I am the position after he will change, but this is my version number on the top, my version number he is taking this on both sides, and each node, The hash on this side is changed by the following, if I change the data on this side, the hash on this side will change, which will cause the version number on the top to change, so I can just use this hash as the version number. So if I’m going to change the data on this side then I’m going to change the top version through this path, and this is where I’m going to store my data, so anything I store anywhere affects the top version number, kind of like top version control.
Here is our data structure of data storage, and then we have a look at how this data structure and finally into the chain, this is our block chain, it will save the version number, like say you go to making the above to see a project, there will be a historical update document, will tell the version number changes, the version number change. This data structure is similar to a version, each version will record what the corresponding version number is, and then from this version number can be retrieved all the data of the relative version.
Just to recap, we go from the front end through RPC, which is the service provided by the Ethereum node, we call the node through RPC, the data in the call is structured by ABI code, and then passed to the contract, which processes the data, and finally the contract is the corresponding bytecode, which is parsed by EVM, The result of the analysis is usually to deposit out or to send money to another account, and that’s about it.
Blockchain present & Future
The new world of blockchain, this is our small outlook for the future. At present, there are about 50 million users in speculation, and maybe in the next decade, blockchain will grow to 1 billion users, as popular as mobile phones. If that happens, everyone will have some data assets, become as ubiquitous as cell phones, and then we’ll talk about that. This involves expansion. There are several schemes for expansion. Now I prefer side chain and cross-chain schemes. If we want to make products, we should consider how to make good products for people. Another is privacy. Blockchain is a public ledger. There is no privacy at all.
There are a lot of problems with smart contracts. Smart contracts are very expensive to learn in terms of security or usability, because it’s a different platform, and the programming language is different from other languages, so you have to learn everything from scratch. Now we can see the emergence of a new generation of smart contract platforms, such as WEB 3.0, which is a near-bottom version in the browser. So we can see like the etheric fang ecological circle truebit trying then like rholang project, it also solved the problem of the contract of concurrent, like quantum I now do a virtual machine in our company, this is to simulate the CPU architecture, we hope that through this architecture can support more for the development of the underlying contract to do intelligence, And then we’re going to look at things like the layering between the operating system and the virtual machine. Now if you look at EBM, it’s bytecode, and you see some instructions that belong to the operating system, and it’s actually built into the virtual machine, and that’s also a technical problem. We just mentioned a problem that whether the smart contract can be updated and how it should be updated, which is also a discussion in the industry now. We will adopt a method like DGP, and let users choose how to update the smart contract through voting. Welcome to talk to me, thank you.