使用geth联调c++客户端eth

c++版的eth功能很弱,没有geth强大的命令,比如无法查看区块信息,无法查看账户的余额eth.getBalance()

想要调试C++版以太坊程序,可以借助getch命令来查看区块账户数据进行测试。

1、准备测试私链的创世区块json文件genesis.json,内容如下

{
    "nonce": "0x0000000000000042",
    "difficulty": "0x200",
    "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "author": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
    "gasLimit": "0x1388"

}

2、利用C++客户端eth 创建私链

进入程序编译的eth debug目录


进入命令行执行命令 eth  -d data  --genesis genesis.json

区块的目录为data


看到Nodeid启动后,多按几个ctrl+c终止,可以看到区块文件夹创建成功



3、启动私链eth  -d data  --private test    -C


4、另外开启一个命令窗口,执行geth

geth attach ipc:\\.\pipe\geth.ipc


personal.newAccount('123456')

如上,创建若干账户

执行pesonal,可以看到账户信息


查看第0个账户的金额,可以看到目前是0

eth.getBalance(eth.accounts[0])


设置挖矿的缺省账户

eth.defaultAccount=eth.accounts[0]

启动挖矿

miner.start(1)


可以看到c++ eth命令行服务端,出现挖矿信息


过几分钟停止挖矿

miner.stop()

查看账户信息

 eth.getBalance(eth.accounts[0])

发现是0


这是因为默认以太坊挖矿对单机cpu难度太大,需要很长时间,这时需要启用一个配置文件config.json,放到C++ ETH的debug目录下

"sealEngine": "Ethash",
"params": {
"accountStartNonce": "0x00",
"homesteadForkBlock": "0x118c30",
"daoHardforkBlock": "0x1d4c00",
"EIP150ForkBlock": "0x259518",
"EIP158ForkBlock": "0x28d138",
"byzantiumForkBlock": "0x42ae50",
"constantinopleForkBlock": "0x500000",
"networkID" : "0x01",
"chainID": "0x01",
"maximumExtraDataSize": "0x20",
"tieBreakingGas": false,
"minGasLimit": "0x1388",
"maxGasLimit": "7fffffffffffffff",
"gasLimitBoundDivisor": "0x0400",
"minimumDifficulty": "0x0200",
"difficultyBoundDivisor": "0x080",
"durationLimit": "0x0d",
"blockReward": "0x4563918244F40000"
},
"genesis": {
"nonce": "0x0000000000000042",
"difficulty": "0x200",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1388"
},
"accounts": {
"0000000000000000000000000000000000000001": { "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
"0000000000000000000000000000000000000003": { "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
"0000000000000000000000000000000000000004": { "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } },
"0000000000000000000000000000000000000005": { "precompiled": { "name": "modexp", "startingBlock" : "0x2dc6c0" } },
"0000000000000000000000000000000000000006": { "precompiled": { "name": "alt_bn128_G1_add", "startingBlock" : "0x2dc6c0", "linear": { "base": 500, "word": 0 } } },
"0000000000000000000000000000000000000007": { "precompiled": { "name": "alt_bn128_G1_mul", "startingBlock" : "0x2dc6c0", "linear": { "base": 40000, "word": 0 } } },
"0000000000000000000000000000000000000008": { "precompiled": { "name": "alt_bn128_pairing_product", "startingBlock" : "0x2dc6c0" } }
}

}


重新启动C++ ETH服务端,使用配置文件

eth  -d data  --private test   --config config.json -C


重启getch连接c++ eth服务端


设置挖矿默认账户

miner.setEtherbase(eth.accounts[0])

启动挖矿 miner.start(1)

过几+秒停止挖矿 miner.stop()

查看账户


可以看到该账户挖矿成功了


猜你喜欢

转载自blog.csdn.net/metal1/article/details/80227629