区块链热身报告

16340296 张子权

以太坊的安装、私有链创世区块搭建、私有链节点的加入(选做)

  1. 安装以太坊
    http://ethereum.github.io/go-ethereum/downloads/ 下载安装Geth,可能需要在windows下添加环境变量。
    安装以太坊

  2. 私有链创世区块搭建
    新建一个genesis.json,设置创世区块的难度,id等参数。新建文件夹data。
    初始化创世区块:

    1
    geth --datadir data/ init genesis.json

    启动节点并进入交互式控制台:

    1
    geth --datadir data --networkid 1008 --ipcdisable --port 1001 --rpcport 8001 --verbosity=4 console 2>>output.log
  3. 私有链节点的加入
    再新建一个节点为data0。
    启动节点并进入交互式控制台:

    1
    geth --datadir data0 --networkid 1008 --ipcdisable --port 1003 --rpcport 80222  console

    在第一个节点中使用admin.nodeInfo.enode获取enode。
    在第二个节点中添加第一个节点。

    1
    admin.addPeer("enode://e9e7ad186217fbf7b7d7185695dcd17afa73d23e1a1365dd96d01327aee87b2b95af8ca3857ce6b1f6a7d26c58abc19f5e0846787a1d35e21cab7c5fea0cee2a@[::]:1001")

    再使用admin.peers可得到加入节点的信息。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [{
    caps: ["eth/62", "eth/63"],
    enode: "enode://e9e7ad186217fbf7b7d7185695dcd17afa73d23e1a1365dd96d01327aee87b2b95af8ca3857ce6b1f6a7d26c58abc19f5e0846787a1d35e21cab7c5fea0cee2a@[::]:1001",
    id: "e4a6cb32cfb6b641dd2606bdb67571f63ff80ff1fe4883c1aa46cd5b89e110eb",
    name: "Geth/v1.8.17-stable-8bbe7207/windows-amd64/go1.11.1",
    network: {
    inbound: false,
    localAddress: "127.0.0.1:18752",
    remoteAddress: "127.0.0.1:1001",
    static: true,
    trusted: false
    },
    protocols: {
    eth: {
    difficulty: 85380105,
    head: "0xd2c073c35fb027d8dd8fe4d95174bc82ca93d96599dfb05e8deee320631621f0",
    version: 63
    }
    }
    }]

对 getBlock 中所得区块的各个字段进行解释

字段 类型 意义
difficulty BigNumber 表示当前区块的难度,根据前一个区块的难度和时间戳计算得到,可知当前区块1难度为131072
extraData String 指的是附加信息,可以根据自己的需要填写信息。32字节以内的字节数组。
gasLimit Number 表示当前区块允许使用的最大gas,指这笔交易最多只能给矿工这么多gas,若用不完则返回多出的gas返回,但如果gaslimit不足以支付交易,那么不会退回,因为以太坊规定每笔交易gas最少为21000,所以一定要大于21000。同理若合约的gaslimit小于区块的gaslimit则部署不了。
gasUsed Number 和gasLimit相对,为当前区块所累积使用的gas值。
hash String 指该区块的哈希值,当区块等待被区块链确认时,则为null,可以认为是区块在整个区块链的索引。
logsBloom String 日志Bloom过滤器,信息包含在每个日志入口, 来自交易列表中每个交易的接受者。
miner String 表示打包这一区块的矿工的地址。
mixHash String 混合哈希,与nonce进行哈希运算,证明已经付出了足够的工作量。
nonce String 一个随机数64位哈希,表示该矿工找到的符合条件的nonce值。
number Number 为区块编号,指该区块为第几个区块。
parentHash String 父哈希,指上一个区块的哈希值。因为创世区块没父区块,所以这个值为0。
receiptsRoot String 交易完成后,由交易收据,日志内容构成的数据的字典树根节点哈希。
sha3Uncles String 类似上一个,指由叔区块哈希构成的字典树的根节点哈希。
size Number 当前这个区块的字节大小。
stateRoot String 状态字典树根节点的哈希。
timestamp Number 指当前该区块初始化时的Unix时间戳。
totalDifficulty Number 到当前区块为止,所积累的所有区块难度之和。
transactions 数组 当前区块所包含的交易。
transactionsRoot String 包含所有交易的交易字典树的根节点哈希值。
Uncles 数组 包含当前区块的所以叔区块。

对日志输出进行解释

  • 初始化区块链,分配文件夹和空间,写入创世块,持久化字典树。

    1
    2
    3
    4
    5
    6
    7
    INFO [11-04|15:30:01.915] Allocated cache and file handles         database="D:\\Program Files\\Geth\\data\\geth\\chaindata" cache=16 handles=16

    INFO [11-04|15:30:01.940] Writing custom genesis block

    INFO [11-04|15:30:01.941] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B

    INFO [11-04|15:30:01.941] Successfully wrote genesis state database=chaindata hash=5e1fc7…790e0
  • 开始连接时,初始化以太坊协议,载入最近的区块,重新加载交易和生成交易日志。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    INFO [11-04|15:31:05.759] Initialising Ethereum protocol           versions="[63 62]" network=1008

    INFO [11-04|15:31:05.759] Loaded most recent local header number=0 hash=5e1fc7…790e0 td=131072 age=49y6mo2w

    INFO [11-04|15:31:05.760] Loaded most recent local full block number=0 hash=5e1fc7…790e0 td=131072 age=49y6mo2w
    INFO [11-04|15:31:05.760] Loaded most recent local fast block number=0 hash=5e1fc7…790e0 td=131072 age=49y6mo2w

    DEBUG[11-04|15:31:05.760] Reinjecting stale transactions count=0

    INFO [11-04|15:31:05.762] Regenerated local transaction journal transactions=0 accounts=0
  • 输入exit关闭连接,之后将缓存中的状态写入磁盘持久化数据,关闭区块链,关闭协议和交易池,最后关闭数据库。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    INFO [11-04|17:11:41.533] Writing cached state to disk             block=454 hash=4713ec鈥?7d4ca root=a006ff鈥e94ea

    INFO [11-04|17:11:41.533] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gc
    time=0s livenodes=1 livesize=0.00B

    INFO [11-04|17:11:41.533] Blockchain manager stopped

    INFO [11-04|17:11:41.533] Stopping Ethereum protocol

    INFO [11-04|17:11:41.533] Ethereum protocol stopped

    INFO [11-04|17:11:41.533] Transaction pool stopped

    INFO [11-04|17:11:41.533] Database closed database="D:\\Program Files\\Geth\\data\\geth\\chain
    data"
  • 矿工挖矿,打包新区块,开采潜在的区块。

    1
    2
    3
    INFO [11-04|17:30:48.082] Commit new mining work                   number=593 sealhash=108a77…bbb5d6 uncles=0 txs=0 gas=0 fees=0 elapsed=35.904ms
    INFO [11-04|17:30:49.212] Successfully sealed new block number=593 sealhash=108a77…bbb5d6 hash=c52790…332920 elapsed=1.166s
    INFO [11-04|17:30:49.217] 🔨 mined potential block number=593 hash=c52790…332920
  • 提交交易。

    1
    2
    3
    INFO [11-04|18:25:09.342] Setting new local account                address=0x15b97213fB8ee04D2ed53113E41035FEaE88461e
    INFO [11-04|18:25:09.346] Submitted transaction fullhash=0x889aa5bb7bc45463a5f59012b2ac15abdce71542a48bc6a6be91969c48eac6a5 recipient=0x6a949Cd94E739d035d13c2242B32093553567361
    "0x889aa5bb7bc45463a5f59012b2ac15abdce71542a48bc6a6be91969c48eac6a5"
  • 随后通过挖矿发现有一gas=420000的区块。

    1
    INFO [11-04|18:32:42.760] Commit new mining work                   number=607 sealhash=916dea…38331b uncles=0 txs=2 gas=42000 fees=4.2e-05 elapsed=8.943ms
  • 添加节点。

    1
    2
    INFO [11-04|18:39:56.578] Block synchronisation started
    INFO [11-04|18:39:56.709] Imported new chain segment blocks=26 txs=7 mgas=0.126 elapsed=111.730ms mgasps=1.128 number=607 hash=d2c073…1621f0 age=7m14s cache=17.84kB ignored=2

编写简单的智能合约,在 remix 下进行调试,并部署在链上进行调用

编写简单的智能合约

编写一个类似与复读机的东西,发送字符串保存,之后复读发送的话。

solidity代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
contract AI {
string box;

function send(string s) public {
box = s;
}

function clear() public{
box = "";
}

function receive() public constant returns (string) {
return box;
}

function greet() public constant returns (string) {
return "hello";
}
}

进行调试部署调用

  1. 部署合约
    部署合约
    进行挖矿后成功部署。

  2. greet
    greet

  3. 发送信息,产生交易,挖矿后交易成功。
    send

  4. 交易成功后更新里面的string,再用receive得到刚刚的消息。
    receive

  5. 清除消息,也需要产生交易,然后挖矿使交易成功。

对交易的字段进行解释

根据以上3的发送消息的交易进行解释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
eth.getTransaction("0xeb7a55e1760ecc986f813c881b76ab12b812bc611f8f6b9cacb69248438b50ec")
{
blockHash: "0x06258165d31d4ea7ed945fa430d8e25f75690c0ae20ca4d2ae8f6de40576d261",
blockNumber: 642,
from: "0x15b97213fb8ee04d2ed53113e41035feae88461e",
gas: 43406,
gasPrice: 1000000000,
hash: "0xeb7a55e1760ecc986f813c881b76ab12b812bc611f8f6b9cacb69248438b50ec",
input: "0x66792ba10000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000ce5bc80e5a78be5a48de8afbb0000000000000000000000000000000000000000",
nonce: 14,
r: "0x3892abe334fbe797ce57155373c9c36860121d143950f873e554dc37321847d",
s: "0x55eea1e92c492bd89f7db381fda08ae795fdc7abdbcff4c98f897cae94edae7c",
to: "0x2eb0f4a6c064c51033dbc5977973bcb6702a9cdd",
transactionIndex: 0,
v: "0x37",
value: 0
}
字段名称 类型 意义
blockHash String 交易所处区块的哈希值
blockNumber Number 当前交易所处在区块的编号
from String 发起交易的账户(地址)
gas Number 完成这笔交易所需要的gas
gasPrice Number 当前每个gas所需的花费
hash String 当前交易的哈希值
input String 部署智能合约交易的16进制代码,合约调用相关的二进制信息
nonce Number 相当于发起人的交易序号,每发送一笔交易,nonce+1,放置交易重复进行
r String 发起人EOA的ECDSA签名的三个组成部分
s String 发起人EOA的ECDSA签名的三个组成部分
to String 目标以太坊地址
transactionIndex Number 交易索引
v String 发起人EOA的ECDSA签名的三个组成部分
values Number 要发送到目的地址的以太币数