先上一篇的以太坊的私有搭建,交易,节点连接,结合搭建的步骤对以太坊进行深入介绍。本文介绍的方式在win10 和ubuntu16.04 的方式测试过。
第一步:geth的安装,请直接移步:https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum
或者百度其他博客。为了方便操作,windows 平台在安装完成geth后 ,最好将geth.exe 所在的目录添加到Path环境变量中以便于开发。linux 的方式推荐使用ppa的方式安装。如果不是也推荐奖geth 添加到环境变量里面。
第二步:创建创世块
先不要想太多直接复制我的创世块用:
新建一个文件夹名为mynode,然后在mynode 文件夹里面创建一个文件
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x80000000",
"difficulty": "0x01",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": { }
}
如果初始化出错请,检查json配置文件是否正常。为了避免各种错误,请直接复制我上面的genesis.json
第三步:打开控制台
geth --datadir "/newhome/blockchain/geth/mynode" --nodiscover console
运行结果
INFO [04-01|18:38:01] Maximum peer count ETH=25 LES=0 total=25
INFO [04-01|18:38:01] Starting peer-to-peer node instance=Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4
INFO [04-01|18:38:01] Allocated cache and file handles database=/newhome/blockchain/geth/mynode/geth/chaindata cache=768 handles=512
WARN [04-01|18:38:01] Upgrading database to use lookup entries
INFO [04-01|18:38:01] Initialised chain configuration config="{ChainID: 01 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Constantinople: <nil> Engine: unknown}"
INFO [04-01|18:38:01] Disk storage enabled for ethash caches dir=/newhome/blockchain/geth/mynode/geth/ethash count=3
INFO [04-01|18:38:01] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
INFO [04-01|18:38:01] Initialising Ethereum protocol versions="[63 62]" network=1
INFO [04-01|18:38:01] Loaded most recent local header number=0 hash=6099b6…abe624 td=1
INFO [04-01|18:38:01] Database deduplication successful deduped=0
INFO [04-01|18:38:01] Loaded most recent local full block number=0 hash=6099b6…abe624 td=1
INFO [04-01|18:38:01] Loaded most recent local fast block number=0 hash=6099b6…abe624 td=1
INFO [04-01|18:38:01] Regenerated local transaction journal transactions=0 accounts=0
INFO [04-01|18:38:01] Starting P2P networking
INFO [04-01|18:38:01] RLPx listener up self="enode://29af54c8bbc87acf38f2b259966a1c54ef25d5293c8b447080cbd8bd18fb3fc06e692c43a7ea8011708f217776420cc48fb8ef5d5aa3bcf39528a52963b0a8f2@[::]:30303?discport=0"
INFO [04-01|18:38:01] IPC endpoint opened url=/newhome/blockchain/geth/mynode/geth.ipc
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4
modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
常用命令介绍:
查看账户:eth.accounts
["0x45144978abe0e0bbfc187853c8dcef117fa86f89", "0xcad5cc7224b44db9192ef20e9e4263140fa0c1aa"]
创建账户,第一种方式:
web3.personal.newAccount("mu123456")
账户地址 : "0x79e27b1c9ce1ae7c7d51ee69fa6b2178af3c844e”
其中 mu123456 是密码 ,0x79e27b1c9ce1ae7c7d51ee69fa6b2178af3c844e
是账户
第二个账户:
用第二种方式创建
web3.personal.newAccount()
Passphrase:
Repeat passphrase:
"0x041fca7a8bbf27ee9a7b8300f31306210ce7da10"
查看账户余额:
web3.eth.getBalance(eth.accounts[0])
或者
eth.getBalance(eth.accounts[0])
或者
web3.eth.getBalance("0x74bb4ae9816788a6f81621b177096c2ecad075f2")
或者以“wei”展示:
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]),"ether")
挖矿
在js console 输入:
启动挖矿:miner.start()
停止挖矿:miner.stop()
查看是否正在挖矿中:eth.mining
查看节点信息:
> admin
运行结果:
{
datadir: "/newhome/blockchain/geth/mynode",
nodeInfo: {
enode: "enode://29af54c8bbc87acf38f2b259966a1c54ef25d5293c8b447080cbd8bd18fb3fc06e692c43a7ea8011708f217776420cc48fb8ef5d5aa3bcf39528a52963b0a8f2@[::]:30303?discport=0",
id: "29af54c8bbc87acf38f2b259966a1c54ef25d5293c8b447080cbd8bd18fb3fc06e692c43a7ea8011708f217776420cc48fb8ef5d5aa3bcf39528a52963b0a8f2",
ip: "::",
listenAddr: "[::]:30303",
name: "Geth/v1.8.2-stable-b8b9f7f4/linux-amd64/go1.9.4",
ports: {
discovery: 0,
listener: 30303
},
protocols: {
eth: {
config: {...},
difficulty: 27131985,
genesis: "0x6099b65e564bd511f49e8f39ba27b6a68b6b78fd1481592257f06bbf93abe624",
head: "0x39196960b218ff5638ca990750dae49c47bcf14ea2c52a77aec37d26cc1c0084",
network: 1
}
}
},
peers: [],
addPeer: function(),
clearHistory: function(),
exportChain: function(),
getDatadir: function(callback),
getNodeInfo: function(callback),
getPeers: function(callback),
importChain: function(),
removePeer: function(),
sleep: function github.com/ethereum/go-ethereum/console.(*bridge).Sleep-fm(),
sleepBlocks: function github.com/ethereum/go-ethereum/console.(*bridge).SleepBlocks-fm(),
startRPC: function(),
startWS: function(),
stopRPC: function(),
stopWS: function()
}
如果单单想看nodeInfo的信息可以执行:
admin.nodeInfo.enode
运行结果:
"enode://29af54c8bbc87acf38f2b259966a1c54ef25d5293c8b447080cbd8bd18fb3fc06e692c43a7ea8011708f217776420cc48fb8ef5d5aa3bcf39528a52963b0a8f2@[::]:30303?discport=0"
这样就可以选择一部分信息进行展示
交易:
以太坊在交易的时候,必须先解锁账户,执行挖矿,并且生产新的区块,交易才能完成。
解锁账户
personal.unlockAccount(eth.accounts[0])
web3.eth.sendTransaction({from:web3.eth.accounts[0] , to:web3.eth.accounts[1],value: web3.toWei(1, "ether")})
挖到新的区块,则交易完成。