以太坊私有鏈的搭建和運行

一佳镜,新建工作目錄并創(chuàng)建創(chuàng)世區(qū)塊配置文件

$ mkdir private-geth
$ cd private-geth/
$ gedit gensis.json

gensis.json為創(chuàng)世區(qū)塊配置文件慕嚷,以下為內(nèi)容

{
    "config": { 
        "chainId": 10,
        "homesteadBlock": 0,
        "eip155Block": 0, 
        "eip158Block": 0 
    }, 
    "coinbase" : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x66",
    "extraData" : "",
    "gasLimit" : "0xffffffff", 
    "nonce" : "0x0000000000000001",
    "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 
    "timestamp" : "0x00", 
    "alloc" : {}
}

其中chainId指定了獨立的區(qū)塊鏈網(wǎng)絡ID洼冻。網(wǎng)絡ID在鏈接到其他節(jié)點的時候會用到杖玲,以太坊公網(wǎng)的網(wǎng)絡ID是1办素,為了不與公有鏈網(wǎng)絡沖突诞帐,要指定自己的網(wǎng)絡ID欣尼。不同ID網(wǎng)絡的節(jié)點無法相互連接。

二停蕉,初始化

$ geth --datadir "./db" init gensis.json

再看工作目錄愕鼓,生成了keystore和geth目錄,其中慧起,chaindata存放區(qū)塊數(shù)據(jù)菇晃,keystore存放賬戶數(shù)據(jù)

zhujiantao@ubuntu:~/eth/private-geth$ tree ../private-geth/
../private-geth/
├── db
│   ├── geth
│   │   ├── chaindata
│   │   │   ├── 000001.log
│   │   │   ├── CURRENT
│   │   │   ├── LOCK
│   │   │   ├── LOG
│   │   │   └── MANIFEST-000000
│   │   └── lightchaindata
│   │       ├── 000001.log
│   │       ├── CURRENT
│   │       ├── LOCK
│   │       ├── LOG
│   │       └── MANIFEST-000000
│   └── keystore
└── gensis.json

三,啟動節(jié)點

$ geth --datadir "./db" --networkid 10 console

參數(shù)datadir指定當前區(qū)塊鏈私鑰和網(wǎng)絡數(shù)據(jù)存放位置蚓挤,console表示啟動命令行模式磺送,可以在geth中執(zhí)行命令

四,進入JavaScript控制臺

通過attach命令灿意,連接一個已經(jīng)啟動的節(jié)點估灿,啟動Js命令環(huán)境

$ geth --datadir './db' attach ipc:./db/geth.ipc 

在以太坊的JavaScript控制臺中內(nèi)置了一些對象,通過這些對象我們可以方便地與以太坊交互缤剧,
eth:提供操作區(qū)塊鏈相關(guān)方法

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

net:提供查看p2p網(wǎng)絡相關(guān)方法

> net
{
  listening: true,
  peerCount: 0,
  version: "10",
  getListening: function(callback),
  getPeerCount: function(callback),
  getVersion: function(callback)
}

admin:提供管理節(jié)點相關(guān)方法

> admin
{
  datadir: "/home/zhujiantao/eth/private-geth/db",
  nodeInfo: {
    enode: "enode://1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f@[::]:30303",
    id: "1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f",
    ip: "::",
    listenAddr: "[::]:30303",
    name: "Geth/v1.8.12-stable-37685930/linux-amd64/go1.10.1",
    ports: {
      discovery: 30303,
      listener: 30303
    },
    protocols: {
      eth: {
        config: {...},
        difficulty: 102,
        genesis: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
        head: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
        network: 10
      }
    }
  },
  peers: [],
  addPeer: function(),
  clearHistory: function(),
  exportChain: function(),
  getDatadir: function(callback),
  getNodeInfo: function(callback),
  getPeers: function(callback),
  importChain: function(),
  removePeer: function(),
  sleep: function github.com/ethereum/go-ethereum/console.(*bridge).Sleep-fm(),
  sleepBlocks: function github.com/ethereum/go-ethereum/console.(*bridge).SleepBlocks-fm(),
  startRPC: function(),
  startWS: function(),
  stopRPC: function(),
  stopWS: function()
}

miner:提供啟動和停止挖礦方法

> miner
{
  getHashrate: function(),
  setEtherbase: function(),
  setExtra: function(),
  setGasPrice: function(),
  start: function(),
  stop: function()
}
personal:提供管理賬戶方法
> personal
{
  listAccounts: [],
  listWallets: [],
  deriveAccount: function(),
  ecRecover: function(),
  getListAccounts: function(callback),
  getListWallets: function(callback),
  importRawKey: function(),
  lockAccount: function(),
  newAccount: function github.com/ethereum/go-ethereum/console.(*bridge).NewAccount-fm(),
  openWallet: function github.com/ethereum/go-ethereum/console.(*bridge).OpenWallet-fm(),
  sendTransaction: function(),
  sign: function github.com/ethereum/go-ethereum/console.(*bridge).Sign-fm(),
  signTransaction: function(),
  unlockAccount: function github.com/ethereum/go-ethereum/console.(*bridge).UnlockAccount-fm()
}

txpool:提供查看交易內(nèi)存池方法

> txpool
{
  content: {
    pending: {},
    queued: {}
  },
  inspect: {
    pending: {},
    queued: {}
  },
  status: {
    pending: 0,
    queued: 0
  },
  getContent: function(callback),
  getInspect: function(callback),
  getStatus: function(callback)
}

web3:除了以上對象中有的方法馅袁,還包含一些單位換算等其他方法

五,通過命令進行簡單交互

1荒辕,新建賬戶并挖礦

> eth.accounts
[]
> personal.newAccount("zhujiantao")
"0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121"
> personal.newAccount("hejiawei")
eth"0x86322ea53a15460f83a43ea3366974af37897e7c"
> eth.accounts
["0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121", "0x86322ea53a15460f83a43ea3366974af37897e7c"]
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
> miner.setEtherbase(eth.accounts[0])
true
> eth.coinbase
"0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121"
> miner.start(2)

2汗销,解鎖賬戶并交易

> personal.unlockAccount(eth.accounts[0], "zhujiantao")
true
> personal.unlockAccount(eth.accounts[1], "hejiawei")
true
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:web3.toWei(1,"ether")})
"0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006"
> eth.getBalance(eth.accounts[0])
50000000000000000000
> eth.getBalance(eth.accounts[1])
0
> txpool.status
{
  pending: 1,
  queued: 0
}
> txpool.inspect.pending
{
  0x49b70C9F35FBD0b39Fd98418BFB9DC25e6e08121: {
    0: "0x86322ea53A15460f83A43ea3366974AF37897e7c: 1000000000000000000 wei + 90000 gas × 18000000000 wei"
  }
}

以上操作分別是解鎖賬戶,發(fā)送交易抵窒,查看賬戶余額弛针,查看交易池狀態(tài),查看pending交易詳情
接下來挖礦打包交易

> miner.start(2);admin.sleepBlocks(1);miner.stop();
交易結(jié)束后查看余額
> eth.getBalance(eth.accounts[0])
54000000000000000000
> eth.getBalance(eth.accounts[1])
1000000000000000000

欸估脆,發(fā)現(xiàn)交易成功了G辗堋!!
還可以通過交易hash查看發(fā)起交易時交易詳情和交易被打包進區(qū)塊時的交易詳情

> eth.getTransaction("0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006")
{
  blockHash: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
  blockNumber: 11,
  from: "0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006",
  input: "0x",
  nonce: 0,
  r: "0x60b4d86aaa5edd3fbfced5ec713ad2031df60496e12384b0a2a9106382aaa137",
  s: "0x1b792f70cbbb42aacc05032794cfb3be8168ef2e75f1e263db62a61cfeaf4e5a",
  to: "0x86322ea53a15460f83a43ea3366974af37897e7c",
  transactionIndex: 0,
  v: "0x38",
  value: 1000000000000000000
}
> eth.getTransactionReceipt("0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006")
{
  blockHash: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
  blockNumber: 11,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  from: "0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0xc7b07a186147f10aea58412ec75d25412248f6d1d3335291d74794646900660e",
  to: "0x86322ea53a15460f83a43ea3366974af37897e7c",
  transactionHash: "0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006",
  transactionIndex: 0
}

3付材,查看區(qū)塊相關(guān)信息

> eth.blockNumber
11
> eth.getBlock("latest")
{
  difficulty: 131072,
  extraData: "0xd88301080c846765746888676f312e31302e31856c696e7578",
  gasLimit: 4249054591,
  gasUsed: 21000,
  hash: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121",
  mixHash: "0xa9d585a50a989192e2c239f97f24e3283f13855f21c364bafebe531ba2bc0fc7",
  nonce: "0x5ae360db4d1b56f7",
  number: 11,
  parentHash: "0x3470ce4807e6f1aba46fed960f3306526eac3085eb425b9e4cc74aa31fb1a14e",
  receiptsRoot: "0x084e81eec67a61b4342ab5698d5798aadf822392a9579535d839d30e30388f85",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 651,
  stateRoot: "0x701abf15ddb892bcdfc3f2127984506602db25c98c74321f2f50128b97d68c03",
  timestamp: 1532233119,
  totalDifficulty: 1443686,
  transactions: ["0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006"],
  transactionsRoot: "0x932fd9feba26c3996a34a7a8b0d74f65826671921d181d7aadac5a3d37992c24",
  uncles: []
}
> eth.getBlock(0)
{
  difficulty: 102,
  extraData: "0x",
  gasLimit: 4294967295,
  gasUsed: 0,
  hash: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x0000000000000000000000000000000000000000",
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: "0x0000000000000001",
  number: 0,
  parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 505,
  stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  timestamp: 0,
  totalDifficulty: 102,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

以上操作分別是查詢區(qū)塊總數(shù)朦拖,查詢最新區(qū)塊,查詢Number為0區(qū)塊
4厌衔,遠程節(jié)點管理
查看節(jié)點信息

> admin.nodeInfo
{
  enode: "enode://1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f@[::]:30303",
  id: "1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f",
  ip: "::",
  listenAddr: "[::]:30303",
  name: "Geth/v1.8.12-stable-37685930/linux-amd64/go1.10.1",
  ports: {
    discovery: 30303,
    listener: 30303
  },
  protocols: {
    eth: {
      config: {
        chainId: 10,
        eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        eip155Block: 0,
        eip158Block: 0,
        homesteadBlock: 0
      },
      difficulty: 1443686,
      genesis: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
      head: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
      network: 10
    }
  }
}

添加其他節(jié)點璧帝,必須保證網(wǎng)絡相同和相同的networkid

> admin.addPeer("enode://1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f@[192.168.81.128]:30303")
true

查看已連接的遠程節(jié)點

> admin.peers
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市富寿,隨后出現(xiàn)的幾起案子睬隶,更是在濱河造成了極大的恐慌,老刑警劉巖页徐,帶你破解...
    沈念sama閱讀 211,290評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苏潜,死亡現(xiàn)場離奇詭異,居然都是意外死亡变勇,警方通過查閱死者的電腦和手機恤左,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搀绣,“玉大人飞袋,你說我怎么就攤上這事×椿迹” “怎么了巧鸭?”我有些...
    開封第一講書人閱讀 156,872評論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長麻捻。 經(jīng)常有香客問我纲仍,道長,這世上最難降的妖魔是什么芯肤? 我笑而不...
    開封第一講書人閱讀 56,415評論 1 283
  • 正文 為了忘掉前任巷折,我火速辦了婚禮,結(jié)果婚禮上崖咨,老公的妹妹穿的比我還像新娘锻拘。我一直安慰自己,他們只是感情好击蹲,可當我...
    茶點故事閱讀 65,453評論 6 385
  • 文/花漫 我一把揭開白布署拟。 她就那樣靜靜地躺著,像睡著了一般歌豺。 火紅的嫁衣襯著肌膚如雪推穷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,784評論 1 290
  • 那天类咧,我揣著相機與錄音馒铃,去河邊找鬼蟹腾。 笑死,一個胖子當著我的面吹牛区宇,可吹牛的內(nèi)容都是我干的娃殖。 我是一名探鬼主播,決...
    沈念sama閱讀 38,927評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼议谷,長吁一口氣:“原來是場噩夢啊……” “哼炉爆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起卧晓,我...
    開封第一講書人閱讀 37,691評論 0 266
  • 序言:老撾萬榮一對情侶失蹤芬首,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后逼裆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體郁稍,經(jīng)...
    沈念sama閱讀 44,137評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,472評論 2 326
  • 正文 我和宋清朗相戀三年胜宇,在試婚紗的時候發(fā)現(xiàn)自己被綠了艺晴。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,622評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡掸屡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出然评,到底是詐尸還是另有隱情仅财,我是刑警寧澤,帶...
    沈念sama閱讀 34,289評論 4 329
  • 正文 年R本政府宣布碗淌,位于F島的核電站盏求,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏亿眠。R本人自食惡果不足惜碎罚,卻給世界環(huán)境...
    茶點故事閱讀 39,887評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望纳像。 院中可真熱鬧荆烈,春花似錦、人聲如沸竟趾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽岔帽。三九已至玫鸟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間犀勒,已是汗流浹背屎飘。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工妥曲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钦购。 一個月前我還...
    沈念sama閱讀 46,316評論 2 360
  • 正文 我出身青樓檐盟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親肮雨。 傳聞我的和親對象是個殘疾皇子遵堵,可洞房花燭夜當晚...
    茶點故事閱讀 43,490評論 2 348

推薦閱讀更多精彩內(nèi)容