Class: CoinOp::Bit::MockChain

Inherits:
Object
  • Object
show all
Includes:
Bitcoin::Builder
Defined in:
lib/coin-op/blockchain/mockchain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ MockChain

Returns a new instance of MockChain.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/coin-op/blockchain/mockchain.rb', line 34

def initialize(config={})
  @key = Bitcoin::Key.new
  @key.generate
  @config = config
  @coinbase_value = 50e8

  if @config[:db]
    @store = Bitcoin::Storage.sequel(
      :db => @config[:db], :log_level => :warn
    )
    @store.connect
    if head = @store.get_head
      @last = head
    else
      self.reset
    end
  else
    self.reset
  end
end

Instance Attribute Details

#coinbase_valueObject (readonly)

Returns the value of attribute coinbase_value.



32
33
34
# File 'lib/coin-op/blockchain/mockchain.rb', line 32

def coinbase_value
  @coinbase_value
end

#keyObject (readonly)

Returns the value of attribute key.



32
33
34
# File 'lib/coin-op/blockchain/mockchain.rb', line 32

def key
  @key
end

#lastObject (readonly)

Returns the value of attribute last.



32
33
34
# File 'lib/coin-op/blockchain/mockchain.rb', line 32

def last
  @last
end

#storeObject (readonly)

Returns the value of attribute store.



32
33
34
# File 'lib/coin-op/blockchain/mockchain.rb', line 32

def store
  @store
end

Instance Method Details

#add(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/coin-op/blockchain/mockchain.rb', line 69

def add(options={})
  if !options[:key]
    raise "Must provide :key"
  end
  options[:bits] ||= Bitcoin.network[:proof_of_work_limit]
  previous_block = options[:previous] || @last

  block = build_block(Bitcoin.decode_compact_bits(options[:bits])) do |b|
    b.time options[:time] if options[:time]
    b.prev_block previous_block
    b.tx do |t|
      t.input {|i| i.coinbase }
      t.output do |o|
        o.value @coinbase_value
        o.script {|s| s.recipient options[:key].addr }
      end
    end
  end
  if options[:transactions]
    options[:transactions].each do |transaction|
      block.tx << transaction
    end
  end

  @store.store_block(block) if @store
  @last = block
  block
end

#mine(key = nil) ⇒ Object



64
65
66
67
# File 'lib/coin-op/blockchain/mockchain.rb', line 64

def mine(key=nil)
  key ||= @key
  block = self.add :key => key, :previous => @last.hash
end

#resetObject



55
56
57
58
59
60
61
62
# File 'lib/coin-op/blockchain/mockchain.rb', line 55

def reset
  @store.reset if @store
  @block0 = self.add :key => @key, :previous => "00"*32

  Bitcoin.network[:genesis_hash] = @block0.hash
  @store.store_block(@block0) if @store
  @last = @block0
end

#transaction(recipients) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/coin-op/blockchain/mockchain.rb', line 98

def transaction(recipients)
  last_transaction = @last.tx.first

  transaction = Builder.build_tx do |t|
    t.input do |i|
      i.prev_out last_transaction
      i.prev_out_index 0
      i.signature_key @key
    end

    recipients.each do |address, value|
      t.output do |o|
        o.value value
        o.script do |s|
          s.type :address
          s.recipient address
        end
      end
    end

  end

end