schema.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. "bytes"
  20. "encoding/binary"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/metrics"
  23. )
  24. // The fields below define the low level database schema prefixing.
  25. var (
  26. // databaseVersionKey tracks the current database version.
  27. databaseVersionKey = []byte("DatabaseVersion")
  28. // headHeaderKey tracks the latest known header's hash.
  29. headHeaderKey = []byte("LastHeader")
  30. // headBlockKey tracks the latest known full block's hash.
  31. headBlockKey = []byte("LastBlock")
  32. // headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
  33. headFastBlockKey = []byte("LastFast")
  34. // headFinalizedBlockKey tracks the latest known finalized block hash.
  35. headFinalizedBlockKey = []byte("LastFinalized")
  36. // lastPivotKey tracks the last pivot block used by fast sync (to reenable on sethead).
  37. lastPivotKey = []byte("LastPivot")
  38. // fastTrieProgressKey tracks the number of trie entries imported during fast sync.
  39. fastTrieProgressKey = []byte("TrieSync")
  40. // snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync.
  41. snapshotDisabledKey = []byte("SnapshotDisabled")
  42. // SnapshotRootKey tracks the hash of the last snapshot.
  43. SnapshotRootKey = []byte("SnapshotRoot")
  44. // snapshotJournalKey tracks the in-memory diff layers across restarts.
  45. snapshotJournalKey = []byte("SnapshotJournal")
  46. // snapshotGeneratorKey tracks the snapshot generation marker across restarts.
  47. snapshotGeneratorKey = []byte("SnapshotGenerator")
  48. // snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
  49. snapshotRecoveryKey = []byte("SnapshotRecovery")
  50. // snapshotSyncStatusKey tracks the snapshot sync status across restarts.
  51. snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
  52. // skeletonSyncStatusKey tracks the skeleton sync status across restarts.
  53. skeletonSyncStatusKey = []byte("SkeletonSyncStatus")
  54. // txIndexTailKey tracks the oldest block whose transactions have been indexed.
  55. txIndexTailKey = []byte("TransactionIndexTail")
  56. // fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
  57. fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
  58. // badBlockKey tracks the list of bad blocks seen by local
  59. badBlockKey = []byte("InvalidBlock")
  60. // uncleanShutdownKey tracks the list of local crashes
  61. uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
  62. // transitionStatusKey tracks the eth2 transition status.
  63. transitionStatusKey = []byte("eth2-transition")
  64. // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
  65. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  66. headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
  67. headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
  68. headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
  69. blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
  70. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  71. txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
  72. bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
  73. SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
  74. SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
  75. CodePrefix = []byte("c") // CodePrefix + code hash -> account code
  76. skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header
  77. PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage
  78. configPrefix = []byte("ethereum-config-") // config prefix for the db
  79. genesisPrefix = []byte("ethereum-genesis-") // genesis state prefix for the db
  80. // Chain index prefixes (use `i` + single byte to avoid mixing data types).
  81. BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
  82. preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
  83. preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
  84. )
  85. // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
  86. // fields.
  87. type LegacyTxLookupEntry struct {
  88. BlockHash common.Hash
  89. BlockIndex uint64
  90. Index uint64
  91. }
  92. // encodeBlockNumber encodes a block number as big endian uint64
  93. func encodeBlockNumber(number uint64) []byte {
  94. enc := make([]byte, 8)
  95. binary.BigEndian.PutUint64(enc, number)
  96. return enc
  97. }
  98. // headerKeyPrefix = headerPrefix + num (uint64 big endian)
  99. func headerKeyPrefix(number uint64) []byte {
  100. return append(headerPrefix, encodeBlockNumber(number)...)
  101. }
  102. // headerKey = headerPrefix + num (uint64 big endian) + hash
  103. func headerKey(number uint64, hash common.Hash) []byte {
  104. return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  105. }
  106. // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
  107. func headerTDKey(number uint64, hash common.Hash) []byte {
  108. return append(headerKey(number, hash), headerTDSuffix...)
  109. }
  110. // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
  111. func headerHashKey(number uint64) []byte {
  112. return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
  113. }
  114. // headerNumberKey = headerNumberPrefix + hash
  115. func headerNumberKey(hash common.Hash) []byte {
  116. return append(headerNumberPrefix, hash.Bytes()...)
  117. }
  118. // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
  119. func blockBodyKey(number uint64, hash common.Hash) []byte {
  120. return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  121. }
  122. // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
  123. func blockReceiptsKey(number uint64, hash common.Hash) []byte {
  124. return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  125. }
  126. // txLookupKey = txLookupPrefix + hash
  127. func txLookupKey(hash common.Hash) []byte {
  128. return append(txLookupPrefix, hash.Bytes()...)
  129. }
  130. // accountSnapshotKey = SnapshotAccountPrefix + hash
  131. func accountSnapshotKey(hash common.Hash) []byte {
  132. return append(SnapshotAccountPrefix, hash.Bytes()...)
  133. }
  134. // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
  135. func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
  136. return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
  137. }
  138. // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
  139. func storageSnapshotsKey(accountHash common.Hash) []byte {
  140. return append(SnapshotStoragePrefix, accountHash.Bytes()...)
  141. }
  142. // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
  143. func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
  144. key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
  145. binary.BigEndian.PutUint16(key[1:], uint16(bit))
  146. binary.BigEndian.PutUint64(key[3:], section)
  147. return key
  148. }
  149. // skeletonHeaderKey = skeletonHeaderPrefix + num (uint64 big endian)
  150. func skeletonHeaderKey(number uint64) []byte {
  151. return append(skeletonHeaderPrefix, encodeBlockNumber(number)...)
  152. }
  153. // preimageKey = PreimagePrefix + hash
  154. func preimageKey(hash common.Hash) []byte {
  155. return append(PreimagePrefix, hash.Bytes()...)
  156. }
  157. // codeKey = CodePrefix + hash
  158. func codeKey(hash common.Hash) []byte {
  159. return append(CodePrefix, hash.Bytes()...)
  160. }
  161. // IsCodeKey reports whether the given byte slice is the key of contract code,
  162. // if so return the raw code hash as well.
  163. func IsCodeKey(key []byte) (bool, []byte) {
  164. if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
  165. return true, key[len(CodePrefix):]
  166. }
  167. return false, nil
  168. }
  169. // configKey = configPrefix + hash
  170. func configKey(hash common.Hash) []byte {
  171. return append(configPrefix, hash.Bytes()...)
  172. }
  173. // genesisStateSpecKey = genesisPrefix + hash
  174. func genesisStateSpecKey(hash common.Hash) []byte {
  175. return append(genesisPrefix, hash.Bytes()...)
  176. }