default_psr_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package mps
  2. import (
  3. "math/big"
  4. "sync"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/privatecache"
  8. "github.com/ethereum/go-ethereum/core/rawdb"
  9. "github.com/ethereum/go-ethereum/core/state"
  10. "github.com/ethereum/go-ethereum/core/types"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. //TestDefaultPSRCopy tests that copying a the PSR object indeed makes the original and
  14. // the copy and their states independent of each other.
  15. func TestDefaultPSRCopy(t *testing.T) {
  16. testdb := rawdb.NewMemoryDatabase()
  17. testCache := state.NewDatabase(testdb)
  18. privateCacheProvider := privatecache.NewPrivateCacheProvider(testdb, nil, testCache, false)
  19. psr, _ := NewDefaultPrivateStateRepository(testdb, testCache, privateCacheProvider, common.Hash{})
  20. testState, _ := psr.DefaultState()
  21. for i := byte(0); i < 255; i++ {
  22. addr := common.BytesToAddress([]byte{i})
  23. testState.AddBalance(addr, big.NewInt(int64(i)))
  24. }
  25. testState.Finalise(false)
  26. psrCopy := psr.Copy().(*DefaultPrivateStateRepository)
  27. testStateCopy, _ := psrCopy.DefaultState()
  28. // modify all in memory
  29. for i := byte(0); i < 255; i++ {
  30. testState.AddBalance(common.BytesToAddress([]byte{i}), big.NewInt(2*int64(i)))
  31. testStateCopy.AddBalance(common.BytesToAddress([]byte{i}), big.NewInt(3*int64(i)))
  32. }
  33. // Finalise the changes on all concurrently
  34. finalise := func(wg *sync.WaitGroup, db *state.StateDB) {
  35. defer wg.Done()
  36. db.Finalise(true)
  37. }
  38. var wg sync.WaitGroup
  39. wg.Add(2)
  40. go finalise(&wg, testState)
  41. go finalise(&wg, testStateCopy)
  42. wg.Wait()
  43. assert.Equal(t, psr.db, psrCopy.db)
  44. assert.Equal(t, psr.stateCache, psrCopy.stateCache)
  45. // Verify that the all states have been updated independently
  46. for i := byte(0); i < 255; i++ {
  47. addr := common.BytesToAddress([]byte{i})
  48. testObj := testState.GetOrNewStateObject(addr)
  49. testCopyObj := testStateCopy.GetOrNewStateObject(addr)
  50. if want := big.NewInt(3 * int64(i)); testObj.Balance().Cmp(want) != 0 {
  51. t.Errorf("empty obj %d: balance mismatch: have %v, want %v", i, testObj.Balance(), want)
  52. }
  53. if want := big.NewInt(4 * int64(i)); testCopyObj.Balance().Cmp(want) != 0 {
  54. t.Errorf("empty copy obj %d: balance mismatch: have %v, want %v", i, testCopyObj.Balance(), want)
  55. }
  56. }
  57. }
  58. //TestDefaultPSRReset tests that state objects are cleared from statedb after reset call
  59. //Any updated stateObjects not committed before reset will be cleared
  60. func TestDefaultPSRReset(t *testing.T) {
  61. testdb := rawdb.NewMemoryDatabase()
  62. testCache := state.NewDatabase(testdb)
  63. privateCacheProvider := privatecache.NewPrivateCacheProvider(testdb, nil, testCache, false)
  64. psr, _ := NewDefaultPrivateStateRepository(testdb, testCache, privateCacheProvider, common.Hash{})
  65. testState, _ := psr.DefaultState()
  66. for i := byte(0); i < 255; i++ {
  67. addr := common.BytesToAddress([]byte{i})
  68. testState.AddBalance(addr, big.NewInt(int64(i)))
  69. }
  70. testState.Finalise(false)
  71. for i := byte(0); i < 255; i++ {
  72. addr := common.BytesToAddress([]byte{i})
  73. assert.True(t, testState.Exist(addr))
  74. }
  75. psr.Reset()
  76. for i := byte(0); i < 255; i++ {
  77. addr := common.BytesToAddress([]byte{i})
  78. assert.False(t, testState.Exist(addr))
  79. }
  80. }
  81. func TestOnlyPrivateStateAccessible(t *testing.T) {
  82. testdb := rawdb.NewMemoryDatabase()
  83. testCache := state.NewDatabase(testdb)
  84. privateCacheProvider := privatecache.NewPrivateCacheProvider(testdb, nil, testCache, false)
  85. psr, _ := NewDefaultPrivateStateRepository(testdb, testCache, privateCacheProvider, common.Hash{})
  86. privateState, _ := psr.DefaultState()
  87. assert.NotEqual(t, privateState, nil)
  88. privateState, _ = psr.StatePSI(types.DefaultPrivateStateIdentifier)
  89. assert.NotEqual(t, privateState, nil)
  90. _, err := psr.StatePSI(types.PrivateStateIdentifier("test"))
  91. assert.Error(t, err, "only the 'private' psi is supported by the default private state manager")
  92. }
  93. //TestDefaultPSRCommitAndWrite tests that statedb is updated but not written to db
  94. func TestDefaultPSRCommit(t *testing.T) {
  95. testdb := rawdb.NewMemoryDatabase()
  96. testCache := state.NewDatabase(testdb)
  97. privateCacheProvider := privatecache.NewPrivateCacheProvider(testdb, nil, testCache, false)
  98. psr, _ := NewDefaultPrivateStateRepository(testdb, testCache, privateCacheProvider, common.Hash{})
  99. header := &types.Header{Number: big.NewInt(int64(1)), Root: common.Hash{123}}
  100. block := types.NewBlockWithHeader(header)
  101. testState, _ := psr.DefaultState()
  102. testRoot := testState.IntermediateRoot(false)
  103. assert.Equal(t, testRoot, common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"))
  104. //make updates to states
  105. for i := byte(0); i < 255; i++ {
  106. addr := common.BytesToAddress([]byte{i})
  107. testState.AddBalance(addr, big.NewInt(int64(i)))
  108. }
  109. assert.Equal(t, rawdb.GetPrivateStateRoot(testdb, block.Root()), common.Hash{})
  110. psr.Commit(false, block)
  111. //private root updated but not committed
  112. assert.NotEqual(t, psr.root, common.Hash{})
  113. assert.Equal(t, rawdb.GetPrivateStateRoot(testdb, block.Root()), common.Hash{})
  114. testRoot = testState.IntermediateRoot(false)
  115. assert.NotEqual(t, testRoot, common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"))
  116. }
  117. //TestDefaultPSRCommitAndWrite tests that statedb is updated and written to db
  118. func TestDefaultPSRCommitAndWrite(t *testing.T) {
  119. testdb := rawdb.NewMemoryDatabase()
  120. testCache := state.NewDatabase(testdb)
  121. privateCacheProvider := privatecache.NewPrivateCacheProvider(testdb, nil, testCache, false)
  122. psr, _ := NewDefaultPrivateStateRepository(testdb, testCache, privateCacheProvider, common.Hash{})
  123. header := &types.Header{Number: big.NewInt(int64(1)), Root: common.Hash{123}}
  124. block := types.NewBlockWithHeader(header)
  125. testState, _ := psr.DefaultState()
  126. testRoot := testState.IntermediateRoot(false)
  127. assert.Equal(t, testRoot, common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"))
  128. //make updates to states
  129. for i := byte(0); i < 255; i++ {
  130. addr := common.BytesToAddress([]byte{i})
  131. testState.AddBalance(addr, big.NewInt(int64(i)))
  132. }
  133. assert.Equal(t, rawdb.GetPrivateStateRoot(testdb, block.Root()), common.Hash{})
  134. psr.CommitAndWrite(false, block)
  135. //private root gets committed to db, but isn't updated on psr (only needed for commit)
  136. assert.Equal(t, psr.root, common.Hash{})
  137. assert.NotEqual(t, rawdb.GetPrivateStateRoot(testdb, block.Root()), common.Hash{})
  138. testRoot = testState.IntermediateRoot(false)
  139. assert.NotEqual(t, testRoot, common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"))
  140. }