github.com/ConsenSys/Quorum@v20.10.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  	privateRootToPrivacyMetadataRootPrefix = []byte("PSR2PMDR")
    34  	// emptyRoot is the known root hash of an empty trie. Duplicate from `trie/trie.go#emptyRoot`
    35  	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    36  )
    37  
    38  //returns whether we have a chain configuration that can't be updated
    39  //after the EIP155 HF has happened
    40  func GetIsQuorumEIP155Activated(db ethdb.KeyValueReader) bool {
    41  	data, _ := db.Get(quorumEIP155ActivatedPrefix)
    42  	return len(data) == 1
    43  }
    44  
    45  // WriteQuorumEIP155Activation writes a flag to the database saying EIP155 HF is enforced
    46  func WriteQuorumEIP155Activation(db ethdb.KeyValueWriter) error {
    47  	return db.Put(quorumEIP155ActivatedPrefix, []byte{1})
    48  }
    49  
    50  func GetPrivateStateRoot(db ethdb.Database, blockRoot common.Hash) common.Hash {
    51  	root, _ := db.Get(append(privateRootPrefix, blockRoot[:]...))
    52  	return common.BytesToHash(root)
    53  }
    54  
    55  func GetPrivacyMetadataStateRootForPrivateStateRoot(db ethdb.KeyValueReader, privateStateRoot common.Hash) common.Hash {
    56  	root, _ := db.Get(append(privateRootToPrivacyMetadataRootPrefix, privateStateRoot[:]...))
    57  	return common.BytesToHash(root)
    58  }
    59  
    60  func WritePrivateStateRoot(db ethdb.Database, blockRoot, root common.Hash) error {
    61  	return db.Put(append(privateRootPrefix, blockRoot[:]...), root[:])
    62  }
    63  
    64  func WritePrivacyMetadataStateRootForPrivateStateRoot(db ethdb.KeyValueWriter, privateStateRoot, privacyMetadataRoot common.Hash) error {
    65  	return db.Put(append(privateRootToPrivacyMetadataRootPrefix, privateStateRoot[:]...), privacyMetadataRoot[:])
    66  }
    67  
    68  // WritePrivateBlockBloom creates a bloom filter for the given receipts and saves it to the database
    69  // with the number given as identifier (i.e. block number).
    70  func WritePrivateBlockBloom(db ethdb.Database, number uint64, receipts types.Receipts) error {
    71  	rbloom := types.CreateBloom(receipts)
    72  	return db.Put(append(privateBloomPrefix, encodeBlockNumber(number)...), rbloom[:])
    73  }
    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  
    84  type PrivacyMetadataLinker interface {
    85  	PrivacyMetadataRootForPrivateStateRoot(privateStateRoot common.Hash) common.Hash
    86  	LinkPrivacyMetadataRootToPrivateStateRoot(privateStateRoot, privacyMetadataRoot common.Hash) error
    87  }
    88  
    89  type ethDBPrivacyMetadataLinker struct {
    90  	db ethdb.Database
    91  }
    92  
    93  func NewPrivacyMetadataLinker(db ethdb.Database) PrivacyMetadataLinker {
    94  	return &ethDBPrivacyMetadataLinker{
    95  		db: db,
    96  	}
    97  }
    98  
    99  func (pml *ethDBPrivacyMetadataLinker) PrivacyMetadataRootForPrivateStateRoot(privateStateRoot common.Hash) common.Hash {
   100  	return GetPrivacyMetadataStateRootForPrivateStateRoot(pml.db, privateStateRoot)
   101  }
   102  
   103  func (pml *ethDBPrivacyMetadataLinker) LinkPrivacyMetadataRootToPrivateStateRoot(privateStateRoot, privacyMetadataRoot common.Hash) error {
   104  	if privacyMetadataRoot != emptyRoot {
   105  		return WritePrivacyMetadataStateRootForPrivateStateRoot(pml.db, privateStateRoot, privacyMetadataRoot)
   106  	}
   107  	return nil
   108  }