difflayer_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2019 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 snapshot
  17. import (
  18. "bytes"
  19. "math/rand"
  20. "testing"
  21. "github.com/VictoriaMetrics/fastcache"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  25. )
  26. // TestMergeBasics tests some simple merges
  27. func TestMergeBasics(t *testing.T) {
  28. var (
  29. accounts = make(map[common.Hash][]byte)
  30. storage = make(map[common.Hash]map[common.Hash][]byte)
  31. )
  32. // Fill up a parent
  33. for i := 0; i < 100; i++ {
  34. h := randomHash()
  35. data := randomAccount()
  36. accounts[h] = data
  37. if rand.Intn(20) < 10 {
  38. accStorage := make(map[common.Hash][]byte)
  39. value := make([]byte, 32)
  40. rand.Read(value)
  41. accStorage[randomHash()] = value
  42. storage[h] = accStorage
  43. }
  44. }
  45. // Add some (identical) layers on top
  46. parent := newDiffLayer(emptyLayer(), common.Hash{}, accounts, storage)
  47. child := newDiffLayer(parent, common.Hash{}, accounts, storage)
  48. child = newDiffLayer(child, common.Hash{}, accounts, storage)
  49. child = newDiffLayer(child, common.Hash{}, accounts, storage)
  50. child = newDiffLayer(child, common.Hash{}, accounts, storage)
  51. // And flatten
  52. merged := (child.flatten()).(*diffLayer)
  53. { // Check account lists
  54. // Should be zero/nil first
  55. if got, exp := len(merged.accountList), 0; got != exp {
  56. t.Errorf("accountList wrong, got %v exp %v", got, exp)
  57. }
  58. // Then set when we call AccountList
  59. if got, exp := len(merged.AccountList()), len(accounts); got != exp {
  60. t.Errorf("AccountList() wrong, got %v exp %v", got, exp)
  61. }
  62. if got, exp := len(merged.accountList), len(accounts); got != exp {
  63. t.Errorf("accountList [2] wrong, got %v exp %v", got, exp)
  64. }
  65. }
  66. { // Check storage lists
  67. i := 0
  68. for aHash, sMap := range storage {
  69. if got, exp := len(merged.storageList), i; got != exp {
  70. t.Errorf("[1] storageList wrong, got %v exp %v", got, exp)
  71. }
  72. if got, exp := len(merged.StorageList(aHash)), len(sMap); got != exp {
  73. t.Errorf("[2] StorageList() wrong, got %v exp %v", got, exp)
  74. }
  75. if got, exp := len(merged.storageList[aHash]), len(sMap); got != exp {
  76. t.Errorf("storageList wrong, got %v exp %v", got, exp)
  77. }
  78. i++
  79. }
  80. }
  81. }
  82. // TestMergeDelete tests some deletion
  83. func TestMergeDelete(t *testing.T) {
  84. var (
  85. storage = make(map[common.Hash]map[common.Hash][]byte)
  86. )
  87. // Fill up a parent
  88. h1 := common.HexToHash("0x01")
  89. h2 := common.HexToHash("0x02")
  90. flip := func() map[common.Hash][]byte {
  91. accs := make(map[common.Hash][]byte)
  92. accs[h1] = randomAccount()
  93. accs[h2] = nil
  94. return accs
  95. }
  96. flop := func() map[common.Hash][]byte {
  97. accs := make(map[common.Hash][]byte)
  98. accs[h1] = nil
  99. accs[h2] = randomAccount()
  100. return accs
  101. }
  102. // Add some flip-flopping layers on top
  103. parent := newDiffLayer(emptyLayer(), common.Hash{}, flip(), storage)
  104. child := parent.Update(common.Hash{}, flop(), storage)
  105. child = child.Update(common.Hash{}, flip(), storage)
  106. child = child.Update(common.Hash{}, flop(), storage)
  107. child = child.Update(common.Hash{}, flip(), storage)
  108. child = child.Update(common.Hash{}, flop(), storage)
  109. child = child.Update(common.Hash{}, flip(), storage)
  110. if data, _ := child.Account(h1); data == nil {
  111. t.Errorf("last diff layer: expected %x to be non-nil", h1)
  112. }
  113. if data, _ := child.Account(h2); data != nil {
  114. t.Errorf("last diff layer: expected %x to be nil", h2)
  115. }
  116. // And flatten
  117. merged := (child.flatten()).(*diffLayer)
  118. if data, _ := merged.Account(h1); data == nil {
  119. t.Errorf("merged layer: expected %x to be non-nil", h1)
  120. }
  121. if data, _ := merged.Account(h2); data != nil {
  122. t.Errorf("merged layer: expected %x to be nil", h2)
  123. }
  124. // If we add more granular metering of memory, we can enable this again,
  125. // but it's not implemented for now
  126. //if got, exp := merged.memory, child.memory; got != exp {
  127. // t.Errorf("mem wrong, got %d, exp %d", got, exp)
  128. //}
  129. }
  130. // This tests that if we create a new account, and set a slot, and then merge
  131. // it, the lists will be correct.
  132. func TestInsertAndMerge(t *testing.T) {
  133. // Fill up a parent
  134. var (
  135. acc = common.HexToHash("0x01")
  136. slot = common.HexToHash("0x02")
  137. parent *diffLayer
  138. child *diffLayer
  139. )
  140. {
  141. var accounts = make(map[common.Hash][]byte)
  142. var storage = make(map[common.Hash]map[common.Hash][]byte)
  143. parent = newDiffLayer(emptyLayer(), common.Hash{}, accounts, storage)
  144. }
  145. {
  146. var accounts = make(map[common.Hash][]byte)
  147. var storage = make(map[common.Hash]map[common.Hash][]byte)
  148. accounts[acc] = randomAccount()
  149. accstorage := make(map[common.Hash][]byte)
  150. storage[acc] = accstorage
  151. storage[acc][slot] = []byte{0x01}
  152. child = newDiffLayer(parent, common.Hash{}, accounts, storage)
  153. }
  154. // And flatten
  155. merged := (child.flatten()).(*diffLayer)
  156. { // Check that slot value is present
  157. got, _ := merged.Storage(acc, slot)
  158. if exp := []byte{0x01}; !bytes.Equal(got, exp) {
  159. t.Errorf("merged slot value wrong, got %x, exp %x", got, exp)
  160. }
  161. }
  162. }
  163. func emptyLayer() *diskLayer {
  164. return &diskLayer{
  165. diskdb: memorydb.New(),
  166. cache: fastcache.New(500 * 1024),
  167. }
  168. }
  169. // BenchmarkSearch checks how long it takes to find a non-existing key
  170. // BenchmarkSearch-6 200000 10481 ns/op (1K per layer)
  171. // BenchmarkSearch-6 200000 10760 ns/op (10K per layer)
  172. // BenchmarkSearch-6 100000 17866 ns/op
  173. //
  174. // BenchmarkSearch-6 500000 3723 ns/op (10k per layer, only top-level RLock()
  175. func BenchmarkSearch(b *testing.B) {
  176. // First, we set up 128 diff layers, with 1K items each
  177. fill := func(parent snapshot) *diffLayer {
  178. accounts := make(map[common.Hash][]byte)
  179. storage := make(map[common.Hash]map[common.Hash][]byte)
  180. for i := 0; i < 10000; i++ {
  181. accounts[randomHash()] = randomAccount()
  182. }
  183. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  184. }
  185. var layer snapshot
  186. layer = emptyLayer()
  187. for i := 0; i < 128; i++ {
  188. layer = fill(layer)
  189. }
  190. key := crypto.Keccak256Hash([]byte{0x13, 0x38})
  191. b.ResetTimer()
  192. for i := 0; i < b.N; i++ {
  193. layer.AccountRLP(key)
  194. }
  195. }
  196. // BenchmarkSearchSlot checks how long it takes to find a non-existing key
  197. // - Number of layers: 128
  198. // - Each layers contains the account, with a couple of storage slots
  199. // BenchmarkSearchSlot-6 100000 14554 ns/op
  200. // BenchmarkSearchSlot-6 100000 22254 ns/op (when checking parent root using mutex)
  201. // BenchmarkSearchSlot-6 100000 14551 ns/op (when checking parent number using atomic)
  202. // With bloom filter:
  203. // BenchmarkSearchSlot-6 3467835 351 ns/op
  204. func BenchmarkSearchSlot(b *testing.B) {
  205. // First, we set up 128 diff layers, with 1K items each
  206. accountKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
  207. storageKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
  208. accountRLP := randomAccount()
  209. fill := func(parent snapshot) *diffLayer {
  210. accounts := make(map[common.Hash][]byte)
  211. accounts[accountKey] = accountRLP
  212. storage := make(map[common.Hash]map[common.Hash][]byte)
  213. accStorage := make(map[common.Hash][]byte)
  214. for i := 0; i < 5; i++ {
  215. value := make([]byte, 32)
  216. rand.Read(value)
  217. accStorage[randomHash()] = value
  218. storage[accountKey] = accStorage
  219. }
  220. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  221. }
  222. var layer snapshot
  223. layer = emptyLayer()
  224. for i := 0; i < 128; i++ {
  225. layer = fill(layer)
  226. }
  227. b.ResetTimer()
  228. for i := 0; i < b.N; i++ {
  229. layer.Storage(accountKey, storageKey)
  230. }
  231. }
  232. // With accountList and sorting
  233. //BenchmarkFlatten-6 50 29890856 ns/op
  234. //
  235. // Without sorting and tracking accountlist
  236. // BenchmarkFlatten-6 300 5511511 ns/op
  237. func BenchmarkFlatten(b *testing.B) {
  238. fill := func(parent snapshot) *diffLayer {
  239. accounts := make(map[common.Hash][]byte)
  240. storage := make(map[common.Hash]map[common.Hash][]byte)
  241. for i := 0; i < 100; i++ {
  242. accountKey := randomHash()
  243. accounts[accountKey] = randomAccount()
  244. accStorage := make(map[common.Hash][]byte)
  245. for i := 0; i < 20; i++ {
  246. value := make([]byte, 32)
  247. rand.Read(value)
  248. accStorage[randomHash()] = value
  249. }
  250. storage[accountKey] = accStorage
  251. }
  252. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  253. }
  254. b.ResetTimer()
  255. for i := 0; i < b.N; i++ {
  256. b.StopTimer()
  257. var layer snapshot
  258. layer = emptyLayer()
  259. for i := 1; i < 128; i++ {
  260. layer = fill(layer)
  261. }
  262. b.StartTimer()
  263. for i := 1; i < 128; i++ {
  264. dl, ok := layer.(*diffLayer)
  265. if !ok {
  266. break
  267. }
  268. layer = dl.flatten()
  269. }
  270. b.StopTimer()
  271. }
  272. }
  273. // This test writes ~324M of diff layers to disk, spread over
  274. // - 128 individual layers,
  275. // - each with 200 accounts
  276. // - containing 200 slots
  277. //
  278. // BenchmarkJournal-6 1 1471373923 ns/ops
  279. // BenchmarkJournal-6 1 1208083335 ns/op // bufio writer
  280. func BenchmarkJournal(b *testing.B) {
  281. fill := func(parent snapshot) *diffLayer {
  282. accounts := make(map[common.Hash][]byte)
  283. storage := make(map[common.Hash]map[common.Hash][]byte)
  284. for i := 0; i < 200; i++ {
  285. accountKey := randomHash()
  286. accounts[accountKey] = randomAccount()
  287. accStorage := make(map[common.Hash][]byte)
  288. for i := 0; i < 200; i++ {
  289. value := make([]byte, 32)
  290. rand.Read(value)
  291. accStorage[randomHash()] = value
  292. }
  293. storage[accountKey] = accStorage
  294. }
  295. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  296. }
  297. layer := snapshot(new(diskLayer))
  298. for i := 1; i < 128; i++ {
  299. layer = fill(layer)
  300. }
  301. b.ResetTimer()
  302. for i := 0; i < b.N; i++ {
  303. layer.Journal(new(bytes.Buffer))
  304. }
  305. }