Skip to content

Commit e422600

Browse files
committed
storage: save current import file/offset and resume from there (lian#138)
1 parent 520f60c commit e422600

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/bitcoin/storage/storage.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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"

spec/bitcoin/storage/reorg_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,17 @@ def @store.in_sync?; true; end
264264
Bitcoin.network = :bitcoin
265265
# Disable difficulty check
266266
Bitcoin.network[:no_difficulty] = true
267-
@store.import "./spec/bitcoin/fixtures/reorg/blk_0_to_4.dat"
267+
@store.import "./spec/bitcoin/fixtures/reorg/blk_0_to_4.dat", resume_file: "/dev/null"
268268
@store.get_depth.should == 4
269269
@store.get_head.hash.should =~ /000000002f264d65040/
270270
balance("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa").should == 10000000000
271271
balance("1NiEGXeURREqqMjCvjCeZn6SwEBZ9AdVet").should == 0
272272
balance("1KXFNhNtrRMfgbdiQeuJqnfD7dR4PhniyJ").should == 5000000000
273273
balance("1JyMKvPHkrCQd8jQrqTR1rBsAd1VpRhTiE").should == 10000000000
274-
@store.import "./spec/bitcoin/fixtures/reorg/blk_3A.dat"
275-
@store.import "./spec/bitcoin/fixtures/reorg/blk_4A.dat"
274+
@store.import "./spec/bitcoin/fixtures/reorg/blk_3A.dat", resume_file: "/dev/null"
275+
@store.import "./spec/bitcoin/fixtures/reorg/blk_4A.dat", resume_file: "/dev/null"
276276
@store.get_head.hash.should =~ /000000002f264d65040/
277-
@store.import "./spec/bitcoin/fixtures/reorg/blk_5A.dat"
277+
@store.import "./spec/bitcoin/fixtures/reorg/blk_5A.dat", resume_file: "/dev/null"
278278
@store.get_depth.should == 5
279279
@store.get_head.hash.should =~ /00000000195f85184e7/
280280
balance("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa").should == 15000000000

0 commit comments

Comments
 (0)