github.com/MaynardMiner/ethereumprogpow@v1.8.23/core/rawdb/accessors_chain.go (about) 1 // Copyright 2018 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 "bytes" 21 "encoding/binary" 22 "math/big" 23 24 "github.com/ethereumprogpow/ethereumprogpow/common" 25 "github.com/ethereumprogpow/ethereumprogpow/core/types" 26 "github.com/ethereumprogpow/ethereumprogpow/log" 27 "github.com/ethereumprogpow/ethereumprogpow/rlp" 28 ) 29 30 // ReadCanonicalHash retrieves the hash assigned to a canonical block number. 31 func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash { 32 data, _ := db.Get(headerHashKey(number)) 33 if len(data) == 0 { 34 return common.Hash{} 35 } 36 return common.BytesToHash(data) 37 } 38 39 // WriteCanonicalHash stores the hash assigned to a canonical block number. 40 func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) { 41 if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil { 42 log.Crit("Failed to store number to hash mapping", "err", err) 43 } 44 } 45 46 // DeleteCanonicalHash removes the number to hash canonical mapping. 47 func DeleteCanonicalHash(db DatabaseDeleter, number uint64) { 48 if err := db.Delete(headerHashKey(number)); err != nil { 49 log.Crit("Failed to delete number to hash mapping", "err", err) 50 } 51 } 52 53 // ReadHeaderNumber returns the header number assigned to a hash. 54 func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 { 55 data, _ := db.Get(headerNumberKey(hash)) 56 if len(data) != 8 { 57 return nil 58 } 59 number := binary.BigEndian.Uint64(data) 60 return &number 61 } 62 63 // ReadHeadHeaderHash retrieves the hash of the current canonical head header. 64 func ReadHeadHeaderHash(db DatabaseReader) common.Hash { 65 data, _ := db.Get(headHeaderKey) 66 if len(data) == 0 { 67 return common.Hash{} 68 } 69 return common.BytesToHash(data) 70 } 71 72 // WriteHeadHeaderHash stores the hash of the current canonical head header. 73 func WriteHeadHeaderHash(db DatabaseWriter, hash common.Hash) { 74 if err := db.Put(headHeaderKey, hash.Bytes()); err != nil { 75 log.Crit("Failed to store last header's hash", "err", err) 76 } 77 } 78 79 // ReadHeadBlockHash retrieves the hash of the current canonical head block. 80 func ReadHeadBlockHash(db DatabaseReader) common.Hash { 81 data, _ := db.Get(headBlockKey) 82 if len(data) == 0 { 83 return common.Hash{} 84 } 85 return common.BytesToHash(data) 86 } 87 88 // WriteHeadBlockHash stores the head block's hash. 89 func WriteHeadBlockHash(db DatabaseWriter, hash common.Hash) { 90 if err := db.Put(headBlockKey, hash.Bytes()); err != nil { 91 log.Crit("Failed to store last block's hash", "err", err) 92 } 93 } 94 95 // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block. 96 func ReadHeadFastBlockHash(db DatabaseReader) common.Hash { 97 data, _ := db.Get(headFastBlockKey) 98 if len(data) == 0 { 99 return common.Hash{} 100 } 101 return common.BytesToHash(data) 102 } 103 104 // WriteHeadFastBlockHash stores the hash of the current fast-sync head block. 105 func WriteHeadFastBlockHash(db DatabaseWriter, hash common.Hash) { 106 if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil { 107 log.Crit("Failed to store last fast block's hash", "err", err) 108 } 109 } 110 111 // ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow 112 // reporting correct numbers across restarts. 113 func ReadFastTrieProgress(db DatabaseReader) uint64 { 114 data, _ := db.Get(fastTrieProgressKey) 115 if len(data) == 0 { 116 return 0 117 } 118 return new(big.Int).SetBytes(data).Uint64() 119 } 120 121 // WriteFastTrieProgress stores the fast sync trie process counter to support 122 // retrieving it across restarts. 123 func WriteFastTrieProgress(db DatabaseWriter, count uint64) { 124 if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil { 125 log.Crit("Failed to store fast sync trie progress", "err", err) 126 } 127 } 128 129 // ReadHeaderRLP retrieves a block header in its raw RLP database encoding. 130 func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { 131 data, _ := db.Get(headerKey(number, hash)) 132 return data 133 } 134 135 // HasHeader verifies the existence of a block header corresponding to the hash. 136 func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool { 137 if has, err := db.Has(headerKey(number, hash)); !has || err != nil { 138 return false 139 } 140 return true 141 } 142 143 // ReadHeader retrieves the block header corresponding to the hash. 144 func ReadHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header { 145 data := ReadHeaderRLP(db, hash, number) 146 if len(data) == 0 { 147 return nil 148 } 149 header := new(types.Header) 150 if err := rlp.Decode(bytes.NewReader(data), header); err != nil { 151 log.Error("Invalid block header RLP", "hash", hash, "err", err) 152 return nil 153 } 154 return header 155 } 156 157 // WriteHeader stores a block header into the database and also stores the hash- 158 // to-number mapping. 159 func WriteHeader(db DatabaseWriter, header *types.Header) { 160 // Write the hash -> number mapping 161 var ( 162 hash = header.Hash() 163 number = header.Number.Uint64() 164 encoded = encodeBlockNumber(number) 165 ) 166 key := headerNumberKey(hash) 167 if err := db.Put(key, encoded); err != nil { 168 log.Crit("Failed to store hash to number mapping", "err", err) 169 } 170 // Write the encoded header 171 data, err := rlp.EncodeToBytes(header) 172 if err != nil { 173 log.Crit("Failed to RLP encode header", "err", err) 174 } 175 key = headerKey(number, hash) 176 if err := db.Put(key, data); err != nil { 177 log.Crit("Failed to store header", "err", err) 178 } 179 } 180 181 // DeleteHeader removes all block header data associated with a hash. 182 func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) { 183 if err := db.Delete(headerKey(number, hash)); err != nil { 184 log.Crit("Failed to delete header", "err", err) 185 } 186 if err := db.Delete(headerNumberKey(hash)); err != nil { 187 log.Crit("Failed to delete hash to number mapping", "err", err) 188 } 189 } 190 191 // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. 192 func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { 193 data, _ := db.Get(blockBodyKey(number, hash)) 194 return data 195 } 196 197 // WriteBodyRLP stores an RLP encoded block body into the database. 198 func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) { 199 if err := db.Put(blockBodyKey(number, hash), rlp); err != nil { 200 log.Crit("Failed to store block body", "err", err) 201 } 202 } 203 204 // HasBody verifies the existence of a block body corresponding to the hash. 205 func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool { 206 if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil { 207 return false 208 } 209 return true 210 } 211 212 // ReadBody retrieves the block body corresponding to the hash. 213 func ReadBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body { 214 data := ReadBodyRLP(db, hash, number) 215 if len(data) == 0 { 216 return nil 217 } 218 body := new(types.Body) 219 if err := rlp.Decode(bytes.NewReader(data), body); err != nil { 220 log.Error("Invalid block body RLP", "hash", hash, "err", err) 221 return nil 222 } 223 return body 224 } 225 226 // WriteBody storea a block body into the database. 227 func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.Body) { 228 data, err := rlp.EncodeToBytes(body) 229 if err != nil { 230 log.Crit("Failed to RLP encode body", "err", err) 231 } 232 WriteBodyRLP(db, hash, number, data) 233 } 234 235 // DeleteBody removes all block body data associated with a hash. 236 func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) { 237 if err := db.Delete(blockBodyKey(number, hash)); err != nil { 238 log.Crit("Failed to delete block body", "err", err) 239 } 240 } 241 242 // ReadTd retrieves a block's total difficulty corresponding to the hash. 243 func ReadTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int { 244 data, _ := db.Get(headerTDKey(number, hash)) 245 if len(data) == 0 { 246 return nil 247 } 248 td := new(big.Int) 249 if err := rlp.Decode(bytes.NewReader(data), td); err != nil { 250 log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err) 251 return nil 252 } 253 return td 254 } 255 256 // WriteTd stores the total difficulty of a block into the database. 257 func WriteTd(db DatabaseWriter, hash common.Hash, number uint64, td *big.Int) { 258 data, err := rlp.EncodeToBytes(td) 259 if err != nil { 260 log.Crit("Failed to RLP encode block total difficulty", "err", err) 261 } 262 if err := db.Put(headerTDKey(number, hash), data); err != nil { 263 log.Crit("Failed to store block total difficulty", "err", err) 264 } 265 } 266 267 // DeleteTd removes all block total difficulty data associated with a hash. 268 func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) { 269 if err := db.Delete(headerTDKey(number, hash)); err != nil { 270 log.Crit("Failed to delete block total difficulty", "err", err) 271 } 272 } 273 274 // HasReceipts verifies the existence of all the transaction receipts belonging 275 // to a block. 276 func HasReceipts(db DatabaseReader, hash common.Hash, number uint64) bool { 277 if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil { 278 return false 279 } 280 return true 281 } 282 283 // ReadReceipts retrieves all the transaction receipts belonging to a block. 284 func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts { 285 // Retrieve the flattened receipt slice 286 data, _ := db.Get(blockReceiptsKey(number, hash)) 287 if len(data) == 0 { 288 return nil 289 } 290 // Convert the receipts from their storage form to their internal representation 291 storageReceipts := []*types.ReceiptForStorage{} 292 if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { 293 log.Error("Invalid receipt array RLP", "hash", hash, "err", err) 294 return nil 295 } 296 receipts := make(types.Receipts, len(storageReceipts)) 297 for i, receipt := range storageReceipts { 298 receipts[i] = (*types.Receipt)(receipt) 299 } 300 return receipts 301 } 302 303 // WriteReceipts stores all the transaction receipts belonging to a block. 304 func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts types.Receipts) { 305 // Convert the receipts into their storage form and serialize them 306 storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) 307 for i, receipt := range receipts { 308 storageReceipts[i] = (*types.ReceiptForStorage)(receipt) 309 } 310 bytes, err := rlp.EncodeToBytes(storageReceipts) 311 if err != nil { 312 log.Crit("Failed to encode block receipts", "err", err) 313 } 314 // Store the flattened receipt slice 315 if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil { 316 log.Crit("Failed to store block receipts", "err", err) 317 } 318 } 319 320 // DeleteReceipts removes all receipt data associated with a block hash. 321 func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) { 322 if err := db.Delete(blockReceiptsKey(number, hash)); err != nil { 323 log.Crit("Failed to delete block receipts", "err", err) 324 } 325 } 326 327 // ReadBlock retrieves an entire block corresponding to the hash, assembling it 328 // back from the stored header and body. If either the header or body could not 329 // be retrieved nil is returned. 330 // 331 // Note, due to concurrent download of header and block body the header and thus 332 // canonical hash can be stored in the database but the body data not (yet). 333 func ReadBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block { 334 header := ReadHeader(db, hash, number) 335 if header == nil { 336 return nil 337 } 338 body := ReadBody(db, hash, number) 339 if body == nil { 340 return nil 341 } 342 return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles) 343 } 344 345 // WriteBlock serializes a block into the database, header and body separately. 346 func WriteBlock(db DatabaseWriter, block *types.Block) { 347 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 348 WriteHeader(db, block.Header()) 349 } 350 351 // DeleteBlock removes all block data associated with a hash. 352 func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) { 353 DeleteReceipts(db, hash, number) 354 DeleteHeader(db, hash, number) 355 DeleteBody(db, hash, number) 356 DeleteTd(db, hash, number) 357 } 358 359 // FindCommonAncestor returns the last common ancestor of two block headers 360 func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header { 361 for bn := b.Number.Uint64(); a.Number.Uint64() > bn; { 362 a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) 363 if a == nil { 364 return nil 365 } 366 } 367 for an := a.Number.Uint64(); an < b.Number.Uint64(); { 368 b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) 369 if b == nil { 370 return nil 371 } 372 } 373 for a.Hash() != b.Hash() { 374 a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) 375 if a == nil { 376 return nil 377 } 378 b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) 379 if b == nil { 380 return nil 381 } 382 } 383 return a 384 }