下载并安装Geth:Geth官网下载地址
环境变量配置。
下载完成后,在D盘创建一个文件夹eth用于存储数据。配置创世块genesis.json文件如下:
{
"config": {
"chainId": 0,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
在命令行输入geth --datadir "D:\eth" init "D:\eth\genesis.json"
命令,指定私链的数据目录并进行节点初始化。至此就建立好了一个创世区块。
输入命令geth --datadir "D:\eth" --port 30303 --networkid 99 --nodiscover console
,进入geth控制台。
geth --datadir "/chain0" init genesis.json
创建节点。这里创建了两个节点。geth --datadir "\chain0" --port 30303 --networkid 99 --nodiscover console
启动节点。同时启动两个节点需要使port不同。admin.nodeInfo.enode
查看节点信息;在0号控制台使用addPeer()添加节点。net.peerCount``admin.peers
查看添加的节点数量和节点信息在命令中加入2>>geth.log,可以将控制台信息输出到日志。其中记录了信息类型(INFO / WARN等)、信息输出时间、信息内容。
Started P2P networking
表示P2P服务开启。HTTP endpoint opened
表示HTTP终端开启。Blockchain manager stopped
表示区块链管理器停止。Stopping Ethereum protocol
表示终止以太网协议。Transaction pool stopped
表示终止事务池。pragma solidity 0.5.11;
contract hello {
function multiply(uint a) pure public returns (uint) {
return a*3;
}
}
var helloContract = web3.eth.contract([{
"constant":true,"inputs":[{
"internalType":"uint256","name":"a","type":"uint256"}],"name":"multiply","outputs":[{
"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"}]);
var hello = helloContract.new(
{
from: web3.eth.accounts[0],
data: '0x6080604052348015600f57600080fd5b5060ae8061001e6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c6888fa114602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b600060038202905091905056fea265627a7a72315820b6e72b6de9f4cfd60d1ccb57b3673ef8f82a583403c034a9fc97e6aa8868c32b64736f6c634300050b0032',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
Demo=eth.contract(hello.abi).at("0xb52cd969d662288f57e6a014f611bc4d25f5a89c")
personal.newAccount("password")
,成功创建账号。输入personal.listAccounts
,可以看到创建成功的账号地址。此时使用eth.getBalance(eth.accounts[0])
命令查看账户余额为0。miner.start()
进行挖矿,miner.stop()
停止挖矿。一段时间后挖矿成功,再次查看账户0的余额,发现多出了很多资产。personal.unlockAccount()
为账户解锁,需要输入密码。web3.fromWei(eth.getBalance(eth.accounts[0]))
:转账前进行余额查询,将单位Wei转换为ether。amount = web3.toWei(0.98)
eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: amount})
eth.getTransaction("事务地址")