github.com/bhs-gq/quorum-hotstuff@v21.1.0+incompatible/core/rawdb/database_quorum.go (about)

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