以太坊私有链的搭建和运行

一,新建工作目录并创建创世区块配置文件

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

gensis.json为创世区块配置文件,以下为内容

{
    "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指定了独立的区块链网络ID。网络ID在链接到其他节点的时候会用到,以太坊公网的网络ID是1,为了不与公有链网络冲突,要指定自己的网络ID。不同ID网络的节点无法相互连接。

二,初始化

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

再看工作目录,生成了keystore和geth目录,其中,chaindata存放区块数据,keystore存放账户数据

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

三,启动节点

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

参数datadir指定当前区块链私钥和网络数据存放位置,console表示启动命令行模式,可以在geth中执行命令

四,进入JavaScript控制台

通过attach命令,连接一个已经启动的节点,启动Js命令环境

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

在以太坊的JavaScript控制台中内置了一些对象,通过这些对象我们可以方便地与以太坊交互,
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()
}

net:提供查看p2p网络相关方法

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

admin:提供管理节点相关方法

> 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:提供查看交易内存池方法

> 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"
  }
}

以上操作分别是解锁账户,发送交易,查看账户余额,查看交易池状态,查看pending交易详情
接下来挖矿打包交易

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

欸,发现交易成功了!!!
还可以通过交易hash查看发起交易时交易详情和交易被打包进区块时的交易详情

> 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,查看区块相关信息

> 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: []
}

以上操作分别是查询区块总数,查询最新区块,查询Number为0区块
4,远程节点管理
查看节点信息

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

添加其他节点,必须保证网络相同和相同的networkid

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

查看已连接的远程节点

> admin.peers
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,080评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,422评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,630评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,554评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,662评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,856评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,014评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,752评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,212评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,541评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,687评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,347评论 4 331
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,973评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,777评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,006评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,406评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,576评论 2 349

推荐阅读更多精彩内容