知乎专栏 | 多维度架构 |
> personal.newAccount() Passphrase: Repeat passphrase: "0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
指定密码创建用户
personal.newAccount("123")
列出所有账号
> personal.listAccounts ["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"]
列出指定账号
> personal.listAccounts[1] "0xe8abf98484325fd6afc59b804ac15804b978e607" > personal.listAccounts[0] "0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
查看默认旷工账号,系统中的第一个账号。
> eth.coinbase "0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
查看账号列表
> eth.accounts ["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607"] > eth.accounts[0] "0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"
> eth.getBalance(eth.accounts[0]) 70000000000000000000
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
定义函数
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
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")})
> 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 []
> web3.eth.gasPrice 5000000000 > web3.fromWei(web3.eth.gasPrice.toString()) "0.000000018"
> eth.estimateGas({from:eth.accounts[1], to: eth.accounts[2], value:12340000000000000000}) 21000
> 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: [] }
> 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 }
> 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 - 这个交易产生的日志对象数组。
> 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);
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"
Ether–> Wei > web3.toWei(1) "1000000000000000000" > web3.toWei(1.3423423) "1342342300000000000" > web3.toWei(0.00034) "340000000000000"
> 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() }
可以通过admin.addPeer()方法连接到其他节点,两个节点要想联通,必须保证网络是相通的,并且要指定相同的networkid
提示 | |
---|---|
注意去掉 --nodiscover 参数 |
确保网络可用
> net.listening true
显示当前节点信息
> 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 地址。
> 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');
查看节点数量
> 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 >
> miner.start(2) null
过几分钟后运行
> web3.fromWei(eth.getBalance(eth.coinbase), "ether") 30
这时我们已经看到已经产生了30个以太币。
默认挖矿使用系统中的第一个账号,你可以使用 miner.setEtherbase() 指定账号。
> miner.setEtherbase("0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6") true
> eth.accounts ["0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6", "0xe8abf98484325fd6afc59b804ac15804b978e607", "0x013b5e735e1b48421dd3de3b931d6f03e769e22b"] > eth.coinbase "0x83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6" > miner.setEtherbase("0xe8abf98484325fd6afc59b804ac15804b978e607") true > eth.coinbase "0xe8abf98484325fd6afc59b804ac15804b978e607"
查看状态
> 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