Blockchain Development (ii) deployment and operation of the first Ethereum smart contract __ Blockchain

Source: Internet
Author: User
Tags json mysql database

Blockchain Development (ii) deployment and operation of the first Ethereum smart contract

Lihe August 22, 2016

This article is starting 8BTC

There are many articles on the network that deploy smart contracts, but all have a common feature, is the command line to deploy, first build the SOLC environment, then deploy Geth or ETH node, and then step by step to generate wallets, ABI, contract address for deployment, Obscure and easy to fail for beginners, this article mainly describes how to deploy and invoke smart contracts in a graphical interface. For other blockchain knowledge, please refer to my other articles: Http://blog.csdn.net/sportshark

I. Overview of smart contracts and Dapp

1. Basic concept of smart contract

A smart contract is a collection of code and data that can be deployed to run on an ethereum network. If you make a metaphor, the smart contract is more like a Java program, and the Java program executes the code interpretation bytes through the Java Virtual Machine (JVM), and Ethereum's smart contracts are interpreted as bytecode by the Ethereum virtual machine (EVM), and if you learn the assembly, the compiled bytecode and the assembly are similar. At the same time, the smart contract has its own account, which can automatically perform some functions, such as transferring information between each other, modifying the status of the blockchain such as account information, etc. The biggest feature of Ethereum's smart contracts is Turing's complete, popular way to fully simulate everything a computer can do, and the well-known bitcoin can actually execute some simple scripts, but he is not Turing-complete, such as the circular instruction Bitcoin cannot be executed.

Ethereum virtual Machine (EVM)

Ethereum virtual Machine (EVM) is the operating environment of the smart contract in Ethereum. Not only is it encapsulated in a sandbox, it is actually completely isolated, meaning that code running within EVM does not have access to networks, file systems, or other processes, or even a limited number of calls between smart contracts.

2, Dapp basic concept

Beginners often confuse smart contracts with Dapp, and the Ethereum community calls applications based on smart contracts a centralized application (decentralized app). The goal of Dapp is to have a friendly interface to your smart contract, plus some additional benefits for users. A typical Dapp example consists of an HTML interface, a WEB3 runtime, a section of JS code, and a smart contract deployed on a blockchain. Unlike the General CS Architecture Web site, Dapp cannot run on a normal server, and Dapp must be run on a server that can interact with the Ethereum node, or on any ethereum node. Dapp interacts with the corresponding smart contract by committing the transaction to the blockchain network, and reads important data from the blockchain network rather than the centralized database such as (MySQL database). Unlike the typical BS architecture user login system, Ethereum users are represented as a hexadecimal address and all user data and other data are stored locally, many different from the current Web application architecture.

3. High-level language for smart contracts

It is not possible for users to write Ethereum virtual machine (EVM) bytecode directly, so Ethereum offers several high-level languages for writing smart contracts.

Solidity: Like JavaScript, this is the flagship language recommended by Ethereum and the most popular smart contract language. For specific usage, take the solidity document, address: https://solidity.readthedocs.io/en/latest/

Serpent: Python-like style, document address: Https://github.com/ethereum/wiki/wiki/Serpent

LLL: A lisp-like style that has now been terminated.

According to different habits can choose different high-level language, the most popular is solidity.

II. deployment of the first smart contract on the Ethereum private chain

The smart contract in this article uses the Ethereum official example contract, which is the ability to store a number on the blockchain and be able to read it. The code is as follows:

Contract Simplestorage {

UINT Storeddata;

function set (UINT x) {

Storeddata = x;

}

function get () constant Returns (Uintretval) {

return storeddata;

}

}

Even if you have not learned the solidity language, you can see that the contract set function stores a number in the X variable, the Get function reads the number from the X variable, and the contract is deployed as follows:

1. Start the private chain

Start the Ethereum private chain Geth and Ethereum-wallet graphical interface (this article uses the Geth 1.41 version, Ethereum-wallet 0.8.1 version). If you do not know how to start, please refer to my previous article "Blockchain Development (a) building an ethereum-based private chain environment" address: http://blog.csdn.net/sportshark/article/details/51855007, after the boot interface is as follows, Ethereum-wallet will display a red privte-net marker.


2. Creation of two wallets

Click on the "Add Account" button to add a wallet, the program will pop up a dialog box, prompted to enter the password two times, after entering the password, the accounts are created successfully. Create other accounts, the same operation, from the screenshot above can be seen, there are three accounts, one is the main account, one is ACCOUNT2, one is ACCOUNT3

3. Mining for some etheric coins

After the account is created, there is no etheric currency, you need to dig in the private chain, switch to the Geth interface, enter

Miner.start (1)

The miner command, 1 in parentheses, means mining with one thread, which, if not configured, causes the CPU to run at full speed, affecting the use of the computer.

After running for a while, the main account will get a lot of ethereum, this time the screen will quickly brush screen, no tube, input command miner.stop () stop mining.

4. Transfer One wallet's etheric currency to another wallet

First click ACCOUNT2 Account, enter the account details of the interface, click on the right of the "copy address", the ACCOUNT2 address copy down, the system will prompt you now in a test network, do not transfer to the real etheric currency to this account.

Click the "SEND" button in your wallet to transfer ACCOUNT2 from Mainaccount to a certain etheric currency, and you can see that the cost of executing the transaction is 0.00042 Ethereum.

Click Send will prompt such as enter the password, but it has not been sent successfully, according to the blockchain trading rules, need to confirm the miners, and each transaction needs to confirm 12 blocks, a block is 16 seconds of generation time. Switch to the Geth program and enter the mining command until the 100 etheric coins are displayed on the ACCOUNT2 and then stop mining.

5. Deploy Smart Contracts

Point "contracts", enter the smart Contract management interface, click on "Depoly NEW contract", start to deploy the smart contract, choose to deploy the smart contract account, and enter the code of the smart contract, as shown below

Click "Deploy" when you are finished, and you will be prompted to enter your account password, as it will cost you to deploy the smart contract.

This time is not to see the deployment of the smart contract, switch to the Geth interface, mining, after 12 blocks, the smart contract can be confirmed and displayed.

third, the operation of intelligent contracts

1. Run the smart contract on this node

Click "Contracts" to enter the smart contract interface, you can see the smart contract "Simplestorage" just deployed, click into the smart contract, enter the details interface, which has the intelligent contract writing area and the Reading area, first start the Geth mining, Then select the corresponding smart contract function set in the Write area, enter the value you want to set in the Value input box below, and after running it can see that the return value of retval in the smart contract function get is changed in the Read area.

Other smart contracts run the same way, nothing more than the function multipoint, input multipoint.

2. Run the smart contract on the other nodes

At this point, the smart contract can only see itself, others can not be seen and run, if others want to run your deployment of smart contracts need to provide some information, is the other tutorial mentioned in the Smart contract ABI and address.

Enter the newly deployed "Simplestorage" smart contract interface with four buttons on the right

A "Deposit Eher": Send an Ethernet token to the smart contract

B. " Copy address: Copy the addresses of the smart contract

C Show QR code: Displays a QR code that displays the address of the smart contract if scanned with a mobile phone

D "Show Interface": Displays the smart Contract's JSON interface, which is the ABI

First we click "Copy Address", copy the address of the smart contract, then click "Show Interface" to copy the JSON interface of the smart contract, in another node that needs to run the smart contract open Ethereum-wallet, open the " Contracts "Interface Click on" WATCH contracts "to add a smart contract.

As shown in the image above, contract name is filled in, contract address fill in the Smart contract addresses, JSON Interface fill in "Show Interface" copy of the content just now. After you click OK, you can see the smart contract and run it.

Iv. deployment Principles of smart contracts

1. Deployment framework for Smart contracts

The deployment of the smart contract described in this article is compiled and executed in a graphical interface, but in fact the main thing is to rely on a node running Geth in the background, at which point Geth provides an RPC interface to provide blockchain information to the graphical interface's wallet.

RPC Interface

Geth provides the JSON RPC API on port 8545, data transfer in JSON format, can execute various commands of the WEB3 library, such as to the front end, such as mist, such as graphical clients to provide blockchain information, the default access address is http://localhost:8545.

When we deploy a smart contract, first Ethereum-wallet calls the SOLC Smart contract compiler to compile the code Chengcheng the EVM bytecode, and then sends the EVM bytecode through the Geth RPC interface to the Ethereum network, after a full network authentication, Also written to each Geth managed blockchain, the schema is as follows


2. Deployed Data Flow

First the code through the SOLC compiled into a binary code, and then through a transaction to create a smart contract, the transaction contains the creator account, the smart contract content, the address of the smart contract, the key information, wherein the smart contract address is generated by the creator's account and the number of transactions sent as a random number input, Re-create an address as an account with the KECCA-256 encryption algorithm.


During the deployment process, it needs to be deployed through transactions, and the data is stored on the blockchain, which requires gas.

Trading (Transactions)

A deal is a message that is sent from one account to another. Transactions can contain binary data (payload) and etheric currency.

If the target account contains code, the code and input data are executed.

If the target account is a 0 account (the account address is 0), the transaction will create a new contract. As mentioned above, this smart contract address is not a 0 address, but is calculated by the address of the contract creator and the number of transactions that were issued at that address. The payload that created the contract transaction is executed as an EVM bytecode. The executed output is permanently stored as the contract code. This means that, in order to create a contract, you do not need to send a real contract code to the contract, but rather to send code that returns executable code.

Gas

Each transaction on the Ethereum will be charged a certain amount of gas,gas to limit the amount of work required to execute the exchange and to pay for the execution. When EVM executes the smart contract, gas will be gradually consumed according to certain rules, in fact gas is the smaller unit of the etheric currency, if the etheric currency is compared to 100 yuan, then gas can be regarded as 1 cents. If there is only aether, there will be problems, the etheric currency is required to buy and sell, the market will have price fluctuations. There could be a situation like Bitcoin, which fell 50% a day by 50%. The cost of this calculation is unacceptable, for example, it takes 10 dollars to do an addition today, and it takes 100 dollars to do an addition tomorrow. So the gas is introduced here to decouple. The volatility of the market and the calculation of the cost of decoupling, that is, between the ether and gas there is a exchange rate, the etheric currency rise does not matter, gas price drop can be. It wants to make sure that I do the same calculations and that the Fiat are consistent.

Gas price, the ether currency, is set by the creator of the transaction, and the transaction fee required to send the account is paid in advance = gas prices * Gas amount. If there is gas remaining at the end of execution, the gas will be returned to the sending account.

It was mentioned in the previous article that the deployment of smart contracts used 0.00042 etheric coins, converted into gas is 21,000 gas.

No matter where it is executed, once gas is exhausted (such as a negative value), a Out-of-gas exception is triggered. All state modifications made by the current call frame will be rolled back.

Five, the operation principle of the intelligent contract

The smart contract is the code that is deployed in the blockchain, the blockchain itself cannot execute the code, the code is executed in the local EVM, in fact, the code deployed on the blockchain is able to generate the original smart contract code locally, you can understand the blockchain as a database, and the client reads the stored run code from the database. And after running locally, the results are written to the blockchain database.

In essence, Ethereum's wallet is also an application of smart contracts, and Ethereum is a platform for writing a variety of applications. Next, will explain in detail the development of intelligent contract and debugging methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.