- genesis
{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x400",
"extraData" : "0x00",
"gasLimit" : "0x2fefd8",
"nonce" : "0xdeadbeefdeadbeef",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc" : {}
}
header 1 | header 2 |
---|---|
mixhash | 与nonce配合用于挖矿,由上一个区块的一部分生成的hash。注意他和nonce的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。 |
nonce | nonce就是一个64位随机数,用于挖矿,注意他和mixhash的设置需要满足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章节所描述的条件。 |
difficulty | 设置当前区块的难度,如果难度过大,cpu挖矿就很难,这里设置较小难度 |
alloc | 用来预置账号以及账号的以太币数量,因为私有链挖矿比较容易,所以我们不需要预置有币的账号,需要的时候自己创建即可以。 |
coinbase | 矿工的账号,随便填 |
timestamp | 设置创世块的时间戳 |
parentHash | 上一个区块的hash值,因为是创世块,所以这个值是0 |
extraData | 附加信息,随便填,可以填你的个性信息 |
gasLimit | 该值设置对GAS的消耗总量限制,用来限制区块能包含的交易信息总和,因为我们是私有链,所以填最大。 |
- geth –datadir data0 init genesis.json //#init ``` root@iZ2zeb5n81znirjwr3s29iZ:/home/privatechain# geth –datadir data0 init genesis.json INFO [04-15|11:00:21] Maximum peer count ETH=25 LES=0 total=25 INFO [04-15|11:00:21] Allocated cache and file handles database=/home/privatechain/data0/geth/chaindata cache=16 handles=16 INFO [04-15|11:00:21] Writing custom genesis block INFO [04-15|11:00:21] Persisted trie from memory database nodes=0 size=0.00B time=2.87µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [04-15|11:00:21] Successfully wrote genesis state database=chaindata hash=b240e0…e37e4e INFO [04-15|11:00:21] Allocated cache and file handles database=/home/privatechain/data0/geth/lightchaindata cache=16 handles=16 INFO [04-15|11:00:21] Writing custom genesis block INFO [04-15|11:00:21] Persisted trie from memory database nodes=0 size=0.00B time=1.962µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B INFO [04-15|11:00:21] Successfully wrote genesis state database=lightchaindata hash=b240e0…e37e4e
3. 启动私链
networkid 和 genesis中的chainId一致
console 参数直接进入geth命令行
root@iZ2zeb5n81znirjwr3s29iZ:/home/privatechain# geth –datadir data0 –networkid 10 console
4. 私钥
eth.accounts personal.newAccount()
创建账户后,在home/privatechain/data0/keystore 目录会产生私钥,文件名类似这样UTC--2018-04-23T07-29-12.122743864Z--d75372568204bbd5c77a46c1da9f0d41268dfaef
。vi查看,私钥json格式如下:
{ “address”: “d75372568204bbd5c77a46c1da9f0d41268dfaef”, “crypto”: { “cipher”: “aes-128-ctr”, “ciphertext”: “fe8a952c99e80ff951e39424071bb7f12c018c8ffcf749999fe350144595ef1f”, “cipherparams”: { “iv”: “8d48a97fa7b2b6cac1f90b75244f3355” }, “kdf”: “scrypt”, “kdfparams”: { “dklen”: 32, “n”: 262144, “p”: 1, “r”: 8, “salt”: “ce650bba50b450773cfaed02d3f25c127a8ac4d914844df09e71c841a7031a0d” }, “mac”: “04cdde0fbb8c86011df64af9e0a4b19dd906e3eb59ba04c278b3fe4280f01c3e” }, “id”: “d7b9822d-1e0e-4e1c-84e3-a3dda56225da”, “version”: 3 }
eth.accounts miner.start() miner.stop()
eth.blockNumber 54
eth.getBalance(eth.accounts[0]) 270000000000000000000 //270个以太币
personal.unlockAccount(eth.accounts[0]) Unlock account 0xd75372568204bbd5c77a46c1da9f0d41268dfaef Passphrase: //huangl true
eth.sendTransaction({from:’0xd75372568204bbd5c77a46c1da9f0d41268dfaef’,to:’0x4f9d4d9aaa7f8f7f407084b6216e3a236f2166a4’}) INFO [04-25|11:25:11] Submitted transaction fullhash=0x6989d7591653b83b441be05f18e57b6505d0ab6bc859c7f794ad9644c66d6c2e recipient=0x4F9d4D9aaA7f8F7F407084B6216e3a236f2166a4 “0x6989d7591653b83b441be05f18e57b6505d0ab6bc859c7f794ad9644c66d6c2e”
//矿池里未打包交易
txpool.status { pending: 1, queued: 0 }
miner.start() INFO [04-25|11:28:59] Updated mining threads threads=0 INFO [04-25|11:28:59] Transaction pool price threshold updated price=18000000000 null INFO [04-25|11:28:59] Starting mining operation INFO [04-25|11:28:59] Commit new mining work number=55 txs=1 uncles=0 elapsed=342.885µs INFO [04-25|11:29:00] Successfully sealed new block number=55 hash=0df732…5ee045 INFO [04-25|11:29:00] 🔗 block reached canonical chain number=50 hash=0e93be…0fa261 INFO [04-25|11:29:00] 🔨 mined potential block number=55 hash=0df732…5ee045 INFO [04-25|11:29:00] Commit new mining work number=56 txs=0 uncles=0 elapsed=225.896µs INFO [04-25|11:29:04] Successfully sealed new block number=56 hash=95257e…d09b21 INFO [04-25|11:29:04] 🔗 block reached canonical chain number=51 hash=49d87b…ef8801 INFO [04-25|11:29:04] 🔨 mined potential block number=56 hash=95257e…d09b21
miner.stop()
eth.getBlock(55) { difficulty: 131072, extraData: “0xd783010802846765746887676f312e392e34856c696e7578”, gasLimit: 3314771, gasUsed: 21000, hash: “0x0df73232b798698ba60fb7ef8dca3bd0b27dd6295bca3ef30c8fc508e15ee045”, logsBloom: “0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”, miner: “0xd75372568204bbd5c77a46c1da9f0d41268dfaef”, mixHash: “0x72d60bb589a0f1c3f52e5eba3cf0e6b22e3a72e9a9cf571f048fdd5ee34a3d76”, nonce: “0x4dd1dcaf80269bc7”, number: 55, parentHash: “0x9810d51909e8938ba6683423ca5813da4d698b8c9a8c2205e6b297f3dbccd488”, receiptsRoot: “0x9fa8fefc94a8689a93c195201d18c89d99b9dfb9a737b3fdfbac988aa554bdb6”, sha3Uncles: “0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347”, size: 641, stateRoot: “0x7972dfa3aee60820baaa5911b5af8a807b64fa25da3927aa350db41162fbdee8”, timestamp: 1524626939, totalDifficulty: 7297346, transactions: [“0x6989d7591653b83b441be05f18e57b6505d0ab6bc859c7f794ad9644c66d6c2e”], transactionsRoot: “0x061ff7d57ba9a5c00215a740a9442108f86f137028833eb6059f31a67dc7fb4c”, uncles: [] }
eth.getTransaction(‘0x6989d7591653b83b441be05f18e57b6505d0ab6bc859c7f794ad9644c66d6c2e’) { blockHash: “0x0df73232b798698ba60fb7ef8dca3bd0b27dd6295bca3ef30c8fc508e15ee045”, blockNumber: 55, from: “0xd75372568204bbd5c77a46c1da9f0d41268dfaef”, gas: 90000, gasPrice: 18000000000, hash: “0x6989d7591653b83b441be05f18e57b6505d0ab6bc859c7f794ad9644c66d6c2e”, input: “0x”, nonce: 0, r: “0x799726830061e1a30bd783221b83dbf7865aec641c59c9b19b0bc4065186d6b8”, s: “0x10d4b29d8482bb28ff755c5bd8452e303d09813752bd191d33a534275e549581”, to: “0x4f9d4d9aaa7f8f7f407084b6216e3a236f2166a4”, transactionIndex: 0, v: “0x38”, value: 0 }
//完整transaction,value单位是最小单位
eth.sendTransaction({from:’0xd75372568204bbd5c77a46c1da9f0d41268dfaef’,to:’0x4f9d4d9aaa7f8f7f407084b6216e3a236f2166a4’,gas:31000, ‘gasPrice’: web3.toWei(300, ‘gwei’), “value”: “1”})
eth.getBalance(eth.accounts[1]) 1 eth.getBalance(eth.accounts[0]) 304999999999999999999 eth.blockNumber 61
//查看旷工有多少币
eth.getBlock(61).miner “0xd75372568204bbd5c77a46c1da9f0d41268dfaef” eth.getBalance(eth.getBlock(61).miner) 0
//查看区块同步情况
eth.syncing false
出现如下问题
Ethereum private chain resets back to block 0 when restarted
geth版本从1.8.2退回到1.7.3
apt-get autoremove 删除为了满足依赖而安装的,但现在不再需要的软件包(包括已安装包),保留配置文件。
apt-get remove 删除已安装的软件包(保留配置文件),不会删除依赖软件包,且保留配置文件。
geth1.7.2版本安装
git clone https://github.com/ethereum/go-ethereum.git cd go-ethereum git checkout v1.7.2 make geth make all
make geth需要go1.7版本以上,系统apt安装最新仅为1.6版本
#cd /etc #vi profile 在文件末尾加入
export GOROOT=/usr/local/go export GOPATH=/home/como/gosrc export GOBIN=$GOROOT/bin export GETHBIN=/home/privatechain/go-ethereum/build/bin export PATH=$PATH:$GOROOT/bin:$GETHBIN
source profile
echo $PATH
删除usr/bin下的go文件(1.6版本)
geth make 后,也要把bin目录加到path中
```