spec/bitcoin/spec_helper.rb in bitcoin-ruby-0.0.5 vs spec/bitcoin/spec_helper.rb in bitcoin-ruby-0.0.6

- old
+ new

@@ -18,21 +18,25 @@ rescue LoadError end require 'bitcoin' +def fixtures_path(relative_path) + File.join(File.dirname(__FILE__), 'fixtures', relative_path) +end + def fixtures_file(relative_path) - basedir = File.join(File.dirname(__FILE__), 'fixtures') - Bitcoin::Protocol.read_binary_file( File.join(basedir, relative_path) ) + Bitcoin::Protocol.read_binary_file( fixtures_path(relative_path) ) end + include Bitcoin::Builder # create block for given +prev+ block # if +store+ is true, save it to @store # accepts an array of +tx+ callbacks -def create_block prev, store = true, tx = [], key = Bitcoin::Key.new, coinbase_value = 50e8, opts = {} +def create_block prev, store = true, tx = [], key = Bitcoin::Key.generate, coinbase_value = 50e8, opts = {} opts[:bits] ||= Bitcoin.network[:proof_of_work_limit] block = build_block(Bitcoin.decode_compact_bits(opts[:bits])) do |b| b.time opts[:time] if opts[:time] b.prev_block prev b.tx do |t| @@ -45,12 +49,12 @@ block end # create transaction given builder +tx+ # +outputs+ is an array of [value, key] pairs -def create_tx(tx, prev_tx, prev_out_index, outputs) - tx.input {|i| i.prev_out prev_tx; i.prev_out_index prev_out_index; i.signature_key @key } +def create_tx(tx, prev_tx, prev_out_index, outputs, key = @key) + tx.input {|i| i.prev_out prev_tx; i.prev_out_index prev_out_index; i.signature_key key } outputs.each do |value, key| tx.output {|o| o.value value; o.script {|s| s.recipient key.addr } } end end @@ -88,11 +92,16 @@ :dns_seeds => [], :genesis_hash => "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943", :proof_of_work_limit => 553713663, :alert_pubkeys => [], :known_nodes => [], - :checkpoints => {} + :checkpoints => {}, + :min_tx_fee => 10_000, + :min_relay_tx_fee => 10_000, + :free_tx_bytes => 1_000, + :dust => 1_000_000, + :per_dust_fee => false, } begin require 'bacon' rescue LoadError @@ -120,6 +129,25 @@ end db = Sequel.connect(uri) db.drop_table(*db.tables, cascade: true) end Bitcoin::Storage.send(backend, conf.merge(db: uri, log_level: :warn)) +end + + +class Time + class << self + alias_method :real_new, :new + alias_method :new, :now + def now; @time || real_new; end + def freeze(time = nil) + begin + prev = @time + @time = time || now + yield + ensure + @time = prev + end + end + def frozen?; !@time.nil?; end + end end