difflayer_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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{}, 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. type emptyLayer struct{}
  176. func (emptyLayer) Update(blockRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
  177. panic("implement me")
  178. }
  179. func (emptyLayer) Journal() error {
  180. panic("implement me")
  181. }
  182. func (emptyLayer) Stale() bool {
  183. panic("implement me")
  184. }
  185. func (emptyLayer) Root() common.Hash {
  186. return common.Hash{}
  187. }
  188. func (emptyLayer) Account(hash common.Hash) (*Account, error) {
  189. return nil, nil
  190. }
  191. func (emptyLayer) AccountRLP(hash common.Hash) ([]byte, error) {
  192. return nil, nil
  193. }
  194. func (emptyLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
  195. return nil, nil
  196. }
  197. // BenchmarkSearch checks how long it takes to find a non-existing key
  198. // BenchmarkSearch-6 200000 10481 ns/op (1K per layer)
  199. // BenchmarkSearch-6 200000 10760 ns/op (10K per layer)
  200. // BenchmarkSearch-6 100000 17866 ns/op
  201. //
  202. // BenchmarkSearch-6 500000 3723 ns/op (10k per layer, only top-level RLock()
  203. func BenchmarkSearch(b *testing.B) {
  204. // First, we set up 128 diff layers, with 1K items each
  205. fill := func(parent snapshot) *diffLayer {
  206. accounts := make(map[common.Hash][]byte)
  207. storage := make(map[common.Hash]map[common.Hash][]byte)
  208. for i := 0; i < 10000; i++ {
  209. accounts[randomHash()] = randomAccount()
  210. }
  211. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  212. }
  213. var layer snapshot
  214. layer = emptyLayer{}
  215. for i := 0; i < 128; i++ {
  216. layer = fill(layer)
  217. }
  218. key := common.Hash{}
  219. b.ResetTimer()
  220. for i := 0; i < b.N; i++ {
  221. layer.AccountRLP(key)
  222. }
  223. }
  224. // BenchmarkSearchSlot checks how long it takes to find a non-existing key
  225. // - Number of layers: 128
  226. // - Each layers contains the account, with a couple of storage slots
  227. // BenchmarkSearchSlot-6 100000 14554 ns/op
  228. // BenchmarkSearchSlot-6 100000 22254 ns/op (when checking parent root using mutex)
  229. // BenchmarkSearchSlot-6 100000 14551 ns/op (when checking parent number using atomic)
  230. func BenchmarkSearchSlot(b *testing.B) {
  231. // First, we set up 128 diff layers, with 1K items each
  232. accountKey := common.Hash{}
  233. storageKey := common.HexToHash("0x1337")
  234. accountRLP := randomAccount()
  235. fill := func(parent snapshot) *diffLayer {
  236. accounts := make(map[common.Hash][]byte)
  237. accounts[accountKey] = accountRLP
  238. storage := make(map[common.Hash]map[common.Hash][]byte)
  239. accStorage := make(map[common.Hash][]byte)
  240. for i := 0; i < 5; i++ {
  241. value := make([]byte, 32)
  242. rand.Read(value)
  243. accStorage[randomHash()] = value
  244. storage[accountKey] = accStorage
  245. }
  246. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  247. }
  248. var layer snapshot
  249. layer = emptyLayer{}
  250. for i := 0; i < 128; i++ {
  251. layer = fill(layer)
  252. }
  253. b.ResetTimer()
  254. for i := 0; i < b.N; i++ {
  255. layer.Storage(accountKey, storageKey)
  256. }
  257. }
  258. // With accountList and sorting
  259. //BenchmarkFlatten-6 50 29890856 ns/op
  260. //
  261. // Without sorting and tracking accountlist
  262. // BenchmarkFlatten-6 300 5511511 ns/op
  263. func BenchmarkFlatten(b *testing.B) {
  264. fill := func(parent snapshot) *diffLayer {
  265. accounts := make(map[common.Hash][]byte)
  266. storage := make(map[common.Hash]map[common.Hash][]byte)
  267. for i := 0; i < 100; i++ {
  268. accountKey := randomHash()
  269. accounts[accountKey] = randomAccount()
  270. accStorage := make(map[common.Hash][]byte)
  271. for i := 0; i < 20; i++ {
  272. value := make([]byte, 32)
  273. rand.Read(value)
  274. accStorage[randomHash()] = value
  275. }
  276. storage[accountKey] = accStorage
  277. }
  278. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  279. }
  280. b.ResetTimer()
  281. for i := 0; i < b.N; i++ {
  282. b.StopTimer()
  283. var layer snapshot
  284. layer = emptyLayer{}
  285. for i := 1; i < 128; i++ {
  286. layer = fill(layer)
  287. }
  288. b.StartTimer()
  289. for i := 1; i < 128; i++ {
  290. dl, ok := layer.(*diffLayer)
  291. if !ok {
  292. break
  293. }
  294. layer = dl.flatten()
  295. }
  296. b.StopTimer()
  297. }
  298. }
  299. // This test writes ~324M of diff layers to disk, spread over
  300. // - 128 individual layers,
  301. // - each with 200 accounts
  302. // - containing 200 slots
  303. //
  304. // BenchmarkJournal-6 1 1471373923 ns/ops
  305. // BenchmarkJournal-6 1 1208083335 ns/op // bufio writer
  306. func BenchmarkJournal(b *testing.B) {
  307. fill := func(parent snapshot) *diffLayer {
  308. accounts := make(map[common.Hash][]byte)
  309. storage := make(map[common.Hash]map[common.Hash][]byte)
  310. for i := 0; i < 200; i++ {
  311. accountKey := randomHash()
  312. accounts[accountKey] = randomAccount()
  313. accStorage := make(map[common.Hash][]byte)
  314. for i := 0; i < 200; i++ {
  315. value := make([]byte, 32)
  316. rand.Read(value)
  317. accStorage[randomHash()] = value
  318. }
  319. storage[accountKey] = accStorage
  320. }
  321. return newDiffLayer(parent, common.Hash{}, accounts, storage)
  322. }
  323. var layer snapshot
  324. layer = &diskLayer{
  325. journal: path.Join(os.TempDir(), "difflayer_journal.tmp"),
  326. }
  327. for i := 1; i < 128; i++ {
  328. layer = fill(layer)
  329. }
  330. b.ResetTimer()
  331. for i := 0; i < b.N; i++ {
  332. f, _ := layer.(*diffLayer).journal()
  333. f.Close()
  334. }
  335. }