accessors_chain.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. package rawdb
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/params"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. // ReadCanonicalHash retrieves the hash assigned to a canonical block number.
  29. func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash {
  30. data, _ := db.Ancient(freezerHashTable, number)
  31. if len(data) == 0 {
  32. data, _ = db.Get(headerHashKey(number))
  33. // In the background freezer is moving data from leveldb to flatten files.
  34. // So during the first check for ancient db, the data is not yet in there,
  35. // but when we reach into leveldb, the data was already moved. That would
  36. // result in a not found error.
  37. if len(data) == 0 {
  38. data, _ = db.Ancient(freezerHashTable, number)
  39. }
  40. }
  41. if len(data) == 0 {
  42. return common.Hash{}
  43. }
  44. return common.BytesToHash(data)
  45. }
  46. // WriteCanonicalHash stores the hash assigned to a canonical block number.
  47. func WriteCanonicalHash(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  48. if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil {
  49. log.Crit("Failed to store number to hash mapping", "err", err)
  50. }
  51. }
  52. // DeleteCanonicalHash removes the number to hash canonical mapping.
  53. func DeleteCanonicalHash(db ethdb.KeyValueWriter, number uint64) {
  54. if err := db.Delete(headerHashKey(number)); err != nil {
  55. log.Crit("Failed to delete number to hash mapping", "err", err)
  56. }
  57. }
  58. // ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights,
  59. // both canonical and reorged forks included.
  60. func ReadAllHashes(db ethdb.Iteratee, number uint64) []common.Hash {
  61. prefix := headerKeyPrefix(number)
  62. hashes := make([]common.Hash, 0, 1)
  63. it := db.NewIteratorWithPrefix(prefix)
  64. defer it.Release()
  65. for it.Next() {
  66. if key := it.Key(); len(key) == len(prefix)+32 {
  67. hashes = append(hashes, common.BytesToHash(key[len(key)-32:]))
  68. }
  69. }
  70. return hashes
  71. }
  72. // ReadHeaderNumber returns the header number assigned to a hash.
  73. func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) *uint64 {
  74. data, _ := db.Get(headerNumberKey(hash))
  75. if len(data) != 8 {
  76. return nil
  77. }
  78. number := binary.BigEndian.Uint64(data)
  79. return &number
  80. }
  81. // WriteHeaderNumber stores the hash->number mapping.
  82. func WriteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  83. key := headerNumberKey(hash)
  84. enc := encodeBlockNumber(number)
  85. if err := db.Put(key, enc); err != nil {
  86. log.Crit("Failed to store hash to number mapping", "err", err)
  87. }
  88. }
  89. // DeleteHeaderNumber removes hash->number mapping.
  90. func DeleteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash) {
  91. if err := db.Delete(headerNumberKey(hash)); err != nil {
  92. log.Crit("Failed to delete hash to number mapping", "err", err)
  93. }
  94. }
  95. // ReadHeadHeaderHash retrieves the hash of the current canonical head header.
  96. func ReadHeadHeaderHash(db ethdb.KeyValueReader) common.Hash {
  97. data, _ := db.Get(headHeaderKey)
  98. if len(data) == 0 {
  99. return common.Hash{}
  100. }
  101. return common.BytesToHash(data)
  102. }
  103. // WriteHeadHeaderHash stores the hash of the current canonical head header.
  104. func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) {
  105. if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
  106. log.Crit("Failed to store last header's hash", "err", err)
  107. }
  108. }
  109. // ReadHeadBlockHash retrieves the hash of the current canonical head block.
  110. func ReadHeadBlockHash(db ethdb.KeyValueReader) common.Hash {
  111. data, _ := db.Get(headBlockKey)
  112. if len(data) == 0 {
  113. return common.Hash{}
  114. }
  115. return common.BytesToHash(data)
  116. }
  117. // WriteHeadBlockHash stores the head block's hash.
  118. func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
  119. if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
  120. log.Crit("Failed to store last block's hash", "err", err)
  121. }
  122. }
  123. // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block.
  124. func ReadHeadFastBlockHash(db ethdb.KeyValueReader) common.Hash {
  125. data, _ := db.Get(headFastBlockKey)
  126. if len(data) == 0 {
  127. return common.Hash{}
  128. }
  129. return common.BytesToHash(data)
  130. }
  131. // WriteHeadFastBlockHash stores the hash of the current fast-sync head block.
  132. func WriteHeadFastBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
  133. if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil {
  134. log.Crit("Failed to store last fast block's hash", "err", err)
  135. }
  136. }
  137. // ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow
  138. // reporting correct numbers across restarts.
  139. func ReadFastTrieProgress(db ethdb.KeyValueReader) uint64 {
  140. data, _ := db.Get(fastTrieProgressKey)
  141. if len(data) == 0 {
  142. return 0
  143. }
  144. return new(big.Int).SetBytes(data).Uint64()
  145. }
  146. // WriteFastTrieProgress stores the fast sync trie process counter to support
  147. // retrieving it across restarts.
  148. func WriteFastTrieProgress(db ethdb.KeyValueWriter, count uint64) {
  149. if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
  150. log.Crit("Failed to store fast sync trie progress", "err", err)
  151. }
  152. }
  153. // ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
  154. func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  155. data, _ := db.Ancient(freezerHeaderTable, number)
  156. if len(data) == 0 {
  157. data, _ = db.Get(headerKey(number, hash))
  158. // In the background freezer is moving data from leveldb to flatten files.
  159. // So during the first check for ancient db, the data is not yet in there,
  160. // but when we reach into leveldb, the data was already moved. That would
  161. // result in a not found error.
  162. if len(data) == 0 {
  163. data, _ = db.Ancient(freezerHeaderTable, number)
  164. }
  165. }
  166. return data
  167. }
  168. // HasHeader verifies the existence of a block header corresponding to the hash.
  169. func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
  170. if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
  171. return true
  172. }
  173. if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
  174. return false
  175. }
  176. return true
  177. }
  178. // ReadHeader retrieves the block header corresponding to the hash.
  179. func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header {
  180. data := ReadHeaderRLP(db, hash, number)
  181. if len(data) == 0 {
  182. return nil
  183. }
  184. header := new(types.Header)
  185. if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
  186. log.Error("Invalid block header RLP", "hash", hash, "err", err)
  187. return nil
  188. }
  189. return header
  190. }
  191. // WriteHeader stores a block header into the database and also stores the hash-
  192. // to-number mapping.
  193. func WriteHeader(db ethdb.KeyValueWriter, header *types.Header) {
  194. var (
  195. hash = header.Hash()
  196. number = header.Number.Uint64()
  197. )
  198. // Write the hash -> number mapping
  199. WriteHeaderNumber(db, hash, number)
  200. // Write the encoded header
  201. data, err := rlp.EncodeToBytes(header)
  202. if err != nil {
  203. log.Crit("Failed to RLP encode header", "err", err)
  204. }
  205. key := headerKey(number, hash)
  206. if err := db.Put(key, data); err != nil {
  207. log.Crit("Failed to store header", "err", err)
  208. }
  209. }
  210. // DeleteHeader removes all block header data associated with a hash.
  211. func DeleteHeader(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  212. deleteHeaderWithoutNumber(db, hash, number)
  213. if err := db.Delete(headerNumberKey(hash)); err != nil {
  214. log.Crit("Failed to delete hash to number mapping", "err", err)
  215. }
  216. }
  217. // deleteHeaderWithoutNumber removes only the block header but does not remove
  218. // the hash to number mapping.
  219. func deleteHeaderWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  220. if err := db.Delete(headerKey(number, hash)); err != nil {
  221. log.Crit("Failed to delete header", "err", err)
  222. }
  223. }
  224. // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  225. func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  226. data, _ := db.Ancient(freezerBodiesTable, number)
  227. if len(data) == 0 {
  228. data, _ = db.Get(blockBodyKey(number, hash))
  229. // In the background freezer is moving data from leveldb to flatten files.
  230. // So during the first check for ancient db, the data is not yet in there,
  231. // but when we reach into leveldb, the data was already moved. That would
  232. // result in a not found error.
  233. if len(data) == 0 {
  234. data, _ = db.Ancient(freezerBodiesTable, number)
  235. }
  236. }
  237. return data
  238. }
  239. // WriteBodyRLP stores an RLP encoded block body into the database.
  240. func WriteBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
  241. if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
  242. log.Crit("Failed to store block body", "err", err)
  243. }
  244. }
  245. // HasBody verifies the existence of a block body corresponding to the hash.
  246. func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool {
  247. if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
  248. return true
  249. }
  250. if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
  251. return false
  252. }
  253. return true
  254. }
  255. // ReadBody retrieves the block body corresponding to the hash.
  256. func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
  257. data := ReadBodyRLP(db, hash, number)
  258. if len(data) == 0 {
  259. return nil
  260. }
  261. body := new(types.Body)
  262. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  263. log.Error("Invalid block body RLP", "hash", hash, "err", err)
  264. return nil
  265. }
  266. return body
  267. }
  268. // WriteBody stores a block body into the database.
  269. func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *types.Body) {
  270. data, err := rlp.EncodeToBytes(body)
  271. if err != nil {
  272. log.Crit("Failed to RLP encode body", "err", err)
  273. }
  274. WriteBodyRLP(db, hash, number, data)
  275. }
  276. // DeleteBody removes all block body data associated with a hash.
  277. func DeleteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  278. if err := db.Delete(blockBodyKey(number, hash)); err != nil {
  279. log.Crit("Failed to delete block body", "err", err)
  280. }
  281. }
  282. // ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
  283. func ReadTdRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  284. data, _ := db.Ancient(freezerDifficultyTable, number)
  285. if len(data) == 0 {
  286. data, _ = db.Get(headerTDKey(number, hash))
  287. // In the background freezer is moving data from leveldb to flatten files.
  288. // So during the first check for ancient db, the data is not yet in there,
  289. // but when we reach into leveldb, the data was already moved. That would
  290. // result in a not found error.
  291. if len(data) == 0 {
  292. data, _ = db.Ancient(freezerDifficultyTable, number)
  293. }
  294. }
  295. return data
  296. }
  297. // ReadTd retrieves a block's total difficulty corresponding to the hash.
  298. func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
  299. data := ReadTdRLP(db, hash, number)
  300. if len(data) == 0 {
  301. return nil
  302. }
  303. td := new(big.Int)
  304. if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
  305. log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
  306. return nil
  307. }
  308. return td
  309. }
  310. // WriteTd stores the total difficulty of a block into the database.
  311. func WriteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64, td *big.Int) {
  312. data, err := rlp.EncodeToBytes(td)
  313. if err != nil {
  314. log.Crit("Failed to RLP encode block total difficulty", "err", err)
  315. }
  316. if err := db.Put(headerTDKey(number, hash), data); err != nil {
  317. log.Crit("Failed to store block total difficulty", "err", err)
  318. }
  319. }
  320. // DeleteTd removes all block total difficulty data associated with a hash.
  321. func DeleteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  322. if err := db.Delete(headerTDKey(number, hash)); err != nil {
  323. log.Crit("Failed to delete block total difficulty", "err", err)
  324. }
  325. }
  326. // HasReceipts verifies the existence of all the transaction receipts belonging
  327. // to a block.
  328. func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool {
  329. if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
  330. return true
  331. }
  332. if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil {
  333. return false
  334. }
  335. return true
  336. }
  337. // ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
  338. func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  339. data, _ := db.Ancient(freezerReceiptTable, number)
  340. if len(data) == 0 {
  341. data, _ = db.Get(blockReceiptsKey(number, hash))
  342. // In the background freezer is moving data from leveldb to flatten files.
  343. // So during the first check for ancient db, the data is not yet in there,
  344. // but when we reach into leveldb, the data was already moved. That would
  345. // result in a not found error.
  346. if len(data) == 0 {
  347. data, _ = db.Ancient(freezerReceiptTable, number)
  348. }
  349. }
  350. return data
  351. }
  352. // ReadRawReceipts retrieves all the transaction receipts belonging to a block.
  353. // The receipt metadata fields are not guaranteed to be populated, so they
  354. // should not be used. Use ReadReceipts instead if the metadata is needed.
  355. func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
  356. // Retrieve the flattened receipt slice
  357. data := ReadReceiptsRLP(db, hash, number)
  358. if len(data) == 0 {
  359. return nil
  360. }
  361. // Convert the receipts from their storage form to their internal representation
  362. storageReceipts := []*types.ReceiptForStorage{}
  363. if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
  364. log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
  365. return nil
  366. }
  367. receipts := make(types.Receipts, len(storageReceipts))
  368. for i, storageReceipt := range storageReceipts {
  369. receipts[i] = (*types.Receipt)(storageReceipt)
  370. }
  371. return receipts
  372. }
  373. // ReadReceipts retrieves all the transaction receipts belonging to a block, including
  374. // its correspoinding metadata fields. If it is unable to populate these metadata
  375. // fields then nil is returned.
  376. //
  377. // The current implementation populates these metadata fields by reading the receipts'
  378. // corresponding block body, so if the block body is not found it will return nil even
  379. // if the receipt itself is stored.
  380. func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts {
  381. // We're deriving many fields from the block body, retrieve beside the receipt
  382. receipts := ReadRawReceipts(db, hash, number)
  383. if receipts == nil {
  384. return nil
  385. }
  386. body := ReadBody(db, hash, number)
  387. if body == nil {
  388. log.Error("Missing body but have receipt", "hash", hash, "number", number)
  389. return nil
  390. }
  391. if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil {
  392. log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
  393. return nil
  394. }
  395. return receipts
  396. }
  397. // WriteReceipts stores all the transaction receipts belonging to a block.
  398. func WriteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64, receipts types.Receipts) {
  399. // Convert the receipts into their storage form and serialize them
  400. storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
  401. for i, receipt := range receipts {
  402. storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
  403. }
  404. bytes, err := rlp.EncodeToBytes(storageReceipts)
  405. if err != nil {
  406. log.Crit("Failed to encode block receipts", "err", err)
  407. }
  408. // Store the flattened receipt slice
  409. if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
  410. log.Crit("Failed to store block receipts", "err", err)
  411. }
  412. }
  413. // DeleteReceipts removes all receipt data associated with a block hash.
  414. func DeleteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  415. if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
  416. log.Crit("Failed to delete block receipts", "err", err)
  417. }
  418. }
  419. // ReadBlock retrieves an entire block corresponding to the hash, assembling it
  420. // back from the stored header and body. If either the header or body could not
  421. // be retrieved nil is returned.
  422. //
  423. // Note, due to concurrent download of header and block body the header and thus
  424. // canonical hash can be stored in the database but the body data not (yet).
  425. func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
  426. header := ReadHeader(db, hash, number)
  427. if header == nil {
  428. return nil
  429. }
  430. body := ReadBody(db, hash, number)
  431. if body == nil {
  432. return nil
  433. }
  434. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
  435. }
  436. // WriteBlock serializes a block into the database, header and body separately.
  437. func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) {
  438. WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
  439. WriteHeader(db, block.Header())
  440. }
  441. // WriteAncientBlock writes entire block data into ancient store and returns the total written size.
  442. func WriteAncientBlock(db ethdb.AncientWriter, block *types.Block, receipts types.Receipts, td *big.Int) int {
  443. // Encode all block components to RLP format.
  444. headerBlob, err := rlp.EncodeToBytes(block.Header())
  445. if err != nil {
  446. log.Crit("Failed to RLP encode block header", "err", err)
  447. }
  448. bodyBlob, err := rlp.EncodeToBytes(block.Body())
  449. if err != nil {
  450. log.Crit("Failed to RLP encode body", "err", err)
  451. }
  452. storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
  453. for i, receipt := range receipts {
  454. storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
  455. }
  456. receiptBlob, err := rlp.EncodeToBytes(storageReceipts)
  457. if err != nil {
  458. log.Crit("Failed to RLP encode block receipts", "err", err)
  459. }
  460. tdBlob, err := rlp.EncodeToBytes(td)
  461. if err != nil {
  462. log.Crit("Failed to RLP encode block total difficulty", "err", err)
  463. }
  464. // Write all blob to flatten files.
  465. err = db.AppendAncient(block.NumberU64(), block.Hash().Bytes(), headerBlob, bodyBlob, receiptBlob, tdBlob)
  466. if err != nil {
  467. log.Crit("Failed to write block data to ancient store", "err", err)
  468. }
  469. return len(headerBlob) + len(bodyBlob) + len(receiptBlob) + len(tdBlob) + common.HashLength
  470. }
  471. // DeleteBlock removes all block data associated with a hash.
  472. func DeleteBlock(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  473. DeleteReceipts(db, hash, number)
  474. DeleteHeader(db, hash, number)
  475. DeleteBody(db, hash, number)
  476. DeleteTd(db, hash, number)
  477. }
  478. // DeleteBlockWithoutNumber removes all block data associated with a hash, except
  479. // the hash to number mapping.
  480. func DeleteBlockWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  481. DeleteReceipts(db, hash, number)
  482. deleteHeaderWithoutNumber(db, hash, number)
  483. DeleteBody(db, hash, number)
  484. DeleteTd(db, hash, number)
  485. }
  486. // FindCommonAncestor returns the last common ancestor of two block headers
  487. func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header {
  488. for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
  489. a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
  490. if a == nil {
  491. return nil
  492. }
  493. }
  494. for an := a.Number.Uint64(); an < b.Number.Uint64(); {
  495. b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
  496. if b == nil {
  497. return nil
  498. }
  499. }
  500. for a.Hash() != b.Hash() {
  501. a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
  502. if a == nil {
  503. return nil
  504. }
  505. b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
  506. if b == nil {
  507. return nil
  508. }
  509. }
  510. return a
  511. }