以太坊区块hash的计算

作者: 古则 | 来源:发表于2017-12-09 17:36 被阅读89次

在上一篇以太坊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;

相关文章

  • 以太坊区块hash的计算

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

  • 以太坊是什么?

    【以太坊】以太坊是一种包含图灵完备脚本语言的区块链。以太坊是一台区块链上的虚拟计算机,只要支付以太坊代币并编写脚本...

  • 以太坊入门学习笔记-运行原理二

    以太坊运行原理笔记: (参考以太坊黄皮书) 以太坊区块组成: 区块:相关信息片段(区块头)+区块内交易+其他的区块...

  • 区块链2.0之以太坊

    区块链2.0之以太坊 一、什么是以太坊 以太坊的全称是Ethereum,简称ETH或ether。 以太坊被称为区块...

  • 【区块链基础02】-以太坊基本概念 Geth 安装

    一、以太坊基本概念 以太坊就是区块链技术+智能合约。 以太坊和区块链技术一样,有Transation,Block,...

  • 2018-09-19

    以太坊: 以太坊项目借鉴了比特币区块链的技术,对它的应用范围进行了扩展。如果说比特币是利用区块链技术的专用计算器,...

  • 2018-09-17

    以太坊: 以太坊项目借鉴了比特币区块链的技术,对它的应用范围进行了扩展。如果说比特币是利用区块链技术的专用计算器,...

  • 区块链开发(三)以太坊基本概念及工具Geth、Browser-s

    以太坊基本概念 以太坊,最简单的说法就是:区块链技术+智能合约。 以太坊和区块链技术一样,有Transaction...

  • 以太坊交易的生命周期

    了解以太坊交易是如何生成并在网络中广播的 交易是以太坊区块链(或任何类似的区块链)的核心。在与以太坊区块链进行交互...

  • 【BH区块链项目热点问答】超四成ETH投资者选择持币待涨。你怎么

    问:以太坊股权证明区块链代码完成,pos会让以太坊更有价值吗? 答:以太坊股权证明区块链代码的完成,意味着以太坊很...

网友评论

    本文标题:以太坊区块hash的计算

    本文链接:https://www.haomeiwen.com/subject/kskpixtx.html