difflayer_test.go 10 KB

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