安装操作系统:centos 7
- # yum install git
- # git clone https://github.com/ethereum/go-ethereum
- Building geth requires Go and C compilers to be installed:
- #yum install -y golang
- #cd go-ethereum #make geth
- 添加geth到环境变量
#vi /etc/bashrc
#export PATH=$PATH:/mnt/go-ethereum/build/bin
#source /etc/bashrc
- 创建geth目录
#mkdir geth
#cd geth
#mkdir db
- 创建创世节点初始文件
#vi gensis.json
{
"config":{
"chainID":2018,
"homesteadBlock":0,
"eip155Block":0,
"eip158Block":0
},
"alloc":{"0x1667b9bf214edd9143824a14f101968e1cb35ba5":{"balance":"10000000000000000000000000000000"}},
"coinbase":"0x0000000000000000000000000000000000000000",
"difficulty":"0x01",
"extraData":"0x7777777777777777",
"gasLimit":"0xffffffff",
"nonce":"0x0000000000000001",
"mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp":"0x00"
}
- 初始化创世节点
#geth --datadir db init gensis.json
- 进入JavaScript控制台
#geth --datadir db --networkid 2018 console
#启动节点
#geth --datadir db --rpc --rpcaddr=0.0.0.0 --rpcport 8545 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal,admin,ssh,txpool,debug,miner" --nodiscover --maxpeers 30 --networkid 2018 --port 30303 --mine --minerthreads 1 --etherbase "0x229540cbb3a429c6539a78cf0260b8e543c74404" console
#进入一个运行节点的console端
#geth --datadir db --networkid 2018 console
#账户1
#0x1667b9bf214edd9143824a14f101968e1cb35ba5
账户2
#0xd493c565090d27edfc90b037c4e9344c67af30ea
- #新建账户
#personal.newAccount("235681")
- 查看所有账户
#eth.accounts
- #查看账户余额
#balance=web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
- #设置挖矿地址
# miner.setEtherbase(eth.accounts[0])
- #查看是否成功
#eth.coinbase
- #启动挖矿
#miner.start(1)
- #停止挖矿
#miner.stop()
- 远程管理节点
- 查看节点信息
#admin.nodeInfo
- 添加其他节点
#admin.addPeer()
以太坊的编程接口
6.1 web3.js APIAPI
- #安装nodejs
#curl -sL https://rpm.nodesource.com/setup_10.x | bash -
#yum install nodejs
#npm -v
#npm init -y
#npm install --sava web3@0.20.0
- #查看账户
//初始化过程
var Web3 = require('web3');
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
var version = web3.version.api;
console.log(version);
// 查看节点所有可用账号
var accounts = web3.eth.accounts;
console.info(accounts);
//查看账户余额
var balance_1 = web3.eth.getBalance(web3.eth.accounts[0]);
var balance_eth = web3.fromWei(balance_1,'ether');
console.info(balance_eth.toString());