以太坊(ethereum)实现研究

以太坊区块hash的计算

2017-12-09  本文已影响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;
上一篇下一篇

猜你喜欢

热点阅读