go-ehtereum编译:

git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum && git checkout
make geth //生成的geth在build/bin/geth

创建私链

1、创建文件夹来存储私链数据:

mkdir privatechain

2、 使用geth来加载:

geth --networkid 123 --dev --datadir data1 --rpc --rpcaddr 192.168.1.5 --rpcport 8989 --port 3000

--identity:指定节点 ID;
--rpc:表示开启 HTTP-RPC 服务;
--rpcaddr:HTTP-RPC 服务ip地址;
--rpcport:指定 HTTP-RPC 服务监听端口号(默认为 8545);
--datadir:指定区块链数据的存储位置;
--port:指定和其他节点连接所用的端口号(默认为 30303);
--nodiscover:关闭节点发现机制,防止加入有同样初始配置的陌生节点

3、 打开另个终端, 进入交互式js执行环境:

geth attach ipc:<data1>/geth.ipc

eth:包含一些跟操作区块链相关的方法;

net:包含一些查看p2p网络状态的方法;

admin:包含一些与管理节点相关的方法;

miner:包含启动&停止挖矿的一些方法;

personal:主要包含一些管理账户的方法;

txpool:包含一些查看交易内存池的方法;

web3:包含了以上对象,还包含一些单位换算的方法。

方法二:

1、 新建genesis.json文件在privatechain目录下:

{
  "config": {
          "chainId": 10, 		//与github上仅修改此处
          "homesteadBlock": 0,
          "eip155Block": 0,
          "eip158Block": 0
      },  
  "alloc"      : {}, 
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x20000",
  "extraData"  : "", 
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

2、 初始化,创建创世区块

geth --datadir  ~/privatechain/ init  ~/privatechain/genesis.json

3、 创建私链

geth --datadir ~/privatechain/ --nodiscover

4、使用js交换环境:

geth attach ipc:~/privatechain/geth.ipc

5、 新加入节点:

先操作1,2,3,4
执行:admin.addPeer("<节点信息>@ip: 30303")
返回: true

api:eth_getBalance 流程:

// ethclient/ethclient.go
原文地址:https://www.cnblogs.com/chris-cp/p/9046830.html