Skip to content

Commit 0e7846f

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

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/bitcoin/storage/storage.rb

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

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)