github.com/hardtosaygoodbye/go-ethereum@v1.10.16-0.20220122011429-97003b9e6c15/core/rawdb/accessors_state.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"github.com/hardtosaygoodbye/go-ethereum/common"
    21  	"github.com/hardtosaygoodbye/go-ethereum/ethdb"
    22  	"github.com/hardtosaygoodbye/go-ethereum/log"
    23  )
    24  
    25  // ReadPreimage retrieves a single preimage of the provided hash.
    26  func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
    27  	data, _ := db.Get(preimageKey(hash))
    28  	return data
    29  }
    30  
    31  // WritePreimages writes the provided set of preimages to the database.
    32  func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) {
    33  	for hash, preimage := range preimages {
    34  		if err := db.Put(preimageKey(hash), preimage); err != nil {
    35  			log.Crit("Failed to store trie preimage", "err", err)
    36  		}
    37  	}
    38  	preimageCounter.Inc(int64(len(preimages)))
    39  	preimageHitCounter.Inc(int64(len(preimages)))
    40  }
    41  
    42  // ReadCode retrieves the contract code of the provided code hash.
    43  func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte {
    44  	// Try with the legacy code scheme first, if not then try with current
    45  	// scheme. Since most of the code will be found with legacy scheme.
    46  	//
    47  	// todo(rjl493456442) change the order when we forcibly upgrade the code
    48  	// scheme with snapshot.
    49  	data, _ := db.Get(hash[:])
    50  	if len(data) != 0 {
    51  		return data
    52  	}
    53  	return ReadCodeWithPrefix(db, hash)
    54  }
    55  
    56  // ReadCodeWithPrefix retrieves the contract code of the provided code hash.
    57  // The main difference between this function and ReadCode is this function
    58  // will only check the existence with latest scheme(with prefix).
    59  func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
    60  	data, _ := db.Get(codeKey(hash))
    61  	return data
    62  }
    63  
    64  // HasCodeWithPrefix checks if the contract code corresponding to the
    65  // provided code hash is present in the db. This function will only check
    66  // presence using the prefix-scheme.
    67  func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
    68  	ok, _ := db.Has(codeKey(hash))
    69  	return ok
    70  }
    71  
    72  // WriteCode writes the provided contract code database.
    73  func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) {
    74  	if err := db.Put(codeKey(hash), code); err != nil {
    75  		log.Crit("Failed to store contract code", "err", err)
    76  	}
    77  }
    78  
    79  // DeleteCode deletes the specified contract code from the database.
    80  func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
    81  	if err := db.Delete(codeKey(hash)); err != nil {
    82  		log.Crit("Failed to delete contract code", "err", err)
    83  	}
    84  }
    85  
    86  // ReadTrieNode retrieves the trie node of the provided hash.
    87  func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
    88  	data, _ := db.Get(hash.Bytes())
    89  	return data
    90  }
    91  
    92  // HasTrieNode checks if the trie node with the provided hash is present in db.
    93  func HasTrieNode(db ethdb.KeyValueReader, hash common.Hash) bool {
    94  	ok, _ := db.Has(hash.Bytes())
    95  	return ok
    96  }
    97  
    98  // WriteTrieNode writes the provided trie node database.
    99  func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
   100  	if err := db.Put(hash.Bytes(), node); err != nil {
   101  		log.Crit("Failed to store trie node", "err", err)
   102  	}
   103  }
   104  
   105  // DeleteTrieNode deletes the specified trie node from the database.
   106  func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
   107  	if err := db.Delete(hash.Bytes()); err != nil {
   108  		log.Crit("Failed to delete trie node", "err", err)
   109  	}
   110  }