美文网首页以太坊源码学习笔记
以太坊源码阅读(数据存储-从数据库中读取区块和交易)

以太坊源码阅读(数据存储-从数据库中读取区块和交易)

作者: 坠叶飘香 | 来源:发表于2018-08-06 16:33 被阅读0次

1.交易与所在区块的关系

key:"l"+交易hash

value:TxLookupEntry {区块hash,区块高度,交易位于区块的索引}

go-ethereum/core/rawdb/accessors_indexes.go

1.1.写入leveldb
// WriteTxLookupEntries stores a positional metadata for every transaction from
// a block, enabling hash based transaction and receipt lookups.
func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
    for i, tx := range block.Transactions() {
        entry := TxLookupEntry{
            BlockHash:  block.Hash(), 
            BlockIndex: block.NumberU64(), //区块高度
            Index:      uint64(i), //交易位于区块的索引
        }
        data, err := rlp.EncodeToBytes(entry)
        if err != nil {
            log.Crit("Failed to encode transaction lookup entry", "err", err)
        }
                //key:"l"+交易hash  data:TxLookupEntry
        if err := db.Put(append(txLookupPrefix, tx.Hash().Bytes()...), data); err != nil { //存入db
            log.Crit("Failed to store transaction lookup entry", "err", err)
        }
    }
}
1.2. 从leveldb读取
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
// hash to allow retrieving the transaction or receipt by hash.
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
    data, _ := db.Get(append(txLookupPrefix, hash.Bytes()...))
    if len(data) == 0 {
        return common.Hash{}, 0, 0
    }
    var entry TxLookupEntry
    if err := rlp.DecodeBytes(data, &entry); err != nil {
        log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err)
        return common.Hash{}, 0, 0
    }
    return entry.BlockHash, entry.BlockIndex, entry.Index
}

2.block的数据

key:"b" + 区块高度+ 区块hash
value:区块数据rlp后的数据
go-ethereum/core/rawdb/accessors_chain.go

2.1. 写入leveldb
// WriteBodyRLP stores an RLP encoded block body into the database.
func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
    key := append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
    if err := db.Put(key, rlp); err != nil {
        log.Crit("Failed to store block body", "err", err)
    }
}
2.2. 从leveldb读取
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
    data, _ := db.Get(append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
    return data
}

3.通过交易hash获得交易数据

第一步:通过交易hash获得所在区块讯息
第二步:通过区块hash和区块高度获得区块数据
第三步:通过交易位于区块的索引,获得交易数据
go-ethereum/core/rawdb/accessors_indexes.go

// ReadTransaction retrieves a specific transaction from the database, along with
// its added positional metadata.
func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
    blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash) //第一步

    if blockHash == (common.Hash{}) {
        return nil, common.Hash{}, 0, 0
    }
    body := ReadBody(db, blockHash, blockNumber) //第二步
    if body == nil || len(body.Transactions) <= int(txIndex) {
        log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
        return nil, common.Hash{}, 0, 0
    }
    return body.Transactions[txIndex], blockHash, blockNumber, txIndex //第三步
}

4.从leveldb读取的区块数据需要Decode

go-ethereum/core/rawdb/accessors_chain.go

// ReadBody retrieves the block body corresponding to the hash.
func ReadBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body {
    data := ReadBodyRLP(db, hash, number) //从db中读取区块的rlp后的数据
    if len(data) == 0 {
        return nil
    }
    body := new(types.Body)
    if err := rlp.Decode(bytes.NewReader(data), body); err != nil { //从rlp数据解析出Body
        log.Error("Invalid block body RLP", "hash", hash, "err", err)
        return nil
    }
    return body
}

相关文章

网友评论

    本文标题:以太坊源码阅读(数据存储-从数据库中读取区块和交易)

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