主页 > imtoken苹果钱包 > 以太坊区块哈希的计算

以太坊区块哈希的计算

imtoken苹果钱包 2023-07-24 05:09:41

在之前以太坊rlp编解码规则及实现的文章中,我们看到了字符数组编解码的实现。 接下来在计算以太坊区块的hash时,我们会看到rlp list代码的使用

以太坊区块头

首先请看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"`

sitebihu.com 以太以太坊价格_以太坊 算法_以太坊7月1日改算法

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"` }

有两个块哈希值

(1)header_hash_nononce以太坊 算法,eth.getWork获取到的第一个字段就是这个。 这个hash是对除MixDigest和Nonce之外的header进行rlp编码得到的。

sitebihu.com 以太以太坊价格_以太坊7月1日改算法_以太坊 算法

(2)header_hash以太坊 算法,eth.getBlock得到的数据中的hash就是这个,这个hash是对整个header进行rlp编码后hash得到的

注意:

上面说的rlp编码是指把header的每个字段编码成一个list

上面说的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));

以太坊 算法_sitebihu.com 以太以太坊价格_以太坊7月1日改算法

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));

以太坊 算法_以太坊7月1日改算法_sitebihu.com 以太以太坊价格

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 {

以太坊7月1日改算法_以太坊 算法_sitebihu.com 以太以太坊价格

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;