|
| 1 | +// Copyright 2025 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 | + "encoding/binary" |
| 21 | + |
| 22 | + "github.com/ethereum/go-ethereum/common" |
| 23 | + "github.com/ethereum/go-ethereum/ethdb" |
| 24 | + "github.com/ethereum/go-ethereum/log" |
| 25 | +) |
| 26 | + |
| 27 | +// ReadLastStateHistoryIndex retrieves the number of latest indexed state history. |
| 28 | +func ReadLastStateHistoryIndex(db ethdb.KeyValueReader) *uint64 { |
| 29 | + data, _ := db.Get(headStateHistoryIndexKey) |
| 30 | + if len(data) != 8 { |
| 31 | + return nil |
| 32 | + } |
| 33 | + number := binary.BigEndian.Uint64(data) |
| 34 | + return &number |
| 35 | +} |
| 36 | + |
| 37 | +// WriteLastStateHistoryIndex stores the number of latest indexed state history |
| 38 | +// into database. |
| 39 | +func WriteLastStateHistoryIndex(db ethdb.KeyValueWriter, number uint64) { |
| 40 | + if err := db.Put(headStateHistoryIndexKey, encodeBlockNumber(number)); err != nil { |
| 41 | + log.Crit("Failed to store the state index tail", "err", err) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// DeleteLastStateHistoryIndex removes the number of latest indexed state history. |
| 46 | +func DeleteLastStateHistoryIndex(db ethdb.KeyValueWriter) { |
| 47 | + if err := db.Delete(headStateHistoryIndexKey); err != nil { |
| 48 | + log.Crit("Failed to delete the state index tail", "err", err) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// ReadAccountHistoryIndex retrieves the account history index with the provided |
| 53 | +// account address. |
| 54 | +func ReadAccountHistoryIndex(db ethdb.KeyValueReader, address common.Address) []byte { |
| 55 | + data, err := db.Get(accountHistoryIndexKey(address)) |
| 56 | + if err != nil || len(data) == 0 { |
| 57 | + return nil |
| 58 | + } |
| 59 | + return data |
| 60 | +} |
| 61 | + |
| 62 | +// WriteAccountHistoryIndex writes the provided account history index into database. |
| 63 | +func WriteAccountHistoryIndex(db ethdb.KeyValueWriter, address common.Address, data []byte) { |
| 64 | + if err := db.Put(accountHistoryIndexKey(address), data); err != nil { |
| 65 | + log.Crit("Failed to store account history index", "err", err) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// DeleteAccountHistoryIndex deletes the specified account history index from |
| 70 | +// the database. |
| 71 | +func DeleteAccountHistoryIndex(db ethdb.KeyValueWriter, address common.Address) { |
| 72 | + if err := db.Delete(accountHistoryIndexKey(address)); err != nil { |
| 73 | + log.Crit("Failed to delete account history index", "err", err) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// ReadStorageHistoryIndex retrieves the storage history index with the provided |
| 78 | +// account address and storage key hash. |
| 79 | +func ReadStorageHistoryIndex(db ethdb.KeyValueReader, address common.Address, storageHash common.Hash) []byte { |
| 80 | + data, err := db.Get(storageHistoryIndexKey(address, storageHash)) |
| 81 | + if err != nil || len(data) == 0 { |
| 82 | + return nil |
| 83 | + } |
| 84 | + return data |
| 85 | +} |
| 86 | + |
| 87 | +// WriteStorageHistoryIndex writes the provided storage history index into database. |
| 88 | +func WriteStorageHistoryIndex(db ethdb.KeyValueWriter, address common.Address, storageHash common.Hash, data []byte) { |
| 89 | + if err := db.Put(storageHistoryIndexKey(address, storageHash), data); err != nil { |
| 90 | + log.Crit("Failed to store storage history index", "err", err) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// DeleteStorageHistoryIndex deletes the specified state index from the database. |
| 95 | +func DeleteStorageHistoryIndex(db ethdb.KeyValueWriter, address common.Address, storageHash common.Hash) { |
| 96 | + if err := db.Delete(storageHistoryIndexKey(address, storageHash)); err != nil { |
| 97 | + log.Crit("Failed to delete storage history index", "err", err) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// ReadAccountHistoryIndexBlock retrieves the index block with the provided |
| 102 | +// account address along with the block id. |
| 103 | +func ReadAccountHistoryIndexBlock(db ethdb.KeyValueReader, address common.Address, blockID uint32) []byte { |
| 104 | + data, err := db.Get(accountHistoryIndexBlockKey(address, blockID)) |
| 105 | + if err != nil || len(data) == 0 { |
| 106 | + return nil |
| 107 | + } |
| 108 | + return data |
| 109 | +} |
| 110 | + |
| 111 | +// WriteAccountHistoryIndexBlock writes the provided index block into database. |
| 112 | +func WriteAccountHistoryIndexBlock(db ethdb.KeyValueWriter, address common.Address, blockID uint32, data []byte) { |
| 113 | + if err := db.Put(accountHistoryIndexBlockKey(address, blockID), data); err != nil { |
| 114 | + log.Crit("Failed to store account index block", "err", err) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// DeleteAccountHistoryIndexBlock deletes the specified index block from the database. |
| 119 | +func DeleteAccountHistoryIndexBlock(db ethdb.KeyValueWriter, address common.Address, blockID uint32) { |
| 120 | + if err := db.Delete(accountHistoryIndexBlockKey(address, blockID)); err != nil { |
| 121 | + log.Crit("Failed to delete account index block", "err", err) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// ReadStorageHistoryIndexBlock retrieves the index block with the provided state |
| 126 | +// identifier along with the block id. |
| 127 | +func ReadStorageHistoryIndexBlock(db ethdb.KeyValueReader, address common.Address, storageHash common.Hash, blockID uint32) []byte { |
| 128 | + data, err := db.Get(storageHistoryIndexBlockKey(address, storageHash, blockID)) |
| 129 | + if err != nil || len(data) == 0 { |
| 130 | + return nil |
| 131 | + } |
| 132 | + return data |
| 133 | +} |
| 134 | + |
| 135 | +// WriteStorageHistoryIndexBlock writes the provided index block into database. |
| 136 | +func WriteStorageHistoryIndexBlock(db ethdb.KeyValueWriter, address common.Address, storageHash common.Hash, id uint32, data []byte) { |
| 137 | + if err := db.Put(storageHistoryIndexBlockKey(address, storageHash, id), data); err != nil { |
| 138 | + log.Crit("Failed to store storage index block", "err", err) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// DeleteStorageHistoryIndexBlock deletes the specified index block from the database. |
| 143 | +func DeleteStorageHistoryIndexBlock(db ethdb.KeyValueWriter, address common.Address, state common.Hash, id uint32) { |
| 144 | + if err := db.Delete(storageHistoryIndexBlockKey(address, state, id)); err != nil { |
| 145 | + log.Crit("Failed to delete storage index block", "err", err) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// increaseKey increase the input key by one bit. Return nil if the entire |
| 150 | +// addition operation overflows. |
| 151 | +func increaseKey(key []byte) []byte { |
| 152 | + for i := len(key) - 1; i >= 0; i-- { |
| 153 | + key[i]++ |
| 154 | + if key[i] != 0x0 { |
| 155 | + return key |
| 156 | + } |
| 157 | + } |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +// DeleteHistoryIndex completely removes all history indexing data, including indexes |
| 162 | +// for accounts and storages. |
| 163 | +// |
| 164 | +// Note, this method assumes the storage space with prefix `StateHistoryIndexPrefix` |
| 165 | +// is exclusively occupied by the history indexing data! |
| 166 | +func DeleteHistoryIndex(db ethdb.KeyValueRangeDeleter) { |
| 167 | + if err := db.DeleteRange(StateHistoryIndexPrefix, increaseKey(StateHistoryIndexPrefix)); err != nil { |
| 168 | + log.Crit("Failed to delete history index range", "err", err) |
| 169 | + } |
| 170 | +} |
0 commit comments