一佳镜,新建工作目錄并創(chuàng)建創(chuàng)世區(qū)塊配置文件
$ mkdir private-geth
$ cd private-geth/
$ gedit gensis.json
gensis.json為創(chuàng)世區(qū)塊配置文件慕嚷,以下為內(nèi)容
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x66",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000001",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc" : {}
}
其中chainId指定了獨立的區(qū)塊鏈網(wǎng)絡ID洼冻。網(wǎng)絡ID在鏈接到其他節(jié)點的時候會用到杖玲,以太坊公網(wǎng)的網(wǎng)絡ID是1办素,為了不與公有鏈網(wǎng)絡沖突诞帐,要指定自己的網(wǎng)絡ID欣尼。不同ID網(wǎng)絡的節(jié)點無法相互連接。
二停蕉,初始化
$ geth --datadir "./db" init gensis.json
再看工作目錄愕鼓,生成了keystore和geth目錄,其中慧起,chaindata存放區(qū)塊數(shù)據(jù)菇晃,keystore存放賬戶數(shù)據(jù)
zhujiantao@ubuntu:~/eth/private-geth$ tree ../private-geth/
../private-geth/
├── db
│ ├── geth
│ │ ├── chaindata
│ │ │ ├── 000001.log
│ │ │ ├── CURRENT
│ │ │ ├── LOCK
│ │ │ ├── LOG
│ │ │ └── MANIFEST-000000
│ │ └── lightchaindata
│ │ ├── 000001.log
│ │ ├── CURRENT
│ │ ├── LOCK
│ │ ├── LOG
│ │ └── MANIFEST-000000
│ └── keystore
└── gensis.json
三,啟動節(jié)點
$ geth --datadir "./db" --networkid 10 console
參數(shù)datadir指定當前區(qū)塊鏈私鑰和網(wǎng)絡數(shù)據(jù)存放位置蚓挤,console表示啟動命令行模式磺送,可以在geth中執(zhí)行命令
四,進入JavaScript控制臺
通過attach命令灿意,連接一個已經(jīng)啟動的節(jié)點估灿,啟動Js命令環(huán)境
$ geth --datadir './db' attach ipc:./db/geth.ipc
在以太坊的JavaScript控制臺中內(nèi)置了一些對象,通過這些對象我們可以方便地與以太坊交互缤剧,
eth:提供操作區(qū)塊鏈相關(guān)方法
> eth
{
accounts: [],
blockNumber: 0,
coinbase: undefined,
compile: {
lll: function(),
serpent: function(),
solidity: function()
},
defaultAccount: undefined,
defaultBlock: "latest",
gasPrice: 18000000000,
hashrate: 0,
mining: false,
pendingTransactions: [],
protocolVersion: "0x3f",
syncing: false,
call: function(),
contract: function(abi),
estimateGas: function(),
filter: function(options, callback, filterCreationErrorCallback),
getAccounts: function(callback),
getBalance: function(),
getBlock: function(),
getBlockNumber: function(callback),
getBlockTransactionCount: function(),
getBlockUncleCount: function(),
getCode: function(),
getCoinbase: function(callback),
getCompilers: function(),
getGasPrice: function(callback),
getHashrate: function(callback),
getMining: function(callback),
getPendingTransactions: function(callback),
getProtocolVersion: function(callback),
getRawTransaction: function(),
getRawTransactionFromBlock: function(),
getStorageAt: function(),
getSyncing: function(callback),
getTransaction: function(),
getTransactionCount: function(),
getTransactionFromBlock: function(),
getTransactionReceipt: function(),
getUncle: function(),
getWork: function(),
iban: function(iban),
icapNamereg: function(),
isSyncing: function(callback),
namereg: function(),
resend: function(),
sendIBANTransaction: function(),
sendRawTransaction: function(),
sendTransaction: function(),
sign: function(),
signTransaction: function(),
submitTransaction: function(),
submitWork: function()
}
net:提供查看p2p網(wǎng)絡相關(guān)方法
> net
{
listening: true,
peerCount: 0,
version: "10",
getListening: function(callback),
getPeerCount: function(callback),
getVersion: function(callback)
}
admin:提供管理節(jié)點相關(guān)方法
> admin
{
datadir: "/home/zhujiantao/eth/private-geth/db",
nodeInfo: {
enode: "enode://1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f@[::]:30303",
id: "1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f",
ip: "::",
listenAddr: "[::]:30303",
name: "Geth/v1.8.12-stable-37685930/linux-amd64/go1.10.1",
ports: {
discovery: 30303,
listener: 30303
},
protocols: {
eth: {
config: {...},
difficulty: 102,
genesis: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
head: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
network: 10
}
}
},
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()
}
miner:提供啟動和停止挖礦方法
> miner
{
getHashrate: function(),
setEtherbase: function(),
setExtra: function(),
setGasPrice: function(),
start: function(),
stop: function()
}
personal:提供管理賬戶方法
> personal
{
listAccounts: [],
listWallets: [],
deriveAccount: function(),
ecRecover: function(),
getListAccounts: function(callback),
getListWallets: function(callback),
importRawKey: function(),
lockAccount: function(),
newAccount: function github.com/ethereum/go-ethereum/console.(*bridge).NewAccount-fm(),
openWallet: function github.com/ethereum/go-ethereum/console.(*bridge).OpenWallet-fm(),
sendTransaction: function(),
sign: function github.com/ethereum/go-ethereum/console.(*bridge).Sign-fm(),
signTransaction: function(),
unlockAccount: function github.com/ethereum/go-ethereum/console.(*bridge).UnlockAccount-fm()
}
txpool:提供查看交易內(nèi)存池方法
> txpool
{
content: {
pending: {},
queued: {}
},
inspect: {
pending: {},
queued: {}
},
status: {
pending: 0,
queued: 0
},
getContent: function(callback),
getInspect: function(callback),
getStatus: function(callback)
}
web3:除了以上對象中有的方法馅袁,還包含一些單位換算等其他方法
五,通過命令進行簡單交互
1荒辕,新建賬戶并挖礦
> eth.accounts
[]
> personal.newAccount("zhujiantao")
"0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121"
> personal.newAccount("hejiawei")
eth"0x86322ea53a15460f83a43ea3366974af37897e7c"
> eth.accounts
["0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121", "0x86322ea53a15460f83a43ea3366974af37897e7c"]
> eth.getBalance(eth.accounts[0])
0
> eth.getBalance(eth.accounts[1])
0
> miner.setEtherbase(eth.accounts[0])
true
> eth.coinbase
"0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121"
> miner.start(2)
2汗销,解鎖賬戶并交易
> personal.unlockAccount(eth.accounts[0], "zhujiantao")
true
> personal.unlockAccount(eth.accounts[1], "hejiawei")
true
> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:web3.toWei(1,"ether")})
"0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006"
> eth.getBalance(eth.accounts[0])
50000000000000000000
> eth.getBalance(eth.accounts[1])
0
> txpool.status
{
pending: 1,
queued: 0
}
> txpool.inspect.pending
{
0x49b70C9F35FBD0b39Fd98418BFB9DC25e6e08121: {
0: "0x86322ea53A15460f83A43ea3366974AF37897e7c: 1000000000000000000 wei + 90000 gas × 18000000000 wei"
}
}
以上操作分別是解鎖賬戶,發(fā)送交易抵窒,查看賬戶余額弛针,查看交易池狀態(tài),查看pending交易詳情
接下來挖礦打包交易
> miner.start(2);admin.sleepBlocks(1);miner.stop();
交易結(jié)束后查看余額
> eth.getBalance(eth.accounts[0])
54000000000000000000
> eth.getBalance(eth.accounts[1])
1000000000000000000
欸估脆,發(fā)現(xiàn)交易成功了G辗堋!!
還可以通過交易hash查看發(fā)起交易時交易詳情和交易被打包進區(qū)塊時的交易詳情
> eth.getTransaction("0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006")
{
blockHash: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
blockNumber: 11,
from: "0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121",
gas: 90000,
gasPrice: 18000000000,
hash: "0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006",
input: "0x",
nonce: 0,
r: "0x60b4d86aaa5edd3fbfced5ec713ad2031df60496e12384b0a2a9106382aaa137",
s: "0x1b792f70cbbb42aacc05032794cfb3be8168ef2e75f1e263db62a61cfeaf4e5a",
to: "0x86322ea53a15460f83a43ea3366974af37897e7c",
transactionIndex: 0,
v: "0x38",
value: 1000000000000000000
}
> eth.getTransactionReceipt("0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006")
{
blockHash: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
blockNumber: 11,
contractAddress: null,
cumulativeGasUsed: 21000,
from: "0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121",
gasUsed: 21000,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0xc7b07a186147f10aea58412ec75d25412248f6d1d3335291d74794646900660e",
to: "0x86322ea53a15460f83a43ea3366974af37897e7c",
transactionHash: "0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006",
transactionIndex: 0
}
3付材,查看區(qū)塊相關(guān)信息
> eth.blockNumber
11
> eth.getBlock("latest")
{
difficulty: 131072,
extraData: "0xd88301080c846765746888676f312e31302e31856c696e7578",
gasLimit: 4249054591,
gasUsed: 21000,
hash: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x49b70c9f35fbd0b39fd98418bfb9dc25e6e08121",
mixHash: "0xa9d585a50a989192e2c239f97f24e3283f13855f21c364bafebe531ba2bc0fc7",
nonce: "0x5ae360db4d1b56f7",
number: 11,
parentHash: "0x3470ce4807e6f1aba46fed960f3306526eac3085eb425b9e4cc74aa31fb1a14e",
receiptsRoot: "0x084e81eec67a61b4342ab5698d5798aadf822392a9579535d839d30e30388f85",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 651,
stateRoot: "0x701abf15ddb892bcdfc3f2127984506602db25c98c74321f2f50128b97d68c03",
timestamp: 1532233119,
totalDifficulty: 1443686,
transactions: ["0x9feaa87c9aa9375cfbffc767b71c486c46919a17c91c8d6e3468d02fbfb11006"],
transactionsRoot: "0x932fd9feba26c3996a34a7a8b0d74f65826671921d181d7aadac5a3d37992c24",
uncles: []
}
> eth.getBlock(0)
{
difficulty: 102,
extraData: "0x",
gasLimit: 4294967295,
gasUsed: 0,
hash: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000001",
number: 0,
parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 505,
stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
timestamp: 0,
totalDifficulty: 102,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}
以上操作分別是查詢區(qū)塊總數(shù)朦拖,查詢最新區(qū)塊,查詢Number為0區(qū)塊
4厌衔,遠程節(jié)點管理
查看節(jié)點信息
> admin.nodeInfo
{
enode: "enode://1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f@[::]:30303",
id: "1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f",
ip: "::",
listenAddr: "[::]:30303",
name: "Geth/v1.8.12-stable-37685930/linux-amd64/go1.10.1",
ports: {
discovery: 30303,
listener: 30303
},
protocols: {
eth: {
config: {
chainId: 10,
eip150Hash: "0x0000000000000000000000000000000000000000000000000000000000000000",
eip155Block: 0,
eip158Block: 0,
homesteadBlock: 0
},
difficulty: 1443686,
genesis: "0x559187669a7294b8d661a88bc6dca0ba8cd318263facbe01df17093d21b6e99d",
head: "0x50ddba18e51f81a93bd22bbc74039e580960a235b444bfdff82cee9145fe6642",
network: 10
}
}
}
添加其他節(jié)點璧帝,必須保證網(wǎng)絡相同和相同的networkid
> admin.addPeer("enode://1b034b641239a715e68806251af559208d254f9dc7c11bbb1457065db1b7a8f6f909de24da2a091809af262e99895c8a276d467eb939f56bdcdd05b4089a5e5f@[192.168.81.128]:30303")
true
查看已連接的遠程節(jié)點
> admin.peers