Blockchain Academy (first lesson): Blockchain intro + smart contract +solidity__ Blockchain

Source: Internet
Author: User
Tags anonymous bool time limit
block Chain Intro

What the blockchain really is.

Blockchain (English: Blockchain) is a distributed database, originated from Bitcoin, Blockchain is a series of cryptographic methods associated with the generated data block, each block contains a number of Bitcoin network transactions information, to verify the validity of its information (anti-counterfeiting) and the next block generation. (Excerpt from wiki) definition of blockchain technology:

Blockchain is a distributed ledger, a technical solution to collectively maintain a reliable database through decentralized, trust-based approaches. from the data point of view:

Blockchain is a distributed database that is almost impossible to change, "distributed" has two meanings, one is distributed storage, and one is a few features of all participants to maintain the blockchain technology (1) Anonymous (2) non-tamper and encryption Security (3) No-trust system (4) decentralized (5) Transparent trading

Reference from

Based on the advantages of the above, the Bitcoin system realizes a self-running, volume billions of trading system, and the globalization has been running for many years in a stable. Bitcoin transactions between any two accounts are faithfully recorded on a large number of redundant ledgers.

In a bitcoin network, any account is anonymous, and any transactions between accounts are non-tamper-proof and are recorded on each node. Then through to the mining of bitcoin incentive mechanism to achieve the self-operation of this network, without any centralized trading system.

Aether Square

So what is Ethereum?

Ethereum is an open-source, intelligent contract-based public blockchain platform that provides a centralized virtual machine (EVM) to handle point-to-point contracts via its dedicated crypto-currency Ethereum (from wiki)

The simplest argument is: Blockchain technology + smart contracts.

Based on the technology of Blockchain, Ethereum supports the intelligent contract, so that blockchain technology can be combined with commercial application and the project will be landed.

In Ethereum's network, smart contracts are also considered a special account, allowing users to make calls to properties and methods in the account by trading with the account. Thus, the realization of the intelligent contract is supported from the underlying technology. Technical Architecture Diagram

What a smart contract is.

Previously mentioned five features of blockchain technology. Ethereum inherits all of these blockchain technologies on top of it, providing support for smart contracts. So the blockchain technology from the original account and the trading function between the account, expand into a can realize the smart contract platform. This smart contract can be a crowdfunding contract, a mathematical formula, or a complete random number.

As long as the smart contract is deployed to the Ethereum network, he is born with 5 features of blockchain technology, and because he is written in JavaScript-like languages, many complex business logic can be implemented.

本教程主要介绍的就是对智能合约的编程,通过编写符合自己商业逻辑的智能合约,就可以轻松的实现各种基于区块链的项目落地。 下一章,我们将从一个最简单的智能合约入手,给大家快速介绍一下智能合约长什么样。

The simplest of smart contracts

The simplest of a smart contract

pragma solidity 0.4.9;
Contract Demotypes {
    function f (UINT a) returns (UINT B) {
    UINT result = a * 8;
    return result;
    }
}

The above is the simplest of a smart contract, the smart contract to achieve a basic function, that is, input N, return 8*n.
So what do we need to do with him? This involves a very useful tool, browser-solidity.

Official address: https://ethereum.github.io/browser-solidity/#version =soljson-v0.4.9+commit.364da425.js

Note that the solidity version currently used here is 0.4.9, so pragma solidity should follow the 0.4.9 version, otherwise it will be an error.

By pasting the code above, we can see the result as shown in the following figure:

This time click on the Red Create button

You can deploy this simplest smart contract on a blockchain network (in-memory)

Here we can see a few things trasaction/execution cost: This represents the costs of create a contract, in gas. Gas and ether currency have a conversion relationship, the exchange rate is determined by Oracle here you can see our contract name Demotypes, registered on an address. This represents the contract has been dug out of the mine. The third one is the contract code above us, f (n) {return 8 * n}

This time we enter 100 and then click the F button and we can see the results

The result is clear that the result is 800, which conforms to the expected execution F () This function consumes the gas cost is 21698+800

One of the simplest smart contracts is described above, and the next chapter will introduce the language solidity of smart contracts. is also the focus of the book.

solidity

In the previous article, we can see pragma solidity 0.4.9;

The solidity is the core language solidity of the Ethereum Smart contract and the focus of this tutorial. what is solidity.

Solidity is the programming language of the Ethereum Smart contract, which enables you to create, execute, and view smart contracts by compiling & deploying smart contracts for certain business applications. a few simple solidity examples

Through the following smart contracts, we can link some commercially used chunks to a business model that is intermediary, trusted, and highly transparent.

In the following tutorial, we will step through the solidity programming to help you quickly master the solidity language, and the blockchain to the front-end Web page I implementation of the 1+2+3+. Sum function of +n

pragma solidity 0.4.9;
Contract DEMO1 {/
  * calculate sum from 1 to n */
  function f (UINT N) returns (UINT sum) {
    if (n = = 0) throw; uint result = 0;
  
   for (uint i=0; i<=n; i++) {
      result +=i;
    }
    return result;
  }
}

  
II implements a token function, and comes with the ability to mine and transfer tokens.
pragma solidity ^0.4.0;
    Contract Coin {//the keyword ' public ' makes those variables//readable from outside.
    Address public Minter;

    Mapping (address = uint) public balances;
    Events allow light clients to react on//changes efficiently.

    Event Sent (address from, address to, uint amount);
    This is the constructor whose code was//run only if the contract is created.
    function Coin () {minter = Msg.sender;
        } function Mint (address receiver, uint amount) {if (Msg.sender! = Minter) return;
    Balances[receiver] + = amount;
        } function Send (address receiver, uint amount) {if (Balances[msg.sender] < amount) return;
        Balances[msg.sender]-= amount;
        Balances[receiver] + = amount;
    Sent (Msg.sender, receiver, amount); }
}
III implement a crowdfunding smart contract in which each user can raise funds and fund a successful transfer of the proceeds to the beneficiary, and each participant can obtain tokens.
pragma solidity ^0.4.2; Contract Token {function transfer (address receiver, uint amount) {}} contract Crowdsale4 {address public Benefici
    ary
    UINT public fundinggoal;
    UINT public amountraised;
    UINT public deadline;

    UINT public price;
    Token public tokenreward;
    Mapping (address = uint256) public balanceof;
    BOOL public fundinggoalreached = FALSE;
    Event goalreached (address beneficiary, uint amountraised);
    Event Fundtransfer (address backer, uint amount, bool iscontribution);

    BOOL public crowdsaleclosed = FALSE; /* Data structure to hold information about campaign contributors */* at initialization, setup the owner */FU
        Nction Crowdsale4 (Address ifsuccessfulsendto, uint fundinggoalinethers, uint durationinminutes, UINT Ethercostofeachtoken, token addressoftokenusedasreward) {beneficiary = Ifsuccessfulsend
        to;
       Fundinggoal = fundinggoalinethers * 1 ether; Deadline = Now + durationinminutes * 1 minutes;
        Price = Ethercostofeachtoken * 1 ether;
    Tokenreward = token (addressoftokenusedasreward);
    }/* The function without name is the default function which is called whenever anyone sends funds to a contract */
        function () payable {if (crowdsaleclosed) throw;
        UINT amount = Msg.value;
        Balanceof[msg.sender] + = amount;
        amountraised + = amount;
        Tokenreward.transfer (Msg.sender, Amount/price);
    Fundtransfer (Msg.sender, amount, true);

    } modifier Afterdeadline () {if (now >= deadline) _;} /* Checks if the goal or time limit has been reached and ends the campaign */function checkgoalreached () Afterdeadlin
            e {if (amountraised >= fundinggoal) {fundinggoalreached = true;
        goalreached (beneficiary, amountraised);
    } crowdsaleclosed = true; } function Safewithdrawal () afterdeadline {if (!fundinggoalreached) {UINT amount = Balanceof[msg.sender];
            Balanceof[msg.sender] = 0; if (Amount > 0) {if (Msg.sender.send (amount)) {Fundtransfer (Msg.sender, amount, F
                Alse);
                } else {Balanceof[msg.sender] = amount; }}} if (fundinggoalreached && beneficiary = = Msg.sender) {if (Benefici
            Ary.send (amountraised)) {Fundtransfer (beneficiary, amountraised, false); } else {//if we fail to send the funds to beneficiary, unlock funders balance fundinggoal
            reached = false;
 }
        }
    }
}

Solidity's introduction is over, and we're going to explain the specifics of these contracts.

As mentioned earlier, the smart contracts are deployed on the Ethereum network, so how to build an Ethereum network requires the official tools to Geth. The next chapter will explain in detail.

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.