以太坊搭建私有链

1.关于私有链

在以太坊上部署智能合约、发起交易需要花费以太币。而私链和公有链没有关系,不用同步大量的数据,也不用花钱购买以太币,可以很好的满足智能合约的开发和测试要求,而且在私有链上开发的智能合约可以很容易的部署到以太坊的公有链上。

2.开发环境

2.1 安装Go compiler

安装命令:

$ axel https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz  
$ tar -C /usr/local -zxvf go1.9.linux-amd64.tar.gz  
$ mkdir -p ~/work/golang/src  
$ echo "export GOPATH=$HOME/work/golang" >> ~/.bashrc  
$ echo "export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin" >> ~/.bashrc  
$ source ~/.bashrc  
$ go version 

2.2 安装Go-Ethereum

Go-Ethereum是由以太坊基金会提供的官方客户端软件。它是用Go编程语言编写的,简称Geth

github地址:https://github.com/ethereum/go-ethereum

安装过程:

$ git clone https://github.com/ethereum/go-ethereum.git
$ cd  go-ethereum
$ git checkout v1.7.2
$ make geth
$ make 

$ make
build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/go/bin/go install -ldflags -X main.gitCommit=1db4ecdc0b9e828ff65777fb466fc7c1d04e0de9 -v ./cmd/geth
Done building.
Run "/home/fc/work/geth/go-ethereum/build/bin/geth" to launch geth.

2.3 安装solidity的语言包

$ sudo apt-get install solidity


$ sudo npm install -g solc

3.建立私有链

3.1 创建储存私有链数据的文件夹

$ mkdir privatechain

3.2 使用geth来加载

$ ~/work/geth/go-ethereum/build/bin/geth --networkid 123 --dev --datadir data1 --rpc --rpcaddr 192.168.1.102 --rpcport 8989 --port 3000

各项参数含义:

  • --identityid:指定节点ID
  • --dev:开发环境
  • --datadir:指定区块链数据存放的位置
  • --rpc: 开启HTTP-RPC服务
  • --rpcaddr:HTTP-RPC的ip地址
  • --rpcport:指定HTTP-RPC服务器的端口地址(默认为:8545)
  • --port:指定其他节点连接时所用的端口好(默认为30303)
  • --nodiscover:关闭节点发现机制,防止加入有同样初始配置的陌生节点

具体操作:

$ ~/work/geth/go-ethereum/build/bin/geth --networkid 123 --dev --datadir data1 --rpc --rpcaddr 192.168.1.102 --rpcport 8989 --port 3000
WARN [07-08|13:40:48] No etherbase set and no accounts found as default
INFO [07-08|13:40:48] Starting peer-to-peer node               instance=Geth/v1.7.2-stable-1db4ecdc/linux-amd64/go1.9
INFO [07-08|13:40:48] Allocated cache and file handles         database=/home/fc/work/geth/privatechain/data1/geth/chaindata cache=128 handles=1024
INFO [07-08|13:40:48] Writing custom genesis block
INFO [07-08|13:40:48] Initialised chain configuration          config="{ChainID: 1337 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Engine: ethash}"
WARN [07-08|13:40:48] Ethash used in test mode
INFO [07-08|13:40:48] Initialising Ethereum protocol           versions="[63 62]" network=123
INFO [07-08|13:40:48] Loaded most recent local header          number=0 hash=e5be92…38f3bc td=131072
INFO [07-08|13:40:48] Loaded most recent local full block      number=0 hash=e5be92…38f3bc td=131072
INFO [07-08|13:40:48] Loaded most recent local fast block      number=0 hash=e5be92…38f3bc td=131072
INFO [07-08|13:40:48] Regenerated local transaction journal    transactions=0 accounts=0
INFO [07-08|13:40:48] Starting P2P networking
INFO [07-08|13:40:48] started whisper v.5.0
INFO [07-08|13:40:48] RLPx listener up                         self="enode://88c8d51604ea22a813fe8fc28d6d45bb596e4770db7cd5fcaa0805358309271cda50249a5b454c122c8166b7ef50291683d5f6fbdc22e2a2767e35979b825a55@[::]:41891?discport=0"
INFO [07-08|13:40:48] IPC endpoint opened: /home/fc/work/geth/privatechain/data1/geth.ipc
INFO [07-08|13:40:48] HTTP endpoint opened: http://192.168.1.102:8989
INFO [07-08|13:40:50] Mapped network port                      proto=tcp extport=41891 intport=41891 interface="UPNP IGDv1-IP1"

打开一个新的终端输入打开geth终端

$ cd ~/work/geth/privatechain/data1
$ ~/work/geth/go-ethereum/build/bin/geth attach ipc:geth.ipc
$ ~/work/geth/go-ethereum/build/bin/geth attach ipc:geth.ipc
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.2-stable-1db4ecdc/linux-amd64/go1.9
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0

>
>

geth终端打开后便可以进行相关操作:

  • 查看当前账户列表:personal.listAccounts
  • 新建账号(account为账户密码):personal.newAccount("account1")
  • 查看某一个账户:personal.listAccounts[0]
>
> personal.listAccounts
[]
> personal.newAccount("account1")
"0x6c4ac6e04d033b050f08158fcde28d341e297aae"
> personal.listAccounts
["0x6c4ac6e04d033b050f08158fcde28d341e297aae"]
> personal.newAccount("account2")
"0x2a7760e97d3142088b7419d0e1e9223946ab03de"
> personal.listAccounts
["0x6c4ac6e04d033b050f08158fcde28d341e297aae", "0x2a7760e97d3142088b7419d0e1e9223946ab03de"]
> personal.listAccounts[0]
"0x6c4ac6e04d033b050f08158fcde28d341e297aae"
> personal.listAccounts[1]
"0x2a7760e97d3142088b7419d0e1e9223946ab03de"
  • 查看账户余额:eth.getBalance("0x6c4ac6e04d033b050f08158fcde28d341e297aae")
  • 开始挖矿: miner.start()
  • 停止挖矿: miner.stop()
> eth.getBalance("0x6c4ac6e04d033b050f08158fcde28d341e297aae")
0
> miner.start()
null
> miner.stop()
true
> eth.getBalance("0x6c4ac6e04d033b050f08158fcde28d341e297aae")
96000000000000000000
> eth.getBalance("0x2a7760e97d3142088b7419d0e1e9223946ab03de")
0

下面进行转账操作:

  • 解锁账户(密码为:account1):
    personal.unlockAccount("0x6c4ac6e04d033b050f08158fcde28d341e297aae")
  • 设置amount:amount = web3.toWei(5,'ether')
  • 转账:eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:amount})
  • 查看状态:txpool.status
  • 查看区块状态: eth.getBlock("pending",true)
  • 查看当前区块:eth.blockNumber
  • 查看交易(传入交易后的哈希值):eth.getTransaction("0x6508f37684a20ce34af973ae321762aad43495e83414e2ff57f1dfe6b4dca087")
> personal.unlockAccount("0x6c4ac6e04d033b050f08158fcde28d341e297aae")
Unlock account 0x6c4ac6e04d033b050f08158fcde28d341e297aae
Passphrase:
true
> amount = web3.toWei(5,'ether')
"5000000000000000000"
eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:amount})
"0x6508f37684a20ce34af973ae321762aad43495e83414e2ff57f1dfe6b4dca087"
> txpool.status
{
  pending: 1,
  queued: 0
}
> eth.getBlock("pending",true)
{
  difficulty: 133120,
  extraData: "0xd583010702846765746885676f312e39856c696e7578",
  gasLimit: 4712388,
  gasUsed: 21000,
  hash: null,
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: null,
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: null,
  number: 33,
  parentHash: "0x0de4b23162ed73ca8977a64341a266ac3e17f7a85ceb3b9a5c6def2e8f996999",
  receiptsRoot: "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 644,
  stateRoot: "0x4ff33dcfbc06a005835d0bd84ab9e16096f611274099d6d82ecc8fbdcb2d8b0a",
  timestamp: 1531029456,
  totalDifficulty: 0,
  transactions: [{
      blockHash: "0xd5fd41e3f3d55ea40768410b1d875269e71e452fc4df17bd0d90a28486ffb192",
      blockNumber: 33,
      from: "0x6c4ac6e04d033b050f08158fcde28d341e297aae",
      gas: 90000,
      gasPrice: 0,
      hash: "0x6508f37684a20ce34af973ae321762aad43495e83414e2ff57f1dfe6b4dca087",
      input: "0x",
      nonce: 0,
      r: "0x2f243e1ea33b8fe67109e4fc64bdb490b7f7cd951259c9c8707651b774438711",
      s: "0x55c70724cb1ace94df2dc0faae121a6836e42f76af5e33407df75224acc769c5",
      to: "0x2a7760e97d3142088b7419d0e1e9223946ab03de",
      transactionIndex: 0,
      v: "0xa96",
      value: 5000000000000000000
  }],
  transactionsRoot: "0x36491bb2960150bd17acbb9aaec2e728c871a9609e066c22d4a441686425c7a4",
  uncles: []
}
> miner.start()
null
> miner.stop()
true
> txpool.status
{
  pending: 0,
  queued: 0
}
> eth.getBlock("pending",true)
{
  difficulty: 131584,
  extraData: "0xd583010702846765746885676f312e39856c696e7578",
  gasLimit: 4712388,
  gasUsed: 0,
  hash: null,
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: null,
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: null,
  number: 41,
  parentHash: "0x06923121f1d1062533212d9ce72e946df498eadcebadf4d9f3e80300820b7c4c",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 533,
  stateRoot: "0x5992d971978ee2471205d07d2d4fabb7f3037c207457da7a66926c8f7203f074",
  timestamp: 1531031421,
  totalDifficulty: 0,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
> eth.getBalance("0x2a7760e97d3142088b7419d0e1e9223946ab03de")
5000000000000000000

> eth.getTransaction("0x6508f37684a20ce34af973ae321762aad43495e83414e2ff57f1dfe6b4dca087")
{
  blockHash: "0xa67a234188cf4ff99f40785ec1fcd8711366a2da2728afec43542064a9f1a26a",
  blockNumber: 33,
  from: "0x6c4ac6e04d033b050f08158fcde28d341e297aae",
  gas: 90000,
  gasPrice: 0,
  hash: "0x6508f37684a20ce34af973ae321762aad43495e83414e2ff57f1dfe6b4dca087",
  input: "0x",
  nonce: 0,
  r: "0x2f243e1ea33b8fe67109e4fc64bdb490b7f7cd951259c9c8707651b774438711",
  s: "0x55c70724cb1ace94df2dc0faae121a6836e42f76af5e33407df75224acc769c5",
  to: "0x2a7760e97d3142088b7419d0e1e9223946ab03de",
  transactionIndex: 0,
  v: "0xa96",
  value: 5000000000000000000
}

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

推荐阅读更多精彩内容

  • 一 环境以及软件版本: 1)VMware Workstation 12 Pro 2)Ubuntu 16.04 de...
    张_a1d6阅读 887评论 0 0
  • MAC上以太坊私有链搭建 go-ethereum客户端安装 安装完成之后在命令行输入 搭建私有链 以太坊支持自定义...
    daos阅读 322评论 0 2
  • 新手第一次发文,希望大佬轻喷,谢谢。 当前我使用的系统是Centos7,geth是v1.7.3版本 以太坊搭建私有...
    leo_hero阅读 314评论 0 0
  • 感赏亲爱的儿子,用琅琅的读书声将早晨唤醒,将我唤起。我一看,儿子正端坐在书桌前,一边读背一边在草稿纸上默写,不由自...
    苇絮轻扬阅读 171评论 1 7
  • 夜,悄悄降临。不落岛周身散发着幽蓝的微光。在月光的映衬下,像极了一片漂浮在夜空中的海洋。 此时此刻,囚岛上却是另一...
    曦禾子阅读 340评论 0 4