github.com/EgonCoin/EgonChain@v1.10.16/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/EgonCoin/EgonChain/common"
    21  	"github.com/EgonCoin/EgonChain/ethdb"
    22  	"github.com/EgonCoin/EgonChain/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 prefixed code scheme first, if not then try with legacy
    45  	// scheme.
    46  	data := ReadCodeWithPrefix(db, hash)
    47  	if len(data) != 0 {
    48  		return data
    49  	}
    50  	data, _ = db.Get(hash[:])
    51  	return data
    52  }
    53  
    54  // ReadCodeWithPrefix retrieves the contract code of the provided code hash.
    55  // The main difference between this function and ReadCode is this function
    56  // will only check the existence with latest scheme(with prefix).
    57  func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
    58  	data, _ := db.Get(codeKey(hash))
    59  	return data
    60  }
    61  
    62  // HasCodeWithPrefix checks if the contract code corresponding to the
    63  // provided code hash is present in the db. This function will only check
    64  // presence using the prefix-scheme.
    65  func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
    66  	ok, _ := db.Has(codeKey(hash))
    67  	return ok
    68  }
    69  
    70  // WriteCode writes the provided contract code database.
    71  func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) {
    72  	if err := db.Put(codeKey(hash), code); err != nil {
    73  		log.Crit("Failed to store contract code", "err", err)
    74  	}
    75  }
    76  
    77  // DeleteCode deletes the specified contract code from the database.
    78  func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
    79  	if err := db.Delete(codeKey(hash)); err != nil {
    80  		log.Crit("Failed to delete contract code", "err", err)
    81  	}
    82  }
    83  
    84  // ReadTrieNode retrieves the trie node of the provided hash.
    85  func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
    86  	data, _ := db.Get(hash.Bytes())
    87  	return data
    88  }
    89  
    90  // HasTrieNode checks if the trie node with the provided hash is present in db.
    91  func HasTrieNode(db ethdb.KeyValueReader, hash common.Hash) bool {
    92  	ok, _ := db.Has(hash.Bytes())
    93  	return ok
    94  }
    95  
    96  // WriteTrieNode writes the provided trie node database.
    97  func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
    98  	if err := db.Put(hash.Bytes(), node); err != nil {
    99  		log.Crit("Failed to store trie node", "err", err)
   100  	}
   101  }
   102  
   103  // DeleteTrieNode deletes the specified trie node from the database.
   104  func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
   105  	if err := db.Delete(hash.Bytes()); err != nil {
   106  		log.Crit("Failed to delete trie node", "err", err)
   107  	}
   108  }