github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/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/tacshi/go-ethereum/common"
    21  	"github.com/tacshi/go-ethereum/ethdb"
    22  	"github.com/tacshi/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 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.Bytes())
    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  // HasCode checks if the contract code corresponding to the
    63  // provided code hash is present in the db.
    64  func HasCode(db ethdb.KeyValueReader, hash common.Hash) bool {
    65  	// Try with the prefixed code scheme first, if not then try with legacy
    66  	// scheme.
    67  	if ok := HasCodeWithPrefix(db, hash); ok {
    68  		return true
    69  	}
    70  	ok, _ := db.Has(hash.Bytes())
    71  	return ok
    72  }
    73  
    74  // HasCodeWithPrefix checks if the contract code corresponding to the
    75  // provided code hash is present in the db. This function will only check
    76  // presence using the prefix-scheme.
    77  func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
    78  	ok, _ := db.Has(codeKey(hash))
    79  	return ok
    80  }
    81  
    82  // WriteCode writes the provided contract code database.
    83  func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) {
    84  	if err := db.Put(codeKey(hash), code); err != nil {
    85  		log.Crit("Failed to store contract code", "err", err)
    86  	}
    87  }
    88  
    89  // DeleteCode deletes the specified contract code from the database.
    90  func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
    91  	if err := db.Delete(codeKey(hash)); err != nil {
    92  		log.Crit("Failed to delete contract code", "err", err)
    93  	}
    94  }