@@ -16,7 +16,6 @@ class Blockchain {
1616 // Finalized chain
1717 this . chain = [ ] ;
1818 this . blockchainDir = blockchainDir ;
19- this . backupDb = null ;
2019 this . syncedAfterStartup = false ;
2120 }
2221
@@ -101,13 +100,6 @@ class Blockchain {
101100 }
102101 }
103102
104- setBackupDb ( backupDb ) {
105- if ( this . backupDb !== null ) {
106- throw Error ( 'Already set backupdb' ) ;
107- }
108- this . backupDb = backupDb ;
109- }
110-
111103 lastBlock ( ) {
112104 if ( this . chain . length === 0 ) {
113105 return null ;
@@ -139,7 +131,7 @@ class Blockchain {
139131 return lastBlock . timestamp ;
140132 }
141133
142- addNewBlockToChain ( newBlock ) {
134+ addNewBlockToChain ( newBlock , db ) {
143135 if ( ! newBlock ) {
144136 logger . error ( '[blockchain.addNewBlockToChain] Block is null' ) ;
145137 return false ;
@@ -151,15 +143,17 @@ class Blockchain {
151143 if ( ! ( newBlock instanceof Block ) ) {
152144 newBlock = Block . parse ( newBlock ) ;
153145 }
154- if ( ! this . backupDb . executeTransactionList ( newBlock . last_votes ) ) {
155- logger . error ( '[blockchain.addNewBlockToChain] Failed to execute last_votes of block' +
156- `${ JSON . stringify ( newBlock , null , 2 ) } ` ) ;
157- return false ;
158- }
159- if ( ! this . backupDb . executeTransactionList ( newBlock . transactions ) ) {
160- logger . error ( '[blockchain.addNewBlockToChain] Failed to execute transactions of block' +
161- `${ JSON . stringify ( newBlock , null , 2 ) } ` ) ;
162- return false ;
146+ if ( db ) {
147+ if ( ! db . executeTransactionList ( newBlock . last_votes ) ) {
148+ logger . error ( '[blockchain.addNewBlockToChain] Failed to execute last_votes of block' +
149+ `${ JSON . stringify ( newBlock , null , 2 ) } ` ) ;
150+ return false ;
151+ }
152+ if ( ! db . executeTransactionList ( newBlock . transactions ) ) {
153+ logger . error ( '[blockchain.addNewBlockToChain] Failed to execute transactions of block' +
154+ `${ JSON . stringify ( newBlock , null , 2 ) } ` ) ;
155+ return false ;
156+ }
163157 }
164158 this . chain . push ( newBlock ) ;
165159 this . writeChain ( ) ;
@@ -266,7 +260,7 @@ class Blockchain {
266260 return chainSubSection . length > 0 ? chainSubSection : [ ] ;
267261 }
268262
269- merge ( chainSubSection ) {
263+ merge ( chainSubSection , db ) {
270264 // Call to shift here is important as it removes the first element from the list !!
271265 logger . info ( `Last block number before merge: ${ this . lastBlockNumber ( ) } ` ) ;
272266 if ( ! chainSubSection || chainSubSection . length === 0 ) {
@@ -323,7 +317,7 @@ class Blockchain {
323317 continue ;
324318 }
325319 // TODO(lia): validate the state proof of each block
326- if ( ! this . addNewBlockToChain ( block ) ) {
320+ if ( ! this . addNewBlockToChain ( block , db ) ) {
327321 logger . error ( `Failed to add block ` + block ) ;
328322 return false ;
329323 }
0 commit comments