美文网首页
ETH源码阅读(TxLookupEntry)

ETH源码阅读(TxLookupEntry)

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

    1.数据结构

    TxLookupEntry:一个交易所对应的区块讯息

    // TxLookupEntry is a positional metadata to help looking up the data content of
    // a transaction or receipt given only its hash.
    type TxLookupEntry struct {
        BlockHash  common.Hash  //区块hash
        BlockIndex uint64          //区块高度
        Index      uint64           //交易在区块中的索引
    }
    

    2.源码

    TxLookupEntry在db中存储的key:l + hash(交易hash)
    //l + hash
    // txLookupKey = txLookupPrefix + hash
    func txLookupKey(hash common.Hash) []byte {
        return append(txLookupPrefix, hash.Bytes()...)
    }
    
    通过交易hash从db读取TxLookupEntry

    go-ethereum/core/rawdb/accessors_indexes.go

    // 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(txLookupKey(hash)) //通过key从db读取出data
        if len(data) == 0 {
            return common.Hash{}, 0, 0
        }
        var entry TxLookupEntry
        if err := rlp.DecodeBytes(data, &entry); err != nil { //将data解析成TxLookupEntry
            log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err)
            return common.Hash{}, 0, 0
        }
        return entry.BlockHash, entry.BlockIndex, entry.Index
    }
    

    相关文章

      网友评论

          本文标题:ETH源码阅读(TxLookupEntry)

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