github.com/mysteriumnetwork/go-ethereum@v1.11.0/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 // ReadCode retrieves the contract code of the provided code hash. 32 func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte { 33 // Try with the prefixed code scheme first, if not then try with legacy 34 // scheme. 35 data := ReadCodeWithPrefix(db, hash) 36 if len(data) != 0 { 37 return data 38 } 39 data, _ = db.Get(hash.Bytes()) 40 return data 41 } 42 43 // ReadCodeWithPrefix retrieves the contract code of the provided code hash. 44 // The main difference between this function and ReadCode is this function 45 // will only check the existence with latest scheme(with prefix). 46 func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte { 47 data, _ := db.Get(codeKey(hash)) 48 return data 49 } 50 51 // ReadTrieNode retrieves the trie node of the provided hash. 52 func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte { 53 data, _ := db.Get(hash.Bytes()) 54 return data 55 } 56 57 // HasCode checks if the contract code corresponding to the 58 // provided code hash is present in the db. 59 func HasCode(db ethdb.KeyValueReader, hash common.Hash) bool { 60 // Try with the prefixed code scheme first, if not then try with legacy 61 // scheme. 62 if ok := HasCodeWithPrefix(db, hash); ok { 63 return true 64 } 65 ok, _ := db.Has(hash.Bytes()) 66 return ok 67 } 68 69 // HasCodeWithPrefix checks if the contract code corresponding to the 70 // provided code hash is present in the db. This function will only check 71 // presence using the prefix-scheme. 72 func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool { 73 ok, _ := db.Has(codeKey(hash)) 74 return ok 75 } 76 77 // HasTrieNode checks if the trie node with the provided hash is present in db. 78 func HasTrieNode(db ethdb.KeyValueReader, hash common.Hash) bool { 79 ok, _ := db.Has(hash.Bytes()) 80 return ok 81 } 82 83 // WritePreimages writes the provided set of preimages to the database. 84 func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) { 85 for hash, preimage := range preimages { 86 if err := db.Put(preimageKey(hash), preimage); err != nil { 87 log.Crit("Failed to store trie preimage", "err", err) 88 } 89 } 90 preimageCounter.Inc(int64(len(preimages))) 91 preimageHitCounter.Inc(int64(len(preimages))) 92 } 93 94 // WriteCode writes the provided contract code database. 95 func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) { 96 if err := db.Put(codeKey(hash), code); err != nil { 97 log.Crit("Failed to store contract code", "err", err) 98 } 99 } 100 101 // WriteTrieNode writes the provided trie node database. 102 func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) { 103 if err := db.Put(hash.Bytes(), node); err != nil { 104 log.Crit("Failed to store trie node", "err", err) 105 } 106 } 107 108 // DeleteCode deletes the specified contract code from the database. 109 func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) { 110 if err := db.Delete(codeKey(hash)); err != nil { 111 log.Crit("Failed to delete contract code", "err", err) 112 } 113 } 114 115 // DeleteTrieNode deletes the specified trie node from the database. 116 func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) { 117 if err := db.Delete(hash.Bytes()); err != nil { 118 log.Crit("Failed to delete trie node", "err", err) 119 } 120 }