github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/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 "errors" 23 "fmt" 24 "math/big" 25 "sort" 26 27 "github.com/ethw3/go-ethereuma/common" 28 "github.com/ethw3/go-ethereuma/core/types" 29 "github.com/ethw3/go-ethereuma/crypto" 30 "github.com/ethw3/go-ethereuma/ethdb" 31 "github.com/ethw3/go-ethereuma/log" 32 "github.com/ethw3/go-ethereuma/params" 33 "github.com/ethw3/go-ethereuma/rlp" 34 ) 35 36 // ReadCanonicalHash retrieves the hash assigned to a canonical block number. 37 func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash { 38 var data []byte 39 db.ReadAncients(func(reader ethdb.AncientReaderOp) error { 40 data, _ = reader.Ancient(chainFreezerHashTable, number) 41 if len(data) == 0 { 42 // Get it by hash from leveldb 43 data, _ = db.Get(headerHashKey(number)) 44 } 45 return nil 46 }) 47 return common.BytesToHash(data) 48 } 49 50 // WriteCanonicalHash stores the hash assigned to a canonical block number. 51 func WriteCanonicalHash(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 52 if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil { 53 log.Crit("Failed to store number to hash mapping", "err", err) 54 } 55 } 56 57 // DeleteCanonicalHash removes the number to hash canonical mapping. 58 func DeleteCanonicalHash(db ethdb.KeyValueWriter, number uint64) { 59 if err := db.Delete(headerHashKey(number)); err != nil { 60 log.Crit("Failed to delete number to hash mapping", "err", err) 61 } 62 } 63 64 // ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights, 65 // both canonical and reorged forks included. 66 func ReadAllHashes(db ethdb.Iteratee, number uint64) []common.Hash { 67 prefix := headerKeyPrefix(number) 68 69 hashes := make([]common.Hash, 0, 1) 70 it := db.NewIterator(prefix, nil) 71 defer it.Release() 72 73 for it.Next() { 74 if key := it.Key(); len(key) == len(prefix)+32 { 75 hashes = append(hashes, common.BytesToHash(key[len(key)-32:])) 76 } 77 } 78 return hashes 79 } 80 81 type NumberHash struct { 82 Number uint64 83 Hash common.Hash 84 } 85 86 // ReadAllHashesInRange retrieves all the hashes assigned to blocks at certain 87 // heights, both canonical and reorged forks included. 88 // This method considers both limits to be _inclusive_. 89 func ReadAllHashesInRange(db ethdb.Iteratee, first, last uint64) []*NumberHash { 90 var ( 91 start = encodeBlockNumber(first) 92 keyLength = len(headerPrefix) + 8 + 32 93 hashes = make([]*NumberHash, 0, 1+last-first) 94 it = db.NewIterator(headerPrefix, start) 95 ) 96 defer it.Release() 97 for it.Next() { 98 key := it.Key() 99 if len(key) != keyLength { 100 continue 101 } 102 num := binary.BigEndian.Uint64(key[len(headerPrefix) : len(headerPrefix)+8]) 103 if num > last { 104 break 105 } 106 hash := common.BytesToHash(key[len(key)-32:]) 107 hashes = append(hashes, &NumberHash{num, hash}) 108 } 109 return hashes 110 } 111 112 // ReadAllCanonicalHashes retrieves all canonical number and hash mappings at the 113 // certain chain range. If the accumulated entries reaches the given threshold, 114 // abort the iteration and return the semi-finish result. 115 func ReadAllCanonicalHashes(db ethdb.Iteratee, from uint64, to uint64, limit int) ([]uint64, []common.Hash) { 116 // Short circuit if the limit is 0. 117 if limit == 0 { 118 return nil, nil 119 } 120 var ( 121 numbers []uint64 122 hashes []common.Hash 123 ) 124 // Construct the key prefix of start point. 125 start, end := headerHashKey(from), headerHashKey(to) 126 it := db.NewIterator(nil, start) 127 defer it.Release() 128 129 for it.Next() { 130 if bytes.Compare(it.Key(), end) >= 0 { 131 break 132 } 133 if key := it.Key(); len(key) == len(headerPrefix)+8+1 && bytes.Equal(key[len(key)-1:], headerHashSuffix) { 134 numbers = append(numbers, binary.BigEndian.Uint64(key[len(headerPrefix):len(headerPrefix)+8])) 135 hashes = append(hashes, common.BytesToHash(it.Value())) 136 // If the accumulated entries reaches the limit threshold, return. 137 if len(numbers) >= limit { 138 break 139 } 140 } 141 } 142 return numbers, hashes 143 } 144 145 // ReadHeaderNumber returns the header number assigned to a hash. 146 func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) *uint64 { 147 data, _ := db.Get(headerNumberKey(hash)) 148 if len(data) != 8 { 149 return nil 150 } 151 number := binary.BigEndian.Uint64(data) 152 return &number 153 } 154 155 // WriteHeaderNumber stores the hash->number mapping. 156 func WriteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 157 key := headerNumberKey(hash) 158 enc := encodeBlockNumber(number) 159 if err := db.Put(key, enc); err != nil { 160 log.Crit("Failed to store hash to number mapping", "err", err) 161 } 162 } 163 164 // DeleteHeaderNumber removes hash->number mapping. 165 func DeleteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash) { 166 if err := db.Delete(headerNumberKey(hash)); err != nil { 167 log.Crit("Failed to delete hash to number mapping", "err", err) 168 } 169 } 170 171 // ReadHeadHeaderHash retrieves the hash of the current canonical head header. 172 func ReadHeadHeaderHash(db ethdb.KeyValueReader) common.Hash { 173 data, _ := db.Get(headHeaderKey) 174 if len(data) == 0 { 175 return common.Hash{} 176 } 177 return common.BytesToHash(data) 178 } 179 180 // WriteHeadHeaderHash stores the hash of the current canonical head header. 181 func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) { 182 if err := db.Put(headHeaderKey, hash.Bytes()); err != nil { 183 log.Crit("Failed to store last header's hash", "err", err) 184 } 185 } 186 187 // ReadHeadBlockHash retrieves the hash of the current canonical head block. 188 func ReadHeadBlockHash(db ethdb.KeyValueReader) common.Hash { 189 data, _ := db.Get(headBlockKey) 190 if len(data) == 0 { 191 return common.Hash{} 192 } 193 return common.BytesToHash(data) 194 } 195 196 // WriteHeadBlockHash stores the head block's hash. 197 func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) { 198 if err := db.Put(headBlockKey, hash.Bytes()); err != nil { 199 log.Crit("Failed to store last block's hash", "err", err) 200 } 201 } 202 203 // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block. 204 func ReadHeadFastBlockHash(db ethdb.KeyValueReader) common.Hash { 205 data, _ := db.Get(headFastBlockKey) 206 if len(data) == 0 { 207 return common.Hash{} 208 } 209 return common.BytesToHash(data) 210 } 211 212 // WriteHeadFastBlockHash stores the hash of the current fast-sync head block. 213 func WriteHeadFastBlockHash(db ethdb.KeyValueWriter, hash common.Hash) { 214 if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil { 215 log.Crit("Failed to store last fast block's hash", "err", err) 216 } 217 } 218 219 // ReadFinalizedBlockHash retrieves the hash of the finalized block. 220 func ReadFinalizedBlockHash(db ethdb.KeyValueReader) common.Hash { 221 data, _ := db.Get(headFinalizedBlockKey) 222 if len(data) == 0 { 223 return common.Hash{} 224 } 225 return common.BytesToHash(data) 226 } 227 228 // WriteFinalizedBlockHash stores the hash of the finalized block. 229 func WriteFinalizedBlockHash(db ethdb.KeyValueWriter, hash common.Hash) { 230 if err := db.Put(headFinalizedBlockKey, hash.Bytes()); err != nil { 231 log.Crit("Failed to store last finalized block's hash", "err", err) 232 } 233 } 234 235 // ReadLastPivotNumber retrieves the number of the last pivot block. If the node 236 // full synced, the last pivot will always be nil. 237 func ReadLastPivotNumber(db ethdb.KeyValueReader) *uint64 { 238 data, _ := db.Get(lastPivotKey) 239 if len(data) == 0 { 240 return nil 241 } 242 var pivot uint64 243 if err := rlp.DecodeBytes(data, &pivot); err != nil { 244 log.Error("Invalid pivot block number in database", "err", err) 245 return nil 246 } 247 return &pivot 248 } 249 250 // WriteLastPivotNumber stores the number of the last pivot block. 251 func WriteLastPivotNumber(db ethdb.KeyValueWriter, pivot uint64) { 252 enc, err := rlp.EncodeToBytes(pivot) 253 if err != nil { 254 log.Crit("Failed to encode pivot block number", "err", err) 255 } 256 if err := db.Put(lastPivotKey, enc); err != nil { 257 log.Crit("Failed to store pivot block number", "err", err) 258 } 259 } 260 261 // ReadTxIndexTail retrieves the number of oldest indexed block 262 // whose transaction indices has been indexed. If the corresponding entry 263 // is non-existent in database it means the indexing has been finished. 264 func ReadTxIndexTail(db ethdb.KeyValueReader) *uint64 { 265 data, _ := db.Get(txIndexTailKey) 266 if len(data) != 8 { 267 return nil 268 } 269 number := binary.BigEndian.Uint64(data) 270 return &number 271 } 272 273 // WriteTxIndexTail stores the number of oldest indexed block 274 // into database. 275 func WriteTxIndexTail(db ethdb.KeyValueWriter, number uint64) { 276 if err := db.Put(txIndexTailKey, encodeBlockNumber(number)); err != nil { 277 log.Crit("Failed to store the transaction index tail", "err", err) 278 } 279 } 280 281 // ReadFastTxLookupLimit retrieves the tx lookup limit used in fast sync. 282 func ReadFastTxLookupLimit(db ethdb.KeyValueReader) *uint64 { 283 data, _ := db.Get(fastTxLookupLimitKey) 284 if len(data) != 8 { 285 return nil 286 } 287 number := binary.BigEndian.Uint64(data) 288 return &number 289 } 290 291 // WriteFastTxLookupLimit stores the txlookup limit used in fast sync into database. 292 func WriteFastTxLookupLimit(db ethdb.KeyValueWriter, number uint64) { 293 if err := db.Put(fastTxLookupLimitKey, encodeBlockNumber(number)); err != nil { 294 log.Crit("Failed to store transaction lookup limit for fast sync", "err", err) 295 } 296 } 297 298 // ReadHeaderRange returns the rlp-encoded headers, starting at 'number', and going 299 // backwards towards genesis. This method assumes that the caller already has 300 // placed a cap on count, to prevent DoS issues. 301 // Since this method operates in head-towards-genesis mode, it will return an empty 302 // slice in case the head ('number') is missing. Hence, the caller must ensure that 303 // the head ('number') argument is actually an existing header. 304 // 305 // N.B: Since the input is a number, as opposed to a hash, it's implicit that 306 // this method only operates on canon headers. 307 func ReadHeaderRange(db ethdb.Reader, number uint64, count uint64) []rlp.RawValue { 308 var rlpHeaders []rlp.RawValue 309 if count == 0 { 310 return rlpHeaders 311 } 312 i := number 313 if count-1 > number { 314 // It's ok to request block 0, 1 item 315 count = number + 1 316 } 317 limit, _ := db.Ancients() 318 // First read live blocks 319 if i >= limit { 320 // If we need to read live blocks, we need to figure out the hash first 321 hash := ReadCanonicalHash(db, number) 322 for ; i >= limit && count > 0; i-- { 323 if data, _ := db.Get(headerKey(i, hash)); len(data) > 0 { 324 rlpHeaders = append(rlpHeaders, data) 325 // Get the parent hash for next query 326 hash = types.HeaderParentHashFromRLP(data) 327 } else { 328 break // Maybe got moved to ancients 329 } 330 count-- 331 } 332 } 333 if count == 0 { 334 return rlpHeaders 335 } 336 // read remaining from ancients 337 max := count * 700 338 data, err := db.AncientRange(chainFreezerHeaderTable, i+1-count, count, max) 339 if err == nil && uint64(len(data)) == count { 340 // the data is on the order [h, h+1, .., n] -- reordering needed 341 for i := range data { 342 rlpHeaders = append(rlpHeaders, data[len(data)-1-i]) 343 } 344 } 345 return rlpHeaders 346 } 347 348 // ReadHeaderRLP retrieves a block header in its raw RLP database encoding. 349 func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { 350 var data []byte 351 db.ReadAncients(func(reader ethdb.AncientReaderOp) error { 352 // First try to look up the data in ancient database. Extra hash 353 // comparison is necessary since ancient database only maintains 354 // the canonical data. 355 data, _ = reader.Ancient(chainFreezerHeaderTable, number) 356 if len(data) > 0 && crypto.Keccak256Hash(data) == hash { 357 return nil 358 } 359 // If not, try reading from leveldb 360 data, _ = db.Get(headerKey(number, hash)) 361 return nil 362 }) 363 return data 364 } 365 366 // HasHeader verifies the existence of a block header corresponding to the hash. 367 func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool { 368 if isCanon(db, number, hash) { 369 return true 370 } 371 if has, err := db.Has(headerKey(number, hash)); !has || err != nil { 372 return false 373 } 374 return true 375 } 376 377 // ReadHeader retrieves the block header corresponding to the hash. 378 func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header { 379 data := ReadHeaderRLP(db, hash, number) 380 if len(data) == 0 { 381 return nil 382 } 383 header := new(types.Header) 384 if err := rlp.Decode(bytes.NewReader(data), header); err != nil { 385 log.Error("Invalid block header RLP", "hash", hash, "err", err) 386 return nil 387 } 388 return header 389 } 390 391 // WriteHeader stores a block header into the database and also stores the hash- 392 // to-number mapping. 393 func WriteHeader(db ethdb.KeyValueWriter, header *types.Header) { 394 var ( 395 hash = header.Hash() 396 number = header.Number.Uint64() 397 ) 398 // Write the hash -> number mapping 399 WriteHeaderNumber(db, hash, number) 400 401 // Write the encoded header 402 data, err := rlp.EncodeToBytes(header) 403 if err != nil { 404 log.Crit("Failed to RLP encode header", "err", err) 405 } 406 key := headerKey(number, hash) 407 if err := db.Put(key, data); err != nil { 408 log.Crit("Failed to store header", "err", err) 409 } 410 } 411 412 // DeleteHeader removes all block header data associated with a hash. 413 func DeleteHeader(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 414 deleteHeaderWithoutNumber(db, hash, number) 415 if err := db.Delete(headerNumberKey(hash)); err != nil { 416 log.Crit("Failed to delete hash to number mapping", "err", err) 417 } 418 } 419 420 // deleteHeaderWithoutNumber removes only the block header but does not remove 421 // the hash to number mapping. 422 func deleteHeaderWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 423 if err := db.Delete(headerKey(number, hash)); err != nil { 424 log.Crit("Failed to delete header", "err", err) 425 } 426 } 427 428 // isCanon is an internal utility method, to check whether the given number/hash 429 // is part of the ancient (canon) set. 430 func isCanon(reader ethdb.AncientReaderOp, number uint64, hash common.Hash) bool { 431 h, err := reader.Ancient(chainFreezerHashTable, number) 432 if err != nil { 433 return false 434 } 435 return bytes.Equal(h, hash[:]) 436 } 437 438 // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. 439 func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { 440 // First try to look up the data in ancient database. Extra hash 441 // comparison is necessary since ancient database only maintains 442 // the canonical data. 443 var data []byte 444 db.ReadAncients(func(reader ethdb.AncientReaderOp) error { 445 // Check if the data is in ancients 446 if isCanon(reader, number, hash) { 447 data, _ = reader.Ancient(chainFreezerBodiesTable, number) 448 return nil 449 } 450 // If not, try reading from leveldb 451 data, _ = db.Get(blockBodyKey(number, hash)) 452 return nil 453 }) 454 return data 455 } 456 457 // ReadCanonicalBodyRLP retrieves the block body (transactions and uncles) for the canonical 458 // block at number, in RLP encoding. 459 func ReadCanonicalBodyRLP(db ethdb.Reader, number uint64) rlp.RawValue { 460 var data []byte 461 db.ReadAncients(func(reader ethdb.AncientReaderOp) error { 462 data, _ = reader.Ancient(chainFreezerBodiesTable, number) 463 if len(data) > 0 { 464 return nil 465 } 466 // Block is not in ancients, read from leveldb by hash and number. 467 // Note: ReadCanonicalHash cannot be used here because it also 468 // calls ReadAncients internally. 469 hash, _ := db.Get(headerHashKey(number)) 470 data, _ = db.Get(blockBodyKey(number, common.BytesToHash(hash))) 471 return nil 472 }) 473 return data 474 } 475 476 // WriteBodyRLP stores an RLP encoded block body into the database. 477 func WriteBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rlp rlp.RawValue) { 478 if err := db.Put(blockBodyKey(number, hash), rlp); err != nil { 479 log.Crit("Failed to store block body", "err", err) 480 } 481 } 482 483 // HasBody verifies the existence of a block body corresponding to the hash. 484 func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool { 485 if isCanon(db, number, hash) { 486 return true 487 } 488 if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil { 489 return false 490 } 491 return true 492 } 493 494 // ReadBody retrieves the block body corresponding to the hash. 495 func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body { 496 data := ReadBodyRLP(db, hash, number) 497 if len(data) == 0 { 498 return nil 499 } 500 body := new(types.Body) 501 if err := rlp.Decode(bytes.NewReader(data), body); err != nil { 502 log.Error("Invalid block body RLP", "hash", hash, "err", err) 503 return nil 504 } 505 return body 506 } 507 508 // WriteBody stores a block body into the database. 509 func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *types.Body) { 510 data, err := rlp.EncodeToBytes(body) 511 if err != nil { 512 log.Crit("Failed to RLP encode body", "err", err) 513 } 514 WriteBodyRLP(db, hash, number, data) 515 } 516 517 // DeleteBody removes all block body data associated with a hash. 518 func DeleteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 519 if err := db.Delete(blockBodyKey(number, hash)); err != nil { 520 log.Crit("Failed to delete block body", "err", err) 521 } 522 } 523 524 // ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding. 525 func ReadTdRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { 526 var data []byte 527 db.ReadAncients(func(reader ethdb.AncientReaderOp) error { 528 // Check if the data is in ancients 529 if isCanon(reader, number, hash) { 530 data, _ = reader.Ancient(chainFreezerDifficultyTable, number) 531 return nil 532 } 533 // If not, try reading from leveldb 534 data, _ = db.Get(headerTDKey(number, hash)) 535 return nil 536 }) 537 return data 538 } 539 540 // ReadTd retrieves a block's total difficulty corresponding to the hash. 541 func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int { 542 data := ReadTdRLP(db, hash, number) 543 if len(data) == 0 { 544 return nil 545 } 546 td := new(big.Int) 547 if err := rlp.Decode(bytes.NewReader(data), td); err != nil { 548 log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err) 549 return nil 550 } 551 return td 552 } 553 554 // WriteTd stores the total difficulty of a block into the database. 555 func WriteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64, td *big.Int) { 556 data, err := rlp.EncodeToBytes(td) 557 if err != nil { 558 log.Crit("Failed to RLP encode block total difficulty", "err", err) 559 } 560 if err := db.Put(headerTDKey(number, hash), data); err != nil { 561 log.Crit("Failed to store block total difficulty", "err", err) 562 } 563 } 564 565 // DeleteTd removes all block total difficulty data associated with a hash. 566 func DeleteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 567 if err := db.Delete(headerTDKey(number, hash)); err != nil { 568 log.Crit("Failed to delete block total difficulty", "err", err) 569 } 570 } 571 572 // HasReceipts verifies the existence of all the transaction receipts belonging 573 // to a block. 574 func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool { 575 if isCanon(db, number, hash) { 576 return true 577 } 578 if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil { 579 return false 580 } 581 return true 582 } 583 584 // ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding. 585 func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue { 586 var data []byte 587 db.ReadAncients(func(reader ethdb.AncientReaderOp) error { 588 // Check if the data is in ancients 589 if isCanon(reader, number, hash) { 590 data, _ = reader.Ancient(chainFreezerReceiptTable, number) 591 return nil 592 } 593 // If not, try reading from leveldb 594 data, _ = db.Get(blockReceiptsKey(number, hash)) 595 return nil 596 }) 597 return data 598 } 599 600 // ReadRawReceipts retrieves all the transaction receipts belonging to a block. 601 // The receipt metadata fields are not guaranteed to be populated, so they 602 // should not be used. Use ReadReceipts instead if the metadata is needed. 603 func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts { 604 // Retrieve the flattened receipt slice 605 data := ReadReceiptsRLP(db, hash, number) 606 if len(data) == 0 { 607 return nil 608 } 609 // Convert the receipts from their storage form to their internal representation 610 storageReceipts := []*types.ReceiptForStorage{} 611 if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { 612 log.Error("Invalid receipt array RLP", "hash", hash, "err", err) 613 return nil 614 } 615 receipts := make(types.Receipts, len(storageReceipts)) 616 for i, storageReceipt := range storageReceipts { 617 receipts[i] = (*types.Receipt)(storageReceipt) 618 } 619 return receipts 620 } 621 622 // ReadReceipts retrieves all the transaction receipts belonging to a block, including 623 // its corresponding metadata fields. If it is unable to populate these metadata 624 // fields then nil is returned. 625 // 626 // The current implementation populates these metadata fields by reading the receipts' 627 // corresponding block body, so if the block body is not found it will return nil even 628 // if the receipt itself is stored. 629 func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts { 630 // We're deriving many fields from the block body, retrieve beside the receipt 631 receipts := ReadRawReceipts(db, hash, number) 632 if receipts == nil { 633 return nil 634 } 635 body := ReadBody(db, hash, number) 636 if body == nil { 637 log.Error("Missing body but have receipt", "hash", hash, "number", number) 638 return nil 639 } 640 if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil { 641 log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err) 642 return nil 643 } 644 return receipts 645 } 646 647 // WriteReceipts stores all the transaction receipts belonging to a block. 648 func WriteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64, receipts types.Receipts) { 649 // Convert the receipts into their storage form and serialize them 650 storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) 651 for i, receipt := range receipts { 652 storageReceipts[i] = (*types.ReceiptForStorage)(receipt) 653 } 654 bytes, err := rlp.EncodeToBytes(storageReceipts) 655 if err != nil { 656 log.Crit("Failed to encode block receipts", "err", err) 657 } 658 // Store the flattened receipt slice 659 if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil { 660 log.Crit("Failed to store block receipts", "err", err) 661 } 662 } 663 664 // DeleteReceipts removes all receipt data associated with a block hash. 665 func DeleteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 666 if err := db.Delete(blockReceiptsKey(number, hash)); err != nil { 667 log.Crit("Failed to delete block receipts", "err", err) 668 } 669 } 670 671 // storedReceiptRLP is the storage encoding of a receipt. 672 // Re-definition in core/types/receipt.go. 673 type storedReceiptRLP struct { 674 PostStateOrStatus []byte 675 CumulativeGasUsed uint64 676 Logs []*types.LogForStorage 677 } 678 679 // ReceiptLogs is a barebone version of ReceiptForStorage which only keeps 680 // the list of logs. When decoding a stored receipt into this object we 681 // avoid creating the bloom filter. 682 type receiptLogs struct { 683 Logs []*types.Log 684 } 685 686 // DecodeRLP implements rlp.Decoder. 687 func (r *receiptLogs) DecodeRLP(s *rlp.Stream) error { 688 var stored storedReceiptRLP 689 if err := s.Decode(&stored); err != nil { 690 return err 691 } 692 r.Logs = make([]*types.Log, len(stored.Logs)) 693 for i, log := range stored.Logs { 694 r.Logs[i] = (*types.Log)(log) 695 } 696 return nil 697 } 698 699 // DeriveLogFields fills the logs in receiptLogs with information such as block number, txhash, etc. 700 func deriveLogFields(receipts []*receiptLogs, hash common.Hash, number uint64, txs types.Transactions) error { 701 logIndex := uint(0) 702 if len(txs) != len(receipts) { 703 return errors.New("transaction and receipt count mismatch") 704 } 705 for i := 0; i < len(receipts); i++ { 706 txHash := txs[i].Hash() 707 // The derived log fields can simply be set from the block and transaction 708 for j := 0; j < len(receipts[i].Logs); j++ { 709 receipts[i].Logs[j].BlockNumber = number 710 receipts[i].Logs[j].BlockHash = hash 711 receipts[i].Logs[j].TxHash = txHash 712 receipts[i].Logs[j].TxIndex = uint(i) 713 receipts[i].Logs[j].Index = logIndex 714 logIndex++ 715 } 716 } 717 return nil 718 } 719 720 // ReadLogs retrieves the logs for all transactions in a block. The log fields 721 // are populated with metadata. In case the receipts or the block body 722 // are not found, a nil is returned. 723 func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) [][]*types.Log { 724 // Retrieve the flattened receipt slice 725 data := ReadReceiptsRLP(db, hash, number) 726 if len(data) == 0 { 727 return nil 728 } 729 receipts := []*receiptLogs{} 730 if err := rlp.DecodeBytes(data, &receipts); err != nil { 731 // Receipts might be in the legacy format, try decoding that. 732 // TODO: to be removed after users migrated 733 if logs := readLegacyLogs(db, hash, number, config); logs != nil { 734 return logs 735 } 736 log.Error("Invalid receipt array RLP", "hash", hash, "err", err) 737 return nil 738 } 739 740 body := ReadBody(db, hash, number) 741 if body == nil { 742 log.Error("Missing body but have receipt", "hash", hash, "number", number) 743 return nil 744 } 745 if err := deriveLogFields(receipts, hash, number, body.Transactions); err != nil { 746 log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err) 747 return nil 748 } 749 logs := make([][]*types.Log, len(receipts)) 750 for i, receipt := range receipts { 751 logs[i] = receipt.Logs 752 } 753 return logs 754 } 755 756 // readLegacyLogs is a temporary workaround for when trying to read logs 757 // from a block which has its receipt stored in the legacy format. It'll 758 // be removed after users have migrated their freezer databases. 759 func readLegacyLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) [][]*types.Log { 760 receipts := ReadReceipts(db, hash, number, config) 761 if receipts == nil { 762 return nil 763 } 764 logs := make([][]*types.Log, len(receipts)) 765 for i, receipt := range receipts { 766 logs[i] = receipt.Logs 767 } 768 return logs 769 } 770 771 // ReadBlock retrieves an entire block corresponding to the hash, assembling it 772 // back from the stored header and body. If either the header or body could not 773 // be retrieved nil is returned. 774 // 775 // Note, due to concurrent download of header and block body the header and thus 776 // canonical hash can be stored in the database but the body data not (yet). 777 func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block { 778 header := ReadHeader(db, hash, number) 779 if header == nil { 780 return nil 781 } 782 body := ReadBody(db, hash, number) 783 if body == nil { 784 return nil 785 } 786 return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles) 787 } 788 789 // WriteBlock serializes a block into the database, header and body separately. 790 func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) { 791 WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) 792 WriteHeader(db, block.Header()) 793 } 794 795 // WriteAncientBlocks writes entire block data into ancient store and returns the total written size. 796 func WriteAncientBlocks(db ethdb.AncientWriter, blocks []*types.Block, receipts []types.Receipts, td *big.Int) (int64, error) { 797 var ( 798 tdSum = new(big.Int).Set(td) 799 stReceipts []*types.ReceiptForStorage 800 ) 801 return db.ModifyAncients(func(op ethdb.AncientWriteOp) error { 802 for i, block := range blocks { 803 // Convert receipts to storage format and sum up total difficulty. 804 stReceipts = stReceipts[:0] 805 for _, receipt := range receipts[i] { 806 stReceipts = append(stReceipts, (*types.ReceiptForStorage)(receipt)) 807 } 808 header := block.Header() 809 if i > 0 { 810 tdSum.Add(tdSum, header.Difficulty) 811 } 812 if err := writeAncientBlock(op, block, header, stReceipts, tdSum); err != nil { 813 return err 814 } 815 } 816 return nil 817 }) 818 } 819 820 func writeAncientBlock(op ethdb.AncientWriteOp, block *types.Block, header *types.Header, receipts []*types.ReceiptForStorage, td *big.Int) error { 821 num := block.NumberU64() 822 if err := op.AppendRaw(chainFreezerHashTable, num, block.Hash().Bytes()); err != nil { 823 return fmt.Errorf("can't add block %d hash: %v", num, err) 824 } 825 if err := op.Append(chainFreezerHeaderTable, num, header); err != nil { 826 return fmt.Errorf("can't append block header %d: %v", num, err) 827 } 828 if err := op.Append(chainFreezerBodiesTable, num, block.Body()); err != nil { 829 return fmt.Errorf("can't append block body %d: %v", num, err) 830 } 831 if err := op.Append(chainFreezerReceiptTable, num, receipts); err != nil { 832 return fmt.Errorf("can't append block %d receipts: %v", num, err) 833 } 834 if err := op.Append(chainFreezerDifficultyTable, num, td); err != nil { 835 return fmt.Errorf("can't append block %d total difficulty: %v", num, err) 836 } 837 return nil 838 } 839 840 // DeleteBlock removes all block data associated with a hash. 841 func DeleteBlock(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 842 DeleteReceipts(db, hash, number) 843 DeleteHeader(db, hash, number) 844 DeleteBody(db, hash, number) 845 DeleteTd(db, hash, number) 846 } 847 848 // DeleteBlockWithoutNumber removes all block data associated with a hash, except 849 // the hash to number mapping. 850 func DeleteBlockWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) { 851 DeleteReceipts(db, hash, number) 852 deleteHeaderWithoutNumber(db, hash, number) 853 DeleteBody(db, hash, number) 854 DeleteTd(db, hash, number) 855 } 856 857 const badBlockToKeep = 10 858 859 type badBlock struct { 860 Header *types.Header 861 Body *types.Body 862 } 863 864 // badBlockList implements the sort interface to allow sorting a list of 865 // bad blocks by their number in the reverse order. 866 type badBlockList []*badBlock 867 868 func (s badBlockList) Len() int { return len(s) } 869 func (s badBlockList) Less(i, j int) bool { 870 return s[i].Header.Number.Uint64() < s[j].Header.Number.Uint64() 871 } 872 func (s badBlockList) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 873 874 // ReadBadBlock retrieves the bad block with the corresponding block hash. 875 func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block { 876 blob, err := db.Get(badBlockKey) 877 if err != nil { 878 return nil 879 } 880 var badBlocks badBlockList 881 if err := rlp.DecodeBytes(blob, &badBlocks); err != nil { 882 return nil 883 } 884 for _, bad := range badBlocks { 885 if bad.Header.Hash() == hash { 886 return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles) 887 } 888 } 889 return nil 890 } 891 892 // ReadAllBadBlocks retrieves all the bad blocks in the database. 893 // All returned blocks are sorted in reverse order by number. 894 func ReadAllBadBlocks(db ethdb.Reader) []*types.Block { 895 blob, err := db.Get(badBlockKey) 896 if err != nil { 897 return nil 898 } 899 var badBlocks badBlockList 900 if err := rlp.DecodeBytes(blob, &badBlocks); err != nil { 901 return nil 902 } 903 var blocks []*types.Block 904 for _, bad := range badBlocks { 905 blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles)) 906 } 907 return blocks 908 } 909 910 // WriteBadBlock serializes the bad block into the database. If the cumulated 911 // bad blocks exceeds the limitation, the oldest will be dropped. 912 func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) { 913 blob, err := db.Get(badBlockKey) 914 if err != nil { 915 log.Warn("Failed to load old bad blocks", "error", err) 916 } 917 var badBlocks badBlockList 918 if len(blob) > 0 { 919 if err := rlp.DecodeBytes(blob, &badBlocks); err != nil { 920 log.Crit("Failed to decode old bad blocks", "error", err) 921 } 922 } 923 for _, b := range badBlocks { 924 if b.Header.Number.Uint64() == block.NumberU64() && b.Header.Hash() == block.Hash() { 925 log.Info("Skip duplicated bad block", "number", block.NumberU64(), "hash", block.Hash()) 926 return 927 } 928 } 929 badBlocks = append(badBlocks, &badBlock{ 930 Header: block.Header(), 931 Body: block.Body(), 932 }) 933 sort.Sort(sort.Reverse(badBlocks)) 934 if len(badBlocks) > badBlockToKeep { 935 badBlocks = badBlocks[:badBlockToKeep] 936 } 937 data, err := rlp.EncodeToBytes(badBlocks) 938 if err != nil { 939 log.Crit("Failed to encode bad blocks", "err", err) 940 } 941 if err := db.Put(badBlockKey, data); err != nil { 942 log.Crit("Failed to write bad blocks", "err", err) 943 } 944 } 945 946 // DeleteBadBlocks deletes all the bad blocks from the database 947 func DeleteBadBlocks(db ethdb.KeyValueWriter) { 948 if err := db.Delete(badBlockKey); err != nil { 949 log.Crit("Failed to delete bad blocks", "err", err) 950 } 951 } 952 953 // FindCommonAncestor returns the last common ancestor of two block headers 954 func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header { 955 for bn := b.Number.Uint64(); a.Number.Uint64() > bn; { 956 a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) 957 if a == nil { 958 return nil 959 } 960 } 961 for an := a.Number.Uint64(); an < b.Number.Uint64(); { 962 b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) 963 if b == nil { 964 return nil 965 } 966 } 967 for a.Hash() != b.Hash() { 968 a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) 969 if a == nil { 970 return nil 971 } 972 b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) 973 if b == nil { 974 return nil 975 } 976 } 977 return a 978 } 979 980 // ReadHeadHeader returns the current canonical head header. 981 func ReadHeadHeader(db ethdb.Reader) *types.Header { 982 headHeaderHash := ReadHeadHeaderHash(db) 983 if headHeaderHash == (common.Hash{}) { 984 return nil 985 } 986 headHeaderNumber := ReadHeaderNumber(db, headHeaderHash) 987 if headHeaderNumber == nil { 988 return nil 989 } 990 return ReadHeader(db, headHeaderHash, *headHeaderNumber) 991 } 992 993 // ReadHeadBlock returns the current canonical head block. 994 func ReadHeadBlock(db ethdb.Reader) *types.Block { 995 headBlockHash := ReadHeadBlockHash(db) 996 if headBlockHash == (common.Hash{}) { 997 return nil 998 } 999 headBlockNumber := ReadHeaderNumber(db, headBlockHash) 1000 if headBlockNumber == nil { 1001 return nil 1002 } 1003 return ReadBlock(db, headBlockHash, *headBlockNumber) 1004 }