snapshot_test.go 12 KB

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