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
}
网友评论