geth搭建私有链,remix连接私有链

搭建私有链

  1. 创建genesis.json
{
  "alloc": {},
  "config": {
    "chainID": 72,
    "homesteadBlock": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0
  },
  "nonce": "0x0000000000000000",
  "difficulty": "0x4000",
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "timestamp": "0x00",
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
  "gasLimit": "0xffffffff"
}
  1. 创建⼀个⽂件夹,指定为私有⽹络数据存储位置
mkdir private-net
cd private-net
  1. 创建私有链
    geth --datadir "./node1" init genesis.json
  2. 启动一个节点
    geth --datadir "./node1" --networkid 72 --port 30301 console
  3. 私有链基本命令
- 创建新账户
- 查看余额
- 挖矿
- 指定矿工
- 查看所有节点
  1. 创建一个新节点,步骤同上
geth --datadir "./node2" init genesis.json
geth --datadir "./node2" --networkid 72 --port 30302 --authrpc.port 8552 console
init不要忘记,直接执⾏启动也会成功运⾏,但是后续添加节点总是失败。
两个节点想互连的话,需要指定相同的networkid,这里是 --networkid 72
  1. 子命令attach,接⼊控制台(在private-net目录下)
    geth attach ipc:note2/geth.ipc

  2. 在node1中添加node2节点

获取node2的enode:admin.nodeInfo.enode
在node1中 T添加node2的enode:admin.addPeer("enode://")
  1. 由node1的账户0向node2的账户1转账
eth.sendTransaction({from : eth.accounts[0], to: "0xf78160e527e3fe2f187546008b59ca36608f8ef4", value: web3.toWei(5, "ether")})
可能会报错:
Error: authentication needed: password or unlock
at web3.js:3143:20 
at web3.js:6347:15 
at web3.js:5081:36 
at <anonymous>:1:
这说明当前账户被锁定,需要解锁后才能发起交易。
  1. 解锁账户
    personal.unlockAccount(eth.accounts[0])

  2. 然后再尝试转账
    eth.sendTransaction({from : eth.accounts[0], to: "0xf78160e527e3fe2f187546008b59ca36608f8ef4", value: web3.toWei(5, "ether")})

  3. 查看交易状态: 此时交易处于pending状态
    txpool.status

  4. 将交易打包上链(挖矿)

miner.start()
miner.stop()

remix连接私有链

  1. 最重要的是: --http相关的选项要手动开启。

在1.10.8之后,--rpc相关选项被--http替代
相关回答:https://stackoverflow.com/questions/69463898/flag-provided-but-not-defined-rpc
geth --datadir ./node1 --networkid 72 --http --http.port 8545 --http.api 'web3,eth,net,debug,personal' --http.corsdomain '*' console
image

  1. 使用remix连接external http provider。
    image
posted @ 2022-10-23 12:07  jujubos  阅读(414)  评论(0)    收藏  举报