无论是主网、测试网还是私网,都可以用Geth来启动。很多时候开发者都习惯自己搭建一套私网,下面分享一套私网搭建的步骤。
步骤01:
配置创世块文件,将如下内容保存为genesis.json文件。
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip150Block":0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x2000",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {
"08a58f09194e403d02a1928a7bf78646cfc260b0": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
},
"87366ef81db496edd0ea2055ca605e8686eec1e6": {
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
}
}
}
其中:
1、chainId:指定了独立的区块链网络 ID。网络 ID 在连接到其他节点的时候会用到,以太坊公网的网络 ID 是 1,为了不与公有链网络冲突,运行私有链节点的时候要指定自己的网络 ID。不同 ID 网络的节点无法相互连接。
2、Coinbase:挖矿后获得奖励的账户地址。
3、Difficulty:挖矿难度。
4、gasLimit:一个区块所能容纳的gas上限,智能合约指令在执行中需要消耗gas,可通过以太币自动兑换。
5、nonce:随机值。
6、mixhash:一个256位的哈希证明,与nonce结合验证本块的有效性。
7、parentHash:前一块的hash值,因为是创世块,所以为0.
步骤02:
将genesis.json文件保存在工作目录下。
//创建工作目录
mkdir private_chain
init是初始化的命令,--datadir是用来指定数据存储路径。
geth init genesis.json --datadir ./data
此时在data目录下会有一些文件产生(类似即可):
ydcg@ydcg-virtual-machine:~/private_chain$ tree data
data
├── geth
│ ├── chaindata
│ │ ├── 000011.ldb
│ │ ├── 000012.log
│ │ ├── 000014.ldb
│ │ ├── ancient
│ │ │ └── chain
│ │ │ ├── bodies.0000.cdat
│ │ │ ├── bodies.cidx
│ │ │ ├── bodies.meta
│ │ │ ├── diffs.0000.rdat
│ │ │ ├── diffs.meta
│ │ │ ├── diffs.ridx
│ │ │ ├── FLOCK
│ │ │ ├── hashes.0000.rdat
│ │ │ ├── hashes.meta
│ │ │ ├── hashes.ridx
│ │ │ ├── headers.0000.cdat
│ │ │ ├── headers.cidx
│ │ │ ├── headers.meta
│ │ │ ├── receipts.0000.cdat
│ │ │ ├── receipts.cidx
│ │ │ └── receipts.meta
│ │ ├── CURRENT
│ │ ├── CURRENT.bak
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000013
│ ├── jwtsecret
│ ├── lightchaindata
│ │ ├── 000001.log
│ │ ├── ancient
│ │ │ └── chain
│ │ │ ├── bodies.0000.cdat
│ │ │ ├── bodies.cidx
│ │ │ ├── bodies.meta
│ │ │ ├── diffs.0000.rdat
│ │ │ ├── diffs.meta
│ │ │ ├── diffs.ridx
│ │ │ ├── FLOCK
│ │ │ ├── hashes.0000.rdat
│ │ │ ├── hashes.meta
│ │ │ ├── hashes.ridx
│ │ │ ├── headers.0000.cdat
│ │ │ ├── headers.cidx
│ │ │ ├── headers.meta
│ │ │ ├── receipts.0000.cdat
│ │ │ ├── receipts.cidx
│ │ │ └── receipts.meta
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ ├── LOCK
│ ├── nodekey
│ ├── nodes
│ │ ├── 000009.ldb
│ │ ├── 000010.log
│ │ ├── 000012.ldb
│ │ ├── CURRENT
│ │ ├── CURRENT.bak
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000011
│ ├── transactions.rlp
│ └── triecache
│ ├── data.0.bin
│ └── metadata.bin
├── geth.ipc
├── history
└── keystore
├── UTC--2023-02-13T03-32-59.415880356Z--6d8f664bd8f79471275e6369b0c3d16ff15ea139
└── UTC--2023-02-13T03-46-26.791249692Z--978c6500e4f7ecf19f9c9d675731374497254c7f
步骤03:
启动Geth节点。
geth --datadir ./data --networkid 10 --port 30303 --rpc --rpcport 8545 --rpcapi 'db,net,eth,web3,personal' --rpccorsdomain '*' --gasrice 0 --allow-insecure-unlock console 2> 1.log
参数 | 描述 |
datadir | 指定之前初始化的数据目录文件 |
networkid | 配置成与配置文件config内的chainId相同值,代表加入那个网络,私网随意编号即可 |
port | P2P端口,也就是节点之间互相通信的端口 |
rpc | 代表开启远程调用服务 |
rpcport | 远程服务的端口,默认是8545 |
rpcapi | 远程服务提供的远程调用函数集 |
rpccorsdomain | 指定可以接受请求来源的域名列表(浏览器访问,必须开启) |
gasprice | gas的单价 |
allow-insecure-unlock | 允许在Geth命令窗口解锁账户 |
console | 进入管理台 |
2>1.log | 将Geth产生的日志输出都重定向到1.log中 |
启动后应看到类似下面的结果:
Welcome to the Geth JavaScript console!
instance: Geth/v1.10.26-stable-e5eb32ac/linux-amd64/go1.18.5
coinbase: 0x6e7db33c6e363934a1491a8f2c226fa891260af8
at block: 1 (Wed Feb 15 2023 15:16:51 GMT+0800 (CST))
datadir:
modules: admin:1.0 clique:1.0 debug:1.0 engine:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
To exit, press ctrl-d or type exit
>
到此,Geth启动成功,私网搭建成功。但在实践过程中发现步骤03的启动指令报错,显示
Incorrect Usage. flag provided but not defined: -rpc
应该是geth更新了,更新不使用rpc命令,改为使用http。具体可以查看geth的help。之后在Remix上找到答案(http://remix.etherum.org/)
我们先随便写一个程序calldemo.sol
pragma soildity^0.6.0;
contract calldemo {
uint256 count;
constructor() public {
count = 2023;
}
function setCount(uint256 _number) external {
count = _count;
}
function getCount() public view returns(uint256) {
return count;
}
}
然后在Environment一栏选择Web3 Provider。该操作是连接我们刚刚搭建好的私链。
之后跳出一个提示框,上面有一句话:To run Remix & a local Geth test node, use this command: (see Geth Docs on Dev mode)
大意是要是想要连接私链我们应该采用下面的指令,具体可参考Deploy & Run — Remix - Ethereum IDE 1 documentation
因此我在启动Geth时采用以下指令
geth --datadir ./data --networkid 10 --port 30303 --http --http.corsdomain="http://localhost:8080" --http.api web3,eth,debug,personal,net --http.corsdomain '*' --allow-insecure-unlock console 2> 1.log
发现没有报错,大功告成!并且成功链接remix