Class: Bitcoin::Protocol::AuxPow

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

Overview

Auxiliary Proof-of-Work for merge-mined blockchains

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ AuxPow

Returns a new instance of AuxPow.



30
31
32
# File 'lib/bitcoin/protocol/aux_pow.rb', line 30

def initialize(data)
  parse_data (data) if data
end

Instance Attribute Details

#aux_branchObject

Merkle branches linking this aux chains to the aux root



22
23
24
# File 'lib/bitcoin/protocol/aux_pow.rb', line 22

def aux_branch
  @aux_branch
end

#aux_indexObject

Index of “this” block chain in the aux chain list



25
26
27
# File 'lib/bitcoin/protocol/aux_pow.rb', line 25

def aux_index
  @aux_index
end

#block_hashObject

Hash of the block header



13
14
15
# File 'lib/bitcoin/protocol/aux_pow.rb', line 13

def block_hash
  @block_hash
end

#branchObject

Merkle branches to bring the transaction to the block’s merkle root



16
17
18
# File 'lib/bitcoin/protocol/aux_pow.rb', line 16

def branch
  @branch
end

#mrkl_indexObject

Index of this transaction in the merkle tree



19
20
21
# File 'lib/bitcoin/protocol/aux_pow.rb', line 19

def mrkl_index
  @mrkl_index
end

#parent_blockObject

Parent block header



28
29
30
# File 'lib/bitcoin/protocol/aux_pow.rb', line 28

def parent_block
  @parent_block
end

#txObject

Coinbase transaction linking the aux to its parent block



10
11
12
# File 'lib/bitcoin/protocol/aux_pow.rb', line 10

def tx
  @tx
end

Class Method Details

.from_hash(h) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bitcoin/protocol/aux_pow.rb', line 96

def self.from_hash h
  aux_pow = new(nil)
  aux_pow.instance_eval do
    @tx = P::Tx.from_hash(h['tx'])
    @block_hash = h['block_hash'].htb
    @branch = h['branch'].map(&:htb)
    @mrkl_index = h['mrkl_index']
    @aux_branch = h['aux_branch'].map(&:htb)
    @aux_index = h['aux_index']
    @parent_block = P::Block.from_hash(h['parent_block'])
  end
  aux_pow
end

Instance Method Details

#parse_data(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bitcoin/protocol/aux_pow.rb', line 34

def parse_data(data)
  @tx = P::Tx.new(nil)
  payload = @tx.parse_data(data)
  @block_hash, payload = payload.unpack("a32a*")

  branch_count, payload = P.unpack_var_int(payload)
  @branch = []
  branch_count.times {
    b, payload = payload.unpack("a32a*")
    @branch << b
  }
  @mrkl_index, payload = payload.unpack("Ia*")

  @aux_branch = []
  aux_branch_count, payload = P.unpack_var_int(payload)
  aux_branch_count.times {
    b, payload = payload.unpack("a32a*")
    @aux_branch << b
  }

  @aux_index, payload = payload.unpack("Ia*")
  block, payload = payload.unpack("a80a*")
  @parent_block = P::Block.new(block)

  payload
end

#parse_data_from_io(data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bitcoin/protocol/aux_pow.rb', line 61

def parse_data_from_io(data)
  @tx = P::Tx.new(nil)
  @tx.parse_data_from_io(data)

  @block_hash = data.read(32)
  branch_count = P.unpack_var_int_from_io(data)
  @branch = []
  branch_count.times{ @branch << data.read(32) }
  @mrkl_index = data.read(4).unpack("I")[0]

  @aux_branch = []
  aux_branch_count = P.unpack_var_int_from_io(data)
  aux_branch_count.times{ @aux_branch << data.read(32) }

  @aux_index = data.read(4).unpack("I")[0]
  block = data.read(80)
  @parent_block = P::Block.new(block)

  data
end

#to_hashObject



110
111
112
113
114
115
116
117
118
# File 'lib/bitcoin/protocol/aux_pow.rb', line 110

def to_hash
  { 'tx' => @tx.to_hash,
    'block_hash' => @block_hash.hth,
    'branch' => @branch.map(&:hth),
    'mrkl_index' => @mrkl_index,
    'aux_branch' => @aux_branch.map(&:hth),
    'aux_index' => @aux_index,
    'parent_block' => @parent_block.to_hash }
end

#to_payloadObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bitcoin/protocol/aux_pow.rb', line 83

def to_payload
  payload = @tx.to_payload
  payload << @block_hash
  payload << P.pack_var_int(@branch.count)
  payload << @branch.join
  payload << [@mrkl_index].pack("I")
  payload << P.pack_var_int(@aux_branch.count)
  payload << @aux_branch.join
  payload << [@aux_index].pack("I")
  payload << @parent_block.to_payload
  payload
end