@@ -11,7 +11,7 @@ import {
11
11
headHeaderKey ,
12
12
headerKey ,
13
13
numberToHashKey ,
14
- tdKey ,
14
+ tdKey
15
15
} from "./util" ;
16
16
17
17
const Block = require ( "ethereumjs-block" ) ;
@@ -229,29 +229,13 @@ export default class Blockchain {
229
229
* Adds a block to the blockchain
230
230
*/
231
231
putBlock ( block : object , cb : any , isGenesis ?: any ) {
232
- const self = this ;
233
-
234
232
// make sure init has completed
235
- self . _initLock . await ( ( ) => {
233
+ this . _initLock . await ( ( ) => {
236
234
// perform put with mutex dance
237
- lockUnlock ( ( done : any ) => {
238
- self . _putBlockOrHeader ( block , done , isGenesis ) ;
235
+ this . _lockUnlock ( ( done : any ) => {
236
+ this . _putBlockOrHeader ( block , done , isGenesis ) ;
239
237
} , cb ) ;
240
238
} ) ;
241
-
242
- // lock, call fn, unlock
243
- function lockUnlock ( fn : any , cb : any ) {
244
- // take lock
245
- self . _putSemaphore . take ( function ( ) {
246
- // call fn
247
- fn ( function ( ) {
248
- // leave lock
249
- self . _putSemaphore . leave ( ) ;
250
- // exit
251
- cb . apply ( null , arguments ) ;
252
- } ) ;
253
- } ) ;
254
- }
255
239
}
256
240
257
241
/**
@@ -271,29 +255,13 @@ export default class Blockchain {
271
255
* Adds a header to the blockchain
272
256
*/
273
257
putHeader ( header : object , cb : any ) {
274
- const self = this ;
275
-
276
258
// make sure init has completed
277
- self . _initLock . await ( ( ) => {
259
+ this . _initLock . await ( ( ) => {
278
260
// perform put with mutex dance
279
- lockUnlock ( ( done : any ) => {
280
- self . _putBlockOrHeader ( header , done ) ;
261
+ this . _lockUnlock ( ( done : any ) => {
262
+ this . _putBlockOrHeader ( header , done ) ;
281
263
} , cb ) ;
282
264
} ) ;
283
-
284
- // lock, call fn, unlock
285
- function lockUnlock ( fn : any , cb : any ) {
286
- // take lock
287
- self . _putSemaphore . take ( function ( ) {
288
- // call fn
289
- fn ( function ( ) {
290
- // leave lock
291
- self . _putSemaphore . leave ( ) ;
292
- // exit
293
- cb . apply ( null , arguments ) ;
294
- } ) ;
295
- } ) ;
296
- }
297
265
}
298
266
299
267
_putBlockOrHeader ( item : any , cb : any , isGenesis ?: any ) {
@@ -728,29 +696,13 @@ export default class Blockchain {
728
696
* and any encountered heads are set to the parent block
729
697
*/
730
698
delBlock ( blockHash : Buffer , cb : any ) {
731
- const self = this ;
732
-
733
699
// make sure init has completed
734
- self . _initLock . await ( ( ) => {
700
+ this . _initLock . await ( ( ) => {
735
701
// perform put with mutex dance
736
- lockUnlock ( ( done : boolean ) => {
737
- self . _delBlock ( blockHash , done ) ;
702
+ this . _lockUnlock ( ( done : boolean ) => {
703
+ this . _delBlock ( blockHash , done ) ;
738
704
} , cb ) ;
739
705
} ) ;
740
-
741
- // lock, call fn, unlock
742
- function lockUnlock ( fn : any , cb : any ) {
743
- // take lock
744
- self . _putSemaphore . take ( function ( ) {
745
- // call fn
746
- fn ( function ( ) {
747
- // leave lock
748
- self . _putSemaphore . leave ( ) ;
749
- // exit
750
- cb . apply ( null , arguments ) ;
751
- } ) ;
752
- } ) ;
753
- }
754
706
}
755
707
756
708
_delBlock ( blockHash : any , cb : any ) {
@@ -1057,4 +1009,16 @@ export default class Blockchain {
1057
1009
}
1058
1010
) ;
1059
1011
}
1012
+
1013
+ _lockUnlock ( fn : any , cb : any ) {
1014
+ const self = this ;
1015
+ this . _putSemaphore . take ( ( ) => {
1016
+ fn ( after ) ;
1017
+
1018
+ function after ( ) {
1019
+ self . _putSemaphore . leave ( ) ;
1020
+ cb . apply ( null , arguments ) ;
1021
+ }
1022
+ } ) ;
1023
+ }
1060
1024
}
0 commit comments