|
17 | 17 | package rawdb
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "encoding/binary" |
| 21 | + |
20 | 22 | "github.com/ethereum/go-ethereum/common"
|
21 | 23 | "github.com/ethereum/go-ethereum/ethdb"
|
22 | 24 | "github.com/ethereum/go-ethereum/log"
|
@@ -92,3 +94,173 @@ func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
|
92 | 94 | log.Crit("Failed to delete contract code", "err", err)
|
93 | 95 | }
|
94 | 96 | }
|
| 97 | + |
| 98 | +// ReadStateID retrieves the state id with the provided state root. |
| 99 | +func ReadStateID(db ethdb.KeyValueReader, root common.Hash) *uint64 { |
| 100 | + data, err := db.Get(stateIDKey(root)) |
| 101 | + if err != nil || len(data) == 0 { |
| 102 | + return nil |
| 103 | + } |
| 104 | + number := binary.BigEndian.Uint64(data) |
| 105 | + return &number |
| 106 | +} |
| 107 | + |
| 108 | +// WriteStateID writes the provided state lookup to database. |
| 109 | +func WriteStateID(db ethdb.KeyValueWriter, root common.Hash, id uint64) { |
| 110 | + var buff [8]byte |
| 111 | + binary.BigEndian.PutUint64(buff[:], id) |
| 112 | + if err := db.Put(stateIDKey(root), buff[:]); err != nil { |
| 113 | + log.Crit("Failed to store state ID", "err", err) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// DeleteStateID deletes the specified state lookup from the database. |
| 118 | +func DeleteStateID(db ethdb.KeyValueWriter, root common.Hash) { |
| 119 | + if err := db.Delete(stateIDKey(root)); err != nil { |
| 120 | + log.Crit("Failed to delete state ID", "err", err) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ReadPersistentStateID retrieves the id of the persistent state from the database. |
| 125 | +func ReadPersistentStateID(db ethdb.KeyValueReader) uint64 { |
| 126 | + data, _ := db.Get(persistentStateIDKey) |
| 127 | + if len(data) != 8 { |
| 128 | + return 0 |
| 129 | + } |
| 130 | + return binary.BigEndian.Uint64(data) |
| 131 | +} |
| 132 | + |
| 133 | +// WritePersistentStateID stores the id of the persistent state into database. |
| 134 | +func WritePersistentStateID(db ethdb.KeyValueWriter, number uint64) { |
| 135 | + if err := db.Put(persistentStateIDKey, encodeBlockNumber(number)); err != nil { |
| 136 | + log.Crit("Failed to store the persistent state ID", "err", err) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// ReadTrieJournal retrieves the serialized in-memory trie nodes of layers saved at |
| 141 | +// the last shutdown. |
| 142 | +func ReadTrieJournal(db ethdb.KeyValueReader) []byte { |
| 143 | + data, _ := db.Get(trieJournalKey) |
| 144 | + return data |
| 145 | +} |
| 146 | + |
| 147 | +// WriteTrieJournal stores the serialized in-memory trie nodes of layers to save at |
| 148 | +// shutdown. |
| 149 | +func WriteTrieJournal(db ethdb.KeyValueWriter, journal []byte) { |
| 150 | + if err := db.Put(trieJournalKey, journal); err != nil { |
| 151 | + log.Crit("Failed to store tries journal", "err", err) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// DeleteTrieJournal deletes the serialized in-memory trie nodes of layers saved at |
| 156 | +// the last shutdown. |
| 157 | +func DeleteTrieJournal(db ethdb.KeyValueWriter) { |
| 158 | + if err := db.Delete(trieJournalKey); err != nil { |
| 159 | + log.Crit("Failed to remove tries journal", "err", err) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// ReadStateHistoryMeta retrieves the metadata corresponding to the specified |
| 164 | +// state history. Compute the position of state history in freezer by minus |
| 165 | +// one since the id of first state history starts from one(zero for initial |
| 166 | +// state). |
| 167 | +func ReadStateHistoryMeta(db ethdb.AncientReaderOp, id uint64) []byte { |
| 168 | + blob, err := db.Ancient(stateHistoryMeta, id-1) |
| 169 | + if err != nil { |
| 170 | + return nil |
| 171 | + } |
| 172 | + return blob |
| 173 | +} |
| 174 | + |
| 175 | +// ReadStateHistoryMetaList retrieves a batch of meta objects with the specified |
| 176 | +// start position and count. Compute the position of state history in freezer by |
| 177 | +// minus one since the id of first state history starts from one(zero for initial |
| 178 | +// state). |
| 179 | +func ReadStateHistoryMetaList(db ethdb.AncientReaderOp, start uint64, count uint64) ([][]byte, error) { |
| 180 | + return db.AncientRange(stateHistoryMeta, start-1, count, 0) |
| 181 | +} |
| 182 | + |
| 183 | +// ReadStateAccountIndex retrieves the state root corresponding to the specified |
| 184 | +// state history. Compute the position of state history in freezer by minus one |
| 185 | +// since the id of first state history starts from one(zero for initial state). |
| 186 | +func ReadStateAccountIndex(db ethdb.AncientReaderOp, id uint64) []byte { |
| 187 | + blob, err := db.Ancient(stateHistoryAccountIndex, id-1) |
| 188 | + if err != nil { |
| 189 | + return nil |
| 190 | + } |
| 191 | + return blob |
| 192 | +} |
| 193 | + |
| 194 | +// ReadStateStorageIndex retrieves the state root corresponding to the specified |
| 195 | +// state history. Compute the position of state history in freezer by minus one |
| 196 | +// since the id of first state history starts from one(zero for initial state). |
| 197 | +func ReadStateStorageIndex(db ethdb.AncientReaderOp, id uint64) []byte { |
| 198 | + blob, err := db.Ancient(stateHistoryStorageIndex, id-1) |
| 199 | + if err != nil { |
| 200 | + return nil |
| 201 | + } |
| 202 | + return blob |
| 203 | +} |
| 204 | + |
| 205 | +// ReadStateAccountHistory retrieves the state root corresponding to the specified |
| 206 | +// state history. Compute the position of state history in freezer by minus one |
| 207 | +// since the id of first state history starts from one(zero for initial state). |
| 208 | +func ReadStateAccountHistory(db ethdb.AncientReaderOp, id uint64) []byte { |
| 209 | + blob, err := db.Ancient(stateHistoryAccountData, id-1) |
| 210 | + if err != nil { |
| 211 | + return nil |
| 212 | + } |
| 213 | + return blob |
| 214 | +} |
| 215 | + |
| 216 | +// ReadStateStorageHistory retrieves the state root corresponding to the specified |
| 217 | +// state history. Compute the position of state history in freezer by minus one |
| 218 | +// since the id of first state history starts from one(zero for initial state). |
| 219 | +func ReadStateStorageHistory(db ethdb.AncientReaderOp, id uint64) []byte { |
| 220 | + blob, err := db.Ancient(stateHistoryStorageData, id-1) |
| 221 | + if err != nil { |
| 222 | + return nil |
| 223 | + } |
| 224 | + return blob |
| 225 | +} |
| 226 | + |
| 227 | +// ReadStateHistory retrieves the state history from database with provided id. |
| 228 | +// Compute the position of state history in freezer by minus one since the id |
| 229 | +// of first state history starts from one(zero for initial state). |
| 230 | +func ReadStateHistory(db ethdb.AncientReaderOp, id uint64) ([]byte, []byte, []byte, []byte, []byte, error) { |
| 231 | + meta, err := db.Ancient(stateHistoryMeta, id-1) |
| 232 | + if err != nil { |
| 233 | + return nil, nil, nil, nil, nil, err |
| 234 | + } |
| 235 | + accountIndex, err := db.Ancient(stateHistoryAccountIndex, id-1) |
| 236 | + if err != nil { |
| 237 | + return nil, nil, nil, nil, nil, err |
| 238 | + } |
| 239 | + storageIndex, err := db.Ancient(stateHistoryStorageIndex, id-1) |
| 240 | + if err != nil { |
| 241 | + return nil, nil, nil, nil, nil, err |
| 242 | + } |
| 243 | + accountData, err := db.Ancient(stateHistoryAccountData, id-1) |
| 244 | + if err != nil { |
| 245 | + return nil, nil, nil, nil, nil, err |
| 246 | + } |
| 247 | + storageData, err := db.Ancient(stateHistoryStorageData, id-1) |
| 248 | + if err != nil { |
| 249 | + return nil, nil, nil, nil, nil, err |
| 250 | + } |
| 251 | + return meta, accountIndex, storageIndex, accountData, storageData, nil |
| 252 | +} |
| 253 | + |
| 254 | +// WriteStateHistory writes the provided state history to database. Compute the |
| 255 | +// position of state history in freezer by minus one since the id of first state |
| 256 | +// history starts from one(zero for initial state). |
| 257 | +func WriteStateHistory(db ethdb.AncientWriter, id uint64, meta []byte, accountIndex []byte, storageIndex []byte, accounts []byte, storages []byte) { |
| 258 | + db.ModifyAncients(func(op ethdb.AncientWriteOp) error { |
| 259 | + op.AppendRaw(stateHistoryMeta, id-1, meta) |
| 260 | + op.AppendRaw(stateHistoryAccountIndex, id-1, accountIndex) |
| 261 | + op.AppendRaw(stateHistoryStorageIndex, id-1, storageIndex) |
| 262 | + op.AppendRaw(stateHistoryAccountData, id-1, accounts) |
| 263 | + op.AppendRaw(stateHistoryStorageData, id-1, storages) |
| 264 | + return nil |
| 265 | + }) |
| 266 | +} |
0 commit comments