@@ -406,10 +406,16 @@ def rescan
406406 end
407407
408408 # import satoshi bitcoind blk0001.dat blockchain file
409- def import filename , max_depth = nil
409+ def import filename , opts = { }
410+ opts [ :resume_file ] ||= File . join ( ENV [ "HOME" ] , ".bitcoin-ruby" , Bitcoin . network_name . to_s , "import_resume.state" )
411+ @resume = File . read ( opts [ :resume_file ] ) . split ( "|" ) . map ( &:to_i ) if File . exist? ( opts [ :resume_file ] )
412+
410413 if File . file? ( filename )
411414 log . info { "Importing #{ filename } " }
412415 File . open ( filename ) do |file |
416+ @offset = @resume && @resume [ 1 ] ? @resume [ 1 ] : 0
417+ file . seek ( @offset )
418+
413419 until file . eof?
414420 magic = file . read ( 4 )
415421
@@ -421,13 +427,18 @@ def import filename, max_depth = nil
421427 size = file . read ( 4 ) . unpack ( "L" ) [ 0 ]
422428 blk = Bitcoin ::P ::Block . new ( file . read ( size ) )
423429 depth , chain = new_block ( blk )
424- break if max_depth && depth >= max_depth
430+ break if opts [ :max_depth ] && depth >= opts [ :max_depth ]
431+
432+ File . write ( opts [ :resume_file ] , [ @import_file_num , @offset += ( size + 8 ) ] . join ( "|" ) )
425433 end
426434 end
427435 elsif File . directory? ( filename )
428436 Dir . entries ( filename ) . sort . each do |file |
429- next unless file =~ /^blk.*?\. dat$/
437+ next unless file =~ /^blk(\d +)\. dat$/
438+ @import_file_num = $1. to_i
439+ next if @resume && @resume [ 0 ] && @resume [ 0 ] > @import_file_num
430440 import ( File . join ( filename , file ) , max_depth )
441+ File . write ( opts [ :resume_file ] , [ @import_file_num , 0 ] . join ( "|" ) )
431442 end
432443 else
433444 raise "Import dir/file #{ filename } not found"
0 commit comments