blockchain_sethead_test.go 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. // Copyright 2020 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. // Tests that setting the chain head backwards doesn't leave the database in some
  17. // strange state with gaps in the chain, nor with block data dangling in the future.
  18. package core
  19. import (
  20. "fmt"
  21. "math/big"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/consensus/ethash"
  27. "github.com/ethereum/go-ethereum/core/rawdb"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/params"
  31. )
  32. // rewindTest is a test case for chain rollback upon user request.
  33. type rewindTest struct {
  34. canonicalBlocks int // Number of blocks to generate for the canonical chain (heavier)
  35. sidechainBlocks int // Number of blocks to generate for the side chain (lighter)
  36. freezeThreshold uint64 // Block number until which to move things into the freezer
  37. commitBlock uint64 // Block number for which to commit the state to disk
  38. pivotBlock *uint64 // Pivot block number in case of fast sync
  39. setheadBlock uint64 // Block number to set head back to
  40. expCanonicalBlocks int // Number of canonical blocks expected to remain in the database (excl. genesis)
  41. expSidechainBlocks int // Number of sidechain blocks expected to remain in the database (excl. genesis)
  42. expFrozen int // Number of canonical blocks expected to be in the freezer (incl. genesis)
  43. expHeadHeader uint64 // Block number of the expected head header
  44. expHeadFastBlock uint64 // Block number of the expected head fast sync block
  45. expHeadBlock uint64 // Block number of the expected head full block
  46. }
  47. //nolint:unused
  48. func (tt *rewindTest) dump(crash bool) string {
  49. buffer := new(strings.Builder)
  50. fmt.Fprint(buffer, "Chain:\n G")
  51. for i := 0; i < tt.canonicalBlocks; i++ {
  52. fmt.Fprintf(buffer, "->C%d", i+1)
  53. }
  54. fmt.Fprint(buffer, " (HEAD)\n")
  55. if tt.sidechainBlocks > 0 {
  56. fmt.Fprintf(buffer, " └")
  57. for i := 0; i < tt.sidechainBlocks; i++ {
  58. fmt.Fprintf(buffer, "->S%d", i+1)
  59. }
  60. fmt.Fprintf(buffer, "\n")
  61. }
  62. fmt.Fprintf(buffer, "\n")
  63. if tt.canonicalBlocks > int(tt.freezeThreshold) {
  64. fmt.Fprint(buffer, "Frozen:\n G")
  65. for i := 0; i < tt.canonicalBlocks-int(tt.freezeThreshold); i++ {
  66. fmt.Fprintf(buffer, "->C%d", i+1)
  67. }
  68. fmt.Fprintf(buffer, "\n\n")
  69. } else {
  70. fmt.Fprintf(buffer, "Frozen: none\n")
  71. }
  72. fmt.Fprintf(buffer, "Commit: G")
  73. if tt.commitBlock > 0 {
  74. fmt.Fprintf(buffer, ", C%d", tt.commitBlock)
  75. }
  76. fmt.Fprint(buffer, "\n")
  77. if tt.pivotBlock == nil {
  78. fmt.Fprintf(buffer, "Pivot : none\n")
  79. } else {
  80. fmt.Fprintf(buffer, "Pivot : C%d\n", *tt.pivotBlock)
  81. }
  82. if crash {
  83. fmt.Fprintf(buffer, "\nCRASH\n\n")
  84. } else {
  85. fmt.Fprintf(buffer, "\nSetHead(%d)\n\n", tt.setheadBlock)
  86. }
  87. fmt.Fprintf(buffer, "------------------------------\n\n")
  88. if tt.expFrozen > 0 {
  89. fmt.Fprint(buffer, "Expected in freezer:\n G")
  90. for i := 0; i < tt.expFrozen-1; i++ {
  91. fmt.Fprintf(buffer, "->C%d", i+1)
  92. }
  93. fmt.Fprintf(buffer, "\n\n")
  94. }
  95. if tt.expFrozen > 0 {
  96. if tt.expFrozen >= tt.expCanonicalBlocks {
  97. fmt.Fprintf(buffer, "Expected in leveldb: none\n")
  98. } else {
  99. fmt.Fprintf(buffer, "Expected in leveldb:\n C%d)", tt.expFrozen-1)
  100. for i := tt.expFrozen - 1; i < tt.expCanonicalBlocks; i++ {
  101. fmt.Fprintf(buffer, "->C%d", i+1)
  102. }
  103. fmt.Fprint(buffer, "\n")
  104. if tt.expSidechainBlocks > tt.expFrozen {
  105. fmt.Fprintf(buffer, " └")
  106. for i := tt.expFrozen - 1; i < tt.expSidechainBlocks; i++ {
  107. fmt.Fprintf(buffer, "->S%d", i+1)
  108. }
  109. fmt.Fprintf(buffer, "\n")
  110. }
  111. }
  112. } else {
  113. fmt.Fprint(buffer, "Expected in leveldb:\n G")
  114. for i := tt.expFrozen; i < tt.expCanonicalBlocks; i++ {
  115. fmt.Fprintf(buffer, "->C%d", i+1)
  116. }
  117. fmt.Fprint(buffer, "\n")
  118. if tt.expSidechainBlocks > tt.expFrozen {
  119. fmt.Fprintf(buffer, " └")
  120. for i := tt.expFrozen; i < tt.expSidechainBlocks; i++ {
  121. fmt.Fprintf(buffer, "->S%d", i+1)
  122. }
  123. fmt.Fprintf(buffer, "\n")
  124. }
  125. }
  126. fmt.Fprintf(buffer, "\n")
  127. fmt.Fprintf(buffer, "Expected head header : C%d\n", tt.expHeadHeader)
  128. fmt.Fprintf(buffer, "Expected head fast block: C%d\n", tt.expHeadFastBlock)
  129. if tt.expHeadBlock == 0 {
  130. fmt.Fprintf(buffer, "Expected head block : G\n")
  131. } else {
  132. fmt.Fprintf(buffer, "Expected head block : C%d\n", tt.expHeadBlock)
  133. }
  134. return buffer.String()
  135. }
  136. // Tests a sethead for a short canonical chain where a recent block was already
  137. // committed to disk and then the sethead called. In this case we expect the full
  138. // chain to be rolled back to the committed block. Everything above the sethead
  139. // point should be deleted. In between the committed block and the requested head
  140. // the data can remain as "fast sync" data to avoid redownloading it.
  141. func TestShortSetHead(t *testing.T) { testShortSetHead(t, false) }
  142. func TestShortSetHeadWithSnapshots(t *testing.T) { testShortSetHead(t, true) }
  143. func testShortSetHead(t *testing.T, snapshots bool) {
  144. // Chain:
  145. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  146. //
  147. // Frozen: none
  148. // Commit: G, C4
  149. // Pivot : none
  150. //
  151. // SetHead(7)
  152. //
  153. // ------------------------------
  154. //
  155. // Expected in leveldb:
  156. // G->C1->C2->C3->C4->C5->C6->C7
  157. //
  158. // Expected head header : C7
  159. // Expected head fast block: C7
  160. // Expected head block : C4
  161. testSetHead(t, &rewindTest{
  162. canonicalBlocks: 8,
  163. sidechainBlocks: 0,
  164. freezeThreshold: 16,
  165. commitBlock: 4,
  166. pivotBlock: nil,
  167. setheadBlock: 7,
  168. expCanonicalBlocks: 7,
  169. expSidechainBlocks: 0,
  170. expFrozen: 0,
  171. expHeadHeader: 7,
  172. expHeadFastBlock: 7,
  173. expHeadBlock: 4,
  174. }, snapshots)
  175. }
  176. // Tests a sethead for a short canonical chain where the fast sync pivot point was
  177. // already committed, after which sethead was called. In this case we expect the
  178. // chain to behave like in full sync mode, rolling back to the committed block
  179. // Everything above the sethead point should be deleted. In between the committed
  180. // block and the requested head the data can remain as "fast sync" data to avoid
  181. // redownloading it.
  182. func TestShortSnapSyncedSetHead(t *testing.T) { testShortSnapSyncedSetHead(t, false) }
  183. func TestShortSnapSyncedSetHeadWithSnapshots(t *testing.T) { testShortSnapSyncedSetHead(t, true) }
  184. func testShortSnapSyncedSetHead(t *testing.T, snapshots bool) {
  185. // Chain:
  186. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  187. //
  188. // Frozen: none
  189. // Commit: G, C4
  190. // Pivot : C4
  191. //
  192. // SetHead(7)
  193. //
  194. // ------------------------------
  195. //
  196. // Expected in leveldb:
  197. // G->C1->C2->C3->C4->C5->C6->C7
  198. //
  199. // Expected head header : C7
  200. // Expected head fast block: C7
  201. // Expected head block : C4
  202. testSetHead(t, &rewindTest{
  203. canonicalBlocks: 8,
  204. sidechainBlocks: 0,
  205. freezeThreshold: 16,
  206. commitBlock: 4,
  207. pivotBlock: uint64ptr(4),
  208. setheadBlock: 7,
  209. expCanonicalBlocks: 7,
  210. expSidechainBlocks: 0,
  211. expFrozen: 0,
  212. expHeadHeader: 7,
  213. expHeadFastBlock: 7,
  214. expHeadBlock: 4,
  215. }, snapshots)
  216. }
  217. // Tests a sethead for a short canonical chain where the fast sync pivot point was
  218. // not yet committed, but sethead was called. In this case we expect the chain to
  219. // detect that it was fast syncing and delete everything from the new head, since
  220. // we can just pick up fast syncing from there. The head full block should be set
  221. // to the genesis.
  222. func TestShortSnapSyncingSetHead(t *testing.T) { testShortSnapSyncingSetHead(t, false) }
  223. func TestShortSnapSyncingSetHeadWithSnapshots(t *testing.T) { testShortSnapSyncingSetHead(t, true) }
  224. func testShortSnapSyncingSetHead(t *testing.T, snapshots bool) {
  225. // Chain:
  226. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  227. //
  228. // Frozen: none
  229. // Commit: G
  230. // Pivot : C4
  231. //
  232. // SetHead(7)
  233. //
  234. // ------------------------------
  235. //
  236. // Expected in leveldb:
  237. // G->C1->C2->C3->C4->C5->C6->C7
  238. //
  239. // Expected head header : C7
  240. // Expected head fast block: C7
  241. // Expected head block : G
  242. testSetHead(t, &rewindTest{
  243. canonicalBlocks: 8,
  244. sidechainBlocks: 0,
  245. freezeThreshold: 16,
  246. commitBlock: 0,
  247. pivotBlock: uint64ptr(4),
  248. setheadBlock: 7,
  249. expCanonicalBlocks: 7,
  250. expSidechainBlocks: 0,
  251. expFrozen: 0,
  252. expHeadHeader: 7,
  253. expHeadFastBlock: 7,
  254. expHeadBlock: 0,
  255. }, snapshots)
  256. }
  257. // Tests a sethead for a short canonical chain and a shorter side chain, where a
  258. // recent block was already committed to disk and then sethead was called. In this
  259. // test scenario the side chain is below the committed block. In this case we expect
  260. // the canonical full chain to be rolled back to the committed block. Everything
  261. // above the sethead point should be deleted. In between the committed block and
  262. // the requested head the data can remain as "fast sync" data to avoid redownloading
  263. // it. The side chain should be left alone as it was shorter.
  264. func TestShortOldForkedSetHead(t *testing.T) { testShortOldForkedSetHead(t, false) }
  265. func TestShortOldForkedSetHeadWithSnapshots(t *testing.T) { testShortOldForkedSetHead(t, true) }
  266. func testShortOldForkedSetHead(t *testing.T, snapshots bool) {
  267. // Chain:
  268. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  269. // └->S1->S2->S3
  270. //
  271. // Frozen: none
  272. // Commit: G, C4
  273. // Pivot : none
  274. //
  275. // SetHead(7)
  276. //
  277. // ------------------------------
  278. //
  279. // Expected in leveldb:
  280. // G->C1->C2->C3->C4->C5->C6->C7
  281. // └->S1->S2->S3
  282. //
  283. // Expected head header : C7
  284. // Expected head fast block: C7
  285. // Expected head block : C4
  286. testSetHead(t, &rewindTest{
  287. canonicalBlocks: 8,
  288. sidechainBlocks: 3,
  289. freezeThreshold: 16,
  290. commitBlock: 4,
  291. pivotBlock: nil,
  292. setheadBlock: 7,
  293. expCanonicalBlocks: 7,
  294. expSidechainBlocks: 3,
  295. expFrozen: 0,
  296. expHeadHeader: 7,
  297. expHeadFastBlock: 7,
  298. expHeadBlock: 4,
  299. }, snapshots)
  300. }
  301. // Tests a sethead for a short canonical chain and a shorter side chain, where
  302. // the fast sync pivot point was already committed to disk and then sethead was
  303. // called. In this test scenario the side chain is below the committed block. In
  304. // this case we expect the canonical full chain to be rolled back to the committed
  305. // block. Everything above the sethead point should be deleted. In between the
  306. // committed block and the requested head the data can remain as "fast sync" data
  307. // to avoid redownloading it. The side chain should be left alone as it was shorter.
  308. func TestShortOldForkedSnapSyncedSetHead(t *testing.T) {
  309. testShortOldForkedSnapSyncedSetHead(t, false)
  310. }
  311. func TestShortOldForkedSnapSyncedSetHeadWithSnapshots(t *testing.T) {
  312. testShortOldForkedSnapSyncedSetHead(t, true)
  313. }
  314. func testShortOldForkedSnapSyncedSetHead(t *testing.T, snapshots bool) {
  315. // Chain:
  316. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  317. // └->S1->S2->S3
  318. //
  319. // Frozen: none
  320. // Commit: G, C4
  321. // Pivot : C4
  322. //
  323. // SetHead(7)
  324. //
  325. // ------------------------------
  326. //
  327. // Expected in leveldb:
  328. // G->C1->C2->C3->C4->C5->C6->C7
  329. // └->S1->S2->S3
  330. //
  331. // Expected head header : C7
  332. // Expected head fast block: C7
  333. // Expected head block : C4
  334. testSetHead(t, &rewindTest{
  335. canonicalBlocks: 8,
  336. sidechainBlocks: 3,
  337. freezeThreshold: 16,
  338. commitBlock: 4,
  339. pivotBlock: uint64ptr(4),
  340. setheadBlock: 7,
  341. expCanonicalBlocks: 7,
  342. expSidechainBlocks: 3,
  343. expFrozen: 0,
  344. expHeadHeader: 7,
  345. expHeadFastBlock: 7,
  346. expHeadBlock: 4,
  347. }, snapshots)
  348. }
  349. // Tests a sethead for a short canonical chain and a shorter side chain, where
  350. // the fast sync pivot point was not yet committed, but sethead was called. In this
  351. // test scenario the side chain is below the committed block. In this case we expect
  352. // the chain to detect that it was fast syncing and delete everything from the new
  353. // head, since we can just pick up fast syncing from there. The head full block
  354. // should be set to the genesis.
  355. func TestShortOldForkedSnapSyncingSetHead(t *testing.T) {
  356. testShortOldForkedSnapSyncingSetHead(t, false)
  357. }
  358. func TestShortOldForkedSnapSyncingSetHeadWithSnapshots(t *testing.T) {
  359. testShortOldForkedSnapSyncingSetHead(t, true)
  360. }
  361. func testShortOldForkedSnapSyncingSetHead(t *testing.T, snapshots bool) {
  362. // Chain:
  363. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  364. // └->S1->S2->S3
  365. //
  366. // Frozen: none
  367. // Commit: G
  368. // Pivot : C4
  369. //
  370. // SetHead(7)
  371. //
  372. // ------------------------------
  373. //
  374. // Expected in leveldb:
  375. // G->C1->C2->C3->C4->C5->C6->C7
  376. // └->S1->S2->S3
  377. //
  378. // Expected head header : C7
  379. // Expected head fast block: C7
  380. // Expected head block : G
  381. testSetHead(t, &rewindTest{
  382. canonicalBlocks: 8,
  383. sidechainBlocks: 3,
  384. freezeThreshold: 16,
  385. commitBlock: 0,
  386. pivotBlock: uint64ptr(4),
  387. setheadBlock: 7,
  388. expCanonicalBlocks: 7,
  389. expSidechainBlocks: 3,
  390. expFrozen: 0,
  391. expHeadHeader: 7,
  392. expHeadFastBlock: 7,
  393. expHeadBlock: 0,
  394. }, snapshots)
  395. }
  396. // Tests a sethead for a short canonical chain and a shorter side chain, where a
  397. // recent block was already committed to disk and then sethead was called. In this
  398. // test scenario the side chain reaches above the committed block. In this case we
  399. // expect the canonical full chain to be rolled back to the committed block. All
  400. // data above the sethead point should be deleted. In between the committed block
  401. // and the requested head the data can remain as "fast sync" data to avoid having
  402. // to redownload it. The side chain should be truncated to the head set.
  403. //
  404. // The side chain could be left to be if the fork point was before the new head
  405. // we are deleting to, but it would be exceedingly hard to detect that case and
  406. // properly handle it, so we'll trade extra work in exchange for simpler code.
  407. func TestShortNewlyForkedSetHead(t *testing.T) { testShortNewlyForkedSetHead(t, false) }
  408. func TestShortNewlyForkedSetHeadWithSnapshots(t *testing.T) { testShortNewlyForkedSetHead(t, true) }
  409. func testShortNewlyForkedSetHead(t *testing.T, snapshots bool) {
  410. // Chain:
  411. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10 (HEAD)
  412. // └->S1->S2->S3->S4->S5->S6->S7->S8
  413. //
  414. // Frozen: none
  415. // Commit: G, C4
  416. // Pivot : none
  417. //
  418. // SetHead(7)
  419. //
  420. // ------------------------------
  421. //
  422. // Expected in leveldb:
  423. // G->C1->C2->C3->C4->C5->C6->C7
  424. // └->S1->S2->S3->S4->S5->S6->S7
  425. //
  426. // Expected head header : C7
  427. // Expected head fast block: C7
  428. // Expected head block : C4
  429. testSetHead(t, &rewindTest{
  430. canonicalBlocks: 10,
  431. sidechainBlocks: 8,
  432. freezeThreshold: 16,
  433. commitBlock: 4,
  434. pivotBlock: nil,
  435. setheadBlock: 7,
  436. expCanonicalBlocks: 7,
  437. expSidechainBlocks: 7,
  438. expFrozen: 0,
  439. expHeadHeader: 7,
  440. expHeadFastBlock: 7,
  441. expHeadBlock: 4,
  442. }, snapshots)
  443. }
  444. // Tests a sethead for a short canonical chain and a shorter side chain, where
  445. // the fast sync pivot point was already committed to disk and then sethead was
  446. // called. In this case we expect the canonical full chain to be rolled back to
  447. // between the committed block and the requested head the data can remain as
  448. // "fast sync" data to avoid having to redownload it. The side chain should be
  449. // truncated to the head set.
  450. //
  451. // The side chain could be left to be if the fork point was before the new head
  452. // we are deleting to, but it would be exceedingly hard to detect that case and
  453. // properly handle it, so we'll trade extra work in exchange for simpler code.
  454. func TestShortNewlyForkedSnapSyncedSetHead(t *testing.T) {
  455. testShortNewlyForkedSnapSyncedSetHead(t, false)
  456. }
  457. func TestShortNewlyForkedSnapSyncedSetHeadWithSnapshots(t *testing.T) {
  458. testShortNewlyForkedSnapSyncedSetHead(t, true)
  459. }
  460. func testShortNewlyForkedSnapSyncedSetHead(t *testing.T, snapshots bool) {
  461. // Chain:
  462. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10 (HEAD)
  463. // └->S1->S2->S3->S4->S5->S6->S7->S8
  464. //
  465. // Frozen: none
  466. // Commit: G, C4
  467. // Pivot : C4
  468. //
  469. // SetHead(7)
  470. //
  471. // ------------------------------
  472. //
  473. // Expected in leveldb:
  474. // G->C1->C2->C3->C4->C5->C6->C7
  475. // └->S1->S2->S3->S4->S5->S6->S7
  476. //
  477. // Expected head header : C7
  478. // Expected head fast block: C7
  479. // Expected head block : C4
  480. testSetHead(t, &rewindTest{
  481. canonicalBlocks: 10,
  482. sidechainBlocks: 8,
  483. freezeThreshold: 16,
  484. commitBlock: 4,
  485. pivotBlock: uint64ptr(4),
  486. setheadBlock: 7,
  487. expCanonicalBlocks: 7,
  488. expSidechainBlocks: 7,
  489. expFrozen: 0,
  490. expHeadHeader: 7,
  491. expHeadFastBlock: 7,
  492. expHeadBlock: 4,
  493. }, snapshots)
  494. }
  495. // Tests a sethead for a short canonical chain and a shorter side chain, where
  496. // the fast sync pivot point was not yet committed, but sethead was called. In
  497. // this test scenario the side chain reaches above the committed block. In this
  498. // case we expect the chain to detect that it was fast syncing and delete
  499. // everything from the new head, since we can just pick up fast syncing from
  500. // there.
  501. //
  502. // The side chain could be left to be if the fork point was before the new head
  503. // we are deleting to, but it would be exceedingly hard to detect that case and
  504. // properly handle it, so we'll trade extra work in exchange for simpler code.
  505. func TestShortNewlyForkedSnapSyncingSetHead(t *testing.T) {
  506. testShortNewlyForkedSnapSyncingSetHead(t, false)
  507. }
  508. func TestShortNewlyForkedSnapSyncingSetHeadWithSnapshots(t *testing.T) {
  509. testShortNewlyForkedSnapSyncingSetHead(t, true)
  510. }
  511. func testShortNewlyForkedSnapSyncingSetHead(t *testing.T, snapshots bool) {
  512. // Chain:
  513. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10 (HEAD)
  514. // └->S1->S2->S3->S4->S5->S6->S7->S8
  515. //
  516. // Frozen: none
  517. // Commit: G
  518. // Pivot : C4
  519. //
  520. // SetHead(7)
  521. //
  522. // ------------------------------
  523. //
  524. // Expected in leveldb:
  525. // G->C1->C2->C3->C4->C5->C6->C7
  526. // └->S1->S2->S3->S4->S5->S6->S7
  527. //
  528. // Expected head header : C7
  529. // Expected head fast block: C7
  530. // Expected head block : G
  531. testSetHead(t, &rewindTest{
  532. canonicalBlocks: 10,
  533. sidechainBlocks: 8,
  534. freezeThreshold: 16,
  535. commitBlock: 0,
  536. pivotBlock: uint64ptr(4),
  537. setheadBlock: 7,
  538. expCanonicalBlocks: 7,
  539. expSidechainBlocks: 7,
  540. expFrozen: 0,
  541. expHeadHeader: 7,
  542. expHeadFastBlock: 7,
  543. expHeadBlock: 0,
  544. }, snapshots)
  545. }
  546. // Tests a sethead for a short canonical chain and a longer side chain, where a
  547. // recent block was already committed to disk and then sethead was called. In this
  548. // case we expect the canonical full chain to be rolled back to the committed block.
  549. // All data above the sethead point should be deleted. In between the committed
  550. // block and the requested head the data can remain as "fast sync" data to avoid
  551. // having to redownload it. The side chain should be truncated to the head set.
  552. //
  553. // The side chain could be left to be if the fork point was before the new head
  554. // we are deleting to, but it would be exceedingly hard to detect that case and
  555. // properly handle it, so we'll trade extra work in exchange for simpler code.
  556. func TestShortReorgedSetHead(t *testing.T) { testShortReorgedSetHead(t, false) }
  557. func TestShortReorgedSetHeadWithSnapshots(t *testing.T) { testShortReorgedSetHead(t, true) }
  558. func testShortReorgedSetHead(t *testing.T, snapshots bool) {
  559. // Chain:
  560. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  561. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  562. //
  563. // Frozen: none
  564. // Commit: G, C4
  565. // Pivot : none
  566. //
  567. // SetHead(7)
  568. //
  569. // ------------------------------
  570. //
  571. // Expected in leveldb:
  572. // G->C1->C2->C3->C4->C5->C6->C7
  573. // └->S1->S2->S3->S4->S5->S6->S7
  574. //
  575. // Expected head header : C7
  576. // Expected head fast block: C7
  577. // Expected head block : C4
  578. testSetHead(t, &rewindTest{
  579. canonicalBlocks: 8,
  580. sidechainBlocks: 10,
  581. freezeThreshold: 16,
  582. commitBlock: 4,
  583. pivotBlock: nil,
  584. setheadBlock: 7,
  585. expCanonicalBlocks: 7,
  586. expSidechainBlocks: 7,
  587. expFrozen: 0,
  588. expHeadHeader: 7,
  589. expHeadFastBlock: 7,
  590. expHeadBlock: 4,
  591. }, snapshots)
  592. }
  593. // Tests a sethead for a short canonical chain and a longer side chain, where
  594. // the fast sync pivot point was already committed to disk and then sethead was
  595. // called. In this case we expect the canonical full chain to be rolled back to
  596. // the committed block. All data above the sethead point should be deleted. In
  597. // between the committed block and the requested head the data can remain as
  598. // "fast sync" data to avoid having to redownload it. The side chain should be
  599. // truncated to the head set.
  600. //
  601. // The side chain could be left to be if the fork point was before the new head
  602. // we are deleting to, but it would be exceedingly hard to detect that case and
  603. // properly handle it, so we'll trade extra work in exchange for simpler code.
  604. func TestShortReorgedSnapSyncedSetHead(t *testing.T) {
  605. testShortReorgedSnapSyncedSetHead(t, false)
  606. }
  607. func TestShortReorgedSnapSyncedSetHeadWithSnapshots(t *testing.T) {
  608. testShortReorgedSnapSyncedSetHead(t, true)
  609. }
  610. func testShortReorgedSnapSyncedSetHead(t *testing.T, snapshots bool) {
  611. // Chain:
  612. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  613. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  614. //
  615. // Frozen: none
  616. // Commit: G, C4
  617. // Pivot : C4
  618. //
  619. // SetHead(7)
  620. //
  621. // ------------------------------
  622. //
  623. // Expected in leveldb:
  624. // G->C1->C2->C3->C4->C5->C6->C7
  625. // └->S1->S2->S3->S4->S5->S6->S7
  626. //
  627. // Expected head header : C7
  628. // Expected head fast block: C7
  629. // Expected head block : C4
  630. testSetHead(t, &rewindTest{
  631. canonicalBlocks: 8,
  632. sidechainBlocks: 10,
  633. freezeThreshold: 16,
  634. commitBlock: 4,
  635. pivotBlock: uint64ptr(4),
  636. setheadBlock: 7,
  637. expCanonicalBlocks: 7,
  638. expSidechainBlocks: 7,
  639. expFrozen: 0,
  640. expHeadHeader: 7,
  641. expHeadFastBlock: 7,
  642. expHeadBlock: 4,
  643. }, snapshots)
  644. }
  645. // Tests a sethead for a short canonical chain and a longer side chain, where
  646. // the fast sync pivot point was not yet committed, but sethead was called. In
  647. // this case we expect the chain to detect that it was fast syncing and delete
  648. // everything from the new head, since we can just pick up fast syncing from
  649. // there.
  650. //
  651. // The side chain could be left to be if the fork point was before the new head
  652. // we are deleting to, but it would be exceedingly hard to detect that case and
  653. // properly handle it, so we'll trade extra work in exchange for simpler code.
  654. func TestShortReorgedSnapSyncingSetHead(t *testing.T) {
  655. testShortReorgedSnapSyncingSetHead(t, false)
  656. }
  657. func TestShortReorgedSnapSyncingSetHeadWithSnapshots(t *testing.T) {
  658. testShortReorgedSnapSyncingSetHead(t, true)
  659. }
  660. func testShortReorgedSnapSyncingSetHead(t *testing.T, snapshots bool) {
  661. // Chain:
  662. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  663. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  664. //
  665. // Frozen: none
  666. // Commit: G
  667. // Pivot : C4
  668. //
  669. // SetHead(7)
  670. //
  671. // ------------------------------
  672. //
  673. // Expected in leveldb:
  674. // G->C1->C2->C3->C4->C5->C6->C7
  675. // └->S1->S2->S3->S4->S5->S6->S7
  676. //
  677. // Expected head header : C7
  678. // Expected head fast block: C7
  679. // Expected head block : G
  680. testSetHead(t, &rewindTest{
  681. canonicalBlocks: 8,
  682. sidechainBlocks: 10,
  683. freezeThreshold: 16,
  684. commitBlock: 0,
  685. pivotBlock: uint64ptr(4),
  686. setheadBlock: 7,
  687. expCanonicalBlocks: 7,
  688. expSidechainBlocks: 7,
  689. expFrozen: 0,
  690. expHeadHeader: 7,
  691. expHeadFastBlock: 7,
  692. expHeadBlock: 0,
  693. }, snapshots)
  694. }
  695. // Tests a sethead for a long canonical chain with frozen blocks where a recent
  696. // block - newer than the ancient limit - was already committed to disk and then
  697. // sethead was called. In this case we expect the full chain to be rolled back
  698. // to the committed block. Everything above the sethead point should be deleted.
  699. // In between the committed block and the requested head the data can remain as
  700. // "fast sync" data to avoid redownloading it.
  701. func TestLongShallowSetHead(t *testing.T) { testLongShallowSetHead(t, false) }
  702. func TestLongShallowSetHeadWithSnapshots(t *testing.T) { testLongShallowSetHead(t, true) }
  703. func testLongShallowSetHead(t *testing.T, snapshots bool) {
  704. // Chain:
  705. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  706. //
  707. // Frozen:
  708. // G->C1->C2
  709. //
  710. // Commit: G, C4
  711. // Pivot : none
  712. //
  713. // SetHead(6)
  714. //
  715. // ------------------------------
  716. //
  717. // Expected in freezer:
  718. // G->C1->C2
  719. //
  720. // Expected in leveldb:
  721. // C2)->C3->C4->C5->C6
  722. //
  723. // Expected head header : C6
  724. // Expected head fast block: C6
  725. // Expected head block : C4
  726. testSetHead(t, &rewindTest{
  727. canonicalBlocks: 18,
  728. sidechainBlocks: 0,
  729. freezeThreshold: 16,
  730. commitBlock: 4,
  731. pivotBlock: nil,
  732. setheadBlock: 6,
  733. expCanonicalBlocks: 6,
  734. expSidechainBlocks: 0,
  735. expFrozen: 3,
  736. expHeadHeader: 6,
  737. expHeadFastBlock: 6,
  738. expHeadBlock: 4,
  739. }, snapshots)
  740. }
  741. // Tests a sethead for a long canonical chain with frozen blocks where a recent
  742. // block - older than the ancient limit - was already committed to disk and then
  743. // sethead was called. In this case we expect the full chain to be rolled back
  744. // to the committed block. Since the ancient limit was underflown, everything
  745. // needs to be deleted onwards to avoid creating a gap.
  746. func TestLongDeepSetHead(t *testing.T) { testLongDeepSetHead(t, false) }
  747. func TestLongDeepSetHeadWithSnapshots(t *testing.T) { testLongDeepSetHead(t, true) }
  748. func testLongDeepSetHead(t *testing.T, snapshots bool) {
  749. // Chain:
  750. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  751. //
  752. // Frozen:
  753. // G->C1->C2->C3->C4->C5->C6->C7->C8
  754. //
  755. // Commit: G, C4
  756. // Pivot : none
  757. //
  758. // SetHead(6)
  759. //
  760. // ------------------------------
  761. //
  762. // Expected in freezer:
  763. // G->C1->C2->C3->C4
  764. //
  765. // Expected in leveldb: none
  766. //
  767. // Expected head header : C4
  768. // Expected head fast block: C4
  769. // Expected head block : C4
  770. testSetHead(t, &rewindTest{
  771. canonicalBlocks: 24,
  772. sidechainBlocks: 0,
  773. freezeThreshold: 16,
  774. commitBlock: 4,
  775. pivotBlock: nil,
  776. setheadBlock: 6,
  777. expCanonicalBlocks: 4,
  778. expSidechainBlocks: 0,
  779. expFrozen: 5,
  780. expHeadHeader: 4,
  781. expHeadFastBlock: 4,
  782. expHeadBlock: 4,
  783. }, snapshots)
  784. }
  785. // Tests a sethead for a long canonical chain with frozen blocks where the fast
  786. // sync pivot point - newer than the ancient limit - was already committed, after
  787. // which sethead was called. In this case we expect the full chain to be rolled
  788. // back to the committed block. Everything above the sethead point should be
  789. // deleted. In between the committed block and the requested head the data can
  790. // remain as "fast sync" data to avoid redownloading it.
  791. func TestLongSnapSyncedShallowSetHead(t *testing.T) {
  792. testLongSnapSyncedShallowSetHead(t, false)
  793. }
  794. func TestLongSnapSyncedShallowSetHeadWithSnapshots(t *testing.T) {
  795. testLongSnapSyncedShallowSetHead(t, true)
  796. }
  797. func testLongSnapSyncedShallowSetHead(t *testing.T, snapshots bool) {
  798. // Chain:
  799. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  800. //
  801. // Frozen:
  802. // G->C1->C2
  803. //
  804. // Commit: G, C4
  805. // Pivot : C4
  806. //
  807. // SetHead(6)
  808. //
  809. // ------------------------------
  810. //
  811. // Expected in freezer:
  812. // G->C1->C2
  813. //
  814. // Expected in leveldb:
  815. // C2)->C3->C4->C5->C6
  816. //
  817. // Expected head header : C6
  818. // Expected head fast block: C6
  819. // Expected head block : C4
  820. testSetHead(t, &rewindTest{
  821. canonicalBlocks: 18,
  822. sidechainBlocks: 0,
  823. freezeThreshold: 16,
  824. commitBlock: 4,
  825. pivotBlock: uint64ptr(4),
  826. setheadBlock: 6,
  827. expCanonicalBlocks: 6,
  828. expSidechainBlocks: 0,
  829. expFrozen: 3,
  830. expHeadHeader: 6,
  831. expHeadFastBlock: 6,
  832. expHeadBlock: 4,
  833. }, snapshots)
  834. }
  835. // Tests a sethead for a long canonical chain with frozen blocks where the fast
  836. // sync pivot point - older than the ancient limit - was already committed, after
  837. // which sethead was called. In this case we expect the full chain to be rolled
  838. // back to the committed block. Since the ancient limit was underflown, everything
  839. // needs to be deleted onwards to avoid creating a gap.
  840. func TestLongSnapSyncedDeepSetHead(t *testing.T) { testLongSnapSyncedDeepSetHead(t, false) }
  841. func TestLongSnapSyncedDeepSetHeadWithSnapshots(t *testing.T) { testLongSnapSyncedDeepSetHead(t, true) }
  842. func testLongSnapSyncedDeepSetHead(t *testing.T, snapshots bool) {
  843. // Chain:
  844. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  845. //
  846. // Frozen:
  847. // G->C1->C2->C3->C4->C5->C6->C7->C8
  848. //
  849. // Commit: G, C4
  850. // Pivot : C4
  851. //
  852. // SetHead(6)
  853. //
  854. // ------------------------------
  855. //
  856. // Expected in freezer:
  857. // G->C1->C2->C3->C4
  858. //
  859. // Expected in leveldb: none
  860. //
  861. // Expected head header : C4
  862. // Expected head fast block: C4
  863. // Expected head block : C4
  864. testSetHead(t, &rewindTest{
  865. canonicalBlocks: 24,
  866. sidechainBlocks: 0,
  867. freezeThreshold: 16,
  868. commitBlock: 4,
  869. pivotBlock: uint64ptr(4),
  870. setheadBlock: 6,
  871. expCanonicalBlocks: 4,
  872. expSidechainBlocks: 0,
  873. expFrozen: 5,
  874. expHeadHeader: 4,
  875. expHeadFastBlock: 4,
  876. expHeadBlock: 4,
  877. }, snapshots)
  878. }
  879. // Tests a sethead for a long canonical chain with frozen blocks where the fast
  880. // sync pivot point - newer than the ancient limit - was not yet committed, but
  881. // sethead was called. In this case we expect the chain to detect that it was fast
  882. // syncing and delete everything from the new head, since we can just pick up fast
  883. // syncing from there.
  884. func TestLongSnapSyncingShallowSetHead(t *testing.T) {
  885. testLongSnapSyncingShallowSetHead(t, false)
  886. }
  887. func TestLongSnapSyncingShallowSetHeadWithSnapshots(t *testing.T) {
  888. testLongSnapSyncingShallowSetHead(t, true)
  889. }
  890. func testLongSnapSyncingShallowSetHead(t *testing.T, snapshots bool) {
  891. // Chain:
  892. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  893. //
  894. // Frozen:
  895. // G->C1->C2
  896. //
  897. // Commit: G
  898. // Pivot : C4
  899. //
  900. // SetHead(6)
  901. //
  902. // ------------------------------
  903. //
  904. // Expected in freezer:
  905. // G->C1->C2
  906. //
  907. // Expected in leveldb:
  908. // C2)->C3->C4->C5->C6
  909. //
  910. // Expected head header : C6
  911. // Expected head fast block: C6
  912. // Expected head block : G
  913. testSetHead(t, &rewindTest{
  914. canonicalBlocks: 18,
  915. sidechainBlocks: 0,
  916. freezeThreshold: 16,
  917. commitBlock: 0,
  918. pivotBlock: uint64ptr(4),
  919. setheadBlock: 6,
  920. expCanonicalBlocks: 6,
  921. expSidechainBlocks: 0,
  922. expFrozen: 3,
  923. expHeadHeader: 6,
  924. expHeadFastBlock: 6,
  925. expHeadBlock: 0,
  926. }, snapshots)
  927. }
  928. // Tests a sethead for a long canonical chain with frozen blocks where the fast
  929. // sync pivot point - older than the ancient limit - was not yet committed, but
  930. // sethead was called. In this case we expect the chain to detect that it was fast
  931. // syncing and delete everything from the new head, since we can just pick up fast
  932. // syncing from there.
  933. func TestLongSnapSyncingDeepSetHead(t *testing.T) {
  934. testLongSnapSyncingDeepSetHead(t, false)
  935. }
  936. func TestLongSnapSyncingDeepSetHeadWithSnapshots(t *testing.T) {
  937. testLongSnapSyncingDeepSetHead(t, true)
  938. }
  939. func testLongSnapSyncingDeepSetHead(t *testing.T, snapshots bool) {
  940. // Chain:
  941. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  942. //
  943. // Frozen:
  944. // G->C1->C2->C3->C4->C5->C6->C7->C8
  945. //
  946. // Commit: G
  947. // Pivot : C4
  948. //
  949. // SetHead(6)
  950. //
  951. // ------------------------------
  952. //
  953. // Expected in freezer:
  954. // G->C1->C2->C3->C4->C5->C6
  955. //
  956. // Expected in leveldb: none
  957. //
  958. // Expected head header : C6
  959. // Expected head fast block: C6
  960. // Expected head block : G
  961. testSetHead(t, &rewindTest{
  962. canonicalBlocks: 24,
  963. sidechainBlocks: 0,
  964. freezeThreshold: 16,
  965. commitBlock: 0,
  966. pivotBlock: uint64ptr(4),
  967. setheadBlock: 6,
  968. expCanonicalBlocks: 6,
  969. expSidechainBlocks: 0,
  970. expFrozen: 7,
  971. expHeadHeader: 6,
  972. expHeadFastBlock: 6,
  973. expHeadBlock: 0,
  974. }, snapshots)
  975. }
  976. // Tests a sethead for a long canonical chain with frozen blocks and a shorter side
  977. // chain, where a recent block - newer than the ancient limit - was already committed
  978. // to disk and then sethead was called. In this case we expect the canonical full
  979. // chain to be rolled back to the committed block. Everything above the sethead point
  980. // should be deleted. In between the committed block and the requested head the data
  981. // can remain as "fast sync" data to avoid redownloading it. The side chain is nuked
  982. // by the freezer.
  983. func TestLongOldForkedShallowSetHead(t *testing.T) {
  984. testLongOldForkedShallowSetHead(t, false)
  985. }
  986. func TestLongOldForkedShallowSetHeadWithSnapshots(t *testing.T) {
  987. testLongOldForkedShallowSetHead(t, true)
  988. }
  989. func testLongOldForkedShallowSetHead(t *testing.T, snapshots bool) {
  990. // Chain:
  991. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  992. // └->S1->S2->S3
  993. //
  994. // Frozen:
  995. // G->C1->C2
  996. //
  997. // Commit: G, C4
  998. // Pivot : none
  999. //
  1000. // SetHead(6)
  1001. //
  1002. // ------------------------------
  1003. //
  1004. // Expected in freezer:
  1005. // G->C1->C2
  1006. //
  1007. // Expected in leveldb:
  1008. // C2)->C3->C4->C5->C6
  1009. //
  1010. // Expected head header : C6
  1011. // Expected head fast block: C6
  1012. // Expected head block : C4
  1013. testSetHead(t, &rewindTest{
  1014. canonicalBlocks: 18,
  1015. sidechainBlocks: 3,
  1016. freezeThreshold: 16,
  1017. commitBlock: 4,
  1018. pivotBlock: nil,
  1019. setheadBlock: 6,
  1020. expCanonicalBlocks: 6,
  1021. expSidechainBlocks: 0,
  1022. expFrozen: 3,
  1023. expHeadHeader: 6,
  1024. expHeadFastBlock: 6,
  1025. expHeadBlock: 4,
  1026. }, snapshots)
  1027. }
  1028. // Tests a sethead for a long canonical chain with frozen blocks and a shorter side
  1029. // chain, where a recent block - older than the ancient limit - was already committed
  1030. // to disk and then sethead was called. In this case we expect the canonical full
  1031. // chain to be rolled back to the committed block. Since the ancient limit was
  1032. // underflown, everything needs to be deleted onwards to avoid creating a gap. The
  1033. // side chain is nuked by the freezer.
  1034. func TestLongOldForkedDeepSetHead(t *testing.T) { testLongOldForkedDeepSetHead(t, false) }
  1035. func TestLongOldForkedDeepSetHeadWithSnapshots(t *testing.T) { testLongOldForkedDeepSetHead(t, true) }
  1036. func testLongOldForkedDeepSetHead(t *testing.T, snapshots bool) {
  1037. // Chain:
  1038. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1039. // └->S1->S2->S3
  1040. //
  1041. // Frozen:
  1042. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1043. //
  1044. // Commit: G, C4
  1045. // Pivot : none
  1046. //
  1047. // SetHead(6)
  1048. //
  1049. // ------------------------------
  1050. //
  1051. // Expected in freezer:
  1052. // G->C1->C2->C3->C4
  1053. //
  1054. // Expected in leveldb: none
  1055. //
  1056. // Expected head header : C4
  1057. // Expected head fast block: C4
  1058. // Expected head block : C4
  1059. testSetHead(t, &rewindTest{
  1060. canonicalBlocks: 24,
  1061. sidechainBlocks: 3,
  1062. freezeThreshold: 16,
  1063. commitBlock: 4,
  1064. pivotBlock: nil,
  1065. setheadBlock: 6,
  1066. expCanonicalBlocks: 4,
  1067. expSidechainBlocks: 0,
  1068. expFrozen: 5,
  1069. expHeadHeader: 4,
  1070. expHeadFastBlock: 4,
  1071. expHeadBlock: 4,
  1072. }, snapshots)
  1073. }
  1074. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1075. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1076. // was already committed to disk and then sethead was called. In this test scenario
  1077. // the side chain is below the committed block. In this case we expect the canonical
  1078. // full chain to be rolled back to the committed block. Everything above the
  1079. // sethead point should be deleted. In between the committed block and the
  1080. // requested head the data can remain as "fast sync" data to avoid redownloading
  1081. // it. The side chain is nuked by the freezer.
  1082. func TestLongOldForkedSnapSyncedShallowSetHead(t *testing.T) {
  1083. testLongOldForkedSnapSyncedShallowSetHead(t, false)
  1084. }
  1085. func TestLongOldForkedSnapSyncedShallowSetHeadWithSnapshots(t *testing.T) {
  1086. testLongOldForkedSnapSyncedShallowSetHead(t, true)
  1087. }
  1088. func testLongOldForkedSnapSyncedShallowSetHead(t *testing.T, snapshots bool) {
  1089. // Chain:
  1090. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1091. // └->S1->S2->S3
  1092. //
  1093. // Frozen:
  1094. // G->C1->C2
  1095. //
  1096. // Commit: G, C4
  1097. // Pivot : C4
  1098. //
  1099. // SetHead(6)
  1100. //
  1101. // ------------------------------
  1102. //
  1103. // Expected in freezer:
  1104. // G->C1->C2
  1105. //
  1106. // Expected in leveldb:
  1107. // C2)->C3->C4->C5->C6
  1108. //
  1109. // Expected head header : C6
  1110. // Expected head fast block: C6
  1111. // Expected head block : C4
  1112. testSetHead(t, &rewindTest{
  1113. canonicalBlocks: 18,
  1114. sidechainBlocks: 3,
  1115. freezeThreshold: 16,
  1116. commitBlock: 4,
  1117. pivotBlock: uint64ptr(4),
  1118. setheadBlock: 6,
  1119. expCanonicalBlocks: 6,
  1120. expSidechainBlocks: 0,
  1121. expFrozen: 3,
  1122. expHeadHeader: 6,
  1123. expHeadFastBlock: 6,
  1124. expHeadBlock: 4,
  1125. }, snapshots)
  1126. }
  1127. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1128. // side chain, where the fast sync pivot point - older than the ancient limit -
  1129. // was already committed to disk and then sethead was called. In this test scenario
  1130. // the side chain is below the committed block. In this case we expect the canonical
  1131. // full chain to be rolled back to the committed block. Since the ancient limit was
  1132. // underflown, everything needs to be deleted onwards to avoid creating a gap. The
  1133. // side chain is nuked by the freezer.
  1134. func TestLongOldForkedSnapSyncedDeepSetHead(t *testing.T) {
  1135. testLongOldForkedSnapSyncedDeepSetHead(t, false)
  1136. }
  1137. func TestLongOldForkedSnapSyncedDeepSetHeadWithSnapshots(t *testing.T) {
  1138. testLongOldForkedSnapSyncedDeepSetHead(t, true)
  1139. }
  1140. func testLongOldForkedSnapSyncedDeepSetHead(t *testing.T, snapshots bool) {
  1141. // Chain:
  1142. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1143. // └->S1->S2->S3
  1144. //
  1145. // Frozen:
  1146. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1147. //
  1148. // Commit: G, C4
  1149. // Pivot : C4
  1150. //
  1151. // SetHead(6)
  1152. //
  1153. // ------------------------------
  1154. //
  1155. // Expected in freezer:
  1156. // G->C1->C2->C3->C4->C5->C6
  1157. //
  1158. // Expected in leveldb: none
  1159. //
  1160. // Expected head header : C6
  1161. // Expected head fast block: C6
  1162. // Expected head block : C4
  1163. testSetHead(t, &rewindTest{
  1164. canonicalBlocks: 24,
  1165. sidechainBlocks: 3,
  1166. freezeThreshold: 16,
  1167. commitBlock: 4,
  1168. pivotBlock: uint64ptr(4),
  1169. setheadBlock: 6,
  1170. expCanonicalBlocks: 4,
  1171. expSidechainBlocks: 0,
  1172. expFrozen: 5,
  1173. expHeadHeader: 4,
  1174. expHeadFastBlock: 4,
  1175. expHeadBlock: 4,
  1176. }, snapshots)
  1177. }
  1178. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1179. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1180. // was not yet committed, but sethead was called. In this test scenario the side
  1181. // chain is below the committed block. In this case we expect the chain to detect
  1182. // that it was fast syncing and delete everything from the new head, since we can
  1183. // just pick up fast syncing from there. The side chain is completely nuked by the
  1184. // freezer.
  1185. func TestLongOldForkedSnapSyncingShallowSetHead(t *testing.T) {
  1186. testLongOldForkedSnapSyncingShallowSetHead(t, false)
  1187. }
  1188. func TestLongOldForkedSnapSyncingShallowSetHeadWithSnapshots(t *testing.T) {
  1189. testLongOldForkedSnapSyncingShallowSetHead(t, true)
  1190. }
  1191. func testLongOldForkedSnapSyncingShallowSetHead(t *testing.T, snapshots bool) {
  1192. // Chain:
  1193. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1194. // └->S1->S2->S3
  1195. //
  1196. // Frozen:
  1197. // G->C1->C2
  1198. //
  1199. // Commit: G
  1200. // Pivot : C4
  1201. //
  1202. // SetHead(6)
  1203. //
  1204. // ------------------------------
  1205. //
  1206. // Expected in freezer:
  1207. // G->C1->C2
  1208. //
  1209. // Expected in leveldb:
  1210. // C2)->C3->C4->C5->C6
  1211. //
  1212. // Expected head header : C6
  1213. // Expected head fast block: C6
  1214. // Expected head block : G
  1215. testSetHead(t, &rewindTest{
  1216. canonicalBlocks: 18,
  1217. sidechainBlocks: 3,
  1218. freezeThreshold: 16,
  1219. commitBlock: 0,
  1220. pivotBlock: uint64ptr(4),
  1221. setheadBlock: 6,
  1222. expCanonicalBlocks: 6,
  1223. expSidechainBlocks: 0,
  1224. expFrozen: 3,
  1225. expHeadHeader: 6,
  1226. expHeadFastBlock: 6,
  1227. expHeadBlock: 0,
  1228. }, snapshots)
  1229. }
  1230. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1231. // side chain, where the fast sync pivot point - older than the ancient limit -
  1232. // was not yet committed, but sethead was called. In this test scenario the side
  1233. // chain is below the committed block. In this case we expect the chain to detect
  1234. // that it was fast syncing and delete everything from the new head, since we can
  1235. // just pick up fast syncing from there. The side chain is completely nuked by the
  1236. // freezer.
  1237. func TestLongOldForkedSnapSyncingDeepSetHead(t *testing.T) {
  1238. testLongOldForkedSnapSyncingDeepSetHead(t, false)
  1239. }
  1240. func TestLongOldForkedSnapSyncingDeepSetHeadWithSnapshots(t *testing.T) {
  1241. testLongOldForkedSnapSyncingDeepSetHead(t, true)
  1242. }
  1243. func testLongOldForkedSnapSyncingDeepSetHead(t *testing.T, snapshots bool) {
  1244. // Chain:
  1245. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1246. // └->S1->S2->S3
  1247. //
  1248. // Frozen:
  1249. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1250. //
  1251. // Commit: G
  1252. // Pivot : C4
  1253. //
  1254. // SetHead(6)
  1255. //
  1256. // ------------------------------
  1257. //
  1258. // Expected in freezer:
  1259. // G->C1->C2->C3->C4->C5->C6
  1260. //
  1261. // Expected in leveldb: none
  1262. //
  1263. // Expected head header : C6
  1264. // Expected head fast block: C6
  1265. // Expected head block : G
  1266. testSetHead(t, &rewindTest{
  1267. canonicalBlocks: 24,
  1268. sidechainBlocks: 3,
  1269. freezeThreshold: 16,
  1270. commitBlock: 0,
  1271. pivotBlock: uint64ptr(4),
  1272. setheadBlock: 6,
  1273. expCanonicalBlocks: 6,
  1274. expSidechainBlocks: 0,
  1275. expFrozen: 7,
  1276. expHeadHeader: 6,
  1277. expHeadFastBlock: 6,
  1278. expHeadBlock: 0,
  1279. }, snapshots)
  1280. }
  1281. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1282. // side chain, where a recent block - newer than the ancient limit - was already
  1283. // committed to disk and then sethead was called. In this test scenario the side
  1284. // chain is above the committed block. In this case the freezer will delete the
  1285. // sidechain since it's dangling, reverting to TestLongShallowSetHead.
  1286. func TestLongNewerForkedShallowSetHead(t *testing.T) {
  1287. testLongNewerForkedShallowSetHead(t, false)
  1288. }
  1289. func TestLongNewerForkedShallowSetHeadWithSnapshots(t *testing.T) {
  1290. testLongNewerForkedShallowSetHead(t, true)
  1291. }
  1292. func testLongNewerForkedShallowSetHead(t *testing.T, snapshots bool) {
  1293. // Chain:
  1294. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1295. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1296. //
  1297. // Frozen:
  1298. // G->C1->C2
  1299. //
  1300. // Commit: G, C4
  1301. // Pivot : none
  1302. //
  1303. // SetHead(6)
  1304. //
  1305. // ------------------------------
  1306. //
  1307. // Expected in freezer:
  1308. // G->C1->C2
  1309. //
  1310. // Expected in leveldb:
  1311. // C2)->C3->C4->C5->C6
  1312. //
  1313. // Expected head header : C6
  1314. // Expected head fast block: C6
  1315. // Expected head block : C4
  1316. testSetHead(t, &rewindTest{
  1317. canonicalBlocks: 18,
  1318. sidechainBlocks: 12,
  1319. freezeThreshold: 16,
  1320. commitBlock: 4,
  1321. pivotBlock: nil,
  1322. setheadBlock: 6,
  1323. expCanonicalBlocks: 6,
  1324. expSidechainBlocks: 0,
  1325. expFrozen: 3,
  1326. expHeadHeader: 6,
  1327. expHeadFastBlock: 6,
  1328. expHeadBlock: 4,
  1329. }, snapshots)
  1330. }
  1331. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1332. // side chain, where a recent block - older than the ancient limit - was already
  1333. // committed to disk and then sethead was called. In this test scenario the side
  1334. // chain is above the committed block. In this case the freezer will delete the
  1335. // sidechain since it's dangling, reverting to TestLongDeepSetHead.
  1336. func TestLongNewerForkedDeepSetHead(t *testing.T) {
  1337. testLongNewerForkedDeepSetHead(t, false)
  1338. }
  1339. func TestLongNewerForkedDeepSetHeadWithSnapshots(t *testing.T) {
  1340. testLongNewerForkedDeepSetHead(t, true)
  1341. }
  1342. func testLongNewerForkedDeepSetHead(t *testing.T, snapshots bool) {
  1343. // Chain:
  1344. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1345. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1346. //
  1347. // Frozen:
  1348. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1349. //
  1350. // Commit: G, C4
  1351. // Pivot : none
  1352. //
  1353. // SetHead(6)
  1354. //
  1355. // ------------------------------
  1356. //
  1357. // Expected in freezer:
  1358. // G->C1->C2->C3->C4
  1359. //
  1360. // Expected in leveldb: none
  1361. //
  1362. // Expected head header : C4
  1363. // Expected head fast block: C4
  1364. // Expected head block : C4
  1365. testSetHead(t, &rewindTest{
  1366. canonicalBlocks: 24,
  1367. sidechainBlocks: 12,
  1368. freezeThreshold: 16,
  1369. commitBlock: 4,
  1370. pivotBlock: nil,
  1371. setheadBlock: 6,
  1372. expCanonicalBlocks: 4,
  1373. expSidechainBlocks: 0,
  1374. expFrozen: 5,
  1375. expHeadHeader: 4,
  1376. expHeadFastBlock: 4,
  1377. expHeadBlock: 4,
  1378. }, snapshots)
  1379. }
  1380. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1381. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1382. // was already committed to disk and then sethead was called. In this test scenario
  1383. // the side chain is above the committed block. In this case the freezer will delete
  1384. // the sidechain since it's dangling, reverting to TestLongSnapSyncedShallowSetHead.
  1385. func TestLongNewerForkedSnapSyncedShallowSetHead(t *testing.T) {
  1386. testLongNewerForkedSnapSyncedShallowSetHead(t, false)
  1387. }
  1388. func TestLongNewerForkedSnapSyncedShallowSetHeadWithSnapshots(t *testing.T) {
  1389. testLongNewerForkedSnapSyncedShallowSetHead(t, true)
  1390. }
  1391. func testLongNewerForkedSnapSyncedShallowSetHead(t *testing.T, snapshots bool) {
  1392. // Chain:
  1393. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1394. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1395. //
  1396. // Frozen:
  1397. // G->C1->C2
  1398. //
  1399. // Commit: G, C4
  1400. // Pivot : C4
  1401. //
  1402. // SetHead(6)
  1403. //
  1404. // ------------------------------
  1405. //
  1406. // Expected in freezer:
  1407. // G->C1->C2
  1408. //
  1409. // Expected in leveldb:
  1410. // C2)->C3->C4->C5->C6
  1411. //
  1412. // Expected head header : C6
  1413. // Expected head fast block: C6
  1414. // Expected head block : C4
  1415. testSetHead(t, &rewindTest{
  1416. canonicalBlocks: 18,
  1417. sidechainBlocks: 12,
  1418. freezeThreshold: 16,
  1419. commitBlock: 4,
  1420. pivotBlock: uint64ptr(4),
  1421. setheadBlock: 6,
  1422. expCanonicalBlocks: 6,
  1423. expSidechainBlocks: 0,
  1424. expFrozen: 3,
  1425. expHeadHeader: 6,
  1426. expHeadFastBlock: 6,
  1427. expHeadBlock: 4,
  1428. }, snapshots)
  1429. }
  1430. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1431. // side chain, where the fast sync pivot point - older than the ancient limit -
  1432. // was already committed to disk and then sethead was called. In this test scenario
  1433. // the side chain is above the committed block. In this case the freezer will delete
  1434. // the sidechain since it's dangling, reverting to TestLongSnapSyncedDeepSetHead.
  1435. func TestLongNewerForkedSnapSyncedDeepSetHead(t *testing.T) {
  1436. testLongNewerForkedSnapSyncedDeepSetHead(t, false)
  1437. }
  1438. func TestLongNewerForkedSnapSyncedDeepSetHeadWithSnapshots(t *testing.T) {
  1439. testLongNewerForkedSnapSyncedDeepSetHead(t, true)
  1440. }
  1441. func testLongNewerForkedSnapSyncedDeepSetHead(t *testing.T, snapshots bool) {
  1442. // Chain:
  1443. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1444. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1445. //
  1446. // Frozen:
  1447. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1448. //
  1449. // Commit: G, C4
  1450. // Pivot : C4
  1451. //
  1452. // SetHead(6)
  1453. //
  1454. // ------------------------------
  1455. //
  1456. // Expected in freezer:
  1457. // G->C1->C2->C3->C4
  1458. //
  1459. // Expected in leveldb: none
  1460. //
  1461. // Expected head header : C4
  1462. // Expected head fast block: C4
  1463. // Expected head block : C
  1464. testSetHead(t, &rewindTest{
  1465. canonicalBlocks: 24,
  1466. sidechainBlocks: 12,
  1467. freezeThreshold: 16,
  1468. commitBlock: 4,
  1469. pivotBlock: uint64ptr(4),
  1470. setheadBlock: 6,
  1471. expCanonicalBlocks: 4,
  1472. expSidechainBlocks: 0,
  1473. expFrozen: 5,
  1474. expHeadHeader: 4,
  1475. expHeadFastBlock: 4,
  1476. expHeadBlock: 4,
  1477. }, snapshots)
  1478. }
  1479. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1480. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1481. // was not yet committed, but sethead was called. In this test scenario the side
  1482. // chain is above the committed block. In this case the freezer will delete the
  1483. // sidechain since it's dangling, reverting to TestLongSnapSyncinghallowSetHead.
  1484. func TestLongNewerForkedSnapSyncingShallowSetHead(t *testing.T) {
  1485. testLongNewerForkedSnapSyncingShallowSetHead(t, false)
  1486. }
  1487. func TestLongNewerForkedSnapSyncingShallowSetHeadWithSnapshots(t *testing.T) {
  1488. testLongNewerForkedSnapSyncingShallowSetHead(t, true)
  1489. }
  1490. func testLongNewerForkedSnapSyncingShallowSetHead(t *testing.T, snapshots bool) {
  1491. // Chain:
  1492. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1493. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1494. //
  1495. // Frozen:
  1496. // G->C1->C2
  1497. //
  1498. // Commit: G
  1499. // Pivot : C4
  1500. //
  1501. // SetHead(6)
  1502. //
  1503. // ------------------------------
  1504. //
  1505. // Expected in freezer:
  1506. // G->C1->C2
  1507. //
  1508. // Expected in leveldb:
  1509. // C2)->C3->C4->C5->C6
  1510. //
  1511. // Expected head header : C6
  1512. // Expected head fast block: C6
  1513. // Expected head block : G
  1514. testSetHead(t, &rewindTest{
  1515. canonicalBlocks: 18,
  1516. sidechainBlocks: 12,
  1517. freezeThreshold: 16,
  1518. commitBlock: 0,
  1519. pivotBlock: uint64ptr(4),
  1520. setheadBlock: 6,
  1521. expCanonicalBlocks: 6,
  1522. expSidechainBlocks: 0,
  1523. expFrozen: 3,
  1524. expHeadHeader: 6,
  1525. expHeadFastBlock: 6,
  1526. expHeadBlock: 0,
  1527. }, snapshots)
  1528. }
  1529. // Tests a sethead for a long canonical chain with frozen blocks and a shorter
  1530. // side chain, where the fast sync pivot point - older than the ancient limit -
  1531. // was not yet committed, but sethead was called. In this test scenario the side
  1532. // chain is above the committed block. In this case the freezer will delete the
  1533. // sidechain since it's dangling, reverting to TestLongSnapSyncingDeepSetHead.
  1534. func TestLongNewerForkedSnapSyncingDeepSetHead(t *testing.T) {
  1535. testLongNewerForkedSnapSyncingDeepSetHead(t, false)
  1536. }
  1537. func TestLongNewerForkedSnapSyncingDeepSetHeadWithSnapshots(t *testing.T) {
  1538. testLongNewerForkedSnapSyncingDeepSetHead(t, true)
  1539. }
  1540. func testLongNewerForkedSnapSyncingDeepSetHead(t *testing.T, snapshots bool) {
  1541. // Chain:
  1542. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1543. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1544. //
  1545. // Frozen:
  1546. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1547. //
  1548. // Commit: G
  1549. // Pivot : C4
  1550. //
  1551. // SetHead(6)
  1552. //
  1553. // ------------------------------
  1554. //
  1555. // Expected in freezer:
  1556. // G->C1->C2->C3->C4->C5->C6
  1557. //
  1558. // Expected in leveldb: none
  1559. //
  1560. // Expected head header : C6
  1561. // Expected head fast block: C6
  1562. // Expected head block : G
  1563. testSetHead(t, &rewindTest{
  1564. canonicalBlocks: 24,
  1565. sidechainBlocks: 12,
  1566. freezeThreshold: 16,
  1567. commitBlock: 0,
  1568. pivotBlock: uint64ptr(4),
  1569. setheadBlock: 6,
  1570. expCanonicalBlocks: 6,
  1571. expSidechainBlocks: 0,
  1572. expFrozen: 7,
  1573. expHeadHeader: 6,
  1574. expHeadFastBlock: 6,
  1575. expHeadBlock: 0,
  1576. }, snapshots)
  1577. }
  1578. // Tests a sethead for a long canonical chain with frozen blocks and a longer side
  1579. // chain, where a recent block - newer than the ancient limit - was already committed
  1580. // to disk and then sethead was called. In this case the freezer will delete the
  1581. // sidechain since it's dangling, reverting to TestLongShallowSetHead.
  1582. func TestLongReorgedShallowSetHead(t *testing.T) { testLongReorgedShallowSetHead(t, false) }
  1583. func TestLongReorgedShallowSetHeadWithSnapshots(t *testing.T) { testLongReorgedShallowSetHead(t, true) }
  1584. func testLongReorgedShallowSetHead(t *testing.T, snapshots bool) {
  1585. // Chain:
  1586. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1587. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12->S13->S14->S15->S16->S17->S18->S19->S20->S21->S22->S23->S24->S25->S26
  1588. //
  1589. // Frozen:
  1590. // G->C1->C2
  1591. //
  1592. // Commit: G, C4
  1593. // Pivot : none
  1594. //
  1595. // SetHead(6)
  1596. //
  1597. // ------------------------------
  1598. //
  1599. // Expected in freezer:
  1600. // G->C1->C2
  1601. //
  1602. // Expected in leveldb:
  1603. // C2)->C3->C4->C5->C6
  1604. //
  1605. // Expected head header : C6
  1606. // Expected head fast block: C6
  1607. // Expected head block : C4
  1608. testSetHead(t, &rewindTest{
  1609. canonicalBlocks: 18,
  1610. sidechainBlocks: 26,
  1611. freezeThreshold: 16,
  1612. commitBlock: 4,
  1613. pivotBlock: nil,
  1614. setheadBlock: 6,
  1615. expCanonicalBlocks: 6,
  1616. expSidechainBlocks: 0,
  1617. expFrozen: 3,
  1618. expHeadHeader: 6,
  1619. expHeadFastBlock: 6,
  1620. expHeadBlock: 4,
  1621. }, snapshots)
  1622. }
  1623. // Tests a sethead for a long canonical chain with frozen blocks and a longer side
  1624. // chain, where a recent block - older than the ancient limit - was already committed
  1625. // to disk and then sethead was called. In this case the freezer will delete the
  1626. // sidechain since it's dangling, reverting to TestLongDeepSetHead.
  1627. func TestLongReorgedDeepSetHead(t *testing.T) { testLongReorgedDeepSetHead(t, false) }
  1628. func TestLongReorgedDeepSetHeadWithSnapshots(t *testing.T) { testLongReorgedDeepSetHead(t, true) }
  1629. func testLongReorgedDeepSetHead(t *testing.T, snapshots bool) {
  1630. // Chain:
  1631. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1632. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12->S13->S14->S15->S16->S17->S18->S19->S20->S21->S22->S23->S24->S25->S26
  1633. //
  1634. // Frozen:
  1635. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1636. //
  1637. // Commit: G, C4
  1638. // Pivot : none
  1639. //
  1640. // SetHead(6)
  1641. //
  1642. // ------------------------------
  1643. //
  1644. // Expected in freezer:
  1645. // G->C1->C2->C3->C4
  1646. //
  1647. // Expected in leveldb: none
  1648. //
  1649. // Expected head header : C4
  1650. // Expected head fast block: C4
  1651. // Expected head block : C4
  1652. testSetHead(t, &rewindTest{
  1653. canonicalBlocks: 24,
  1654. sidechainBlocks: 26,
  1655. freezeThreshold: 16,
  1656. commitBlock: 4,
  1657. pivotBlock: nil,
  1658. setheadBlock: 6,
  1659. expCanonicalBlocks: 4,
  1660. expSidechainBlocks: 0,
  1661. expFrozen: 5,
  1662. expHeadHeader: 4,
  1663. expHeadFastBlock: 4,
  1664. expHeadBlock: 4,
  1665. }, snapshots)
  1666. }
  1667. // Tests a sethead for a long canonical chain with frozen blocks and a longer
  1668. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1669. // was already committed to disk and then sethead was called. In this case the
  1670. // freezer will delete the sidechain since it's dangling, reverting to
  1671. // TestLongSnapSyncedShallowSetHead.
  1672. func TestLongReorgedSnapSyncedShallowSetHead(t *testing.T) {
  1673. testLongReorgedSnapSyncedShallowSetHead(t, false)
  1674. }
  1675. func TestLongReorgedSnapSyncedShallowSetHeadWithSnapshots(t *testing.T) {
  1676. testLongReorgedSnapSyncedShallowSetHead(t, true)
  1677. }
  1678. func testLongReorgedSnapSyncedShallowSetHead(t *testing.T, snapshots bool) {
  1679. // Chain:
  1680. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1681. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12->S13->S14->S15->S16->S17->S18->S19->S20->S21->S22->S23->S24->S25->S26
  1682. //
  1683. // Frozen:
  1684. // G->C1->C2
  1685. //
  1686. // Commit: G, C4
  1687. // Pivot : C4
  1688. //
  1689. // SetHead(6)
  1690. //
  1691. // ------------------------------
  1692. //
  1693. // Expected in freezer:
  1694. // G->C1->C2
  1695. //
  1696. // Expected in leveldb:
  1697. // C2)->C3->C4->C5->C6
  1698. //
  1699. // Expected head header : C6
  1700. // Expected head fast block: C6
  1701. // Expected head block : C4
  1702. testSetHead(t, &rewindTest{
  1703. canonicalBlocks: 18,
  1704. sidechainBlocks: 26,
  1705. freezeThreshold: 16,
  1706. commitBlock: 4,
  1707. pivotBlock: uint64ptr(4),
  1708. setheadBlock: 6,
  1709. expCanonicalBlocks: 6,
  1710. expSidechainBlocks: 0,
  1711. expFrozen: 3,
  1712. expHeadHeader: 6,
  1713. expHeadFastBlock: 6,
  1714. expHeadBlock: 4,
  1715. }, snapshots)
  1716. }
  1717. // Tests a sethead for a long canonical chain with frozen blocks and a longer
  1718. // side chain, where the fast sync pivot point - older than the ancient limit -
  1719. // was already committed to disk and then sethead was called. In this case the
  1720. // freezer will delete the sidechain since it's dangling, reverting to
  1721. // TestLongSnapSyncedDeepSetHead.
  1722. func TestLongReorgedSnapSyncedDeepSetHead(t *testing.T) {
  1723. testLongReorgedSnapSyncedDeepSetHead(t, false)
  1724. }
  1725. func TestLongReorgedSnapSyncedDeepSetHeadWithSnapshots(t *testing.T) {
  1726. testLongReorgedSnapSyncedDeepSetHead(t, true)
  1727. }
  1728. func testLongReorgedSnapSyncedDeepSetHead(t *testing.T, snapshots bool) {
  1729. // Chain:
  1730. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1731. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12->S13->S14->S15->S16->S17->S18->S19->S20->S21->S22->S23->S24->S25->S26
  1732. //
  1733. // Frozen:
  1734. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1735. //
  1736. // Commit: G, C4
  1737. // Pivot : C4
  1738. //
  1739. // SetHead(6)
  1740. //
  1741. // ------------------------------
  1742. //
  1743. // Expected in freezer:
  1744. // G->C1->C2->C3->C4
  1745. //
  1746. // Expected in leveldb: none
  1747. //
  1748. // Expected head header : C4
  1749. // Expected head fast block: C4
  1750. // Expected head block : C4
  1751. testSetHead(t, &rewindTest{
  1752. canonicalBlocks: 24,
  1753. sidechainBlocks: 26,
  1754. freezeThreshold: 16,
  1755. commitBlock: 4,
  1756. pivotBlock: uint64ptr(4),
  1757. setheadBlock: 6,
  1758. expCanonicalBlocks: 4,
  1759. expSidechainBlocks: 0,
  1760. expFrozen: 5,
  1761. expHeadHeader: 4,
  1762. expHeadFastBlock: 4,
  1763. expHeadBlock: 4,
  1764. }, snapshots)
  1765. }
  1766. // Tests a sethead for a long canonical chain with frozen blocks and a longer
  1767. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1768. // was not yet committed, but sethead was called. In this case we expect the
  1769. // chain to detect that it was fast syncing and delete everything from the new
  1770. // head, since we can just pick up fast syncing from there. The side chain is
  1771. // completely nuked by the freezer.
  1772. func TestLongReorgedSnapSyncingShallowSetHead(t *testing.T) {
  1773. testLongReorgedSnapSyncingShallowSetHead(t, false)
  1774. }
  1775. func TestLongReorgedSnapSyncingShallowSetHeadWithSnapshots(t *testing.T) {
  1776. testLongReorgedSnapSyncingShallowSetHead(t, true)
  1777. }
  1778. func testLongReorgedSnapSyncingShallowSetHead(t *testing.T, snapshots bool) {
  1779. // Chain:
  1780. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1781. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12->S13->S14->S15->S16->S17->S18->S19->S20->S21->S22->S23->S24->S25->S26
  1782. //
  1783. // Frozen:
  1784. // G->C1->C2
  1785. //
  1786. // Commit: G
  1787. // Pivot : C4
  1788. //
  1789. // SetHead(6)
  1790. //
  1791. // ------------------------------
  1792. //
  1793. // Expected in freezer:
  1794. // G->C1->C2
  1795. //
  1796. // Expected in leveldb:
  1797. // C2)->C3->C4->C5->C6
  1798. //
  1799. // Expected head header : C6
  1800. // Expected head fast block: C6
  1801. // Expected head block : G
  1802. testSetHead(t, &rewindTest{
  1803. canonicalBlocks: 18,
  1804. sidechainBlocks: 26,
  1805. freezeThreshold: 16,
  1806. commitBlock: 0,
  1807. pivotBlock: uint64ptr(4),
  1808. setheadBlock: 6,
  1809. expCanonicalBlocks: 6,
  1810. expSidechainBlocks: 0,
  1811. expFrozen: 3,
  1812. expHeadHeader: 6,
  1813. expHeadFastBlock: 6,
  1814. expHeadBlock: 0,
  1815. }, snapshots)
  1816. }
  1817. // Tests a sethead for a long canonical chain with frozen blocks and a longer
  1818. // side chain, where the fast sync pivot point - older than the ancient limit -
  1819. // was not yet committed, but sethead was called. In this case we expect the
  1820. // chain to detect that it was fast syncing and delete everything from the new
  1821. // head, since we can just pick up fast syncing from there. The side chain is
  1822. // completely nuked by the freezer.
  1823. func TestLongReorgedSnapSyncingDeepSetHead(t *testing.T) {
  1824. testLongReorgedSnapSyncingDeepSetHead(t, false)
  1825. }
  1826. func TestLongReorgedSnapSyncingDeepSetHeadWithSnapshots(t *testing.T) {
  1827. testLongReorgedSnapSyncingDeepSetHead(t, true)
  1828. }
  1829. func testLongReorgedSnapSyncingDeepSetHead(t *testing.T, snapshots bool) {
  1830. // Chain:
  1831. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24 (HEAD)
  1832. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12->S13->S14->S15->S16->S17->S18->S19->S20->S21->S22->S23->S24->S25->S26
  1833. //
  1834. // Frozen:
  1835. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1836. //
  1837. // Commit: G
  1838. // Pivot : C4
  1839. //
  1840. // SetHead(6)
  1841. //
  1842. // ------------------------------
  1843. //
  1844. // Expected in freezer:
  1845. // G->C1->C2->C3->C4->C5->C6
  1846. //
  1847. // Expected in leveldb: none
  1848. //
  1849. // Expected head header : C6
  1850. // Expected head fast block: C6
  1851. // Expected head block : G
  1852. testSetHead(t, &rewindTest{
  1853. canonicalBlocks: 24,
  1854. sidechainBlocks: 26,
  1855. freezeThreshold: 16,
  1856. commitBlock: 0,
  1857. pivotBlock: uint64ptr(4),
  1858. setheadBlock: 6,
  1859. expCanonicalBlocks: 6,
  1860. expSidechainBlocks: 0,
  1861. expFrozen: 7,
  1862. expHeadHeader: 6,
  1863. expHeadFastBlock: 6,
  1864. expHeadBlock: 0,
  1865. }, snapshots)
  1866. }
  1867. func testSetHead(t *testing.T, tt *rewindTest, snapshots bool) {
  1868. // It's hard to follow the test case, visualize the input
  1869. // log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
  1870. // fmt.Println(tt.dump(false))
  1871. // Create a temporary persistent database
  1872. datadir := t.TempDir()
  1873. db, err := rawdb.NewLevelDBDatabaseWithFreezer(datadir, 0, 0, datadir, "", false)
  1874. if err != nil {
  1875. t.Fatalf("Failed to create persistent database: %v", err)
  1876. }
  1877. defer db.Close()
  1878. // Initialize a fresh chain
  1879. var (
  1880. genesis = (&Genesis{BaseFee: big.NewInt(params.InitialBaseFee)}).MustCommit(db)
  1881. engine = ethash.NewFullFaker()
  1882. config = &CacheConfig{
  1883. TrieCleanLimit: 256,
  1884. TrieDirtyLimit: 256,
  1885. TrieTimeLimit: 5 * time.Minute,
  1886. SnapshotLimit: 0, // Disable snapshot
  1887. }
  1888. )
  1889. if snapshots {
  1890. config.SnapshotLimit = 256
  1891. config.SnapshotWait = true
  1892. }
  1893. chain, err := NewBlockChain(db, config, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil)
  1894. if err != nil {
  1895. t.Fatalf("Failed to create chain: %v", err)
  1896. }
  1897. // If sidechain blocks are needed, make a light chain and import it
  1898. var sideblocks types.Blocks
  1899. if tt.sidechainBlocks > 0 {
  1900. sideblocks, _ = GenerateChain(params.TestChainConfig, genesis, engine, rawdb.NewMemoryDatabase(), tt.sidechainBlocks, func(i int, b *BlockGen) {
  1901. b.SetCoinbase(common.Address{0x01})
  1902. })
  1903. if _, err := chain.InsertChain(sideblocks); err != nil {
  1904. t.Fatalf("Failed to import side chain: %v", err)
  1905. }
  1906. }
  1907. canonblocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, rawdb.NewMemoryDatabase(), tt.canonicalBlocks, func(i int, b *BlockGen) {
  1908. b.SetCoinbase(common.Address{0x02})
  1909. b.SetDifficulty(big.NewInt(1000000))
  1910. })
  1911. if _, err := chain.InsertChain(canonblocks[:tt.commitBlock]); err != nil {
  1912. t.Fatalf("Failed to import canonical chain start: %v", err)
  1913. }
  1914. if tt.commitBlock > 0 {
  1915. chain.stateCache.TrieDB().Commit(canonblocks[tt.commitBlock-1].Root(), true, nil)
  1916. if snapshots {
  1917. if err := chain.snaps.Cap(canonblocks[tt.commitBlock-1].Root(), 0); err != nil {
  1918. t.Fatalf("Failed to flatten snapshots: %v", err)
  1919. }
  1920. }
  1921. }
  1922. if _, err := chain.InsertChain(canonblocks[tt.commitBlock:]); err != nil {
  1923. t.Fatalf("Failed to import canonical chain tail: %v", err)
  1924. }
  1925. // Manually dereference anything not committed to not have to work with 128+ tries
  1926. for _, block := range sideblocks {
  1927. chain.stateCache.TrieDB().Dereference(block.Root())
  1928. }
  1929. for _, block := range canonblocks {
  1930. chain.stateCache.TrieDB().Dereference(block.Root())
  1931. }
  1932. // Force run a freeze cycle
  1933. type freezer interface {
  1934. Freeze(threshold uint64) error
  1935. Ancients() (uint64, error)
  1936. }
  1937. db.(freezer).Freeze(tt.freezeThreshold)
  1938. // Set the simulated pivot block
  1939. if tt.pivotBlock != nil {
  1940. rawdb.WriteLastPivotNumber(db, *tt.pivotBlock)
  1941. }
  1942. // Set the head of the chain back to the requested number
  1943. chain.SetHead(tt.setheadBlock)
  1944. // Iterate over all the remaining blocks and ensure there are no gaps
  1945. verifyNoGaps(t, chain, true, canonblocks)
  1946. verifyNoGaps(t, chain, false, sideblocks)
  1947. verifyCutoff(t, chain, true, canonblocks, tt.expCanonicalBlocks)
  1948. verifyCutoff(t, chain, false, sideblocks, tt.expSidechainBlocks)
  1949. if head := chain.CurrentHeader(); head.Number.Uint64() != tt.expHeadHeader {
  1950. t.Errorf("Head header mismatch: have %d, want %d", head.Number, tt.expHeadHeader)
  1951. }
  1952. if head := chain.CurrentFastBlock(); head.NumberU64() != tt.expHeadFastBlock {
  1953. t.Errorf("Head fast block mismatch: have %d, want %d", head.NumberU64(), tt.expHeadFastBlock)
  1954. }
  1955. if head := chain.CurrentBlock(); head.NumberU64() != tt.expHeadBlock {
  1956. t.Errorf("Head block mismatch: have %d, want %d", head.NumberU64(), tt.expHeadBlock)
  1957. }
  1958. if frozen, err := db.(freezer).Ancients(); err != nil {
  1959. t.Errorf("Failed to retrieve ancient count: %v\n", err)
  1960. } else if int(frozen) != tt.expFrozen {
  1961. t.Errorf("Frozen block count mismatch: have %d, want %d", frozen, tt.expFrozen)
  1962. }
  1963. }
  1964. // verifyNoGaps checks that there are no gaps after the initial set of blocks in
  1965. // the database and errors if found.
  1966. func verifyNoGaps(t *testing.T, chain *BlockChain, canonical bool, inserted types.Blocks) {
  1967. t.Helper()
  1968. var end uint64
  1969. for i := uint64(0); i <= uint64(len(inserted)); i++ {
  1970. header := chain.GetHeaderByNumber(i)
  1971. if header == nil && end == 0 {
  1972. end = i
  1973. }
  1974. if header != nil && end > 0 {
  1975. if canonical {
  1976. t.Errorf("Canonical header gap between #%d-#%d", end, i-1)
  1977. } else {
  1978. t.Errorf("Sidechain header gap between #%d-#%d", end, i-1)
  1979. }
  1980. end = 0 // Reset for further gap detection
  1981. }
  1982. }
  1983. end = 0
  1984. for i := uint64(0); i <= uint64(len(inserted)); i++ {
  1985. block := chain.GetBlockByNumber(i)
  1986. if block == nil && end == 0 {
  1987. end = i
  1988. }
  1989. if block != nil && end > 0 {
  1990. if canonical {
  1991. t.Errorf("Canonical block gap between #%d-#%d", end, i-1)
  1992. } else {
  1993. t.Errorf("Sidechain block gap between #%d-#%d", end, i-1)
  1994. }
  1995. end = 0 // Reset for further gap detection
  1996. }
  1997. }
  1998. end = 0
  1999. for i := uint64(1); i <= uint64(len(inserted)); i++ {
  2000. receipts := chain.GetReceiptsByHash(inserted[i-1].Hash())
  2001. if receipts == nil && end == 0 {
  2002. end = i
  2003. }
  2004. if receipts != nil && end > 0 {
  2005. if canonical {
  2006. t.Errorf("Canonical receipt gap between #%d-#%d", end, i-1)
  2007. } else {
  2008. t.Errorf("Sidechain receipt gap between #%d-#%d", end, i-1)
  2009. }
  2010. end = 0 // Reset for further gap detection
  2011. }
  2012. }
  2013. }
  2014. // verifyCutoff checks that there are no chain data available in the chain after
  2015. // the specified limit, but that it is available before.
  2016. func verifyCutoff(t *testing.T, chain *BlockChain, canonical bool, inserted types.Blocks, head int) {
  2017. t.Helper()
  2018. for i := 1; i <= len(inserted); i++ {
  2019. if i <= head {
  2020. if header := chain.GetHeader(inserted[i-1].Hash(), uint64(i)); header == nil {
  2021. if canonical {
  2022. t.Errorf("Canonical header #%2d [%x...] missing before cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2023. } else {
  2024. t.Errorf("Sidechain header #%2d [%x...] missing before cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2025. }
  2026. }
  2027. if block := chain.GetBlock(inserted[i-1].Hash(), uint64(i)); block == nil {
  2028. if canonical {
  2029. t.Errorf("Canonical block #%2d [%x...] missing before cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2030. } else {
  2031. t.Errorf("Sidechain block #%2d [%x...] missing before cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2032. }
  2033. }
  2034. if receipts := chain.GetReceiptsByHash(inserted[i-1].Hash()); receipts == nil {
  2035. if canonical {
  2036. t.Errorf("Canonical receipts #%2d [%x...] missing before cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2037. } else {
  2038. t.Errorf("Sidechain receipts #%2d [%x...] missing before cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2039. }
  2040. }
  2041. } else {
  2042. if header := chain.GetHeader(inserted[i-1].Hash(), uint64(i)); header != nil {
  2043. if canonical {
  2044. t.Errorf("Canonical header #%2d [%x...] present after cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2045. } else {
  2046. t.Errorf("Sidechain header #%2d [%x...] present after cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2047. }
  2048. }
  2049. if block := chain.GetBlock(inserted[i-1].Hash(), uint64(i)); block != nil {
  2050. if canonical {
  2051. t.Errorf("Canonical block #%2d [%x...] present after cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2052. } else {
  2053. t.Errorf("Sidechain block #%2d [%x...] present after cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2054. }
  2055. }
  2056. if receipts := chain.GetReceiptsByHash(inserted[i-1].Hash()); receipts != nil {
  2057. if canonical {
  2058. t.Errorf("Canonical receipts #%2d [%x...] present after cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2059. } else {
  2060. t.Errorf("Sidechain receipts #%2d [%x...] present after cap %d", inserted[i-1].Number(), inserted[i-1].Hash().Bytes()[:3], head)
  2061. }
  2062. }
  2063. }
  2064. }
  2065. }
  2066. // uint64ptr is a weird helper to allow 1-line constant pointer creation.
  2067. func uint64ptr(n uint64) *uint64 {
  2068. return &n
  2069. }