爱踢门

致力于IT技术解密,明明白白解读市面产品技术实现细节

以太坊header.extra字段
 一开始看到header.extra这个可变长度字段很是惊喜和诧异,因为它是变长的,那矿工不是可以随意写数据到这个字段,万一矿工作恶加入一个很大的数据,那其他节点不得累死?解决这一疑惑的唯一方式------看代码
type Header struct {
    ...
    Extra []byte `json:"extraData" gencodec:"required"`
    ...
}

Header.extra字段有长度限制


MaximumExtraDataSize uint64 = 32

func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error {
    // Ensure that the header's extra-data section is of a reasonable size
    if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
        return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
    }
}

DAO分叉后extra被占用了,没法用作其他用途


func (self *worker) commitNewWork() {
    // If we are care about TheDAO hard-fork check whether to override the extra-data or not
    if daoBlock := self.config.DAOForkBlock; daoBlock != nil {
        // Check whether the block is among the fork extra-override range
        limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
        if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
            // Depending whether we support or oppose the fork, override differently
            if self.config.DAOForkSupport {
                header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
            } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
                header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
            }
        }
    }
}


unc VerifyDAOHeaderExtraData(config *params.ChainConfig, header *types.Header) error {
    // Short circuit validation if the node doesn't care about the DAO fork
    if config.DAOForkBlock == nil {
        return nil
    }
    // Make sure the block is within the fork's modified extra-data range
    limit := new(big.Int).Add(config.DAOForkBlock, params.DAOForkExtraRange)
    if header.Number.Cmp(config.DAOForkBlock) < 0 || header.Number.Cmp(limit) >= 0 {
        return nil
    }
    // Depending on whether we support or oppose the fork, validate the extra-data contents
    if config.DAOForkSupport {
        if !bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
            return ErrBadProDAOExtra
        }
    } else {
        if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
            return ErrBadNoDAOExtra
        }
    }
    // All ok, header has the same extra-data we expect
    return nil
}

/********************************
* 本文来自CSDN博主"爱踢门"
* 转载请标明出处:http://blog.csdn.net/itleaks
******************************************/

阅读更多

扫码向博主提问

去开通我的Chat快问

itleaks

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • Android系统
  • Linux内核
  • 区块链
  • AI人工智能
  • OpenGLES
个人分类: 区块链
所属专栏: 以太坊源码分析
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

以太坊header.extra字段

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭