Skip to content

Create A Private Ethereum Chain

changwu edited this page Mar 8, 2016 · 1 revision

參考文章

安裝 go-ethereum

$ brew tap ethereum/ethereum
$ brew install ethereal
$ geth account new

Your new account is locked with a password. Please give a password. Do not forget this password.
Passphrase:
Repeat Passphrase:
Address: {eef171323b96cb34dd66fede388bd8eb13d399fc}

成功後, 就可以使用 geth command

建立 genesis block

$ sublime ~/ethereum/gen.json

{
    "nonce": "0xdeadbeefdeadbeef",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
    }
}

運行區塊鏈

$ geth --genesis ~/ethereum/gen.json --datadir ~/block --networkid 123 --nodiscover --maxpeers 0 console

I0307 10:37:57.558236   16811 flags.go:425] WARNING: No etherbase set and no accounts found as default
I0307 10:37:57.558795   16811 database.go:71] Alloted 16MB cache to /Users/user/block/chaindata
I0307 10:37:57.563390   16811 database.go:71] Alloted 16MB cache to /Users/user/block/dapp
I0307 10:37:57.566917   16811 backend.go:314] Protocol Versions: [63 62 61], Network Id: 123
I0307 10:37:57.567974   16811 backend.go:326] Successfully wrote genesis block. New genesis hash = ba4fe4055a968c1b05a1254289164e7665cfef89782dcc7dcaec2e5e4edc83a6
I0307 10:37:57.568046   16811 backend.go:362] Blockchain DB Version: 3
I0307 10:37:57.569333   16811 blockchain.go:214] Last header: #0 [ba4fe405…] TD=1024
I0307 10:37:57.569471   16811 blockchain.go:215] Last block: #0 [ba4fe405…] TD=1024
I0307 10:37:57.569489   16811 blockchain.go:216] Fast block: #0 [ba4fe405…] TD=1024
I0307 10:37:57.580509   16811 cmd.go:114] Starting Geth/v1.3.5/darwin/go1.6
I0307 10:37:57.580810   16811 server.go:311] Starting Server
I0307 10:37:57.581117   16811 backend.go:526] Server started
I0307 10:37:57.581231   16811 server.go:552] Listening on [::]:30303
I0307 10:37:57.581754   16811 ipc.go:112] IPC service started (/Users/user/block/geth.ipc)
instance: Geth/v1.3.5/darwin/go1.6
 datadir: /Users/user/block
coinbase: null
at block: 0 (Wed, 31 Dec 1969 18:00:00 CST)
modules: admin:1.0 db:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 shh:1.0 txpool:1.0 web3:1.0
> personal.newAccount("password");
"0x8ecbf0b5673aa4efd46f620fef05e86974fd1ef6"
>

移除 datadir 中除了 keystore 的資料夾

$ cd ~/block
$ rm -rf `ls | grep -v keystone`

修改區塊內容

$ sublime ~/ethereum/gen.json

{
    "nonce": "0xdeadbeefdeadbeef",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
        "0x8ecbf0b5673aa4efd46f620fef05e86974fd1ef6": {
        "balance": "10000000000000000000"
        }
    }
}

重新運行區塊鏈

$ geth --genesis ~/ethereum/gen.json --datadir ~/block --networkid 123 --nodiscover --maxpeers 0 console
I0307 10:43:44.455530   16902 database.go:71] Alloted 16MB cache to /Users/user/block/chaindata
I0307 10:43:44.460506   16902 database.go:71] Alloted 16MB cache to /Users/user/block/dapp
I0307 10:43:44.463776   16902 backend.go:314] Protocol Versions: [63 62 61], Network Id: 123
I0307 10:43:44.464706   16902 backend.go:326] Successfully wrote genesis block. New genesis hash = 42c87732c0ef28d6d2a4783e2dc39851ecf3791ee6e01b67929c7f20540eb45c
I0307 10:43:44.464774   16902 backend.go:362] Blockchain DB Version: 3
I0307 10:43:44.465765   16902 blockchain.go:214] Last header: #0 [42c87732…] TD=1024
I0307 10:43:44.465794   16902 blockchain.go:215] Last block: #0 [42c87732…] TD=1024
I0307 10:43:44.465809   16902 blockchain.go:216] Fast block: #0 [42c87732…] TD=1024
I0307 10:43:44.473988   16902 cmd.go:114] Starting Geth/v1.3.5/darwin/go1.6
I0307 10:43:44.474287   16902 server.go:311] Starting Server
I0307 10:43:44.474736   16902 backend.go:526] Server started
I0307 10:43:44.474911   16902 server.go:552] Listening on [::]:30303
I0307 10:43:44.475889   16902 ipc.go:112] IPC service started (/Users/user/block/geth.ipc)
instance: Geth/v1.3.5/darwin/go1.6
 datadir: /Users/user/block
coinbase: 0x8ecbf0b5673aa4efd46f620fef05e86974fd1ef6
at block: 0 (Wed, 31 Dec 1969 18:00:00 CST)
modules: admin:1.0 db:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 shh:1.0 txpool:1.0 web3:1.0
> primary = eth.accounts[0];
"0x8ecbf0b5673aa4efd46f620fef05e86974fd1ef6"
> balance = web3.fromWei(eth.getBalance(primary), "ether");
10

得到 10 Ether

Clone this wiki locally