accessors_chain.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. "sort"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/log"
  27. "github.com/ethereum/go-ethereum/params"
  28. "github.com/ethereum/go-ethereum/rlp"
  29. )
  30. // ReadCanonicalHash retrieves the hash assigned to a canonical block number.
  31. func ReadCanonicalHash(db ethdb.Reader, number uint64) common.Hash {
  32. data, _ := db.Ancient(freezerHashTable, number)
  33. if len(data) == 0 {
  34. data, _ = db.Get(headerHashKey(number))
  35. // In the background freezer is moving data from leveldb to flatten files.
  36. // So during the first check for ancient db, the data is not yet in there,
  37. // but when we reach into leveldb, the data was already moved. That would
  38. // result in a not found error.
  39. if len(data) == 0 {
  40. data, _ = db.Ancient(freezerHashTable, number)
  41. }
  42. }
  43. if len(data) == 0 {
  44. return common.Hash{}
  45. }
  46. return common.BytesToHash(data)
  47. }
  48. // WriteCanonicalHash stores the hash assigned to a canonical block number.
  49. func WriteCanonicalHash(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  50. if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil {
  51. log.Crit("Failed to store number to hash mapping", "err", err)
  52. }
  53. }
  54. // DeleteCanonicalHash removes the number to hash canonical mapping.
  55. func DeleteCanonicalHash(db ethdb.KeyValueWriter, number uint64) {
  56. if err := db.Delete(headerHashKey(number)); err != nil {
  57. log.Crit("Failed to delete number to hash mapping", "err", err)
  58. }
  59. }
  60. // ReadAllHashes retrieves all the hashes assigned to blocks at a certain heights,
  61. // both canonical and reorged forks included.
  62. func ReadAllHashes(db ethdb.Iteratee, number uint64) []common.Hash {
  63. prefix := headerKeyPrefix(number)
  64. hashes := make([]common.Hash, 0, 1)
  65. it := db.NewIterator(prefix, nil)
  66. defer it.Release()
  67. for it.Next() {
  68. if key := it.Key(); len(key) == len(prefix)+32 {
  69. hashes = append(hashes, common.BytesToHash(key[len(key)-32:]))
  70. }
  71. }
  72. return hashes
  73. }
  74. // ReadAllCanonicalHashes retrieves all canonical number and hash mappings at the
  75. // certain chain range. If the accumulated entries reaches the given threshold,
  76. // abort the iteration and return the semi-finish result.
  77. func ReadAllCanonicalHashes(db ethdb.Iteratee, from uint64, to uint64, limit int) ([]uint64, []common.Hash) {
  78. // Short circuit if the limit is 0.
  79. if limit == 0 {
  80. return nil, nil
  81. }
  82. var (
  83. numbers []uint64
  84. hashes []common.Hash
  85. )
  86. // Construct the key prefix of start point.
  87. start, end := headerHashKey(from), headerHashKey(to)
  88. it := db.NewIterator(nil, start)
  89. defer it.Release()
  90. for it.Next() {
  91. if bytes.Compare(it.Key(), end) >= 0 {
  92. break
  93. }
  94. if key := it.Key(); len(key) == len(headerPrefix)+8+1 && bytes.Equal(key[len(key)-1:], headerHashSuffix) {
  95. numbers = append(numbers, binary.BigEndian.Uint64(key[len(headerPrefix):len(headerPrefix)+8]))
  96. hashes = append(hashes, common.BytesToHash(it.Value()))
  97. // If the accumulated entries reaches the limit threshold, return.
  98. if len(numbers) >= limit {
  99. break
  100. }
  101. }
  102. }
  103. return numbers, hashes
  104. }
  105. // ReadHeaderNumber returns the header number assigned to a hash.
  106. func ReadHeaderNumber(db ethdb.KeyValueReader, hash common.Hash) *uint64 {
  107. data, _ := db.Get(headerNumberKey(hash))
  108. if len(data) != 8 {
  109. return nil
  110. }
  111. number := binary.BigEndian.Uint64(data)
  112. return &number
  113. }
  114. // WriteHeaderNumber stores the hash->number mapping.
  115. func WriteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  116. key := headerNumberKey(hash)
  117. enc := encodeBlockNumber(number)
  118. if err := db.Put(key, enc); err != nil {
  119. log.Crit("Failed to store hash to number mapping", "err", err)
  120. }
  121. }
  122. // DeleteHeaderNumber removes hash->number mapping.
  123. func DeleteHeaderNumber(db ethdb.KeyValueWriter, hash common.Hash) {
  124. if err := db.Delete(headerNumberKey(hash)); err != nil {
  125. log.Crit("Failed to delete hash to number mapping", "err", err)
  126. }
  127. }
  128. // ReadHeadHeaderHash retrieves the hash of the current canonical head header.
  129. func ReadHeadHeaderHash(db ethdb.KeyValueReader) common.Hash {
  130. data, _ := db.Get(headHeaderKey)
  131. if len(data) == 0 {
  132. return common.Hash{}
  133. }
  134. return common.BytesToHash(data)
  135. }
  136. // WriteHeadHeaderHash stores the hash of the current canonical head header.
  137. func WriteHeadHeaderHash(db ethdb.KeyValueWriter, hash common.Hash) {
  138. if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
  139. log.Crit("Failed to store last header's hash", "err", err)
  140. }
  141. }
  142. // ReadHeadBlockHash retrieves the hash of the current canonical head block.
  143. func ReadHeadBlockHash(db ethdb.KeyValueReader) common.Hash {
  144. data, _ := db.Get(headBlockKey)
  145. if len(data) == 0 {
  146. return common.Hash{}
  147. }
  148. return common.BytesToHash(data)
  149. }
  150. // WriteHeadBlockHash stores the head block's hash.
  151. func WriteHeadBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
  152. if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
  153. log.Crit("Failed to store last block's hash", "err", err)
  154. }
  155. }
  156. // ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block.
  157. func ReadHeadFastBlockHash(db ethdb.KeyValueReader) common.Hash {
  158. data, _ := db.Get(headFastBlockKey)
  159. if len(data) == 0 {
  160. return common.Hash{}
  161. }
  162. return common.BytesToHash(data)
  163. }
  164. // WriteHeadFastBlockHash stores the hash of the current fast-sync head block.
  165. func WriteHeadFastBlockHash(db ethdb.KeyValueWriter, hash common.Hash) {
  166. if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil {
  167. log.Crit("Failed to store last fast block's hash", "err", err)
  168. }
  169. }
  170. // ReadLastPivotNumber retrieves the number of the last pivot block. If the node
  171. // full synced, the last pivot will always be nil.
  172. func ReadLastPivotNumber(db ethdb.KeyValueReader) *uint64 {
  173. data, _ := db.Get(lastPivotKey)
  174. if len(data) == 0 {
  175. return nil
  176. }
  177. var pivot uint64
  178. if err := rlp.DecodeBytes(data, &pivot); err != nil {
  179. log.Error("Invalid pivot block number in database", "err", err)
  180. return nil
  181. }
  182. return &pivot
  183. }
  184. // WriteLastPivotNumber stores the number of the last pivot block.
  185. func WriteLastPivotNumber(db ethdb.KeyValueWriter, pivot uint64) {
  186. enc, err := rlp.EncodeToBytes(pivot)
  187. if err != nil {
  188. log.Crit("Failed to encode pivot block number", "err", err)
  189. }
  190. if err := db.Put(lastPivotKey, enc); err != nil {
  191. log.Crit("Failed to store pivot block number", "err", err)
  192. }
  193. }
  194. // ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow
  195. // reporting correct numbers across restarts.
  196. func ReadFastTrieProgress(db ethdb.KeyValueReader) uint64 {
  197. data, _ := db.Get(fastTrieProgressKey)
  198. if len(data) == 0 {
  199. return 0
  200. }
  201. return new(big.Int).SetBytes(data).Uint64()
  202. }
  203. // WriteFastTrieProgress stores the fast sync trie process counter to support
  204. // retrieving it across restarts.
  205. func WriteFastTrieProgress(db ethdb.KeyValueWriter, count uint64) {
  206. if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
  207. log.Crit("Failed to store fast sync trie progress", "err", err)
  208. }
  209. }
  210. // ReadTxIndexTail retrieves the number of oldest indexed block
  211. // whose transaction indices has been indexed. If the corresponding entry
  212. // is non-existent in database it means the indexing has been finished.
  213. func ReadTxIndexTail(db ethdb.KeyValueReader) *uint64 {
  214. data, _ := db.Get(txIndexTailKey)
  215. if len(data) != 8 {
  216. return nil
  217. }
  218. number := binary.BigEndian.Uint64(data)
  219. return &number
  220. }
  221. // WriteTxIndexTail stores the number of oldest indexed block
  222. // into database.
  223. func WriteTxIndexTail(db ethdb.KeyValueWriter, number uint64) {
  224. if err := db.Put(txIndexTailKey, encodeBlockNumber(number)); err != nil {
  225. log.Crit("Failed to store the transaction index tail", "err", err)
  226. }
  227. }
  228. // ReadFastTxLookupLimit retrieves the tx lookup limit used in fast sync.
  229. func ReadFastTxLookupLimit(db ethdb.KeyValueReader) *uint64 {
  230. data, _ := db.Get(fastTxLookupLimitKey)
  231. if len(data) != 8 {
  232. return nil
  233. }
  234. number := binary.BigEndian.Uint64(data)
  235. return &number
  236. }
  237. // WriteFastTxLookupLimit stores the txlookup limit used in fast sync into database.
  238. func WriteFastTxLookupLimit(db ethdb.KeyValueWriter, number uint64) {
  239. if err := db.Put(fastTxLookupLimitKey, encodeBlockNumber(number)); err != nil {
  240. log.Crit("Failed to store transaction lookup limit for fast sync", "err", err)
  241. }
  242. }
  243. // ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
  244. func ReadHeaderRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  245. // First try to look up the data in ancient database. Extra hash
  246. // comparison is necessary since ancient database only maintains
  247. // the canonical data.
  248. data, _ := db.Ancient(freezerHeaderTable, number)
  249. if len(data) > 0 && crypto.Keccak256Hash(data) == hash {
  250. return data
  251. }
  252. // Then try to look up the data in leveldb.
  253. data, _ = db.Get(headerKey(number, hash))
  254. if len(data) > 0 {
  255. return data
  256. }
  257. // In the background freezer is moving data from leveldb to flatten files.
  258. // So during the first check for ancient db, the data is not yet in there,
  259. // but when we reach into leveldb, the data was already moved. That would
  260. // result in a not found error.
  261. data, _ = db.Ancient(freezerHeaderTable, number)
  262. if len(data) > 0 && crypto.Keccak256Hash(data) == hash {
  263. return data
  264. }
  265. return nil // Can't find the data anywhere.
  266. }
  267. // HasHeader verifies the existence of a block header corresponding to the hash.
  268. func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
  269. if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
  270. return true
  271. }
  272. if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
  273. return false
  274. }
  275. return true
  276. }
  277. // ReadHeader retrieves the block header corresponding to the hash.
  278. func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header {
  279. data := ReadHeaderRLP(db, hash, number)
  280. if len(data) == 0 {
  281. return nil
  282. }
  283. header := new(types.Header)
  284. if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
  285. log.Error("Invalid block header RLP", "hash", hash, "err", err)
  286. return nil
  287. }
  288. return header
  289. }
  290. // WriteHeader stores a block header into the database and also stores the hash-
  291. // to-number mapping.
  292. func WriteHeader(db ethdb.KeyValueWriter, header *types.Header) {
  293. var (
  294. hash = header.Hash()
  295. number = header.Number.Uint64()
  296. )
  297. // Write the hash -> number mapping
  298. WriteHeaderNumber(db, hash, number)
  299. // Write the encoded header
  300. data, err := rlp.EncodeToBytes(header)
  301. if err != nil {
  302. log.Crit("Failed to RLP encode header", "err", err)
  303. }
  304. key := headerKey(number, hash)
  305. if err := db.Put(key, data); err != nil {
  306. log.Crit("Failed to store header", "err", err)
  307. }
  308. }
  309. // DeleteHeader removes all block header data associated with a hash.
  310. func DeleteHeader(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  311. deleteHeaderWithoutNumber(db, hash, number)
  312. if err := db.Delete(headerNumberKey(hash)); err != nil {
  313. log.Crit("Failed to delete hash to number mapping", "err", err)
  314. }
  315. }
  316. // deleteHeaderWithoutNumber removes only the block header but does not remove
  317. // the hash to number mapping.
  318. func deleteHeaderWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  319. if err := db.Delete(headerKey(number, hash)); err != nil {
  320. log.Crit("Failed to delete header", "err", err)
  321. }
  322. }
  323. // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  324. func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  325. // First try to look up the data in ancient database. Extra hash
  326. // comparison is necessary since ancient database only maintains
  327. // the canonical data.
  328. data, _ := db.Ancient(freezerBodiesTable, number)
  329. if len(data) > 0 {
  330. h, _ := db.Ancient(freezerHashTable, number)
  331. if common.BytesToHash(h) == hash {
  332. return data
  333. }
  334. }
  335. // Then try to look up the data in leveldb.
  336. data, _ = db.Get(blockBodyKey(number, hash))
  337. if len(data) > 0 {
  338. return data
  339. }
  340. // In the background freezer is moving data from leveldb to flatten files.
  341. // So during the first check for ancient db, the data is not yet in there,
  342. // but when we reach into leveldb, the data was already moved. That would
  343. // result in a not found error.
  344. data, _ = db.Ancient(freezerBodiesTable, number)
  345. if len(data) > 0 {
  346. h, _ := db.Ancient(freezerHashTable, number)
  347. if common.BytesToHash(h) == hash {
  348. return data
  349. }
  350. }
  351. return nil // Can't find the data anywhere.
  352. }
  353. // ReadCanonicalBodyRLP retrieves the block body (transactions and uncles) for the canonical
  354. // block at number, in RLP encoding.
  355. func ReadCanonicalBodyRLP(db ethdb.Reader, number uint64) rlp.RawValue {
  356. // If it's an ancient one, we don't need the canonical hash
  357. data, _ := db.Ancient(freezerBodiesTable, number)
  358. if len(data) == 0 {
  359. // Need to get the hash
  360. data, _ = db.Get(blockBodyKey(number, ReadCanonicalHash(db, number)))
  361. // In the background freezer is moving data from leveldb to flatten files.
  362. // So during the first check for ancient db, the data is not yet in there,
  363. // but when we reach into leveldb, the data was already moved. That would
  364. // result in a not found error.
  365. if len(data) == 0 {
  366. data, _ = db.Ancient(freezerBodiesTable, number)
  367. }
  368. }
  369. return data
  370. }
  371. // WriteBodyRLP stores an RLP encoded block body into the database.
  372. func WriteBodyRLP(db ethdb.KeyValueWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
  373. if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
  374. log.Crit("Failed to store block body", "err", err)
  375. }
  376. }
  377. // HasBody verifies the existence of a block body corresponding to the hash.
  378. func HasBody(db ethdb.Reader, hash common.Hash, number uint64) bool {
  379. if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
  380. return true
  381. }
  382. if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
  383. return false
  384. }
  385. return true
  386. }
  387. // ReadBody retrieves the block body corresponding to the hash.
  388. func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
  389. data := ReadBodyRLP(db, hash, number)
  390. if len(data) == 0 {
  391. return nil
  392. }
  393. body := new(types.Body)
  394. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  395. log.Error("Invalid block body RLP", "hash", hash, "err", err)
  396. return nil
  397. }
  398. return body
  399. }
  400. // WriteBody stores a block body into the database.
  401. func WriteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64, body *types.Body) {
  402. data, err := rlp.EncodeToBytes(body)
  403. if err != nil {
  404. log.Crit("Failed to RLP encode body", "err", err)
  405. }
  406. WriteBodyRLP(db, hash, number, data)
  407. }
  408. func WriteDiffLayer(db ethdb.KeyValueWriter, hash common.Hash, layer *types.DiffLayer) {
  409. data, err := rlp.EncodeToBytes(layer)
  410. if err != nil {
  411. log.Crit("Failed to RLP encode diff layer", "err", err)
  412. }
  413. WriteDiffLayerRLP(db, hash, data)
  414. }
  415. func WriteDiffLayerRLP(db ethdb.KeyValueWriter, blockHash common.Hash, rlp rlp.RawValue) {
  416. if err := db.Put(diffLayerKey(blockHash), rlp); err != nil {
  417. log.Crit("Failed to store diff layer", "err", err)
  418. }
  419. }
  420. func ReadDiffLayer(db ethdb.KeyValueReader, blockHash common.Hash) *types.DiffLayer {
  421. data := ReadDiffLayerRLP(db, blockHash)
  422. if len(data) == 0 {
  423. return nil
  424. }
  425. diff := new(types.DiffLayer)
  426. if err := rlp.Decode(bytes.NewReader(data), diff); err != nil {
  427. log.Error("Invalid diff layer RLP", "hash", blockHash, "err", err)
  428. return nil
  429. }
  430. return diff
  431. }
  432. func ReadDiffLayerRLP(db ethdb.KeyValueReader, blockHash common.Hash) rlp.RawValue {
  433. data, _ := db.Get(diffLayerKey(blockHash))
  434. return data
  435. }
  436. func DeleteDiffLayer(db ethdb.KeyValueWriter, blockHash common.Hash) {
  437. if err := db.Delete(diffLayerKey(blockHash)); err != nil {
  438. log.Crit("Failed to delete diffLayer", "err", err)
  439. }
  440. }
  441. // DeleteBody removes all block body data associated with a hash.
  442. func DeleteBody(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  443. if err := db.Delete(blockBodyKey(number, hash)); err != nil {
  444. log.Crit("Failed to delete block body", "err", err)
  445. }
  446. }
  447. // ReadTdRLP retrieves a block's total difficulty corresponding to the hash in RLP encoding.
  448. func ReadTdRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  449. // First try to look up the data in ancient database. Extra hash
  450. // comparison is necessary since ancient database only maintains
  451. // the canonical data.
  452. data, _ := db.Ancient(freezerDifficultyTable, number)
  453. if len(data) > 0 {
  454. h, _ := db.Ancient(freezerHashTable, number)
  455. if common.BytesToHash(h) == hash {
  456. return data
  457. }
  458. }
  459. // Then try to look up the data in leveldb.
  460. data, _ = db.Get(headerTDKey(number, hash))
  461. if len(data) > 0 {
  462. return data
  463. }
  464. // In the background freezer is moving data from leveldb to flatten files.
  465. // So during the first check for ancient db, the data is not yet in there,
  466. // but when we reach into leveldb, the data was already moved. That would
  467. // result in a not found error.
  468. data, _ = db.Ancient(freezerDifficultyTable, number)
  469. if len(data) > 0 {
  470. h, _ := db.Ancient(freezerHashTable, number)
  471. if common.BytesToHash(h) == hash {
  472. return data
  473. }
  474. }
  475. return nil // Can't find the data anywhere.
  476. }
  477. // ReadTd retrieves a block's total difficulty corresponding to the hash.
  478. func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
  479. data := ReadTdRLP(db, hash, number)
  480. if len(data) == 0 {
  481. return nil
  482. }
  483. td := new(big.Int)
  484. if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
  485. log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
  486. return nil
  487. }
  488. return td
  489. }
  490. // WriteTd stores the total difficulty of a block into the database.
  491. func WriteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64, td *big.Int) {
  492. data, err := rlp.EncodeToBytes(td)
  493. if err != nil {
  494. log.Crit("Failed to RLP encode block total difficulty", "err", err)
  495. }
  496. if err := db.Put(headerTDKey(number, hash), data); err != nil {
  497. log.Crit("Failed to store block total difficulty", "err", err)
  498. }
  499. }
  500. // DeleteTd removes all block total difficulty data associated with a hash.
  501. func DeleteTd(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  502. if err := db.Delete(headerTDKey(number, hash)); err != nil {
  503. log.Crit("Failed to delete block total difficulty", "err", err)
  504. }
  505. }
  506. // HasReceipts verifies the existence of all the transaction receipts belonging
  507. // to a block.
  508. func HasReceipts(db ethdb.Reader, hash common.Hash, number uint64) bool {
  509. if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
  510. return true
  511. }
  512. if has, err := db.Has(blockReceiptsKey(number, hash)); !has || err != nil {
  513. return false
  514. }
  515. return true
  516. }
  517. // ReadReceiptsRLP retrieves all the transaction receipts belonging to a block in RLP encoding.
  518. func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue {
  519. // First try to look up the data in ancient database. Extra hash
  520. // comparison is necessary since ancient database only maintains
  521. // the canonical data.
  522. data, _ := db.Ancient(freezerReceiptTable, number)
  523. if len(data) > 0 {
  524. h, _ := db.Ancient(freezerHashTable, number)
  525. if common.BytesToHash(h) == hash {
  526. return data
  527. }
  528. }
  529. // Then try to look up the data in leveldb.
  530. data, _ = db.Get(blockReceiptsKey(number, hash))
  531. if len(data) > 0 {
  532. return data
  533. }
  534. // In the background freezer is moving data from leveldb to flatten files.
  535. // So during the first check for ancient db, the data is not yet in there,
  536. // but when we reach into leveldb, the data was already moved. That would
  537. // result in a not found error.
  538. data, _ = db.Ancient(freezerReceiptTable, number)
  539. if len(data) > 0 {
  540. h, _ := db.Ancient(freezerHashTable, number)
  541. if common.BytesToHash(h) == hash {
  542. return data
  543. }
  544. }
  545. return nil // Can't find the data anywhere.
  546. }
  547. // ReadRawReceipts retrieves all the transaction receipts belonging to a block.
  548. // The receipt metadata fields are not guaranteed to be populated, so they
  549. // should not be used. Use ReadReceipts instead if the metadata is needed.
  550. func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
  551. // Retrieve the flattened receipt slice
  552. data := ReadReceiptsRLP(db, hash, number)
  553. if len(data) == 0 {
  554. return nil
  555. }
  556. // Convert the receipts from their storage form to their internal representation
  557. storageReceipts := []*types.ReceiptForStorage{}
  558. if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
  559. log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
  560. return nil
  561. }
  562. receipts := make(types.Receipts, len(storageReceipts))
  563. for i, storageReceipt := range storageReceipts {
  564. receipts[i] = (*types.Receipt)(storageReceipt)
  565. }
  566. return receipts
  567. }
  568. // ReadReceipts retrieves all the transaction receipts belonging to a block, including
  569. // its correspoinding metadata fields. If it is unable to populate these metadata
  570. // fields then nil is returned.
  571. //
  572. // The current implementation populates these metadata fields by reading the receipts'
  573. // corresponding block body, so if the block body is not found it will return nil even
  574. // if the receipt itself is stored.
  575. func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts {
  576. // We're deriving many fields from the block body, retrieve beside the receipt
  577. receipts := ReadRawReceipts(db, hash, number)
  578. if receipts == nil {
  579. return nil
  580. }
  581. body := ReadBody(db, hash, number)
  582. if body == nil {
  583. log.Error("Missing body but have receipt", "hash", hash, "number", number)
  584. return nil
  585. }
  586. if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil {
  587. log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
  588. return nil
  589. }
  590. return receipts
  591. }
  592. // WriteReceipts stores all the transaction receipts belonging to a block.
  593. func WriteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64, receipts types.Receipts) {
  594. // Convert the receipts into their storage form and serialize them
  595. storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
  596. for i, receipt := range receipts {
  597. storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
  598. }
  599. bytes, err := rlp.EncodeToBytes(storageReceipts)
  600. if err != nil {
  601. log.Crit("Failed to encode block receipts", "err", err)
  602. }
  603. // Store the flattened receipt slice
  604. if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
  605. log.Crit("Failed to store block receipts", "err", err)
  606. }
  607. }
  608. // DeleteReceipts removes all receipt data associated with a block hash.
  609. func DeleteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  610. if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
  611. log.Crit("Failed to delete block receipts", "err", err)
  612. }
  613. }
  614. // ReadBlock retrieves an entire block corresponding to the hash, assembling it
  615. // back from the stored header and body. If either the header or body could not
  616. // be retrieved nil is returned.
  617. //
  618. // Note, due to concurrent download of header and block body the header and thus
  619. // canonical hash can be stored in the database but the body data not (yet).
  620. func ReadBlock(db ethdb.Reader, hash common.Hash, number uint64) *types.Block {
  621. header := ReadHeader(db, hash, number)
  622. if header == nil {
  623. return nil
  624. }
  625. body := ReadBody(db, hash, number)
  626. if body == nil {
  627. return nil
  628. }
  629. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
  630. }
  631. // WriteBlock serializes a block into the database, header and body separately.
  632. func WriteBlock(db ethdb.KeyValueWriter, block *types.Block) {
  633. WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
  634. WriteHeader(db, block.Header())
  635. }
  636. // WriteAncientBlock writes entire block data into ancient store and returns the total written size.
  637. func WriteAncientBlock(db ethdb.AncientWriter, block *types.Block, receipts types.Receipts, td *big.Int) int {
  638. // Encode all block components to RLP format.
  639. headerBlob, err := rlp.EncodeToBytes(block.Header())
  640. if err != nil {
  641. log.Crit("Failed to RLP encode block header", "err", err)
  642. }
  643. bodyBlob, err := rlp.EncodeToBytes(block.Body())
  644. if err != nil {
  645. log.Crit("Failed to RLP encode body", "err", err)
  646. }
  647. storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
  648. for i, receipt := range receipts {
  649. storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
  650. }
  651. receiptBlob, err := rlp.EncodeToBytes(storageReceipts)
  652. if err != nil {
  653. log.Crit("Failed to RLP encode block receipts", "err", err)
  654. }
  655. tdBlob, err := rlp.EncodeToBytes(td)
  656. if err != nil {
  657. log.Crit("Failed to RLP encode block total difficulty", "err", err)
  658. }
  659. // Write all blob to flatten files.
  660. err = db.AppendAncient(block.NumberU64(), block.Hash().Bytes(), headerBlob, bodyBlob, receiptBlob, tdBlob)
  661. if err != nil {
  662. log.Crit("Failed to write block data to ancient store", "err", err)
  663. }
  664. return len(headerBlob) + len(bodyBlob) + len(receiptBlob) + len(tdBlob) + common.HashLength
  665. }
  666. // DeleteBlock removes all block data associated with a hash.
  667. func DeleteBlock(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  668. DeleteReceipts(db, hash, number)
  669. DeleteHeader(db, hash, number)
  670. DeleteBody(db, hash, number)
  671. DeleteTd(db, hash, number)
  672. }
  673. // DeleteBlockWithoutNumber removes all block data associated with a hash, except
  674. // the hash to number mapping.
  675. func DeleteBlockWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
  676. DeleteReceipts(db, hash, number)
  677. deleteHeaderWithoutNumber(db, hash, number)
  678. DeleteBody(db, hash, number)
  679. DeleteTd(db, hash, number)
  680. }
  681. const badBlockToKeep = 10
  682. type badBlock struct {
  683. Header *types.Header
  684. Body *types.Body
  685. }
  686. // badBlockList implements the sort interface to allow sorting a list of
  687. // bad blocks by their number in the reverse order.
  688. type badBlockList []*badBlock
  689. func (s badBlockList) Len() int { return len(s) }
  690. func (s badBlockList) Less(i, j int) bool {
  691. return s[i].Header.Number.Uint64() < s[j].Header.Number.Uint64()
  692. }
  693. func (s badBlockList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  694. // ReadBadBlock retrieves the bad block with the corresponding block hash.
  695. func ReadBadBlock(db ethdb.Reader, hash common.Hash) *types.Block {
  696. blob, err := db.Get(badBlockKey)
  697. if err != nil {
  698. return nil
  699. }
  700. var badBlocks badBlockList
  701. if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
  702. return nil
  703. }
  704. for _, bad := range badBlocks {
  705. if bad.Header.Hash() == hash {
  706. return types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles)
  707. }
  708. }
  709. return nil
  710. }
  711. // ReadAllBadBlocks retrieves all the bad blocks in the database.
  712. // All returned blocks are sorted in reverse order by number.
  713. func ReadAllBadBlocks(db ethdb.Reader) []*types.Block {
  714. blob, err := db.Get(badBlockKey)
  715. if err != nil {
  716. return nil
  717. }
  718. var badBlocks badBlockList
  719. if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
  720. return nil
  721. }
  722. var blocks []*types.Block
  723. for _, bad := range badBlocks {
  724. blocks = append(blocks, types.NewBlockWithHeader(bad.Header).WithBody(bad.Body.Transactions, bad.Body.Uncles))
  725. }
  726. return blocks
  727. }
  728. // WriteBadBlock serializes the bad block into the database. If the cumulated
  729. // bad blocks exceeds the limitation, the oldest will be dropped.
  730. func WriteBadBlock(db ethdb.KeyValueStore, block *types.Block) {
  731. blob, err := db.Get(badBlockKey)
  732. if err != nil {
  733. log.Warn("Failed to load old bad blocks", "error", err)
  734. }
  735. var badBlocks badBlockList
  736. if len(blob) > 0 {
  737. if err := rlp.DecodeBytes(blob, &badBlocks); err != nil {
  738. log.Crit("Failed to decode old bad blocks", "error", err)
  739. }
  740. }
  741. for _, b := range badBlocks {
  742. if b.Header.Number.Uint64() == block.NumberU64() && b.Header.Hash() == block.Hash() {
  743. log.Info("Skip duplicated bad block", "number", block.NumberU64(), "hash", block.Hash())
  744. return
  745. }
  746. }
  747. badBlocks = append(badBlocks, &badBlock{
  748. Header: block.Header(),
  749. Body: block.Body(),
  750. })
  751. sort.Sort(sort.Reverse(badBlocks))
  752. if len(badBlocks) > badBlockToKeep {
  753. badBlocks = badBlocks[:badBlockToKeep]
  754. }
  755. data, err := rlp.EncodeToBytes(badBlocks)
  756. if err != nil {
  757. log.Crit("Failed to encode bad blocks", "err", err)
  758. }
  759. if err := db.Put(badBlockKey, data); err != nil {
  760. log.Crit("Failed to write bad blocks", "err", err)
  761. }
  762. }
  763. // DeleteBadBlocks deletes all the bad blocks from the database
  764. func DeleteBadBlocks(db ethdb.KeyValueWriter) {
  765. if err := db.Delete(badBlockKey); err != nil {
  766. log.Crit("Failed to delete bad blocks", "err", err)
  767. }
  768. }
  769. // FindCommonAncestor returns the last common ancestor of two block headers
  770. func FindCommonAncestor(db ethdb.Reader, a, b *types.Header) *types.Header {
  771. for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
  772. a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
  773. if a == nil {
  774. return nil
  775. }
  776. }
  777. for an := a.Number.Uint64(); an < b.Number.Uint64(); {
  778. b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
  779. if b == nil {
  780. return nil
  781. }
  782. }
  783. for a.Hash() != b.Hash() {
  784. a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1)
  785. if a == nil {
  786. return nil
  787. }
  788. b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1)
  789. if b == nil {
  790. return nil
  791. }
  792. }
  793. return a
  794. }
  795. // ReadHeadHeader returns the current canonical head header.
  796. func ReadHeadHeader(db ethdb.Reader) *types.Header {
  797. headHeaderHash := ReadHeadHeaderHash(db)
  798. if headHeaderHash == (common.Hash{}) {
  799. return nil
  800. }
  801. headHeaderNumber := ReadHeaderNumber(db, headHeaderHash)
  802. if headHeaderNumber == nil {
  803. return nil
  804. }
  805. return ReadHeader(db, headHeaderHash, *headHeaderNumber)
  806. }
  807. // ReadHeadBlock returns the current canonical head block.
  808. func ReadHeadBlock(db ethdb.Reader) *types.Block {
  809. headBlockHash := ReadHeadBlockHash(db)
  810. if headBlockHash == (common.Hash{}) {
  811. return nil
  812. }
  813. headBlockNumber := ReadHeaderNumber(db, headBlockHash)
  814. if headBlockNumber == nil {
  815. return nil
  816. }
  817. return ReadBlock(db, headBlockHash, *headBlockNumber)
  818. }