美文网首页
ETH源码阅读(存储Block的Receipts)

ETH源码阅读(存储Block的Receipts)

作者: 坠叶飘香 | 来源:发表于2018-09-20 14:53 被阅读0次

1.流程图

存储Receipt

2.

构建key
r + number(区块高度) + hash(区块hash)

go-ethereum/core/rawdb/schema.go

//key:r + number + hash
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
func blockReceiptsKey(number uint64, hash common.Hash) []byte {
    return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
}
存入db
  • 将Receipt类型转换为ReceiptForStorage
  • 将storageReceipts进行RLP Encode
  • 将RLP Encode后的数据存入db

go-ethereum/core/rawdb/accessors_chain.go

// WriteReceipts stores all the transaction receipts belonging to a block.
func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts types.Receipts) {
    // Convert the receipts into their storage form and serialize them
    //Receipt
    storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
    for i, receipt := range receipts {
        storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
    }
    bytes, err := rlp.EncodeToBytes(storageReceipts)
    if err != nil {
        log.Crit("Failed to encode block receipts", "err", err)
    }
    // Store the flattened receipt slice
    if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
        log.Crit("Failed to store block receipts", "err", err)
    }
}

相关文章

网友评论

      本文标题:ETH源码阅读(存储Block的Receipts)

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