github.com/gilgames000/kcc-geth@v1.0.6/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/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/ethdb"
    22  	"github.com/ethereum/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  // WriteCode writes the provided contract code database.
    65  func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) {
    66  	if err := db.Put(codeKey(hash), code); err != nil {
    67  		log.Crit("Failed to store contract code", "err", err)
    68  	}
    69  }
    70  
    71  // DeleteCode deletes the specified contract code from the database.
    72  func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
    73  	if err := db.Delete(codeKey(hash)); err != nil {
    74  		log.Crit("Failed to delete contract code", "err", err)
    75  	}
    76  }
    77  
    78  // ReadTrieNode retrieves the trie node of the provided hash.
    79  func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
    80  	data, _ := db.Get(hash.Bytes())
    81  	return data
    82  }
    83  
    84  // WriteTrieNode writes the provided trie node database.
    85  func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
    86  	if err := db.Put(hash.Bytes(), node); err != nil {
    87  		log.Crit("Failed to store trie node", "err", err)
    88  	}
    89  }
    90  
    91  // DeleteTrieNode deletes the specified trie node from the database.
    92  func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
    93  	if err := db.Delete(hash.Bytes()); err != nil {
    94  		log.Crit("Failed to delete trie node", "err", err)
    95  	}
    96  }