以太坊区块hash的计算

在上一篇以太坊rlp编解码规则及实现中,我们看到了字符数组编解码的实现,下面在计算以太坊区块hash时,我们会看到rlp列表编码的使用

以太坊区块头

首先请看go-ethereum中以太坊区块头的定义

type Header struct {
    ParentHash  common.Hash    `json:"parentHash"       gencodec:"required"`
    UncleHash   common.Hash    `json:"sha3Uncles"       gencodec:"required"`
    Coinbase    common.Address `json:"miner"            gencodec:"required"`
    Root        common.Hash    `json:"stateRoot"        gencodec:"required"`
    TxHash      common.Hash    `json:"transactionsRoot" gencodec:"required"`
    ReceiptHash common.Hash    `json:"receiptsRoot"     gencodec:"required"`
    Bloom       Bloom          `json:"logsBloom"        gencodec:"required"`
    Difficulty  *big.Int       `json:"difficulty"       gencodec:"required"`
    Number      *big.Int       `json:"number"           gencodec:"required"`
    GasLimit    *big.Int       `json:"gasLimit"         gencodec:"required"`
    GasUsed     *big.Int       `json:"gasUsed"          gencodec:"required"`
    Time        *big.Int       `json:"timestamp"        gencodec:"required"`
    Extra       []byte         `json:"extraData"        gencodec:"required"`
    MixDigest   common.Hash    `json:"mixHash"          gencodec:"required"`
    Nonce       BlockNonce     `json:"nonce"            gencodec:"required"`
}

存在两种区块hash值
(1)header_hash_nononce,eth.getWork获取的第一个字段就是这个,这个hash是对header除 MixDigest,Nonce外进行rlp编码后进行hash得到的
(2)header_hash, eth.getBlock得到的数据中的hash就是这个,这个hash是对整个header进行rlp编码后进行hash得到的

注意:
上面所说的rlp编码是指将header的各个字段看作一个列表进行编码
上面所说的hash是指keccakf 256算法,keccakf是sha3所采用的hash算法,算法可参见ethash算法库

区块头rlp编码及hash的计算

int start_index = 0;
int data_index = 1 + 8;
char buf[4096] = {0};
void *p = buf + data_index;
size_t left = sizeof(buf) - data_index;
rlp_pack_buf(&p, &left, parent_hash_bin, sdslen(parent_hash_bin));
rlp_pack_buf(&p, &left, uncles_hash_bin, sdslen(uncles_hash_bin));
rlp_pack_buf(&p, &left, miner_bin, sdslen(miner_bin));
rlp_pack_buf(&p, &left, state_root_bin, sdslen(state_root_bin));
rlp_pack_buf(&p, &left, tx_hash_bin, sdslen(tx_hash_bin));
rlp_pack_buf(&p, &left, receipt_hash_bin, sdslen(receipt_hash_bin));
rlp_pack_buf(&p, &left, bloom_bin, sdslen(bloom_bin));
rlp_pack_buf(&p, &left, diff_bin, sdslen(diff_bin));
rlp_pack_buf(&p, &left, number_bin, sdslen(number_bin));
rlp_pack_buf(&p, &left, gas_limit_bin, sdslen(gas_limit_bin));
rlp_pack_buf(&p, &left, gas_used_bin, sdslen(gas_used_bin));
rlp_pack_buf(&p, &left, time_bin, sdslen(time_bin));
rlp_pack_buf(&p, &left, extra_data_bin, sdslen(extra_data_bin));
 //不包含这两个就是header_hash_nononce
rlp_pack_buf(&p, &left, mixdigest_bin, sdslen(mixdigest_bin));
rlp_pack_buf(&p, &left, nonce_bin, sdslen(nonce_bin));

//列表前缀的处理
size_t alllen = sizeof(buf) - data_index - left;
 if (alllen <= 55) {
        start_index = data_index - 1; 
        buf[start_index] = 192 + alllen;
} else {
        char len_buf[8] = {0};
        void *len_buf_p = len_buf;
        size_t len_buf_left = sizeof(len_buf);
        int space = rlp_pack_varint_be(&len_buf_p, &len_buf_left, alllen);
        start_index = data_index - 1 - space;
        buf[start_index] = 247 + space;
        memcpy(buf + start_index + 1, len_buf, space);
}
size_t buf_alllen = sizeof(buf) - left - start_index;
sds hash = sdsnewlen(NULL, 32);
sha3_256((uint8_t *)hash, 32, (uint8_t*)(buf + start_index), buf_alllen);
return hash;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一,项目介绍 开发者可以获取为其他用户创造内容和应用带来的全部价值。作为开发者,您可以在 Decentraland...
    big_aooA阅读 3,385评论 0 0
  • 有些人看起来整天面带笑容, 并不是因为他们事事顺利, 只是他们比你敢于面对问题、善于遗忘不幸、勇于拥抱未来。 Fr...
    笃学青衿阅读 1,459评论 2 0
  • 高三了,后天开学。因为亲故的一句感叹突然有点醒悟?!真的高三了。开学考试,其实真的撒撒娇耍耍赖就可以不去考可是想想...
    3cbf5ffda6ab阅读 1,850评论 0 1
  • 想起外婆的小手巾才想起来,外婆还会用牛津草编小手镯。小的时候很喜欢这样的小手镯,只可惜从小看到大也没有学会。 总喜...
    嗖嗖咪咪阅读 1,324评论 0 0
  • 回到离开了二十多天的家,看着楼下的公园,河水,觉得一切都是这么的美。 感赏群里的桃子热心的帮我分析,出主意,太睿智...
    叛逆的榴莲阅读 1,702评论 2 5

友情链接更多精彩内容