blockchain_repair_test.go 49 KB

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