difflayer_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. "os"
  22. "path"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/common"
  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{}, 1, common.Hash{}, accounts, storage)
  59. child := newDiffLayer(parent, 1, common.Hash{}, accounts, storage)
  60. child = newDiffLayer(child, 1, common.Hash{}, accounts, storage)
  61. child = newDiffLayer(child, 1, common.Hash{}, accounts, storage)
  62. child = newDiffLayer(child, 1, 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{}, 1, 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. // check number
  131. if got, exp := merged.number, child.number; got != exp {
  132. t.Errorf("merged layer: wrong number - exp %d got %d", exp, got)
  133. }
  134. if data, _ := merged.Account(h1); data == nil {
  135. t.Errorf("merged layer: expected %x to be non-nil", h1)
  136. }
  137. if data, _ := merged.Account(h2); data != nil {
  138. t.Errorf("merged layer: expected %x to be nil", h2)
  139. }
  140. // If we add more granular metering of memory, we can enable this again,
  141. // but it's not implemented for now
  142. //if got, exp := merged.memory, child.memory; got != exp {
  143. // t.Errorf("mem wrong, got %d, exp %d", got, exp)
  144. //}
  145. }
  146. // This tests that if we create a new account, and set a slot, and then merge
  147. // it, the lists will be correct.
  148. func TestInsertAndMerge(t *testing.T) {
  149. // Fill up a parent
  150. var (
  151. acc = common.HexToHash("0x01")
  152. slot = common.HexToHash("0x02")
  153. parent *diffLayer
  154. child *diffLayer
  155. )
  156. {
  157. var accounts = make(map[common.Hash][]byte)
  158. var storage = make(map[common.Hash]map[common.Hash][]byte)
  159. parent = newDiffLayer(emptyLayer{}, 1, common.Hash{}, accounts, storage)
  160. }
  161. {
  162. var accounts = make(map[common.Hash][]byte)
  163. var storage = make(map[common.Hash]map[common.Hash][]byte)
  164. accounts[acc] = randomAccount()
  165. accstorage := make(map[common.Hash][]byte)
  166. storage[acc] = accstorage
  167. storage[acc][slot] = []byte{0x01}
  168. child = newDiffLayer(parent, 2, common.Hash{}, accounts, storage)
  169. }
  170. // And flatten
  171. merged := (child.flatten()).(*diffLayer)
  172. { // Check that slot value is present
  173. got, _ := merged.Storage(acc, slot)
  174. if exp := []byte{0x01}; bytes.Compare(got, exp) != 0 {
  175. t.Errorf("merged slot value wrong, got %x, exp %x", got, exp)
  176. }
  177. }
  178. }
  179. type emptyLayer struct{}
  180. func (emptyLayer) Update(blockRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
  181. panic("implement me")
  182. }
  183. func (emptyLayer) Journal() error {
  184. panic("implement me")
  185. }
  186. func (emptyLayer) Info() (uint64, common.Hash) {
  187. return 0, common.Hash{}
  188. }
  189. func (emptyLayer) Number() uint64 {
  190. return 0
  191. }
  192. func (emptyLayer) Account(hash common.Hash) (*Account, error) {
  193. return nil, nil
  194. }
  195. func (emptyLayer) AccountRLP(hash common.Hash) ([]byte, error) {
  196. return nil, nil
  197. }
  198. func (emptyLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
  199. return nil, nil
  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. blocknum := uint64(0)
  210. fill := func(parent snapshot) *diffLayer {
  211. accounts := make(map[common.Hash][]byte)
  212. storage := make(map[common.Hash]map[common.Hash][]byte)
  213. for i := 0; i < 10000; i++ {
  214. accounts[randomHash()] = randomAccount()
  215. }
  216. blocknum++
  217. return newDiffLayer(parent, blocknum, common.Hash{}, accounts, storage)
  218. }
  219. var layer snapshot
  220. layer = emptyLayer{}
  221. for i := 0; i < 128; i++ {
  222. layer = fill(layer)
  223. }
  224. key := common.Hash{}
  225. b.ResetTimer()
  226. for i := 0; i < b.N; i++ {
  227. layer.AccountRLP(key)
  228. }
  229. }
  230. // BenchmarkSearchSlot checks how long it takes to find a non-existing key
  231. // - Number of layers: 128
  232. // - Each layers contains the account, with a couple of storage slots
  233. // BenchmarkSearchSlot-6 100000 14554 ns/op
  234. // BenchmarkSearchSlot-6 100000 22254 ns/op (when checking parent root using mutex)
  235. // BenchmarkSearchSlot-6 100000 14551 ns/op (when checking parent number using atomic)
  236. func BenchmarkSearchSlot(b *testing.B) {
  237. // First, we set up 128 diff layers, with 1K items each
  238. blocknum := uint64(0)
  239. accountKey := common.Hash{}
  240. storageKey := common.HexToHash("0x1337")
  241. accountRLP := randomAccount()
  242. fill := func(parent snapshot) *diffLayer {
  243. accounts := make(map[common.Hash][]byte)
  244. accounts[accountKey] = accountRLP
  245. storage := make(map[common.Hash]map[common.Hash][]byte)
  246. accStorage := make(map[common.Hash][]byte)
  247. for i := 0; i < 5; i++ {
  248. value := make([]byte, 32)
  249. rand.Read(value)
  250. accStorage[randomHash()] = value
  251. storage[accountKey] = accStorage
  252. }
  253. blocknum++
  254. return newDiffLayer(parent, blocknum, common.Hash{}, accounts, storage)
  255. }
  256. var layer snapshot
  257. layer = emptyLayer{}
  258. for i := 0; i < 128; i++ {
  259. layer = fill(layer)
  260. }
  261. b.ResetTimer()
  262. for i := 0; i < b.N; i++ {
  263. layer.Storage(accountKey, storageKey)
  264. }
  265. }
  266. // With accountList and sorting
  267. //BenchmarkFlatten-6 50 29890856 ns/op
  268. //
  269. // Without sorting and tracking accountlist
  270. // BenchmarkFlatten-6 300 5511511 ns/op
  271. func BenchmarkFlatten(b *testing.B) {
  272. fill := func(parent snapshot, blocknum int) *diffLayer {
  273. accounts := make(map[common.Hash][]byte)
  274. storage := make(map[common.Hash]map[common.Hash][]byte)
  275. for i := 0; i < 100; i++ {
  276. accountKey := randomHash()
  277. accounts[accountKey] = randomAccount()
  278. accStorage := make(map[common.Hash][]byte)
  279. for i := 0; i < 20; i++ {
  280. value := make([]byte, 32)
  281. rand.Read(value)
  282. accStorage[randomHash()] = value
  283. }
  284. storage[accountKey] = accStorage
  285. }
  286. return newDiffLayer(parent, uint64(blocknum), common.Hash{}, accounts, storage)
  287. }
  288. b.ResetTimer()
  289. for i := 0; i < b.N; i++ {
  290. b.StopTimer()
  291. var layer snapshot
  292. layer = emptyLayer{}
  293. for i := 1; i < 128; i++ {
  294. layer = fill(layer, i)
  295. }
  296. b.StartTimer()
  297. for i := 1; i < 128; i++ {
  298. dl, ok := layer.(*diffLayer)
  299. if !ok {
  300. break
  301. }
  302. layer = dl.flatten()
  303. }
  304. b.StopTimer()
  305. }
  306. }
  307. // This test writes ~324M of diff layers to disk, spread over
  308. // - 128 individual layers,
  309. // - each with 200 accounts
  310. // - containing 200 slots
  311. //
  312. // BenchmarkJournal-6 1 1471373923 ns/ops
  313. // BenchmarkJournal-6 1 1208083335 ns/op // bufio writer
  314. func BenchmarkJournal(b *testing.B) {
  315. fill := func(parent snapshot, blocknum int) *diffLayer {
  316. accounts := make(map[common.Hash][]byte)
  317. storage := make(map[common.Hash]map[common.Hash][]byte)
  318. for i := 0; i < 200; i++ {
  319. accountKey := randomHash()
  320. accounts[accountKey] = randomAccount()
  321. accStorage := make(map[common.Hash][]byte)
  322. for i := 0; i < 200; i++ {
  323. value := make([]byte, 32)
  324. rand.Read(value)
  325. accStorage[randomHash()] = value
  326. }
  327. storage[accountKey] = accStorage
  328. }
  329. return newDiffLayer(parent, uint64(blocknum), common.Hash{}, accounts, storage)
  330. }
  331. var layer snapshot
  332. layer = &diskLayer{
  333. journal: path.Join(os.TempDir(), "difflayer_journal.tmp"),
  334. }
  335. for i := 1; i < 128; i++ {
  336. layer = fill(layer, i)
  337. }
  338. b.ResetTimer()
  339. for i := 0; i < b.N; i++ {
  340. f, _ := layer.(*diffLayer).journal()
  341. f.Close()
  342. }
  343. }