JobPlus知识库 IT 大数据 文章
以太坊私有链搭建教程

geth客户端安装


- 方法一:Installing from PPA

sudo apt-get install software-properties-common

sudo add-apt-repository -y ppa:ethereum/ethereum

 sudo apt-get update

 sudo apt-get install ethereum


  • 方法二:Building from source

前提条件是已经安装好go

git clone https://github.com/ethereum/go-ethereum

sudo apt-get install -y build-essential golang

 cd go-ethereum

make geth


搭建私有链

  • 准备创世区块 
    新建文件夹名字位置自己选(比如eth),eth目录下新建.json格式文件(比如eth.json)。文件内容如下 
    如果你的json文件内容不是如下,后续可能会报错。

{

  "config":

 {

        "chainId": 15,

        "homesteadBlock": 0,

        "eip155Block": 0,

        "eip158Block": 0

    }, 

    "coinbase" : "0x0000000000000000000000000000000000000000",

    "difficulty" : "0x40000",

    "extraData" : "",

    "gasLimit" : "0xffffffff",

    "nonce" : "0x0000000000000042",

    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 

    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",

    "timestamp" : "0x00",

    "alloc": { }

}


  • 写入创世区块 
    eth文件夹下新建一个data目录

eth

  ├── data

  └── eth.json


eth目录下打开终端输入:

geth --datadir data init eth.json


–datadir data指定数据存储位置在data目录下,以eth.json文件初始化区块数据。

提示:如前面所说,如果参考其他人的教程json文件内容和本教程不一致,可能这步会报错! 
初始化成功后会在目录结构如下。其中geth/chaindata中存放的是区块数据,keystore中存放的是账户数据。

eth

   ├── data

   │   ├── geth

   │   │   ├── chaindata

   │   │   │   ├── 000001.log

   │   │   │   ├── CURRENT

   │   │   │   ├── LOCK

   │   │   │   ├── LOG

   │   │   │   └── MANIFEST-000000

   │   │   └── lightchaindata

   │   │       ├── 000001.log

   │   │       ├── CURRENT

   │   │       ├── LOCK

   │   │       ├── LOG

   │   │       └── MANIFEST-000000

   │   └── keystore

   └── eth.json


  • 启动私有链节点

geth --datadir data console 2>console.log

Welcome to the Geth JavaScript console!

instance: Geth/v1.8.7-stable-66432f38/linux-amd64/go1.10.1

modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0


geth console,表示启动节点并进入交互式控制台,–datadir选项指定使用data作为数据目录,–networkid选项后面跟一个数字,这里是110,表示指定这个私有链的网络id为110。网络id在连接到其他节点的时候会用到,以太坊公网的网络id是1,为了不与公有链网络冲突,运行私有链节点的时候要指定自己的网络id。 
等待数分钟,启动成功后进入命令行交互。

测试

  • 1

输入eth:

> eth

 {

  accounts: [],

  blockNumber: 0,

  coinbase: undefined,

  compile: {

    lll: function(),

    serpent: function(),

    solidity: function()

  },

  defaultAccount: undefined,

  defaultBlock: "latest",

  gasPrice: 18000000000,

  hashrate: 0,

  mining: false,

  pendingTransactions: [],

  protocolVersion: "0x3f",

  syncing: false,

  call: function(),

  contract: function(abi),

  estimateGas: function(),

  filter: function(options, callback, filterCreationErrorCallback),

  getAccounts: function(callback),

  getBalance: function(),

  getBlock: function(),

  getBlockNumber: function(callback),

  getBlockTransactionCount: function(),

  getBlockUncleCount: function(),

  getCode: function(),

  getCoinbase: function(callback),

  getCompilers: function(),

  getGasPrice: function(callback),

  getHashrate: function(callback),

  getMining: function(callback),

  getPendingTransactions: function(callback),

  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()

}


  • 查看账户

>eth.account

 []


  • 新建账户

按提示输入密码

 >

 personal.newAccount()

Passphrase: 

Repeat passphrase:

 "0xcf4d7713eb0e26d372308f320617095f25771b37"


  • 查看账户余额

>eth.getBalance(eth.accounts[0])

0


  • 挖矿相关 
    输入miner查看挖矿相关命令

> miner

 {

  getHashrate: function(), 

  setEtherbase: function(),

  setExtra: function(),

  setGasPrice: function(),

  start: function(),

  stop: function()

}


挖矿

> miner.start(1)


其中start的参数表示挖矿使用的线程数。第一次启动挖矿会先生成挖矿所需的DAG文件,这个过程有点慢,等进度达到100%后,就会开始挖矿,此时屏幕会被挖矿信息刷屏。 
如果想停止挖矿,在js console中输入miner.stop():

  • 发送交易

eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})



如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
130人赞 举报
分享到
用户评价(1)
扫码APP

扫描使用APP

扫码使用

扫描使用小程序