snapshot_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. "fmt"
  19. "testing"
  20. "time"
  21. "github.com/allegro/bigcache"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. )
  25. // Tests that if a disk layer becomes stale, no active external references will
  26. // be returned with junk data. This version of the test flattens every diff layer
  27. // to check internal corner case around the bottom-most memory accumulator.
  28. func TestDiskLayerExternalInvalidationFullFlatten(t *testing.T) {
  29. // Create an empty base layer and a snapshot tree out of it
  30. cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
  31. base := &diskLayer{
  32. db: rawdb.NewMemoryDatabase(),
  33. root: common.HexToHash("0x01"),
  34. cache: cache,
  35. }
  36. snaps := &SnapshotTree{
  37. layers: map[common.Hash]snapshot{
  38. base.root: base,
  39. },
  40. }
  41. // Retrieve a reference to the base and commit a diff on top
  42. ref := snaps.Snapshot(base.root)
  43. accounts := map[common.Hash][]byte{
  44. common.HexToHash("0xa1"): randomAccount(),
  45. }
  46. storage := make(map[common.Hash]map[common.Hash][]byte)
  47. if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), accounts, storage); err != nil {
  48. t.Fatalf("failed to create a diff layer: %v", err)
  49. }
  50. if n := len(snaps.layers); n != 2 {
  51. t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 2)
  52. }
  53. // Commit the diff layer onto the disk and ensure it's persisted
  54. if err := snaps.Cap(common.HexToHash("0x02"), 0, 0); err != nil {
  55. t.Fatalf("failed to merge diff layer onto disk: %v", err)
  56. }
  57. // Since the base layer was modified, ensure that data retrievald on the external reference fail
  58. if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
  59. t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
  60. }
  61. if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
  62. t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
  63. }
  64. if n := len(snaps.layers); n != 1 {
  65. t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 1)
  66. fmt.Println(snaps.layers)
  67. }
  68. }
  69. // Tests that if a disk layer becomes stale, no active external references will
  70. // be returned with junk data. This version of the test retains the bottom diff
  71. // layer to check the usual mode of operation where the accumulator is retained.
  72. func TestDiskLayerExternalInvalidationPartialFlatten(t *testing.T) {
  73. // Create an empty base layer and a snapshot tree out of it
  74. cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
  75. base := &diskLayer{
  76. db: rawdb.NewMemoryDatabase(),
  77. root: common.HexToHash("0x01"),
  78. cache: cache,
  79. }
  80. snaps := &SnapshotTree{
  81. layers: map[common.Hash]snapshot{
  82. base.root: base,
  83. },
  84. }
  85. // Retrieve a reference to the base and commit two diffs on top
  86. ref := snaps.Snapshot(base.root)
  87. accounts := map[common.Hash][]byte{
  88. common.HexToHash("0xa1"): randomAccount(),
  89. }
  90. storage := make(map[common.Hash]map[common.Hash][]byte)
  91. if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), accounts, storage); err != nil {
  92. t.Fatalf("failed to create a diff layer: %v", err)
  93. }
  94. if err := snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), accounts, storage); err != nil {
  95. t.Fatalf("failed to create a diff layer: %v", err)
  96. }
  97. if n := len(snaps.layers); n != 3 {
  98. t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 3)
  99. }
  100. // Commit the diff layer onto the disk and ensure it's persisted
  101. if err := snaps.Cap(common.HexToHash("0x03"), 2, 0); err != nil {
  102. t.Fatalf("failed to merge diff layer onto disk: %v", err)
  103. }
  104. // Since the base layer was modified, ensure that data retrievald on the external reference fail
  105. if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
  106. t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
  107. }
  108. if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
  109. t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
  110. }
  111. if n := len(snaps.layers); n != 2 {
  112. t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 2)
  113. fmt.Println(snaps.layers)
  114. }
  115. }
  116. // Tests that if a diff layer becomes stale, no active external references will
  117. // be returned with junk data. This version of the test flattens every diff layer
  118. // to check internal corner case around the bottom-most memory accumulator.
  119. func TestDiffLayerExternalInvalidationFullFlatten(t *testing.T) {
  120. // Create an empty base layer and a snapshot tree out of it
  121. cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
  122. base := &diskLayer{
  123. db: rawdb.NewMemoryDatabase(),
  124. root: common.HexToHash("0x01"),
  125. cache: cache,
  126. }
  127. snaps := &SnapshotTree{
  128. layers: map[common.Hash]snapshot{
  129. base.root: base,
  130. },
  131. }
  132. // Commit two diffs on top and retrieve a reference to the bottommost
  133. accounts := map[common.Hash][]byte{
  134. common.HexToHash("0xa1"): randomAccount(),
  135. }
  136. storage := make(map[common.Hash]map[common.Hash][]byte)
  137. if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), accounts, storage); err != nil {
  138. t.Fatalf("failed to create a diff layer: %v", err)
  139. }
  140. if err := snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), accounts, storage); err != nil {
  141. t.Fatalf("failed to create a diff layer: %v", err)
  142. }
  143. if n := len(snaps.layers); n != 3 {
  144. t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 3)
  145. }
  146. ref := snaps.Snapshot(common.HexToHash("0x02"))
  147. // Flatten the diff layer into the bottom accumulator
  148. if err := snaps.Cap(common.HexToHash("0x03"), 1, 1024*1024); err != nil {
  149. t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
  150. }
  151. // Since the accumulator diff layer was modified, ensure that data retrievald on the external reference fail
  152. if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
  153. t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
  154. }
  155. if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
  156. t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
  157. }
  158. if n := len(snaps.layers); n != 2 {
  159. t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 2)
  160. fmt.Println(snaps.layers)
  161. }
  162. }
  163. // Tests that if a diff layer becomes stale, no active external references will
  164. // be returned with junk data. This version of the test retains the bottom diff
  165. // layer to check the usual mode of operation where the accumulator is retained.
  166. func TestDiffLayerExternalInvalidationPartialFlatten(t *testing.T) {
  167. // Create an empty base layer and a snapshot tree out of it
  168. cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
  169. base := &diskLayer{
  170. db: rawdb.NewMemoryDatabase(),
  171. root: common.HexToHash("0x01"),
  172. cache: cache,
  173. }
  174. snaps := &SnapshotTree{
  175. layers: map[common.Hash]snapshot{
  176. base.root: base,
  177. },
  178. }
  179. // Commit three diffs on top and retrieve a reference to the bottommost
  180. accounts := map[common.Hash][]byte{
  181. common.HexToHash("0xa1"): randomAccount(),
  182. }
  183. storage := make(map[common.Hash]map[common.Hash][]byte)
  184. if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), accounts, storage); err != nil {
  185. t.Fatalf("failed to create a diff layer: %v", err)
  186. }
  187. if err := snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), accounts, storage); err != nil {
  188. t.Fatalf("failed to create a diff layer: %v", err)
  189. }
  190. if err := snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), accounts, storage); err != nil {
  191. t.Fatalf("failed to create a diff layer: %v", err)
  192. }
  193. if n := len(snaps.layers); n != 4 {
  194. t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 4)
  195. }
  196. ref := snaps.Snapshot(common.HexToHash("0x02"))
  197. // Flatten the diff layer into the bottom accumulator
  198. if err := snaps.Cap(common.HexToHash("0x04"), 2, 1024*1024); err != nil {
  199. t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
  200. }
  201. // Since the accumulator diff layer was modified, ensure that data retrievald on the external reference fail
  202. if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
  203. t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
  204. }
  205. if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
  206. t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
  207. }
  208. if n := len(snaps.layers); n != 3 {
  209. t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 3)
  210. fmt.Println(snaps.layers)
  211. }
  212. }
  213. // TestPostCapBasicDataAccess tests some functionality regarding capping/flattening.
  214. func TestPostCapBasicDataAccess(t *testing.T) {
  215. // setAccount is a helper to construct a random account entry and assign it to
  216. // an account slot in a snapshot
  217. setAccount := func(accKey string) map[common.Hash][]byte {
  218. return map[common.Hash][]byte{
  219. common.HexToHash(accKey): randomAccount(),
  220. }
  221. }
  222. // Create a starting base layer and a snapshot tree out of it
  223. cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
  224. base := &diskLayer{
  225. db: rawdb.NewMemoryDatabase(),
  226. root: common.HexToHash("0x01"),
  227. cache: cache,
  228. }
  229. snaps := &SnapshotTree{
  230. layers: map[common.Hash]snapshot{
  231. base.root: base,
  232. },
  233. }
  234. // The lowest difflayer
  235. snaps.Update(common.HexToHash("0xa1"), common.HexToHash("0x01"), setAccount("0xa1"), nil)
  236. snaps.Update(common.HexToHash("0xa2"), common.HexToHash("0xa1"), setAccount("0xa2"), nil)
  237. snaps.Update(common.HexToHash("0xb2"), common.HexToHash("0xa1"), setAccount("0xb2"), nil)
  238. snaps.Update(common.HexToHash("0xa3"), common.HexToHash("0xa2"), setAccount("0xa3"), nil)
  239. snaps.Update(common.HexToHash("0xb3"), common.HexToHash("0xb2"), setAccount("0xb3"), nil)
  240. // checkExist verifies if an account exiss in a snapshot
  241. checkExist := func(layer *diffLayer, key string) error {
  242. if data, _ := layer.Account(common.HexToHash(key)); data == nil {
  243. return fmt.Errorf("expected %x to exist, got nil", common.HexToHash(key))
  244. }
  245. return nil
  246. }
  247. // shouldErr checks that an account access errors as expected
  248. shouldErr := func(layer *diffLayer, key string) error {
  249. if data, err := layer.Account(common.HexToHash(key)); err == nil {
  250. return fmt.Errorf("expected error, got data %x", data)
  251. }
  252. return nil
  253. }
  254. // check basics
  255. snap := snaps.Snapshot(common.HexToHash("0xb3")).(*diffLayer)
  256. if err := checkExist(snap, "0xa1"); err != nil {
  257. t.Error(err)
  258. }
  259. if err := checkExist(snap, "0xb2"); err != nil {
  260. t.Error(err)
  261. }
  262. if err := checkExist(snap, "0xb3"); err != nil {
  263. t.Error(err)
  264. }
  265. // Now, merge the a-chain
  266. snaps.Cap(common.HexToHash("0xa3"), 0, 1024)
  267. // At this point, a2 got merged into a1. Thus, a1 is now modified, and as a1 is
  268. // the parent of b2, b2 should no longer be able to iterate into parent.
  269. // These should still be accessible
  270. if err := checkExist(snap, "0xb2"); err != nil {
  271. t.Error(err)
  272. }
  273. if err := checkExist(snap, "0xb3"); err != nil {
  274. t.Error(err)
  275. }
  276. // But these would need iteration into the modified parent
  277. if err := shouldErr(snap, "0xa1"); err != nil {
  278. t.Error(err)
  279. }
  280. if err := shouldErr(snap, "0xa2"); err != nil {
  281. t.Error(err)
  282. }
  283. if err := shouldErr(snap, "0xa3"); err != nil {
  284. t.Error(err)
  285. }
  286. }