database_util.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. "sync"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/logger"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. "github.com/ethereum/go-ethereum/metrics"
  31. "github.com/ethereum/go-ethereum/params"
  32. "github.com/ethereum/go-ethereum/rlp"
  33. )
  34. var (
  35. headHeaderKey = []byte("LastHeader")
  36. headBlockKey = []byte("LastBlock")
  37. headFastKey = []byte("LastFast")
  38. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  39. tdSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
  40. numSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
  41. blockHashPrefix = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian)
  42. bodyPrefix = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body
  43. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  44. preimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage
  45. txMetaSuffix = []byte{0x01}
  46. receiptsPrefix = []byte("receipts-")
  47. mipmapPre = []byte("mipmap-log-bloom-")
  48. MIPMapLevels = []uint64{1000000, 500000, 100000, 50000, 1000}
  49. configPrefix = []byte("ethereum-config-") // config prefix for the db
  50. // used by old (non-sequential keys) db, now only used for conversion
  51. oldBlockPrefix = []byte("block-")
  52. oldHeaderSuffix = []byte("-header")
  53. oldTdSuffix = []byte("-td") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
  54. oldBodySuffix = []byte("-body")
  55. oldBlockNumPrefix = []byte("block-num-")
  56. oldBlockReceiptsPrefix = []byte("receipts-block-")
  57. oldBlockHashPrefix = []byte("block-hash-") // [deprecated by the header/block split, remove eventually]
  58. ChainConfigNotFoundErr = errors.New("ChainConfig not found") // general config not found error
  59. mipmapBloomMu sync.Mutex // protect against race condition when updating mipmap blooms
  60. preimageCounter = metrics.NewCounter("db/preimage/total")
  61. preimageHitCounter = metrics.NewCounter("db/preimage/hits")
  62. )
  63. // encodeBlockNumber encodes a block number as big endian uint64
  64. func encodeBlockNumber(number uint64) []byte {
  65. enc := make([]byte, 8)
  66. binary.BigEndian.PutUint64(enc, number)
  67. return enc
  68. }
  69. // GetCanonicalHash retrieves a hash assigned to a canonical block number.
  70. func GetCanonicalHash(db ethdb.Database, number uint64) common.Hash {
  71. data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
  72. if len(data) == 0 {
  73. data, _ = db.Get(append(oldBlockNumPrefix, big.NewInt(int64(number)).Bytes()...))
  74. if len(data) == 0 {
  75. return common.Hash{}
  76. }
  77. }
  78. return common.BytesToHash(data)
  79. }
  80. // missingNumber is returned by GetBlockNumber if no header with the
  81. // given block hash has been stored in the database
  82. const missingNumber = uint64(0xffffffffffffffff)
  83. // GetBlockNumber returns the block number assigned to a block hash
  84. // if the corresponding header is present in the database
  85. func GetBlockNumber(db ethdb.Database, hash common.Hash) uint64 {
  86. data, _ := db.Get(append(blockHashPrefix, hash.Bytes()...))
  87. if len(data) != 8 {
  88. data, _ := db.Get(append(append(oldBlockPrefix, hash.Bytes()...), oldHeaderSuffix...))
  89. if len(data) == 0 {
  90. return missingNumber
  91. }
  92. header := new(types.Header)
  93. if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
  94. glog.Fatalf("failed to decode block header: %v", err)
  95. }
  96. return header.Number.Uint64()
  97. }
  98. return binary.BigEndian.Uint64(data)
  99. }
  100. // GetHeadHeaderHash retrieves the hash of the current canonical head block's
  101. // header. The difference between this and GetHeadBlockHash is that whereas the
  102. // last block hash is only updated upon a full block import, the last header
  103. // hash is updated already at header import, allowing head tracking for the
  104. // light synchronization mechanism.
  105. func GetHeadHeaderHash(db ethdb.Database) common.Hash {
  106. data, _ := db.Get(headHeaderKey)
  107. if len(data) == 0 {
  108. return common.Hash{}
  109. }
  110. return common.BytesToHash(data)
  111. }
  112. // GetHeadBlockHash retrieves the hash of the current canonical head block.
  113. func GetHeadBlockHash(db ethdb.Database) common.Hash {
  114. data, _ := db.Get(headBlockKey)
  115. if len(data) == 0 {
  116. return common.Hash{}
  117. }
  118. return common.BytesToHash(data)
  119. }
  120. // GetHeadFastBlockHash retrieves the hash of the current canonical head block during
  121. // fast synchronization. The difference between this and GetHeadBlockHash is that
  122. // whereas the last block hash is only updated upon a full block import, the last
  123. // fast hash is updated when importing pre-processed blocks.
  124. func GetHeadFastBlockHash(db ethdb.Database) common.Hash {
  125. data, _ := db.Get(headFastKey)
  126. if len(data) == 0 {
  127. return common.Hash{}
  128. }
  129. return common.BytesToHash(data)
  130. }
  131. // GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
  132. // if the header's not found.
  133. func GetHeaderRLP(db ethdb.Database, hash common.Hash, number uint64) rlp.RawValue {
  134. data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  135. if len(data) == 0 {
  136. data, _ = db.Get(append(append(oldBlockPrefix, hash.Bytes()...), oldHeaderSuffix...))
  137. }
  138. return data
  139. }
  140. // GetHeader retrieves the block header corresponding to the hash, nil if none
  141. // found.
  142. func GetHeader(db ethdb.Database, hash common.Hash, number uint64) *types.Header {
  143. data := GetHeaderRLP(db, hash, number)
  144. if len(data) == 0 {
  145. return nil
  146. }
  147. header := new(types.Header)
  148. if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
  149. glog.V(logger.Error).Infof("invalid block header RLP for hash %x: %v", hash, err)
  150. return nil
  151. }
  152. return header
  153. }
  154. // GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
  155. func GetBodyRLP(db ethdb.Database, hash common.Hash, number uint64) rlp.RawValue {
  156. data, _ := db.Get(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  157. if len(data) == 0 {
  158. data, _ = db.Get(append(append(oldBlockPrefix, hash.Bytes()...), oldBodySuffix...))
  159. }
  160. return data
  161. }
  162. // GetBody retrieves the block body (transactons, uncles) corresponding to the
  163. // hash, nil if none found.
  164. func GetBody(db ethdb.Database, hash common.Hash, number uint64) *types.Body {
  165. data := GetBodyRLP(db, hash, number)
  166. if len(data) == 0 {
  167. return nil
  168. }
  169. body := new(types.Body)
  170. if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
  171. glog.V(logger.Error).Infof("invalid block body RLP for hash %x: %v", hash, err)
  172. return nil
  173. }
  174. return body
  175. }
  176. // GetTd retrieves a block's total difficulty corresponding to the hash, nil if
  177. // none found.
  178. func GetTd(db ethdb.Database, hash common.Hash, number uint64) *big.Int {
  179. data, _ := db.Get(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), tdSuffix...))
  180. if len(data) == 0 {
  181. data, _ = db.Get(append(append(oldBlockPrefix, hash.Bytes()...), oldTdSuffix...))
  182. if len(data) == 0 {
  183. return nil
  184. }
  185. }
  186. td := new(big.Int)
  187. if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
  188. glog.V(logger.Error).Infof("invalid block total difficulty RLP for hash %x: %v", hash, err)
  189. return nil
  190. }
  191. return td
  192. }
  193. // GetBlock retrieves an entire block corresponding to the hash, assembling it
  194. // back from the stored header and body. If either the header or body could not
  195. // be retrieved nil is returned.
  196. //
  197. // Note, due to concurrent download of header and block body the header and thus
  198. // canonical hash can be stored in the database but the body data not (yet).
  199. func GetBlock(db ethdb.Database, hash common.Hash, number uint64) *types.Block {
  200. // Retrieve the block header and body contents
  201. header := GetHeader(db, hash, number)
  202. if header == nil {
  203. return nil
  204. }
  205. body := GetBody(db, hash, number)
  206. if body == nil {
  207. return nil
  208. }
  209. // Reassemble the block and return
  210. return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles)
  211. }
  212. // GetBlockReceipts retrieves the receipts generated by the transactions included
  213. // in a block given by its hash.
  214. func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types.Receipts {
  215. data, _ := db.Get(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...))
  216. if len(data) == 0 {
  217. data, _ = db.Get(append(oldBlockReceiptsPrefix, hash.Bytes()...))
  218. if len(data) == 0 {
  219. return nil
  220. }
  221. }
  222. storageReceipts := []*types.ReceiptForStorage{}
  223. if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
  224. glog.V(logger.Error).Infof("invalid receipt array RLP for hash %x: %v", hash, err)
  225. return nil
  226. }
  227. receipts := make(types.Receipts, len(storageReceipts))
  228. for i, receipt := range storageReceipts {
  229. receipts[i] = (*types.Receipt)(receipt)
  230. }
  231. return receipts
  232. }
  233. // GetTransaction retrieves a specific transaction from the database, along with
  234. // its added positional metadata.
  235. func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  236. // Retrieve the transaction itself from the database
  237. data, _ := db.Get(hash.Bytes())
  238. if len(data) == 0 {
  239. return nil, common.Hash{}, 0, 0
  240. }
  241. var tx types.Transaction
  242. if err := rlp.DecodeBytes(data, &tx); err != nil {
  243. return nil, common.Hash{}, 0, 0
  244. }
  245. // Retrieve the blockchain positional metadata
  246. data, _ = db.Get(append(hash.Bytes(), txMetaSuffix...))
  247. if len(data) == 0 {
  248. return nil, common.Hash{}, 0, 0
  249. }
  250. var meta struct {
  251. BlockHash common.Hash
  252. BlockIndex uint64
  253. Index uint64
  254. }
  255. if err := rlp.DecodeBytes(data, &meta); err != nil {
  256. return nil, common.Hash{}, 0, 0
  257. }
  258. return &tx, meta.BlockHash, meta.BlockIndex, meta.Index
  259. }
  260. // GetReceipt returns a receipt by hash
  261. func GetReceipt(db ethdb.Database, txHash common.Hash) *types.Receipt {
  262. data, _ := db.Get(append(receiptsPrefix, txHash[:]...))
  263. if len(data) == 0 {
  264. return nil
  265. }
  266. var receipt types.ReceiptForStorage
  267. err := rlp.DecodeBytes(data, &receipt)
  268. if err != nil {
  269. glog.V(logger.Debug).Infoln("GetReceipt err:", err)
  270. }
  271. return (*types.Receipt)(&receipt)
  272. }
  273. // WriteCanonicalHash stores the canonical hash for the given block number.
  274. func WriteCanonicalHash(db ethdb.Database, hash common.Hash, number uint64) error {
  275. key := append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)
  276. if err := db.Put(key, hash.Bytes()); err != nil {
  277. glog.Fatalf("failed to store number to hash mapping into database: %v", err)
  278. }
  279. return nil
  280. }
  281. // WriteHeadHeaderHash stores the head header's hash.
  282. func WriteHeadHeaderHash(db ethdb.Database, hash common.Hash) error {
  283. if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
  284. glog.Fatalf("failed to store last header's hash into database: %v", err)
  285. }
  286. return nil
  287. }
  288. // WriteHeadBlockHash stores the head block's hash.
  289. func WriteHeadBlockHash(db ethdb.Database, hash common.Hash) error {
  290. if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
  291. glog.Fatalf("failed to store last block's hash into database: %v", err)
  292. }
  293. return nil
  294. }
  295. // WriteHeadFastBlockHash stores the fast head block's hash.
  296. func WriteHeadFastBlockHash(db ethdb.Database, hash common.Hash) error {
  297. if err := db.Put(headFastKey, hash.Bytes()); err != nil {
  298. glog.Fatalf("failed to store last fast block's hash into database: %v", err)
  299. }
  300. return nil
  301. }
  302. // WriteHeader serializes a block header into the database.
  303. func WriteHeader(db ethdb.Database, header *types.Header) error {
  304. data, err := rlp.EncodeToBytes(header)
  305. if err != nil {
  306. return err
  307. }
  308. hash := header.Hash().Bytes()
  309. num := header.Number.Uint64()
  310. encNum := encodeBlockNumber(num)
  311. key := append(blockHashPrefix, hash...)
  312. if err := db.Put(key, encNum); err != nil {
  313. glog.Fatalf("failed to store hash to number mapping into database: %v", err)
  314. }
  315. key = append(append(headerPrefix, encNum...), hash...)
  316. if err := db.Put(key, data); err != nil {
  317. glog.Fatalf("failed to store header into database: %v", err)
  318. }
  319. glog.V(logger.Debug).Infof("stored header #%v [%x…]", header.Number, hash[:4])
  320. return nil
  321. }
  322. // WriteBody serializes the body of a block into the database.
  323. func WriteBody(db ethdb.Database, hash common.Hash, number uint64, body *types.Body) error {
  324. data, err := rlp.EncodeToBytes(body)
  325. if err != nil {
  326. return err
  327. }
  328. return WriteBodyRLP(db, hash, number, data)
  329. }
  330. // WriteBodyRLP writes a serialized body of a block into the database.
  331. func WriteBodyRLP(db ethdb.Database, hash common.Hash, number uint64, rlp rlp.RawValue) error {
  332. key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  333. if err := db.Put(key, rlp); err != nil {
  334. glog.Fatalf("failed to store block body into database: %v", err)
  335. }
  336. glog.V(logger.Debug).Infof("stored block body [%x…]", hash.Bytes()[:4])
  337. return nil
  338. }
  339. // WriteTd serializes the total difficulty of a block into the database.
  340. func WriteTd(db ethdb.Database, hash common.Hash, number uint64, td *big.Int) error {
  341. data, err := rlp.EncodeToBytes(td)
  342. if err != nil {
  343. return err
  344. }
  345. key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...)
  346. if err := db.Put(key, data); err != nil {
  347. glog.Fatalf("failed to store block total difficulty into database: %v", err)
  348. }
  349. glog.V(logger.Debug).Infof("stored block total difficulty [%x…]: %v", hash.Bytes()[:4], td)
  350. return nil
  351. }
  352. // WriteBlock serializes a block into the database, header and body separately.
  353. func WriteBlock(db ethdb.Database, block *types.Block) error {
  354. // Store the body first to retain database consistency
  355. if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
  356. return err
  357. }
  358. // Store the header too, signaling full block ownership
  359. if err := WriteHeader(db, block.Header()); err != nil {
  360. return err
  361. }
  362. return nil
  363. }
  364. // WriteBlockReceipts stores all the transaction receipts belonging to a block
  365. // as a single receipt slice. This is used during chain reorganisations for
  366. // rescheduling dropped transactions.
  367. func WriteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64, receipts types.Receipts) error {
  368. // Convert the receipts into their storage form and serialize them
  369. storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
  370. for i, receipt := range receipts {
  371. storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
  372. }
  373. bytes, err := rlp.EncodeToBytes(storageReceipts)
  374. if err != nil {
  375. return err
  376. }
  377. // Store the flattened receipt slice
  378. key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  379. if err := db.Put(key, bytes); err != nil {
  380. glog.Fatalf("failed to store block receipts into database: %v", err)
  381. }
  382. glog.V(logger.Debug).Infof("stored block receipts [%x…]", hash.Bytes()[:4])
  383. return nil
  384. }
  385. // WriteTransactions stores the transactions associated with a specific block
  386. // into the given database. Beside writing the transaction, the function also
  387. // stores a metadata entry along with the transaction, detailing the position
  388. // of this within the blockchain.
  389. func WriteTransactions(db ethdb.Database, block *types.Block) error {
  390. batch := db.NewBatch()
  391. // Iterate over each transaction and encode it with its metadata
  392. for i, tx := range block.Transactions() {
  393. // Encode and queue up the transaction for storage
  394. data, err := rlp.EncodeToBytes(tx)
  395. if err != nil {
  396. return err
  397. }
  398. if err := batch.Put(tx.Hash().Bytes(), data); err != nil {
  399. return err
  400. }
  401. // Encode and queue up the transaction metadata for storage
  402. meta := struct {
  403. BlockHash common.Hash
  404. BlockIndex uint64
  405. Index uint64
  406. }{
  407. BlockHash: block.Hash(),
  408. BlockIndex: block.NumberU64(),
  409. Index: uint64(i),
  410. }
  411. data, err = rlp.EncodeToBytes(meta)
  412. if err != nil {
  413. return err
  414. }
  415. if err := batch.Put(append(tx.Hash().Bytes(), txMetaSuffix...), data); err != nil {
  416. return err
  417. }
  418. }
  419. // Write the scheduled data into the database
  420. if err := batch.Write(); err != nil {
  421. glog.Fatalf("failed to store transactions into database: %v", err)
  422. }
  423. return nil
  424. }
  425. // WriteReceipt stores a single transaction receipt into the database.
  426. func WriteReceipt(db ethdb.Database, receipt *types.Receipt) error {
  427. storageReceipt := (*types.ReceiptForStorage)(receipt)
  428. data, err := rlp.EncodeToBytes(storageReceipt)
  429. if err != nil {
  430. return err
  431. }
  432. return db.Put(append(receiptsPrefix, receipt.TxHash.Bytes()...), data)
  433. }
  434. // WriteReceipts stores a batch of transaction receipts into the database.
  435. func WriteReceipts(db ethdb.Database, receipts types.Receipts) error {
  436. batch := db.NewBatch()
  437. // Iterate over all the receipts and queue them for database injection
  438. for _, receipt := range receipts {
  439. storageReceipt := (*types.ReceiptForStorage)(receipt)
  440. data, err := rlp.EncodeToBytes(storageReceipt)
  441. if err != nil {
  442. return err
  443. }
  444. if err := batch.Put(append(receiptsPrefix, receipt.TxHash.Bytes()...), data); err != nil {
  445. return err
  446. }
  447. }
  448. // Write the scheduled data into the database
  449. if err := batch.Write(); err != nil {
  450. glog.Fatalf("failed to store receipts into database: %v", err)
  451. }
  452. return nil
  453. }
  454. // DeleteCanonicalHash removes the number to hash canonical mapping.
  455. func DeleteCanonicalHash(db ethdb.Database, number uint64) {
  456. db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
  457. }
  458. // DeleteHeader removes all block header data associated with a hash.
  459. func DeleteHeader(db ethdb.Database, hash common.Hash, number uint64) {
  460. db.Delete(append(blockHashPrefix, hash.Bytes()...))
  461. db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  462. }
  463. // DeleteBody removes all block body data associated with a hash.
  464. func DeleteBody(db ethdb.Database, hash common.Hash, number uint64) {
  465. db.Delete(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  466. }
  467. // DeleteTd removes all block total difficulty data associated with a hash.
  468. func DeleteTd(db ethdb.Database, hash common.Hash, number uint64) {
  469. db.Delete(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...))
  470. }
  471. // DeleteBlock removes all block data associated with a hash.
  472. func DeleteBlock(db ethdb.Database, hash common.Hash, number uint64) {
  473. DeleteBlockReceipts(db, hash, number)
  474. DeleteHeader(db, hash, number)
  475. DeleteBody(db, hash, number)
  476. DeleteTd(db, hash, number)
  477. }
  478. // DeleteBlockReceipts removes all receipt data associated with a block hash.
  479. func DeleteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) {
  480. db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
  481. }
  482. // DeleteTransaction removes all transaction data associated with a hash.
  483. func DeleteTransaction(db ethdb.Database, hash common.Hash) {
  484. db.Delete(hash.Bytes())
  485. db.Delete(append(hash.Bytes(), txMetaSuffix...))
  486. }
  487. // DeleteReceipt removes all receipt data associated with a transaction hash.
  488. func DeleteReceipt(db ethdb.Database, hash common.Hash) {
  489. db.Delete(append(receiptsPrefix, hash.Bytes()...))
  490. }
  491. // [deprecated by the header/block split, remove eventually]
  492. // GetBlockByHashOld returns the old combined block corresponding to the hash
  493. // or nil if not found. This method is only used by the upgrade mechanism to
  494. // access the old combined block representation. It will be dropped after the
  495. // network transitions to eth/63.
  496. func GetBlockByHashOld(db ethdb.Database, hash common.Hash) *types.Block {
  497. data, _ := db.Get(append(oldBlockHashPrefix, hash[:]...))
  498. if len(data) == 0 {
  499. return nil
  500. }
  501. var block types.StorageBlock
  502. if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
  503. glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
  504. return nil
  505. }
  506. return (*types.Block)(&block)
  507. }
  508. // returns a formatted MIP mapped key by adding prefix, canonical number and level
  509. //
  510. // ex. fn(98, 1000) = (prefix || 1000 || 0)
  511. func mipmapKey(num, level uint64) []byte {
  512. lkey := make([]byte, 8)
  513. binary.BigEndian.PutUint64(lkey, level)
  514. key := new(big.Int).SetUint64(num / level * level)
  515. return append(mipmapPre, append(lkey, key.Bytes()...)...)
  516. }
  517. // WriteMapmapBloom writes each address included in the receipts' logs to the
  518. // MIP bloom bin.
  519. func WriteMipmapBloom(db ethdb.Database, number uint64, receipts types.Receipts) error {
  520. mipmapBloomMu.Lock()
  521. defer mipmapBloomMu.Unlock()
  522. batch := db.NewBatch()
  523. for _, level := range MIPMapLevels {
  524. key := mipmapKey(number, level)
  525. bloomDat, _ := db.Get(key)
  526. bloom := types.BytesToBloom(bloomDat)
  527. for _, receipt := range receipts {
  528. for _, log := range receipt.Logs {
  529. bloom.Add(log.Address.Big())
  530. }
  531. }
  532. batch.Put(key, bloom.Bytes())
  533. }
  534. if err := batch.Write(); err != nil {
  535. return fmt.Errorf("mipmap write fail for: %d: %v", number, err)
  536. }
  537. return nil
  538. }
  539. // GetMipmapBloom returns a bloom filter using the number and level as input
  540. // parameters. For available levels see MIPMapLevels.
  541. func GetMipmapBloom(db ethdb.Database, number, level uint64) types.Bloom {
  542. bloomDat, _ := db.Get(mipmapKey(number, level))
  543. return types.BytesToBloom(bloomDat)
  544. }
  545. // PreimageTable returns a Database instance with the key prefix for preimage entries.
  546. func PreimageTable(db ethdb.Database) ethdb.Database {
  547. return ethdb.NewTable(db, preimagePrefix)
  548. }
  549. // WritePreimages writes the provided set of preimages to the database. `number` is the
  550. // current block number, and is used for debug messages only.
  551. func WritePreimages(db ethdb.Database, number uint64, preimages map[common.Hash][]byte) error {
  552. table := PreimageTable(db)
  553. batch := table.NewBatch()
  554. hitCount := 0
  555. for hash, preimage := range preimages {
  556. if _, err := table.Get(hash.Bytes()); err != nil {
  557. batch.Put(hash.Bytes(), preimage)
  558. hitCount += 1
  559. }
  560. }
  561. preimageCounter.Inc(int64(len(preimages)))
  562. preimageHitCounter.Inc(int64(hitCount))
  563. if hitCount > 0 {
  564. if err := batch.Write(); err != nil {
  565. return fmt.Errorf("preimage write fail for block %d: %v", number, err)
  566. }
  567. glog.V(logger.Debug).Infof("%d preimages in block %d, including %d new", len(preimages), number, hitCount)
  568. }
  569. return nil
  570. }
  571. // GetBlockChainVersion reads the version number from db.
  572. func GetBlockChainVersion(db ethdb.Database) int {
  573. var vsn uint
  574. enc, _ := db.Get([]byte("BlockchainVersion"))
  575. rlp.DecodeBytes(enc, &vsn)
  576. return int(vsn)
  577. }
  578. // WriteBlockChainVersion writes vsn as the version number to db.
  579. func WriteBlockChainVersion(db ethdb.Database, vsn int) {
  580. enc, _ := rlp.EncodeToBytes(uint(vsn))
  581. db.Put([]byte("BlockchainVersion"), enc)
  582. }
  583. // WriteChainConfig writes the chain config settings to the database.
  584. func WriteChainConfig(db ethdb.Database, hash common.Hash, cfg *params.ChainConfig) error {
  585. // short circuit and ignore if nil config. GetChainConfig
  586. // will return a default.
  587. if cfg == nil {
  588. return nil
  589. }
  590. jsonChainConfig, err := json.Marshal(cfg)
  591. if err != nil {
  592. return err
  593. }
  594. return db.Put(append(configPrefix, hash[:]...), jsonChainConfig)
  595. }
  596. // GetChainConfig will fetch the network settings based on the given hash.
  597. func GetChainConfig(db ethdb.Database, hash common.Hash) (*params.ChainConfig, error) {
  598. jsonChainConfig, _ := db.Get(append(configPrefix, hash[:]...))
  599. if len(jsonChainConfig) == 0 {
  600. return nil, ChainConfigNotFoundErr
  601. }
  602. var config params.ChainConfig
  603. if err := json.Unmarshal(jsonChainConfig, &config); err != nil {
  604. return nil, err
  605. }
  606. return &config, nil
  607. }
  608. // FindCommonAncestor returns the last common ancestor of two block headers
  609. func FindCommonAncestor(db ethdb.Database, a, b *types.Header) *types.Header {
  610. for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
  611. a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
  612. if a == nil {
  613. return nil
  614. }
  615. }
  616. for an := a.Number.Uint64(); an < b.Number.Uint64(); {
  617. b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1)
  618. if b == nil {
  619. return nil
  620. }
  621. }
  622. for a.Hash() != b.Hash() {
  623. a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
  624. if a == nil {
  625. return nil
  626. }
  627. b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1)
  628. if b == nil {
  629. return nil
  630. }
  631. }
  632. return a
  633. }