从零开始搭建Qtum测试网络Sparknet


  • qtum team

    从零开始搭建Qtum测试网络Sparknet

    qtum 小龙

    6月28日,Qtum第一版测试网络正式发布,这里给大家介绍一下如何从零开始搭建Qtum的测试网络,先将一些有用的网站列举如下:

    本文中,我的电脑环境:macOS 10.12.5

    ···················································

    安装篇

    1. 环境准备

    以下需要安装的工具或者库,如果已经安装过,可以忽略对应步骤,直接进入下一步。

    1.1 安装Homebrew

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

    1.2 安装命令行工具

    xcode-select --install
    

    1.3 安装依赖

    brew install cmake automake berkeley-db4 libtool boost --c++11 miniupnpc openssl pkg-config protobuf qt libevent
    

    这一步安装的包比较多,如果电脑里从未安装过以上的包的话,需要耐心的等待一段时间,起来吃点水果吧

    1.4 拉取qtum代码

    首先找个路径来存放qtum的代码,没有的话新建个路径,我的代码放在路径~/workspace/sparknet

    cd ~/workspace/sparknet
    git clone --recursive https://github.com/qtumproject/qtum.git
    cd qtum
    git submodule update --init --recursive
    


    这个也需要耐心等待,吃完水果听点音乐吧

    1.5 编译代码

    ./autogen.sh
    ./configure
    make
    

    提示:如果./autogen.sh的时候下载boost卡住了的话(像下面的图中那样),不要慌,可以先从这里(<---戳这里)下载离线包,然后复制到路径~/workspace/sparknet/qtum/src/cpp-ethereum/deps/src,记得把~/workspace/sparknet替换成你自己的存放路径,然后再执行./autogen.sh

    好了,一切顺利的话,就开始make了,make的过程也会需要等待,而且电脑会进入暴躁的状态,风扇呼呼地,这个时候你可以拿出小电影看起来了

    编完之后,不放心可以跑一下

    make check
    

    使用篇

    代码顺利编完之后,会生成几个我们需要用到的文件:

    • src/qt/qtum-qt - Qtum QT 钱包
    • src/qtumd - Qtum Core
    • src/qtum-cli - Qtum Client

    1. QT钱包

    运行命令打开QT钱包

    ./src/qt/qtum-qt
    

    运行之后的界面如下,钱包开始同步区块数据,由于测试网络刚开始没多久,所以同步很快就能完成,不想等待可以直接点击Hide隐藏。

    好了,下面我们来看看钱包里边的样子:

    写文章的时候我把自己的老数据备份起来了,所以大家看到我的钱包里余额都是0。

    没有钱怎么玩儿?不要方,跟我做,先来请求一个qtum的地址:

    然后把地址粘贴到我们提供的测试网络代币的发放网站(俗称水龙头 http://qtum.me)(<---戳这里)申请测试代币。经过一小会儿的等待,可以刷新看看代币是否发送,一般几分钟就到帐了:

    此外,我们还提供了Qtum区块链浏览器(qtum.info)(<--- 戳这里)用来查询区块链的各种信息。将刚才的地址粘贴到搜索栏里,就可以看到该地址的信息了:

    现在,我们在钱包里就能看到账户余额发生变动了:

    接着大家就可以在钱包里尝试收发代币,查看交易历史了。关于qt钱包的部分就介绍到这里。

    2. 智能合约

    目前智能合约的功能只能通过在终端中使用qtum-cl发送rpc命令来使用。如果刚才qt钱包还开着,先把它关掉,然后打开一个terminal,运行

    cd ~/workspace/sparknet/qtum/src
    ./qtumd
    

    qtum使用基于EVM的智能合约,合约的语言也是solidity。本文仅仅介绍如何在qtum测试网络上面创建和执行合约的方法,不会详细介绍如何书写一个合约代码。
    我们的测试合约很简单,set函数设置变量nameget函数获取变量name

    pragma solidity ^0.4.11;
    
    contract Test {
    
        bytes public name;
        
        function Test() {
        }
        
        function set(bytes _name) returns (bool) {
            name = _name;
            return true;
        }
        
        function get() returns (bytes) {
            return name;
        }
    }
    
    

    2.1 创建合约

    将代码复制粘贴到Browser Solidity(<--- 戳这里)的代码框里
    Markdown
    点击右侧的contract detail可以看到更多的细节,包括Bytecode,Interface,Functions等。
    现在打开一个新的terminal

    cd ~/workspace/sparknet/qtum/src
    ./qtum-cli createcontract "6060604052341561000c57fe5b5b5b5b6104448061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630399321e1461005157806306fdde03146100c35780636d4ce63c1461015c575bfe5b341561005957fe5b6100a9600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101f5565b604051808215151515815260200191505060405180910390f35b34156100cb57fe5b6100d3610218565b6040518080602001828103825283818151815260200191508051906020019080838360008314610122575b805182526020831115610122576020820191506020810190506020830392506100fe565b505050905090810190601f16801561014e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561016457fe5b61016c6102b6565b60405180806020018281038252838181518152602001915080519060200190808383600083146101bb575b8051825260208311156101bb57602082019150602081019050602083039250610197565b505050905090810190601f1680156101e75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000816000908051906020019061020d92919061035f565b50600190505b919050565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102ae5780601f10610283576101008083540402835291602001916102ae565b820191906000526020600020905b81548152906001019060200180831161029157829003601f168201915b505050505081565b6102be6103df565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103545780601f1061032957610100808354040283529160200191610354565b820191906000526020600020905b81548152906001019060200180831161033757829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103a057805160ff19168380011785556103ce565b828001600101855582156103ce579182015b828111156103cd5782518255916020019190600101906103b2565b5b5090506103db91906103f3565b5090565b602060405190810160405280600081525090565b61041591905b808211156104115760008160009055506001016103f9565b5090565b905600a165627a7a72305820685f6c079aaad9816d92e95b129b79020c6fad4c0496d6b30993152d869939760029" 4000000 0.0000001
    

    返回结果中的address就是合约的地址,这里的地址是9e083b66586d0eba7ef35b147cb1d63308d0834e

    2.2 查询合约信息

    调用createcontract创建合约后,需要等待该笔交易打包进入区块才能生效,通过getaccountinfo和合约地址可以查询到合约的信息。

    ./qtum-cli getaccountinfo "9e083b66586d0eba7ef35b147cb1d63308d0834e"
    

    2.3 调用测试合约的函数:set

    调用合约函数需要用到ABI(<--- 戳这里了解更多),ABI的数据可以自己手动拼接,也可以使用一些工具(例如ethabi)生成,这里不做详细介绍。
    我们现在已经通过ethabi生成了ABI数据,下面调用sendtocontract命令来设置测试合约中的变量name

    ./qtum-cli sendtocontract "9e083b66586d0eba7ef35b147cb1d63308d0834e" "0399321e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b49206c6f7665205174756d000000000000000000000000000000000000000000"
    

    2.4 调用测试合约的函数:get

    get函数的地址是6d4ce63c,由于该函数不会改变链上的数据和状态,我们可以调用callcontract命令,关于更多callcontractsendtocontract的比较,以及其他关于合约相关的说明可以参考另一篇文章(<---戳这里)

    ./qtum-cli callcontract "9e083b66586d0eba7ef35b147cb1d63308d0834e" "6d4ce63c"
    

    比较图中的output字段为get返回的结果:

    "output": "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b49206c6f7665205174756d000000000000000000000000000000000000000000"
    

    我们可以发现output与2.3节中set的数据是一样的。

    好了,到这里我们已经在Qtum上成功的跑了一个简单的智能合约。

    最后,如果在测试网络中发现任何bug欢迎报送到这里 https://github.com/qtumproject/qtum/issues

    更多的设计和操作方法可以看github上面的ITD(<---戳这里)文档。



  • @xiaolong

    Is there a possible way to get step by step instructions on how to run the sparknet on my computer in english? Im running windows. I'm pretty new to this, but with instructions, I got the ethereum blockchain to run on my computer.

    有没有可能的方法来获得有关如何在英文电脑上运行sparknet的分步说明?


  • qtum team

    This post is deleted!

  • qtum team

    @frankrbruno
    I just completed the Chinese version, maybe later I will translate it to English.
    And I think you can get help from here https://github.com/qtumproject/qtum/blob/testnet-1/README.md

    When you come to any problem you can ask me : [email protected]



  • @xiaolong

    Thank you very much.



  • 大神你好 我昨天在linux机器上做了部署,执行到 ./autogen.sh这一步的时候,显示一下错误信息,请问是什么原因呢?

    CMake Error at cryptopp-stamp/download-cryptopp.cmake:157 (message):
    Each download failed!

    error: downloading 'https://github.com/weidai11/cryptopp/archive/bccc6443c4d4d611066c2de4c17109380cf97704.tar.gz' failed
         status_code: 1
         status_string: "Unsupported protocol"
         log:
         --- LOG BEGIN ---
         Protocol "https" not supported or disabled in libcurl
    

    Closing connection -1

    -- Using src='https://github.com/ethereum/cpp-dependencies/releases/download/cache/boost_1_63_0.tar.gz'
    CMake Error at boost-stamp/download-boost.cmake:157 (message):
    Each download failed!

    error: downloading 'https://github.com/ethereum/cpp-dependencies/releases/download/cache/boost_1_63_0.tar.gz' failed
         status_code: 1
         status_string: "Unsupported protocol"
         log:
         --- LOG BEGIN ---
         Protocol "https" not supported or disabled in libcurl
    

    Closing connection -1

         --- LOG END ---
         error: downloading 'https://github.com/ethereum/cpp-dependencies/releases/download/cache/boost_1_63_0.tar.gz' failed
         status_code: 1
         status_string: "Unsupported protocol"
         log:
         --- LOG BEGIN ---
         Protocol "https" not supported or disabled in libcurl
    

    Closing connection -1

    make[2]: *** [deps/src/cryptopp-stamp/cryptopp-download] Error 1
    make[1]: *** [CMakeFiles/cryptopp.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs....
    make[2]: *** [deps/src/boost-stamp/boost-download] Error 1
    make[1]: *** [CMakeFiles/boost.dir/all] Error 2
    make: *** [all] Error 2


Log in to reply
 

Looks like your connection to QTUM was lost, please wait while we try to reconnect.