Class: Bitcoin::Builder::BlockBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/builder.rb

Overview

DSL to create a Bitcoin::Protocol::Block used by Builder#create_block.

block = blk("00".ljust(32, 'f')) do |b|
  b.prev_block "\x00"*32
  b.tx do |t|
    t.input {|i| i.coinbase }
    t.output do |o|
      o.value 5000000000;
      o.to Bitcoin::Key.generate.addr
    end
  end
end

See Bitcoin::Builder::TxBuilder for details on building transactions.

Instance Method Summary collapse

Constructor Details

#initializeBlockBuilder

Returns a new instance of BlockBuilder.



49
50
51
# File 'lib/bitcoin/builder.rb', line 49

def initialize
  @block = P::Block.new(nil)
end

Instance Method Details

#block(target) ⇒ Object

create the block according to values specified via DSL.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bitcoin/builder.rb', line 76

def block target
  @block.ver = @version || 1
  @block.prev_block = @prev_block.htb.reverse
  @block.mrkl_root = @mrkl_root
  @block.time = @time || Time.now.to_i
  @block.nonce = 0
  @block.mrkl_root = Bitcoin.hash_mrkl_tree(@block.tx.map(&:hash)).last.htb.reverse
  find_hash(target)
  block = P::Block.new(@block.to_payload)
  raise "Payload Error"  unless block.to_payload == @block.to_payload
  block
end

#prev_block(hash) ⇒ Object

set the hash of the previous block.



59
60
61
# File 'lib/bitcoin/builder.rb', line 59

def prev_block hash
  @prev_block = hash
end

#time(time) ⇒ Object

set the block timestamp (defaults to current time).



64
65
66
# File 'lib/bitcoin/builder.rb', line 64

def time time
  @time = time
end

#tx(tx = nil) ⇒ Object

add transactions to the block (see TxBuilder).



69
70
71
72
73
# File 'lib/bitcoin/builder.rb', line 69

def tx tx = nil
  tx ||= ( c = TxBuilder.new; yield c; c.tx )
  @block.tx << tx
  tx
end

#version(v) ⇒ Object

specify block version. this is usually not necessary. defaults to 1.



54
55
56
# File 'lib/bitcoin/builder.rb', line 54

def version v
  @version = v
end