schema.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // lastPivotKey tracks the last pivot block used by fast sync (to reenable on sethead).
  35. lastPivotKey = []byte("LastPivot")
  36. // fastTrieProgressKey tracks the number of trie entries imported during fast sync.
  37. fastTrieProgressKey = []byte("TrieSync")
  38. // snapshotRootKey tracks the hash of the last snapshot.
  39. snapshotRootKey = []byte("SnapshotRoot")
  40. // snapshotJournalKey tracks the in-memory diff layers across restarts.
  41. snapshotJournalKey = []byte("SnapshotJournal")
  42. // snapshotGeneratorKey tracks the snapshot generation marker across restarts.
  43. snapshotGeneratorKey = []byte("SnapshotGenerator")
  44. // snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
  45. snapshotRecoveryKey = []byte("SnapshotRecovery")
  46. // snapshotSyncStatusKey tracks the snapshot sync status across restarts.
  47. snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
  48. // txIndexTailKey tracks the oldest block whose transactions have been indexed.
  49. txIndexTailKey = []byte("TransactionIndexTail")
  50. // fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
  51. fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
  52. // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
  53. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  54. headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
  55. headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
  56. headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
  57. blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
  58. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  59. txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
  60. bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
  61. SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
  62. SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
  63. codePrefix = []byte("c") // codePrefix + code hash -> account code
  64. preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
  65. configPrefix = []byte("ethereum-config-") // config prefix for the db
  66. uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
  67. // Chain index prefixes (use `i` + single byte to avoid mixing data types).
  68. BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
  69. preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
  70. preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
  71. )
  72. const (
  73. // freezerHeaderTable indicates the name of the freezer header table.
  74. freezerHeaderTable = "headers"
  75. // freezerHashTable indicates the name of the freezer canonical hash table.
  76. freezerHashTable = "hashes"
  77. // freezerBodiesTable indicates the name of the freezer block body table.
  78. freezerBodiesTable = "bodies"
  79. // freezerReceiptTable indicates the name of the freezer receipts table.
  80. freezerReceiptTable = "receipts"
  81. // freezerDifficultyTable indicates the name of the freezer total difficulty table.
  82. freezerDifficultyTable = "diffs"
  83. )
  84. // freezerNoSnappy configures whether compression is disabled for the ancient-tables.
  85. // Hashes and difficulties don't compress well.
  86. var freezerNoSnappy = map[string]bool{
  87. freezerHeaderTable: false,
  88. freezerHashTable: true,
  89. freezerBodiesTable: false,
  90. freezerReceiptTable: false,
  91. freezerDifficultyTable: true,
  92. }
  93. // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
  94. // fields.
  95. type LegacyTxLookupEntry struct {
  96. BlockHash common.Hash
  97. BlockIndex uint64
  98. Index uint64
  99. }
  100. // encodeBlockNumber encodes a block number as big endian uint64
  101. func encodeBlockNumber(number uint64) []byte {
  102. enc := make([]byte, 8)
  103. binary.BigEndian.PutUint64(enc, number)
  104. return enc
  105. }
  106. // headerKeyPrefix = headerPrefix + num (uint64 big endian)
  107. func headerKeyPrefix(number uint64) []byte {
  108. return append(headerPrefix, encodeBlockNumber(number)...)
  109. }
  110. // headerKey = headerPrefix + num (uint64 big endian) + hash
  111. func headerKey(number uint64, hash common.Hash) []byte {
  112. return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  113. }
  114. // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
  115. func headerTDKey(number uint64, hash common.Hash) []byte {
  116. return append(headerKey(number, hash), headerTDSuffix...)
  117. }
  118. // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
  119. func headerHashKey(number uint64) []byte {
  120. return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
  121. }
  122. // headerNumberKey = headerNumberPrefix + hash
  123. func headerNumberKey(hash common.Hash) []byte {
  124. return append(headerNumberPrefix, hash.Bytes()...)
  125. }
  126. // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
  127. func blockBodyKey(number uint64, hash common.Hash) []byte {
  128. return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  129. }
  130. // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
  131. func blockReceiptsKey(number uint64, hash common.Hash) []byte {
  132. return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  133. }
  134. // txLookupKey = txLookupPrefix + hash
  135. func txLookupKey(hash common.Hash) []byte {
  136. return append(txLookupPrefix, hash.Bytes()...)
  137. }
  138. // accountSnapshotKey = SnapshotAccountPrefix + hash
  139. func accountSnapshotKey(hash common.Hash) []byte {
  140. return append(SnapshotAccountPrefix, hash.Bytes()...)
  141. }
  142. // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
  143. func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
  144. return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
  145. }
  146. // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
  147. func storageSnapshotsKey(accountHash common.Hash) []byte {
  148. return append(SnapshotStoragePrefix, accountHash.Bytes()...)
  149. }
  150. // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
  151. func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
  152. key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
  153. binary.BigEndian.PutUint16(key[1:], uint16(bit))
  154. binary.BigEndian.PutUint64(key[3:], section)
  155. return key
  156. }
  157. // preimageKey = preimagePrefix + hash
  158. func preimageKey(hash common.Hash) []byte {
  159. return append(preimagePrefix, hash.Bytes()...)
  160. }
  161. // codeKey = codePrefix + hash
  162. func codeKey(hash common.Hash) []byte {
  163. return append(codePrefix, hash.Bytes()...)
  164. }
  165. // IsCodeKey reports whether the given byte slice is the key of contract code,
  166. // if so return the raw code hash as well.
  167. func IsCodeKey(key []byte) (bool, []byte) {
  168. if bytes.HasPrefix(key, codePrefix) && len(key) == common.HashLength+len(codePrefix) {
  169. return true, key[len(codePrefix):]
  170. }
  171. return false, nil
  172. }
  173. // configKey = configPrefix + hash
  174. func configKey(hash common.Hash) []byte {
  175. return append(configPrefix, hash.Bytes()...)
  176. }