Commit 0a34508352196f47067fbb1bfb03fa9cc2b5ab69

Authored by comboy
1 parent 2ffaeb44da
Exists in master and in 1 other branch benchmark

fix fake chain generation

genesis block was not being yield, so the blocki #1 was becoming genesis

* moved parsing blocks from disk away from the benchmark
* added check to make sure block was really stored in the main chain

Showing 2 changed files with 11 additions and 11 deletions Side-by-side Diff

spec/bitcoin/helpers/fake_blockchain.rb
... ... @@ -31,7 +31,7 @@
31 31  
32 32 # Initialize fake blockchain and generate +num_blocks+ starting blocks with given
33 33 # +opts+ (see #generate).
34   - def initialize num = 50, opts = {}
  34 + def initialize(num = 50, opts = {})
35 35 Bitcoin.network = :fake
36 36 if File.exist? block_path(0)
37 37 genesis = Bitcoin::P::Block.new File.read block_path 0
... ... @@ -44,8 +44,6 @@
44 44 File.open(block_path(depth),'w') {|f| f.write blk.to_payload }
45 45 depth += 1
46 46 end
47   - # return new instance because this one apparently has half the data cached now
48   - self.class.new
49 47 end
50 48 end
51 49  
... ... @@ -53,7 +51,6 @@
53 51 # Blocks are provided as an argument to the block given to the method
54 52 # fake_chain.generate(5) {|b| save_block(b) }
55 53 def generate(num = 50, opts = {})
56   - Bitcoin.network = :fake
57 54 srand 1337
58 55  
59 56 default_opts = {
60 57  
61 58  
... ... @@ -66,19 +63,20 @@
66 63 opts = default_opts.merge(opts)
67 64  
68 65 to_spend = [] # table of outputs that we can spend
69   - lost_count = 0 # keeping track of lost coins
  66 + lost_count = 0 # keeping track of lost coins
70 67 keys = Array.new(opts[:num_keys]) { Bitcoin::Key.generate }
71 68 timestamp = opts[:genesis_timestamp]
72 69  
73 70 genesis = Bitcoin::Builder.build_block do |blk|
74 71 blk.time timestamp
75   - blk.prev_block "00"*32
  72 + blk.prev_block "00"*32
76 73 blk.tx do |t|
77 74 t.input {|i| i.coinbase }
78 75 t.output {|o| o.value 50*Bitcoin::COIN; o.script {|s| s.recipient keys[0].addr } }
79 76 end
80 77 end
81 78 Bitcoin.network[:genesis_hash] = genesis.hash
  79 + yield(genesis)
82 80  
83 81 to_spend << {tx: genesis.tx[0], tx_idx: 0, key: keys[0], value: 50e8}
84 82  
... ... @@ -167,7 +165,7 @@
167 165 # coinbase
168 166 to_spend << {tx: tx0, tx_idx: 0, key: key0, value: 50e8}
169 167 end
170   - puts "depth #{blk_i}/#{num} \t txcount: #{block.tx.size} \t size: #{block.to_payload.size} \t utxo count: #{to_spend.size + lost_count} (#{to_spend.size}) \t ttg: #{'%.2f' % (Time.now - t0)}s" if opts[:verbose]
  168 + puts "depth #{blk_i+1}/#{num} \t txcount: #{block.tx.size} \t size: #{block.to_payload.size} \t utxo count: #{to_spend.size + lost_count} (#{to_spend.size}) \t ttg: #{'%.2f' % (Time.now - t0)}s" if opts[:verbose]
171 169 yield(block)
172 170 prev_block = block
173 171 end
spec/bitcoin/performance/storage_spec.rb
... ... @@ -20,18 +20,20 @@
20 20 end
21 21  
22 22 it "block storage" do
  23 + blocks = (0..10).to_a.map{|i| @fake_chain.block(i) }
23 24  
24 25 bm = Benchmark.measure do
25 26 bm = Benchmark.bm do |b|
26   - 10.times do |i|
27   - b.report("storing fake block ##{i}") { @store.new_block @fake_chain.block(i) }
  27 + blocks.each.with_index do |blk,i|
  28 + b.report("storing fake block ##{i}") do
  29 + depth, chain = @store.new_block blk
  30 + chain.should == 0
  31 + end
28 32 end
29 33 end
30 34 end
31 35 puts '-'*80
32 36 puts "TOTAL #{bm.format}"
33   -
34   - should.satisfy { "human" }
35 37 end
36 38  
37 39