difflayer_test.go 10 KB

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