Permalink
Browse files

fix specs

  • Loading branch information...
1 parent c54b893 commit 966c5ec06825ee4f77b00c868879e441cf2a54e3 @mhanne committed Apr 18, 2012
@@ -1,4 +1,4 @@
-xbmodule Bitcoin
+module Bitcoin
module Protocol
class TxOut
@@ -1,5 +1,5 @@
module Bitcoin::Wallet
-
+
class SimpleCoinSelector
def initialize txouts
@@ -21,7 +21,7 @@ def select(value)
end
nil
end
-
+
end
end
@@ -12,7 +12,7 @@ def initialize storage, keystore, selector
def get_txouts(unconfirmed = false)
txouts = @keystore.keys.map {|k|
@storage.get_txouts_for_address(k[:addr])}.flatten.uniq
- txouts.select! {|o| !!o.get_tx.get_block} unless unconfirmed
+ unconfirmed ? txouts : txouts.select {|o| !!o.get_tx.get_block}
end
def get_balance
@@ -204,10 +204,8 @@
it "should get balance for address" do
@store.store_tx(@tx)
- @store.get_balance("3129d7051d509424d23d533fa2d5258977e822e3").should == 1000000
-
+ @store.get_balance("62e907b15cbf27d5425399ebf6f0fb50ebb88f18").should == 5000000000
@store.get_balance("4580f1b3632948202655fd555fdaaf9b9ef5ac0d").should == 0
- @store.get_balance("f3de26ff7d472d5365e3adafece9bbdcace915a0").should == 200000000
end
it "should store multisig tx and index hash160's" do
@@ -5,20 +5,31 @@
describe Bitcoin::Wallet::SimpleCoinSelector do
- def txout_mock(value, next_in)
+ def txout_mock(value, next_in = true, in_block = true)
+ tx = Mock.new
+ tx.expect(:get_block, in_block)
txout = Mock.new
txout.expect(:value, value)
txout.expect(:get_next_in, next_in)
txout.expect(:get_address, "addr")
+ txout.expect(:get_tx, tx)
txout
end
- it "should select txouts" do
+ it "should select only txouts which have not been spent" do
txouts = [txout_mock(1000, nil), txout_mock(2000, nil),
- txout_mock(1000, true), txout_mock(3000, nil)]
+ txout_mock(1000), txout_mock(3000, nil)]
cs = SimpleCoinSelector.new(txouts)
cs.select(2000).should == txouts[0..1]
cs.select(4000).should == [txouts[0], txouts[1], txouts[3]]
end
+ it "should select only txouts which are in a block" do
+ txouts = [txout_mock(1000, nil, false), txout_mock(2000, nil),
+ txout_mock(1000), txout_mock(3000, nil)]
+ cs = SimpleCoinSelector.new(txouts)
+ cs.select(2000).should == txouts[1..1]
+ cs.select(4000).should == [txouts[1], txouts[3]]
+ end
+
end
@@ -40,12 +40,12 @@
filename = @filename.sub('test1', 'test2')
File.open(filename, 'w') {|f| f.write(@test1.to_json) }
ks = SimpleKeyStore.new(file: filename)
- ks.keys.size.should == 5
+ ks.keys.size.should == 6
File.delete(filename) rescue nil
end
it "should load store" do
- @ks.keys.size.should == 5
+ @ks.keys.size.should == 6
end
it "should save store" do
@@ -65,7 +65,7 @@
it "should delete key" do
@ks.delete(@ks.keys.last[:addr])
- @ks.keys.size.should == 4
+ @ks.keys.size.should == 5
end
it "should get key" do
@@ -111,15 +111,15 @@
it "should store only address" do
k = {:label => 'test6', :addr => @key.addr}
@ks.add_key(k)
- @ks.keys.size.should == 6
+ @ks.keys.size.should == 7
@ks.key('test6').should == k
@ks.key(@key.addr).should == k
end
it "should store only pubkey and addr" do
k = {:label => 'test6', :addr => @key.addr, :pub => @key.pub}
@ks.add_key(k)
- @ks.keys.size.should == 6
+ @ks.keys.size.should == 7
@ks.key('test6').should == k
@ks.key(@key.addr).should == k
end
@@ -4,13 +4,16 @@
include Bitcoin
include Bitcoin::Wallet
-def txout_mock(value, next_in)
+def txout_mock(value, next_in = true, in_block = true)
+ tx = Mock.new
+ tx.expect(:get_block, in_block)
txout = Mock.new
txout.expect(:value, value)
txout.expect(:get_next_in, next_in)
txout.expect(:hash, [value, next_in].hash)
txout.expect(:eql?, false, [1])
txout.expect(:==, false, [1])
+ txout.expect(:get_tx, tx)
end
describe Bitcoin::Wallet::Wallet do
@@ -76,10 +79,20 @@ def new_key
it "should list all addrs with balances" do
@storage.expect(:get_balance, 0, ['dcbc93494b38ae96b14b1cc080d2acb514b7e955'])
- @wallet.list.should == [[@addr, 0]]
+ list = @wallet.list
+ list.size.should == 1
+ list = list[0]
+ list.size.should == 2
+ list[0][:addr].should == "1M89ZeWtmZmATzE3b6PHTBi8c7tGsg5xpo"
+ list[1].should == 0
@storage.expect(:get_balance, 5000, ['dcbc93494b38ae96b14b1cc080d2acb514b7e955'])
- @wallet.list.should == [[@addr, 5000]]
+ list = @wallet.list
+ list.size.should == 1
+ list = list[0]
+ list.size.should == 2
+ list[0][:addr].should == @addr
+ list[1].should == 5000
@storage.verify
end
@@ -99,6 +112,7 @@ def new_key
tx = Mock.new
tx.expect(:binary_hash, "foo")
tx.expect(:out, [txout])
+ tx.expect(:get_block, true)
txout.expect(:get_tx, tx)
txout.expect(:get_address, @addr)
txout.expect(:pk_script,
@@ -167,6 +181,7 @@ def new_key
tx = Mock.new
tx.expect(:binary_hash, "foo")
tx.expect(:out, [txout])
+ tx.expect(:get_block, true)
txout.expect(:get_tx, tx)
txout.expect(:get_address, @addr)
txout.expect(:pk_script, Script.to_address_script(@addr))

0 comments on commit 966c5ec

Please sign in to comment.