以太坊源碼研讀0x01GenesisBlock(創(chuàng)世區(qū)塊)

上次講了以太坊在mac下的本地編譯環(huán)境哩都,從這次開始我們從創(chuàng)世區(qū)塊入手來逐步研讀以太坊核心的部分源代碼魁兼。

創(chuàng)世命令

geth --datadir data0 init genesis.json

  • geth main -->在/cmd/geth/main.go
  • --datadir data0 -->啟動創(chuàng)世命令后創(chuàng)建的專門儲存節(jié)點數(shù)據(jù)的文件目錄
  • init -->調(diào)用的是cmd/geth/chaincmd.go中的initCommand, initCommand調(diào)用的是initGenesis(ctx *cli.Context)
  • genesis.json -->創(chuàng)世區(qū)塊配置文件,與創(chuàng)世區(qū)塊的數(shù)據(jù)結(jié)構(gòu)一一對應(yīng)

廢話少說讀代碼

Genesis struct

首先漠嵌,在我們還對源碼結(jié)構(gòu)不太熟悉的情況下咐汞,通過全局搜索來尋找關(guān)于創(chuàng)世區(qū)塊的結(jié)構(gòu)定義。

找啊找啊找朋友
// 創(chuàng)世區(qū)塊類結(jié)構(gòu)儒鹿,后面的是區(qū)塊對應(yīng)json字段名稱
type Genesis struct {
    // 配置文件化撕,用于指定鏈的chainId(network id)
    Config     *params.ChainConfig `json:"config"`
    // 隨機數(shù),與Mixhash組合用于滿足POW算法要求
    Nonce      uint64              `json:"nonce"`
    // 時間戳
    Timestamp  uint64              `json:"timestamp"`
    // 區(qū)塊額外信息
    ExtraData  []byte              `json:"extraData"`
    // Gas消耗量限制
    GasLimit   uint64              `json:"gasLimit"   gencodec:"required"`
    // 區(qū)塊難度值
    Difficulty *big.Int            `json:"difficulty" gencodec:"required"`
    // 由上個區(qū)塊的一部分生成的Hash约炎,和Nonce組合用于找到滿足POW算法的條件
    Mixhash    common.Hash         `json:"mixHash"`
    // 礦工地址
    Coinbase   common.Address      `json:"coinbase"`
    // 創(chuàng)世區(qū)塊初始狀態(tài)
    Alloc      GenesisAlloc        `json:"alloc"      gencodec:"required"`

    // These fields are used for consensus tests. Please don't use them
    // in actual genesis blocks.
    /** 下面字段用于共識測試植阴,不要在創(chuàng)世區(qū)塊中使用
    */
    Number     uint64      `json:"number"`
    GasUsed    uint64      `json:"gasUsed"`
    // 父區(qū)塊哈希
    ParentHash common.Hash `json:"parentHash"`
}

SetupGenesisBlock

了解了創(chuàng)世區(qū)塊的數(shù)據(jù)結(jié)構(gòu)后,我們來看一下以太坊是如何啟動創(chuàng)世區(qū)塊的圾浅。

/**如果存儲的區(qū)塊鏈配置不兼容掠手,將調(diào)用該方法更新。為了避免沖突狸捕,錯誤將會被作為參數(shù)返回喷鸽,并且新的配置
和原有配置都會被返回。
*/
func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, error) {
    if genesis != nil && genesis.Config == nil {
        return params.AllEthashProtocolChanges, common.Hash{}, errGenesisNoConfig
    }

    // Just commit the new block if there is no stored genesis block.
    // 如果沒有存儲的genesis塊灸拍,只需提交新塊做祝。
    stored := rawdb.ReadCanonicalHash(db, 0)    // 從數(shù)據(jù)庫中獲取genesis對應(yīng)的區(qū)塊
    if (stored == common.Hash{}) {
        if genesis == nil {
            // genesis和stored都為空,使用主網(wǎng)
            log.Info("Writing default main-net genesis block")
            genesis = DefaultGenesisBlock()
        } else {
            // 使用自定義的genesis配置
            log.Info("Writing custom genesis block")
        }
        // 創(chuàng)世區(qū)塊寫入數(shù)據(jù)庫
        block, err := genesis.Commit(db)
        return genesis.Config, block.Hash(), err
    }

    // Check whether the genesis block is already written.
    // 檢查創(chuàng)世區(qū)塊是否寫入成功
    if genesis != nil {
        // 獲取創(chuàng)世區(qū)塊哈希
        hash := genesis.ToBlock(nil).Hash()
        if hash != stored {
            return genesis.Config, hash, &GenesisMismatchError{stored, hash}
        }
    }

    // Get the existing chain configuration.
    // 獲取當前區(qū)塊鏈相關(guān)配置
    newcfg := genesis.configOrDefault(stored)
    storedcfg := rawdb.ReadChainConfig(db, stored)
    if storedcfg == nil {
        // 讀取失敗株搔,說明創(chuàng)世區(qū)塊寫入被中斷
        log.Warn("Found genesis block without chain config")
        rawdb.WriteChainConfig(db, stored, newcfg)
        return newcfg, stored, nil
    }
    // Special case: don't change the existing config of a non-mainnet chain if no new
    // config is supplied. These chains would get AllProtocolChanges (and a compat error)
    // if we just continued here.
    // 特殊情況:如果沒有新的配置剖淀,請勿更改非主網(wǎng)鏈的相關(guān)配置
    // 這些鏈會得到AllProtocolChanges以及compat error,如果我們繼續(xù)
    if genesis == nil && stored != params.MainnetGenesisHash {
        return storedcfg, stored, nil
    }

    // Check config compatibility and write the config. Compatibility errors
    // are returned to the caller unless we're already at block zero.
    // 檢查配置兼容性并寫入配置
    // 兼容性錯誤將返回給調(diào)用者纤房,除非我們已經(jīng)處于塊0
    height := rawdb.ReadHeaderNumber(db, rawdb.ReadHeadHeaderHash(db))
    if height == nil {
        return newcfg, stored, fmt.Errorf("missing block number for head header hash")
    }
    compatErr := storedcfg.CheckCompatible(newcfg, *height)
    // 如果compatErr為空并且非高度為0的區(qū)塊纵隔,那么久不能更改genesis配置了
    if compatErr != nil && *height != 0 && compatErr.RewindTo != 0 {
        return newcfg, stored, compatErr
    }
    rawdb.WriteChainConfig(db, stored, newcfg)
    return newcfg, stored, nil
}

ToBlock

ToBlock方法使用genesis的數(shù)據(jù),使用基于內(nèi)存的數(shù)據(jù)庫炮姨,然后創(chuàng)建了一個block并返回捌刮。

// ToBlock, 這個方法使用genesis的數(shù)據(jù),使用基于內(nèi)存的數(shù)據(jù)庫舒岸,然后創(chuàng)建了一個block并返回绅作。
func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
    if db == nil {
        db = ethdb.NewMemDatabase()
    }
    // 用genesis的數(shù)據(jù)給statedb賦值
    statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
    for addr, account := range g.Alloc {
        statedb.AddBalance(addr, account.Balance)
        statedb.SetCode(addr, account.Code)
        statedb.SetNonce(addr, account.Nonce)
        for key, value := range account.Storage {
            statedb.SetState(addr, key, value)
        }
    }
    root := statedb.IntermediateRoot(false)
    // 填充head值
    head := &types.Header{
        Number:     new(big.Int).SetUint64(g.Number),
        Nonce:      types.EncodeNonce(g.Nonce),
        Time:       new(big.Int).SetUint64(g.Timestamp),
        ParentHash: g.ParentHash,
        Extra:      g.ExtraData,
        GasLimit:   g.GasLimit,
        GasUsed:    g.GasUsed,
        Difficulty: g.Difficulty,
        MixDigest:  g.Mixhash,
        Coinbase:   g.Coinbase,
        Root:       root,
    }
    if g.GasLimit == 0 {
        head.GasLimit = params.GenesisGasLimit
    }
    if g.Difficulty == nil {
        head.Difficulty = params.GenesisDifficulty
    }

    // 數(shù)據(jù)庫提交
    statedb.Commit(false)
    statedb.Database().TrieDB().Commit(root, true)

    return types.NewBlock(head, nil, nil, nil)
}

Commit

上面兩個主要函數(shù)都用到了Commit方法,接下來我們就看下該方法到底做了哪些操作蛾派。

// Commit writes the block and state of a genesis specification to the database.
// The block is committed as the canonical head block.
// Commit方法調(diào)用rawdb.WriteChainConfig(db, block.Hash(), config)函數(shù)把給定的genesis的block
// 和state寫入數(shù)據(jù)庫俄认,該block被認為是規(guī)范的區(qū)塊鏈頭
func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
    
    // 取到genesis對應(yīng)的block
    block := g.ToBlock(db)
    if block.Number().Sign() != 0 {
        return nil, fmt.Errorf("can't commit genesis block with number > 0")
    }
    // 寫入總難度
    rawdb.WriteTd(db, block.Hash(), block.NumberU64(), g.Difficulty)
    // 寫入?yún)^(qū)塊
    rawdb.WriteBlock(db, block)
    // 寫入?yún)^(qū)塊數(shù)據(jù)
    rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil)
    // 寫入 headerPrefix + num (uint64 big endian) + numSuffix -> hash
    rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
    rawdb.WriteHeadBlockHash(db, block.Hash())
    rawdb.WriteHeadHeaderHash(db, block.Hash())

    config := g.Config
    if config == nil {
        config = params.AllEthashProtocolChanges
    }
    // 寫入 ethereum-config-hash -> config
    rawdb.WriteChainConfig(db, block.Hash(), config)
    return block, nil
}

各種模式的GenesisBlock


// 各種返回模式的Genesis
// GenesisBlockForTesting creates and writes a block in which addr has the given wei balance.
// GenesisBlockForTesting創(chuàng)建并寫入一個塊个少,其中addr具有給定的wei余額。
func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
    g := Genesis{Alloc: GenesisAlloc{addr: {Balance: balance}}}
    return g.MustCommit(db)
}

// DefaultGenesisBlock returns the Ethereum main net genesis block.
// DefaultGenesisBlock返回以太坊主網(wǎng)絡(luò)創(chuàng)世塊
func DefaultGenesisBlock() *Genesis {
    return &Genesis{
        Config:     params.MainnetChainConfig,
        Nonce:      66,
        ExtraData:  hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"),
        GasLimit:   5000,
        Difficulty: big.NewInt(17179869184),
        Alloc:      decodePrealloc(mainnetAllocData),
    }
}

// DefaultTestnetGenesisBlock returns the Ropsten network genesis block.
// DefaultTestnetGenesisBlock返回Ropsten網(wǎng)絡(luò)創(chuàng)世塊眯杏。
func DefaultTestnetGenesisBlock() *Genesis {
    return &Genesis{
        Config:     params.TestnetChainConfig,
        Nonce:      66,
        ExtraData:  hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
        GasLimit:   16777216,
        Difficulty: big.NewInt(1048576),
        Alloc:      decodePrealloc(testnetAllocData),
    }
}

// DefaultRinkebyGenesisBlock returns the Rinkeby network genesis block.
// DefaultRinkebyGenesisBlock返回Rinkeby網(wǎng)絡(luò)創(chuàng)世塊夜焦。
func DefaultRinkebyGenesisBlock() *Genesis {
    return &Genesis{
        Config:     params.RinkebyChainConfig,
        Timestamp:  1492009146,
        ExtraData:  hexutil.MustDecode("0x52657370656374206d7920617574686f7269746168207e452e436172746d616e42eb768f2244c8811c63729a21a3569731535f067ffc57839b00206d1ad20c69a1981b489f772031b279182d99e65703f0076e4812653aab85fca0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
        GasLimit:   4700000,
        Difficulty: big.NewInt(1),
        Alloc:      decodePrealloc(rinkebyAllocData),
    }
}

// DeveloperGenesisBlock returns the 'geth --dev' genesis block. Note, this must
// be seeded with the
// eveloperGenesisBlock返回'geth --dev'創(chuàng)世塊。
func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
    // Override the default period to the user requested one
    config := *params.AllCliqueProtocolChanges
    config.Clique.Period = period

    // Assemble and return the genesis with the precompiles and faucet pre-funded
    return &Genesis{
        Config:     &config,
        ExtraData:  append(append(make([]byte, 32), faucet[:]...), make([]byte, 65)...),
        GasLimit:   6283185,
        Difficulty: big.NewInt(1),
        Alloc: map[common.Address]GenesisAccount{
            common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
            common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256
            common.BytesToAddress([]byte{3}): {Balance: big.NewInt(1)}, // RIPEMD
            common.BytesToAddress([]byte{4}): {Balance: big.NewInt(1)}, // Identity
            common.BytesToAddress([]byte{5}): {Balance: big.NewInt(1)}, // ModExp
            common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
            common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
            common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
            faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
        },
    }
}

對創(chuàng)世區(qū)塊的源碼分析就到這岂贩,下次我們通過聯(lián)盟鏈來大概認識下go的以太坊客戶端geth的基本功能茫经,依次來了解以太坊核心功能。

更多以太坊源碼解析請移駕全球最大同性交友網(wǎng),覺得有用記得給個小star哦??????

.
.
.
.

互聯(lián)網(wǎng)顛覆世界萎津,區(qū)塊鏈顛覆互聯(lián)網(wǎng)!

--------------------------------------------------20180904 23:11
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末卸伞,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子锉屈,更是在濱河造成了極大的恐慌荤傲,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件部念,死亡現(xiàn)場離奇詭異弃酌,居然都是意外死亡,警方通過查閱死者的電腦和手機儡炼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門妓湘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乌询,你說我怎么就攤上這事榜贴。” “怎么了妹田?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵唬党,是天一觀的道長。 經(jīng)常有香客問我鬼佣,道長驶拱,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任晶衷,我火速辦了婚禮蓝纲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘晌纫。我一直安慰自己税迷,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布锹漱。 她就那樣靜靜地躺著箭养,像睡著了一般。 火紅的嫁衣襯著肌膚如雪哥牍。 梳的紋絲不亂的頭發(fā)上毕泌,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天喝检,我揣著相機與錄音,去河邊找鬼撼泛。 笑死蛇耀,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的坎弯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼译暂,長吁一口氣:“原來是場噩夢啊……” “哼抠忘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起外永,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤崎脉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后伯顶,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體囚灼,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年祭衩,在試婚紗的時候發(fā)現(xiàn)自己被綠了灶体。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡掐暮,死狀恐怖蝎抽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情路克,我是刑警寧澤樟结,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站精算,受9級特大地震影響瓢宦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜灰羽,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一驮履、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谦趣,春花似錦疲吸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至舰绘,卻和暖如春蹂喻,著一層夾襖步出監(jiān)牢的瞬間葱椭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工口四, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留孵运,地道東北人。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓蔓彩,卻偏偏與公主長得像治笨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子赤嚼,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容