core: prefix the lookup entries to make their purpose clearer

This commit is contained in:
Péter Szilágyi 2017-07-14 19:21:27 +03:00
parent 7f33092187
commit 9cc1bd9cf8
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
5 changed files with 22 additions and 22 deletions

View file

@ -759,7 +759,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
log.Crit("Failed to write log blooms", "err", err) log.Crit("Failed to write log blooms", "err", err)
return return
} }
if err := WriteLookupEntries(bc.chainDb, block); err != nil { if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
errs[index] = fmt.Errorf("failed to write lookup metadata: %v", err) errs[index] = fmt.Errorf("failed to write lookup metadata: %v", err)
atomic.AddInt32(&failed, 1) atomic.AddInt32(&failed, 1)
log.Crit("Failed to write lookup metadata", "err", err) log.Crit("Failed to write lookup metadata", "err", err)
@ -997,7 +997,7 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
events = append(events, ChainEvent{block, block.Hash(), logs}) events = append(events, ChainEvent{block, block.Hash(), logs})
// Write the positional metadata for transaction and receipt lookups // Write the positional metadata for transaction and receipt lookups
if err := WriteLookupEntries(bc.chainDb, block); err != nil { if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
return i, err return i, err
} }
// Write map map bloom filters // Write map map bloom filters
@ -1158,7 +1158,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
// insert the block in the canonical way, re-writing history // insert the block in the canonical way, re-writing history
bc.insert(block) bc.insert(block)
// write lookup entries for hash based transaction/receipt searches // write lookup entries for hash based transaction/receipt searches
if err := WriteLookupEntries(bc.chainDb, block); err != nil { if err := WriteTxLookupEntries(bc.chainDb, block); err != nil {
return err return err
} }
// Write map map bloom filters // Write map map bloom filters
@ -1174,7 +1174,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
// When transactions get deleted from the database that means the // When transactions get deleted from the database that means the
// receipts that were created in the fork must also be deleted // receipts that were created in the fork must also be deleted
for _, tx := range diff { for _, tx := range diff {
DeleteLookupEntry(bc.chainDb, tx.Hash()) DeleteTxLookupEntry(bc.chainDb, tx.Hash())
} }
// Must be posted in a goroutine because of the transaction pool trying // Must be posted in a goroutine because of the transaction pool trying
// to acquire the chain manager lock // to acquire the chain manager lock

View file

@ -65,9 +65,9 @@ var (
preimageHitCounter = metrics.NewCounter("db/preimage/hits") preimageHitCounter = metrics.NewCounter("db/preimage/hits")
) )
// lookupEntry is a positional metadata to help looking up the data content of // txLookupEntry is a positional metadata to help looking up the data content of
// a transaction or receipt given only its hash. // a transaction or receipt given only its hash.
type lookupEntry struct { type txLookupEntry struct {
BlockHash common.Hash BlockHash common.Hash
BlockIndex uint64 BlockIndex uint64
Index uint64 Index uint64
@ -234,16 +234,16 @@ func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types.
return receipts return receipts
} }
// GetLookupEntry retrieves the positional metadata associated with a transaction // GetTxLookupEntry retrieves the positional metadata associated with a transaction
// hash to allow retrieving the transaction or receipt by hash. // hash to allow retrieving the transaction or receipt by hash.
func GetLookupEntry(db ethdb.Database, hash common.Hash) (common.Hash, uint64, uint64) { func GetTxLookupEntry(db ethdb.Database, hash common.Hash) (common.Hash, uint64, uint64) {
// Load the positional metadata from disk and bail if it fails // Load the positional metadata from disk and bail if it fails
data, _ := db.Get(append(lookupPrefix, hash.Bytes()...)) data, _ := db.Get(append(lookupPrefix, hash.Bytes()...))
if len(data) == 0 { if len(data) == 0 {
return common.Hash{}, 0, 0 return common.Hash{}, 0, 0
} }
// Parse and return the contents of the lookup entry // Parse and return the contents of the lookup entry
var entry lookupEntry var entry txLookupEntry
if err := rlp.DecodeBytes(data, &entry); err != nil { if err := rlp.DecodeBytes(data, &entry); err != nil {
log.Error("Invalid lookup entry RLP", "hash", hash, "err", err) log.Error("Invalid lookup entry RLP", "hash", hash, "err", err)
return common.Hash{}, 0, 0 return common.Hash{}, 0, 0
@ -255,7 +255,7 @@ func GetLookupEntry(db ethdb.Database, hash common.Hash) (common.Hash, uint64, u
// its added positional metadata. // its added positional metadata.
func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
// Retrieve the lookup metadata and resolve the transaction from the body // Retrieve the lookup metadata and resolve the transaction from the body
blockHash, blockNumber, txIndex := GetLookupEntry(db, hash) blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash)
if blockHash != (common.Hash{}) { if blockHash != (common.Hash{}) {
body := GetBody(db, blockHash, blockNumber) body := GetBody(db, blockHash, blockNumber)
@ -279,7 +279,7 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co
if len(data) == 0 { if len(data) == 0 {
return nil, common.Hash{}, 0, 0 return nil, common.Hash{}, 0, 0
} }
var entry lookupEntry var entry txLookupEntry
if err := rlp.DecodeBytes(data, &entry); err != nil { if err := rlp.DecodeBytes(data, &entry); err != nil {
return nil, common.Hash{}, 0, 0 return nil, common.Hash{}, 0, 0
} }
@ -290,7 +290,7 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co
// its added positional metadata. // its added positional metadata.
func GetReceipt(db ethdb.Database, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { func GetReceipt(db ethdb.Database, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
// Retrieve the lookup metadata and resolve the receipt from the receipts // Retrieve the lookup metadata and resolve the receipt from the receipts
blockHash, blockNumber, receiptIndex := GetLookupEntry(db, hash) blockHash, blockNumber, receiptIndex := GetTxLookupEntry(db, hash)
if blockHash != (common.Hash{}) { if blockHash != (common.Hash{}) {
receipts := GetBlockReceipts(db, blockHash, blockNumber) receipts := GetBlockReceipts(db, blockHash, blockNumber)
@ -431,14 +431,14 @@ func WriteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64, rece
return nil return nil
} }
// WriteLookupEntries stores a positional metadata for every transaction from // WriteTxLookupEntries stores a positional metadata for every transaction from
// a block, enabling hash based transaction and receipt lookups. // a block, enabling hash based transaction and receipt lookups.
func WriteLookupEntries(db ethdb.Database, block *types.Block) error { func WriteTxLookupEntries(db ethdb.Database, block *types.Block) error {
batch := db.NewBatch() batch := db.NewBatch()
// Iterate over each transaction and encode its metadata // Iterate over each transaction and encode its metadata
for i, tx := range block.Transactions() { for i, tx := range block.Transactions() {
entry := lookupEntry{ entry := txLookupEntry{
BlockHash: block.Hash(), BlockHash: block.Hash(),
BlockIndex: block.NumberU64(), BlockIndex: block.NumberU64(),
Index: uint64(i), Index: uint64(i),
@ -492,8 +492,8 @@ func DeleteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) {
db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
} }
// DeleteLookupEntry removes all transaction data associated with a hash. // DeleteTxLookupEntry removes all transaction data associated with a hash.
func DeleteLookupEntry(db ethdb.Database, hash common.Hash) { func DeleteTxLookupEntry(db ethdb.Database, hash common.Hash) {
db.Delete(append(lookupPrefix, hash.Bytes()...)) db.Delete(append(lookupPrefix, hash.Bytes()...))
} }

View file

@ -311,7 +311,7 @@ func TestLookupStorage(t *testing.T) {
if err := WriteBlock(db, block); err != nil { if err := WriteBlock(db, block); err != nil {
t.Fatalf("failed to write block contents: %v", err) t.Fatalf("failed to write block contents: %v", err)
} }
if err := WriteLookupEntries(db, block); err != nil { if err := WriteTxLookupEntries(db, block); err != nil {
t.Fatalf("failed to write transactions: %v", err) t.Fatalf("failed to write transactions: %v", err)
} }
for i, tx := range txs { for i, tx := range txs {
@ -328,7 +328,7 @@ func TestLookupStorage(t *testing.T) {
} }
// Delete the transactions and check purge // Delete the transactions and check purge
for i, tx := range txs { for i, tx := range txs {
DeleteLookupEntry(db, tx.Hash()) DeleteTxLookupEntry(db, tx.Hash())
if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil { if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
} }

View file

@ -181,7 +181,7 @@ func (pool *TxPool) checkMinedTxs(ctx context.Context, hash common.Hash, number
if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results if _, err := GetBlockReceipts(ctx, pool.odr, hash, number); err != nil { // ODR caches, ignore results
return err return err
} }
if err := core.WriteLookupEntries(pool.chainDb, block); err != nil { if err := core.WriteTxLookupEntries(pool.chainDb, block); err != nil {
return err return err
} }
// Update the transaction pool's state // Update the transaction pool's state
@ -200,7 +200,7 @@ func (pool *TxPool) rollbackTxs(hash common.Hash, txc txStateChanges) {
if list, ok := pool.mined[hash]; ok { if list, ok := pool.mined[hash]; ok {
for _, tx := range list { for _, tx := range list {
txHash := tx.Hash() txHash := tx.Hash()
core.DeleteLookupEntry(pool.chainDb, txHash) core.DeleteTxLookupEntry(pool.chainDb, txHash)
pool.pending[txHash] = tx pool.pending[txHash] = tx
txc.setState(txHash, false) txc.setState(txHash, false)
} }

View file

@ -293,7 +293,7 @@ func (self *worker) wait() {
// check if canon block and write transactions // check if canon block and write transactions
if stat == core.CanonStatTy { if stat == core.CanonStatTy {
// This puts transactions in a extra db for rpc // This puts transactions in a extra db for rpc
core.WriteLookupEntries(self.chainDb, block) core.WriteTxLookupEntries(self.chainDb, block)
// Write map map bloom filters // Write map map bloom filters
core.WriteMipmapBloom(self.chainDb, block.NumberU64(), work.receipts) core.WriteMipmapBloom(self.chainDb, block.NumberU64(), work.receipts)
// implicit by posting ChainHeadEvent // implicit by posting ChainHeadEvent