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

     1  // Copyright 2015 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  
    17  package rawdb
    18  
    19  import (
    20  	"errors"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/ethdb/memorydb"
    25  )
    26  
    27  // Tests that setting the flag for Quorum EIP155 activation read values correctly
    28  func TestIsQuorumEIP155Active(t *testing.T) {
    29  	db := NewMemoryDatabase()
    30  
    31  	isQuorumEIP155Active := GetIsQuorumEIP155Activated(db)
    32  	if isQuorumEIP155Active {
    33  		t.Fatal("Quorum EIP155 active read to be set, but wasn't set beforehand")
    34  	}
    35  
    36  	dbSet := NewMemoryDatabase()
    37  	err := WriteQuorumEIP155Activation(dbSet)
    38  
    39  	if err != nil {
    40  		t.Fatal("unable to write quorum EIP155 activation")
    41  	}
    42  
    43  	isQuorumEIP155ActiveAfterSetting := GetIsQuorumEIP155Activated(dbSet)
    44  	if !isQuorumEIP155ActiveAfterSetting {
    45  		t.Fatal("Quorum EIP155 active read to be unset, but was set beforehand")
    46  	}
    47  }
    48  
    49  func TestAccountExtraDataLinker_whenLinkingEmptyRoot(t *testing.T) {
    50  	db := NewMemoryDatabase()
    51  	psr := common.Hash{1}
    52  
    53  	linker := NewAccountExtraDataLinker(db)
    54  
    55  	err := linker.Link(psr, emptyRoot)
    56  
    57  	if err != nil {
    58  		t.Fatal("unable to store the link")
    59  	}
    60  
    61  	value, _ := db.Get(append(stateRootToExtraDataRootPrefix, psr[:]...))
    62  
    63  	if value != nil {
    64  		t.Fatal("the mapping should not have been stored")
    65  	}
    66  }
    67  
    68  func TestAccountExtraDataLinker_whenLinkingRoots(t *testing.T) {
    69  	db := NewMemoryDatabase()
    70  	psr := common.Hash{1}
    71  	pmr := common.Hash{2}
    72  
    73  	linker := NewAccountExtraDataLinker(db)
    74  
    75  	err := linker.Link(psr, pmr)
    76  
    77  	if err != nil {
    78  		t.Fatal("unable to store the link")
    79  	}
    80  
    81  	value, _ := db.Get(append(stateRootToExtraDataRootPrefix, psr[:]...))
    82  
    83  	if value == nil {
    84  		t.Fatal("the mapping should have been stored")
    85  	}
    86  
    87  	valueHash := common.BytesToHash(value)
    88  
    89  	if pmr != valueHash {
    90  		t.Fatal("the privacy metadata root does not have the expected value")
    91  	}
    92  }
    93  
    94  var errReadOnly = errors.New("unable to write")
    95  
    96  type ReadOnlyDB struct {
    97  	memorydb.Database
    98  }
    99  
   100  func (t *ReadOnlyDB) Put(key []byte, value []byte) error {
   101  	return errReadOnly
   102  }
   103  
   104  func TestAccountExtraDataLinker_whenError(t *testing.T) {
   105  	db := NewDatabase(&ReadOnlyDB{})
   106  	psr := common.Hash{1}
   107  	pmr := common.Hash{2}
   108  
   109  	linker := NewAccountExtraDataLinker(db)
   110  
   111  	err := linker.Link(psr, pmr)
   112  
   113  	if err == nil {
   114  		t.Fatal("expecting a read only error to be returned")
   115  	}
   116  
   117  	if err != errReadOnly {
   118  		t.Fatal("expecting the read only error to be returned")
   119  	}
   120  }
   121  
   122  func TestAccountExtraDataLinker_whenFinding(t *testing.T) {
   123  	db := NewMemoryDatabase()
   124  	psr := common.Hash{1}
   125  	pmr := common.Hash{2}
   126  
   127  	err := db.Put(append(stateRootToExtraDataRootPrefix, psr[:]...), pmr[:])
   128  
   129  	if err != nil {
   130  		t.Fatal("unable to write to db")
   131  	}
   132  
   133  	pml := NewAccountExtraDataLinker(db)
   134  
   135  	pmrRetrieved := pml.GetAccountExtraDataRoot(psr)
   136  
   137  	if pmrRetrieved != pmr {
   138  		t.Fatal("the mapping should have been retrieved")
   139  	}
   140  }
   141  
   142  func TestAccountExtraDataLinker_whenNotFound(t *testing.T) {
   143  	db := NewMemoryDatabase()
   144  	psr := common.Hash{1}
   145  
   146  	pml := NewAccountExtraDataLinker(db)
   147  
   148  	pmrRetrieved := pml.GetAccountExtraDataRoot(psr)
   149  
   150  	if !common.EmptyHash(pmrRetrieved) {
   151  		t.Fatal("the retrieved privacy metadata root should be the empty hash")
   152  	}
   153  }