Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

11.14. JavaScript Console

11.14.1. personal 管理

11.14.1.1. 創建賬號

				
> personal.newAccount()
Passphrase: 
Repeat passphrase: 
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"				
				
			

指定密碼創建用戶

				
personal.newAccount("123")				
				
			

11.14.1.2. 列出賬號

列出所有賬號

				
> personal.listAccounts
["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"]				
				
			

列出指定賬號

				
> personal.listAccounts[1]
"0xe8abf98484325fd6afc59b804ac15804b978e607"

> personal.listAccounts[0]
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"				
				
			

11.14.1.3. 解鎖賬號

				
 > personal.unlockAccount(eth.accounts[0],"password")						
				
			

指定過期時間,單位是毫秒,下面例子是 20 分鐘

				
> personal.unlockAccount(eth.accounts[0],"password", 1000*60*20)				
				
			

11.14.2. eth 管理

11.14.2.1. 礦工賬號

查看預設曠工賬號,系統中的第一個賬號。

				
> eth.coinbase
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"				
				
			

查看賬號列表

				
> eth.accounts
["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"]	

> eth.accounts[0]
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"			
				
			

11.14.2.2. 餘額

				
> eth.getBalance(eth.accounts[0])
70000000000000000000
				
			
11.14.2.2.1. 單位轉換

eth.getBalance()返回的餘額是以太幣的最小面額wei,將wei轉換為以太幣ether。

				
primary = eth.accounts[0]
balance = web3.fromWei(eth.getBalance(primary), "ether");	
				
				

演示

				
> primary = eth.accounts[0]
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
> balance = web3.fromWei(eth.getBalance(primary), "ether");
70			
				
				
11.14.2.2.2. 一次檢查所有賬號餘額

定義函數

				
function checkAllBalances() {
  web3.eth.getAccounts(function(err, accounts) {
   accounts.forEach(function(id) {
    web3.eth.getBalance(id, function(err, balance) {
     console.log("" + id + ":\tbalance: " + web3.fromWei(balance, "ether") + " ether");
   });
  });
 });
};					
				
				

運行函數

				
checkAllBalances()					
				
				

輸出演示

				
> function checkAllBalances() {
...   web3.eth.getAccounts(function(err, accounts) {
.........    accounts.forEach(function(id) {
...............     web3.eth.getBalance(id, function(err, balance) {
.....................      console.log("" + id + ":\tbalance: " + web3.fromWei(balance, "ether") + " ether");
.....................    });
...............   });
.........  });
... };
undefined
> checkAllBalances()
0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6:	balance: 929 ether
0xe8abf98484325fd6afc59b804ac15804b978e607:	balance: 11 ether
undefined					
				
				

11.14.2.3. 解鎖賬號

			
> personal.unlockAccount(eth.accounts[0],"password",50000)
true
			
			

11.14.2.4. 轉賬

			
personal.unlockAccount("0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "", 300)	
eth.sendTransaction({from: '0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6', to: '0xe8abf98484325fd6afc59b804ac15804b978e607', value: web3.toWei(1, "ether")})
			
			
				
預設礦工賬號
				
> eth.coinbase
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"

查看系統中的賬號,如果沒有請參考上面章節創建

> eth.accounts
["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"]

轉出賬號中又 285 個以太幣

> web3.fromWei(eth.getBalance(eth.accounts[0]))
285

轉入賬號目前是 0 

> web3.fromWei(eth.getBalance(eth.accounts[1]))
0

解鎖傳出賬號,否則不能轉出。personal.unlockAccount(賬號, 密碼, 300)

> personal.unlockAccount("0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "", 300)
true

轉賬操作

> eth.sendTransaction({from: '0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6', to: '0xe8abf98484325fd6afc59b804ac15804b978e607', value: web3.toWei(10, "ether")})
"0xb0674a7fee52555d8712f3a1f0f30fbbbf67ff7b5b4b53ab5d131262613215c6"

如果你現在查看轉入賬號,你會發現餘額仍然是 0 ,交易還未成功寫進區塊,寫進區塊的方式是挖礦,所以你必須執行挖礦

> miner.start(1)
null

稍後幾分鐘,再次查看轉入賬號,將會看到有10個以太幣入賬。傳出賬號會減少10個以太幣,同時仍然繼續挖礦中。

> web3.fromWei(eth.getBalance(eth.accounts[1]))
10
				
			

從賬號0想賬號1轉賬

				
> personal.unlockAccount(eth.accounts[0],"password",50000)
true
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(1, "ether")})				
				
			

11.14.2.5. 查看掛起的交易

				
> eth.pendingTransactions
[{
    blockHash: null,
    blockNumber: null,
    from: "0x5fba50fce50baf0b8a7314200ba46336958ac97e",
    gas: 90000,
    gasPrice: 20000000000,
    hash: "0x51a75422f79fa96e70a0c1481851bc9f827868c44203b68d74f9815ffb367d5f",
    input: "0x",
    nonce: 0,
    r: "0x5632a8ade4a767dbd949ba1042cb33f98dd0722ab999ba18e1454d19d8bd1f6d",
    s: "0x515dcfa3de297f0c956ad9a061a5561f47cc9ccbb0a547cda59193c77fcbe3f7",
    to: "0x0a8c35653d8b229c16f0c9ce6f63cffb877cfdcf",
    transactionIndex: 0,
    v: "0x42",
    value: 1000000000000000000
}]				
				
			

如果返回空值,表示交易全部完成。

				
> eth.pendingTransactions
[]				
				
			

11.14.2.6. 查看當前區塊總數

				
> eth.blockNumber 
719			
				
			

11.14.2.7. 查看當前Gas價格

			
> web3.eth.gasPrice
5000000000

> web3.fromWei(web3.eth.gasPrice.toString())
"0.000000018"
			
			

11.14.2.8. 評估執行花費的GAS

			
> eth.estimateGas({from:eth.accounts[1], to: eth.accounts[2], value:12340000000000000000})
21000
			
			

11.14.2.9. 查看區塊信息

				
> eth.getBlock(1) 
{
  difficulty: 131072,
  extraData: "0xd783010702846765746885676f312e398777696e646f7773",
  gasLimit: 4995119,
  gasUsed: 0,
  hash: "0x62bc2fc0fd647b43013ba75e65a3f16520cebb60cadc0995965c891d4266a88c",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xba1821227ebad7e88a51078f6219b392fd601822",
  mixHash: "0x80ad2e29d057e0f6bb9d03f12a252fdc65cb520248fa0023ae6624c997b5d5f2",
  nonce: "0x3d743ef28ba8c8c8",
  number: 1,
  parentHash: "0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 535,
  stateRoot: "0x418d9f66e33cbd41fd524b87c10c5fc75f8a6360896967d827a2d56a0c8aadda",
  timestamp: 1510715249,
  totalDifficulty: 262144,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
> eth.getBlock(2)
{
  difficulty: 131072,
  extraData: "0xd783010702846765746885676f312e398777696e646f7773",
  gasLimit: 4990242,
  gasUsed: 0,
  hash: "0x3323c566fd3501c29e8b026c4a9d2e83a8dd62d153a32b5f879120e999013d07",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0xba1821227ebad7e88a51078f6219b392fd601822",
  mixHash: "0xc415fc64f1bc13842455f8289d33842260c1578a32b5befded5b4293e7a92276",
  nonce: "0x4f4a0e65412663db",
  number: 2,
  parentHash: "0x62bc2fc0fd647b43013ba75e65a3f16520cebb60cadc0995965c891d4266a88c",
  receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 535,
  stateRoot: "0x39ccb22ff5ca19a2340e137fa64acedb704d42827a753780b53a0aff8922e403",
  timestamp: 1510715341,
  totalDifficulty: 393216,
  transactions: [],
  transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
  uncles: []
}
				
				
			

11.14.2.10. 返回交易信息

			
> web3.eth.getTransaction("0x4e27a477e128b200239bc2ecd899077c6ae064da963a919fef41bcc7462aec8d")
{
  blockHash: "0x59a9905831e7ae3cb9e7c6f125cf48e2688ef4b39317838f6f6b6c8837d01404",
  blockNumber: 4367,
  from: "0x8efb99ec55bcfbe2cfe47918f2d9e55fa732111f",
  gas: 21000,
  gasPrice: 18000000000,
  hash: "0x4e27a477e128b200239bc2ecd899077c6ae064da963a919fef41bcc7462aec8d",
  input: "0x",
  nonce: 15,
  r: "0xa297401df3a1fb0298cbc1dd609deebe9ded319fadc55934ecef4d525198215",
  s: "0x780d8c46bc8d1bb89ae9d78055307d9d68a4f89ba699ef86d3f8ba88383139a6",
  to: "0xf0688330101d53bd0c6ede2ef04d33c2010e9a5d",
  transactionIndex: 0,
  v: "0x42",
  value: 999622000000000000
}
			
			

11.14.2.11. 返回交易收據

			
> web3.eth.getTransactionReceipt("0x4e27a477e128b200239bc2ecd899077c6ae064da963a919fef41bcc7462aec8d")
{
  blockHash: "0x59a9905831e7ae3cb9e7c6f125cf48e2688ef4b39317838f6f6b6c8837d01404",
  blockNumber: 4367,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  from: "0x8efb99ec55bcfbe2cfe47918f2d9e55fa732111f",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0x69d278e7594e8a5affbebc84380516437f3e4b241b272e6eaebf752af9c76047",
  to: "0xf0688330101d53bd0c6ede2ef04d33c2010e9a5d",
  transactionHash: "0x4e27a477e128b200239bc2ecd899077c6ae064da963a919fef41bcc7462aec8d",
  transactionIndex: 0
}
			
			

返回值:



Object - 交易的收據對象,如果找不到返回null 
blockHash: String - 32位元組,這個交易所在區塊的哈希。
blockNumber: Number - 交易所在區塊的塊號。
transactionHash: String - 32位元組,交易的哈希值。
transactionIndex: Number - 交易在區塊裡面的序號,整數。
from: String - 20位元組,交易發送者的地址。
to: String - 20位元組,交易接收者的地址。如果是一個合約創建的交易,返回null。
cumulativeGasUsed: Number - 當前交易執行後累計花費的gas總值10。
gasUsed: Number - 執行當前這個交易單獨花費的gas。
contractAddress: String - 20位元組,創建的合約地址。如果是一個合約創建交易,返回合約地址,其它情況返回null。
logs: Array - 這個交易產生的日誌對象數組。

11.14.2.12. eth.syncing 同步狀態

			
> eth.syncing
{
  currentBlock: 645835,
  highestBlock: 5439559,
  knownStates: 1090968,
  pulledStates: 1080746,
  startingBlock: 0
}			
			
			

顯示百分比

			
console.log(parseInt(eth.syncing.currentBlock/eth.syncing.highestBlock*100,10)+'%')		
			
			

剩餘塊數

			
eth.syncing.highestBlock - eth.syncing.currentBlock

setInterval(function(){
	console.log(eth.syncing.highestBlock - eth.syncing.currentBlock)
},5000);
			
			

進度監控

			
var lastPercentage = 0;var lastBlocksToGo = 0;var timeInterval = 10000;
setInterval(function(){
    var percentage = eth.syncing.currentBlock/eth.syncing.highestBlock*100;
    var percentagePerTime = percentage - lastPercentage;
    var blocksToGo = eth.syncing.highestBlock - eth.syncing.currentBlock;
    var bps = (lastBlocksToGo - blocksToGo) / (timeInterval / 1000)
    var etas = 100 / percentagePerTime * (timeInterval / 1000)

    var etaM = parseInt(etas/60,10);
    console.log(parseInt(percentage,10)+'% ETA: '+etaM+' minutes @ '+bps+'bps');

    lastPercentage = percentage;lastBlocksToGo = blocksToGo;
},timeInterval);
			
			

11.14.2.13. 查看智能合約編譯器

				
> eth.compile
{
  lll: function(),
  serpent: function(),
  solidity: function()
}				
				
			

11.14.3. web3

11.14.3.1. Ether幣的基本單位

Ether幣最小的單位是Wei,也是命令行預設的單位, 然後每1000個進一個單位,依次是



kwei (1000 Wei)
mwei (1000 KWei)
gwei (1000 mwei)
szabo (1000 gwei)
finney (1000 szabo)
ether (1000 finney)

如何進行ether 和 Wei之間的轉換,簡單地說就是就是1 以太幣 = 1000000000000000000 Wei (這就是上一站章中為何我們轉移0.01個以太幣,結果卻顯示很長的原因)

單位轉換

					
> web3.fromWei(10000000000000000,"ether")
"0.01"				
					
			

11.14.3.2. web3.toWei

						
Ether–> Wei

> web3.toWei(1)
"1000000000000000000"
> web3.toWei(1.3423423)
"1342342300000000000"
> web3.toWei(0.00034)
"340000000000000"						
						
			

11.14.3.3. web3.fromWei

						
Wei –> Ether

> web3.fromWei(10000000000000000)
"0.01"
> web3.fromWei(1000000000000000000)
"1"
>
						
			

11.14.4. admin 管理

		
> admin
{
  datadir: "/home/ethereum/.ethereum",
  nodeInfo: {
    enode: "enode://89a7bfd4472dff266ccdccc76bc75586a9005144df54d4575a9e07d9b2aee8aae6c8004cc98c6212d5f0f4d5e3a524617abd7e7d6f39d5df053f94b7250e5610@[::]:30303?discport=0",
    id: "89a7bfd4472dff266ccdccc76bc75586a9005144df54d4575a9e07d9b2aee8aae6c8004cc98c6212d5f0f4d5e3a524617abd7e7d6f39d5df053f94b7250e5610",
    ip: "::",
    listenAddr: "[::]:30303",
    name: "Geth/v1.8.2-stable/linux-amd64/go1.8.3",
    ports: {
      discovery: 0,
      listener: 30303
    },
    protocols: {
      les: {
        config: {...},
        difficulty: 17179869184,
        genesis: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
        head: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
        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()
}		
		
		

11.14.4.1. 看看 networkid

				
> admin.nodeInfo.protocols.eth.network
123456				
				
			

11.14.4.2. 節點管理

可以通過admin.addPeer()方法連接到其他節點,兩個節點要想聯通,必須保證網絡是相通的,並且要指定相同的networkid

[提示]提示

注意去掉 --nodiscover 參數

確保網絡可用

				
> net.listening
true
				
			
11.14.4.2.1. 顯示節點

顯示當前節點信息

					
> admin.nodeInfo
{
  enode: "enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@14.103.209.119:30303",
  id: "9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527",
  ip: "14.103.209.119",
  listenAddr: "[::]:30303",
  name: "Geth/v1.7.3-stable-4bb3c89d/linux-amd64/go1.9.1",
  ports: {
    discovery: 30303,
    listener: 30303
  },
  protocols: {
    eth: {
      difficulty: 108754979,
      genesis: "0x611596e7979cd4e7ca1531260fa706093a5492ecbdf58f20a39545397e424d04",
      head: "0x61330b27cfbfaecbb36bb8666cbe0564c1e0bdecfdcd153622d8c2ca2b82786e",
      network: 123456
    }
  }
}					
					
				

節點地址

					
> admin.nodeInfo.enode 
"enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@[::]:30303?discport=0"
					
				

[::] 是 ipv6 地址,可以改為 ipv4 地址。

11.14.4.2.2. 添加節點
					
> admin.addPeer('enode://9f6490ffb5236f2ddc5710ae73d47c740e0a3644bbd2d67029cf4a6c4693d2f470b642fd2cc3507f7e851df60aaeb730a1270b7a477f91ec5b6b17a8a4b40527@172.16.0.1:30303')				
					
				
				

admin.addPeer('enode://6427b7e7446bb05f22fe7ce9ea175ec05858953d75a5a6e4f99a6aec0779a8bd6276f1959a42fe5948acbe14bcd0652082dc546d3b37ae8f2aea41eba4eca43b@121.201.14.181:30303');
admin.addPeer('enode://91922b12115c067005c574844c6bbdb114eb262f90b6355cec89e13b483c3e4669c6d63ec66b6e3ca7a3a462d28edb3c659e9fa05ed4c7234524e582a8816743@120.27.164.92:13333');
admin.addPeer('enode://3dde41a994b3b99f938f75ddf6d48318c78ddd869c70b48d00b922190bb434fc5474f6250c143723f4387273d123e02f6a38f07d0311f240d2915f6140e09850@207.226.141.212:30303');
admin.addPeer('enode://7ab8fa90b204f2146c00939b8474549c544caa3598a0894fa639a5cdbd992cbc6135fd776f8bcf97ae95fdaa3afbfa2d107fea71549119afd7ea57356b899be5@121.201.24.236:30303');
admin.addPeer('enode://db81152a8296089b04a21ad9bf347df3ff0450ffc8215d9f50c400ccf8d18963118010cacf03c4b71981cf9cac5394438cab3039e98db4d2aae5859ab7d1793e@139.198.1.244:30303');
admin.addPeer('enode://68dd1360f0a4ac362b41124692e31652ffe26f6f06a284ca11f3b514b3968594ac1f4320d1aa1ca343b06327c18a2e40eded74edfb3086e1baaa27ca24226b21@113.106.85.172:30303');
admin.addPeer('enode://58f6b6908286cefe43c166cfc4fed033c750caa1bc3f6e1e1e1507752c0b91248addb3122f8557c5f8912e702285a160ab3a10203ae1eff3807eda25d6ed6478@45.113.71.186:30303');
admin.addPeer('enode://87190a01c02cafb97e7f49672b4c3be2937cf79c3969e0b8e7b35cac28cebfbda52a13d56fd2113c726a1dd359c9476ccf7e60651439cef56e3a71039f6a4f5e@119.29.207.90:30303');
admin.addPeer('enode://d1fdd05a62fd9544eeb455e4f4d4bd8bb574138d82d8f909f3041d0792e3401f8695133d39ad0a3aa5d217e3c5bed0511b531505a67b03607a909ae9096720d2@120.26.129.121:30303');
admin.addPeer('enode://a1e9cf99eca94590ae776c8dd5c6c043a8c1f0375e9e391c9fb55133385bf453ac3d3fb3ead8e63415b2ef99d54a19e2a7bc830cd1fdbbb283818e3bcb0ea31e@182.254.209.254:30303');
admin.addPeer('enode://562796b19d43d79dfb6160abd2d7bb78a2f2efd9501a0a767c00677e0fb3a4407235f813c3003682c2a421a58709c52f595827bc15708cc5f534f55d0f8d03ad@121.40.199.54:30303');
admin.addPeer('enode://fa2c17dcc83a6e2825668210abf7480452de4b13d8bdea8f301c3b603701918bc4dade9e68d119d7a8214e90e7ea10a2782041c98951385d97bee73358fb08f4@120.26.124.58:30303');
admin.addPeer('enode://0b331b27e2976d797aed1d1464ac483a7f262860334cb5737a01a0188da08d79226a6973adc5f2a2c1a20192b399161eee23a0d56ecf472cbe4058d010ecc89f@47.89.49.61:30303');
admin.addPeer('enode://0639f20fdb5af1fecd2f2bc0ddb648885483a5945686530e6b046678635d3435dd7b92fe34209f81ec6f003482aa78e407e5e6eb1b10be4773a2adbcf1fc1ba6@118.192.161.147:30303');
admin.addPeer('enode://fd2a5d30e4f3917ee640876cc57d72a8bf5ecf049e9106c95e60cf306dd7a5dd68d1a295f3718af44a7083252686926d6e8a402f1abe6f805e10e7281967db28@121.201.29.82:30303');
admin.addPeer('enode://0d1b9eed7afe2d5878d5d8a4c2066b600a3bcac2e5730586421af224e93a58cd03cac75bf0b2a62fd8049cd3692a085758cc1e407c8b2c94bb069814a5e8d0f0@209.9.106.245:30303');
admin.addPeer('enode://ca087a651571d04953187753af969f7deb1582af2a06a3048b90adb3f87d4c41973aac4b5e80449efc97154dac769a5ea447b123c3aaf7a2c23825a1558804dc@182.150.37.23:30303');
admin.addPeer('enode://9b53b9d41d964f71db60d2198cfa9013fc7808d707c5e0a32da1e22d3cacd6adbae46901df6506a752d9d4e3791df29171315fbb86f7b09331a25458158fe65b@182.150.37.24:30303');			

				
				
11.14.4.2.3. 查看節點

查看節點數量

					
> net.peerCount
1					
					
				

查看節點地址

					
> admin.peers
[{
    caps: ["eth/62", "eth/63", "par/1", "par/2", "pip/1"],
    id: "a7bbd8fb72e02681b027908f14fd2dbd80e35a1477d7d9d4dc19ed34420be26fe9f991c83a83e4ab8aa371ffbb149494471f30216bc2f662d1ebc6d01811c7a2",
    name: "Parity/v1.7.12-stable-9b796e8-20180121/x86_64-linux-gnu/rustc1.21.0",
    network: {
      localAddress: "172.16.0.1:34092",
      remoteAddress: "52.67.171.152:30388"
    },
    protocols: {
      eth: "handshake"
    }
}]				
					
				

列出節點IP地址

				
admin.peers.forEach(function(p) {console.log(p.network.remoteAddress);})	
				
				
				
> admin.peers.forEach(function(p) {console.log(p.network.remoteAddress);})
52.90.40.206:30303
120.79.161.22:30303
13.229.198.234:30303
169.0.182.34:30303
23.111.151.158:30304
120.27.196.141:30303
118.193.93.174:30303
35.229.242.1:30303
159.203.32.64:30303
undefined
>				
				
				
11.14.4.2.4. networkid
			
> admin.nodeInfo.protocols.les.network
1			
			
				

11.14.5. miner 挖礦管理

11.14.5.1. 開始挖礦

				
> miner.start(2)
null			
				
			

過幾分鐘後運行

				
> web3.fromWei(eth.getBalance(eth.coinbase), "ether")
30					
				
			

這時我們已經看到已經產生了30個以太幣。

11.14.5.2. 停止挖礦

				
> miner.stop()
true
>
				
			

11.14.5.3. 設置預設礦工賬號

預設挖礦使用系統中的第一個賬號,你可以使用 miner.setEtherbase() 指定賬號。

				
> miner.setEtherbase("0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6")
true	
				
			

				
> eth.accounts
["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607", "0x013b5e735e1b48421dd3de3b931d6f03e769e22b"]

> eth.coinbase
"0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"


> miner.setEtherbase("0xe8abf98484325fd6afc59b804ac15804b978e607")
true

> eth.coinbase
"0xe8abf98484325fd6afc59b804ac15804b978e607"	
				
			

11.14.6. txpool 管理

11.14.6.1. txpool.status

查看狀態

				
> txpool.status 
{
  pending: 0,
  queued: 0
}
				
			

例如做一筆轉賬

				
> amount = web3.toWei(5,'ether')  
"5000000000000000000"  
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})	

> txpool.status 
{
  pending: 1,
  queued: 0
}

> miner.start(1);admin.sleepBlocks(1);miner.stop(); 

> txpool.status 
{
  pending: 0,
  queued: 0
}

> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether')  
5  
				
			

11.14.7. net

11.14.7.1. 監聽狀態

				
> net.listening
true