以太坊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
******************************************/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值