github.com/MetalBlockchain/subnet-evm@v0.4.9/core/rawdb/accessors_state.go (about)

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