difflayer_test.go 11 KB

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