github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/rawdb/accessors_indexes.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:35</date> 10 //</624450079364747264> 11 12 13 package rawdb 14 15 import ( 16 "github.com/ethereum/go-ethereum/common" 17 "github.com/ethereum/go-ethereum/core/types" 18 "github.com/ethereum/go-ethereum/log" 19 "github.com/ethereum/go-ethereum/rlp" 20 ) 21 22 //Read TxLoopUpTeAccess检索与事务关联的位置元数据 23 //哈希以允许按哈希检索事务或收据。 24 func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) { 25 data, _ := db.Get(txLookupKey(hash)) 26 if len(data) == 0 { 27 return common.Hash{}, 0, 0 28 } 29 var entry TxLookupEntry 30 if err := rlp.DecodeBytes(data, &entry); err != nil { 31 log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err) 32 return common.Hash{}, 0, 0 33 } 34 return entry.BlockHash, entry.BlockIndex, entry.Index 35 } 36 37 //WraveTxLoopUpStand为每个事务存储位置元数据。 38 //一个块,启用基于哈希的事务和收据查找。 39 func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) { 40 for i, tx := range block.Transactions() { 41 entry := TxLookupEntry{ 42 BlockHash: block.Hash(), 43 BlockIndex: block.NumberU64(), 44 Index: uint64(i), 45 } 46 data, err := rlp.EncodeToBytes(entry) 47 if err != nil { 48 log.Crit("Failed to encode transaction lookup entry", "err", err) 49 } 50 if err := db.Put(txLookupKey(tx.Hash()), data); err != nil { 51 log.Crit("Failed to store transaction lookup entry", "err", err) 52 } 53 } 54 } 55 56 //DeleteTxLookupEntry删除与哈希关联的所有事务数据。 57 func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { 58 db.Delete(txLookupKey(hash)) 59 } 60 61 //readTransaction从数据库中检索特定事务,以及 62 //它添加了位置元数据。 63 func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { 64 blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash) 65 if blockHash == (common.Hash{}) { 66 return nil, common.Hash{}, 0, 0 67 } 68 body := ReadBody(db, blockHash, blockNumber) 69 if body == nil || len(body.Transactions) <= int(txIndex) { 70 log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex) 71 return nil, common.Hash{}, 0, 0 72 } 73 return body.Transactions[txIndex], blockHash, blockNumber, txIndex 74 } 75 76 //readreceipt从数据库中检索特定的事务回执,以及 77 //它添加了位置元数据。 78 func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { 79 blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash) 80 if blockHash == (common.Hash{}) { 81 return nil, common.Hash{}, 0, 0 82 } 83 receipts := ReadReceipts(db, blockHash, blockNumber) 84 if len(receipts) <= int(receiptIndex) { 85 log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex) 86 return nil, common.Hash{}, 0, 0 87 } 88 return receipts[receiptIndex], blockHash, blockNumber, receiptIndex 89 } 90 91 //readBloomBits检索属于给定的 92 //中的节和位索引。 93 func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) { 94 return db.Get(bloomBitsKey(bit, section, head)) 95 } 96 97 //WriteBloomBits存储属于给定的 98 //节和位索引。 99 func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) { 100 if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil { 101 log.Crit("Failed to store bloom bits", "err", err) 102 } 103 } 104