database_quorum.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. // Copyright 2015 The go-ethereum Authors
  3. // This file is part of the go-ethereum library.
  4. //
  5. // The go-ethereum library is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // The go-ethereum library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package rawdb
  19. import (
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. )
  24. var (
  25. privateRootPrefix = []byte("P")
  26. privateStatesTrieRootPrefix = []byte("PSTP")
  27. privateBloomPrefix = []byte("Pb")
  28. quorumEIP155ActivatedPrefix = []byte("quorum155active")
  29. // Quorum
  30. // we introduce a generic approach to store extra data for an account. PrivacyMetadata is wrapped.
  31. // However, this value is kept as-is to support backward compatibility
  32. stateRootToExtraDataRootPrefix = []byte("PSR2PMDR")
  33. // emptyRoot is the known root hash of an empty trie. Duplicate from `trie/trie.go#emptyRoot`
  34. emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
  35. )
  36. //returns whether we have a chain configuration that can't be updated
  37. //after the EIP155 HF has happened
  38. func GetIsQuorumEIP155Activated(db ethdb.KeyValueReader) bool {
  39. data, _ := db.Get(quorumEIP155ActivatedPrefix)
  40. return len(data) == 1
  41. }
  42. // WriteQuorumEIP155Activation writes a flag to the database saying EIP155 HF is enforced
  43. func WriteQuorumEIP155Activation(db ethdb.KeyValueWriter) error {
  44. return db.Put(quorumEIP155ActivatedPrefix, []byte{1})
  45. }
  46. func GetPrivateStateRoot(db ethdb.Database, blockRoot common.Hash) common.Hash {
  47. root, _ := db.Get(append(privateRootPrefix, blockRoot[:]...))
  48. return common.BytesToHash(root)
  49. }
  50. func GetPrivateStatesTrieRoot(db ethdb.Database, blockRoot common.Hash) common.Hash {
  51. root, _ := db.Get(append(privateStatesTrieRootPrefix, blockRoot[:]...))
  52. return common.BytesToHash(root)
  53. }
  54. func GetAccountExtraDataRoot(db ethdb.KeyValueReader, stateRoot common.Hash) common.Hash {
  55. root, _ := db.Get(append(stateRootToExtraDataRootPrefix, stateRoot[:]...))
  56. return common.BytesToHash(root)
  57. }
  58. func WritePrivateStateRoot(db ethdb.Database, blockRoot, root common.Hash) error {
  59. return db.Put(append(privateRootPrefix, blockRoot[:]...), root[:])
  60. }
  61. func WritePrivateStatesTrieRoot(db ethdb.Database, blockRoot, root common.Hash) error {
  62. return db.Put(append(privateStatesTrieRootPrefix, blockRoot[:]...), root[:])
  63. }
  64. // WriteRootHashMapping stores the mapping between root hash of state trie and
  65. // root hash of state.AccountExtraData trie to persistent storage
  66. func WriteRootHashMapping(db ethdb.KeyValueWriter, stateRoot, extraDataRoot common.Hash) error {
  67. return db.Put(append(stateRootToExtraDataRootPrefix, stateRoot[:]...), extraDataRoot[:])
  68. }
  69. // WritePrivateBlockBloom creates a bloom filter for the given receipts and saves it to the database
  70. // with the number given as identifier (i.e. block number).
  71. func WritePrivateBlockBloom(db ethdb.Database, number uint64, receipts types.Receipts) error {
  72. rbloom := types.CreateBloom(receipts.Flatten())
  73. return db.Put(append(privateBloomPrefix, encodeBlockNumber(number)...), rbloom[:])
  74. }
  75. // GetPrivateBlockBloom retrieves the private bloom associated with the given number.
  76. func GetPrivateBlockBloom(db ethdb.Database, number uint64) (bloom types.Bloom) {
  77. data, _ := db.Get(append(privateBloomPrefix, encodeBlockNumber(number)...))
  78. if len(data) > 0 {
  79. bloom = types.BytesToBloom(data)
  80. }
  81. return bloom
  82. }
  83. // AccountExtraDataLinker maintains mapping between root hash of the state trie
  84. // and root hash of state.AccountExtraData trie
  85. type AccountExtraDataLinker interface {
  86. // GetAccountExtraDataRoot returns the root hash of the state.AccountExtraData trie from
  87. // the given root hash of the state trie.
  88. //
  89. // It returns an empty hash if not found.
  90. GetAccountExtraDataRoot(stateRoot common.Hash) common.Hash
  91. // Link saves the mapping between root hash of the state trie and
  92. // root hash of state.AccountExtraData trie to the persistent storage.
  93. // Don't write the mapping if extraDataRoot is an emptyRoot
  94. Link(stateRoot, extraDataRoot common.Hash) error
  95. }
  96. // ethdbAccountExtraDataLinker implements AccountExtraDataLinker using ethdb.Database
  97. // as the persistence storage
  98. type ethdbAccountExtraDataLinker struct {
  99. db ethdb.Database
  100. }
  101. func NewAccountExtraDataLinker(db ethdb.Database) AccountExtraDataLinker {
  102. return &ethdbAccountExtraDataLinker{
  103. db: db,
  104. }
  105. }
  106. func (pml *ethdbAccountExtraDataLinker) GetAccountExtraDataRoot(stateRoot common.Hash) common.Hash {
  107. return GetAccountExtraDataRoot(pml.db, stateRoot)
  108. }
  109. func (pml *ethdbAccountExtraDataLinker) Link(stateRoot, extraDataRoot common.Hash) error {
  110. if extraDataRoot != emptyRoot {
  111. return WriteRootHashMapping(pml.db, stateRoot, extraDataRoot)
  112. }
  113. return nil
  114. }