github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/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  	privateStatesTrieRootPrefix = []byte("PSTP")
    32  	privateBloomPrefix          = []byte("Pb")
    33  	quorumEIP155ActivatedPrefix = []byte("quorum155active")
    34  	// Quorum
    35  	// we introduce a generic approach to store extra data for an account. PrivacyMetadata is wrapped.
    36  	// However, this value is kept as-is to support backward compatibility
    37  	stateRootToExtraDataRootPrefix = []byte("PSR2PMDR")
    38  	// emptyRoot is the known root hash of an empty trie. Duplicate from `trie/trie.go#emptyRoot`
    39  	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    40  )
    41  
    42  //returns whether we have a chain configuration that can't be updated
    43  //after the EIP155 HF has happened
    44  func GetIsQuorumEIP155Activated(db ethdb.KeyValueReader) bool {
    45  	data, _ := db.Get(quorumEIP155ActivatedPrefix)
    46  	return len(data) == 1
    47  }
    48  
    49  // WriteQuorumEIP155Activation writes a flag to the database saying EIP155 HF is enforced
    50  func WriteQuorumEIP155Activation(db ethdb.KeyValueWriter) error {
    51  	return db.Put(quorumEIP155ActivatedPrefix, []byte{1})
    52  }
    53  
    54  func GetPrivateStateRoot(db ethdb.Database, blockRoot common.Hash) common.Hash {
    55  	root, _ := db.Get(append(privateRootPrefix, blockRoot[:]...))
    56  	return common.BytesToHash(root)
    57  }
    58  
    59  func GetPrivateStatesTrieRoot(db ethdb.Database, blockRoot common.Hash) common.Hash {
    60  	root, _ := db.Get(append(privateStatesTrieRootPrefix, blockRoot[:]...))
    61  	return common.BytesToHash(root)
    62  }
    63  
    64  func GetAccountExtraDataRoot(db ethdb.KeyValueReader, stateRoot common.Hash) common.Hash {
    65  	root, _ := db.Get(append(stateRootToExtraDataRootPrefix, stateRoot[:]...))
    66  	return common.BytesToHash(root)
    67  }
    68  
    69  func WritePrivateStateRoot(db ethdb.Database, blockRoot, root common.Hash) error {
    70  	return db.Put(append(privateRootPrefix, blockRoot[:]...), root[:])
    71  }
    72  
    73  func WritePrivateStatesTrieRoot(db ethdb.Database, blockRoot, root common.Hash) error {
    74  	return db.Put(append(privateStatesTrieRootPrefix, blockRoot[:]...), root[:])
    75  }
    76  
    77  // WriteRootHashMapping stores the mapping between root hash of state trie and
    78  // root hash of state.AccountExtraData trie to persistent storage
    79  func WriteRootHashMapping(db ethdb.KeyValueWriter, stateRoot, extraDataRoot common.Hash) error {
    80  	return db.Put(append(stateRootToExtraDataRootPrefix, stateRoot[:]...), extraDataRoot[:])
    81  }
    82  
    83  // WritePrivateBlockBloom creates a bloom filter for the given receipts and saves it to the database
    84  // with the number given as identifier (i.e. block number).
    85  func WritePrivateBlockBloom(db ethdb.Database, number uint64, receipts types.Receipts) error {
    86  	rbloom := types.CreateBloom(receipts.Flatten())
    87  	return db.Put(append(privateBloomPrefix, encodeBlockNumber(number)...), rbloom[:])
    88  }
    89  
    90  // GetPrivateBlockBloom retrieves the private bloom associated with the given number.
    91  func GetPrivateBlockBloom(db ethdb.Database, number uint64) (bloom types.Bloom) {
    92  	data, _ := db.Get(append(privateBloomPrefix, encodeBlockNumber(number)...))
    93  	if len(data) > 0 {
    94  		bloom = types.BytesToBloom(data)
    95  	}
    96  	return bloom
    97  }
    98  
    99  // AccountExtraDataLinker maintains mapping between root hash of the state trie
   100  // and root hash of state.AccountExtraData trie
   101  type AccountExtraDataLinker interface {
   102  	// GetAccountExtraDataRoot returns the root hash of the state.AccountExtraData trie from
   103  	// the given root hash of the state trie.
   104  	//
   105  	// It returns an empty hash if not found.
   106  	GetAccountExtraDataRoot(stateRoot common.Hash) common.Hash
   107  	// Link saves the mapping between root hash of the state trie and
   108  	// root hash of state.AccountExtraData trie to the persistent storage.
   109  	// Don't write the mapping if extraDataRoot is an emptyRoot
   110  	Link(stateRoot, extraDataRoot common.Hash) error
   111  }
   112  
   113  // ethdbAccountExtraDataLinker implements AccountExtraDataLinker using ethdb.Database
   114  // as the persistence storage
   115  type ethdbAccountExtraDataLinker struct {
   116  	db ethdb.Database
   117  }
   118  
   119  func NewAccountExtraDataLinker(db ethdb.Database) AccountExtraDataLinker {
   120  	return &ethdbAccountExtraDataLinker{
   121  		db: db,
   122  	}
   123  }
   124  
   125  func (pml *ethdbAccountExtraDataLinker) GetAccountExtraDataRoot(stateRoot common.Hash) common.Hash {
   126  	return GetAccountExtraDataRoot(pml.db, stateRoot)
   127  }
   128  
   129  func (pml *ethdbAccountExtraDataLinker) Link(stateRoot, extraDataRoot common.Hash) error {
   130  	if extraDataRoot != emptyRoot {
   131  		return WriteRootHashMapping(pml.db, stateRoot, extraDataRoot)
   132  	}
   133  	return nil
   134  }