安装Geth
mkdir private-geth
cd private-geth
编辑创世纪区块genesis.json文件:
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0
},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x1",
"gasLimit": "800000000",
"extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" }
}
}
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0
},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x0200",
"gasLimit": "8000000",
"extradata": "0x00000000000000000000000000000000000000000000000000000000000000007df9a875a174b3bc565e6424a0050ebc1b2d1d820000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" }
}
}
进行区块链的初始化:
geth --datadir ./data/00 init genesis.json
启动节点:
geth --datadir ./data/00 --rpc --rpcport 8545 --port 30303 --networkid 15 --allow-insecure-unlock --nodiscover console
注意: rpcport要和truffle.js中的一致,默认geth port为30303, networkid和truffle.js中的一致。
geth --datadir ./data/00 --rpc --rpcport 8545 --port 30303 --networkid 15 --nodiscover --allow-insecure-unlock console --rpccorsdomain="moz-extension://dd70654e-21bd-4a65-ae05-12b15d937823" 2>log.txt
--nodiscover 不会发现peers,用于测试。
进入终端之后可以创建账户:
>personal.newAccount("password")
>personal.unlockAccount(eth.accounts[0],"password", 0)
最新版的geth会报错,account unlock with HTTP access is forbidden异常。
解决:启动时添加:geth --rpc --rpcapi eth,web3,personal --allow-insecure-unlock
第三个参数0代表永久解锁
>eth.accounts
>eth.getBalance(eth.accounts[0])
>eth.blockNumber
>personal.listAccounts
>web3.eth.coinbase
>web3.fromWei(web3.eth.getBalance(acc0)) 查看格式化的以太币
>admin.nodeInfo.enode 获取节点实例的enode url
>admin.addPeer(enode url) 添加新节点,注意url中[::]替换为新节点的IP地址
发送以太币:
>eth.sendTransaction({from: "....",to: " ...", value: web3.toWei(1, "ether")})
>eth.pendingTransactions
>miner.start()
可能会返回NULL, 但实际上是在挖矿的
查看>eth.coinbase
>eth.getBalance(eth.accounts[0])
>eth.hashrate
因为geth javascript console
是基于javascript的,所以也可以创建js函数,查看所有帐户余额:
> function checkAllBalances() {
var totalBal = 0;
for (var acctNum in eth.accounts) {
var acct = eth.accounts[acctNum];
var acctBal = web3.fromWei(eth.getBalance(acct), "ether");
totalBal += parseFloat(acctBal);
console.log(" eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");
}
console.log(" Total balance: " + totalBal + " ether");
};
> checkAllBalances()
eth.accounts[0]: 0xbe323cc4fde114269a9513a27d3e985f82b9e25d balance: 1245 ether
eth.accounts[1]: 0x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70 balance: 0 ether
Total balance: 1245 ether
如果命令较多,可以保存到一个脚本里,使用命令载入脚本:loadScript(‘/path/script/here.js’);
$ truffle init
配置truffle.js文件中的网络,需要和geth一致。
$ truffle compile
$ truffle migrate
migrate时可能报错password or unlock...; 解决: >personal.unlockAccount(web3.eth.coinbase, "111") 其中111为密码;
报错Insufficient funds for gas...; 解决:可能是没有ether, >miner.start(4); admin.sleepBlocks(1); miner.stop();
报错Error exceeds block gas limit..; 解决:eth.getBlock("latest").gasLimit 将这个值 在truffle.js network配置中加入 gas:XXX,
报错Contract migration error: The contract code couldn't be stored, please check your gas amount 解决:删除contracts/build文件夹,重新编译。
报错:
解决:检查truffle-config.js中的gas是不是太小了。
$truffle migrate --network
1. 找到build/contracts/Trade.json中的abi,压缩成一行
>var abi=[{}]; address=" ";
>trade=eth.contract(abi).at('0x contract address'); 创建trade 合约实例
>trade.initialDeposit({from: eth.accounts[0], value: 30}); 调用Trade contract 中的方法
>eth.getTransactionReceipt('0x contract的 address') 查看交易详情,有gasUsed.
2. 评估合约的 gas usage
trade.initialDeposit.estimateGas({from:eth.accounts[0], value: 30})
trade.makeDeal.estimateGas("11122",{from: eth.accounts[2]})
trade.refundDeposit.estimateGas({from:eth.accounts[0]})
3.评估创建合约的手续费:
(1)找到build/contracts/Trade.json中的bytecode, 然后:
web3.eth.estimateGas({data: bytecode});
参考 https://www.jianshu.com/p/f68a9ad8119a 使用Geth truffle进行私有区块链创建和部署
参考 https://www.cnblogs.com/zl03jsj/p/6858928.html 区块链入门(2):搭建以太坊私有链,执行挖矿.