【区块链】DOCKER部署量子链私有网络环境以及部署智能合约实践

DOCKER部署量子链私有网络环境以及部署智能合约实践

安装环境

安装Docker

https://store.docker.com/editions/community/docker-ce-desktop-mac

这里安装的是CE的稳定版

xiaoyu@LIXIAOYUdeMacBook-Pro.com docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:09 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:45:38 2017
 OS/Arch:      linux/amd64
 Experimental: true

xiaoyu@LIXIAOYUdeMacBook-Pro.com docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (******************): 
Password: 
Login Succeeded


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
5b0f327be733: Pull complete 
Digest: sha256:07d5f7800dfe37b8c2196c7b1c524c33808ce2e0f74e7aa00e603295ca9a0972
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

创建桥接网络


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker network create --driver bridge qtum_network
2b13b85764a65c48335edd059a7c715da1cbf7c07df4387d68bb269ef16e6796


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f180013a366c        bridge              bridge              local
d1f91168e308        host                host                local
c7412c2ee6d2        none                null                local
2b13b85764a6        qtum_network        bridge              local




拉取Qtum镜像


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker pull cryptominder/qtum
Using default tag: latest
latest: Pulling from cryptominder/qtum
280aca6ddce2: Pull complete 
a3ed95caeb02: Pull complete 
c05bbaad6588: Pull complete 
d58e30222385: Pull complete 
72e52753ff3a: Pull complete 
e2f4e66e4f9a: Pull complete 
ab49593d9269: Pull complete 
5334c4f4c433: Pull complete 
Digest: sha256:99764799a4fc807882cd8a2744d844f701b41f6846bf0b87c7d8780ff517a83f
Status: Downloaded newer image for cryptominder/qtum:latest


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cryptominder/qtum   latest              f5adde30c8eb        8 days ago          92.1MB
hello-world         latest              05a3bd381fc2        4 weeks ago         1.84kB

本地文件映射

docker run后面接下面两种参数


-v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro

-v ${PWD}/node1_data:/data

获取配置文件


git clone https://github.com/crypt0m1nd3r/docker-qtum-config.git

启动节点

xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -d --rm --name qtumd_node1 --network=qtum_network -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtumd
522be9f3d830c1f749f15c4a7b6e87008930b3a416bb2ae2cc8561ac84fe6a9c

列表


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                       NAMES
522be9f3d830        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   23 seconds ago      Up 25 seconds       3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node1

在当前文件夹可以看到一个node1_data文件夹


xiaoyu@LIXIAOYUdeMacBook-Pro.com ls node1_data/regtest 
banlist.dat blocks      chainstate  database    db.log      debug.log   peers.dat   qtumd.pid   stateQtum   vm.log      wallet.dat

其他两个节点



~/docker_qtum/docker-qtum-config git:(master) [11:47:23]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -d --rm --name qtumd_node2 --network=qtum_network -v ${PWD}/node2_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node2_data:/data cryptominder/qtum:latest qtumd
cf5bf31175d5627c52a6f3513f8f96bf1bdf74869bcb5b14bff9ef4232a02763

~/docker_qtum/docker-qtum-config git:(master) [11:48:11]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -d --rm --name qtumd_node3 --network=qtum_network -v ${PWD}/node3_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node3_data:/data cryptominder/qtum:latest qtumd 
c4bc72f072a500fbcf1ea3d63ecd02b59b8641c246afa6744ab40597edc9f7ca

查看


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS              PORTS                                       NAMES
c4bc72f072a5        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   46 seconds ago       Up 48 seconds       3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node3
cf5bf31175d5        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   About a minute ago   Up About a minute   3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node2
522be9f3d830        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   4 minutes ago        Up 4 minutes        3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node1

验证节点有效

~/docker_qtum/docker-qtum-config git:(master) [11:49:30]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i –network container:qtumd_node1 -v PWD/node1qtumd.conf:/home/qtum/qtum.conf:rov{PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli getinfo
{
“version”: 140301,
“protocolversion”: 70016,
“walletversion”: 130000,
“balance”: 0.00000000,
“stake”: 0.00000000,
“blocks”: 0,
“timeoffset”: 0,
“connections”: 2,
“proxy”: “”,
“difficulty”: {
“proof-of-work”: 4.656542373906925e-10,
“proof-of-stake”: 4.656542373906925e-10
},
“testnet”: false,
“moneysupply”: 0,
“keypoololdest”: 1508125526,
“keypoolsize”: 100,
“paytxfee”: 0.00000000,
“relayfee”: 0.00400000,
“errors”: “”
}

Proof-of-Stake (POS)


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli generate 1
[
  "56b7a6fb87d15dd289fe682f7adce8fb2c49a330a8f19ac4c0217a35d8c639c4"
]
  ~/docker_qtum/docker-qtum-config git:(master) [12:03:54]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli generate 4999
[
  "794857ae6cdae4b483407fda666d0004d0c4385335ea706d17aa2c3b4b591e62", 
  "1f6ab1697ece120857db6f7f364efaf01563985c5b962d2d76c0614423209ed8", 
  "74f72495383cd7037a94c4652165874727f7a86543527c30fae07c5b702492c4", 
  "310657e8a642213cfdfb0f93fb6bdaa11b485c16dc29daf792d0a5e69b0a21bd", 
  "3fbd40f4455c65fd15dddca49a401cbd568df1aec20d2f0c74108cd082dbb4d6", 
  "62893413cf52b7d0c990a847139ea402a002f18f9131470b841ff699eab5356f", 
  "7481d60c63d3a9c064c2c91d1ae7d50a2c6bd4ffea08fb6af9fcbd586b163861"
  ...................




超过5000个块后,开始POS,每过几分钟,就会自动产生新的区块。



xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli getwalletinfo  
{
  "walletversion": 130000,
  "balance": 90000000.00000000,
  "stake": 110000.00000000,
  "unconfirmed_balance": 0.00000000,
  "immature_balance": 9900000.00000000,
  "txcount": 5005,
  "keypoololdest": 1508125526,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "hdmasterkeyid": "7edb36583046f5ca439537f24073d0a6602ea901"
}

stake为自动产生的部分。

停止运行


docker stop qtumd_node1

部署

确认环境


  ~/docker_qtum/docker-qtum-config git:(master) [13:35:19]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker ps -f name=qtumd
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                                       NAMES
c4bc72f072a5        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   2 hours ago         Up 2 hours          3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node3
cf5bf31175d5        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   2 hours ago         Up 2 hours          3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node2
522be9f3d830        cryptominder/qtum:latest   "/entrypoint.sh qtumd"   2 hours ago         Up 2 hours          3888-3889/tcp, 13888-13889/tcp, 23888/tcp   qtumd_node1
  ~/docker_qtum/docker-qtum-config git:(master) [13:49:48]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli getwalletinfo
{
  "walletversion": 130000,
  "balance": 90000000.00000000,
  "stake": 3960000.00000000,
  "unconfirmed_balance": 0.00000000,
  "immature_balance": 6400000.00000000,
  "txcount": 5180,
  "keypoololdest": 1508125526,
  "keypoolsize": 100,
  "paytxfee": 0.00000000,
  "hdmasterkeyid": "7edb36583046f5ca439537f24073d0a6602ea901"
}







确认是不是有智能合约


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli listcontracts
{
  "0000000000000000000000000000000000000083": 0.00000000,
  "0000000000000000000000000000000000000080": 0.00000000,
  "0000000000000000000000000000000000000081": 0.00000000,
  "0000000000000000000000000000000000000082": 0.00000000,
  "0000000000000000000000000000000000000084": 0.00000000
}

获取solc和ethabi


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run ethereum/solc:stable solc --version
Unable to find image 'ethereum/solc:stable' locally
stable: Pulling from ethereum/solc
88286f41530e: Pull complete 
5650800b8aa2: Pull complete 
4c72a311195d: Pull complete 
e416a2de4b1d: Pull complete 
6dcc936acf64: Pull complete 
Digest: sha256:c61b13352fb2feb691cce49439eababdf9da215d4669437c17c9fd36311ad264
Status: Downloaded newer image for ethereum/solc:stable
solc, the solidity compiler commandline interface
Version: 0.4.17+commit.bdeb9e52.Linux.g++
  ~ [14:48:40]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run ethereum/solc:stable solc --version
solc, the solidity compiler commandline interface
Version: 0.4.17+commit.bdeb9e52.Linux.g++


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker pull cryptominder/ethabi
Using default tag: latest
latest: Pulling from cryptominder/ethabi
219d2e45b4af: Pull complete 
ef9ce992ffe4: Pull complete 
d0df8518230c: Pull complete 
38ae21afde7b: Pull complete 
baa1c2e15c01: Pull complete 
85b637e7eada: Pull complete 
223f4c62bf4a: Pull complete 
7a1623674a9e: Pull complete 
Digest: sha256:2fabc4f844a1309037bfd93f63956cac6c3dd76779e674d5388c02192aec6147
Status: Downloaded newer image for cryptominder/ethabi:latest
  ~/docker_qtum/docker-qtum-config git:(master) [14:54:17]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run --rm -v ${PWD}:/ethabi cryptominder/ethabi:latest --help
Ethereum ABI coder.
  Copyright 2016-2017 Parity Technologies (UK) Limited

Usage:
    ethabi encode function <abi-path> <function-name> [-p <param>]... [-l | --lenient]
    ethabi encode params [-v <type> <param>]... [-l | --lenient]
    ethabi decode function <abi-path> <function-name> <data>
    ethabi decode params [-t <type>]... <data>
    ethabi decode log <abi-path> <event-name> [-l <topic>]... <data>
    ethabi -h | --help

Options:
    -h, --help         Display this message and exit.
    -l, --lenient      Allow short representation of input params.

Commands:
    encode             Encode ABI call.
    decode             Decode ABI call result.
    function           Load function from json ABI file.
    params             Specify types of input params inline.
    log                Decode event log.

编译智能合约




vi helloworld.sol


contract mortal {
    /* Define variable owner of the type address */
    address owner;

    /* This function is executed at initialization and sets the owner of the contract */
    function mortal() { owner = msg.sender; }

    /* Function to recover the funds on the contract */
    function kill() { if (msg.sender == owner) selfdestruct(owner); }
}

contract greeter is mortal {
    /* Define variable greeting of the type string */
    string greeting;

    /* This runs when the contract is executed */
    function greeter(string _greeting) public {
        greeting = _greeting;
    }

    /* Main function */
    function greet() constant returns (string) {
        return greeting;
    }
}


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run --rm -v ${PWD}:/solidity ethereum/solc:stable --optimize --bin --abi --hashes -o /solidity --overwrite helloworld.sol     
helloworld.sol:6:5: Warning: No visibility specified. Defaulting to "public".
    function mortal() { owner = msg.sender; }
    ^---------------------------------------^
helloworld.sol:9:5: Warning: No visibility specified. Defaulting to "public".
    function kill() { if (msg.sender == owner) selfdestruct(owner); }
    ^---------------------------------------------------------------^
helloworld.sol:22:5: Warning: No visibility specified. Defaulting to "public".
    function greet() constant returns (string) {
    ^
Spanning multiple lines.
helloworld.sol:1:1: Warning: Source file does not specify required compiler version!Consider adding "pragma solidity ^0.4.17
contract mortal {
^
Spanning multiple lines.


xiaoyu@LIXIAOYUdeMacBook-Pro.com ls
docker-qtum-config greeter.abi        greeter.bin        greeter.signatures helloworld.sol     mortal.abi         mortal.bin         mortal.signatures


部署智能合约



  ~/docker_qtum/docker-qtum-config git:(master) [15:20:48]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli getaccountaddress greeter_owner
qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7

我们会得到一个地址qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli listaccounts
{
  "": 97420000.00000000,
  "greeter_owner": 0.00000000
}



开始编译



xiaoyu@LIXIAOYUdeMacBook-Pro.com cat greeter.bin && docker run --rm -v ${PWD}:/ethabi cryptominder/ethabi:latest encode params -v string 'Hello World!' --lenient
6060604052341561000f57600080fd5b6040516103173803806103178339810160405280805160008054600160a060020a03191633600160a060020a03161790559190910190506001818051610059929160200190610060565b50506100fb565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100ce565b828001600101855582156100ce579182015b828111156100ce5782518255916020019190600101906100b3565b506100da9291506100de565b5090565b6100f891905b808211156100da57600081556001016100e4565b90565b61020d8061010a6000396000f300606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b58114610047578063cfae32171461005c57600080fd5b341561005257600080fd5b61005a6100e6565b005b341561006757600080fd5b61006f610127565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100ab578082015183820152602001610093565b50505050905090810190601f1680156100d85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101255760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b61012f6101cf565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101c55780601f1061019a576101008083540402835291602001916101c5565b820191906000526020600020905b8154815290600101906020018083116101a857829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a72305820d4b628395b481f1c0f4bee7365b1922f05eaa223abe2bbebf74804849a66181c00290000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f20576f726c64210000000000000000000000000000000000000000

下面会用到的命令



createcontract "bytecode" (gaslimit gasprice "senderaddress" broadcast)
Create a contract with bytcode.

Arguments:
1. "bytecode"  (string, required) contract bytcode.
2. gasLimit  (numeric or string, optional) gasLimit, default: 2500000, max: 40000000
3. gasPrice  (numeric or string, optional) gasPrice QTUM price per gas unit, default: 0.0000004, min:0.0000004
4. "senderaddress" (string, optional) The quantum address that will be used to create the contract.
5. "broadcast" (bool, optional, default=true) Whether to broadcast the transaction or not.

正式部署




  ~/docker_qtum/docker-qtum-config git:(master) [15:25:47]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli createcontract  6060604052341561000f57600080fd5b6040516103173803806103178339810160405280805160008054600160a060020a03191633600160a060020a03161790559190910190506001818051610059929160200190610060565b50506100fb565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100ce565b828001600101855582156100ce579182015b828111156100ce5782518255916020019190600101906100b3565b506100da9291506100de565b5090565b6100f891905b808211156100da57600081556001016100e4565b90565b61020d8061010a6000396000f300606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b58114610047578063cfae32171461005c57600080fd5b341561005257600080fd5b61005a6100e6565b005b341561006757600080fd5b61006f610127565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100ab578082015183820152602001610093565b50505050905090810190601f1680156100d85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101255760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b61012f6101cf565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101c55780601f1061019a576101008083540402835291602001916101c5565b820191906000526020600020905b8154815290600101906020018083116101a857829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a72305820d4b628395b481f1c0f4bee7365b1922f05eaa223abe2bbebf74804849a66181c00290000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f20576f726c64210000000000000000000000000000000000000000 2500000 0.00000049  qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7 

error code: -3
error message:
Sender address does not have any unspent outputs

这是因为对应的地址有余额,我们需要保证部署合约的地址有UTXO


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli sendtoaddress    qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7    3
44de44c625fe3971b8cd528f98a46913a6b3a14ea4eb4a3d8b292202833f29ff

经过1个区块的确认,我们就有UTXO了。



xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli listunspent 1 9999999 [\"qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7\"]
[
  {
    "txid": "44de44c625fe3971b8cd528f98a46913a6b3a14ea4eb4a3d8b292202833f29ff",
    "vout": 1,
    "address": "qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7",
    "account": "greeter_owner",
    "scriptPubKey": "76a91408cd94aa2c2aea9fd23c7c1ff435066b88b5741288ac",
    "amount": 3.00000000,
    "confirmations": 12,
    "spendable": true,
    "solvable": true
  }
]


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli createcontract  6060604052341561000f57600080fd5b6040516103173803806103178339810160405280805160008054600160a060020a03191633600160a060020a03161790559190910190506001818051610059929160200190610060565b50506100fb565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100ce565b828001600101855582156100ce579182015b828111156100ce5782518255916020019190600101906100b3565b506100da9291506100de565b5090565b6100f891905b808211156100da57600081556001016100e4565b90565b61020d8061010a6000396000f300606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b58114610047578063cfae32171461005c57600080fd5b341561005257600080fd5b61005a6100e6565b005b341561006757600080fd5b61006f610127565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100ab578082015183820152602001610093565b50505050905090810190601f1680156100d85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101255760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b61012f6101cf565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101c55780601f1061019a576101008083540402835291602001916101c5565b820191906000526020600020905b8154815290600101906020018083116101a857829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a72305820d4b628395b481f1c0f4bee7365b1922f05eaa223abe2bbebf74804849a66181c00290000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f20576f726c64210000000000000000000000000000000000000000 2500000 0.00000049  qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7

{
  "txid": "2cb2ef3e57d9f72ddc76736e5859badf62904ddce2af5ddfe57fdbe2af9e8558",
  "sender": "qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7",
  "hash160": "08cd94aa2c2aea9fd23c7c1ff435066b88b57412",
  "address": "d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f"
}

检查智能合约


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli listcontracts
{
  "0000000000000000000000000000000000000083": 0.00000000,
  "0000000000000000000000000000000000000080": 0.00000000,
  "0000000000000000000000000000000000000081": 0.00000000,
  "0000000000000000000000000000000000000082": 0.00000000,
  "d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f": 0.00000000, //我们创建的智能合约
  "0000000000000000000000000000000000000084": 0.00000000
}



xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli getaccountinfo d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f 
{
  "address": "d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f",
  "balance": 0,
  "storage": {
    "290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563": {
      "0000000000000000000000000000000000000000000000000000000000000000": "00000000000000000000000008cd94aa2c2aea9fd23c7c1ff435066b88b57412"
    },
    "b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6": {
      "0000000000000000000000000000000000000000000000000000000000000001": "48656c6c6f20576f726c64210000000000000000000000000000000000000018"
    }
  },
  "code": "606060405263ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b58114610047578063cfae32171461005c57600080fd5b341561005257600080fd5b61005a6100e6565b005b341561006757600080fd5b61006f610127565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100ab578082015183820152602001610093565b50505050905090810190601f1680156100d85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101255760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b61012f6101cf565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101c55780601f1061019a576101008083540402835291602001916101c5565b820191906000526020600020905b8154815290600101906020018083116101a857829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a72305820d4b628395b481f1c0f4bee7365b1922f05eaa223abe2bbebf74804849a66181c0029"
}

运行智能合约



xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run --rm -v ${PWD}:/ethabi cryptominder/ethabi:latest encode function /ethabi/greeter.abi greet
cfae3217
  ~/docker_qtum [16:11:49]
xiaoyu@LIXIAOYUdeMacBook-Pro.com ls
docker-qtum-config greeter.abi        greeter.bin        greeter.signatures helloworld.sol     mortal.abi         mortal.bin         mortal.signatures
  ~/docker_qtum [16:11:57]
xiaoyu@LIXIAOYUdeMacBook-Pro.com cat greeter.signatures
cfae3217: greet()
41c0e1b5: kill()


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node2 -v ${PWD}/node2_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node2_data:/data cryptominder/qtum:latest qtum-cli callcontract  d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f    cfae3217
{
  "address": "d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f",
  "executionResult": {
    "gasUsed": 22737,
    "excepted": "None",
    "newAddress": "d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f",
    "output": "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f20576f726c64210000000000000000000000000000000000000000",
    "codeDeposit": 0,
    "gasRefunded": 0,
    "depositSize": 0,
    "gasForDeposit": 0
  },
  "transactionReceipt": {
    "stateRoot": "5d6bf971874e554793c28ce2dcef60eadca95abd18e77686a9d14371f2504386",
    "gasUsed": 22737,
    "bloom": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "log": [
    ]
  }
}

解码output


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run --rm -v ${PWD}:/ethabi cryptominder/ethabi:latest decode params -t string 0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f20576f726c64210000000000000000000000000000000000000000
string Hello World!

清除智能合约

这里对比两个命令

  • callcontract 链下完成,不会在区块链上产生记录。不消耗gas
  • sendtocontract 链上完成,需要同步,会消耗gas
我们使用下面的命令

sendtocontract "contractaddress" "data" (amount gaslimit gasprice senderaddress broadcast)
Send funds and data to a contract.

Arguments:
1. "contractaddress" (string, required) The contract address that will receive the funds and data.
2. "datahex"  (string, required) data to send.
3. "amount"      (numeric or string, optional) The amount in QTUM to send. eg 0.1, default: 0
4. gasLimit  (numeric or string, optional) gasLimit, default: 250000, max: 40000000
5. gasPrice  (numeric or string, optional) gasPrice Qtum price per gas unit, default: 0.0000004, min:0.0000004
6. "senderaddress" (string, optional) The quantum address that will be used as sender.
7. "broadcast" (bool, optional, default=true) Whether to broadcast the transaction or not.

xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli sendtocontract    d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f    41c0e1b5
{
  "txid": "07017d992df14d07dde8cd4fcf5e1dc3837deff6c463c677796d88b2c70defa7",
  "sender": "qbbWnpv1up8HP8T7tYSkfUiv6qnw4qbBw8",
  "hash160": "c5d9780aba93fec62be8624ab0bd1ccbb3bdf6a0"
}





再次调用发现并没有被销毁,因为在合约中规定了调用者必须和创建者一致。


xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli sendtocontract     d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f    41c0e1b5 0 250000 0.00000049 qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7
error code: -3
error message:
Sender address does not have any unspent outputs

//提示没有UTXO


 ~/docker_qtum/docker-qtum-config git:(master) [16:26:41]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli sendtoaddress    qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7    3      
fccbe9c36a26747314d40d2bdfc9cb917f67f0bc011a51e305a4b92dc79a422c
 ~/docker_qtum/docker-qtum-config git:(master) [16:27:22]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli sendtocontract     d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f    41c0e1b5 0 250000 0.00000049 qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7
{
  "txid": "fcc6904e090d89acd65f70c3708d5b49b2f2b1a03f3d3ac19572da6135206bbb",
  "sender": "qJMvqZibSRfVfbHNbew5rZmxD4cdhCXnR7",
  "hash160": "08cd94aa2c2aea9fd23c7c1ff435066b88b57412"
}

再次查看详情,发现已经不存在了



  ~/docker_qtum/docker-qtum-config git:(master) [16:27:26]
xiaoyu@LIXIAOYUdeMacBook-Pro.com docker run -i --network container:qtumd_node1 -v ${PWD}/node1_qtumd.conf:/home/qtum/qtum.conf:ro -v ${PWD}/node1_data:/data cryptominder/qtum:latest qtum-cli getaccountinfo  d5662f5de4e5e3a2dc6dd49a87843f893dbcb48f    
error code: -5
error message:
Address does not exist

到此,智能合约的基本操作完成。

参考资料

上一篇:互联网协议入门(二)


下一篇:OceanBase 问答区上线!Ask OBQ and the Community!