以太坊私有链 —— POW搭建

一、使用的GO与Geth版本

GO:1.22.4

geth:1.10.26

二、安装配置GO语言

安装GO 1.22.4语言环境(linux)

wget https://golang.google.cn/dl/go1.22.4.linux-amd64.tar.gz

解压到指定目录

tar -zxvf go1.22.4.linux-amd64.tar.gz -C /usr/local/

配置GOPATH环境

export PATH=$PATH:/usr/local/go/bin

三、下载geth并配置 

​wget  https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.10.26-e5eb32ac.tar.gz

解压到指定目录

tar -zxvf geth-alltools-linux-amd64-1.10.26-e5eb32ac.tar.gz -C /usr/local/

配置geth环境

export PATH=$PATH:/usr/local/geth-alltools-linux-amd64-1.10.26-e5eb32ac

四、搭建POW私链

1、创建账户

# 创建私有链目录
mkdir privatechain
cd privatechain

# 创建账户
geth account new --datadir data

2、自定义创世区块(genesis.json),在alloc字段下面写入创建账户的地址

# 创建genesis.json文件
vim genesis.json

# 写入一下代码
{
  "config": {
    "chainId": 108,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "ethash": {}
  },
  "difficulty": "1",
  "gasLimit": "8000000",
   "alloc": {
    "生成的账户地址": { "balance": "10000000000000000" }
  }
}

3、初始化区块链

geth --datadir data init genesis.json

4、启动私有链

geth --datadir data --networkid 1008 console 2>geth.log

5、启动并开启rpc服务

普通模式
geth --datadir data --networkid 1008  --http --http.addr 127.0.0.1 --http.vhosts "*" --http.api "db,net,eth,web3,personal,miner" --http.corsdomain "*" --nodiscover -- --allow-insecure-unlock  console 2> 1.log
 
 
开发者模式
 
geth --datadir ./devdata --networkid 18 --port 30303 --http --http.addr 0.0.0.0 --http.vhosts "*"  --http.port 8545 --http.api 'db,net,eth,web3,personal' --http.corsdomain "*"  --dev --dev.period 1 console 2> 1.log
 
挂起模式
nohup geth --datadir data --networkid 1009  --http --http.addr 127.0.0.1 --http.port 8545 --http.vhosts "*" --http.api "db,net,eth,web3,personal,miner" --http.corsdomain "*" --nodiscover --snapshot=false --allow-insecure-unlock 2> 1.log &
 
挂起模式进入控制台:
geth attach http://localhost:8545

五、geth的使用

--datadir 指定节点数据目录
init 指定初始化节点使用的配置文件 genesis.json
--identity 设定节点标识
--http 开启http rpc 服务
--http.port 指定http rpc端口
--http.corsdomain 指定跨域
--http.addr 监听地址,默认为127.0.0.1,只能本地访问
--http.api 设置节点上启用RPC接口
--nodiscover 使用此选项可确保未手动添加您的人员无法发现您的节点。否则,如果您的节点具有相同的创世纪文件和网络ID,则可能无意中将您的节点添加到陌生人的区块链中
--networkid 设定网络ID,当创建的链的 genesis block 和 network id 刚好与网络上其他人的链相同,那么就看哪条链长,如果比对方的短,那么链上的数据会全部被覆盖,变成对方的链。
--allow-insecure-unlock 允许使用 http 协议进行账户解锁
--port 网络侦听端口,对等端连接端口
--ipcdisable 指定跨域

六、私有链相关操作

1、进入区块链数据中运行节点运行一下代码进入终端

geth attach ipc:geth.ipc

2、相关操作

  1. eth:包含一些跟操作区块链相关的方法;
  2. net:包含一些查看p2p网络状态的方法;
  3. admin:包含一些与管理节点相关的方法;
  4. miner:包含启动&停止挖矿的一些方法;
  5. personal:主要包含一些管理账户的方法;
  6. txpool:包含一些查看交易内存池的方法;
  7. web3:包含了以上对象,还包含一些单位换算方法;

3、相关API命令

3.1、查看账户

# 查看账户,如果为空代表没有创世区块,如果有代表创世区块也是第一个区块
> personal.listAccounts
["0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb"]
# 可使用eth.conbase查看创世区块
> eth.coinbase
"0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb"

3,2、创建账户

# 创建账户,并设置密码为123456
> personal.newAccount("123456")
"0x45ed6706610d8b2c0d97027871636b39eb9829b2"

3.3、获取指定账户

> personal.listAccounts
["0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb", "0x45ed6706610d8b2c0d97027871636b39eb9829b2", "0xb892bdbd6305a38790b044f8a0012da67c69d64b"]
> personal.listAccounts[0]
"0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb"
> personal.listAccounts[1]
"0x45ed6706610d8b2c0d97027871636b39eb9829b2"
> personal.listAccounts[2]
"0xb892bdbd6305a38790b044f8a0012da67c69d64b"

3.4、查看账户余额

> eth.getBalance("0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb")
10000000000000000

3.5、转账操作

# 成功获取交易区块
> eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:10})
"0xb1f8e6f92260b7870aaa99e6f8e8f78c6c991cbd9c7b24c846365dcd1147f07c"
# 获取状态
> txpool.status
{
  pending: 1,
  queued: 0
}
# 获取代办区块
> eth.getBlock("pending",true)
{
  difficulty: 131072,
  extraData: "0xd883010a1a846765746888676f312e31382e35856c696e7578",
  gasLimit: 8007811,
  gasUsed: 21000,
  hash: null,
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: null,
  mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  nonce: null,
  number: 1,
  parentHash: "0x3d9209ff83171cdfe2b20b9c970f62d0bbde3d678f9ff2a3601056a7851efea2",
  receiptsRoot: "0x056b23fbba480696b65fe5a59b8f2148a1299103c4f57df839233af2cf4ca2d2",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 641,
  stateRoot: "0x0000000000000000000000000000000000000000000000000000000000000000",
  timestamp: 1718342329,
  totalDifficulty: 0,
  transactions: [{
      blockHash: "0x49d7bb9bd69fb6643a84dfeb40c0abebdec5b22f95c08e280ae47486d3aee494",
      blockNumber: 1,
      chainId: "0x6c",
      from: "0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb",
      gas: 21000,
      gasPrice: 1000000000,
      hash: "0xb1f8e6f92260b7870aaa99e6f8e8f78c6c991cbd9c7b24c846365dcd1147f07c",
      input: "0x",
      nonce: 0,
      r: "0xf87348f863df0044d588790c207d96bdb4a57b94362a5d51ad76ecfe05a2f201",
      s: "0x66cf27c90de49db8c1ec7d32d9967906b4182ef76215e20c119d06b13e9d3441",
      to: "0x45ed6706610d8b2c0d97027871636b39eb9829b2",
      transactionIndex: 0,
      type: "0x0",
      v: "0xfc",
      value: 10
  }],
  transactionsRoot: "0xe23873f07b95abbcd5df83391afd4648943e23faae7b4a202e40d6dd1e88d32e",
  uncles: []
}

3.6、进行挖矿及状态的pending为0表示转账成功

> eth.getBalance("0x45ed6706610d8b2c0d97027871636b39eb9829b2")
0
> eth.getBalance("0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb")
10000000000000000
> miner.start()
null
> txpool.status
{
  pending: 0,
  queued: 0
}
> eth.getBalance("0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb")
4009999999999999990
> eth.getBalance("0x45ed6706610d8b2c0d97027871636b39eb9829b2")
10

# 获取交易哈希
> eth.getTransaction("0xb1f8e6f92260b7870aaa99e6f8e8f78c6c991cbd9c7b24c846365dcd1147f07c")
{
  blockHash: "0x047a7bfb275352ae58467a1078df71f4b14fa19660c646a9a0ab0454337789a2",
  blockNumber: 1,
  chainId: "0x6c",
  from: "0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0xb1f8e6f92260b7870aaa99e6f8e8f78c6c991cbd9c7b24c846365dcd1147f07c",
  input: "0x",
  nonce: 0,
  r: "0xf87348f863df0044d588790c207d96bdb4a57b94362a5d51ad76ecfe05a2f201",
  s: "0x66cf27c90de49db8c1ec7d32d9967906b4182ef76215e20c119d06b13e9d3441",
  to: "0x45ed6706610d8b2c0d97027871636b39eb9829b2",
  transactionIndex: 0,
  type: "0x0",
  v: "0xfc",
  value: 10
}

3.8、查看区块

# 查看当前区块总数
> eth.blockNumber
177
> eth.getBlock(4)
{
  difficulty: 131264,
  extraData: "0xd883010a1a846765746888676f312e31382e35856c696e7578",
  gasLimit: 8031290,
  gasUsed: 0,
  hash: "0x4cefd8e79ba65ab6b692aaf7f98789a58cdb0a5fa59dc6251e756e8bfa77224b",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb",
  mixHash: "0x7ca0368b1420c5ae28c05980aa90e29420ff996112f9a8ff3d9a0289a22a7a1a",
  nonce: "0x368b7a0b4b052002",
  number: 4,
  parentHash: "0xbf02ab12a7d3357471f3a7692782745e365c88de02e8f416646a0762a60faa3d",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 536,
  stateRoot: "0x1c9173dbf81dab449ddae081282bdec235d8deeea364c313ec0d61fa6031e3cd",
  timestamp: 1718342947,
  totalDifficulty: 524673,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}

七、配置合约到私有链中

        7.1、合约

pragma solidity ^0.8.19;



contract test{
    function testGeth(uint a) public view  returns(uint b){
        b = a * 100;
    }
    
}

       7.2、abi和bin

> abi
[{
    inputs: [{
        internalType: "uint256",
        name: "a",
        type: "uint256"
    }],
    name: "testGeth",
    outputs: [{
        internalType: "uint256",
        name: "b",
        type: "uint256"
    }],
    stateMutability: "view",
    type: "function"
}]
> bin
"0x6080604052348015600e575f80fd5b506101a08061001c5f395ff3fe608060405234801561000f575f80fd5b5060043610610029575f3560e01c80632f3483451461002d575b5f80fd5b610047600480360381019061004291906100a9565b61005d565b60405161005491906100e3565b60405180910390f35b5f60648261006b9190610129565b9050919050565b5f80fd5b5f819050919050565b61008881610076565b8114610092575f80fd5b50565b5f813590506100a38161007f565b92915050565b5f602082840312156100be576100bd610072565b5b5f6100cb84828501610095565b91505092915050565b6100dd81610076565b82525050565b5f6020820190506100f65f8301846100d4565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61013382610076565b915061013e83610076565b925082820261014c81610076565b91508282048414831517610163576101626100fc565b5b509291505056fea2646970667358221220d93a45e5cc420131c3afd1e7cf89c1735fc398d32f296f6c458a7f703abf00fc64736f6c634300081a0033"

7.3、部署合约

        

# 评估合约手续费,判断账户是否可以进行部署
> web3.eth.estimateGas({data:bin})
53000

> eth.getBalance(personal.listAccounts[0])
1.22600999999999999999e+21   # 由于刚刚一直在挖矿没有关闭,所以余额变大了

> personal.unlockAccount(personal.listAccounts[0])
Unlock account 0xcb0bf04fdb1dde448e5ef34568c3145b6f9103fb
Passphrase: 
true
> var myContract = eth.contract(abi);
undefined
> var contract = myContract.new({from:personal.listAccounts[0],data:bin, gas:600000})
undefined
> contract
{
  abi: [{
      inputs: [{...}],
      name: "testGeth",
      outputs: [{...}],
      stateMutability: "view",
      type: "function"
  }],
  address: undefined,
  transactionHash: "0xe9602d91330272965c28901e11373ce8684d72f9c75b2899937f69bb3bab1224"
}

相关推荐

  1. 私有 —— POW

    2024-06-15 05:38:01       6 阅读
  2. 在Ubuntu下自己的私有

    2024-06-15 05:38:01       9 阅读
  3. 2024-04-30 区块--相关文档

    2024-06-15 05:38:01       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 05:38:01       10 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 05:38:01       12 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 05:38:01       11 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 05:38:01       13 阅读

热门阅读

  1. 6.11 c语言

    2024-06-15 05:38:01       4 阅读
  2. DP专项训练

    2024-06-15 05:38:01       4 阅读
  3. uniapp面试题

    2024-06-15 05:38:01       5 阅读
  4. 【lesson3】服务端Json工具类的设计和实现

    2024-06-15 05:38:01       8 阅读
  5. 力扣475.供暖器

    2024-06-15 05:38:01       6 阅读
  6. 图片based64编码解码python代码

    2024-06-15 05:38:01       5 阅读
  7. ray框架训练阶段和 Serve 阶段对比

    2024-06-15 05:38:01       9 阅读