Commit f006d5e109228b90b3140885c4355a5a7e2186bd

Authored by comboy
Committed by Julian Langschaedel
1 parent 3dcafc73a4

let transaction tests also run on dummy store

+ add get_idx_from_tx_hash for dummy store
+ support looking for hash160 among multisig outputs
+ use store_tx instead of store_txout in test
  (perhaps store_txout/txin should be protected methods?)

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

lib/bitcoin/storage/dummy/dummy_store.rb
... ... @@ -72,6 +72,12 @@
72 72 wrap_block(@blk.find {|blk| blk.tx.map(&:hash).include?(tx_hash) })
73 73 end
74 74  
  75 + def get_idx_from_tx_hash(tx_hash)
  76 + return nil unless tx = get_tx(tx_hash)
  77 + return nil unless blk = tx.get_block
  78 + blk.tx.index tx
  79 + end
  80 +
75 81 def get_tx(tx_hash)
76 82 transaction = @tx[tx_hash]
77 83 return nil unless transaction
... ... @@ -96,7 +102,11 @@
96 102 def get_txouts_for_hash160(hash160, unconfirmed = false)
97 103 @tx.values.map(&:out).flatten.map {|o|
98 104 o = wrap_txout(o)
99   - o.hash160 == hash160 ? o : nil
  105 + if o.script.is_multisig?
  106 + o.script.get_multisig_pubkeys.map{|pk| Bitcoin.hash160(pk.unpack("H*")[0])}.include?(hash160) ? o : nil
  107 + else
  108 + o.hash160 == hash160 ? o : nil
  109 + end
100 110 }.compact
101 111 end
102 112  
... ... @@ -143,8 +153,7 @@
143 153 def wrap_txout(output)
144 154 return nil unless output
145 155 tx = @tx.values.find{|t| t.out.include?(output)}
146   - data = {tx_id: tx.hash, tx_idx: tx.out.index(output),
147   - hash160: Bitcoin::Script.new(output.pk_script).get_hash160 }
  156 + data = {tx_id: tx.hash, tx_idx: tx.out.index(output), hash160: output.script.get_hash160}
148 157 txout = Bitcoin::Storage::Models::TxOut.new(self, data)
149 158 [:value, :pk_script_length, :pk_script].each do |attr|
150 159 txout.send("#{attr}=", output.send(attr))
spec/bitcoin/storage/storage_spec.rb
... ... @@ -154,8 +154,7 @@
154 154 @store.get_block_by_tx(@blk.tx[0].hash).should == @blk
155 155 end
156 156  
157   - # TODO calling .is_a? on @store breaks it (?!?)
158   - if @store.class.name =~ /SequelStore/
  157 + unless @store.backend_name == 'utxo'
159 158 describe :transactions do
160 159  
161 160 it "should store tx" do
162 161  
... ... @@ -221,10 +220,11 @@
221 220 end
222 221  
223 222 it "should store multisig tx and index hash160's" do
224   - *keys = Bitcoin::Key.generate, Bitcoin::Key.generate
  223 + keys = Array.new(2) { Bitcoin::Key.generate }
225 224 pk_script = Bitcoin::Script.to_multisig_script(1, keys[0].pub, keys[1].pub)
226 225 txout = P::TxOut.new(1000, pk_script)
227   - @store.store_txout(0, txout, 0)
  226 + @tx.out[0] = txout
  227 + @store.store_tx(@tx, false)
228 228 keys.each do |key|
229 229 hash160 = Bitcoin.hash160(key.pub)
230 230 txouts = @store.get_txouts_for_hash160(hash160, true)