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)
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)
atomic.AddInt32(&failed, 1)
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})
// 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
}
// 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
bc.insert(block)
// 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
}
// 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
// receipts that were created in the fork must also be deleted
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
// to acquire the chain manager lock

View file

@ -65,9 +65,9 @@ var (
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.
type lookupEntry struct {
type txLookupEntry struct {
BlockHash common.Hash
BlockIndex uint64
Index uint64
@ -234,16 +234,16 @@ func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types.
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.
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
data, _ := db.Get(append(lookupPrefix, hash.Bytes()...))
if len(data) == 0 {
return common.Hash{}, 0, 0
}
// Parse and return the contents of the lookup entry
var entry lookupEntry
var entry txLookupEntry
if err := rlp.DecodeBytes(data, &entry); err != nil {
log.Error("Invalid lookup entry RLP", "hash", hash, "err", err)
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.
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
blockHash, blockNumber, txIndex := GetLookupEntry(db, hash)
blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash)
if blockHash != (common.Hash{}) {
body := GetBody(db, blockHash, blockNumber)
@ -279,7 +279,7 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co
if len(data) == 0 {
return nil, common.Hash{}, 0, 0
}
var entry lookupEntry
var entry txLookupEntry
if err := rlp.DecodeBytes(data, &entry); err != nil {
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.
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
blockHash, blockNumber, receiptIndex := GetLookupEntry(db, hash)
blockHash, blockNumber, receiptIndex := GetTxLookupEntry(db, hash)
if blockHash != (common.Hash{}) {
receipts := GetBlockReceipts(db, blockHash, blockNumber)
@ -431,14 +431,14 @@ func WriteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64, rece
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.
func WriteLookupEntries(db ethdb.Database, block *types.Block) error {
func WriteTxLookupEntries(db ethdb.Database, block *types.Block) error {
batch := db.NewBatch()
// Iterate over each transaction and encode its metadata
for i, tx := range block.Transactions() {
entry := lookupEntry{
entry := txLookupEntry{
BlockHash: block.Hash(),
BlockIndex: block.NumberU64(),
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()...))
}
// DeleteLookupEntry removes all transaction data associated with a hash.
func DeleteLookupEntry(db ethdb.Database, hash common.Hash) {
// DeleteTxLookupEntry removes all transaction data associated with a hash.
func DeleteTxLookupEntry(db ethdb.Database, hash common.Hash) {
db.Delete(append(lookupPrefix, hash.Bytes()...))
}

View file

@ -311,7 +311,7 @@ func TestLookupStorage(t *testing.T) {
if err := WriteBlock(db, block); err != nil {
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)
}
for i, tx := range txs {
@ -328,7 +328,7 @@ func TestLookupStorage(t *testing.T) {
}
// Delete the transactions and check purge
for i, tx := range txs {
DeleteLookupEntry(db, tx.Hash())
DeleteTxLookupEntry(db, tx.Hash())
if txn, _, _, _ := GetTransaction(db, tx.Hash()); txn != nil {
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
return err
}
if err := core.WriteLookupEntries(pool.chainDb, block); err != nil {
if err := core.WriteTxLookupEntries(pool.chainDb, block); err != nil {
return err
}
// 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 {
for _, tx := range list {
txHash := tx.Hash()
core.DeleteLookupEntry(pool.chainDb, txHash)
core.DeleteTxLookupEntry(pool.chainDb, txHash)
pool.pending[txHash] = tx
txc.setState(txHash, false)
}

View file

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