blockchain_sethead_test.go 69 KB

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