schema.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 contains a collection of low level database accessors.
  17. package rawdb
  18. import (
  19. "encoding/binary"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/metrics"
  22. )
  23. // The fields below define the low level database schema prefixing.
  24. var (
  25. // databaseVerisionKey tracks the current database version.
  26. databaseVerisionKey = []byte("DatabaseVersion")
  27. // headHeaderKey tracks the latest know header's hash.
  28. headHeaderKey = []byte("LastHeader")
  29. // headBlockKey tracks the latest know full block's hash.
  30. headBlockKey = []byte("LastBlock")
  31. // headFastBlockKey tracks the latest known incomplete block's hash duirng fast sync.
  32. headFastBlockKey = []byte("LastFast")
  33. // fastTrieProgressKey tracks the number of trie entries imported during fast sync.
  34. fastTrieProgressKey = []byte("TrieSync")
  35. // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
  36. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  37. headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
  38. headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
  39. headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
  40. blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
  41. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  42. txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
  43. bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
  44. preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
  45. configPrefix = []byte("ethereum-config-") // config prefix for the db
  46. // Chain index prefixes (use `i` + single byte to avoid mixing data types).
  47. BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
  48. preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
  49. preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
  50. )
  51. // TxLookupEntry is a positional metadata to help looking up the data content of
  52. // a transaction or receipt given only its hash.
  53. type TxLookupEntry struct {
  54. BlockHash common.Hash
  55. BlockIndex uint64
  56. Index uint64
  57. }
  58. // encodeBlockNumber encodes a block number as big endian uint64
  59. func encodeBlockNumber(number uint64) []byte {
  60. enc := make([]byte, 8)
  61. binary.BigEndian.PutUint64(enc, number)
  62. return enc
  63. }