docker+geth运行以太坊私有链

霸气de小男生 提交于 2019-12-26 23:59:26

以太坊

基本介绍

官方文档

​ 以太坊(Ethereum)是将比特币中的一些技术和概念运用于计算领域的一项创新。
​ 以太坊利用了很多跟比特币类似的机制(比如区块链技术和 P2P 网络),来维护一个共享的计算平台,这个平台可以灵活且安全地运行用户想要的任何程序(当然也包括类似比特币的区块链程序)。

geth+docker运行以太坊私有链

geth项目地址:https://github.com/ethereum/go-ethereum

1. 拉取geth镜像

go pull ethereum/client-go
[devops@gen dapp]$ docker images
REPOSITORY           TAG    IMAGE ID       CREATED     SIZE
ethereum/client-go   latest dafcf40ec9c0   6 days ago  41.3MB

2. 新建挂载目录及部分文件

[devops@gen dapp]$ pwd
/home/devops/docker-instances/geth/dapp
[devops@gen dapp]$ ls
data  debug.log  genesis.json  init.sh  miner

init.sh文件内容:

#!/bin/sh
#初始化创世区块
geth -datadir  /workspace/dapp/data/ init /workspace/dapp/genesis.json

if [  $# -lt 1 ]; then 
  exec "/bin/sh"
else
  exec /bin/sh -c "$@"
fi
#拷贝旷工私钥 其实第一次启动没有
cp -r /workspace/dapp/miner/data/keystore/* ./data/keystore/

genesis.json文件内容:

{
  "config": {
    "chainId": 9527,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,
    "ethash": {}
  },
  "nonce": "0x0",
  "timestamp": "0x5ddf8f3e",
  "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "gasLimit": "0x47b760",
  "difficulty": "0x00002",
  "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": { }
  },
  "number": "0x0",
  "gasUsed": "0x0",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
参数名称 参数描述
chainId 链类型ID,私链:10,testnet: ,mainnet:
alloc 预置账号以及对应以太币数量,因为私有链挖矿容易,故不需要预置有币的账号,需要的时候自己创建即可以
coinbase 矿工的账号,随便填
difficulty 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度
extraData 附加信息,随便填,可以填你的个性信息
gasLimit 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大
nonce nonce nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件
mixhash 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。
parentHash 上一个区块的hash值(父hash),因为是创世块,所以这个值是0
timestamp 设置创世块的时间戳

3. 启动容器

docker run -d -it --name=geth-miner  -u root -p 8899:8899 --hostname node -v /home/devops/docker-instances/geth:/workspace --privileged=true --entrypoint /workspace/dapp/init.sh ethereum/client-go

–privileged=true 使用该参数,container内的root拥有真正的root权限。

-p 8899:8899 预留的RPC端口

4. 开启挖矿

进入容器

docker exec -it geth-miner sh

切换至工作目录

/workspace/dapp # 
data   debug.log  genesis.json  init.sh  miner 
/workspace/dapp # ls data
geth      history   keystore

启动私有链

geth -datadir ./data/ --networkid 9527 --rpc  --rpcport 8899 --rpcapi admin,eth,miner,web3,personal,net,txpool --allow-insecure-unlock   console 2>> ./debug.log

–networkid 后面的值需要和初始化创世区块(genesis.json)时的chainId一致

eth api

> eth
{
  accounts: [],
  blockNumber: 0,
  coinbase: undefined,
  compile: {
    lll: function(),
    serpent: function(),
    solidity: function()
  },
  defaultAccount: undefined,
  defaultBlock: "latest",
  gasPrice: 1000000000,
  hashrate: 0,
  mining: false,
  pendingTransactions: [],
  protocolVersion: "0x40",
  syncing: false,
  call: function(),
  chainId: function(),
  contract: function(abi),
  estimateGas: function(),
  fillTransaction: function(),
  filter: function(options, callback, filterCreationErrorCallback),
  getAccounts: function(callback),
  getBalance: function(),
  getBlock: function(),
  getBlockByHash: function(),
  getBlockByNumber: function(),
  getBlockNumber: function(callback),
  getBlockTransactionCount: function(),
  getBlockUncleCount: function(),
  getCode: function(),
  getCoinbase: function(callback),
  getCompilers: function(),
  getGasPrice: function(callback),
  getHashrate: function(callback),
  getHeaderByHash: function(),
  getHeaderByNumber: function(),
  getMining: function(callback),
  getPendingTransactions: function(callback),
  getProof: function(),
  getProtocolVersion: function(callback),
  getRawTransaction: function(),
  getRawTransactionFromBlock: function(),
  getStorageAt: function(),
  getSyncing: function(callback),
  getTransaction: function(),
  getTransactionCount: function(),
  getTransactionFromBlock: function(),
  getTransactionReceipt: function(),
  getUncle: function(),
  getWork: function(),
  iban: function(iban),
  icapNamereg: function(),
  isSyncing: function(callback),
  namereg: function(),
  resend: function(),
  sendIBANTransaction: function(),
  sendRawTransaction: function(),
  sendTransaction: function(),
  sign: function(),
  signTransaction: function(),
  submitTransaction: function(),
  submitWork: function()
}

创建第一个地址(默认第一个地址为coinbase账户)

> personal.newAccount("123456")
"0x440272384d9c8d9b77e9cae6314f3ceef105537f

启动挖矿

>miner.start()
null
> eth.blockNumber
9

转账

> eth.accounts
["0x440272384d9c8d9b77e9cae6314f3ceef105537f"]
> personal.newAccount("123456")
"0x9528d98b43c302627dd84961c85ee8025d63e0ed"
> eth.accounts
["0x440272384d9c8d9b77e9cae6314f3ceef105537f","0x9528d98b43c302627dd84961c85ee8025d63e0ed"]
> eth.getBalance("0x440272384d9c8d9b77e9cae6314f3ceef105537f")
1.234e+21
> eth.getBalance(eth.accounts[0])
1.234e+21
> eth.getBalance(eth.accounts[1])
0
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(12, "ether")})
Error: authentication needed: password or unlock
    at web3.js:3143:20
    at web3.js:6347:15
    at web3.js:5081:36
    at <anonymous>:1:1

账户未解锁,解锁后继续

> personal.unlockAccount(eth.accounts[0], "123456")
true
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(12, "ether")})
"0xf057b8438e719f893e3aab303ad2efd07dcefd9d966f81bb8fa034f560c06646"
>
>

查询交易

> eth.getBalance(eth.accounts[1])
12000000000000000000
> eth.getTransaction("0xf057b8438e719f893e3aab303ad2efd07dcefd9d966f81bb8fa034f560c06646")
{
  blockHash: "0xaf8d935aa6d982e4bbaaa14c19adf1d2bf1a4253dbbfc90b9be5dafaed651814",
  blockNumber: 618,
  from: "0x440272384d9c8d9b77e9cae6314f3ceef105537f",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0xf057b8438e719f893e3aab303ad2efd07dcefd9d966f81bb8fa034f560c06646",
  input: "0x",
  nonce: 0,
  r: "0xa399cc7a1328822a486c0132f1cc40cc57219a3c276afa58914c453665e343a2",
  s: "0x48f128075625161db47fd40f27966fffb7f6b8b09e7cb545b4d111b2db09bec9",
  to: "0x9528d98b43c302627dd84961c85ee8025d63e0ed",
  transactionIndex: 0,
  v: "0x558",
  value: 12000000000000000000
}

以太坊币单位

单位 wei值
wei 1
Kwei (babbage) 1e3 wei
Mwei (lovelace) 1e6 wei
Gwei (shannon) 1e9 wei
microether (szabo) 1e12 wei
milliether (finney) 1e15 wei
ether 1e18 wei

合约相关待续

**

参考资料

简书上docker运行geth

https://blog.csdn.net/u010159567/article/details/83865823

https://blog.csdn.net/weixin_39571268/article/details/103612084

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!