github.com/Unheilbar/quorum@v1.0.0/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  	"testing"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/ethdb/memorydb"
    24  	"github.com/stretchr/testify/assert"
    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  type ReadOnlyDB struct {
    95  	memorydb.Database
    96  }
    97  
    98  func (t *ReadOnlyDB) Put(key []byte, value []byte) error {
    99  	return errReadOnly
   100  }
   101  
   102  func TestAccountExtraDataLinker_whenError(t *testing.T) {
   103  	db := NewDatabase(&ReadOnlyDB{})
   104  	psr := common.Hash{1}
   105  	pmr := common.Hash{2}
   106  
   107  	linker := NewAccountExtraDataLinker(db)
   108  
   109  	err := linker.Link(psr, pmr)
   110  
   111  	if err == nil {
   112  		t.Fatal("expecting a read only error to be returned")
   113  	}
   114  
   115  	if err != errReadOnly {
   116  		t.Fatal("expecting the read only error to be returned")
   117  	}
   118  }
   119  
   120  func TestAccountExtraDataLinker_whenFinding(t *testing.T) {
   121  	db := NewMemoryDatabase()
   122  	psr := common.Hash{1}
   123  	pmr := common.Hash{2}
   124  
   125  	err := db.Put(append(stateRootToExtraDataRootPrefix, psr[:]...), pmr[:])
   126  
   127  	if err != nil {
   128  		t.Fatal("unable to write to db")
   129  	}
   130  
   131  	pml := NewAccountExtraDataLinker(db)
   132  
   133  	pmrRetrieved := pml.GetAccountExtraDataRoot(psr)
   134  
   135  	if pmrRetrieved != pmr {
   136  		t.Fatal("the mapping should have been retrieved")
   137  	}
   138  }
   139  
   140  func TestAccountExtraDataLinker_whenNotFound(t *testing.T) {
   141  	db := NewMemoryDatabase()
   142  	psr := common.Hash{1}
   143  
   144  	pml := NewAccountExtraDataLinker(db)
   145  
   146  	pmrRetrieved := pml.GetAccountExtraDataRoot(psr)
   147  
   148  	if !common.EmptyHash(pmrRetrieved) {
   149  		t.Fatal("the retrieved privacy metadata root should be the empty hash")
   150  	}
   151  }
   152  
   153  func TestPrivateStatesTrieRoot(t *testing.T) {
   154  	db := NewMemoryDatabase()
   155  	blockRoot := common.HexToHash("0x4c50c7d11e58e5c6f40fa1a630ffcb3a017453e7f9d0ec8ccb01033fcf9f2210")
   156  	pstRoot := common.HexToHash("0x5c46375b6b333983077e152d1b6ca101d0586a6565fa75750deb1b07154bbdca")
   157  
   158  	err := WritePrivateStatesTrieRoot(db, blockRoot, pstRoot)
   159  	assert.Nil(t, err)
   160  
   161  	retrievedRoot := GetPrivateStatesTrieRoot(db, blockRoot)
   162  	assert.Equal(t, pstRoot, retrievedRoot)
   163  
   164  	retrievedEmptyRoot := GetPrivateStatesTrieRoot(db, common.Hash{})
   165  	assert.Equal(t, common.Hash{}, retrievedEmptyRoot)
   166  }
   167  
   168  func TestPrivateStateRoot(t *testing.T) {
   169  	db := NewMemoryDatabase()
   170  	blockRoot := common.HexToHash("0x4c50c7d11e58e5c6f40fa1a630ffcb3a017453e7f9d0ec8ccb01033fcf9f2210")
   171  	psRoot := common.HexToHash("0x5c46375b6b333983077e152d1b6ca101d0586a6565fa75750deb1b07154bbdca")
   172  
   173  	err := WritePrivateStateRoot(db, blockRoot, psRoot)
   174  	assert.Nil(t, err)
   175  
   176  	retrievedRoot := GetPrivateStateRoot(db, blockRoot)
   177  	assert.Equal(t, psRoot, retrievedRoot)
   178  
   179  	retrievedEmptyRoot := GetPrivateStateRoot(db, common.Hash{})
   180  	assert.Equal(t, common.Hash{}, retrievedEmptyRoot)
   181  }