@@ -406,10 +406,20 @@ 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+ if File . exist? ( opts [ :resume_file ] )
412+ @resume = File . read ( opts [ :resume_file ] ) . split ( "|" ) . map ( &:to_i )
413+ else
414+ FileUtils . mkdir_p ( File . dirname ( opts [ :resume_file ] ) )
415+ end
416+
410417 if File . file? ( filename )
411418 log . info { "Importing #{ filename } " }
412419 File . open ( filename ) do |file |
420+ @offset = @resume && @resume [ 1 ] ? @resume [ 1 ] : 0
421+ file . seek ( @offset )
422+
413423 until file . eof?
414424 magic = file . read ( 4 )
415425
@@ -421,13 +431,18 @@ def import filename, max_depth = nil
421431 size = file . read ( 4 ) . unpack ( "L" ) [ 0 ]
422432 blk = Bitcoin ::P ::Block . new ( file . read ( size ) )
423433 depth , chain = new_block ( blk )
424- break if max_depth && depth >= max_depth
434+ break if opts [ :max_depth ] && depth >= opts [ :max_depth ]
435+
436+ File . write ( opts [ :resume_file ] , [ @import_file_num , @offset += ( size + 8 ) ] . join ( "|" ) )
425437 end
426438 end
427439 elsif File . directory? ( filename )
428440 Dir . entries ( filename ) . sort . each do |file |
429- next unless file =~ /^blk.*?\. dat$/
441+ next unless file =~ /^blk(\d +)\. dat$/
442+ @import_file_num = $1. to_i
443+ next if @resume && @resume [ 0 ] && @resume [ 0 ] > @import_file_num
430444 import ( File . join ( filename , file ) , max_depth )
445+ File . write ( opts [ :resume_file ] , [ @import_file_num , 0 ] . join ( "|" ) )
431446 end
432447 else
433448 raise "Import dir/file #{ filename } not found"
0 commit comments