snapshot_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 := &Tree{
  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 := &Tree{
  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 := &Tree{
  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 := &Tree{
  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. // Doing a Cap operation with many allowed layers should be a no-op
  198. exp := len(snaps.layers)
  199. if err := snaps.Cap(common.HexToHash("0x04"), 2000, 1024*1024); err != nil {
  200. t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
  201. }
  202. if got := len(snaps.layers); got != exp {
  203. t.Errorf("layers modified, got %d exp %d", got, exp)
  204. }
  205. // Flatten the diff layer into the bottom accumulator
  206. if err := snaps.Cap(common.HexToHash("0x04"), 2, 1024*1024); err != nil {
  207. t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
  208. }
  209. // Since the accumulator diff layer was modified, ensure that data retrievald on the external reference fail
  210. if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
  211. t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
  212. }
  213. if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
  214. t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
  215. }
  216. if n := len(snaps.layers); n != 3 {
  217. t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 3)
  218. fmt.Println(snaps.layers)
  219. }
  220. }
  221. // TestPostCapBasicDataAccess tests some functionality regarding capping/flattening.
  222. func TestPostCapBasicDataAccess(t *testing.T) {
  223. // setAccount is a helper to construct a random account entry and assign it to
  224. // an account slot in a snapshot
  225. setAccount := func(accKey string) map[common.Hash][]byte {
  226. return map[common.Hash][]byte{
  227. common.HexToHash(accKey): randomAccount(),
  228. }
  229. }
  230. // Create a starting base layer and a snapshot tree out of it
  231. cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
  232. base := &diskLayer{
  233. db: rawdb.NewMemoryDatabase(),
  234. root: common.HexToHash("0x01"),
  235. cache: cache,
  236. }
  237. snaps := &Tree{
  238. layers: map[common.Hash]snapshot{
  239. base.root: base,
  240. },
  241. }
  242. // The lowest difflayer
  243. snaps.Update(common.HexToHash("0xa1"), common.HexToHash("0x01"), setAccount("0xa1"), nil)
  244. snaps.Update(common.HexToHash("0xa2"), common.HexToHash("0xa1"), setAccount("0xa2"), nil)
  245. snaps.Update(common.HexToHash("0xb2"), common.HexToHash("0xa1"), setAccount("0xb2"), nil)
  246. snaps.Update(common.HexToHash("0xa3"), common.HexToHash("0xa2"), setAccount("0xa3"), nil)
  247. snaps.Update(common.HexToHash("0xb3"), common.HexToHash("0xb2"), setAccount("0xb3"), nil)
  248. // checkExist verifies if an account exiss in a snapshot
  249. checkExist := func(layer *diffLayer, key string) error {
  250. if data, _ := layer.Account(common.HexToHash(key)); data == nil {
  251. return fmt.Errorf("expected %x to exist, got nil", common.HexToHash(key))
  252. }
  253. return nil
  254. }
  255. // shouldErr checks that an account access errors as expected
  256. shouldErr := func(layer *diffLayer, key string) error {
  257. if data, err := layer.Account(common.HexToHash(key)); err == nil {
  258. return fmt.Errorf("expected error, got data %x", data)
  259. }
  260. return nil
  261. }
  262. // check basics
  263. snap := snaps.Snapshot(common.HexToHash("0xb3")).(*diffLayer)
  264. if err := checkExist(snap, "0xa1"); err != nil {
  265. t.Error(err)
  266. }
  267. if err := checkExist(snap, "0xb2"); err != nil {
  268. t.Error(err)
  269. }
  270. if err := checkExist(snap, "0xb3"); err != nil {
  271. t.Error(err)
  272. }
  273. // Cap to a bad root should fail
  274. if err := snaps.Cap(common.HexToHash("0x1337"), 0, 1024); err == nil {
  275. t.Errorf("expected error, got none")
  276. }
  277. // Now, merge the a-chain
  278. snaps.Cap(common.HexToHash("0xa3"), 0, 1024)
  279. // At this point, a2 got merged into a1. Thus, a1 is now modified, and as a1 is
  280. // the parent of b2, b2 should no longer be able to iterate into parent.
  281. // These should still be accessible
  282. if err := checkExist(snap, "0xb2"); err != nil {
  283. t.Error(err)
  284. }
  285. if err := checkExist(snap, "0xb3"); err != nil {
  286. t.Error(err)
  287. }
  288. // But these would need iteration into the modified parent
  289. if err := shouldErr(snap, "0xa1"); err != nil {
  290. t.Error(err)
  291. }
  292. if err := shouldErr(snap, "0xa2"); err != nil {
  293. t.Error(err)
  294. }
  295. if err := shouldErr(snap, "0xa3"); err != nil {
  296. t.Error(err)
  297. }
  298. // Now, merge it again, just for fun. It should now error, since a3
  299. // is a disk layer
  300. if err := snaps.Cap(common.HexToHash("0xa3"), 0, 1024); err == nil {
  301. t.Error("expected error capping the disk layer, got none")
  302. }
  303. }