blockchain_sethead_test.go 60 KB

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