database_quorum_test.go 4.9 KB

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