schema.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 known header's hash.
  28. headHeaderKey = []byte("LastHeader")
  29. // headBlockKey tracks the latest known full block's hash.
  30. headBlockKey = []byte("LastBlock")
  31. // headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
  32. headFastBlockKey = []byte("LastFast")
  33. // fastTrieProgressKey tracks the number of trie entries imported during fast sync.
  34. fastTrieProgressKey = []byte("TrieSync")
  35. // snapshotRootKey tracks the number and hash of the last snapshot.
  36. snapshotRootKey = []byte("SnapshotRoot")
  37. // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
  38. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  39. headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
  40. headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
  41. headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
  42. blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
  43. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  44. txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
  45. bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
  46. SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
  47. SnapshotStoragePrefix = []byte("s") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
  48. preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
  49. configPrefix = []byte("ethereum-config-") // config prefix for the db
  50. // Chain index prefixes (use `i` + single byte to avoid mixing data types).
  51. BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
  52. preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
  53. preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
  54. )
  55. const (
  56. // freezerHeaderTable indicates the name of the freezer header table.
  57. freezerHeaderTable = "headers"
  58. // freezerHashTable indicates the name of the freezer canonical hash table.
  59. freezerHashTable = "hashes"
  60. // freezerBodiesTable indicates the name of the freezer block body table.
  61. freezerBodiesTable = "bodies"
  62. // freezerReceiptTable indicates the name of the freezer receipts table.
  63. freezerReceiptTable = "receipts"
  64. // freezerDifficultyTable indicates the name of the freezer total difficulty table.
  65. freezerDifficultyTable = "diffs"
  66. )
  67. // freezerNoSnappy configures whether compression is disabled for the ancient-tables.
  68. // Hashes and difficulties don't compress well.
  69. var freezerNoSnappy = map[string]bool{
  70. freezerHeaderTable: false,
  71. freezerHashTable: true,
  72. freezerBodiesTable: false,
  73. freezerReceiptTable: false,
  74. freezerDifficultyTable: true,
  75. }
  76. // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
  77. // fields.
  78. type LegacyTxLookupEntry struct {
  79. BlockHash common.Hash
  80. BlockIndex uint64
  81. Index uint64
  82. }
  83. // encodeBlockNumber encodes a block number as big endian uint64
  84. func encodeBlockNumber(number uint64) []byte {
  85. enc := make([]byte, 8)
  86. binary.BigEndian.PutUint64(enc, number)
  87. return enc
  88. }
  89. // headerKeyPrefix = headerPrefix + num (uint64 big endian)
  90. func headerKeyPrefix(number uint64) []byte {
  91. return append(headerPrefix, encodeBlockNumber(number)...)
  92. }
  93. // headerKey = headerPrefix + num (uint64 big endian) + hash
  94. func headerKey(number uint64, hash common.Hash) []byte {
  95. return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  96. }
  97. // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
  98. func headerTDKey(number uint64, hash common.Hash) []byte {
  99. return append(headerKey(number, hash), headerTDSuffix...)
  100. }
  101. // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
  102. func headerHashKey(number uint64) []byte {
  103. return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
  104. }
  105. // headerNumberKey = headerNumberPrefix + hash
  106. func headerNumberKey(hash common.Hash) []byte {
  107. return append(headerNumberPrefix, hash.Bytes()...)
  108. }
  109. // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
  110. func blockBodyKey(number uint64, hash common.Hash) []byte {
  111. return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  112. }
  113. // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
  114. func blockReceiptsKey(number uint64, hash common.Hash) []byte {
  115. return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  116. }
  117. // txLookupKey = txLookupPrefix + hash
  118. func txLookupKey(hash common.Hash) []byte {
  119. return append(txLookupPrefix, hash.Bytes()...)
  120. }
  121. // accountSnapshotKey = SnapshotAccountPrefix + hash
  122. func accountSnapshotKey(hash common.Hash) []byte {
  123. return append(SnapshotAccountPrefix, hash.Bytes()...)
  124. }
  125. // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
  126. func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
  127. return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
  128. }
  129. // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
  130. func storageSnapshotsKey(accountHash common.Hash) []byte {
  131. return append(SnapshotStoragePrefix, accountHash.Bytes()...)
  132. }
  133. // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
  134. func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
  135. key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
  136. binary.BigEndian.PutUint16(key[1:], uint16(bit))
  137. binary.BigEndian.PutUint64(key[3:], section)
  138. return key
  139. }
  140. // preimageKey = preimagePrefix + hash
  141. func preimageKey(hash common.Hash) []byte {
  142. return append(preimagePrefix, hash.Bytes()...)
  143. }
  144. // configKey = configPrefix + hash
  145. func configKey(hash common.Hash) []byte {
  146. return append(configPrefix, hash.Bytes()...)
  147. }