以太坊学习节点搭建

创作人 Leo


编辑时间 Fri Jul 29,2022 at 19:47


以太坊私有节点搭建

首先写一个配置文件

{
	"config": {
		"chainId": 1337,
		"homesteadBlock": 0,
		"eip150Block": 0,
		"eip155Block": 0,
		"eip158Block": 0,
		"byzantiumBlock": 0,
		"constantinopleBlock": 0,
		"petersburgBlock": 0,
		"istanbulBlock": 0,
		"berlinBlock": 0,
		"londonBlock": 0
	},
	"alloc": {
		"0xbaea55dcb4e21b33197e2714493f8735d8c621dd": {"balance": "100000000000000000000"},
		"0x6ab36d09c322f65edde913d298af94e487073c13": {"balance": "100000000000000000000"},
		"0x1a800511693217b15d538c34246bc67eaa236434": {"balance": "100000000000000000000"},
		"0xcec49125b39b3ffa9672bb8d264385b93cecde29": {"balance": "100000000000000000000"},
		"0xbdf4a9a5cab1442ab0800382ea2deb09a335b1bd": {"balance": "100000000000000000000"},
		"0x5bf05c1bb6a63aad8d0fb643d9fa179c11c94ba2": {"balance": "100000000000000000000"},
		"0xb3cf3d496b730f7664114fdeb09c0e94ef1ca092": {"balance": "100000000000000000000"},
		"0xedaf0af56393421aba35b492989b3abf940c5081": {"balance": "100000000000000000000"},
		"0x4d22e6b33f8d944aeda1b0b3efac28a4b4848978": {"balance": "100000000000000000000"},
		"0x0f8a5f27d27b570dbd7bea15e272f8c76d69e131": {"balance": "100000000000000000000"},
		"0x7396b47027F2751E3353F942C1Bff99751F9f0c8": {"balance": "100000000000000000000"}
	},
	"coinbase": "0x0000000000000000000000000000000000000000",
	"difficulty": "0x20000",
	"extraData": "",
	"gasLimit": "0x4a817c800",
	"nonce": "0x0000000000000042",
	"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"timestamp": "0x00"
}

alloc 代表需要分配的eth币,单位 wei
私有链默认 network id 为 1337,ehtereumjs-common 没有该配置,可以复制其他文件配置一个,否则签名会报错

sendSignedTransaction Error: Returned error: invalid sender
...

建立两个目录用来做本地双节点同步

node 1


$ mkdir -p /home/lixin/ethlocal/data

$ cp ./geneis.eth.json /home/lixin/ethlocal/

$0 echo 1234qwer>pwd.txt

$ cp ./pwd.txt /home/lixin/ethlocal/

$ cd /home/lixin/ethlocal/

初始化节点目录
$ /usr/local/geth/geth --datadir=/home/lixin/ethlocal/data init ./geneis.eth.json

生成 p2p 种子服务器
$ /usr/local/geth/bootnode --genkey=boot.key   

启动种子服务器
$ /usr/local/geth/bootnode --nodekey=boot.key  

创建矿工地址,密码输入上面的 1234qwer 或自定义
$ /usr/local/geth/geth --datadir=/home/lixin/ethlocal/data account new

启动私有以太坊节点
$ /usr/local/geth/geth \
--datadir=/home/lixin/ethlocal/data \
--bootnodes=enode://9818946dcafdd87984cbe9fc1f8a28a5325bac19f1d25e466b57c6768eef32426b0e6115cbcee790d88b91ed1b191f5ebbb903bd72555d2fe696ad592d20a444@127.0.0.1:0?discport=30301 \
--http --http.addr 0.0.0.0  --http.port 7545 --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --http.corsdomain "*" \
--ws --ws.addr 0.0.0.0 --ws.port 7546 --ws.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" \
--mine --miner.etherbase 0x89a4875862B2cb7947260b6496B95e0D06b6bB36 --password ./pwd.txt 

node 2


$ mkdir -p /home/lixin/ethlocal2/data

$ cp ./geneis.eth.json /home/lixin/ethlocal2/

$0 echo 1234qwer>pwd.txt

$ cp ./pwd.txt /home/lixin/ethlocal2/

$ cd /home/lixin/ethlocal2/

初始化节点目录
$ /usr/local/geth/geth --datadir=/home/lixin/ethlocal2/data init ./geneis.eth.json

生成 p2p 种子服务器
$ /usr/local/geth/bootnode --genkey=boot.key   

启动种子服务器
$ /usr/local/geth/bootnode --nodekey=boot.key  

创建矿工地址,密码输入上面的 1234qwer 或自定义
$ /usr/local/geth/geth --datadir=/home/lixin/ethlocal2/data account new

启动私有以太坊节点
$ /usr/local/geth/geth \
--datadir=/home/lixin/ethlocal2/data \
--bootnodes=enode://9818946dcafdd87984cbe9fc1f8a28a5325bac19f1d25e466b57c6768eef32426b0e6115cbcee790d88b91ed1b191f5ebbb903bd72555d2fe696ad592d20a444@127.0.0.1:0?discport=30301 \
--http --http.addr 0.0.0.0  --http.port 8545 --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --http.corsdomain "*" \
--ws --ws.addr 0.0.0.0 --ws.port 8546 --ws.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" \
--mine --miner.etherbase 0x89a4875862B2cb7947260b6496B95e0D06b6bB36 --password ./pwd.txt 

启动开发模式 dev

  1. 开发模式默认有事务出块
  2. 系统会分配一个有很多以太的矿工,没有锁,可以将其以太随意转出
  3. 注意:dev 模式 datadir,bootnodes 不可用
  4. 可以直接复制下方命令直接启动,dev 模式主要是用来开发调试,而且不保存状态,关闭后下次启动重新初始化
/usr/local/geth/geth \
--dev \
--http --http.addr 0.0.0.0 --http.port 7545 --http.api "admin,debug,web3,eth,txpool,personal,miner,net" --http.corsdomain "*" \
--ws --ws.addr 0.0.0.0 --ws.port 7546 --ws.api "admin,debug,web3,eth,txpool,personal,miner,net" --ws.origins "*"

启动后通过命令行可以给其他有需要的转点以太

eth.sendTransaction({
    from:"0x457c4f6002e19F0Ba6d7D3086086196ba01A463A",
    to:"0xbaea55dcb4e21b33197e2714493f8735d8c621dd",
    value:web3.toWei(10000,"ether")
})

该模式与 ethereumjs 交互需要自己给 ethereum-common 新建一个 chain 配置
复制如下代码到 node_modules/ethereumjs-common/dist/chains/local.json

{
    "name": "local",
    "chainId": 1337,
    "networkId": 1337,
    "comment": "PoA test network",
    "url": "https://www.rinkeby.io",
    "genesis": {
        "hash": "0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177",
        "timestamp": "0x58ee40ba",
        "gasLimit": 4700000,
        "difficulty": 1,
        "nonce": "0x0000000000000000",
        "extraData": "0x52657370656374206d7920617574686f7269746168207e452e436172746d616e42eb768f2244c8811c63729a21a3569731535f067ffc57839b00206d1ad20c69a1981b489f772031b279182d99e65703f0076e4812653aab85fca0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        "stateRoot": "0x53580584816f617295ea26c0e17641e0120cab2f0a8ffb53a866fd53aa8e8c2d"
    },
    "hardforks": [
        {
            "name": "chainstart",
            "block": 0,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "homestead",
            "block": 1,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "dao",
            "block": null,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "tangerineWhistle",
            "block": 2,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "spuriousDragon",
            "block": 3,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "byzantium",
            "block": 1035301,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "constantinople",
            "block": 3660663,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "petersburg",
            "block": 4321234,
            "consensus": "poa",
            "finality": null
        },
        {
            "name": "istanbul",
            "block": 5435345,
            "consensus": "poa",
            "finality": null
        }
    ],
    "bootstrapNodes": [
        {
            "ip": "52.169.42.101",
            "port": 30303,
            "id": "a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf",
            "location": "",
            "comment": "IE"
        },
        {
            "ip": "52.3.158.184",
            "port": 30303,
            "id": "343149e4feefa15d882d9fe4ac7d88f885bd05ebb735e547f12e12080a9fa07c8014ca6fd7f373123488102fe5e34111f8509cf0b7de3f5b44339c9f25e87cb8",
            "location": "",
            "comment": "INFURA"
        },
        {
            "ip": "159.89.28.211",
            "port": 30303,
            "id": "b6b28890b006743680c52e64e0d16db57f28124885595fa03a562be1d2bf0f3a1da297d56b13da25fb992888fd556d4c1a27b1f39d531bde7de1921c90061cc6",
            "location": "",
            "comment": "AKASHA"
        }
    ]
}

dev 模式是 POA 出块,所以该文件是复制rinkeby 配置文件改了name,chainId,networkId 三个配置项


阅读:133
搜索
  • Linux 高性能网络编程库 Libevent 简介和示例 2288
  • Mac系统编译PHP7【20190929更新】 2186
  • Hadoop 高可用集群搭建 (Hadoop HA) 1852
  • Linux 常用命令 1837
  • zksync 和 layer2 1832
  • 安徽黄山游 1817
  • Windows 安装Swoole 1794
  • Hadoop 高可用YARN 配置 1750
  • 小白鼠问题 1707
  • 使用 Java+Thrift 实现异步事件处理服务 1627
简介
不定期分享软件开发经验,生活经验