搭建私有区块链与挖矿转账

一、创建私有链

1、创建文件 genesis.json

{
    "config":{
        "chainId":15,
        "homesteadBlock":0,
        "eip155Block":0,
        "eip158Block":0
    },
    "difficulty":"4",
    "gasLimit":"2100000",
    "alloc":{ }
}

参数说明:

  • difficulty : 挖矿难度
  • gasLimit : 单笔交易限额
  • alloc : 初始账户信息,可以设置一下初始账户,账户里可以设置初始余额

2、使用配置文件初始化创建区块

geth init genesis.json --datadir dataChain

3、启动区块链节点–服务端

 geth --networkid 123 --datadir dataChain --rpc --rpcaddr 192.168.72.128 --rpcport 9595 --port 3000

参数说明:

  • –identity: 制定节点id
  • –network : 网络id,默认1为主干网
  • –rpc : 表示开启 HTTP-RPC 服务
  • –rpcaddr : HTTP-RPC 服务ip地址,这里是本机ip地址
  • –rpcport : HTTP-RPC 服务监听端口,默认是8545
  • –datadir : 指定区块链数据的存储位置
  • –port : 指定和其他节点连接所使用的端口号,默认30303
  • –nodiscover : 关闭节点发现机制,防止加入有同样初始配置的陌生节点

4、启动geth客户端

cd dataChain
geth attach ipc:geth.ipc

5、挖矿

  • 查看账户:
> personal.listAccounts
  • 添加账户:
> personal.newAccount("yuan")
> personal.newAcount("malakh")
// personal.newAccount("密码")
  • 查看账户地址
> personal.listAcounts[0]
  • 查看账户余额
> eth.getBalance(personal.listAccounts[0])
// eth.getBalance("账户地址")
  • 设置挖矿默认账户
> miner.setEtherbase(personal.listAcounts[0])
// miner.setEtherbase("账户地址")
  • 挖矿
// 开始挖矿
> miner.start()
// 停止挖矿
> miner.stop()
// 查看余额
> eth.getBalance(personal.listAcounts[0])

二、Geth客户端操作

1、几个对象

<1> personal

// 查看账户列表
>  personal.listAccounts

// 创建账户
>  personal.newAccount("密码")

// 查看账户地址
>  personal.listAccounts[index]

// 解锁账户
>  personal.unlockAccount(personal.listAccount[0])

<2> eth

// 查看账户余额
>  eth.getBalance("账号地址")
>  eth.getBalance(personal.listAccounts[0])

// 转账
>  eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:1000})

<3> miner

// 设置挖矿账户
>  miner.setEtherbase("账户地址")
>  miner.setEtherbase(personal.listAccounts[0])

// 开始挖矿
>  miner.start()

// 停止挖矿
>  miner.stop()

2、打开geth控制台

geth attach ipc:geth.ipc

3、转账操作

<1> 解锁账户

> personal.unlocakAccount(personal.listAccount[0])

<2> 创建交易

> eth.sendTransaction({from:personal.listAccounts[0],to:personal.listAccounts[1],value:1000})

<3> 查看交易情况

// pending 表示待办事项
> txpool.status
// 查看待办事项明细
> eth.getBlock("pending",true)

<4> 开始挖矿,使交易生效

> miner.start()
> miner.stop()

<5> 查看交易结果

// 查看待办事项
> txpool.status
// 查看账户金额
> eth.getBalance(personal.listAccounts[1])

// 查看交易明细,交易hash就是sendTransaction后返回的hash字符串
> eth.getTransaction("交易hash")

转账说明:

  • 转账之前需要将from账户解锁,账户解锁一定时间不使用时,会自动加锁;
  • sendTransaction 方法接收一个json字符串
  • from : 转账账户地址
  • to : 目标账户地址
  • valeu: 交易金额
  • 执行完sendTransaction后,并不会立即转账,而是会有一个代办事项,需要mine挖矿后,才能转账成功
上一篇:链路聚合笔记


下一篇:AS5600 磁编码器的调试经验