以太坊搭建私有链

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
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券