blockchain_repair_test.go 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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 abnormal program termination (i.e.crash) and restart doesn't leave
  17. // the database in some strange state with gaps in the chain, nor with block data
  18. // dangling in the future.
  19. package core
  20. import (
  21. "io/ioutil"
  22. "math/big"
  23. "os"
  24. "testing"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/consensus/ethash"
  28. "github.com/ethereum/go-ethereum/core/rawdb"
  29. "github.com/ethereum/go-ethereum/core/types"
  30. "github.com/ethereum/go-ethereum/core/vm"
  31. "github.com/ethereum/go-ethereum/params"
  32. )
  33. // Tests a recovery for a short canonical chain where a recent block was already
  34. // committed to disk and then the process crashed. In this case we expect the full
  35. // chain to be rolled back to the committed block, but the chain data itself left
  36. // in the database for replaying.
  37. func TestShortRepair(t *testing.T) { testShortRepair(t, false) }
  38. func TestShortRepairWithSnapshots(t *testing.T) { testShortRepair(t, true) }
  39. func testShortRepair(t *testing.T, snapshots bool) {
  40. // Chain:
  41. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  42. //
  43. // Frozen: none
  44. // Commit: G, C4
  45. // Pivot : none
  46. //
  47. // CRASH
  48. //
  49. // ------------------------------
  50. //
  51. // Expected in leveldb:
  52. // G->C1->C2->C3->C4->C5->C6->C7->C8
  53. //
  54. // Expected head header : C8
  55. // Expected head fast block: C8
  56. // Expected head block : C4
  57. testRepair(t, &rewindTest{
  58. canonicalBlocks: 8,
  59. sidechainBlocks: 0,
  60. freezeThreshold: 16,
  61. commitBlock: 4,
  62. pivotBlock: nil,
  63. expCanonicalBlocks: 8,
  64. expSidechainBlocks: 0,
  65. expFrozen: 0,
  66. expHeadHeader: 8,
  67. expHeadFastBlock: 8,
  68. expHeadBlock: 4,
  69. }, snapshots)
  70. }
  71. // Tests a recovery for a short canonical chain where the fast sync pivot point was
  72. // already committed, after which the process crashed. In this case we expect the full
  73. // chain to be rolled back to the committed block, but the chain data itself left in
  74. // the database for replaying.
  75. func TestShortFastSyncedRepair(t *testing.T) { testShortFastSyncedRepair(t, false) }
  76. func TestShortFastSyncedRepairWithSnapshots(t *testing.T) { testShortFastSyncedRepair(t, true) }
  77. func testShortFastSyncedRepair(t *testing.T, snapshots bool) {
  78. // Chain:
  79. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  80. //
  81. // Frozen: none
  82. // Commit: G, C4
  83. // Pivot : C4
  84. //
  85. // CRASH
  86. //
  87. // ------------------------------
  88. //
  89. // Expected in leveldb:
  90. // G->C1->C2->C3->C4->C5->C6->C7->C8
  91. //
  92. // Expected head header : C8
  93. // Expected head fast block: C8
  94. // Expected head block : C4
  95. testRepair(t, &rewindTest{
  96. canonicalBlocks: 8,
  97. sidechainBlocks: 0,
  98. freezeThreshold: 16,
  99. commitBlock: 4,
  100. pivotBlock: uint64ptr(4),
  101. expCanonicalBlocks: 8,
  102. expSidechainBlocks: 0,
  103. expFrozen: 0,
  104. expHeadHeader: 8,
  105. expHeadFastBlock: 8,
  106. expHeadBlock: 4,
  107. }, snapshots)
  108. }
  109. // Tests a recovery for a short canonical chain where the fast sync pivot point was
  110. // not yet committed, but the process crashed. In this case we expect the chain to
  111. // detect that it was fast syncing and not delete anything, since we can just pick
  112. // up directly where we left off.
  113. func TestShortFastSyncingRepair(t *testing.T) { testShortFastSyncingRepair(t, false) }
  114. func TestShortFastSyncingRepairWithSnapshots(t *testing.T) { testShortFastSyncingRepair(t, true) }
  115. func testShortFastSyncingRepair(t *testing.T, snapshots bool) {
  116. // Chain:
  117. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  118. //
  119. // Frozen: none
  120. // Commit: G
  121. // Pivot : C4
  122. //
  123. // CRASH
  124. //
  125. // ------------------------------
  126. //
  127. // Expected in leveldb:
  128. // G->C1->C2->C3->C4->C5->C6->C7->C8
  129. //
  130. // Expected head header : C8
  131. // Expected head fast block: C8
  132. // Expected head block : G
  133. testRepair(t, &rewindTest{
  134. canonicalBlocks: 8,
  135. sidechainBlocks: 0,
  136. freezeThreshold: 16,
  137. commitBlock: 0,
  138. pivotBlock: uint64ptr(4),
  139. expCanonicalBlocks: 8,
  140. expSidechainBlocks: 0,
  141. expFrozen: 0,
  142. expHeadHeader: 8,
  143. expHeadFastBlock: 8,
  144. expHeadBlock: 0,
  145. }, snapshots)
  146. }
  147. // Tests a recovery for a short canonical chain and a shorter side chain, where a
  148. // recent block was already committed to disk and then the process crashed. In this
  149. // test scenario the side chain is below the committed block. In this case we expect
  150. // the canonical chain to be rolled back to the committed block, but the chain data
  151. // itself left in the database for replaying.
  152. func TestShortOldForkedRepair(t *testing.T) { testShortOldForkedRepair(t, false) }
  153. func TestShortOldForkedRepairWithSnapshots(t *testing.T) { testShortOldForkedRepair(t, true) }
  154. func testShortOldForkedRepair(t *testing.T, snapshots bool) {
  155. // Chain:
  156. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  157. // └->S1->S2->S3
  158. //
  159. // Frozen: none
  160. // Commit: G, C4
  161. // Pivot : none
  162. //
  163. // CRASH
  164. //
  165. // ------------------------------
  166. //
  167. // Expected in leveldb:
  168. // G->C1->C2->C3->C4->C5->C6->C7->C8
  169. // └->S1->S2->S3
  170. //
  171. // Expected head header : C8
  172. // Expected head fast block: C8
  173. // Expected head block : C4
  174. testRepair(t, &rewindTest{
  175. canonicalBlocks: 8,
  176. sidechainBlocks: 3,
  177. freezeThreshold: 16,
  178. commitBlock: 4,
  179. pivotBlock: nil,
  180. expCanonicalBlocks: 8,
  181. expSidechainBlocks: 3,
  182. expFrozen: 0,
  183. expHeadHeader: 8,
  184. expHeadFastBlock: 8,
  185. expHeadBlock: 4,
  186. }, snapshots)
  187. }
  188. // Tests a recovery for a short canonical chain and a shorter side chain, where
  189. // the fast sync pivot point was already committed to disk and then the process
  190. // crashed. In this test scenario the side chain is below the committed block. In
  191. // this case we expect the canonical chain to be rolled back to the committed block,
  192. // but the chain data itself left in the database for replaying.
  193. func TestShortOldForkedFastSyncedRepair(t *testing.T) {
  194. testShortOldForkedFastSyncedRepair(t, false)
  195. }
  196. func TestShortOldForkedFastSyncedRepairWithSnapshots(t *testing.T) {
  197. testShortOldForkedFastSyncedRepair(t, true)
  198. }
  199. func testShortOldForkedFastSyncedRepair(t *testing.T, snapshots bool) {
  200. // Chain:
  201. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  202. // └->S1->S2->S3
  203. //
  204. // Frozen: none
  205. // Commit: G, C4
  206. // Pivot : C4
  207. //
  208. // CRASH
  209. //
  210. // ------------------------------
  211. //
  212. // Expected in leveldb:
  213. // G->C1->C2->C3->C4->C5->C6->C7->C8
  214. // └->S1->S2->S3
  215. //
  216. // Expected head header : C8
  217. // Expected head fast block: C8
  218. // Expected head block : C4
  219. testRepair(t, &rewindTest{
  220. canonicalBlocks: 8,
  221. sidechainBlocks: 3,
  222. freezeThreshold: 16,
  223. commitBlock: 4,
  224. pivotBlock: uint64ptr(4),
  225. expCanonicalBlocks: 8,
  226. expSidechainBlocks: 3,
  227. expFrozen: 0,
  228. expHeadHeader: 8,
  229. expHeadFastBlock: 8,
  230. expHeadBlock: 4,
  231. }, snapshots)
  232. }
  233. // Tests a recovery for a short canonical chain and a shorter side chain, where
  234. // the fast sync pivot point was not yet committed, but the process crashed. In this
  235. // test scenario the side chain is below the committed block. In this case we expect
  236. // the chain to detect that it was fast syncing and not delete anything, since we
  237. // can just pick up directly where we left off.
  238. func TestShortOldForkedFastSyncingRepair(t *testing.T) {
  239. testShortOldForkedFastSyncingRepair(t, false)
  240. }
  241. func TestShortOldForkedFastSyncingRepairWithSnapshots(t *testing.T) {
  242. testShortOldForkedFastSyncingRepair(t, true)
  243. }
  244. func testShortOldForkedFastSyncingRepair(t *testing.T, snapshots bool) {
  245. // Chain:
  246. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  247. // └->S1->S2->S3
  248. //
  249. // Frozen: none
  250. // Commit: G
  251. // Pivot : C4
  252. //
  253. // CRASH
  254. //
  255. // ------------------------------
  256. //
  257. // Expected in leveldb:
  258. // G->C1->C2->C3->C4->C5->C6->C7->C8
  259. // └->S1->S2->S3
  260. //
  261. // Expected head header : C8
  262. // Expected head fast block: C8
  263. // Expected head block : G
  264. testRepair(t, &rewindTest{
  265. canonicalBlocks: 8,
  266. sidechainBlocks: 3,
  267. freezeThreshold: 16,
  268. commitBlock: 0,
  269. pivotBlock: uint64ptr(4),
  270. expCanonicalBlocks: 8,
  271. expSidechainBlocks: 3,
  272. expFrozen: 0,
  273. expHeadHeader: 8,
  274. expHeadFastBlock: 8,
  275. expHeadBlock: 0,
  276. }, snapshots)
  277. }
  278. // Tests a recovery for a short canonical chain and a shorter side chain, where a
  279. // recent block was already committed to disk and then the process crashed. In this
  280. // test scenario the side chain reaches above the committed block. In this case we
  281. // expect the canonical chain to be rolled back to the committed block, but the
  282. // chain data itself left in the database for replaying.
  283. func TestShortNewlyForkedRepair(t *testing.T) { testShortNewlyForkedRepair(t, false) }
  284. func TestShortNewlyForkedRepairWithSnapshots(t *testing.T) { testShortNewlyForkedRepair(t, true) }
  285. func testShortNewlyForkedRepair(t *testing.T, snapshots bool) {
  286. // Chain:
  287. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  288. // └->S1->S2->S3->S4->S5->S6
  289. //
  290. // Frozen: none
  291. // Commit: G, C4
  292. // Pivot : none
  293. //
  294. // CRASH
  295. //
  296. // ------------------------------
  297. //
  298. // Expected in leveldb:
  299. // G->C1->C2->C3->C4->C5->C6->C7->C8
  300. // └->S1->S2->S3->S4->S5->S6
  301. //
  302. // Expected head header : C8
  303. // Expected head fast block: C8
  304. // Expected head block : C4
  305. testRepair(t, &rewindTest{
  306. canonicalBlocks: 8,
  307. sidechainBlocks: 6,
  308. freezeThreshold: 16,
  309. commitBlock: 4,
  310. pivotBlock: nil,
  311. expCanonicalBlocks: 8,
  312. expSidechainBlocks: 6,
  313. expFrozen: 0,
  314. expHeadHeader: 8,
  315. expHeadFastBlock: 8,
  316. expHeadBlock: 4,
  317. }, snapshots)
  318. }
  319. // Tests a recovery for a short canonical chain and a shorter side chain, where
  320. // the fast sync pivot point was already committed to disk and then the process
  321. // crashed. In this test scenario the side chain reaches above the committed block.
  322. // In this case we expect the canonical chain to be rolled back to the committed
  323. // block, but the chain data itself left in the database for replaying.
  324. func TestShortNewlyForkedFastSyncedRepair(t *testing.T) {
  325. testShortNewlyForkedFastSyncedRepair(t, false)
  326. }
  327. func TestShortNewlyForkedFastSyncedRepairWithSnapshots(t *testing.T) {
  328. testShortNewlyForkedFastSyncedRepair(t, true)
  329. }
  330. func testShortNewlyForkedFastSyncedRepair(t *testing.T, snapshots bool) {
  331. // Chain:
  332. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  333. // └->S1->S2->S3->S4->S5->S6
  334. //
  335. // Frozen: none
  336. // Commit: G, C4
  337. // Pivot : C4
  338. //
  339. // CRASH
  340. //
  341. // ------------------------------
  342. //
  343. // Expected in leveldb:
  344. // G->C1->C2->C3->C4->C5->C6->C7->C8
  345. // └->S1->S2->S3->S4->S5->S6
  346. //
  347. // Expected head header : C8
  348. // Expected head fast block: C8
  349. // Expected head block : C4
  350. testRepair(t, &rewindTest{
  351. canonicalBlocks: 8,
  352. sidechainBlocks: 6,
  353. freezeThreshold: 16,
  354. commitBlock: 4,
  355. pivotBlock: uint64ptr(4),
  356. expCanonicalBlocks: 8,
  357. expSidechainBlocks: 6,
  358. expFrozen: 0,
  359. expHeadHeader: 8,
  360. expHeadFastBlock: 8,
  361. expHeadBlock: 4,
  362. }, snapshots)
  363. }
  364. // Tests a recovery for a short canonical chain and a shorter side chain, where
  365. // the fast sync pivot point was not yet committed, but the process crashed. In
  366. // this test scenario the side chain reaches above the committed block. In this
  367. // case we expect the chain to detect that it was fast syncing and not delete
  368. // anything, since we can just pick up directly where we left off.
  369. func TestShortNewlyForkedFastSyncingRepair(t *testing.T) {
  370. testShortNewlyForkedFastSyncingRepair(t, false)
  371. }
  372. func TestShortNewlyForkedFastSyncingRepairWithSnapshots(t *testing.T) {
  373. testShortNewlyForkedFastSyncingRepair(t, true)
  374. }
  375. func testShortNewlyForkedFastSyncingRepair(t *testing.T, snapshots bool) {
  376. // Chain:
  377. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  378. // └->S1->S2->S3->S4->S5->S6
  379. //
  380. // Frozen: none
  381. // Commit: G
  382. // Pivot : C4
  383. //
  384. // CRASH
  385. //
  386. // ------------------------------
  387. //
  388. // Expected in leveldb:
  389. // G->C1->C2->C3->C4->C5->C6->C7->C8
  390. // └->S1->S2->S3->S4->S5->S6
  391. //
  392. // Expected head header : C8
  393. // Expected head fast block: C8
  394. // Expected head block : G
  395. testRepair(t, &rewindTest{
  396. canonicalBlocks: 8,
  397. sidechainBlocks: 6,
  398. freezeThreshold: 16,
  399. commitBlock: 0,
  400. pivotBlock: uint64ptr(4),
  401. expCanonicalBlocks: 8,
  402. expSidechainBlocks: 6,
  403. expFrozen: 0,
  404. expHeadHeader: 8,
  405. expHeadFastBlock: 8,
  406. expHeadBlock: 0,
  407. }, snapshots)
  408. }
  409. // Tests a recovery for a short canonical chain and a longer side chain, where a
  410. // recent block was already committed to disk and then the process crashed. In this
  411. // case we expect the canonical chain to be rolled back to the committed block, but
  412. // the chain data itself left in the database for replaying.
  413. func TestShortReorgedRepair(t *testing.T) { testShortReorgedRepair(t, false) }
  414. func TestShortReorgedRepairWithSnapshots(t *testing.T) { testShortReorgedRepair(t, true) }
  415. func testShortReorgedRepair(t *testing.T, snapshots bool) {
  416. // Chain:
  417. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  418. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  419. //
  420. // Frozen: none
  421. // Commit: G, C4
  422. // Pivot : none
  423. //
  424. // CRASH
  425. //
  426. // ------------------------------
  427. //
  428. // Expected in leveldb:
  429. // G->C1->C2->C3->C4->C5->C6->C7->C8
  430. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  431. //
  432. // Expected head header : C8
  433. // Expected head fast block: C8
  434. // Expected head block : C4
  435. testRepair(t, &rewindTest{
  436. canonicalBlocks: 8,
  437. sidechainBlocks: 10,
  438. freezeThreshold: 16,
  439. commitBlock: 4,
  440. pivotBlock: nil,
  441. expCanonicalBlocks: 8,
  442. expSidechainBlocks: 10,
  443. expFrozen: 0,
  444. expHeadHeader: 8,
  445. expHeadFastBlock: 8,
  446. expHeadBlock: 4,
  447. }, snapshots)
  448. }
  449. // Tests a recovery for a short canonical chain and a longer side chain, where
  450. // the fast sync pivot point was already committed to disk and then the process
  451. // crashed. In this case we expect the canonical chain to be rolled back to the
  452. // committed block, but the chain data itself left in the database for replaying.
  453. func TestShortReorgedFastSyncedRepair(t *testing.T) {
  454. testShortReorgedFastSyncedRepair(t, false)
  455. }
  456. func TestShortReorgedFastSyncedRepairWithSnapshots(t *testing.T) {
  457. testShortReorgedFastSyncedRepair(t, true)
  458. }
  459. func testShortReorgedFastSyncedRepair(t *testing.T, snapshots bool) {
  460. // Chain:
  461. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  462. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  463. //
  464. // Frozen: none
  465. // Commit: G, C4
  466. // Pivot : C4
  467. //
  468. // CRASH
  469. //
  470. // ------------------------------
  471. //
  472. // Expected in leveldb:
  473. // G->C1->C2->C3->C4->C5->C6->C7->C8
  474. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  475. //
  476. // Expected head header : C8
  477. // Expected head fast block: C8
  478. // Expected head block : C4
  479. testRepair(t, &rewindTest{
  480. canonicalBlocks: 8,
  481. sidechainBlocks: 10,
  482. freezeThreshold: 16,
  483. commitBlock: 4,
  484. pivotBlock: uint64ptr(4),
  485. expCanonicalBlocks: 8,
  486. expSidechainBlocks: 10,
  487. expFrozen: 0,
  488. expHeadHeader: 8,
  489. expHeadFastBlock: 8,
  490. expHeadBlock: 4,
  491. }, snapshots)
  492. }
  493. // Tests a recovery for a short canonical chain and a longer side chain, where
  494. // the fast sync pivot point was not yet committed, but the process crashed. In
  495. // this case we expect the chain to detect that it was fast syncing and not delete
  496. // anything, since we can just pick up directly where we left off.
  497. func TestShortReorgedFastSyncingRepair(t *testing.T) {
  498. testShortReorgedFastSyncingRepair(t, false)
  499. }
  500. func TestShortReorgedFastSyncingRepairWithSnapshots(t *testing.T) {
  501. testShortReorgedFastSyncingRepair(t, true)
  502. }
  503. func testShortReorgedFastSyncingRepair(t *testing.T, snapshots bool) {
  504. // Chain:
  505. // G->C1->C2->C3->C4->C5->C6->C7->C8 (HEAD)
  506. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  507. //
  508. // Frozen: none
  509. // Commit: G
  510. // Pivot : C4
  511. //
  512. // CRASH
  513. //
  514. // ------------------------------
  515. //
  516. // Expected in leveldb:
  517. // G->C1->C2->C3->C4->C5->C6->C7->C8
  518. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10
  519. //
  520. // Expected head header : C8
  521. // Expected head fast block: C8
  522. // Expected head block : G
  523. testRepair(t, &rewindTest{
  524. canonicalBlocks: 8,
  525. sidechainBlocks: 10,
  526. freezeThreshold: 16,
  527. commitBlock: 0,
  528. pivotBlock: uint64ptr(4),
  529. expCanonicalBlocks: 8,
  530. expSidechainBlocks: 10,
  531. expFrozen: 0,
  532. expHeadHeader: 8,
  533. expHeadFastBlock: 8,
  534. expHeadBlock: 0,
  535. }, snapshots)
  536. }
  537. // Tests a recovery for a long canonical chain with frozen blocks where a recent
  538. // block - newer than the ancient limit - was already committed to disk and then
  539. // the process crashed. In this case we expect the chain to be rolled back to the
  540. // committed block, with everything afterwads kept as fast sync data.
  541. func TestLongShallowRepair(t *testing.T) { testLongShallowRepair(t, false) }
  542. func TestLongShallowRepairWithSnapshots(t *testing.T) { testLongShallowRepair(t, true) }
  543. func testLongShallowRepair(t *testing.T, snapshots bool) {
  544. // Chain:
  545. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  546. //
  547. // Frozen:
  548. // G->C1->C2
  549. //
  550. // Commit: G, C4
  551. // Pivot : none
  552. //
  553. // CRASH
  554. //
  555. // ------------------------------
  556. //
  557. // Expected in freezer:
  558. // G->C1->C2
  559. //
  560. // Expected in leveldb:
  561. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  562. //
  563. // Expected head header : C18
  564. // Expected head fast block: C18
  565. // Expected head block : C4
  566. testRepair(t, &rewindTest{
  567. canonicalBlocks: 18,
  568. sidechainBlocks: 0,
  569. freezeThreshold: 16,
  570. commitBlock: 4,
  571. pivotBlock: nil,
  572. expCanonicalBlocks: 18,
  573. expSidechainBlocks: 0,
  574. expFrozen: 3,
  575. expHeadHeader: 18,
  576. expHeadFastBlock: 18,
  577. expHeadBlock: 4,
  578. }, snapshots)
  579. }
  580. // Tests a recovery for a long canonical chain with frozen blocks where a recent
  581. // block - older than the ancient limit - was already committed to disk and then
  582. // the process crashed. In this case we expect the chain to be rolled back to the
  583. // committed block, with everything afterwads deleted.
  584. func TestLongDeepRepair(t *testing.T) { testLongDeepRepair(t, false) }
  585. func TestLongDeepRepairWithSnapshots(t *testing.T) { testLongDeepRepair(t, true) }
  586. func testLongDeepRepair(t *testing.T, snapshots bool) {
  587. // Chain:
  588. // 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)
  589. //
  590. // Frozen:
  591. // G->C1->C2->C3->C4->C5->C6->C7->C8
  592. //
  593. // Commit: G, C4
  594. // Pivot : none
  595. //
  596. // CRASH
  597. //
  598. // ------------------------------
  599. //
  600. // Expected in freezer:
  601. // G->C1->C2->C3->C4
  602. //
  603. // Expected in leveldb: none
  604. //
  605. // Expected head header : C4
  606. // Expected head fast block: C4
  607. // Expected head block : C4
  608. testRepair(t, &rewindTest{
  609. canonicalBlocks: 24,
  610. sidechainBlocks: 0,
  611. freezeThreshold: 16,
  612. commitBlock: 4,
  613. pivotBlock: nil,
  614. expCanonicalBlocks: 4,
  615. expSidechainBlocks: 0,
  616. expFrozen: 5,
  617. expHeadHeader: 4,
  618. expHeadFastBlock: 4,
  619. expHeadBlock: 4,
  620. }, snapshots)
  621. }
  622. // Tests a recovery for a long canonical chain with frozen blocks where the fast
  623. // sync pivot point - newer than the ancient limit - was already committed, after
  624. // which the process crashed. In this case we expect the chain to be rolled back
  625. // to the committed block, with everything afterwads kept as fast sync data.
  626. func TestLongFastSyncedShallowRepair(t *testing.T) {
  627. testLongFastSyncedShallowRepair(t, false)
  628. }
  629. func TestLongFastSyncedShallowRepairWithSnapshots(t *testing.T) {
  630. testLongFastSyncedShallowRepair(t, true)
  631. }
  632. func testLongFastSyncedShallowRepair(t *testing.T, snapshots bool) {
  633. // Chain:
  634. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  635. //
  636. // Frozen:
  637. // G->C1->C2
  638. //
  639. // Commit: G, C4
  640. // Pivot : C4
  641. //
  642. // CRASH
  643. //
  644. // ------------------------------
  645. //
  646. // Expected in freezer:
  647. // G->C1->C2
  648. //
  649. // Expected in leveldb:
  650. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  651. //
  652. // Expected head header : C18
  653. // Expected head fast block: C18
  654. // Expected head block : C4
  655. testRepair(t, &rewindTest{
  656. canonicalBlocks: 18,
  657. sidechainBlocks: 0,
  658. freezeThreshold: 16,
  659. commitBlock: 4,
  660. pivotBlock: uint64ptr(4),
  661. expCanonicalBlocks: 18,
  662. expSidechainBlocks: 0,
  663. expFrozen: 3,
  664. expHeadHeader: 18,
  665. expHeadFastBlock: 18,
  666. expHeadBlock: 4,
  667. }, snapshots)
  668. }
  669. // Tests a recovery for a long canonical chain with frozen blocks where the fast
  670. // sync pivot point - older than the ancient limit - was already committed, after
  671. // which the process crashed. In this case we expect the chain to be rolled back
  672. // to the committed block, with everything afterwads deleted.
  673. func TestLongFastSyncedDeepRepair(t *testing.T) { testLongFastSyncedDeepRepair(t, false) }
  674. func TestLongFastSyncedDeepRepairWithSnapshots(t *testing.T) { testLongFastSyncedDeepRepair(t, true) }
  675. func testLongFastSyncedDeepRepair(t *testing.T, snapshots bool) {
  676. // Chain:
  677. // 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)
  678. //
  679. // Frozen:
  680. // G->C1->C2->C3->C4->C5->C6->C7->C8
  681. //
  682. // Commit: G, C4
  683. // Pivot : C4
  684. //
  685. // CRASH
  686. //
  687. // ------------------------------
  688. //
  689. // Expected in freezer:
  690. // G->C1->C2->C3->C4
  691. //
  692. // Expected in leveldb: none
  693. //
  694. // Expected head header : C4
  695. // Expected head fast block: C4
  696. // Expected head block : C4
  697. testRepair(t, &rewindTest{
  698. canonicalBlocks: 24,
  699. sidechainBlocks: 0,
  700. freezeThreshold: 16,
  701. commitBlock: 4,
  702. pivotBlock: uint64ptr(4),
  703. expCanonicalBlocks: 4,
  704. expSidechainBlocks: 0,
  705. expFrozen: 5,
  706. expHeadHeader: 4,
  707. expHeadFastBlock: 4,
  708. expHeadBlock: 4,
  709. }, snapshots)
  710. }
  711. // Tests a recovery for a long canonical chain with frozen blocks where the fast
  712. // sync pivot point - older than the ancient limit - was not yet committed, but the
  713. // process crashed. In this case we expect the chain to detect that it was fast
  714. // syncing and not delete anything, since we can just pick up directly where we
  715. // left off.
  716. func TestLongFastSyncingShallowRepair(t *testing.T) {
  717. testLongFastSyncingShallowRepair(t, false)
  718. }
  719. func TestLongFastSyncingShallowRepairWithSnapshots(t *testing.T) {
  720. testLongFastSyncingShallowRepair(t, true)
  721. }
  722. func testLongFastSyncingShallowRepair(t *testing.T, snapshots bool) {
  723. // Chain:
  724. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  725. //
  726. // Frozen:
  727. // G->C1->C2
  728. //
  729. // Commit: G
  730. // Pivot : C4
  731. //
  732. // CRASH
  733. //
  734. // ------------------------------
  735. //
  736. // Expected in freezer:
  737. // G->C1->C2
  738. //
  739. // Expected in leveldb:
  740. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  741. //
  742. // Expected head header : C18
  743. // Expected head fast block: C18
  744. // Expected head block : G
  745. testRepair(t, &rewindTest{
  746. canonicalBlocks: 18,
  747. sidechainBlocks: 0,
  748. freezeThreshold: 16,
  749. commitBlock: 0,
  750. pivotBlock: uint64ptr(4),
  751. expCanonicalBlocks: 18,
  752. expSidechainBlocks: 0,
  753. expFrozen: 3,
  754. expHeadHeader: 18,
  755. expHeadFastBlock: 18,
  756. expHeadBlock: 0,
  757. }, snapshots)
  758. }
  759. // Tests a recovery for a long canonical chain with frozen blocks where the fast
  760. // sync pivot point - newer than the ancient limit - was not yet committed, but the
  761. // process crashed. In this case we expect the chain to detect that it was fast
  762. // syncing and not delete anything, since we can just pick up directly where we
  763. // left off.
  764. func TestLongFastSyncingDeepRepair(t *testing.T) { testLongFastSyncingDeepRepair(t, false) }
  765. func TestLongFastSyncingDeepRepairWithSnapshots(t *testing.T) { testLongFastSyncingDeepRepair(t, true) }
  766. func testLongFastSyncingDeepRepair(t *testing.T, snapshots bool) {
  767. // Chain:
  768. // 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)
  769. //
  770. // Frozen:
  771. // G->C1->C2->C3->C4->C5->C6->C7->C8
  772. //
  773. // Commit: G
  774. // Pivot : C4
  775. //
  776. // CRASH
  777. //
  778. // ------------------------------
  779. //
  780. // Expected in freezer:
  781. // G->C1->C2->C3->C4->C5->C6->C7->C8
  782. //
  783. // Expected in leveldb:
  784. // C8)->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24
  785. //
  786. // Expected head header : C24
  787. // Expected head fast block: C24
  788. // Expected head block : G
  789. testRepair(t, &rewindTest{
  790. canonicalBlocks: 24,
  791. sidechainBlocks: 0,
  792. freezeThreshold: 16,
  793. commitBlock: 0,
  794. pivotBlock: uint64ptr(4),
  795. expCanonicalBlocks: 24,
  796. expSidechainBlocks: 0,
  797. expFrozen: 9,
  798. expHeadHeader: 24,
  799. expHeadFastBlock: 24,
  800. expHeadBlock: 0,
  801. }, snapshots)
  802. }
  803. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  804. // side chain, where a recent block - newer than the ancient limit - was already
  805. // committed to disk and then the process crashed. In this test scenario the side
  806. // chain is below the committed block. In this case we expect the chain to be
  807. // rolled back to the committed block, with everything afterwads kept as fast
  808. // sync data; the side chain completely nuked by the freezer.
  809. func TestLongOldForkedShallowRepair(t *testing.T) {
  810. testLongOldForkedShallowRepair(t, false)
  811. }
  812. func TestLongOldForkedShallowRepairWithSnapshots(t *testing.T) {
  813. testLongOldForkedShallowRepair(t, true)
  814. }
  815. func testLongOldForkedShallowRepair(t *testing.T, snapshots bool) {
  816. // Chain:
  817. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  818. // └->S1->S2->S3
  819. //
  820. // Frozen:
  821. // G->C1->C2
  822. //
  823. // Commit: G, C4
  824. // Pivot : none
  825. //
  826. // CRASH
  827. //
  828. // ------------------------------
  829. //
  830. // Expected in freezer:
  831. // G->C1->C2
  832. //
  833. // Expected in leveldb:
  834. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  835. //
  836. // Expected head header : C18
  837. // Expected head fast block: C18
  838. // Expected head block : C4
  839. testRepair(t, &rewindTest{
  840. canonicalBlocks: 18,
  841. sidechainBlocks: 3,
  842. freezeThreshold: 16,
  843. commitBlock: 4,
  844. pivotBlock: nil,
  845. expCanonicalBlocks: 18,
  846. expSidechainBlocks: 0,
  847. expFrozen: 3,
  848. expHeadHeader: 18,
  849. expHeadFastBlock: 18,
  850. expHeadBlock: 4,
  851. }, snapshots)
  852. }
  853. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  854. // side chain, where a recent block - older than the ancient limit - was already
  855. // committed to disk and then the process crashed. In this test scenario the side
  856. // chain is below the committed block. In this case we expect the canonical chain
  857. // to be rolled back to the committed block, with everything afterwads deleted;
  858. // the side chain completely nuked by the freezer.
  859. func TestLongOldForkedDeepRepair(t *testing.T) { testLongOldForkedDeepRepair(t, false) }
  860. func TestLongOldForkedDeepRepairWithSnapshots(t *testing.T) { testLongOldForkedDeepRepair(t, true) }
  861. func testLongOldForkedDeepRepair(t *testing.T, snapshots bool) {
  862. // Chain:
  863. // 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)
  864. // └->S1->S2->S3
  865. //
  866. // Frozen:
  867. // G->C1->C2->C3->C4->C5->C6->C7->C8
  868. //
  869. // Commit: G, C4
  870. // Pivot : none
  871. //
  872. // CRASH
  873. //
  874. // ------------------------------
  875. //
  876. // Expected in freezer:
  877. // G->C1->C2->C3->C4
  878. //
  879. // Expected in leveldb: none
  880. //
  881. // Expected head header : C4
  882. // Expected head fast block: C4
  883. // Expected head block : C4
  884. testRepair(t, &rewindTest{
  885. canonicalBlocks: 24,
  886. sidechainBlocks: 3,
  887. freezeThreshold: 16,
  888. commitBlock: 4,
  889. pivotBlock: nil,
  890. expCanonicalBlocks: 4,
  891. expSidechainBlocks: 0,
  892. expFrozen: 5,
  893. expHeadHeader: 4,
  894. expHeadFastBlock: 4,
  895. expHeadBlock: 4,
  896. }, snapshots)
  897. }
  898. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  899. // side chain, where the fast sync pivot point - newer than the ancient limit -
  900. // was already committed to disk and then the process crashed. In this test scenario
  901. // the side chain is below the committed block. In this case we expect the chain
  902. // to be rolled back to the committed block, with everything afterwads kept as
  903. // fast sync data; the side chain completely nuked by the freezer.
  904. func TestLongOldForkedFastSyncedShallowRepair(t *testing.T) {
  905. testLongOldForkedFastSyncedShallowRepair(t, false)
  906. }
  907. func TestLongOldForkedFastSyncedShallowRepairWithSnapshots(t *testing.T) {
  908. testLongOldForkedFastSyncedShallowRepair(t, true)
  909. }
  910. func testLongOldForkedFastSyncedShallowRepair(t *testing.T, snapshots bool) {
  911. // Chain:
  912. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  913. // └->S1->S2->S3
  914. //
  915. // Frozen:
  916. // G->C1->C2
  917. //
  918. // Commit: G, C4
  919. // Pivot : C4
  920. //
  921. // CRASH
  922. //
  923. // ------------------------------
  924. //
  925. // Expected in freezer:
  926. // G->C1->C2
  927. //
  928. // Expected in leveldb:
  929. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  930. //
  931. // Expected head header : C18
  932. // Expected head fast block: C18
  933. // Expected head block : C4
  934. testRepair(t, &rewindTest{
  935. canonicalBlocks: 18,
  936. sidechainBlocks: 3,
  937. freezeThreshold: 16,
  938. commitBlock: 4,
  939. pivotBlock: uint64ptr(4),
  940. expCanonicalBlocks: 18,
  941. expSidechainBlocks: 0,
  942. expFrozen: 3,
  943. expHeadHeader: 18,
  944. expHeadFastBlock: 18,
  945. expHeadBlock: 4,
  946. }, snapshots)
  947. }
  948. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  949. // side chain, where the fast sync pivot point - older than the ancient limit -
  950. // was already committed to disk and then the process crashed. In this test scenario
  951. // the side chain is below the committed block. In this case we expect the canonical
  952. // chain to be rolled back to the committed block, with everything afterwads deleted;
  953. // the side chain completely nuked by the freezer.
  954. func TestLongOldForkedFastSyncedDeepRepair(t *testing.T) {
  955. testLongOldForkedFastSyncedDeepRepair(t, false)
  956. }
  957. func TestLongOldForkedFastSyncedDeepRepairWithSnapshots(t *testing.T) {
  958. testLongOldForkedFastSyncedDeepRepair(t, true)
  959. }
  960. func testLongOldForkedFastSyncedDeepRepair(t *testing.T, snapshots bool) {
  961. // Chain:
  962. // 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)
  963. // └->S1->S2->S3
  964. //
  965. // Frozen:
  966. // G->C1->C2->C3->C4->C5->C6->C7->C8
  967. //
  968. // Commit: G, C4
  969. // Pivot : C4
  970. //
  971. // CRASH
  972. //
  973. // ------------------------------
  974. //
  975. // Expected in freezer:
  976. // G->C1->C2->C3->C4
  977. //
  978. // Expected in leveldb: none
  979. //
  980. // Expected head header : C4
  981. // Expected head fast block: C4
  982. // Expected head block : C4
  983. testRepair(t, &rewindTest{
  984. canonicalBlocks: 24,
  985. sidechainBlocks: 3,
  986. freezeThreshold: 16,
  987. commitBlock: 4,
  988. pivotBlock: uint64ptr(4),
  989. expCanonicalBlocks: 4,
  990. expSidechainBlocks: 0,
  991. expFrozen: 5,
  992. expHeadHeader: 4,
  993. expHeadFastBlock: 4,
  994. expHeadBlock: 4,
  995. }, snapshots)
  996. }
  997. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  998. // side chain, where the fast sync pivot point - older than the ancient limit -
  999. // was not yet committed, but the process crashed. In this test scenario the side
  1000. // chain is below the committed block. In this case we expect the chain to detect
  1001. // that it was fast syncing and not delete anything. The side chain is completely
  1002. // nuked by the freezer.
  1003. func TestLongOldForkedFastSyncingShallowRepair(t *testing.T) {
  1004. testLongOldForkedFastSyncingShallowRepair(t, false)
  1005. }
  1006. func TestLongOldForkedFastSyncingShallowRepairWithSnapshots(t *testing.T) {
  1007. testLongOldForkedFastSyncingShallowRepair(t, true)
  1008. }
  1009. func testLongOldForkedFastSyncingShallowRepair(t *testing.T, snapshots bool) {
  1010. // Chain:
  1011. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1012. // └->S1->S2->S3
  1013. //
  1014. // Frozen:
  1015. // G->C1->C2
  1016. //
  1017. // Commit: G
  1018. // Pivot : C4
  1019. //
  1020. // CRASH
  1021. //
  1022. // ------------------------------
  1023. //
  1024. // Expected in freezer:
  1025. // G->C1->C2
  1026. //
  1027. // Expected in leveldb:
  1028. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1029. //
  1030. // Expected head header : C18
  1031. // Expected head fast block: C18
  1032. // Expected head block : G
  1033. testRepair(t, &rewindTest{
  1034. canonicalBlocks: 18,
  1035. sidechainBlocks: 3,
  1036. freezeThreshold: 16,
  1037. commitBlock: 0,
  1038. pivotBlock: uint64ptr(4),
  1039. expCanonicalBlocks: 18,
  1040. expSidechainBlocks: 0,
  1041. expFrozen: 3,
  1042. expHeadHeader: 18,
  1043. expHeadFastBlock: 18,
  1044. expHeadBlock: 0,
  1045. }, snapshots)
  1046. }
  1047. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1048. // side chain, where the fast sync pivot point - older than the ancient limit -
  1049. // was not yet committed, but the process crashed. In this test scenario the side
  1050. // chain is below the committed block. In this case we expect the chain to detect
  1051. // that it was fast syncing and not delete anything. The side chain is completely
  1052. // nuked by the freezer.
  1053. func TestLongOldForkedFastSyncingDeepRepair(t *testing.T) {
  1054. testLongOldForkedFastSyncingDeepRepair(t, false)
  1055. }
  1056. func TestLongOldForkedFastSyncingDeepRepairWithSnapshots(t *testing.T) {
  1057. testLongOldForkedFastSyncingDeepRepair(t, true)
  1058. }
  1059. func testLongOldForkedFastSyncingDeepRepair(t *testing.T, snapshots bool) {
  1060. // Chain:
  1061. // 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)
  1062. // └->S1->S2->S3
  1063. //
  1064. // Frozen:
  1065. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1066. //
  1067. // Commit: G
  1068. // Pivot : C4
  1069. //
  1070. // CRASH
  1071. //
  1072. // ------------------------------
  1073. //
  1074. // Expected in freezer:
  1075. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1076. //
  1077. // Expected in leveldb:
  1078. // C8)->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24
  1079. //
  1080. // Expected head header : C24
  1081. // Expected head fast block: C24
  1082. // Expected head block : G
  1083. testRepair(t, &rewindTest{
  1084. canonicalBlocks: 24,
  1085. sidechainBlocks: 3,
  1086. freezeThreshold: 16,
  1087. commitBlock: 0,
  1088. pivotBlock: uint64ptr(4),
  1089. expCanonicalBlocks: 24,
  1090. expSidechainBlocks: 0,
  1091. expFrozen: 9,
  1092. expHeadHeader: 24,
  1093. expHeadFastBlock: 24,
  1094. expHeadBlock: 0,
  1095. }, snapshots)
  1096. }
  1097. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1098. // side chain, where a recent block - newer than the ancient limit - was already
  1099. // committed to disk and then the process crashed. In this test scenario the side
  1100. // chain is above the committed block. In this case we expect the chain to be
  1101. // rolled back to the committed block, with everything afterwads kept as fast
  1102. // sync data; the side chain completely nuked by the freezer.
  1103. func TestLongNewerForkedShallowRepair(t *testing.T) {
  1104. testLongNewerForkedShallowRepair(t, false)
  1105. }
  1106. func TestLongNewerForkedShallowRepairWithSnapshots(t *testing.T) {
  1107. testLongNewerForkedShallowRepair(t, true)
  1108. }
  1109. func testLongNewerForkedShallowRepair(t *testing.T, snapshots bool) {
  1110. // Chain:
  1111. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1112. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1113. //
  1114. // Frozen:
  1115. // G->C1->C2
  1116. //
  1117. // Commit: G, C4
  1118. // Pivot : none
  1119. //
  1120. // CRASH
  1121. //
  1122. // ------------------------------
  1123. //
  1124. // Expected in freezer:
  1125. // G->C1->C2
  1126. //
  1127. // Expected in leveldb:
  1128. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1129. //
  1130. // Expected head header : C18
  1131. // Expected head fast block: C18
  1132. // Expected head block : C4
  1133. testRepair(t, &rewindTest{
  1134. canonicalBlocks: 18,
  1135. sidechainBlocks: 12,
  1136. freezeThreshold: 16,
  1137. commitBlock: 4,
  1138. pivotBlock: nil,
  1139. expCanonicalBlocks: 18,
  1140. expSidechainBlocks: 0,
  1141. expFrozen: 3,
  1142. expHeadHeader: 18,
  1143. expHeadFastBlock: 18,
  1144. expHeadBlock: 4,
  1145. }, snapshots)
  1146. }
  1147. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1148. // side chain, where a recent block - older than the ancient limit - was already
  1149. // committed to disk and then the process crashed. In this test scenario the side
  1150. // chain is above the committed block. In this case we expect the canonical chain
  1151. // to be rolled back to the committed block, with everything afterwads deleted;
  1152. // the side chain completely nuked by the freezer.
  1153. func TestLongNewerForkedDeepRepair(t *testing.T) { testLongNewerForkedDeepRepair(t, false) }
  1154. func TestLongNewerForkedDeepRepairWithSnapshots(t *testing.T) { testLongNewerForkedDeepRepair(t, true) }
  1155. func testLongNewerForkedDeepRepair(t *testing.T, snapshots bool) {
  1156. // Chain:
  1157. // 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)
  1158. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1159. //
  1160. // Frozen:
  1161. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1162. //
  1163. // Commit: G, C4
  1164. // Pivot : none
  1165. //
  1166. // CRASH
  1167. //
  1168. // ------------------------------
  1169. //
  1170. // Expected in freezer:
  1171. // G->C1->C2->C3->C4
  1172. //
  1173. // Expected in leveldb: none
  1174. //
  1175. // Expected head header : C4
  1176. // Expected head fast block: C4
  1177. // Expected head block : C4
  1178. testRepair(t, &rewindTest{
  1179. canonicalBlocks: 24,
  1180. sidechainBlocks: 12,
  1181. freezeThreshold: 16,
  1182. commitBlock: 4,
  1183. pivotBlock: nil,
  1184. expCanonicalBlocks: 4,
  1185. expSidechainBlocks: 0,
  1186. expFrozen: 5,
  1187. expHeadHeader: 4,
  1188. expHeadFastBlock: 4,
  1189. expHeadBlock: 4,
  1190. }, snapshots)
  1191. }
  1192. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1193. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1194. // was already committed to disk and then the process crashed. In this test scenario
  1195. // the side chain is above the committed block. In this case we expect the chain
  1196. // to be rolled back to the committed block, with everything afterwads kept as fast
  1197. // sync data; the side chain completely nuked by the freezer.
  1198. func TestLongNewerForkedFastSyncedShallowRepair(t *testing.T) {
  1199. testLongNewerForkedFastSyncedShallowRepair(t, false)
  1200. }
  1201. func TestLongNewerForkedFastSyncedShallowRepairWithSnapshots(t *testing.T) {
  1202. testLongNewerForkedFastSyncedShallowRepair(t, true)
  1203. }
  1204. func testLongNewerForkedFastSyncedShallowRepair(t *testing.T, snapshots bool) {
  1205. // Chain:
  1206. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1207. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1208. //
  1209. // Frozen:
  1210. // G->C1->C2
  1211. //
  1212. // Commit: G, C4
  1213. // Pivot : C4
  1214. //
  1215. // CRASH
  1216. //
  1217. // ------------------------------
  1218. //
  1219. // Expected in freezer:
  1220. // G->C1->C2
  1221. //
  1222. // Expected in leveldb:
  1223. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1224. //
  1225. // Expected head header : C18
  1226. // Expected head fast block: C18
  1227. // Expected head block : C4
  1228. testRepair(t, &rewindTest{
  1229. canonicalBlocks: 18,
  1230. sidechainBlocks: 12,
  1231. freezeThreshold: 16,
  1232. commitBlock: 4,
  1233. pivotBlock: uint64ptr(4),
  1234. expCanonicalBlocks: 18,
  1235. expSidechainBlocks: 0,
  1236. expFrozen: 3,
  1237. expHeadHeader: 18,
  1238. expHeadFastBlock: 18,
  1239. expHeadBlock: 4,
  1240. }, snapshots)
  1241. }
  1242. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1243. // side chain, where the fast sync pivot point - older than the ancient limit -
  1244. // was already committed to disk and then the process crashed. In this test scenario
  1245. // the side chain is above the committed block. In this case we expect the canonical
  1246. // chain to be rolled back to the committed block, with everything afterwads deleted;
  1247. // the side chain completely nuked by the freezer.
  1248. func TestLongNewerForkedFastSyncedDeepRepair(t *testing.T) {
  1249. testLongNewerForkedFastSyncedDeepRepair(t, false)
  1250. }
  1251. func TestLongNewerForkedFastSyncedDeepRepairWithSnapshots(t *testing.T) {
  1252. testLongNewerForkedFastSyncedDeepRepair(t, true)
  1253. }
  1254. func testLongNewerForkedFastSyncedDeepRepair(t *testing.T, snapshots bool) {
  1255. // Chain:
  1256. // 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)
  1257. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1258. //
  1259. // Frozen:
  1260. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1261. //
  1262. // Commit: G, C4
  1263. // Pivot : C4
  1264. //
  1265. // CRASH
  1266. //
  1267. // ------------------------------
  1268. //
  1269. // Expected in freezer:
  1270. // G->C1->C2->C3->C4
  1271. //
  1272. // Expected in leveldb: none
  1273. //
  1274. // Expected head header : C4
  1275. // Expected head fast block: C4
  1276. // Expected head block : C4
  1277. testRepair(t, &rewindTest{
  1278. canonicalBlocks: 24,
  1279. sidechainBlocks: 12,
  1280. freezeThreshold: 16,
  1281. commitBlock: 4,
  1282. pivotBlock: uint64ptr(4),
  1283. expCanonicalBlocks: 4,
  1284. expSidechainBlocks: 0,
  1285. expFrozen: 5,
  1286. expHeadHeader: 4,
  1287. expHeadFastBlock: 4,
  1288. expHeadBlock: 4,
  1289. }, snapshots)
  1290. }
  1291. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1292. // side chain, where the fast sync pivot point - older than the ancient limit -
  1293. // was not yet committed, but the process crashed. In this test scenario the side
  1294. // chain is above the committed block. In this case we expect the chain to detect
  1295. // that it was fast syncing and not delete anything. The side chain is completely
  1296. // nuked by the freezer.
  1297. func TestLongNewerForkedFastSyncingShallowRepair(t *testing.T) {
  1298. testLongNewerForkedFastSyncingShallowRepair(t, false)
  1299. }
  1300. func TestLongNewerForkedFastSyncingShallowRepairWithSnapshots(t *testing.T) {
  1301. testLongNewerForkedFastSyncingShallowRepair(t, true)
  1302. }
  1303. func testLongNewerForkedFastSyncingShallowRepair(t *testing.T, snapshots bool) {
  1304. // Chain:
  1305. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1306. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1307. //
  1308. // Frozen:
  1309. // G->C1->C2
  1310. //
  1311. // Commit: G
  1312. // Pivot : C4
  1313. //
  1314. // CRASH
  1315. //
  1316. // ------------------------------
  1317. //
  1318. // Expected in freezer:
  1319. // G->C1->C2
  1320. //
  1321. // Expected in leveldb:
  1322. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1323. //
  1324. // Expected head header : C18
  1325. // Expected head fast block: C18
  1326. // Expected head block : G
  1327. testRepair(t, &rewindTest{
  1328. canonicalBlocks: 18,
  1329. sidechainBlocks: 12,
  1330. freezeThreshold: 16,
  1331. commitBlock: 0,
  1332. pivotBlock: uint64ptr(4),
  1333. expCanonicalBlocks: 18,
  1334. expSidechainBlocks: 0,
  1335. expFrozen: 3,
  1336. expHeadHeader: 18,
  1337. expHeadFastBlock: 18,
  1338. expHeadBlock: 0,
  1339. }, snapshots)
  1340. }
  1341. // Tests a recovery for a long canonical chain with frozen blocks and a shorter
  1342. // side chain, where the fast sync pivot point - older than the ancient limit -
  1343. // was not yet committed, but the process crashed. In this test scenario the side
  1344. // chain is above the committed block. In this case we expect the chain to detect
  1345. // that it was fast syncing and not delete anything. The side chain is completely
  1346. // nuked by the freezer.
  1347. func TestLongNewerForkedFastSyncingDeepRepair(t *testing.T) {
  1348. testLongNewerForkedFastSyncingDeepRepair(t, false)
  1349. }
  1350. func TestLongNewerForkedFastSyncingDeepRepairWithSnapshots(t *testing.T) {
  1351. testLongNewerForkedFastSyncingDeepRepair(t, true)
  1352. }
  1353. func testLongNewerForkedFastSyncingDeepRepair(t *testing.T, snapshots bool) {
  1354. // Chain:
  1355. // 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)
  1356. // └->S1->S2->S3->S4->S5->S6->S7->S8->S9->S10->S11->S12
  1357. //
  1358. // Frozen:
  1359. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1360. //
  1361. // Commit: G
  1362. // Pivot : C4
  1363. //
  1364. // CRASH
  1365. //
  1366. // ------------------------------
  1367. //
  1368. // Expected in freezer:
  1369. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1370. //
  1371. // Expected in leveldb:
  1372. // C8)->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24
  1373. //
  1374. // Expected head header : C24
  1375. // Expected head fast block: C24
  1376. // Expected head block : G
  1377. testRepair(t, &rewindTest{
  1378. canonicalBlocks: 24,
  1379. sidechainBlocks: 12,
  1380. freezeThreshold: 16,
  1381. commitBlock: 0,
  1382. pivotBlock: uint64ptr(4),
  1383. expCanonicalBlocks: 24,
  1384. expSidechainBlocks: 0,
  1385. expFrozen: 9,
  1386. expHeadHeader: 24,
  1387. expHeadFastBlock: 24,
  1388. expHeadBlock: 0,
  1389. }, snapshots)
  1390. }
  1391. // Tests a recovery for a long canonical chain with frozen blocks and a longer side
  1392. // chain, where a recent block - newer than the ancient limit - was already committed
  1393. // to disk and then the process crashed. In this case we expect the chain to be
  1394. // rolled back to the committed block, with everything afterwads kept as fast sync
  1395. // data. The side chain completely nuked by the freezer.
  1396. func TestLongReorgedShallowRepair(t *testing.T) { testLongReorgedShallowRepair(t, false) }
  1397. func TestLongReorgedShallowRepairWithSnapshots(t *testing.T) { testLongReorgedShallowRepair(t, true) }
  1398. func testLongReorgedShallowRepair(t *testing.T, snapshots bool) {
  1399. // Chain:
  1400. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1401. // └->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
  1402. //
  1403. // Frozen:
  1404. // G->C1->C2
  1405. //
  1406. // Commit: G, C4
  1407. // Pivot : none
  1408. //
  1409. // CRASH
  1410. //
  1411. // ------------------------------
  1412. //
  1413. // Expected in freezer:
  1414. // G->C1->C2
  1415. //
  1416. // Expected in leveldb:
  1417. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1418. //
  1419. // Expected head header : C18
  1420. // Expected head fast block: C18
  1421. // Expected head block : C4
  1422. testRepair(t, &rewindTest{
  1423. canonicalBlocks: 18,
  1424. sidechainBlocks: 26,
  1425. freezeThreshold: 16,
  1426. commitBlock: 4,
  1427. pivotBlock: nil,
  1428. expCanonicalBlocks: 18,
  1429. expSidechainBlocks: 0,
  1430. expFrozen: 3,
  1431. expHeadHeader: 18,
  1432. expHeadFastBlock: 18,
  1433. expHeadBlock: 4,
  1434. }, snapshots)
  1435. }
  1436. // Tests a recovery for a long canonical chain with frozen blocks and a longer side
  1437. // chain, where a recent block - older than the ancient limit - was already committed
  1438. // to disk and then the process crashed. In this case we expect the canonical chains
  1439. // to be rolled back to the committed block, with everything afterwads deleted. The
  1440. // side chain completely nuked by the freezer.
  1441. func TestLongReorgedDeepRepair(t *testing.T) { testLongReorgedDeepRepair(t, false) }
  1442. func TestLongReorgedDeepRepairWithSnapshots(t *testing.T) { testLongReorgedDeepRepair(t, true) }
  1443. func testLongReorgedDeepRepair(t *testing.T, snapshots bool) {
  1444. // Chain:
  1445. // 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)
  1446. // └->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
  1447. //
  1448. // Frozen:
  1449. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1450. //
  1451. // Commit: G, C4
  1452. // Pivot : none
  1453. //
  1454. // CRASH
  1455. //
  1456. // ------------------------------
  1457. //
  1458. // Expected in freezer:
  1459. // G->C1->C2->C3->C4
  1460. //
  1461. // Expected in leveldb: none
  1462. //
  1463. // Expected head header : C4
  1464. // Expected head fast block: C4
  1465. // Expected head block : C4
  1466. testRepair(t, &rewindTest{
  1467. canonicalBlocks: 24,
  1468. sidechainBlocks: 26,
  1469. freezeThreshold: 16,
  1470. commitBlock: 4,
  1471. pivotBlock: nil,
  1472. expCanonicalBlocks: 4,
  1473. expSidechainBlocks: 0,
  1474. expFrozen: 5,
  1475. expHeadHeader: 4,
  1476. expHeadFastBlock: 4,
  1477. expHeadBlock: 4,
  1478. }, snapshots)
  1479. }
  1480. // Tests a recovery for a long canonical chain with frozen blocks and a longer
  1481. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1482. // was already committed to disk and then the process crashed. In this case we
  1483. // expect the chain to be rolled back to the committed block, with everything
  1484. // afterwads kept as fast sync data. The side chain completely nuked by the
  1485. // freezer.
  1486. func TestLongReorgedFastSyncedShallowRepair(t *testing.T) {
  1487. testLongReorgedFastSyncedShallowRepair(t, false)
  1488. }
  1489. func TestLongReorgedFastSyncedShallowRepairWithSnapshots(t *testing.T) {
  1490. testLongReorgedFastSyncedShallowRepair(t, true)
  1491. }
  1492. func testLongReorgedFastSyncedShallowRepair(t *testing.T, snapshots bool) {
  1493. // Chain:
  1494. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1495. // └->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
  1496. //
  1497. // Frozen:
  1498. // G->C1->C2
  1499. //
  1500. // Commit: G, C4
  1501. // Pivot : C4
  1502. //
  1503. // CRASH
  1504. //
  1505. // ------------------------------
  1506. //
  1507. // Expected in freezer:
  1508. // G->C1->C2
  1509. //
  1510. // Expected in leveldb:
  1511. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1512. //
  1513. // Expected head header : C18
  1514. // Expected head fast block: C18
  1515. // Expected head block : C4
  1516. testRepair(t, &rewindTest{
  1517. canonicalBlocks: 18,
  1518. sidechainBlocks: 26,
  1519. freezeThreshold: 16,
  1520. commitBlock: 4,
  1521. pivotBlock: uint64ptr(4),
  1522. expCanonicalBlocks: 18,
  1523. expSidechainBlocks: 0,
  1524. expFrozen: 3,
  1525. expHeadHeader: 18,
  1526. expHeadFastBlock: 18,
  1527. expHeadBlock: 4,
  1528. }, snapshots)
  1529. }
  1530. // Tests a recovery for a long canonical chain with frozen blocks and a longer
  1531. // side chain, where the fast sync pivot point - older than the ancient limit -
  1532. // was already committed to disk and then the process crashed. In this case we
  1533. // expect the canonical chains to be rolled back to the committed block, with
  1534. // everything afterwads deleted. The side chain completely nuked by the freezer.
  1535. func TestLongReorgedFastSyncedDeepRepair(t *testing.T) {
  1536. testLongReorgedFastSyncedDeepRepair(t, false)
  1537. }
  1538. func TestLongReorgedFastSyncedDeepRepairWithSnapshots(t *testing.T) {
  1539. testLongReorgedFastSyncedDeepRepair(t, true)
  1540. }
  1541. func testLongReorgedFastSyncedDeepRepair(t *testing.T, snapshots bool) {
  1542. // Chain:
  1543. // 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)
  1544. // └->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
  1545. //
  1546. // Frozen:
  1547. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1548. //
  1549. // Commit: G, C4
  1550. // Pivot : C4
  1551. //
  1552. // CRASH
  1553. //
  1554. // ------------------------------
  1555. //
  1556. // Expected in freezer:
  1557. // G->C1->C2->C3->C4
  1558. //
  1559. // Expected in leveldb: none
  1560. //
  1561. // Expected head header : C4
  1562. // Expected head fast block: C4
  1563. // Expected head block : C4
  1564. testRepair(t, &rewindTest{
  1565. canonicalBlocks: 24,
  1566. sidechainBlocks: 26,
  1567. freezeThreshold: 16,
  1568. commitBlock: 4,
  1569. pivotBlock: uint64ptr(4),
  1570. expCanonicalBlocks: 4,
  1571. expSidechainBlocks: 0,
  1572. expFrozen: 5,
  1573. expHeadHeader: 4,
  1574. expHeadFastBlock: 4,
  1575. expHeadBlock: 4,
  1576. }, snapshots)
  1577. }
  1578. // Tests a recovery for a long canonical chain with frozen blocks and a longer
  1579. // side chain, where the fast sync pivot point - newer than the ancient limit -
  1580. // was not yet committed, but the process crashed. In this case we expect the
  1581. // chain to detect that it was fast syncing and not delete anything, since we
  1582. // can just pick up directly where we left off.
  1583. func TestLongReorgedFastSyncingShallowRepair(t *testing.T) {
  1584. testLongReorgedFastSyncingShallowRepair(t, false)
  1585. }
  1586. func TestLongReorgedFastSyncingShallowRepairWithSnapshots(t *testing.T) {
  1587. testLongReorgedFastSyncingShallowRepair(t, true)
  1588. }
  1589. func testLongReorgedFastSyncingShallowRepair(t *testing.T, snapshots bool) {
  1590. // Chain:
  1591. // G->C1->C2->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18 (HEAD)
  1592. // └->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
  1593. //
  1594. // Frozen:
  1595. // G->C1->C2
  1596. //
  1597. // Commit: G
  1598. // Pivot : C4
  1599. //
  1600. // CRASH
  1601. //
  1602. // ------------------------------
  1603. //
  1604. // Expected in freezer:
  1605. // G->C1->C2
  1606. //
  1607. // Expected in leveldb:
  1608. // C2)->C3->C4->C5->C6->C7->C8->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18
  1609. //
  1610. // Expected head header : C18
  1611. // Expected head fast block: C18
  1612. // Expected head block : G
  1613. testRepair(t, &rewindTest{
  1614. canonicalBlocks: 18,
  1615. sidechainBlocks: 26,
  1616. freezeThreshold: 16,
  1617. commitBlock: 0,
  1618. pivotBlock: uint64ptr(4),
  1619. expCanonicalBlocks: 18,
  1620. expSidechainBlocks: 0,
  1621. expFrozen: 3,
  1622. expHeadHeader: 18,
  1623. expHeadFastBlock: 18,
  1624. expHeadBlock: 0,
  1625. }, snapshots)
  1626. }
  1627. // Tests a recovery for a long canonical chain with frozen blocks and a longer
  1628. // side chain, where the fast sync pivot point - older than the ancient limit -
  1629. // was not yet committed, but the process crashed. In this case we expect the
  1630. // chain to detect that it was fast syncing and not delete anything, since we
  1631. // can just pick up directly where we left off.
  1632. func TestLongReorgedFastSyncingDeepRepair(t *testing.T) {
  1633. testLongReorgedFastSyncingDeepRepair(t, false)
  1634. }
  1635. func TestLongReorgedFastSyncingDeepRepairWithSnapshots(t *testing.T) {
  1636. testLongReorgedFastSyncingDeepRepair(t, true)
  1637. }
  1638. func testLongReorgedFastSyncingDeepRepair(t *testing.T, snapshots bool) {
  1639. // Chain:
  1640. // 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)
  1641. // └->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
  1642. //
  1643. // Frozen:
  1644. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1645. //
  1646. // Commit: G
  1647. // Pivot : C4
  1648. //
  1649. // CRASH
  1650. //
  1651. // ------------------------------
  1652. //
  1653. // Expected in freezer:
  1654. // G->C1->C2->C3->C4->C5->C6->C7->C8
  1655. //
  1656. // Expected in leveldb:
  1657. // C8)->C9->C10->C11->C12->C13->C14->C15->C16->C17->C18->C19->C20->C21->C22->C23->C24
  1658. //
  1659. // Expected head header : C24
  1660. // Expected head fast block: C24
  1661. // Expected head block : G
  1662. testRepair(t, &rewindTest{
  1663. canonicalBlocks: 24,
  1664. sidechainBlocks: 26,
  1665. freezeThreshold: 16,
  1666. commitBlock: 0,
  1667. pivotBlock: uint64ptr(4),
  1668. expCanonicalBlocks: 24,
  1669. expSidechainBlocks: 0,
  1670. expFrozen: 9,
  1671. expHeadHeader: 24,
  1672. expHeadFastBlock: 24,
  1673. expHeadBlock: 0,
  1674. }, snapshots)
  1675. }
  1676. func testRepair(t *testing.T, tt *rewindTest, snapshots bool) {
  1677. // It's hard to follow the test case, visualize the input
  1678. //log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
  1679. // fmt.Println(tt.dump(true))
  1680. // Create a temporary persistent database
  1681. datadir, err := ioutil.TempDir("", "")
  1682. if err != nil {
  1683. t.Fatalf("Failed to create temporary datadir: %v", err)
  1684. }
  1685. os.RemoveAll(datadir)
  1686. db, err := rawdb.NewLevelDBDatabaseWithFreezer(datadir, 0, 0, datadir, "", false)
  1687. if err != nil {
  1688. t.Fatalf("Failed to create persistent database: %v", err)
  1689. }
  1690. defer db.Close() // Might double close, should be fine
  1691. // Initialize a fresh chain
  1692. var (
  1693. genesis = new(Genesis).MustCommit(db)
  1694. engine = ethash.NewFullFaker()
  1695. config = &CacheConfig{
  1696. TrieCleanLimit: 256,
  1697. TrieDirtyLimit: 256,
  1698. TrieTimeLimit: 5 * time.Minute,
  1699. SnapshotLimit: 0, // Disable snapshot by default
  1700. }
  1701. )
  1702. if snapshots {
  1703. config.SnapshotLimit = 256
  1704. config.SnapshotWait = true
  1705. }
  1706. chain, err := NewBlockChain(db, config, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil, nil)
  1707. if err != nil {
  1708. t.Fatalf("Failed to create chain: %v", err)
  1709. }
  1710. // If sidechain blocks are needed, make a light chain and import it
  1711. var sideblocks types.Blocks
  1712. if tt.sidechainBlocks > 0 {
  1713. sideblocks, _ = GenerateChain(params.TestChainConfig, genesis, engine, rawdb.NewMemoryDatabase(), tt.sidechainBlocks, func(i int, b *BlockGen) {
  1714. b.SetCoinbase(common.Address{0x01})
  1715. })
  1716. if _, err := chain.InsertChain(sideblocks); err != nil {
  1717. t.Fatalf("Failed to import side chain: %v", err)
  1718. }
  1719. }
  1720. canonblocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, rawdb.NewMemoryDatabase(), tt.canonicalBlocks, func(i int, b *BlockGen) {
  1721. b.SetCoinbase(common.Address{0x02})
  1722. b.SetDifficulty(big.NewInt(1000000))
  1723. })
  1724. if _, err := chain.InsertChain(canonblocks[:tt.commitBlock]); err != nil {
  1725. t.Fatalf("Failed to import canonical chain start: %v", err)
  1726. }
  1727. if tt.commitBlock > 0 {
  1728. chain.stateCache.TrieDB().Commit(canonblocks[tt.commitBlock-1].Root(), true, nil)
  1729. if snapshots {
  1730. if err := chain.snaps.Cap(canonblocks[tt.commitBlock-1].Root(), 0); err != nil {
  1731. t.Fatalf("Failed to flatten snapshots: %v", err)
  1732. }
  1733. }
  1734. }
  1735. if _, err := chain.InsertChain(canonblocks[tt.commitBlock:]); err != nil {
  1736. t.Fatalf("Failed to import canonical chain tail: %v", err)
  1737. }
  1738. // Force run a freeze cycle
  1739. type freezer interface {
  1740. Freeze(threshold uint64) error
  1741. Ancients() (uint64, error)
  1742. }
  1743. db.(freezer).Freeze(tt.freezeThreshold)
  1744. // Set the simulated pivot block
  1745. if tt.pivotBlock != nil {
  1746. rawdb.WriteLastPivotNumber(db, *tt.pivotBlock)
  1747. }
  1748. // Pull the plug on the database, simulating a hard crash
  1749. db.Close()
  1750. // Start a new blockchain back up and see where the repait leads us
  1751. db, err = rawdb.NewLevelDBDatabaseWithFreezer(datadir, 0, 0, datadir, "", false)
  1752. if err != nil {
  1753. t.Fatalf("Failed to reopen persistent database: %v", err)
  1754. }
  1755. defer db.Close()
  1756. chain, err = NewBlockChain(db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}, nil, nil, nil)
  1757. if err != nil {
  1758. t.Fatalf("Failed to recreate chain: %v", err)
  1759. }
  1760. defer chain.Stop()
  1761. // Iterate over all the remaining blocks and ensure there are no gaps
  1762. verifyNoGaps(t, chain, true, canonblocks)
  1763. verifyNoGaps(t, chain, false, sideblocks)
  1764. verifyCutoff(t, chain, true, canonblocks, tt.expCanonicalBlocks)
  1765. verifyCutoff(t, chain, false, sideblocks, tt.expSidechainBlocks)
  1766. if head := chain.CurrentHeader(); head.Number.Uint64() != tt.expHeadHeader {
  1767. t.Errorf("Head header mismatch: have %d, want %d", head.Number, tt.expHeadHeader)
  1768. }
  1769. if head := chain.CurrentFastBlock(); head.NumberU64() != tt.expHeadFastBlock {
  1770. t.Errorf("Head fast block mismatch: have %d, want %d", head.NumberU64(), tt.expHeadFastBlock)
  1771. }
  1772. if head := chain.CurrentBlock(); head.NumberU64() != tt.expHeadBlock {
  1773. t.Errorf("Head block mismatch: have %d, want %d", head.NumberU64(), tt.expHeadBlock)
  1774. }
  1775. if frozen, err := db.(freezer).Ancients(); err != nil {
  1776. t.Errorf("Failed to retrieve ancient count: %v\n", err)
  1777. } else if int(frozen) != tt.expFrozen {
  1778. t.Errorf("Frozen block count mismatch: have %d, want %d", frozen, tt.expFrozen)
  1779. }
  1780. }