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.



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bc.rb', line 145

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.



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

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.



115
116
117
# File 'lib/bc.rb', line 115

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.



133
134
135
# File 'lib/bc.rb', line 133

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.



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

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.



126
127
128
# File 'lib/bc.rb', line 126

def merkle_root
  @merkle_root
end

#nonceObject (readonly)

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



129
130
131
# File 'lib/bc.rb', line 129

def nonce
  @nonce
end

#targetObject (readonly)

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



140
141
142
# File 'lib/bc.rb', line 140

def target
  @target
end

#transactionsObject (readonly)

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



136
137
138
# File 'lib/bc.rb', line 136

def transactions
  @transactions
end

#versionObject (readonly)

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



122
123
124
# File 'lib/bc.rb', line 122

def version
  @version
end

Instance Method Details

#created_atObject

This is the Time the block was created at.



200
201
202
# File 'lib/bc.rb', line 200

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

#inspectObject



204
205
206
# File 'lib/bc.rb', line 204

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.



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

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.



195
196
197
# File 'lib/bc.rb', line 195

def previous_block
	@bc.get_block(@previous_block_id)
end