以太坊源码分析(22)core-genesis创世区块源码分析

genesis 是创世区块的意思. 一个区块链就是从同一个创世区块开始,通过规则形成的.不同的网络有不同的创世区块, 主网络和测试网路的创世区块是不同的.

这个模块根据传入的genesis的初始值和database,来设置genesis的状态,如果不存在创世区块,那么在database里面创建它。

数据结构
    
    // Genesis specifies the header fields, state of a genesis block. It also defines hard
    // fork switch-over blocks through the chain configuration.
    // Genesis指定header的字段,起始块的状态。 它还通过配置来定义硬叉切换块。
    type Genesis struct {
        Config *params.ChainConfig `json:"config"`
        Nonce uint64 `json:"nonce"`
        Timestamp uint64 `json:"timestamp"`
        ExtraData []byte `json:"extraData"`
        GasLimit uint64 `json:"gasLimit" gencodec:"required"`
        Difficulty *big.Int `json:"difficulty" gencodec:"required"`
        Mixhash common.Hash `json:"mixHash"`
        Coinbase common.Address `json:"coinbase"`
        Alloc GenesisAlloc `json:"alloc" gencodec:"required"`
    
        // These fields are used for consensus tests. Please don't use them
        // in actual genesis blocks.
        Number uint64 `json:"number"`
        GasUsed uint64 `json:"gasUsed"`
        ParentHash common.Hash `json:"parentHash"`
    }
    
    // GenesisAlloc specifies the initial state that is part of the genesis block.
    // GenesisAlloc 指定了最开始的区块的初始状态.
    type GenesisAlloc map[common.Address]GenesisAccount


SetupGenesisBlock,
    
    // SetupGenesisBlock writes or updates the genesis block in db.
    //
    // The block that will be used is:
    //
    // genesis == nil genesis != nil
    // +------------------------------------------
    // db has no genesis | main-net default | genesis
    // db has genesis | from DB | genesis (if compatible)
    //
    // The stored chain configuration will be updated if it is compatible (i.e. does not
    // specify a fork block below the local head block). In case of a conflict, the
    // error is a *params.ConfigCompatError and the new, unwritten config is returned.
    // 如果存储的区块链配置不兼容那么会被更新(). 为了避免发生冲突,会返回一个错误,并且新的配置和原来的配置会返回.
    // The returned chain configuration is never nil.

    // genesis 如果是 testnet dev 或者是 rinkeby 模式, 那么不为nil。如果是mainnet或者是私有链接。那么为空
    func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig, common.Hash, error) {
        if genesis != nil && genesis.Config == nil {
            return params.AllProtocolChanges, common.Hash{}, errGenesisNoConfig
        }
    
        // Just commit the new block if there is no stored genesis block.
        stored := GetCanonicalHash(db, 0) //获取genesis对应的区块
        if (stored == common.Hash{}) { //如果没有区块 最开始启动geth会进入这里。
            if genesis == nil {
                //如果genesis是nil 而且stored也是nil 那么使用主网络
                // 如果是test dev rinkeby 那么genesis不为空 会设置为各自的genesis
                log.Info("Writing default mai
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值