gitlab.com/flarenetwork/coreth@v0.1.1/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/ethereum/go-ethereum/common" 31 "github.com/ethereum/go-ethereum/ethdb" 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 // WritePreimages writes the provided set of preimages to the database. 42 func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) { 43 for hash, preimage := range preimages { 44 if err := db.Put(preimageKey(hash), preimage); err != nil { 45 log.Crit("Failed to store trie preimage", "err", err) 46 } 47 } 48 preimageCounter.Inc(int64(len(preimages))) 49 preimageHitCounter.Inc(int64(len(preimages))) 50 } 51 52 // ReadCode retrieves the contract code of the provided code hash. 53 func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte { 54 // Try with the legacy code scheme first, if not then try with current 55 // scheme. Since most of the code will be found with legacy scheme. 56 // 57 // todo(rjl493456442) change the order when we forcibly upgrade the code 58 // scheme with snapshot. 59 data, _ := db.Get(hash[:]) 60 if len(data) != 0 { 61 return data 62 } 63 return ReadCodeWithPrefix(db, hash) 64 } 65 66 // ReadCodeWithPrefix retrieves the contract code of the provided code hash. 67 // The main difference between this function and ReadCode is this function 68 // will only check the existence with latest scheme(with prefix). 69 func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte { 70 data, _ := db.Get(codeKey(hash)) 71 return data 72 } 73 74 // WriteCode writes the provided contract code database. 75 func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) { 76 if err := db.Put(codeKey(hash), code); err != nil { 77 log.Crit("Failed to store contract code", "err", err) 78 } 79 } 80 81 // DeleteCode deletes the specified contract code from the database. 82 func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) { 83 if err := db.Delete(codeKey(hash)); err != nil { 84 log.Crit("Failed to delete contract code", "err", err) 85 } 86 } 87 88 // ReadTrieNode retrieves the trie node of the provided hash. 89 func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte { 90 data, _ := db.Get(hash.Bytes()) 91 return data 92 } 93 94 // WriteTrieNode writes the provided trie node database. 95 func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) { 96 if err := db.Put(hash.Bytes(), node); err != nil { 97 log.Crit("Failed to store trie node", "err", err) 98 } 99 } 100 101 // DeleteTrieNode deletes the specified trie node from the database. 102 func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) { 103 if err := db.Delete(hash.Bytes()); err != nil { 104 log.Crit("Failed to delete trie node", "err", err) 105 } 106 }