@@ -7,7 +7,7 @@ const naturalSort = require('node-natural-sort');
77const logger = require ( '../logger' ) ( 'BLOCKCHAIN' ) ;
88const { Block} = require ( './block' ) ;
99const BlockFilePatterns = require ( './block-file-patterns' ) ;
10- const { BLOCKCHAINS_DIR } = require ( '../constants' ) ;
10+ const { BLOCKCHAINS_DIR } = require ( '../common/ constants' ) ;
1111const CHAIN_SUBSECT_LENGTH = 20 ;
1212const ON_MEM_CHAIN_LENGTH = 20 ;
1313
@@ -16,7 +16,6 @@ class Blockchain {
1616 // Finalized chain
1717 this . chain = [ ] ;
1818 this . blockchainDir = blockchainDir ;
19- this . syncedAfterStartup = false ;
2019 }
2120
2221 init ( isFirstNode ) {
@@ -175,13 +174,13 @@ class Blockchain {
175174 return false ;
176175 }
177176 // TODO (lia): Check if the tx nonces are correct.
178- return Blockchain . isValidChainSubsection ( chain ) ;
177+ return Blockchain . isValidChainSegment ( chain ) ;
179178 }
180179
181- static isValidChainSubsection ( chainSubSection ) {
182- for ( let i = 1 ; i < chainSubSection . length ; i ++ ) {
183- const block = chainSubSection [ i ] ;
184- const lastBlock = Block . parse ( chainSubSection [ i - 1 ] ) ;
180+ static isValidChainSegment ( chainSegment ) {
181+ for ( let i = 1 ; i < chainSegment . length ; i ++ ) {
182+ const block = chainSegment [ i ] ;
183+ const lastBlock = Block . parse ( chainSegment [ i - 1 ] ) ;
185184 if ( block . last_hash !== lastBlock . hash || ! Block . validateHashes ( block ) ) {
186185 return false ;
187186 }
@@ -253,43 +252,19 @@ class Blockchain {
253252 return [ this . lastBlock ( ) ] ;
254253 }
255254
256- const chainSubSection = [ ] ;
255+ const chainSegment = [ ] ;
257256 blockFiles . forEach ( ( blockFile ) => {
258- chainSubSection . push ( Block . loadBlock ( blockFile ) ) ;
257+ chainSegment . push ( Block . loadBlock ( blockFile ) ) ;
259258 } ) ;
260- return chainSubSection . length > 0 ? chainSubSection : [ ] ;
259+ return chainSegment . length > 0 ? chainSegment : [ ] ;
261260 }
262261
263- merge ( chainSubSection , db ) {
264- // Call to shift here is important as it removes the first element from the list !!
262+ merge ( chainSegment , db ) {
265263 logger . info ( `Last block number before merge: ${ this . lastBlockNumber ( ) } ` ) ;
266- if ( ! chainSubSection || chainSubSection . length === 0 ) {
267- logger . info ( `Empty chain sub section` ) ;
268- if ( ! this . syncedAfterStartup ) {
269- // Regard this situation as if you're synced.
270- // TODO (lia): ask the tracker server for another peer.
271- this . syncedAfterStartup = true ;
272- }
273- return false ;
274- }
275- if ( chainSubSection [ chainSubSection . length - 1 ] . number < this . lastBlockNumber ( ) ) {
276- logger . info ( `Received chain is of lower block number than current last block number` ) ;
277- return false ;
278- }
279- if ( chainSubSection [ chainSubSection . length - 1 ] . number === this . lastBlockNumber ( ) ) {
280- logger . info ( `Received chain is at the same block number` ) ;
281- if ( ! this . syncedAfterStartup ) {
282- // Regard this situation as if you're synced.
283- // TODO (lia): ask the tracker server for another peer.
284- this . syncedAfterStartup = true ;
285- }
286- return false ;
287- }
288-
289- const firstBlock = Block . parse ( chainSubSection [ 0 ] ) ;
264+ const firstBlock = Block . parse ( chainSegment [ 0 ] ) ;
290265 const lastBlockHash = this . lastBlockNumber ( ) >= 0 ? this . lastBlock ( ) . hash : null ;
291266 const overlap = lastBlockHash ?
292- chainSubSection . filter ( ( block ) => block . number === this . lastBlockNumber ( ) ) : null ;
267+ chainSegment . filter ( ( block ) => block . number === this . lastBlockNumber ( ) ) : null ;
293268 const overlappingBlock = overlap ? overlap [ 0 ] : null ;
294269 if ( lastBlockHash ) {
295270 // Case 1: Not a cold start.
@@ -306,12 +281,12 @@ class Blockchain {
306281 return false ;
307282 }
308283 }
309- if ( ! Blockchain . isValidChainSubsection ( chainSubSection ) ) {
310- logger . error ( `Invalid chain subsection ` ) ;
284+ if ( ! Blockchain . isValidChainSegment ( chainSegment ) ) {
285+ logger . error ( `Invalid chain segment ` ) ;
311286 return false ;
312287 }
313- for ( let i = 0 ; i < chainSubSection . length ; i ++ ) {
314- const block = chainSubSection [ i ] ;
288+ for ( let i = 0 ; i < chainSegment . length ; i ++ ) {
289+ const block = chainSegment [ i ] ;
315290
316291 if ( block . number <= this . lastBlockNumber ( ) ) {
317292 continue ;
0 commit comments