Ethereumのprivate networkのblock timeを一定に指定する方法

 

Ethereumのprivate networkをパソコンに立ち上げました。

その際、block confirmationをするblock timeがポワソン分布に従って、一定ではなく保たれています。

しかし、実験のためにblock timeを一定に指定する必要があったため、その設定をしました。

結果、一定間隔でブロックが生成されるようになりました。

 

0:現在使っているgo-ethereum (geth)をアンインストール/ファイルを削除して、githubからファイルをクローンしてくる。

$  git clone https://github.com/ethereum/go-ethereum

 

1:ソースコードの編集

 

最新バージョンgethにおいて編集するコード:

go-ethereum/consensus/ethash/consensus.go:

f:id:haruokny:20180103001118p:plain

https://github.com/ethereum/go-ethereum/blob/908faf8cd715c873e4b5fdbb7af8d4f496702d84/consensus/ethash/consensus.go

 

上のfunctionの中身を以下に置き換える: 

    return big.NewInt(1)

ソースコード編集結果:

func CalcDifficulty(config *params.ChainConfig, time uint64, parent *types.Header) *big.Int {

return big.NewInt(1)

// next := new(big.Int).Add(parent.Number, big1)

// switch {

// case config.IsByzantium(next):

// return calcDifficultyByzantium(time, parent)

// case config.IsHomestead(next):

// return calcDifficultyHomestead(time, parent)

// default:

// return calcDifficultyFrontier(time, parent)

}

 

2:goのmakeコマンドでソースコードをビルドする

➜ go-ethereum git:(master) ✗ make geth
build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/Cellar/go/1.9.2/libexec/bin/go install -ldflags -X main.gitCommit=908faf8cd715c873e4b5fdbb7af8d4f496702d84 -s -v ./cmd/geth
github.com/ethereum/go-ethereum/consensus/ethash
...
Done building.
Run "/Users/HO/go-ethereum/build/bin/geth" to launch geth.

 

 

3:2で ビルドエラーが出た場合

エラー内容:

$ cd go-ethereum

$ go-ethereum git:(master) ✗ make geth
build/env.sh go run build/ci.go install ./cmd/geth
>>> /usr/local/Cellar/go/1.9.2/libexec/bin/go install -ldflags -X main.gitCommit=908faf8cd715c873e4b5fdbb7af8d4f496702d84 -s -v ./cmd/geth
github.com/ethereum/go-ethereum/consensus/ethash
# github.com/ethereum/go-ethereum/consensus/ethash
consensus/ethash/consensus.go:299:42: next declared and not used
util.go:45: exit status 2
exit status 1
make: *** [geth] Error 1


➜ go-ethereum git:(master) ✗ geth version
zsh: command not found: geth

 

解決: Macを最新のOSへアップグレード

f:id:haruokny:20180103001422p:plain

 

同エラー:

Error installing go-ethereum on Mac OS X · Issue #117 · ethereum/homebrew-ethereum · GitHub

"util.go:45: exit status 2 exit status 1 make: *** [all] Error 1" when executing brew install ethereum on El Capitan · Issue #132 · ethereum/homebrew-ethereum · GitHub

 

---

参考:

https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac

https://ethereum.stackexchange.com/questions/2539/how-do-i-decrease-the-difficulty-on-a-private-testnet

https://ethereum.stackexchange.com/questions/7141/is-it-possible-to-change-the-block-target-time/7142

https://github.com/ethereum/go-ethereum/blob/5f55d95aea433ef97c48ae927835d833772350de/core/block_validator.go#L265-L312