1. 程式人生 > >如何分叉以太坊並變成自己的私鏈?

如何分叉以太坊並變成自己的私鏈?

帥初

寫於 2016年2月11日

隨著大家對區塊鏈技術的深入瞭解,越來越多的企業和個人開發者認識到ethereum區塊鏈平臺的價值,由於ethereum平臺開發語言是圖靈完備的,也給區塊鏈相關應用的開發帶來了更多的可拓展性,特別是基於ethereum的智慧合約平臺,可以讓開發者非常快速的部署自己的智慧合約。

類似於R3CEV利用ethereum的區塊鏈技術搭建一個銀行間的區塊鏈服務,未來定會有公司和個人希望站在ethereum的肩膀上,快速部署自己的區塊鏈服務,同時還能使用ethereum生態系統內,無數開發者貢獻的開原始碼。 大大降低自己的部署成本。

本文會簡單介紹如何分叉以太坊,並把它變成自己的私鏈。希望對行業的從業人員有些幫助。

第一部分:重新命名(本文示例私鏈為: neochain)

1-下載以太坊原始碼: download ethereum: https://github.com/ethereum/go-ethereum/tree/release/1.3.3

2- 重新命名程式碼:rename folder to go-neochain

3- 重新命名相關檔案和資料夾 rename files and folders:

- replace ethereum with neochain in files and folders filenames

- replace eth with vch in files and folders filenames

4- replace occurences of ethereum in the code (using text replace software like grepWin): 重新命名ethereum 程式碼中的所有ethereum字元,並替換成neochain,具體替換專案如下:

- repalce github.com/ethereum/go-ethereum with github.com/neochain/go-neochain

- repalce ethereum with neochain in all files

- repalce ETHEREUM with NEOCHAIN in all files

- repalce Ethereum with Neochain in all files

- repalce ether with neochain in all files

- repalce Ether with Neochain in all files

- repalce eth with vch in all files

- repalce ETH with VCH in all files

5- replace back nch_ with eth_ to restore api default functions calls

把eth_替換成nch_,用來儲存呼叫的API

6- replace back mnchod with method

替換method

7- replace back Mnchod with Method

替換Method

8- replace back wvec with weth to restore the word wheter

替換weth

9- replace default rpc port (we’ll use port 7575):

修改RPC 埠,neochain 利用7575 埠

- in file go-neochain\cmd\utils\flags.go line 71

把以太坊預設埠8545修改成7575

replace: 替換

RPCPortFlag = cli.IntFlag{

Name: "rpcport",

Usage: "HTTP-RPC server listening port",

Value: 8545,

}

with:

RPCPortFlag = cli.IntFlag{

Name: "rpcport",

Usage: "HTTP-RPC server listening port",

Value: 7575,

}

在neochain_js.go 第4108行 也進行替換

- in file go-neochain\jsre\neochain_js.go line 4108

replace: 替換

var HttpProvider = function (host) {

this.host = host || 'http://localhost:8545';

};

with:

var HttpProvider = function (host) {

this.host = host || 'http://localhost:7575';

};

在admin_args.go 第130行也需要進行替換

- in file go-neochain\rpc\api\admin_args.go line 130

replace:

args.ListenPort = 8545

with:

args.ListenPort = 7575

在comms.go 127行也進行替換

in file go-neochain\rpc\comms\comms.go line 127

replace:

if strings.HasPrefix(endpoint, "rpc:") {

parts := strings.Split(endpoint, ":")

addr := "http://localhost"

port := uint(8545)

if len(parts) >= 3 {

addr = parts[1] + ":" + parts[2]

}

with:

if strings.HasPrefix(endpoint, "rpc:") {

parts := strings.Split(endpoint, ":")

addr := "http://localhost"

port := uint(7575)

if len(parts) >= 3 {

addr = parts[1] + ":" + parts[2]

}

替換ethereum的p2p埠,在neochain 中,我們利用17575埠

10- replace default p2p port (we’ll use port 17575):

- in file go-neochain\cmd\utils\flags.go line 315

replace: 替換

ListenPortFlag = cli.IntFlag{

Name: "port",

Usage: "Network listening port",

Value: 30303,

}

with:

ListenPortFlag = cli.IntFlag{

Name: "port",

Usage: "Network listening port",

Value: 17575,

}

替換ethereum的discovery port, 把30301埠替換成27575埠。neochain中我們利用 27575埠。

11- replace default discovery port (we’ll use port 27575):

- in go-neochain\cmd\bootnode\main.go line 37

replace:替換

listenAddr = flag.String(“addr”, “:30301″, “listen address”)

with:

listenAddr = flag.String(“addr”, “:27575″, “listen address”)

第二部分: 修改原始程式碼:

1- 停止ethereum的種子節點 disable default seed nodes:


- in go-neochain\vch\backend.go line 71

replace:

defaultBootNodes = []*discover.Node{

// VCH/DEV Go Bootnodes

discover.MustParseNode("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29[email protected]52.16.188.185:30303"), // IE

discover.MustParseNode("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f7310[email protected]54.94.239.50:30303"), // BR

discover.MustParseNode("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad65[email protected]52.74.57.123:30303"), // SG

// VCH/DEV cpp-neochain (poc-9.vchdev.com)

discover.MustParseNode("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba[email protected]5.1.83.226:30303"),}

defaultTestNetBootNodes = []*discover.Node{

discover.MustParseNode("enode://e4533109cc9bd7604e4ff6c095f7a1d807e15b38e9bfeb05d3b7c423ba86af0a9[email protected]94.242.229.4:40404"),

discover.MustParseNode("enode://8c336ee6f03e99613ad21274f269479bf4413fb294d697ef15ab897598afb931f[email protected]94.242.229.203:30303"),}

替換成:


with:

defaultBootNodes = []*discover.Node{

}

defaultTestNetBootNodes = []*discover.Node{

}

2- 生成預挖的公鑰和私鑰對:create premine address keypair:


- 需要一個公鑰地址,來產生neochain的創始區塊,並且需要控制它的私鑰來後面花費公鑰上面的幣。

- 第一步,通過一個隨機數產生器,我們來產生32位的私鑰,最簡單的做法就是通過下面命令產生:openssl rand -hex 32

- 第二步,我們得到私鑰為: eb1b1d5ffc51b07a64e622413479bb87xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (只展示出一部分)

- 第三步,我們需要通過私鑰來得到公鑰,我們建立一個新的檔案 name: priv.txt , 然後複製privatekey 到這個txt檔案

- 第四步,我們可以通過ethereum的客戶端來產生公鑰,使用一下命令: geth account import priv.txt (如果我們沒有ethereum客戶端,我們需要自己編譯gvch並且採用相同的命令列)

- 我們可以得到下面的地址: {861fddd8a92457efb9f016517db38eb7fe586a6f}

- 出於安全原因,刪除了priv.txt

3- 建立創始區塊 create the genesis block string:


- 我們利用 script gengen.go 來建立創始區塊,在gengen.go 中可以編輯一個貨幣系統的引數,包括幣的總量,區塊獎勵,區塊時間,區塊難度資訊,等等。

- 根據私鏈開發的需要,我們可以修改gengen.go 中的各種區塊鏈的引數。

- 第一 修改區塊鏈alive的時間戳

第80行,修改時間unix時間戳,用16進位制。

(line 80): newGenesis.Timestamp="0x569CBF7F"

- 第二 我們把premine的地址替換成我們的地址

第90行(line 90): newGenesis.Coinbase="0x861fddd8a92457efb9f016517db38eb7fe586a6f"

- 第三 我們修改premine的數量

第92行 (line 92): premine:=[]byte('{"0x861fddd8a92457efb9f016517db38eb7fe586a6f":{"balance": "10000000000000000000000000"}}')

(格式為:premine address 和premine amount +18個0)

- 第四 我們也可以修改nonce、初始難度、gas limit 等資訊

因為neochain, 沒有太多人蔘與,因為我們把難度設定成cpu可以挖礦。

第五 所有引數修改好後,我們就可以儲存,然後執行,得到創始區塊的資訊。 用以下命令來產生創始區塊: command: go run gengen.go

第六 我們把執行結果複製到: go-neochain\core\default_genesis.go line 30


const defaultGenesisBlock = "H4sIAAAJbogA/6yRTWrDMBCF76J1FpZl/Ti7xGnaRVsK7QVmpFEjkO1iK+ASfPeqNhQK7SKQWQgkzfvmPenCnvvOEtuyYip+lSjZhr2FlsYE7cfSIFXd7I/6mC9eYKAuPcB4+kN6fWXi3ZQGOECCBcg5IhmHFYlK59XYWmjLrRG6oAolOSHAoXCoaosONQhDnGSBHE3pIQPvYXwMbUgLT/GdyWeH4H2w55g+V9v8Z/xTmE43TNP0oUMY14c1invnnIG6rKQmj7UvuJJc5wCGUHuSRoHyWbeLsbdse7lGlbub3n2PyoDX1A/wnjfdOcYN20OE9X/5/3bnef4KAAD//0BefdIIAgAA"

第七 – 修改區塊獎勵


- 在go-neochain\core\fees.go line 23

replace:

var BlockReward *big.Int = big.NewInt(5e+18)

with:

var BlockReward *big.Int = big.NewInt(0.01e+18)

- 在neochain中,我們把區塊獎勵設定成0.01

第八 – 儲存所有修改後,可以編譯客戶端 (編譯客戶端的說明詳見:see ethereum docs)

第九- 把ethereum的hardnode和keys,替換成我們自己的hardnode和keys


go-neochain\vch\backend.go line 71

replace:替換

defaultBootNodes = []*discover.Node{

// VCH/DEV Go Bootnodes

//discover.MustParseNode("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29[email protected]52.16.188.185:30303"), // IE

//discover.MustParseNode("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f7310[email protected]54.94.239.50:30303"), // BR

//discover.MustParseNode("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad65[email protected]52.74.57.123:30303"), // SG

// VCH/DEV cpp-neochain (poc-9.vchdev.com)

//discover.MustParseNode("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba[email protected]5.1.83.226:30303"),

}

with:

defaultBootNodes = []*discover.Node{

// VCH/DEV Go Bootnodes

discover.MustParseNode("enode://11936c3b31f940391025bba951777d4eafeffbd8aa24bf371b5166b2da6d69e98[email protected]62.210.105.88:17575"), // FR

discover.MustParseNode("enode://e1c30651e549149eb2bd68b77d5ca2a0ea662cd6364b846f456b5c12bd29978f7[email protected]139.196.109.133:17575"), // CN

}

第十- 最後再重新編譯客戶端,併發布各種版本的客戶端。 網路就可以alive,可以參考ethereum的挖礦說明,包括如何搭建礦池,讓礦工參與挖礦。

說明:本文僅適合做參考資料,由於時間匆忙,無法展開,可以在ethereum社群不斷學習。

如果有任何疑問: [email protected]