spec/bitcoin/storage/storage_spec.rb in bitcoin-ruby-0.0.5 vs spec/bitcoin/storage/storage_spec.rb in bitcoin-ruby-0.0.6
- old
+ new
@@ -22,11 +22,11 @@
next unless storage = setup_db(*options)
describe "Storage::Backends::#{options[0].to_s.capitalize}Store (#{options[1]})" do
before do
- class Bitcoin::Validation::Block; def difficulty; true; end; end
+ Bitcoin.network[:no_difficulty] = true
Bitcoin.network[:proof_of_work_limit] = Bitcoin.encode_compact_bits("ff"*32)
@store = storage
def @store.in_sync?; true; end
@store.reset
@@ -44,16 +44,11 @@
@blk = P::Block.new(fixtures_file('testnet/block_4.bin'))
@tx = P::Tx.new(fixtures_file('rawtx-03.bin'))
end
after do
- class Bitcoin::Validation::Block
- def difficulty
- return true if Bitcoin.network_name == :testnet3
- block.bits == next_bits_required || [block.bits, next_bits_required]
- end
- end
+ Bitcoin.network.delete :no_difficulty
end
it "should get backend name" do
@store.backend_name.should == options[0].to_s
end
@@ -152,18 +147,23 @@
it "should get block for tx" do
@store.store_block(@blk)
@store.get_block_by_tx(@blk.tx[0].hash).should == @blk
end
- # TODO calling .is_a? on @store breaks it (?!?)
- if @store.class.name =~ /SequelStore/
+ it "should get block id for tx id" do
+ @store.store_block(@blk)
+ tx = @store.get_tx(@blk.tx[0].hash)
+ @store.get_block_id_for_tx_id(tx.id).should == @store.get_block(@blk.hash).id
+ end
+
+ unless @store.backend_name == 'utxo'
describe :transactions do
it "should store tx" do
@store.store_tx(@tx, false).should != false
end
-
+
it "should not store tx if already stored and return existing id" do
id = @store.store_tx(@tx, false)
@store.store_tx(@tx, false).should == id
end
@@ -219,17 +219,18 @@
@store.store_tx(tx, false)
@store.get_tx(outpoint_tx.hash).out[0].get_next_in.should == tx.in[0]
end
it "should store multisig tx and index hash160's" do
- *keys = Bitcoin::Key.generate, Bitcoin::Key.generate
+ keys = Array.new(2) { Bitcoin::Key.generate }
pk_script = Bitcoin::Script.to_multisig_script(1, keys[0].pub, keys[1].pub)
txout = P::TxOut.new(1000, pk_script)
- @store.store_txout(0, txout, 0)
+ @tx.out[0] = txout
+ @store.store_tx(@tx, false)
keys.each do |key|
hash160 = Bitcoin.hash160(key.pub)
- txouts = @store.get_txouts_for_hash160(hash160, true)
+ txouts = @store.get_txouts_for_hash160(hash160, :hash160, true)
txouts.size.should == 1
txouts[0].pk_script.should == txout.pk_script
end
end
@@ -262,21 +263,63 @@
@store.get_txouts_for_pk_script(script)
.should == [@blk.tx[0].out[0]]
end
it "should get txouts for hash160" do
- @store.get_txouts_for_hash160(@key2.hash160, true)
+ @store.get_txouts_for_hash160(@key2.hash160, :hash160, true)
.should == [@block.tx[1].out[0]]
end
it "should get txouts for address" do
@store.get_txouts_for_address(@key2.addr, true)
.should == [@block.tx[1].out[0]]
end
+ it "should get txouts for txin" do
+ prev_tx = @block.tx[0]
+ tx = build_tx { |t| create_tx(t, prev_tx, 0, [[prev_tx.out[0].value, Bitcoin::Key.generate]], @key) }
+ @store.get_txout_for_txin(tx.in[0]).should == prev_tx.out[0]
+ end
+
+ it "should get unspent txouts for address" do
+ @store.get_unspent_txouts_for_address(@key2.addr, true)
+ .should == [@block.tx[1].out[0]]
+ @block2 = create_block @block.hash, true, [->(t) {
+ create_tx(t, @block.tx[1], 0, [[20, @key2]], @key2) }], @key
+ @store.get_unspent_txouts_for_address(@key2.addr, true)
+ .should == [@block2.tx[1].out[0]]
+ end
+
it "should get balance for address" do
- @store.get_balance(@key.addr).should == 0
+ @store.get_balance(@key2.addr).should == 50
@store.get_balance(@key2.hash160).should == 50
+ end
+
+ it "should get txouts for p2sh address (and not confuse regular and p2sh-type hash160" do
+ block = create_block(@block.hash, false, [], @key)
+
+ tx = build_tx do |t|
+ t.input {|i| i.prev_out(@block.tx[1], 0); i.signature_key(@key2) }
+ t.output do |o|
+ o.value 10
+ o.to @key2.hash160, :p2sh
+ end
+ t.output do |o|
+ o.value 10
+ o.to @key2.addr
+ end
+ end
+ block.tx << tx; block.recalc_mrkl_root; block.recalc_block_hash
+
+ @store.store_block(block)
+
+ p2sh_address = Bitcoin.hash160_to_p2sh_address(@key2.hash160)
+ o1 = @store.get_unspent_txouts_for_address(@key2.addr)
+ o2 = @store.get_unspent_txouts_for_address(p2sh_address)
+
+ o1.size.should == 1
+ o2.size.should == 1
+ o1.should != o2
end
end
describe "validation" do