database_util.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // Copyright 2015 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 core
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "math/big"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/ethdb"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/metrics"
  29. "github.com/ethereum/go-ethereum/params"
  30. "github.com/ethereum/go-ethereum/rlp"
  31. )
  32. // DatabaseReader wraps the Get method of a backing data store.
  33. type DatabaseReader interface {
  34. Get(key []byte) (value []byte, err error)
  35. }
  36. // DatabaseDeleter wraps the Delete method of a backing data store.
  37. type DatabaseDeleter interface {
  38. Delete(key []byte) error
  39. }
  40. var (
  41. headHeaderKey = []byte("LastHeader")
  42. headBlockKey = []byte("LastBlock")
  43. headFastKey = []byte("LastFast")
  44. trieSyncKey = []byte("TrieSync")
  45. // Data item prefixes (use single byte to avoid mixing data types, avoid `i`).
  46. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  47. tdSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
  48. numSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
  49. blockHashPrefix = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian)
  50. bodyPrefix = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body
  51. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  52. lookupPrefix = []byte("l") // lookupPrefix + hash -> transaction/receipt lookup metadata
  53. bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
  54. preimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage
  55. configPrefix = []byte("ethereum-config-") // config prefix for the db
  56. // Chain index prefixes (use `i` + single byte to avoid mixing data types).
  57. BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
  58. // used by old db, now only used for conversion
  59. oldReceiptsPrefix = []byte("receipts-")
  60. oldTxMetaSuffix = []byte{0x01}
  61. ErrChainConfigNotFound = errors.New("ChainConfig not found") // general config not found error
  62. preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
  63. preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
  64. )
  65. // TxLookupEntry is a positional metadata to help looking up the data content of
  66. // a transaction or receipt given only its hash.
  67. type TxLookupEntry struct {
  68. BlockHash common.Hash
  69. BlockIndex uint64
  70. Index uint64
  71. }
  72. // encodeBlockNumber encodes a block number as big endian uint64
  73. func encodeBlockNumber(number uint64) []byte {
  74. enc := make([]byte, 8)
  75. binary.BigEndian.PutUint64(enc, number)
  76. return enc
  77. }
  78. // GetCanonicalHash retrieves a hash assigned to a canonical block number.
  79. func GetCanonicalHash(db DatabaseReader, number uint64) common.Hash {
  80. data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
  81. if len(data) == 0 {
  82. return common.Hash{}
  83. }
  84. return common.BytesToHash(data)
  85. }
  86. // missingNumber is returned by GetBlockNumber if no header with the
  87. // given block hash has been stored in the database
  88. const missingNumber = uint64(0xffffffffffffffff)
  89. // GetBlockNumber returns the block number assigned to a block hash
  90. // if the corresponding header is present in the database
  91. func GetBlockNumber(db DatabaseReader, hash common.Hash) uint64 {
  92. data, _ := db.Get(append(blockHashPrefix, hash.Bytes()...))
  93. if len(data) != 8 {
  94. return missingNumber
  95. }
  96. return binary.BigEndian.Uint64(data)
  97. }
  98. // GetHeadHeaderHash retrieves the hash of the current canonical head block's
  99. // header. The difference between this and GetHeadBlockHash is that whereas the
  100. // last block hash is only updated upon a full block import, the last header
  101. // hash is updated already at header import, allowing head tracking for the
  102. // light synchronization mechanism.
  103. func GetHeadHeaderHash(db DatabaseReader) common.Hash {
  104. data, _ := db.Get(headHeaderKey)
  105. if len(data) == 0 {
  106. return common.Hash{}
  107. }
  108. return common.BytesToHash(data)
  109. }
  110. // GetHeadBlockHash retrieves the hash of the current canonical head block.
  111. func GetHeadBlockHash(db DatabaseReader) common.Hash {
  112. data, _ := db.Get(headBlockKey)
  113. if len(data) == 0 {
  114. return common.Hash{}
  115. }
  116. return common.BytesToHash(data)
  117. }
  118. // GetHeadFastBlockHash retrieves the hash of the current canonical head block during
  119. // fast synchronization. The difference between this and GetHeadBlockHash is that
  120. // whereas the last block hash is only updated upon a full block import, the last
  121. // fast hash is updated when importing pre-processed blocks.
  122. func GetHeadFastBlockHash(db DatabaseReader) common.Hash {
  123. data, _ := db.Get(headFastKey)
  124. if len(data) == 0 {
  125. return common.Hash{}
  126. }
  127. return common.BytesToHash(data)
  128. }
  129. // GetTrieSyncProgress retrieves the number of tries nodes fast synced to allow
  130. // reportinc correct numbers across restarts.
  131. func GetTrieSyncProgress(db DatabaseReader) uint64 {
  132. data, _ := db.Get(trieSyncKey)
  133. if len(data) == 0 {
  134. return 0
  135. }
  136. return new(big.Int).SetBytes(data).Uint64()
  137. }
  138. // GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
  139. // if the header's not found.
  140. func GetHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
  141. data, _ := db.Get(headerKey(hash, number))
  142. return data
  143. }
  144. // GetHeader retrieves the block header corresponding to the hash, nil if none
  145. // found.
  146. func GetHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header {
  147. data := GetHeaderRLP(db, hash, number)
  148. if len(data) == 0 {
  149. return nil
  150. }
  151. header := new(types.Header)
  152. if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
  153. log.Error("Invalid block header RLP", "hash", hash, "err", err)
  154. return nil
  155. }
  156. return header
  157. }
  158. // GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  159. func GetBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
  160. data, _ := db.Get(blockBodyKey(hash, number))
  161. return data
  162. }
  163. func headerKey(hash common.Hash, number uint64) []byte {
  164. return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  165. }
  166. func blockBodyKey(hash common.Hash, number uint64) []byte {
  167. return append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  168. }
  169. // GetBody retrieves the block body (transactons, uncles) corresponding to the
  170. // hash, nil if none found.
  171. func GetBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body {
  172. data := GetBodyRLP(db, hash, number)
  173. if len(data) == 0 {
  174. return nil
  175. }
  176. body := new(types.Body)
  177. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  178. log.Error("Invalid block body RLP", "hash", hash, "err", err)
  179. return nil
  180. }
  181. return body
  182. }
  183. // GetTd retrieves a block's total difficulty corresponding to the hash, nil if
  184. // none found.
  185. func GetTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int {
  186. data, _ := db.Get(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), tdSuffix...))
  187. if len(data) == 0 {
  188. return nil
  189. }
  190. td := new(big.Int)
  191. if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
  192. log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
  193. return nil
  194. }
  195. return td
  196. }
  197. // GetBlock retrieves an entire block corresponding to the hash, assembling it
  198. // back from the stored header and body. If either the header or body could not
  199. // be retrieved nil is returned.
  200. //
  201. // Note, due to concurrent download of header and block body the header and thus
  202. // canonical hash can be stored in the database but the body data not (yet).
  203. func GetBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block {
  204. // Retrieve the block header and body contents
  205. header := GetHeader(db, hash, number)
  206. if header == nil {
  207. return nil
  208. }
  209. body := GetBody(db, hash, number)
  210. if body == nil {
  211. return nil
  212. }
  213. // Reassemble the block and return
  214. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
  215. }
  216. // GetBlockReceipts retrieves the receipts generated by the transactions included
  217. // in a block given by its hash.
  218. func GetBlockReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts {
  219. data, _ := db.Get(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...))
  220. if len(data) == 0 {
  221. return nil
  222. }
  223. storageReceipts := []*types.ReceiptForStorage{}
  224. if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
  225. log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
  226. return nil
  227. }
  228. receipts := make(types.Receipts, len(storageReceipts))
  229. for i, receipt := range storageReceipts {
  230. receipts[i] = (*types.Receipt)(receipt)
  231. }
  232. return receipts
  233. }
  234. // GetTxLookupEntry retrieves the positional metadata associated with a transaction
  235. // hash to allow retrieving the transaction or receipt by hash.
  236. func GetTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
  237. // Load the positional metadata from disk and bail if it fails
  238. data, _ := db.Get(append(lookupPrefix, hash.Bytes()...))
  239. if len(data) == 0 {
  240. return common.Hash{}, 0, 0
  241. }
  242. // Parse and return the contents of the lookup entry
  243. var entry TxLookupEntry
  244. if err := rlp.DecodeBytes(data, &entry); err != nil {
  245. log.Error("Invalid lookup entry RLP", "hash", hash, "err", err)
  246. return common.Hash{}, 0, 0
  247. }
  248. return entry.BlockHash, entry.BlockIndex, entry.Index
  249. }
  250. // GetTransaction retrieves a specific transaction from the database, along with
  251. // its added positional metadata.
  252. func GetTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  253. // Retrieve the lookup metadata and resolve the transaction from the body
  254. blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash)
  255. if blockHash != (common.Hash{}) {
  256. body := GetBody(db, blockHash, blockNumber)
  257. if body == nil || len(body.Transactions) <= int(txIndex) {
  258. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
  259. return nil, common.Hash{}, 0, 0
  260. }
  261. return body.Transactions[txIndex], blockHash, blockNumber, txIndex
  262. }
  263. // Old transaction representation, load the transaction and it's metadata separately
  264. data, _ := db.Get(hash.Bytes())
  265. if len(data) == 0 {
  266. return nil, common.Hash{}, 0, 0
  267. }
  268. var tx types.Transaction
  269. if err := rlp.DecodeBytes(data, &tx); err != nil {
  270. return nil, common.Hash{}, 0, 0
  271. }
  272. // Retrieve the blockchain positional metadata
  273. data, _ = db.Get(append(hash.Bytes(), oldTxMetaSuffix...))
  274. if len(data) == 0 {
  275. return nil, common.Hash{}, 0, 0
  276. }
  277. var entry TxLookupEntry
  278. if err := rlp.DecodeBytes(data, &entry); err != nil {
  279. return nil, common.Hash{}, 0, 0
  280. }
  281. return &tx, entry.BlockHash, entry.BlockIndex, entry.Index
  282. }
  283. // GetReceipt retrieves a specific transaction receipt from the database, along with
  284. // its added positional metadata.
  285. func GetReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
  286. // Retrieve the lookup metadata and resolve the receipt from the receipts
  287. blockHash, blockNumber, receiptIndex := GetTxLookupEntry(db, hash)
  288. if blockHash != (common.Hash{}) {
  289. receipts := GetBlockReceipts(db, blockHash, blockNumber)
  290. if len(receipts) <= int(receiptIndex) {
  291. log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex)
  292. return nil, common.Hash{}, 0, 0
  293. }
  294. return receipts[receiptIndex], blockHash, blockNumber, receiptIndex
  295. }
  296. // Old receipt representation, load the receipt and set an unknown metadata
  297. data, _ := db.Get(append(oldReceiptsPrefix, hash[:]...))
  298. if len(data) == 0 {
  299. return nil, common.Hash{}, 0, 0
  300. }
  301. var receipt types.ReceiptForStorage
  302. err := rlp.DecodeBytes(data, &receipt)
  303. if err != nil {
  304. log.Error("Invalid receipt RLP", "hash", hash, "err", err)
  305. }
  306. return (*types.Receipt)(&receipt), common.Hash{}, 0, 0
  307. }
  308. // GetBloomBits retrieves the compressed bloom bit vector belonging to the given
  309. // section and bit index from the.
  310. func GetBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
  311. key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...)
  312. binary.BigEndian.PutUint16(key[1:], uint16(bit))
  313. binary.BigEndian.PutUint64(key[3:], section)
  314. return db.Get(key)
  315. }
  316. // WriteCanonicalHash stores the canonical hash for the given block number.
  317. func WriteCanonicalHash(db ethdb.Putter, hash common.Hash, number uint64) error {
  318. key := append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)
  319. if err := db.Put(key, hash.Bytes()); err != nil {
  320. log.Crit("Failed to store number to hash mapping", "err", err)
  321. }
  322. return nil
  323. }
  324. // WriteHeadHeaderHash stores the head header's hash.
  325. func WriteHeadHeaderHash(db ethdb.Putter, hash common.Hash) error {
  326. if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
  327. log.Crit("Failed to store last header's hash", "err", err)
  328. }
  329. return nil
  330. }
  331. // WriteHeadBlockHash stores the head block's hash.
  332. func WriteHeadBlockHash(db ethdb.Putter, hash common.Hash) error {
  333. if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
  334. log.Crit("Failed to store last block's hash", "err", err)
  335. }
  336. return nil
  337. }
  338. // WriteHeadFastBlockHash stores the fast head block's hash.
  339. func WriteHeadFastBlockHash(db ethdb.Putter, hash common.Hash) error {
  340. if err := db.Put(headFastKey, hash.Bytes()); err != nil {
  341. log.Crit("Failed to store last fast block's hash", "err", err)
  342. }
  343. return nil
  344. }
  345. // WriteTrieSyncProgress stores the fast sync trie process counter to support
  346. // retrieving it across restarts.
  347. func WriteTrieSyncProgress(db ethdb.Putter, count uint64) error {
  348. if err := db.Put(trieSyncKey, new(big.Int).SetUint64(count).Bytes()); err != nil {
  349. log.Crit("Failed to store fast sync trie progress", "err", err)
  350. }
  351. return nil
  352. }
  353. // WriteHeader serializes a block header into the database.
  354. func WriteHeader(db ethdb.Putter, header *types.Header) error {
  355. data, err := rlp.EncodeToBytes(header)
  356. if err != nil {
  357. return err
  358. }
  359. hash := header.Hash().Bytes()
  360. num := header.Number.Uint64()
  361. encNum := encodeBlockNumber(num)
  362. key := append(blockHashPrefix, hash...)
  363. if err := db.Put(key, encNum); err != nil {
  364. log.Crit("Failed to store hash to number mapping", "err", err)
  365. }
  366. key = append(append(headerPrefix, encNum...), hash...)
  367. if err := db.Put(key, data); err != nil {
  368. log.Crit("Failed to store header", "err", err)
  369. }
  370. return nil
  371. }
  372. // WriteBody serializes the body of a block into the database.
  373. func WriteBody(db ethdb.Putter, hash common.Hash, number uint64, body *types.Body) error {
  374. data, err := rlp.EncodeToBytes(body)
  375. if err != nil {
  376. return err
  377. }
  378. return WriteBodyRLP(db, hash, number, data)
  379. }
  380. // WriteBodyRLP writes a serialized body of a block into the database.
  381. func WriteBodyRLP(db ethdb.Putter, hash common.Hash, number uint64, rlp rlp.RawValue) error {
  382. key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  383. if err := db.Put(key, rlp); err != nil {
  384. log.Crit("Failed to store block body", "err", err)
  385. }
  386. return nil
  387. }
  388. // WriteTd serializes the total difficulty of a block into the database.
  389. func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) error {
  390. data, err := rlp.EncodeToBytes(td)
  391. if err != nil {
  392. return err
  393. }
  394. key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...)
  395. if err := db.Put(key, data); err != nil {
  396. log.Crit("Failed to store block total difficulty", "err", err)
  397. }
  398. return nil
  399. }
  400. // WriteBlock serializes a block into the database, header and body separately.
  401. func WriteBlock(db ethdb.Putter, block *types.Block) error {
  402. // Store the body first to retain database consistency
  403. if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
  404. return err
  405. }
  406. // Store the header too, signaling full block ownership
  407. if err := WriteHeader(db, block.Header()); err != nil {
  408. return err
  409. }
  410. return nil
  411. }
  412. // WriteBlockReceipts stores all the transaction receipts belonging to a block
  413. // as a single receipt slice. This is used during chain reorganisations for
  414. // rescheduling dropped transactions.
  415. func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receipts types.Receipts) error {
  416. // Convert the receipts into their storage form and serialize them
  417. storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
  418. for i, receipt := range receipts {
  419. storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
  420. }
  421. bytes, err := rlp.EncodeToBytes(storageReceipts)
  422. if err != nil {
  423. return err
  424. }
  425. // Store the flattened receipt slice
  426. key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  427. if err := db.Put(key, bytes); err != nil {
  428. log.Crit("Failed to store block receipts", "err", err)
  429. }
  430. return nil
  431. }
  432. // WriteTxLookupEntries stores a positional metadata for every transaction from
  433. // a block, enabling hash based transaction and receipt lookups.
  434. func WriteTxLookupEntries(db ethdb.Putter, block *types.Block) error {
  435. // Iterate over each transaction and encode its metadata
  436. for i, tx := range block.Transactions() {
  437. entry := TxLookupEntry{
  438. BlockHash: block.Hash(),
  439. BlockIndex: block.NumberU64(),
  440. Index: uint64(i),
  441. }
  442. data, err := rlp.EncodeToBytes(entry)
  443. if err != nil {
  444. return err
  445. }
  446. if err := db.Put(append(lookupPrefix, tx.Hash().Bytes()...), data); err != nil {
  447. return err
  448. }
  449. }
  450. return nil
  451. }
  452. // WriteBloomBits writes the compressed bloom bits vector belonging to the given
  453. // section and bit index.
  454. func WriteBloomBits(db ethdb.Putter, bit uint, section uint64, head common.Hash, bits []byte) {
  455. key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...)
  456. binary.BigEndian.PutUint16(key[1:], uint16(bit))
  457. binary.BigEndian.PutUint64(key[3:], section)
  458. if err := db.Put(key, bits); err != nil {
  459. log.Crit("Failed to store bloom bits", "err", err)
  460. }
  461. }
  462. // DeleteCanonicalHash removes the number to hash canonical mapping.
  463. func DeleteCanonicalHash(db DatabaseDeleter, number uint64) {
  464. db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
  465. }
  466. // DeleteHeader removes all block header data associated with a hash.
  467. func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) {
  468. db.Delete(append(blockHashPrefix, hash.Bytes()...))
  469. db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  470. }
  471. // DeleteBody removes all block body data associated with a hash.
  472. func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) {
  473. db.Delete(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  474. }
  475. // DeleteTd removes all block total difficulty data associated with a hash.
  476. func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) {
  477. db.Delete(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...))
  478. }
  479. // DeleteBlock removes all block data associated with a hash.
  480. func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) {
  481. DeleteBlockReceipts(db, hash, number)
  482. DeleteHeader(db, hash, number)
  483. DeleteBody(db, hash, number)
  484. DeleteTd(db, hash, number)
  485. }
  486. // DeleteBlockReceipts removes all receipt data associated with a block hash.
  487. func DeleteBlockReceipts(db DatabaseDeleter, hash common.Hash, number uint64) {
  488. db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  489. }
  490. // DeleteTxLookupEntry removes all transaction data associated with a hash.
  491. func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
  492. db.Delete(append(lookupPrefix, hash.Bytes()...))
  493. }
  494. // PreimageTable returns a Database instance with the key prefix for preimage entries.
  495. func PreimageTable(db ethdb.Database) ethdb.Database {
  496. return ethdb.NewTable(db, preimagePrefix)
  497. }
  498. // WritePreimages writes the provided set of preimages to the database. `number` is the
  499. // current block number, and is used for debug messages only.
  500. func WritePreimages(db ethdb.Database, number uint64, preimages map[common.Hash][]byte) error {
  501. table := PreimageTable(db)
  502. batch := table.NewBatch()
  503. hitCount := 0
  504. for hash, preimage := range preimages {
  505. if _, err := table.Get(hash.Bytes()); err != nil {
  506. batch.Put(hash.Bytes(), preimage)
  507. hitCount++
  508. }
  509. }
  510. preimageCounter.Inc(int64(len(preimages)))
  511. preimageHitCounter.Inc(int64(hitCount))
  512. if hitCount > 0 {
  513. if err := batch.Write(); err != nil {
  514. return fmt.Errorf("preimage write fail for block %d: %v", number, err)
  515. }
  516. }
  517. return nil
  518. }
  519. // GetBlockChainVersion reads the version number from db.
  520. func GetBlockChainVersion(db DatabaseReader) int {
  521. var vsn uint
  522. enc, _ := db.Get([]byte("BlockchainVersion"))
  523. rlp.DecodeBytes(enc, &vsn)
  524. return int(vsn)
  525. }
  526. // WriteBlockChainVersion writes vsn as the version number to db.
  527. func WriteBlockChainVersion(db ethdb.Putter, vsn int) {
  528. enc, _ := rlp.EncodeToBytes(uint(vsn))
  529. db.Put([]byte("BlockchainVersion"), enc)
  530. }
  531. // WriteChainConfig writes the chain config settings to the database.
  532. func WriteChainConfig(db ethdb.Putter, hash common.Hash, cfg *params.ChainConfig) error {
  533. // short circuit and ignore if nil config. GetChainConfig
  534. // will return a default.
  535. if cfg == nil {
  536. return nil
  537. }
  538. jsonChainConfig, err := json.Marshal(cfg)
  539. if err != nil {
  540. return err
  541. }
  542. return db.Put(append(configPrefix, hash[:]...), jsonChainConfig)
  543. }
  544. // GetChainConfig will fetch the network settings based on the given hash.
  545. func GetChainConfig(db DatabaseReader, hash common.Hash) (*params.ChainConfig, error) {
  546. jsonChainConfig, _ := db.Get(append(configPrefix, hash[:]...))
  547. if len(jsonChainConfig) == 0 {
  548. return nil, ErrChainConfigNotFound
  549. }
  550. var config params.ChainConfig
  551. if err := json.Unmarshal(jsonChainConfig, &config); err != nil {
  552. return nil, err
  553. }
  554. return &config, nil
  555. }
  556. // FindCommonAncestor returns the last common ancestor of two block headers
  557. func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header {
  558. for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
  559. a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
  560. if a == nil {
  561. return nil
  562. }
  563. }
  564. for an := a.Number.Uint64(); an < b.Number.Uint64(); {
  565. b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1)
  566. if b == nil {
  567. return nil
  568. }
  569. }
  570. for a.Hash() != b.Hash() {
  571. a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
  572. if a == nil {
  573. return nil
  574. }
  575. b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1)
  576. if b == nil {
  577. return nil
  578. }
  579. }
  580. return a
  581. }