0%

区块链初探

使用 docker 搭建私有链

准备工作

创建创世区块文件/dapp/private_chain/genesis.json

{
"config": {
"chainId": 008,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0
},
"alloc": {
"0x0000000000000000000000000000000000000001": {
"balance": "100000000000000000000000"
},
"0x0000000000000000000000000000000000000002": {
"balance": "200000000000000000000000"
}
},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x2000",
"extraData": "0x",
"gasLimit": "0x800000000000",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}

chainId: 私有区块链的 ID
homesteadBlock:Homestead 是以太坊平台的第二个主要版本,也是以太坊的第一个生产版本。它包括几个协议更改。由于我们已经在 Homestead 版本,因此该属性为 0。
eip155Block/eip158Block:Homestead 版本发布时带有一些向后不兼容的协议更改,因此需要硬分叉。通过以太坊改进提案(EIPs)提出的这些协议变更/改进。然而,我们的链条不会因为这些变化而难以分解,所以保留为 0。
difficulty:此值用于控制区块链的块生成时间。难度越高,Miner 在发现有效块时必须执行的统计更多计算。在我们的测试网络中,我们将保持此值低以避免在测试期间等待,因为需要生成有效块来执行交易处理区块链。
gasLimit:此值指定每块的“gas”支出的当前链范围限制。gas 是以太坊在交易过程中消耗的燃料。我们将在这种情况下将此值标记得足够高,以避免在测试期间受到限制。
alloc:这是你可以创建你的钱包并用假 ether 预填充的地方。注这里的单位是 wei,1ETH=10^18wei,同下面获取余额单位。
coinbase:矿工的账号

获取 geth 镜像

docker pull ethereum/client-go:latest

运行私有链

docker run \
-itd \
--name geth \
-v /etc/localtime:/etc/localtime \
-v /etc/timezone:/etc/timezone \
-v /dapp/private_chain/genesis.json:/dapp/genesis.json \
-v /dapp/private_chain:/dapp \
-p 8545:8545 \
-p 30303:30303 \
-p 30303:30303/udp \
--entrypoint /bin/sh \
ethereum/client-go:latest \

进入容器

docker exec -it geth sh

容器内 ethereum 环境操作

  • 初始化 genesis
geth init --datadir /dapp /dapp/genesis.json
  • 启动私有链 进入 geth 控制台
geth --identity "miner_node" --datadir /dapp --networkid 88 --rpc --rpccorsdomain "*" --rpcaddr 0.0.0.0 --rpcport "8545" --port "30303" --rpcapi "db,eth,net,web3" --nodiscover console
  • 创建账户
# 创建账号
# 输入密码
# 获得账号地址

>personal.newAccount()

Passphrase:
Repeat passphrase:

INFO [01-19|06:22:35.833] Your new key was generated address=0x046c9A712c6c82579cf4F05379b157BAba760e6E
WARN [01-19|06:22:35.833] Please backup your key file! path=/root/privtedata/keystore/UTC--2021-01-18T22-22-32.826039142Z--046c9a712c6c82579cf4f05379b157baba760e6e
WARN [01-19|06:22:35.833] Please remember your password!
"0x19fAe8328f9CD2869153a6fBF898ab6713337641"

注:此时在/dapp/private_chain/keystore 下面可以看到生成的 keystore,需保存好密码及 keystore

  • 查看余额
> eth.getBalance("0x19fAe8328f9CD2869153a6fBF898ab6713337641")
0
>

此时余额为零

  • 获取以太币-挖矿
> miner.start()

INFO [01-19|06:25:02.328] Updated mining threads threads=8
INFO [01-19|06:25:02.328] Transaction pool price threshold updated price=1000000000
INFO [01-19|06:25:02.328] Etherbase automatically configured address=0x046c9A712c6c82579cf4F05379b157BAba760e6E
null
> INFO [01-19|06:25:02.329] Commit new mining work number=1 sealhash="a14903…a8920b" uncles=0 txs=0 gas=0 fees=0 elapsed="353.38µs"
INFO [01-19|06:25:04.318] Generating DAG in progress epoch=0 percentage=0 elapsed=1.055s
INFO [01-19|06:25:05.398] Generating DAG in progress epoch=0 percentage=1 elapsed=2.135s
INFO [01-19|06:25:06.452] Generating DAG in progress epoch=0 percentage=2 elapsed=3.189s
INFO [01-19|06:25:07.727] Generating DAG in progress epoch=0 percentage=3 elapsed=4.464s
INFO [01-19|06:25:08.802] Generating DAG in progress epoch=0 percentage=4 elapsed=5.539s
INFO [01-19|06:25:09.920] Generating DAG in progress epoch=0 percentage=5 elapsed=6.657s
INFO [01-19|06:25:11.077] Generating DAG in progress epoch=0 percentage=6 elapsed=7.814s
INFO [01-19|06:25:12.366] Generating DAG in progress epoch=0 percentage=7 elapsed=9.103s
INFO [01-19|06:25:13.857] Generating DAG in progress epoch=0 percentage=8 elapsed=10.595s
INFO [01-19|06:25:14.963] Generating DAG in progress epoch=0 percentage=9 elapsed=11.700s
INFO [01-19|06:25:16.023] Generating DAG in progress epoch=0 percentage=10 elapsed=12.760s
INFO [01-19|06:25:17.370] Generating DAG in progress epoch=0 percentage=11 elapsed=14.107s
INFO [01-19|06:25:18.688] Generating DAG in progress epoch=0 percentage=12 elapsed=15.425s
INFO [01-19|06:25:19.994] Generating DAG in progress epoch=0 percentage=13 elapsed=16.731s
INFO [01-19|06:25:21.245] Generating DAG in progress epoch=0 percentage=14 elapsed=17.983s
INFO [01-19|06:25:22.474] Generating DAG in progress epoch=0 percentage=15 elapsed=19.211s
  • 挖到矿了
    直到 percentage 到 100 时,计算完毕就挖到矿了

INFO [01-19|06:31:50.709] Generating DAG in progress epoch=1 percentage=97 elapsed=4m29.670s
INFO [01-19|06:31:54.026] Generating DAG in progress epoch=1 percentage=98 elapsed=4m32.987s
INFO [01-19|06:31:57.452] Generating DAG in progress epoch=1 percentage=99 elapsed=4m36.414s

INFO [01-19|06:31:57.479] Generated ethash verification cache epoch=1 elapsed=4m36.420s
INFO [01-19|06:34:45.039] Successfully sealed new block number=1 sealhash="a14903…a8920b" hash="9ff2b2…bb5c8b" elapsed=9m42.710s
INFO [01-19|06:34:45.076] 🔨 mined potential block number=1 hash="9ff2b2…bb5c8b"
INFO [01-19|06:34:45.056] Commit new mining work number=2 sealhash="e438e6…ff51c4" uncles=0 txs=0 gas=0 fees=0 elapsed=17.093ms
INFO [01-19|06:34:45.141] Successfully sealed new block number=2 sealhash="e438e6…ff51c4" hash="d70210…3bca0d" elapsed=84.626ms
INFO [01-19|06:34:45.141] 🔨 mined potential block number=2 hash="d70210…3bca0d"
INFO [01-19|06:34:45.141] Commit new mining work number=3 sealhash="6a98b3…4bdc80" uncles=0 txs=0 gas=0 fees=0 elapsed="367.472µs"
INFO [01-19|06:34:51.295] Successfully sealed new block number=3 sealhash="6a98b3…4bdc80" hash="78d6cb…c033f5" elapsed=6.153s
INFO [01-19|06:34:51.324] 🔨 mined potential block number=3 hash="78d6cb…c033f5"
INFO [01-19|06:34:51.296] Commit new mining work number=4 sealhash="51be0d…46f2e8" uncles=0 txs=0 gas=0 fees=0 elapsed="642.359µs"
INFO [01-19|06:35:02.689] Successfully sealed new block number=4 sealhash="51be0d…46f2e8" hash="6086ce…98c928" elapsed=11.393s
INFO [01-19|06:35:02.828] 🔨 mined potential block number=4 hash="6086ce…98c928"
INFO [01-19|06:35:02.824] Commit new mining work number=5 sealhash="297750…ece4fb" uncles=0 txs=0 gas=0 fees=0 elapsed=134.563ms
INFO [01-19|06:35:12.483] Successfully sealed new block number=5 sealhash="297750…ece4fb" hash="addb2f…85c1ac" elapsed=9.659s
INFO [01-19|06:35:12.527] 🔨 mined potential block number=5 hash="addb2f…85c1ac"
INFO [01-19|06:35:12.500] Commit new mining work number=6 sealhash="284733…e3cbab" uncles=0 txs=0 gas=0 fees=0 elapsed=16.081ms


> eth.getBalance("0x19fAe8328f9CD2869153a6fBF898ab6713337641")
96000000000000000000
>

MetaMask

安装 MeataMask 可以切换网络至 http://localhost:8545,账户是用 JSON 文件导入,导入 keystore,输入密码,可以实时查询余额

Web3.js

const Web3 = require("web3");
// 连接私有链
const web3 = new Web3("http://localhost:8545");

// 查询账户
web3.eth.getAccounts((error, res) => {
if (!error) {
console.log(res);
}
});

// 查询余额
web3.eth
.getBalance("0x19fAe8328f9CD2869153a6fBF898ab6713337641")
.then(console.log);

附录

密码

密码不是私钥,是创建账户的密码

私钥

私钥由 64 位长度的十六进制的字符组成,私钥可以生成公钥,再生成地址,都不可逆。

助记词

助记词是私钥的另外一种展现形式,可以生成私钥,但不可逆。

keystore

将私钥已加密的形式保存为一份 JSON 文件,配合密码可以操作账户。

如何解锁账户

解锁账户有如下几种方式:

  • 私钥(Private Key)
  • Keystore+密码(Keystore+Password)
  • 助记词(Mnemonic code)

我们可以得到以下总结:

  • 通过私钥+密码可以生成 keystore,即加密私钥。
  • 通过 keystore+密码可以获取私钥,即解密 keystore。
  • 通过助记词根据不同的路径获取不同的私钥,即使用 HD 钱包将助记词转化成种子来生成主私钥,然后派生海量的子私钥和地址。