github.com/core-coin/go-core/v2@v2.1.9/core/rawdb/accessors_state.go (about)

     1  // Copyright 2020 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rawdb
    18  
    19  import (
    20  	"github.com/core-coin/go-core/v2/xcbdb"
    21  
    22  	"github.com/core-coin/go-core/v2/common"
    23  	"github.com/core-coin/go-core/v2/log"
    24  )
    25  
    26  // ReadPreimage retrieves a single preimage of the provided hash.
    27  func ReadPreimage(db xcbdb.KeyValueReader, hash common.Hash) []byte {
    28  	data, _ := db.Get(preimageKey(hash))
    29  	return data
    30  }
    31  
    32  // WritePreimages writes the provided set of preimages to the database.
    33  func WritePreimages(db xcbdb.KeyValueWriter, preimages map[common.Hash][]byte) {
    34  	for hash, preimage := range preimages {
    35  		if err := db.Put(preimageKey(hash), preimage); err != nil {
    36  			log.Crit("Failed to store trie preimage", "err", err)
    37  		}
    38  	}
    39  	preimageCounter.Inc(int64(len(preimages)))
    40  	preimageHitCounter.Inc(int64(len(preimages)))
    41  }
    42  
    43  // ReadCode retrieves the contract code of the provided code hash.
    44  func ReadCode(db xcbdb.KeyValueReader, hash common.Hash) []byte {
    45  	// Try with the legacy code scheme first, if not then try with current
    46  	// scheme. Since most of the code will be found with legacy scheme.
    47  	//
    48  	// todo(raisty) change the order when we forcibly upgrade the code
    49  	// scheme with snapshot.
    50  	data, _ := db.Get(hash[:])
    51  	if len(data) != 0 {
    52  		return data
    53  	}
    54  	return ReadCodeWithPrefix(db, hash)
    55  }
    56  
    57  // ReadCodeWithPrefix retrieves the contract code of the provided code hash.
    58  // The main difference between this function and ReadCode is this function
    59  // will only check the existence with latest scheme(with prefix).
    60  func ReadCodeWithPrefix(db xcbdb.KeyValueReader, hash common.Hash) []byte {
    61  	data, _ := db.Get(codeKey(hash))
    62  	return data
    63  }
    64  
    65  // WriteCode writes the provided contract code database.
    66  func WriteCode(db xcbdb.KeyValueWriter, hash common.Hash, code []byte) {
    67  	if err := db.Put(codeKey(hash), code); err != nil {
    68  		log.Crit("Failed to store contract code", "err", err)
    69  	}
    70  }
    71  
    72  // DeleteCode deletes the specified contract code from the database.
    73  func DeleteCode(db xcbdb.KeyValueWriter, hash common.Hash) {
    74  	if err := db.Delete(codeKey(hash)); err != nil {
    75  		log.Crit("Failed to delete contract code", "err", err)
    76  	}
    77  }
    78  
    79  // ReadTrieNode retrieves the trie node of the provided hash.
    80  func ReadTrieNode(db xcbdb.KeyValueReader, hash common.Hash) []byte {
    81  	data, _ := db.Get(hash.Bytes())
    82  	return data
    83  }
    84  
    85  // WriteTrieNode writes the provided trie node database.
    86  func WriteTrieNode(db xcbdb.KeyValueWriter, hash common.Hash, node []byte) {
    87  	if err := db.Put(hash.Bytes(), node); err != nil {
    88  		log.Crit("Failed to store trie node", "err", err)
    89  	}
    90  }
    91  
    92  // DeleteTrieNode deletes the specified trie node from the database.
    93  func DeleteTrieNode(db xcbdb.KeyValueWriter, hash common.Hash) {
    94  	if err := db.Delete(hash.Bytes()); err != nil {
    95  		log.Crit("Failed to delete trie node", "err", err)
    96  	}
    97  }