三、私有链基本操作

创建创世区块

1
2
3
mkdir one
cd one
vi genesis.json

genesis.json的内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc" : { }
}

创建区块链和基本操作

1
2
3
4
5
6
7
8
9
10
geth init ./genesis.json --datadir "./chain"
geth \
--datadir "./chain" \
--nodiscover \
console 2>>eth_output.log
web3.eth.accounts
<!-- more -->
web3.personal.newAccount("123456")
web3.personal.newAccount()
web3.eth.accounts

挖矿

1
2
3
4
5
miner.start(1)
miner.stop()
web3.eth.getBalance(web3.eth.accounts[0])
web3.eth.getBalance(web3.eth.accounts[1])
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0])

函数

1
2
3
4
5
6
7
8
9
10
11
function checkAllBalances() {
var totalBal = 0;
for (var acctNum in eth.accounts) {
var acct = eth.accounts[acctNum];
var acctBal = web3.fromWei(eth.getBalance(acct), "ether");
totalBal += parseFloat(acctBal);
console.log(" eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");
}
console.log(" Total balance: " + totalBal + " ether");
};
checkAllBalances()

转账

1
2
3
4
5
6
7
8
acc0 = web3.eth.accounts[0]
acc1 = web3.eth.accounts[1]
web3.personal.unlockAccount(acc0,"123456")
web3.eth.sendTransaction({from:acc0,to:acc1,value:web3.toWei(3,"ether")})
checkAllBalances()
miner.start()
checkAllBalances()
web3.eth.blockNumber

坚持原创技术分享,您的支持将鼓励我继续创作!