spec/bitcoin/bitcoin_spec.rb in bitcoin-ruby-0.0.6 vs spec/bitcoin/bitcoin_spec.rb in bitcoin-ruby-0.0.7

- old
+ new

@@ -161,10 +161,18 @@ success = false if Bitcoin.valid_address?(addr[0]) != true } success.should == true end + it 'validate bitcoin public key' do + key = Bitcoin::Key.generate + Bitcoin.valid_pubkey?(key.pub_compressed).should == true + Bitcoin.valid_pubkey?(key.pub_uncompressed).should == true + Bitcoin.valid_pubkey?(key.addr).should == false + Bitcoin.valid_pubkey?(key.priv).should == false + end + it 'validate p2sh address' do Bitcoin.network = :testnet Bitcoin.valid_address?("2MyLngQnhzjzatKsB7XfHYoP9e2XUXSiBMM").should == true Bitcoin.network = :bitcoin Bitcoin.valid_address?("3CkxTG25waxsmd13FFgRChPuGYba3ar36B").should == true @@ -316,10 +324,17 @@ it 'should not allow duplicate hash in merkle trees' do Bitcoin.hash_mrkl_tree(["aa", "bb", "cc"]).last.should != Bitcoin.hash_mrkl_tree(["aa", "bb", "cc", "cc"]).last end + + it 'return a value even if a merkle branch is empty' do + branch = [] + mrkl_index = 0 + target = "089b911f5e471c0e1800f3384281ebec5b372fbb6f358790a92747ade271ccdf" + Bitcoin.mrkl_branch_root(branch.map(&:hth), target, mrkl_index).should == target + end it 'nonce compact bits to bignum hex' do Bitcoin.decode_compact_bits( "1b00b5ac".to_i(16) ).index(/[^0]/).should == 12 Bitcoin.decode_compact_bits( "1b00b5ac".to_i(16) ).to_i(16).should == "000000000000b5ac000000000000000000000000000000000000000000000000".to_i(16) @@ -337,10 +352,13 @@ target = 476399191 # from block 40,320 Bitcoin.decode_compact_bits(target).should == "0000000065465700000000000000000000000000000000000000000000000000" Bitcoin.encode_compact_bits( Bitcoin.decode_compact_bits( target ) ).should == target + + #Bitcoin.network = :dogecoin + #Bitcoin.decode_compact_bits( "01fedcba".to_i(16) ).to_i(16).should == -0x7e end it '#block_hash' do # block 0 n_tx: 1 prev_block="0000000000000000000000000000000000000000000000000000000000000000" @@ -419,16 +437,17 @@ "040b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a7447b5f0bba9e01e6fe4735c8383e6e7a3347a0fd72381b8f797a19f694054e5a69"], ["1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs", "12b004fff7f4b69ef8650e767f18f11ede158148b425660723b9f9a66e61f747", "030b4c866585dd868a9d62348a9cd008d6a312937048fff31670e7e920cfc7a744"] ].each{|address, privkey, pubkey| key = Bitcoin.open_key(privkey) - 16.times.all?{|n| + 16.times.each { |n| #10_000.times.all?{|n| # puts 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip if (n % 1_000) == 0 s = Bitcoin.sign_message(key.private_key_hex, key.public_key_hex, "Very secret message %d: 11" % n) - Bitcoin.verify_message(s['address'], s['signature'], s['message']) - }.should == true + Bitcoin.verify_message(s['address'], 'invalid-signature', s['message']).should == false + Bitcoin.verify_message(s['address'], s['signature'], s['message']).should == true + } } end rescue LoadError end @@ -484,9 +503,19 @@ Bitcoin.block_next_retarget(189504).should == 191519 end it '#block_difficulty' do Bitcoin.block_difficulty(436835377).should == "1751454.5353407" + end + + it 'should calculate retarget difficulty' do + prev_height = 201599 + prev_block_time = 1349227021 + prev_block_bits = 0x1a05db8b + last_retarget_time = 1348092851 + new_difficulty = Bitcoin.block_new_target(prev_height, prev_block_time, prev_block_bits, last_retarget_time) + + Bitcoin.decode_compact_bits(new_difficulty.should) == Bitcoin.decode_compact_bits(0x1a057e08) end it '#block_hashes_to_win' do Bitcoin.block_hashes_to_win(436835377).should == 7522554734795001 end