difflayer_test.go 9.8 KB

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