Class: Bitcoin::Block

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

Overview

This class represents a block in the Bitcoin block chain. (c.f. en.bitcoin.it/wiki/Block and en.bitcoin.it/wiki/Block_hashing_algorithm)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bc, block_id) ⇒ Block

bc is a Bitcoin::Client instance. block_id is our unique (String) block ID. We raise UnknownBitcoinBlock if the bitcoind doesn’t know about a block with that ID.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bc.rb', line 129

def initialize(bc, block_id)
	@bc = bc

	unless @bc.is_a?(Bitcoin::Client)
		raise TypeError, "bc must be a Bitcoin::Client (#{@bc.class} given)"
	end

	unless block_id.is_a?(String)
		raise TypeError, "block_id must be a String (#{block_id.class} given)"
	end

	begin
		block_data = @bc.jr.getblock(block_id)
	rescue Jr::ServerError => ex
		if ex.code == -5
			raise UnknownBlock, block_id
		else
			raise
		end
	end

	{
		block_id: :hash,
		height: :height,
		version: :version,
		merkle_root: :merkleroot,
		created_at_unix_time: :time,
		nonce: :nonce,
		difficulty: :difficulty,
		transaction_ids: :tx,
		previous_block_id: :nextblockhash,
		next_block_id: :previoushblockhash
	}.each do |our_attr, block_data_key|
		instance_variable_set(
			"@#{our_attr}",
			block_data[block_data_key.to_s].freeze
		)
	end

	@transactions ||= [].freeze
end

Instance Attribute Details

#bcObject (readonly)

This is the Bitcoin::Client instance we are connected to.



95
96
97
# File 'lib/bc.rb', line 95

def bc
  @bc
end

#block_idObject (readonly)

This is the unique ID of the block. It is a String beginning with a number of ‘0’s.



99
100
101
# File 'lib/bc.rb', line 99

def block_id
  @block_id
end

#difficultyObject (readonly)

This is a Float representing how difficult it was to generate the block. It increases logarithmically in proportion to difficulty.



117
118
119
# File 'lib/bc.rb', line 117

def difficulty
  @difficulty
end

#heightObject (readonly)

This is the order of this block in the block chain, i.e. the number of blocks that came before it.



103
104
105
# File 'lib/bc.rb', line 103

def height
  @height
end

#merkle_rootObject (readonly)

This is a base 16 String representation of a SHA-256 hash generated from all the transactions in the block.



110
111
112
# File 'lib/bc.rb', line 110

def merkle_root
  @merkle_root
end

#nonceObject (readonly)

This is a 32-bit Fixnum used in order to get a @block_id matching @target.



113
114
115
# File 'lib/bc.rb', line 113

def nonce
  @nonce
end

#targetObject (readonly)

This is a compact representation of the prefix this block was attempting to match.



124
125
126
# File 'lib/bc.rb', line 124

def target
  @target
end

#transactionsObject (readonly)

This is an Array of every Transaction that is part of the block.



120
121
122
# File 'lib/bc.rb', line 120

def transactions
  @transactions
end

#versionObject (readonly)

This is the block version. Currently, it should be 1.



106
107
108
# File 'lib/bc.rb', line 106

def version
  @version
end

Instance Method Details

#created_atObject

This is the Time the block was created at.



184
185
186
# File 'lib/bc.rb', line 184

def created_at
	@created_at ||= Time.at(@created_at_unix_time).utc.freeze
end

#inspectObject



188
189
190
# File 'lib/bc.rb', line 188

def inspect
	"#<Bitcoin::Block #{@block_id}>"
end

#next_blockObject

This is the Block created immediately after this one, or nil if this is the most recent block.



173
174
175
# File 'lib/bc.rb', line 173

def next_block
	@bc.get_block(@next_block_id)
end

#previous_blockObject

This is the Block created prior to this one, or nil if this is the origin block.



179
180
181
# File 'lib/bc.rb', line 179

def previous_block
	@bc.get_block(@previous_block_id)
end